postgres.git / summary / log / commit / refs
commit 54833c5e772ee0e8461269e1b40ab6b9fae4b1cd
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu Jul 30 16:11:23 2015 +0000
First released in: 9.6.0, 9.0.23, 9.1.19, 9.2.14, 9.3.10, 9.4.5, 9.5.0
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.
src/backend/executor/nodeHash.c | 3 +++
src/backend/optimizer/plan/planmain.c | 2 +-
src/backend/optimizer/plan/planner.c | 7 +++++--
src/backend/utils/adt/selfuncs.c | 10 +++++-----
4 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c
index fb6cab9451a..1597211ad89 100644
--- a/src/backend/executor/nodeHash.c
+++ b/src/backend/executor/nodeHash.c
@@ -508,6 +508,9 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
i++;
nbuckets = (1 << i);
+ Assert(nbuckets > 0);
+ Assert(nbatch > 0);
+
*numbuckets = nbuckets;
*numbatches = nbatch;
}
diff --git a/src/backend/optimizer/plan/planmain.c b/src/backend/optimizer/plan/planmain.c
index 6a5eb6e2625..78b16f52931 100644
--- a/src/backend/optimizer/plan/planmain.c
+++ b/src/backend/optimizer/plan/planmain.c
@@ -364,7 +364,7 @@ query_planner(PlannerInfo *root, List *tlist,
* can be divided by the number of tuples.
*/
if (tuple_fraction >= 1.0)
- tuple_fraction /= final_rel->rows;
+ tuple_fraction /= clamp_row_est(final_rel->rows);
}
/*
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index c959e1d4aba..ec923fbb773 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -1116,11 +1116,14 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
/*
* Extract rowcount and width estimates for possible use in grouping
* decisions. Beware here of the possibility that
- * cheapest_path->parent is NULL (ie, there is no FROM clause).
+ * cheapest_path->parent is NULL (ie, there is no FROM clause). Also,
+ * if the final rel has been proven dummy, its rows estimate will be
+ * zero; clamp it to one to avoid zero-divide in subsequent
+ * calculations.
*/
if (cheapest_path->parent)
{
- path_rows = cheapest_path->parent->rows;
+ path_rows = clamp_row_est(cheapest_path->parent->rows);
path_width = cheapest_path->parent->width;
}
else
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index b6cbbc9c078..032116acb3d 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -4337,8 +4337,8 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
*
* vardata: results of examine_variable
*
- * NB: be careful to produce an integral result, since callers may compare
- * the result to exact integer counts.
+ * NB: be careful to produce a positive integral result, since callers may
+ * compare the result to exact integer counts, or might divide by it.
*/
double
get_variable_numdistinct(VariableStatData *vardata)
@@ -4412,7 +4412,7 @@ get_variable_numdistinct(VariableStatData *vardata)
* If we had an absolute estimate, use that.
*/
if (stadistinct > 0.0)
- return stadistinct;
+ return clamp_row_est(stadistinct);
/*
* Otherwise we need to get the relation size; punt if not available.
@@ -4427,14 +4427,14 @@ get_variable_numdistinct(VariableStatData *vardata)
* If we had a relative estimate, use that.
*/
if (stadistinct < 0.0)
- return floor((-stadistinct * ntuples) + 0.5);
+ return clamp_row_est(-stadistinct * ntuples);
/*
* With no data, estimate ndistinct = ntuples if the table is small, else
* use default.
*/
if (ntuples < DEFAULT_NUM_DISTINCT)
- return ntuples;
+ return clamp_row_est(ntuples);
return DEFAULT_NUM_DISTINCT;
}
[parent: 2d8c136e7451]