Re: executor startup doesn't skip sub-partitions of a pruned partition

From: "Matheus Alcantara" <matheusssilv97(at)gmail(dot)com>
To: "Zsolt Parragi" <zsolt(dot)parragi(at)percona(dot)com>, "Amit Langote" <amitlangote09(at)gmail(dot)com>
Cc: <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: executor startup doesn't skip sub-partitions of a pruned partition
Date: 2026-12-03 17:49:18
Message-ID: DNBACOQ49MR4.3DVISTTBFXK78@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed Dec 2, 2026 at 8:01 PM -03, Zsolt Parragi wrote:
> The patch seems to bypasses RLS/WCO:
>
> SET plan_cache_mode = force_generic_plan;
> CREATE TABLE t3 (a int, allowed bool, other bool, c int) PARTITION BY LIST (a);
> CREATE TABLE t3_sub PARTITION OF t3 FOR VALUES IN (1) PARTITION BY LIST (c);
> CREATE TABLE t3_sub_1 PARTITION OF t3_sub FOR VALUES IN (1);
> CREATE TABLE t3_surv PARTITION OF t3 FOR VALUES IN (2);
> CREATE TABLE t3_dest (a int, other bool, allowed bool, c int);
> ALTER TABLE t3 ATTACH PARTITION t3_dest FOR VALUES IN (3);
> CREATE VIEW v3 AS SELECT * FROM t3 WHERE allowed = true WITH CHECK OPTION;
> INSERT INTO t3 VALUES (2, true, true, 1);
> PREPARE u3(int) AS UPDATE v3 SET a = 3, allowed = false, other = true
> WHERE a = $1;
> EXECUTE u3(2);

I looked into this, and I think that the bug is not in v1 itself. It's
an older issue from cbc127917e that v1 makes reachable in more cases.
The same thing happens on master without v1 when the first result
relation is pruned directly:

SET plan_cache_mode = force_generic_plan;
CREATE TABLE t3 (a int, allowed bool, other bool, c int) PARTITION BY LIST (a);
CREATE TABLE t3_first PARTITION OF t3 FOR VALUES IN (1);
CREATE TABLE t3_surv PARTITION OF t3 FOR VALUES IN (2);
CREATE TABLE t3_dest (a int, other bool, allowed bool, c int);
ALTER TABLE t3 ATTACH PARTITION t3_dest FOR VALUES IN (3);
CREATE VIEW v3 AS SELECT * FROM t3 WHERE allowed = true WITH CHECK OPTION;
INSERT INTO t3 VALUES (2, true, true, 1);
PREPARE u3(int) AS UPDATE v3 SET a = 3, allowed = false, other = true
WHERE a = $1;
EXECUTE u3(2);
UPDATE 1

With a custom plan the same UPDATE fails with "new row violates check
option for view "v3"", as expected.

When a row is moved into a partition that is not one of the result
relations, ExecInitPartitionInfo() builds that partition's WCO,
RETURNING and MERGE lists by translating the ones of the "first" result
relation. It takes the lists from linitial() of the ModifyTable node's
lists, but translates their Vars using mtstate->resultRelInfo[0]. Since
cbc127917e these can be different relations; if initial pruning removes
the first result relation, ExecInitModifyTable() skips it. The lists
then belong to a relation whose RT index is not the one being
translated, so their Vars are left with that relation's attribute
numbers. If the destination partition has a different column order, the
expressions read the wrong columns. Besides the WCO case, this also lets
rows through an RLS WITH CHECK policy, and MERGE (UPDATE and INSERT
actions) and RETURNING are affected too.

I think that in your example, it only works on master by accident:
t3_sub_1 is the first result relation and, without v1, it is never
pruned, so the lists happen to match. Once v1 prunes it correctly, the
bug shows up.

The attached 0002 fixes it by taking the lists from
mtstate->resultRelInfo[0] and from mtstate->mt_mergeActionLists and
mt_mergeJoinConditions, which ExecInitModifyTable() builds from the
result relations that survive pruning.

0002 doesn't depend on 0001, and it applies on REL_18_STABLE.
Since the bug exists on v18 and 0001 makes it easier to hit, I think
0002 should be back-patched to v18 too.

0001 is Amit's v1 unchanged.

Thoughts?

--
Matheus Alcantara
EDB: https://www.enterprisedb.com

Attachment Content-Type Size
v2-0001-Treat-partitions-under-a-pruned-sub-partitioned-t.patch text/plain 11.9 KB
v2-0002-Fix-tuple-routing-when-the-first-result-relation-.patch text/plain 10.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Burd 2026-12-03 17:56:05 Re: Let an ordering index scan hand its ORDER BY value to the target list
Previous Message Mark Wong 2026-12-03 17:47:39 Re: Credits For v19