From 98a18f1cf04b695ff8bf00ddc4e078acb420db4b Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Sat, 5 Dec 2026 15:03:22 +0900 Subject: [PATCH v1] Create PlaceHolderInfos for PlaceHolderVars in security quals When the query uses grouping sets, expand_virtual_generated_columns() wraps the replacement expressions in PlaceHolderVars. It does that throughout the query, including the securityQuals of the relation being expanded, so a row-level security policy on a table with virtual generated columns can end up with PlaceHolderVars in its quals. find_placeholders_in_jointree() never looks at securityQuals, so no PlaceHolderInfos were made for these PlaceHolderVars before the set of PlaceHolderInfos was frozen. Later attempts to look one up, such as when fixing index quals during plan creation, then failed with "too late to create a new PlaceHolderInfo". To fix, make find_placeholders_recurse() process the securityQuals at each RangeTblRef, mirroring process_security_barrier_quals(), which distributes them there. Back-patch to v18, where virtual generated columns were introduced. --- src/backend/optimizer/util/placeholder.c | 7 ++++++- src/test/regress/expected/rowsecurity.out | 17 +++++++++++++++++ src/test/regress/sql/rowsecurity.sql | 6 ++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/backend/optimizer/util/placeholder.c b/src/backend/optimizer/util/placeholder.c index 07171f84f61..94ebd690deb 100644 --- a/src/backend/optimizer/util/placeholder.c +++ b/src/backend/optimizer/util/placeholder.c @@ -212,7 +212,12 @@ find_placeholders_recurse(PlannerInfo *root, Node *jtnode) return; if (IsA(jtnode, RangeTblRef)) { - /* No quals to deal with here */ + int varno = ((RangeTblRef *) jtnode)->rtindex; + RangeTblEntry *rte = root->simple_rte_array[varno]; + + /* Process any securityQuals attached to the RTE */ + if (root->qual_security_level > 0) + find_placeholders_in_expr(root, (Node *) rte->securityQuals); } else if (IsA(jtnode, FromExpr)) { diff --git a/src/test/regress/expected/rowsecurity.out b/src/test/regress/expected/rowsecurity.out index e7ac2cfa3bf..b6325b6b9cc 100644 --- a/src/test/regress/expected/rowsecurity.out +++ b/src/test/regress/expected/rowsecurity.out @@ -4829,6 +4829,23 @@ SELECT * FROM r1; 3 | 30 | 300 (2 rows) +-- Ensure that PHVs in security quals get PlaceHolderInfos +CREATE INDEX ON r1 ((a * 10)); +SET enable_seqscan = off; +EXPLAIN (COSTS OFF) SELECT a FROM r1 GROUP BY GROUPING SETS (a, ()); + QUERY PLAN +------------------------------------------------------------------------------ + MixedAggregate + Hash Key: r1.a + Group Key: () + -> Index Scan using r1_a_10_idx on r1 + Index Cond: ((a * 10) > 10) + Filter: (((SubPlan expr_1) < 400) AND (((a * 10) * 10) = (a * 100))) + SubPlan expr_1 + -> Result +(8 rows) + +RESET enable_seqscan; DROP TABLE r1; -- Check dependency handling RESET SESSION AUTHORIZATION; diff --git a/src/test/regress/sql/rowsecurity.sql b/src/test/regress/sql/rowsecurity.sql index 1d5625bbf61..a409398c164 100644 --- a/src/test/regress/sql/rowsecurity.sql +++ b/src/test/regress/sql/rowsecurity.sql @@ -2332,6 +2332,12 @@ INSERT INTO r1 VALUES (4); INSERT INTO r1 VALUES (3); SELECT * FROM r1; +-- Ensure that PHVs in security quals get PlaceHolderInfos +CREATE INDEX ON r1 ((a * 10)); +SET enable_seqscan = off; +EXPLAIN (COSTS OFF) SELECT a FROM r1 GROUP BY GROUPING SETS (a, ()); +RESET enable_seqscan; + DROP TABLE r1; -- Check dependency handling -- 2.54.0 (Apple Git-157)