Repository navigation
Conversation
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
force-pushed
the
fix-parallel-subplan-double-visit-alt-alt
branch
from
October 26, 2026 22:27
c701fa8 to
be8b48a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.