diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c index 8cb5611671b2c1c81d59c67f2982993daf53dc20..26eff5eb8a59a9dcf076622532ecc03fc9771e8c 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 bb163d0cab2b2d730b85adaebb6877801a79df9e..6df8ff6d5f3b622d75cfa1d3bf33cf0393ce117e 100644 --- a/src/backend/optimizer/plan/planmain.c +++ b/src/backend/optimizer/plan/planmain.c @@ -345,7 +345,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 e64de3aa67ae467bf3b78709992357b4211631f4..8e90e193bffc7e59af7eae3b4957607ed261e5d4 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1247,11 +1247,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 94a2372ecaa9d90350ac102e6ee71a5efba65a85..9b5e5ab9c251020442c2efc09d85473a4ec9804b 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -4616,8 +4616,8 @@ examine_simple_variable(PlannerInfo *root, Var *var, * *isdefault: set to TRUE if the result is a default rather than based on * anything meaningful. * - * 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, bool *isdefault) @@ -4693,7 +4693,7 @@ get_variable_numdistinct(VariableStatData *vardata, bool *isdefault) * 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. @@ -4714,7 +4714,7 @@ get_variable_numdistinct(VariableStatData *vardata, bool *isdefault) * 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 @@ -4722,7 +4722,7 @@ get_variable_numdistinct(VariableStatData *vardata, bool *isdefault) * that the behavior isn't discontinuous. */ if (ntuples < DEFAULT_NUM_DISTINCT) - return ntuples; + return clamp_row_est(ntuples); *isdefault = true; return DEFAULT_NUM_DISTINCT;