8000 Correctly update hasSubLinks while mutating a rule action. · postgres/postgres@13192a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 13192a3

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 bbfc26d commit 13192a3

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
@@ -468,6 +468,8 @@ rewriteRuleAction(Query *parsetree,
468468
/* other RTE types don't contain bare expressions */
469469
break;
470470
}
471+
sub_action->hasSubLinks |=
472+
checkExprHasSubLink((Node *) rte->securityQuals);
471473
if (sub_action->hasSubLinks)
472474
break; /* no need to keep scanning rtable */
473475
}
@@ -3229,7 +3231,7 @@ rewriteTargetView(Query *parsetree, Relation view)
32293231
view_targetlist,
32303232
REPLACEVARS_REPORT_ERROR,
32313233
0,
3232-
&parsetree->hasSubLinks);
3234+
NULL);
32333235

32343236
/*
32353237
* 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
@@ -2522,6 +2522,38 @@ DROP VIEW v1;
25222522
DROP TABLE t2;
25232523
DROP TABLE t1;
25242524
--
2525+
-- Test sub-select in nested security barrier views, per bug #17972
2526+
--
2527+
CREATE TABLE t1 (a int);
2528+
CREATE VIEW v1 WITH (security_barrier = true) AS
2529+
SELECT * FROM t1;
2530+
CREATE RULE v1_upd_rule AS ON UPDATE TO v1 DO INSTEAD
2531+
UPDATE t1 SET a = NEW.a WHERE a = OLD.a;
2532+
CREATE VIEW v2 WITH (security_barrier = true) AS
2533+
SELECT * FROM v1 WHERE EXISTS (SELECT 1);
2534+
EXPLAIN (COSTS OFF) UPDATE v2 SET a = 1;
2535+
QUERY PLAN
2536+
---------------------------------------------------
2537+
Update on t1
2538+
InitPlan 1 (returns $0)
2539+
-> Result
2540+
-> Merge Join
2541+
Merge Cond: (t1.a = v1.a)
2542+
-> Sort
2543+
Sort Key: t1.a
2544+
-> Seq Scan on t1
2545+
-> Sort
2546+
Sort Key: v1.a
2547+
-> Subquery Scan on v1
2548+
-> Result
2549+
One-Time Filter: $0
2550+
-> Seq Scan on t1 t1_1
2551+
(14 rows)
2552+
2553+
DROP VIEW v2;
2554+
DROP VIEW v1;
2555+
DROP TABLE t1;
2556+
--
25252557
-- Test CREATE OR REPLACE VIEW turning a non-updatable view into an
25262558
-- auto-updatable view and adding check options in a single step
25272559
--

src/test/regress/sql/updatable_views.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,23 @@ DROP VIEW v1;
12061206
DROP TABLE t2;
12071207
DROP TABLE t1;
12081208

1209+
--
1210+
-- Test sub-select in nested security barrier views, per bug #17972
1211+
--
1212+
CREATE TABLE t1 (a int);
1213+
CREATE VIEW v1 WITH (security_barrier = true) AS
1214+
SELECT * FROM t1;
1215+
CREATE RULE v1_upd_rule AS ON UPDATE TO v1 DO INSTEAD
1216+
UPDATE t1 SET a = NEW.a WHERE a = OLD.a;
1217+
CREATE VIEW v2 WITH (security_barrier = true) AS
1218+
SELECT * FROM v1 WHERE EXISTS (SELECT 1);
1219+
1220+
EXPLAIN (COSTS OFF) UPDATE v2 SET a = 1;
1221+
1222+
DROP VIEW v2;
1223+
DROP VIEW v1;
1224+
DROP TABLE t1;
1225+
12091226
--
12101227
-- Test CREATE OR REPLACE VIEW turning a non-updatable view into an
12111228
-- auto-updatable view and adding check options in a single step

0 commit comments

Comments
 (0)
0