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
From 48ef6806e91e112e8dc0df91977f5e3a83de9ddc Mon Sep 17 00:00:00 2001
From: Amit Langote <[email protected]>
Date: Wed, 2 Dec 2026 16:40:31 +0900
Subject: [PATCH v2 1/2] Treat partitions under a pruned sub-partitioned table
 as pruned

cbc127917e made the executor skip relations removed by initial
partition pruning in more places than just the nodes that have a
PartitionPruneInfo, by having those places check es_unpruned_relids.
That set consists of PlannedStmt.unprunableRelids plus the leaf
partitions that survive initial pruning, which works only if the
planner counts as prunable every leaf partition that initial pruning
can remove.  But set_plan_references() counted a leaf partition as
prunable only if its own parent has initial pruning steps, whereas the
steps of a table higher up can prune a partition that is itself
partitioned, taking all of its partitions with it.  Such leaf
partitions were always treated as unpruned; for example,
ExecInitModifyTable() initialized them as result relations and EXPLAIN
showed them as such.

Fix by considering a leaf partition prunable if any of its ancestors
has initial pruning steps.  CreatePartitionPruneState() needs a
matching change, because when initial pruning is skipped, as is the
case for EXPLAIN (GENERIC_PLAN), the leaf partitions it returns are
added to es_unpruned_relids in place of the result of pruning.  It
now returns those of all the tables in the hierarchy, not only of
those that have initial pruning steps.

Add tests for both changes.  One checking that the partitions of a
pruned sub-partitioned table are no longer set up as result relations,
and the other running EXPLAIN (GENERIC_PLAN) on such a table, which
fails with "trying to open a pruned relation" without the
CreatePartitionPruneState() change and which no existing test covers.

Discussion: https://postgr.es/m/
---
 src/backend/executor/execPartition.c          |  9 ++-
 src/backend/optimizer/plan/setrefs.c          | 71 ++++++++++++++++---
 src/backend/partitioning/partprune.c          |  7 +-
 src/test/regress/expected/partition_prune.out | 58 +++++++++++++++
 src/test/regress/sql/partition_prune.sql      | 18 +++++
 5 files changed, 150 insertions(+), 13 deletions(-)

