aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorDean Rasheed <dean.a.rasheed@gmail.com>2023-02-25 14:47:03 +0000
committerDean Rasheed <dean.a.rasheed@gmail.com>2023-02-25 14:47:03 +0000
commit4fd093af7186e9e3bad805e580edd1991f8f3350 (patch)
treee9e55b67e5d569a2438a12d6455464ed32b44475 /src/backend
parent95558bc8ff89c5887f1bffc9d152ca603637e2c0 (diff)
downloadpostgresql-4fd093af7186e9e3bad805e580edd1991f8f3350.tar.gz
postgresql-4fd093af7186e9e3bad805e580edd1991f8f3350.zip
Fix mishandling of OLD/NEW references in subqueries in rule actions.
If a rule action contains a subquery that refers to columns from OLD or NEW, then those are really lateral references, and the planner will complain if it sees such things in a subquery that isn't marked as lateral. However, at rule-definition time, the user isn't required to mark the subquery with LATERAL, and so it can fail when the rule is used. Fix this by marking such subqueries as lateral in the rewriter, at the point where they're used. Dean Rasheed and Tom Lane, per report from Alexander Lakhin. Back-patch to all supported branches. Discussion: https://postgr.es/m/5e09da43-aaba-7ea7-0a51-a2eb981b058b%40gmail.com
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/rewrite/rewriteHandler.c22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c
index b7277d99741..9103b35c774 100644
--- a/src/backend/rewrite/rewriteHandler.c
+++ b/src/backend/rewrite/rewriteHandler.c
@@ -348,6 +348,7 @@ rewriteRuleAction(Query *parsetree,
Query *sub_action;
Query **sub_action_ptr;
acquireLocksOnSubLinks_context context;
+ ListCell *lc;
context.for_execute = true;
@@ -387,6 +388,23 @@ rewriteRuleAction(Query *parsetree,
PRS2_OLD_VARNO + rt_length, rt_index, 0);
/*
+ * Mark any subquery RTEs in the rule action as LATERAL if they contain
+ * Vars referring to the current query level (references to NEW/OLD).
+ * Those really are lateral references, but we've historically not
+ * required users to mark such subqueries with LATERAL explicitly. But
+ * the planner will complain if such Vars exist in a non-LATERAL subquery,
+ * so we have to fix things up here.
+ */
+ foreach(lc, sub_action->rtable)
+ {
+ RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
+
+ if (rte->rtekind == RTE_SUBQUERY && !rte->lateral &&
+ contain_vars_of_level((Node *) rte->subquery, 1))
+ rte->lateral = true;
+ }
+
+ /*
* Generate expanded rtable consisting of main parsetree's rtable plus
* rule action's rtable; this becomes the complete rtable for the rule
* action. Some of the entries may be unused after we finish rewriting,
@@ -427,8 +445,6 @@ rewriteRuleAction(Query *parsetree,
*/
if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
{
- ListCell *lc;
-
foreach(lc, parsetree->rtable)
{
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
@@ -526,8 +542,6 @@ rewriteRuleAction(Query *parsetree,
*/
if (parsetree->cteList != NIL && sub_action->commandType != CMD_UTILITY)
{
- ListCell *lc;
-
/*
* Annoying implementation restriction: because CTEs are identified by
* name within a cteList, we can't merge a CTE from the original query