Skip to content

Commit b4110bd

Browse files
committed
Correctly update hasSubLinks while mutating a rule action.
rewriteRuleAction neglected to check for SubLink nodes in the securityQuals of range table entries. This could lead to failing to convert such a SubLink to a SubPlan, resulting in assertion crashes or weird errors later in planning. In passing, fix some poor coding in rewriteTargetView: we should not pass the source parsetree's hasSubLinks field to ReplaceVarsFromTargetList's outer_hasSubLinks. ReplaceVarsFromTargetList knows enough to ignore that when a Query node is passed, but it's still confusing and bad precedent: if we did try to update that flag we'd be updating a stale copy of the parsetree. Per bug #17972 from Alexander Lakhin. This has been broken since we added RangeTblEntry.securityQuals (although the presented test case only fails back to 215b43c), so back-patch all the way. Discussion: https://postgr.es/m/17972-f422c094237847d0@postgresql.org
1 parent edf1de6 commit b4110bd

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/backend/rewrite/rewriteHandler.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ rewriteRuleAction(Query *parsetree,
472472
/* other RTE types don't contain bare expressions */
473473
break;
474474
}
475+
sub_action->hasSubLinks |=
476+
checkExprHasSubLink((Node *) rte->securityQuals);
475477
if (sub_action->hasSubLinks)
476478
break; /* no need to keep scanning rtable */
477479
}
@@ -3304,7 +3306,7 @@ rewriteTargetView(Query *parsetree, Relation view)
33043306
view_targetlist,
33053307
REPLACEVARS_REPORT_ERROR,
33063308
0,
3307-
&parsetree->hasSubLinks);
3309+
NULL);
33083310

33093311
/*
33103312
* Update all other RTI references in the query that point to the view

src/test/regress/expected/updatable_views.out

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,6 +2616,38 @@ DROP VIEW v1;
26162616
DROP TABLE t2;
26172617
DROP TABLE t1;
26182618
--
2619+
-- Test sub-select in nested security barrier views, per bug #17972
2620+
--
2621+
CREATE TABLE t1 (a int);
2622+
CREATE VIEW v1 WITH (security_barrier = true) AS
2623+
SELECT * FROM t1;
2624+
CREATE RULE v1_upd_rule AS ON UPDATE TO v1 DO INSTEAD
2625+
UPDATE t1 SET a = NEW.a WHERE a = OLD.a;
2626+
CREATE VIEW v2 WITH (security_barrier = true) AS
2627+
SELECT * FROM v1 WHERE EXISTS (SELECT 1);
2628+
EXPLAIN (COSTS OFF) UPDATE v2 SET a = 1;
2629+
QUERY PLAN
2630+
---------------------------------------------------
2631+
Update on t1
2632+
InitPlan 1 (returns $0)
2633+
-> Result
2634+
-> Merge Join
2635+
Merge Cond: (t1.a = v1.a)
2636+
-> Sort
2637+
Sort Key: t1.a
2638+
-> Seq Scan on t1
2639+
-> Sort
2640+
Sort Key: v1.a
2641+
-> Subquery Scan on v1
2642+
-> Result
2643+
One-Time Filter: $0
2644+
-> Seq Scan on t1 t1_1
2645+
(14 rows)
2646+
2647+
DROP VIEW v2;
2648+
DROP VIEW v1;
2649+
DROP TABLE t1;
2650+
--
26192651
-- Test CREATE OR REPLACE VIEW turning a non-updatable view into an
26202652
-- auto-updatable view and adding check options in a single step
26212653
--

src/test/regress/sql/updatable_views.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,23 @@ DROP VIEW v1;
12431243
DROP TABLE t2;
12441244
DROP TABLE t1;
12451245

1246+
--
1247+
-- Test sub-select in nested security barrier views, per bug #17972
1248+
--
1249+
CREATE TABLE t1 (a int);
1250+
CREATE VIEW v1 WITH (security_barrier = true) AS
1251+
SELECT * FROM t1;
1252+
CREATE RULE v1_upd_rule AS ON UPDATE TO v1 DO INSTEAD
1253+
UPDATE t1 SET a = NEW.a WHERE a = OLD.a;
1254+
CREATE VIEW v2 WITH (security_barrier = true) AS
1255+
SELECT * FROM v1 WHERE EXISTS (SELECT 1);
1256+
1257+
EXPLAIN (COSTS OFF) UPDATE v2 SET a = 1;
1258+
1259+
DROP VIEW v2;
1260+
DROP VIEW v1;
1261+
DROP TABLE t1;
1262+
12461263
--
12471264
-- Test CREATE OR REPLACE VIEW turning a non-updatable view into an
12481265
-- auto-updatable view and adding check options in a single step

0 commit comments

Comments
 (0)