| From: | Amit Langote <amitlangote09(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | executor startup doesn't skip sub-partitions of a pruned partition |
| Date: | 2026-12-02 07:44:28 |
| Message-ID: | CA+HiwqH+o6iNLYk_SKpvqvj0EPkfuCuEh-iVSWyvzk8u=S=VCg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
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?
--
Thanks, Amit Langote
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Treat-partitions-under-a-pruned-sub-partitioned-t.patch | application/octet-stream | 11.9 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Chao Li | 2026-12-02 07:52:36 | Fix reversed slot types in copy_replication_slot() error messages |
| Previous Message | Julien Rouhaud | 2026-12-02 07:26:43 | Re: Missing CFI() in SplitIdentifierString |