Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8693ebe

Browse files
committed
Avoid some zero-divide hazards in the planner.
Although I think on all modern machines floating division by zeroresults in Infinity not SIGFPE, we still don't want infinitiesrunning around in the planner's costing estimates; too much riskof that leading to insane behavior.grouping_planner() failed to consider the possibility that final_relmight be known dummy and hence have zero rowcount. (I wonder if itwould be better to set a rows estimate of 1 for dummy relations?But at least in the back branches, changing this convention seemslike a bad idea, so I'll leave that for another day.)Make certain that get_variable_numdistinct() produces a nonzero result.The case that can be shown to be broken is with stadistinct < 0.0 andsmall ntuples; we did not prevent the result from rounding to zero.For good luck I applied clamp_row_est() to all the nonconstant returnvalues.In ExecChooseHashTableSize(), Assert that we compute positive nbucketsand nbatch. I know of no reason to think this isn't the case, but itseems like a good safety check.Per reports from Piotr Stefaniak. Back-patch to all active branches.
1 parent5515ec0 commit8693ebe

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

‎src/backend/executor/nodeHash.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,9 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
542542
nbatch <<=1;
543543
}
544544

545+
Assert(nbuckets>0);
546+
Assert(nbatch>0);
547+
545548
*numbuckets=nbuckets;
546549
*numbatches=nbatch;
547550
}

‎src/backend/optimizer/plan/planner.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,9 +1536,11 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
15361536
standard_qp_callback,&qp_extra);
15371537

15381538
/*
1539-
* Extract rowcount and width estimates for use below.
1539+
* Extract rowcount and width estimates for use below. If final_rel
1540+
* has been proven dummy, its rows estimate will be zero; clamp it to
1541+
* one to avoid zero-divide in subsequent calculations.
15401542
*/
1541-
path_rows=final_rel->rows;
1543+
path_rows=clamp_row_est(final_rel->rows);
15421544
path_width=final_rel->width;
15431545

15441546
/*

‎src/backend/utils/adt/selfuncs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4632,8 +4632,8 @@ examine_simple_variable(PlannerInfo *root, Var *var,
46324632
* *isdefault: set to TRUE if the result is a default rather than based on
46334633
* anything meaningful.
46344634
*
4635-
* NB: be careful to produceanintegral result, since callers may compare
4636-
* the result to exact integer counts.
4635+
* NB: be careful to producea positiveintegral result, since callers may
4636+
*comparethe result to exact integer counts, or might divide by it.
46374637
*/
46384638
double
46394639
get_variable_numdistinct(VariableStatData*vardata,bool*isdefault)
@@ -4709,7 +4709,7 @@ get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
47094709
* If we had an absolute estimate, use that.
47104710
*/
47114711
if (stadistinct>0.0)
4712-
returnstadistinct;
4712+
returnclamp_row_est(stadistinct);
47134713

47144714
/*
47154715
* Otherwise we need to get the relation size; punt if not available.
@@ -4730,15 +4730,15 @@ get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
47304730
* If we had a relative estimate, use that.
47314731
*/
47324732
if (stadistinct<0.0)
4733-
returnfloor((-stadistinct*ntuples)+0.5);
4733+
returnclamp_row_est(-stadistinct*ntuples);
47344734

47354735
/*
47364736
* With no data, estimate ndistinct = ntuples if the table is small, else
47374737
* use default. We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
47384738
* that the behavior isn't discontinuous.
47394739
*/
47404740
if (ntuples<DEFAULT_NUM_DISTINCT)
4741-
returnntuples;
4741+
returnclamp_row_est(ntuples);
47424742

47434743
*isdefault= true;
47444744
returnDEFAULT_NUM_DISTINCT;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp