Skip to content

Commit caae9f7

Browse files
committed
Avoid some zero-divide hazards in the planner.
Although I think on all modern machines floating division by zero results in Infinity not SIGFPE, we still don't want infinities running around in the planner's costing estimates; too much risk of that leading to insane behavior. grouping_planner() failed to consider the possibility that final_rel might be known dummy and hence have zero rowcount. (I wonder if it would be better to set a rows estimate of 1 for dummy relations? But at least in the back branches, changing this convention seems like 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 and small ntuples; we did not prevent the result from rounding to zero. For good luck I applied clamp_row_est() to all the nonconstant return values. In ExecChooseHashTableSize(), Assert that we compute positive nbuckets and nbatch. I know of no reason to think this isn't the case, but it seems like a good safety check. Per reports from Piotr Stefaniak. Back-patch to all active branches.
1 parent 23e7ee9 commit caae9f7

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/backend/executor/nodeHash.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
509509
i++;
510510
nbuckets = (1 << i);
511511

512+
Assert(nbuckets > 0);
513+
Assert(nbatch > 0);
514+
512515
*numbuckets = nbuckets;
513516
*numbatches = nbatch;
514517
}

src/backend/optimizer/plan/planmain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ query_planner(PlannerInfo *root, List *tlist,
356356
* can be divided by the number of tuples.
357357
*/
358358
if (tuple_fraction >= 1.0)
359-
tuple_fraction /= final_rel->rows;
359+
tuple_fraction /= clamp_row_est(final_rel->rows);
360360
}
361361

362362
/*

src/backend/optimizer/plan/planner.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,11 +1295,14 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
12951295
/*
12961296
* Extract rowcount and width estimates for possible use in grouping
12971297
* decisions. Beware here of the possibility that
1298-
* cheapest_path->parent is NULL (ie, there is no FROM clause).
1298+
* cheapest_path->parent is NULL (ie, there is no FROM clause). Also,
1299+
* if the final rel has been proven dummy, its rows estimate will be
1300+
* zero; clamp it to one to avoid zero-divide in subsequent
1301+
* calculations.
12991302
*/
13001303
if (cheapest_path->parent)
13011304
{
1302-
path_rows = cheapest_path->parent->rows;
1305+
path_rows = clamp_row_est(cheapest_path->parent->rows);
13031306
path_width = cheapest_path->parent->width;
13041307
}
13051308
else

src/backend/utils/adt/selfuncs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4622,8 +4622,8 @@ examine_simple_variable(PlannerInfo *root, Var *var,
46224622
* *isdefault: set to TRUE if the result is a default rather than based on
46234623
* anything meaningful.
46244624
*
4625-
* NB: be careful to produce an integral result, since callers may compare
4626-
* the result to exact integer counts.
4625+
* NB: be careful to produce a positive integral result, since callers may
4626+
* compare the result to exact integer counts, or might divide by it.
46274627
*/
46284628
double
46294629
get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
@@ -4699,7 +4699,7 @@ get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
46994699
* If we had an absolute estimate, use that.
47004700
*/
47014701
if (stadistinct > 0.0)
4702-
return stadistinct;
4702+
return clamp_row_est(stadistinct);
47034703

47044704
/*
47054705
* Otherwise we need to get the relation size; punt if not available.
@@ -4720,15 +4720,15 @@ get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
47204720
* If we had a relative estimate, use that.
47214721
*/
47224722
if (stadistinct < 0.0)
4723-
return floor((-stadistinct * ntuples) + 0.5);
4723+
return clamp_row_est(-stadistinct * ntuples);
47244724

47254725
/*
47264726
* With no data, estimate ndistinct = ntuples if the table is small, else
47274727
* use default. We use DEFAULT_NUM_DISTINCT as the cutoff for "small" so
47284728
* that the behavior isn't discontinuous.
47294729
*/
47304730
if (ntuples < DEFAULT_NUM_DISTINCT)
4731-
return ntuples;
4731+
return clamp_row_est(ntuples);
47324732

47334733
*isdefault = true;
47344734
return DEFAULT_NUM_DISTINCT;

0 commit comments

Comments
 (0)