diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index 656d03b69aa510e5573ac2b6f0b15cb884ab5bd9..0d5cafba9627f0529731f3f68b077a353832b00a 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -3209,6 +3209,14 @@ estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows) double numdistinct; ListCell *l; + /* + * We don't ever want to return an estimate of zero groups, as that tends + * to lead to division-by-zero and other unpleasantness. The input_rows + * estimate is usually already at least 1, but clamp it just in case it + * isn't. + */ + input_rows = clamp_row_est(input_rows); + /* * If no grouping columns, there's exactly one group. (This can't happen * for normal cases with GROUP BY or DISTINCT, but it is possible for