diff --git a/src/backend/executor/execPartition.c 
b/src/backend/executor/execPartition.c
index a7ec585763e..387cad7d2c4 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -2346,9 +2346,14 @@ CreatePartitionPruneState(EState *estate, 
PartitionPruneInfo *pruneinfo,
 
                        /*
                         * Return all leaf partition indexes if we're skipping 
pruning in
-                        * the EXPLAIN (GENERIC_PLAN) case.
+                        * the EXPLAIN (GENERIC_PLAN) case.  That must include 
the
+                        * partitions of tables that have no initial pruning 
steps of
+                        * their own.  If a table higher up has such steps, the 
planner
+                        * leaves those partitions out of 
PlannedStmt.unprunableRelids too
+                        * (see add_prunable_leafparts()), so returning them 
here is the
+                        * only way they get into es_unpruned_relids.
                         */
-                       if (pinfo->initial_pruning_steps && 
!prunestate->do_initial_prune)
+                       if (econtext->ecxt_estate->es_top_eflags & 
EXEC_FLAG_EXPLAIN_GENERIC)
                        {
                                int                     part_index = -1;
 
diff --git a/src/backend/optimizer/plan/setrefs.c 
b/src/backend/optimizer/plan/setrefs.c
index 7a04de86879..79e53b6142b 100644
--- a/src/backend/optimizer/plan/setrefs.c
+++ b/src/backend/optimizer/plan/setrefs.c
@@ -17,6 +17,7 @@
 
 #include "access/transam.h"
 #include "catalog/pg_type.h"
+#include "miscadmin.h"
 #include "nodes/makefuncs.h"
 #include "nodes/nodeFuncs.h"
 #include "optimizer/optimizer.h"
@@ -1795,6 +1796,56 @@ set_customscan_references(PlannerInfo *root,
        cscan->custom_relids = offset_relid_set(cscan->custom_relids, rtoffset);
 }
 
+/*
+ * add_prunable_leafparts
+ *             Add to glob->prunableRelids the RT indexes of those leaf 
partitions
+ *             under the given partitioned table that "initial" pruning can 
remove.
+ *
+ * That is all of the table's leaf partitions if it has initial pruning steps
+ * of its own.  It is also all of them if 'pruned_from_above' says that a
+ * table further up the hierarchy has such steps, because when those steps
+ * prune a partition that is itself partitioned, everything beneath it goes
+ * too.
+ *
+ * This descends the hierarchy the same way the executor will when it does the
+ * pruning; compare find_matching_subplans_recurse().
+ */
+static void
+add_prunable_leafparts(PlannerGlobal *glob, List *prune_infos,
+                                          PartitionedRelPruneInfo *prelinfo,
+                                          bool pruned_from_above)
+{
+       bool            prunable = pruned_from_above ||
+               prelinfo->initial_pruning_steps != NIL;
+       int                     i;
+
+       /* Guard against stack overflow due to overly deep partition hierarchy. 
*/
+       check_stack_depth();
+
+       i = -1;
+       while ((i = bms_next_member(prelinfo->present_parts, i)) >= 0)
+       {
+               if (prelinfo->subplan_map[i] >= 0)
+               {
+                       /* Only leaf partitions have an entry in 
leafpart_rti_map. */
+                       if (prunable && prelinfo->leafpart_rti_map[i])
+                               glob->prunableRelids = 
bms_add_member(glob->prunableRelids,
+                                                                               
                          prelinfo->leafpart_rti_map[i]);
+               }
+               else
+               {
+                       int                     partidx = 
prelinfo->subpart_map[i];
+
+                       /* present_parts has only partitions with a subplan or 
subpart */
+                       Assert(partidx >= 0);
+                       add_prunable_leafparts(glob, prune_infos,
+                                                                  
list_nth_node(PartitionedRelPruneInfo,
+                                                                               
                 prune_infos, partidx),
+                                                                  prunable);
+               }
+       }
+}
+
 /*
  * register_partpruneinfo
  *             Subroutine for set_append_references and 
set_mergeappend_references
@@ -1805,9 +1856,9 @@ set_customscan_references(PlannerInfo *root,
  * Also update the RT indexes present in PartitionedRelPruneInfos to add the
  * offset.
  *
- * Finally, if there are initial pruning steps, add the RT indexes of the
- * leaf partitions to the set of relations that are prunable at execution
- * startup time.
+ * Finally, add the RT indexes of the leaf partitions that "initial" pruning
+ * can remove to the set of relations that are prunable at execution startup
+ * time; see add_prunable_leafparts().
  */
 static int
 register_partpruneinfo(PlannerInfo *root, int part_prune_index, int rtoffset)
@@ -1848,14 +1899,18 @@ register_partpruneinfo(PlannerInfo *root, int 
part_prune_index, int rtoffset)
                                 * make_partitionedrel_pruneinfo().
                                 */
                                if (prelinfo->leafpart_rti_map[i])
-                               {
                                        prelinfo->leafpart_rti_map[i] += 
rtoffset;
-                                       if (prelinfo->initial_pruning_steps)
-                                               glob->prunableRelids = 
bms_add_member(glob->prunableRelids,
-                                                                               
                                          prelinfo->leafpart_rti_map[i]);
-                               }
                        }
                }
+
+               /*
+                * Starting from the root table of the hierarchy, which is the 
list's
+                * first item, find the leaf partitions that initial pruning can
+                * remove.
+                */
+               add_prunable_leafparts(glob, prune_infos,
+                                                          
linitial_node(PartitionedRelPruneInfo, prune_infos),
+                                                          false);
        }
 
        glob->partPruneInfos = lappend(glob->partPruneInfos, pinfo);
diff --git a/src/backend/partitioning/partprune.c 
b/src/backend/partitioning/partprune.c
index f128b99c36a..df734f12091 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -682,9 +682,10 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, 
RelOptInfo *parentrel,
                         * relations that may be pruned during executor startup.
                         *
                         * Only leaf partitions with a valid subplan that are 
