From 7de7047bdcec52a0c07a0aaf64a8ed3177fe3682 Mon Sep 17 00:00:00 2001 From: Ayush Tiwari Date: Wed, 21 Oct 2026 16:06:59 +0530 Subject: [PATCH v1] Preserve RETURNING aliases when rewriting rules rewriteRuleAction() replaces the rule action's RETURNING aliases for OLD and NEW with those of the triggering query. But if the triggering query uses "old" or "new" as a relation name (e.g. UPDATE v AS old), its corresponding alias is NULL, while the rewritten RETURNING list can still contain OLD/NEW Vars that came from the rule itself. EXPLAIN VERBOSE printed such Vars without any qualification, and since 1999fe227df that trips the new assertion in get_variable(). To fix, keep the rule action's own alias when the triggering query doesn't have one. Backpatch-through: 18 --- src/backend/rewrite/rewriteHandler.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c index 27964cb83c6..1121568a603 100644 --- a/src/backend/rewrite/rewriteHandler.c +++ b/src/backend/rewrite/rewriteHandler.c @@ -731,9 +731,11 @@ rewriteRuleAction(Query *parsetree, 0, &rule_action->hasSubLinks); - /* use triggering query's aliases for OLD and NEW in RETURNING list */ - rule_action->returningOldAlias = parsetree->returningOldAlias; - rule_action->returningNewAlias = parsetree->returningNewAlias; + /* use triggering query's aliases for OLD and NEW, if it has any */ + if (parsetree->returningOldAlias) + rule_action->returningOldAlias = parsetree->returningOldAlias; + if (parsetree->returningNewAlias) + rule_action->returningNewAlias = parsetree->returningNewAlias; /* * There could have been some SubLinks in parsetree's returningList,