| From: | "Matheus Alcantara" <matheusssilv97(at)gmail(dot)com> |
|---|---|
| To: | "Amit Langote" <amitlangote09(at)gmail(dot)com>, "PostgreSQL Hackers" <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:43:59 |
| Message-ID: | DNBA8LTSUX0O.3P9KMV4BTM15K@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Wed Dec 2, 2026 at 4:44 AM -03, Amit Langote wrote:
> While working on another patch, I noticed that when initial pruning
> removes a partition that is itself partitioned, the executor doesn't
> treat that partition's own partitions as pruned. For example:
>
> create table pt (a int, b text, c bool) partition by list (a);
> create table pt_1 partition of pt for values in (1);
> create table pt_2 partition of pt for values in (2);
> create table pt_sub partition of pt for values in (5) partition by list (c);
> create table pt_sub_1 partition of pt_sub for values in (true);
> create table pt_sub_2 partition of pt_sub for values in (false);
> set plan_cache_mode = force_generic_plan;
> prepare upd as update pt set b = $2 where a = $1;
> explain (costs off) execute upd (1, 'e');
> QUERY PLAN
> --------------------------------
> Update on pt
> Update on pt_1
> Update on pt_sub_2 pt
> Update on pt_sub_1 pt
> -> Append
> Subplans Removed: 3
> -> Seq Scan on pt_1
> Filter: (a = $1)
> (8 rows)
>
> pt_2 is left out of the list of result relations as it should be, but
> pt_sub_1 and pt_sub_2 are not, even though their subplans have been
> removed just like pt_2's.
>
> That's because set_plan_references() adds a leaf partition to
> PlannerGlobal.prunableRelids only if its own parent has initial
> pruning steps, and pt_sub has none. It's pt's steps that prune pt_sub
> and everything under it. So the two end up in
> PlannedStmt.unprunableRelids and the executor initializes them as
> result relations. That's an oversight in my commit cbc127917e.
>
> The attached patch fixes that by considering a leaf partition prunable
> if any of its ancestors has initial pruning steps.
> CreatePartitionPruneState() needs a matching change for the EXPLAIN
> (GENERIC_PLAN) case, where the steps exist but aren't run and so all
> leaf partitions must be reported as surviving. With the patch:
>
> explain (costs off) execute upd (1, 'e');
> QUERY PLAN
> --------------------------------
> Update on pt
> Update on pt_1
> -> Append
> Subplans Removed: 3
> -> Seq Scan on pt_1
> Filter: (a = $1)
> (6 rows)
>
> Note that the subplans of those partitions are not initialized,
> because Append uses the result of the pruning itself. It's only the
> code that consults es_unpruned_relids that treats them as unpruned,
> that is, ExecInitModifyTable() when setting up result relations and
> the code that sets up row marks. No rows can come from them, so as far
> as I can tell nothing misbehaves. It's just that the executor does
> some of the work that cbc127917e meant to avoid, which for a foreign
> partition includes calling BeginForeignModify(), and the EXPLAIN
> output is misleading. Because of the latter, I'm inclined to
> back-patch this to v18, though I'm happy to fix it only in master.
> Thoughts?
>
Hi,
Thanks for working on this!
I've reviewed and tested v1 and it seems to works as expected. The new
tests fail without the patch, so it looks good to me.
I have some comments:
1: The fix only looks at the PartitionPruneInfo of a single Append. If
the sub-partitioned table gets its own MergeAppend under the outer
Append, its partitions are still treated as unpruned with v1:
-- same tables as in your example, but with b int
create table pt (a int, b int, c bool) partition by list (a);
create table pt_1 partition of pt for values in (1);
create table pt_2 partition of pt for values in (2);
create table pt_sub partition of pt for values in (5) partition by list (c);
create table pt_sub_1 partition of pt_sub for values in (true);
create table pt_sub_2 partition of pt_sub for values in (false);
create index on pt (b);
create table o (b int primary key);
set plan_cache_mode = force_generic_plan;
set enable_hashjoin = off; set enable_nestloop = off;
set enable_sort = off; set enable_seqscan = off;
prepare upd as update pt set b = pt.b from o where pt.b = o.b and pt.a = $1;
explain (costs off) execute upd (1);
QUERY PLAN
-------------------------------------------------------
Update on pt
Update on pt_1
Update on pt_sub_2 pt
Update on pt_sub_1 pt
-> Merge Join
Merge Cond: (pt.b = o.b)
-> Append
Subplans Removed: 2
-> Index Scan using pt_1_b_idx on pt_1
Filter: (a = $1)
-> Index Scan using o_pkey on o
The inner MergeAppend's PartitionPruneInfo is rooted at pt_sub. In this
case it doesn't exist at all, since there is nothing to prune pt_sub's
partitions with, so pt's steps never reach pt_sub_1 and pt_sub_2. If
pt_sub's partitions can be pruned too (e.g. and pt.c = $2), execute
upd (1, true) still shows "Update on pt_sub_1", even though the outer
Append removed the whole MergeAppend.
I think this could be fixed by letting make_partition_pruneinfo() add
the partitioned ancestors of a nested Append's parent rel to its
hierarchy, with only initial pruning steps built from the ancestors' own
quals. add_prunable_leafparts() and the executor would then handle it
without other changes. This make sense? What do you think?
2: Since the pruned foreign partitions are no longer set up as result
relations, a generic plan that moves a row into a postgres_fdw partition
under a pruned sub-partitioned table no longer fails with:
ERROR: cannot route tuples into foreign table to be updated "pt_sub_t"
And it now succeeds, as it already does with a custom plan or when the
foreign table is a direct partition of pt. I think that's right and one
more reason to back-patch, so +1 for fixing it on v18 as well.
--
Matheus Alcantara
EDB: https://www.enterprisedb.com
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Mark Wong | 2026-12-03 17:47:39 | Re: Credits For v19 |
| Previous Message | Greg Burd | 2026-12-03 17:32:11 | Compute the distance between a point and a box exactly |