prunable
-                        * using initial pruning are added to prunableRelids. So
-                        * partitions without a subplan due to constraint 
exclusion will
-                        * remain in PlannedStmt.unprunableRelids.
+                        * using initial pruning are added to prunableRelids; 
see
+                        * add_prunable_leafparts(). So partitions without a 
subplan due
+                        * to constraint exclusion will remain in
+                        * PlannedStmt.unprunableRelids.
                         */
                        if (subplanidx >= 0)
                        {
diff --git a/src/test/regress/expected/partition_prune.out 
b/src/test/regress/expected/partition_prune.out
index 3506ecec477..28a7da798cb 100644
--- a/src/test/regress/expected/partition_prune.out
+++ b/src/test/regress/expected/partition_prune.out
@@ -4816,6 +4816,64 @@ execute update_part_abc_view (3, 'a');
 (0 rows)
 
 deallocate update_part_abc_view;
+-- When a partition that is itself partitioned is pruned, everything beneath
+-- it is pruned too, even though there is nothing to prune its own partitions
+-- with.
+create table part_abc_sub partition of part_abc for values in (5) partition by 
list (c);
+create table part_abc_sub_1 partition of part_abc_sub for values in (true);
+create table part_abc_sub_2 partition of part_abc_sub for values in (false);
+prepare update_part_abc as update part_abc set b = $2 where a = $1;
+-- part_abc_sub's partitions should not be shown in the list of relations to
+-- be updated
+explain (costs off) execute update_part_abc (1, 'e');
+             QUERY PLAN             
+------------------------------------
+ Update on part_abc
+   Update on part_abc_1
+   ->  Append
+         Subplans Removed: 3
+         ->  Seq Scan on part_abc_1
+               Filter: (a = $1)
+(6 rows)
+
+-- Only part_abc_sub's partitions should be shown
+explain (costs off) execute update_part_abc (5, 'e');
+                    QUERY PLAN                     
+---------------------------------------------------
+ Update on part_abc
+   Update on part_abc_sub_2 part_abc_1
+   Update on part_abc_sub_1 part_abc_2
+   ->  Append
+         Subplans Removed: 2
+         ->  Seq Scan on part_abc_sub_2 part_abc_1
+               Filter: (a = $1)
+         ->  Seq Scan on part_abc_sub_1 part_abc_2
+               Filter: (a = $1)
+(9 rows)
+
+-- All partitions should be shown with EXPLAIN (GENERIC_PLAN), which doesn't
+-- perform initial pruning
+explain (generic_plan, costs off) update part_abc set b = $2 where a = $1;
+                    QUERY PLAN                     
+---------------------------------------------------
+ Update on part_abc
+   Update on part_abc_1
+   Update on part_abc_2
+   Update on part_abc_sub_2 part_abc_3
+   Update on part_abc_sub_1 part_abc_4
+   ->  Append
+         ->  Seq Scan on part_abc_1
+               Filter: (a = $1)
+         ->  Seq Scan on part_abc_2
+               Filter: (a = $1)
+         ->  Seq Scan on part_abc_sub_2 part_abc_3
+               Filter: (a = $1)
+         ->  Seq Scan on part_abc_sub_1 part_abc_4
+               Filter: (a = $1)
+(14 rows)
+
+deallocate update_part_abc;
+drop table part_abc_sub;
 -- 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 d6e096dc565..d14b961c3fe 100644
--- a/src/test/regress/sql/partition_prune.sql
+++ b/src/test/regress/sql/partition_prune.sql
@@ -1473,6 +1473,24 @@ explain (verbose, costs off) execute 
update_part_abc_view (3, 'a');
 execute update_part_abc_view (3, 'a');
 deallocate update_part_abc_view;
 
+-- When a partition that is itself partitioned is pruned, everything beneath
+-- it is pruned too, even though there is nothing to prune its own partitions
+-- with.
+create table part_abc_sub partition of part_abc for values in (5) partition by 
list (c);
+create table part_abc_sub_1 partition of part_abc_sub for values in (true);
+create table part_abc_sub_2 partition of part_abc_sub for values in (false);
+prepare update_part_abc as update part_abc set b = $2 where a = $1;
+-- part_abc_sub's partitions should not be shown in the list of relations to
+-- be updated
+explain (costs off) execute update_part_abc (1, 'e');
+-- Only part_abc_sub's partitions should be shown
+explain (costs off) execute update_part_abc (5, 'e');
+-- All partitions should be shown with EXPLAIN (GENERIC_PLAN), which doesn't
+-- perform initial pruning
+explain (generic_plan, costs off) update part_abc set b = $2 where a = $1;
+deallocate update_part_abc;
+drop table part_abc_sub;
+
 -- 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)

From 46fd51d3598075a1d29c42229f24180b0eba06d0 Mon Sep 17 00:00:00 2001
From: Matheus Alcantara <[email protected]>
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 <[email protected]>
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)

Reply via email to