Skip to content

Commit 4fd093a

Browse files
committed
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
1 parent 95558bc commit 4fd093a

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

src/backend/rewrite/rewriteHandler.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ rewriteRuleAction(Query *parsetree,
348348
Query *sub_action;
349349
Query **sub_action_ptr;
350350
acquireLocksOnSubLinks_context context;
351+
ListCell *lc;
351352

352353
context.for_execute = true;
353354

@@ -386,6 +387,23 @@ rewriteRuleAction(Query *parsetree,
386387
ChangeVarNodes(rule_qual,
387388
PRS2_OLD_VARNO + rt_length, rt_index, 0);
388389

390+
/*
391+
* Mark any subquery RTEs in the rule action as LATERAL if they contain
392+
* Vars referring to the current query level (references to NEW/OLD).
393+
* Those really are lateral references, but we've historically not
394+
* required users to mark such subqueries with LATERAL explicitly. But
395+
* the planner will complain if such Vars exist in a non-LATERAL subquery,
396+
* so we have to fix things up here.
397+
*/
398+
foreach(lc, sub_action->rtable)
399+
{
400+
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
401+
402+
if (rte->rtekind == RTE_SUBQUERY && !rte->lateral &&
403+
contain_vars_of_level((Node *) rte->subquery, 1))
404+
rte->lateral = true;
405+
}
406+
389407
/*
390408
* Generate expanded rtable consisting of main parsetree's rtable plus
391409
* rule action's rtable; this becomes the complete rtable for the rule
@@ -427,8 +445,6 @@ rewriteRuleAction(Query *parsetree,
427445
*/
428446
if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
429447
{
430-
ListCell *lc;
431-
432448
foreach(lc, parsetree->rtable)
433449
{
434450
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
@@ -526,8 +542,6 @@ rewriteRuleAction(Query *parsetree,
526542
*/
527543
if (parsetree->cteList != NIL && sub_action->commandType != CMD_UTILITY)
528544
{
529-
ListCell *lc;
530-
531545
/*
532546
* Annoying implementation restriction: because CTEs are identified by
533547
* name within a cteList, we can't merge a CTE from the original query

src/test/regress/expected/rules.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3031,6 +3031,31 @@ Rules:
30313031
RETURNING trgt.f1,
30323032
trgt.f2
30333033

3034+
--
3035+
-- Test implicit LATERAL references to old/new in rules
3036+
--
3037+
CREATE TABLE rule_t1(a int, b text DEFAULT 'xxx', c int);
3038+
CREATE VIEW rule_v1 AS SELECT * FROM rule_t1;
3039+
CREATE RULE v1_ins AS ON INSERT TO rule_v1
3040+
DO ALSO INSERT INTO rule_t1
3041+
SELECT * FROM (SELECT a + 10 FROM rule_t1 WHERE a = NEW.a) tt;
3042+
CREATE RULE v1_upd AS ON UPDATE TO rule_v1
3043+
DO ALSO UPDATE rule_t1 t
3044+
SET c = tt.a * 10
3045+
FROM (SELECT a FROM rule_t1 WHERE a = OLD.a) tt WHERE t.a = tt.a;
3046+
INSERT INTO rule_v1 VALUES (1, 'a'), (2, 'b');
3047+
UPDATE rule_v1 SET b = upper(b);
3048+
SELECT * FROM rule_t1;
3049+
a | b | c
3050+
----+-----+-----
3051+
1 | A | 10
3052+
2 | B | 20
3053+
11 | XXX | 110
3054+
12 | XXX | 120
3055+
(4 rows)
3056+
3057+
DROP TABLE rule_t1 CASCADE;
3058+
NOTICE: drop cascades to view rule_v1
30343059
--
30353060
-- check alter rename rule
30363061
--

src/test/regress/sql/rules.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,23 @@ create rule r7 as on delete to rules_src do instead
10541054
-- check display of all rules added above
10551055
\d+ rules_src
10561056

1057+
--
1058+
-- Test implicit LATERAL references to old/new in rules
1059+
--
1060+
CREATE TABLE rule_t1(a int, b text DEFAULT 'xxx', c int);
1061+
CREATE VIEW rule_v1 AS SELECT * FROM rule_t1;
1062+
CREATE RULE v1_ins AS ON INSERT TO rule_v1
1063+
DO ALSO INSERT INTO rule_t1
1064+
SELECT * FROM (SELECT a + 10 FROM rule_t1 WHERE a = NEW.a) tt;
1065+
CREATE RULE v1_upd AS ON UPDATE TO rule_v1
1066+
DO ALSO UPDATE rule_t1 t
1067+
SET c = tt.a * 10
1068+
FROM (SELECT a FROM rule_t1 WHERE a = OLD.a) tt WHERE t.a = tt.a;
1069+
INSERT INTO rule_v1 VALUES (1, 'a'), (2, 'b');
1070+
UPDATE rule_v1 SET b = upper(b);
1071+
SELECT * FROM rule_t1;
1072+
DROP TABLE rule_t1 CASCADE;
1073+
10571074
--
10581075
-- check alter rename rule
10591076
--

0 commit comments

Comments
 (0)