diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index 4b1229484eeedaf7db1572e9ce5cb77cf3cafc49..de2d7f98b27df399b2977caa3fc60e4ef7a407fc 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -3207,6 +3207,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