From 46fd51d3598075a1d29c42229f24180b0eba06d0 Mon Sep 17 00:00:00 2001 From: Matheus Alcantara Date: Thu, 3 Dec 2026 11:29:40 -0300 Subject: [PATCH v2 2/2] Fix tuple routing when the first result relation is pruned When a row is moved by UPDATE or MERGE into a partition that is not among the result relations, ExecInitPartitionInfo() builds the partition's WITH CHECK OPTION, RETURNING and MERGE action lists by translating those of a reference result relation. It took the lists from the first entry of the ModifyTable node's lists, but translated their Vars using the first entry of mtstate->resultRelInfo. Since cbc127917e, these can be different relations: if initial partition pruning removes the first result relation, ExecInitModifyTable() skips it, so the lists belong to a relation whose RT index doesn't match the one being translated, and their Vars are left with that relation's attribute numbers. If the destination partition has its columns in a different order, the expressions then read the wrong columns, which could let rows that violate a view's check option or a row-level security policy through, or return wrong RETURNING values. Fix 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. Reported-by: Zsolt Parragi Backpatch-through: 18 --- src/backend/executor/execPartition.c | 41 ++++++----- src/test/regress/expected/partition_prune.out | 69 +++++++++++++++++++ src/test/regress/sql/partition_prune.sql | 39 +++++++++++ 3 files changed, 133 insertions(+), 16 deletions(-) diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c index 387cad7d2c4..e3af69dba83 100644 --- a/src/backend/executor/execPartition.c +++ b/src/backend/executor/execPartition.c @@ -578,15 +578,20 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate, list_length(node->resultRelations))); /* - * Use the WCO list of the first plan as a reference to calculate - * attno's for the WCO list of this partition. In the INSERT case, - * that refers to the root partitioned table, whereas in the UPDATE - * tuple routing case, that refers to the first partition in the - * mtstate->resultRelInfo array. In any case, both that relation and - * this partition should have the same columns, so we should be able - * to map attributes successfully. + * Use the WCO list of the first result relation in the + * mtstate->resultRelInfo array as a reference to calculate attno's + * for the WCO list of this partition. In the INSERT case, that is + * the root partitioned table, whereas in the UPDATE/MERGE tuple + * routing case, that is the first partition that was not pruned. In + * any case, both that relation and this partition should have the + * same columns, so we should be able to map attributes successfully. + * + * Note that we must not use linitial(node->withCheckOptionLists) + * here, if initial pruning removed the first result relation, that + * list belongs to a relation other than firstVarno, so its Vars + * would not be translated below. */ - wcoList = linitial(node->withCheckOptionLists); + wcoList = mtstate->resultRelInfo[0].ri_WithCheckOptions; /* * Convert Vars in it to contain this partition's attribute numbers. @@ -641,12 +646,12 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate, list_length(node->resultRelations))); /* - * Use the RETURNING list of the first plan as a reference to - * calculate attno's for the RETURNING list of this partition. See - * the comment above for WCO lists for more details on why this is - * okay. + * Use the RETURNING list of the first result relation in the + * mtstate->resultRelInfo array as a reference to calculate attno's + * for the RETURNING list of this partition. See the comment above + * for WCO lists for more details on why this is okay. */ - returningList = linitial(node->returningLists); + returningList = mtstate->resultRelInfo[0].ri_returningList; /* * Convert Vars in it to contain this partition's attribute numbers. @@ -990,14 +995,18 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate, * Initialize information about this partition that's needed to handle * MERGE. We take the "first" result relation's mergeActionList as * reference and make copy for this relation, converting stuff that - * references attribute numbers to match this relation's. + * references attribute numbers to match this relation's. As for the WCO + * and RETURNING lists above, the "first" result relation is the first one + * in the mtstate->resultRelInfo array, so use the lists in + * mtstate->mt_mergeActionLists and mtstate->mt_mergeJoinConditions, from + * which ExecInitModifyTable() has removed those of the pruned relations. * * This duplicates much of the logic in ExecInitMerge(), so if something * changes there, look here too. */ if (node && node->operation == CMD_MERGE) { - List *firstMergeActionList = linitial(node->mergeActionLists); + List *firstMergeActionList = linitial(mtstate->mt_mergeActionLists); ListCell *lc; ExprContext *econtext = mtstate->ps.ps_ExprContext; Node *joinCondition; @@ -1013,7 +1022,7 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate, /* Initialize state for join condition checking. */ joinCondition = - map_variable_attnos(linitial(node->mergeJoinConditions), + map_variable_attnos(linitial(mtstate->mt_mergeJoinConditions), firstVarno, 0, part_attmap, RelationGetForm(partrel)->reltype, diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.out index 28a7da798cb..cf99c55f5a1 100644 --- a/src/test/regress/expected/partition_prune.out +++ b/src/test/regress/expected/partition_prune.out @@ -4874,6 +4874,75 @@ explain (generic_plan, costs off) update part_abc set b = $2 where a = $1; deallocate update_part_abc; drop table part_abc_sub; +-- Moving a row into a partition that is not among the remaining result +-- relations when the first result relation has been pruned. The partition +-- must take its WITH CHECK OPTION, RETURNING and MERGE action lists from the +-- first result relation that was not pruned, not from the pruned one. +create table part_abc_3 (c bool, a int, b text); +alter table part_abc attach partition part_abc_3 for values in (3); +prepare update_part_abc_view_move (int, text) as +update part_abc_view set a = 3, b = $2 where a = $1 returning *; +-- Only part_abc_2 should be shown in the list of relations to be updated +explain (costs off) execute update_part_abc_view_move (2, 'e'); + QUERY PLAN +------------------------------------------------------- + Update on part_abc + Update on part_abc_2 + -> Append + Subplans Removed: 2 + -> Seq Scan on part_abc_2 + Filter: ((b <> 'a'::text) AND (a = $1)) +(6 rows) + +begin; +execute update_part_abc_view_move (2, 'e'); + a | b | c +---+---+--- + 3 | e | t +(1 row) + +rollback; +-- Should fail the check option +execute update_part_abc_view_move (2, 'a'); +ERROR: new row violates check option for view "part_abc_view" +DETAIL: Failing row contains (3, a, t). +deallocate update_part_abc_view_move; +prepare merge_part_abc_view_move (int, text) as +merge into part_abc_view pt using (select $1 as a) s on pt.a = s.a +when matched then update set a = 3, b = $2 +returning merge_action(), pt.*; +begin; +execute merge_part_abc_view_move (2, 'e'); + merge_action | a | b | c +--------------+---+---+--- + UPDATE | 3 | e | t +(1 row) + +rollback; +-- Should fail the check option +execute merge_part_abc_view_move (2, 'a'); +ERROR: new row violates check option for view "part_abc_view" +DETAIL: Failing row contains (3, a, t). +deallocate merge_part_abc_view_move; +prepare merge_part_abc_view_insert (int, text) as +merge into part_abc_view pt using (select $1 as a, $2 as b) s +on pt.a = s.a and pt.b = s.b +when not matched then insert values (3, s.b, false) +returning merge_action(), pt.*; +begin; +execute merge_part_abc_view_insert (2, 'e'); + merge_action | a | b | c +--------------+---+---+--- + INSERT | 3 | e | f +(1 row) + +rollback; +-- Should fail the check option +execute merge_part_abc_view_insert (2, 'a'); +ERROR: new row violates check option for view "part_abc_view" +DETAIL: Failing row contains (3, a, f). +deallocate merge_part_abc_view_insert; +drop table part_abc_3; -- Runtime pruning on MERGE using a stable function create function stable_one() returns int as $$ begin return 1; end; $$ language plpgsql stable; explain (costs off) diff --git a/src/test/regress/sql/partition_prune.sql b/src/test/regress/sql/partition_prune.sql index d14b961c3fe..123f89fb5d8 100644 --- a/src/test/regress/sql/partition_prune.sql +++ b/src/test/regress/sql/partition_prune.sql @@ -1491,6 +1491,45 @@ explain (generic_plan, costs off) update part_abc set b = $2 where a = $1; deallocate update_part_abc; drop table part_abc_sub; +-- Moving a row into a partition that is not among the remaining result +-- relations when the first result relation has been pruned. The partition +-- must take its WITH CHECK OPTION, RETURNING and MERGE action lists from the +-- first result relation that was not pruned, not from the pruned one. +create table part_abc_3 (c bool, a int, b text); +alter table part_abc attach partition part_abc_3 for values in (3); +prepare update_part_abc_view_move (int, text) as +update part_abc_view set a = 3, b = $2 where a = $1 returning *; +-- Only part_abc_2 should be shown in the list of relations to be updated +explain (costs off) execute update_part_abc_view_move (2, 'e'); +begin; +execute update_part_abc_view_move (2, 'e'); +rollback; +-- Should fail the check option +execute update_part_abc_view_move (2, 'a'); +deallocate update_part_abc_view_move; +prepare merge_part_abc_view_move (int, text) as +merge into part_abc_view pt using (select $1 as a) s on pt.a = s.a +when matched then update set a = 3, b = $2 +returning merge_action(), pt.*; +begin; +execute merge_part_abc_view_move (2, 'e'); +rollback; +-- Should fail the check option +execute merge_part_abc_view_move (2, 'a'); +deallocate merge_part_abc_view_move; +prepare merge_part_abc_view_insert (int, text) as +merge into part_abc_view pt using (select $1 as a, $2 as b) s +on pt.a = s.a and pt.b = s.b +when not matched then insert values (3, s.b, false) +returning merge_action(), pt.*; +begin; +execute merge_part_abc_view_insert (2, 'e'); +rollback; +-- Should fail the check option +execute merge_part_abc_view_insert (2, 'a'); +deallocate merge_part_abc_view_insert; +drop table part_abc_3; + -- Runtime pruning on MERGE using a stable function create function stable_one() returns int as $$ begin return 1; end; $$ language plpgsql stable; explain (costs off) -- 2.50.1 (Apple Git-155)