diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c index 601b6dab03..1d1bf958b6 100644 --- a/src/backend/executor/nodeAgg.c +++ b/src/backend/executor/nodeAgg.c @@ -1391,7 +1391,7 @@ project_aggregates(AggState *aggstate) } /* - * Walk tlist and qual to find referenced colnos, dividing them into + * Find input-tuple columns that are needed, dividing them into * aggregated and unaggregated sets. */ static void @@ -1404,9 +1404,15 @@ find_cols(AggState *aggstate, Bitmapset **aggregated, Bitmapset **unaggregated) context.aggregated = NULL; context.unaggregated = NULL; + /* Examine tlist and quals */ (void) find_cols_walker((Node *) agg->plan.targetlist, &context); (void) find_cols_walker((Node *) agg->plan.qual, &context); + /* In some cases, grouping columns will not appear in the tlist */ + for (int i = 0; i < agg->numCols; i++) + context.unaggregated = bms_add_member(context.unaggregated, + agg->grpColIdx[i]); + *aggregated = context.aggregated; *unaggregated = context.unaggregated; }