Skip to content

Commit 54833c5

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 2d8c136 commit 54833c5

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
@@ -508,6 +508,9 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
508508
i++;
509509
nbuckets = (1 << i);
510510

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

src/backend/optimizer/plan/planmain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ query_planner(PlannerInfo *root, List *tlist,
364364
* can be divided by the number of tuples.
365365
*/
366366
if (tuple_fraction >= 1.0)
367-
tuple_fraction /= final_rel->rows;
367+
tuple_fraction /= clamp_row_est(final_rel->rows);
368368
}
369369

370370
/*

src/backend/optimizer/plan/planner.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,11 +1116,14 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
11161116
/*
11171117
* Extract rowcount and width estimates for possible use in grouping
11181118
* decisions. Beware here of the possibility that
1119-
* cheapest_path->parent is NULL (ie, there is no FROM clause).
1119+
* cheapest_path->parent is NULL (ie, there is no FROM clause). Also,
1120+
* if the final rel has been proven dummy, its rows estimate will be
1121+
* zero; clamp it to one to avoid zero-divide in subsequent
1122+
* calculations.
11201123
*/
11211124
if (cheapest_path->parent)
11221125
{
1123-
path_rows = cheapest_path->parent->rows;
1126+
path_rows = clamp_row_est(cheapest_path->parent->rows);
11241127
path_width = cheapest_path->parent->width;
11251128
}
11261129
else

src/backend/utils/adt/selfuncs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4337,8 +4337,8 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
43374337
*
43384338
* vardata: results of examine_variable
43394339
*
4340-
* NB: be careful to produce an integral result, since callers may compare
4341-
* the result to exact integer counts.
4340+
* NB: be careful to produce a positive integral result, since callers may
4341+
* compare the result to exact integer counts, or might divide by it.
43424342
*/
43434343
double
43444344
get_variable_numdistinct(VariableStatData *vardata)
@@ -4412,7 +4412,7 @@ get_variable_numdistinct(VariableStatData *vardata)
44124412
* If we had an absolute estimate, use that.
44134413
*/
44144414
if (stadistinct > 0.0)
4415-
return stadistinct;
4415+
return clamp_row_est(stadistinct);
44164416

44174417
/*
44184418
* Otherwise we need to get the relation size; punt if not available.
@@ -4427,14 +4427,14 @@ get_variable_numdistinct(VariableStatData *vardata)
44274427
* If we had a relative estimate, use that.
44284428
*/
44294429
if (stadistinct < 0.0)
4430-
return floor((-stadistinct * ntuples) + 0.5);
4430+
return clamp_row_est(-stadistinct * ntuples);
44314431

44324432
/*
44334433
* With no data, estimate ndistinct = ntuples if the table is small, else
44344434
* use default.
44354435
*/
44364436
if (ntuples < DEFAULT_NUM_DISTINCT)
4437-
return ntuples;
4437+
return clamp_row_est(ntuples);
44384438

44394439
return DEFAULT_NUM_DISTINCT;
44404440
}

0 commit comments

Comments
 (0)