From 79b58c6f684315d39dfdbd721da7576d90bd9f99 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 12 May 2022 11:31:46 -0400 Subject: Make pull_var_clause() handle GroupingFuncs exactly like Aggrefs. This follows in the footsteps of commit 2591ee8ec by removing one more ill-advised shortcut from planning of GroupingFuncs. It's true that we don't intend to execute the argument expression(s) at runtime, but we still have to process any Vars appearing within them, or we risk failure at setrefs.c time (or more fundamentally, in EXPLAIN trying to print such an expression). Vars in upper plan nodes have to have referents in the next plan level, whether we ever execute 'em or not. Per bug #17479 from Michael J. Sullivan. Back-patch to all supported branches. Richard Guo Discussion: https://postgr.es/m/17479-6260deceaf0ad304@postgresql.org --- src/backend/optimizer/util/var.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'src/backend/optimizer') diff --git a/src/backend/optimizer/util/var.c b/src/backend/optimizer/util/var.c index 6d11d187a0c..ebc6ce84b0b 100644 --- a/src/backend/optimizer/util/var.c +++ b/src/backend/optimizer/util/var.c @@ -586,7 +586,7 @@ locate_var_of_level_walker(Node *node, * Vars within a PHV's expression are included in the result only * when PVC_RECURSE_PLACEHOLDERS is specified. * - * GroupingFuncs are treated mostly like Aggrefs, and so do not need + * GroupingFuncs are treated exactly like Aggrefs, and so do not need * their own flag bits. * * CurrentOfExpr nodes are ignored in all cases. @@ -661,13 +661,7 @@ pull_var_clause_walker(Node *node, pull_var_clause_context *context) } else if (context->flags & PVC_RECURSE_AGGREGATES) { - /* - * We do NOT descend into the contained expression, even if the - * caller asked for it, because we never actually evaluate it - - * the result is driven entirely off the associated GROUP BY - * clause, so we never need to extract the actual Vars here. - */ - return false; + /* fall through to recurse into the GroupingFunc's arguments */ } else elog(ERROR, "GROUPING found where not expected"); -- cgit v1.2.3