Skip to content

Process each PlanState only once during parallel query setup - #83

Open
lfittl wants to merge 1 commit into
masterfrom
fix-parallel-subplan-double-visit-alt-alt
Open

lfittl wants to merge 1 commit into
masterfrom
fix-parallel-subplan-double-visit-alt-alt

Conversation

@lfittl

@lfittl lfittl commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Parallel query walks the PlanState tree several times, to size and initialize DSM state and to collect instrumentation. These walks assumed each PlanState is visited once, but that does not hold for SubPlans: when the same SubPlan is referenced by more than one expression (for example a hash clause and the hash keys derived from it), each reference has its own SubPlanState pointing at the same subplan PlanState, so planstate_walk_subplans() descends into that subtree once per reference.

The repeated visit makes ExecParallelInitializeDSM() insert a duplicate shm_toc key for each node under the subplan, which causes an assertion failure in shm_toc_insert() when doing EXPLAIN ANALYZE with such plans. Additionally it causes instrumentation data to be incorrect, inflating loop counts and losing per-worker details such as "Sort Method".

Fix by adding the planstate_tree_collect() helper, which returns the distinct PlanStates of a tree, and having the parallel setup and instrumentation code iterate over that list instead of recursing per reference.

ExecShutdownNode() has the same structure but is left unchanged: all in-core shutdown callbacks are idempotent, and reworking its nested instrumentation accounting is better done separately.

Parallel query walks the PlanState tree several times, to size and initialize
DSM state and to collect instrumentation. These walks assumed each PlanState
is visited once, but that does not hold for SubPlans: when the same SubPlan is
referenced by more than one expression (for example a hash clause and the hash
keys derived from it), each reference has its own SubPlanState pointing at the
same subplan PlanState, so planstate_walk_subplans() descends into that subtree
once per reference.

The repeated visit makes ExecParallelInitializeDSM() insert a duplicate shm_toc
key for each node under the subplan, which causes an assertion failure in
shm_toc_insert() when doing EXPLAIN ANALYZE with such plans. Additionally it
causes instrumentation data to be incorrect, inflating loop counts and
losing per-worker details such as "Sort Method".

Fix by adding the planstate_tree_collect() helper, which returns the distinct
PlanStates of a tree, and having the parallel setup and instrumentation code
iterate over that list instead of recursing per reference.

ExecShutdownNode() has the same structure but is left unchanged: all in-core
shutdown callbacks are idempotent, and reworking its nested instrumentation
accounting is better done separately.

Author: Lukas Fittl <lukas@fittl.com>
Reviewed-by:
Discussion:
@lfittl
lfittl force-pushed the fix-parallel-subplan-double-visit-alt-alt branch from c701fa8 to be8b48a Compare October 26, 2026 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant