aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/backend/executor/nodeHash.c3
-rw-r--r--src/backend/optimizer/plan/planmain.c2
-rw-r--r--src/backend/optimizer/plan/planner.c7
-rw-r--r--src/backend/utils/adt/selfuncs.c10
4 files changed, 14 insertions, 8 deletions
diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c
index 17de5e95d2d..dbecb193ab4 100644
--- a/src/backend/executor/nodeHash.c
+++ b/src/backend/executor/nodeHash.c
@@ -510,6 +510,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 009841d9dd3..e2967d1cf61 100644
--- a/src/backend/optimizer/plan/planmain.c
+++ b/src/backend/optimizer/plan/planmain.c
@@ -365,7 +365,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 3a812877d62..e062cabf7a2 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -1134,11 +1134,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 b90d2d7412c..6704e7f049f 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -4376,8 +4376,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)
@@ -4451,7 +4451,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.
@@ -4466,14 +4466,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;
}