From ab49e0544a80dbdd2c1cc05a35eb45aebcf0226d Mon Sep 17 00:00:00 2001 From: Amit Langote Date: Wed, 2 Dec 2026 16:40:31 +0900 Subject: [PATCH v1] 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.47.3