pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Assert failure in parallel query due to SubPlan double visit for hash join
9+ messages / 4 participants
[nested] [flat]

* Assert failure in parallel query due to SubPlan double visit for hash join
@ 2026-10-26 23:21  Lukas Fittl <lukas@fittl.com>
  0 siblings, 2 replies; 9+ messages in thread

From: Lukas Fittl @ 2026-10-26 23:21 UTC (permalink / raw)
  To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; +Cc: Andres Freund <andres@anarazel.de>

Hi,

Whilst stress testing the next version of the stack-based
instrumentation patch [0], I ran into what appears to be an ancient
bug in how we walk plan state for SubPlan nodes in parallel query
setup and instrumentation handling.

Specifically, the planstate_tree_walker callbacks in execParallel.c
assume that a given PlanState is only walked once, but that doesn't
hold true for hash joins since (I think, not tested) Postgres 10. We
create two separate references to the same subplan PlanState, one via
hashclauses and the other via hashkeys, each having their own
SubPlanState, but going to the same subplan PlanState. And
planstate_walk_subplans() descends into the subtree once per
reference.

I believe this is an issue since b8d7f053c5c (Postgres 10, CCing
Andres as the author of that one) changed how ExecInitHashJoin calls
ExecInitExpr, creating the second SubPlanState. More digging also
found the very old a64846f3ada that talked about a seemingly related
problem, but that must have happened before parallel query was in the
picture.

We can see this cause an assertion failure on master with the
following reproducer:

CREATE TABLE big (a int, b int);
INSERT INTO big SELECT g, g FROM generate_series(1, 10000) g;
CREATE TABLE small (x int PRIMARY KEY, y int);
INSERT INTO small SELECT g, g FROM generate_series(1, 1000) g;
CREATE TABLE other (k int PRIMARY KEY, f int);
INSERT INTO other SELECT g, g FROM generate_series(1, 1000) g;
ANALYZE big, small, other;
ALTER TABLE other SET (parallel_workers = 0);

SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SET min_parallel_table_scan_size = 0;
SET enable_nestloop = off;
SET enable_mergejoin = off;
SET enable_parallel_hash = off;
SET parallel_leader_participation = off; -- to show loop count more clearly

EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT COUNT(*) FROM big b JOIN small s ON s.x =
  CASE WHEN (b.a, b.b) NOT IN (SELECT o.k, o.f FROM other o ORDER BY o.f)
  THEN 1 ELSE 2 END;

TRAP: failed Assert("toc->toc_entry[i].key != key"), File:
"shm_toc.c", Line: 190

Besides the assertion failure it also causes problems for
instrumentation. In this example we get an incorrect loop count (the
subplan is really only run in the two workers, so the loops=16 below
should be 4), and the Sort node is missing the Sort Method reporting:

                         QUERY PLAN
  --------------------------------------------------------------
   Finalize Aggregate (actual rows=1.00 loops=1)
     ->  Gather (actual rows=2.00 loops=1)
           Workers Planned: 2
           Workers Launched: 2
           ->  Partial Aggregate (actual rows=1.00 loops=2)
                 ->  Hash Join (actual rows=5000.00 loops=2)
                       Hash Cond: (CASE WHEN (NOT (ANY ((b.a = (hashed
SubPlan any_1).col1) AND (b.b = (hashed SubPlan any_1).col2)))) THEN 1
ELSE 2 END = s.x)
                       ->  Parallel Seq Scan on big b (actual
rows=5000.00 loops=2)
                       ->  Hash (actual rows=1000.00 loops=2)
                             Buckets: 1024  Batches: 1  Memory Usage: 44kB
                             ->  Seq Scan on small s (actual
rows=1000.00 loops=2)
                       SubPlan any_1
                         ->  Sort (actual rows=1000.00 loops=16)
                               Sort Key: o.f
                               ->  Seq Scan on other o (actual
rows=1000.00 loops=16)

The assertion failure happens because ExecParallelInitializeDSM visits
that Sort twice and tries to shm_toc_insert() the same plan_node_id
key. The instrumentation issue happens because (1) for loops, we
accumulate the worker instrumentation twice in
ExecParallelReportInstrumentation and then again in
ExecParallelRetrieveInstrumentation for the nodes underneath the
shared SubPlan, (2) for "Sort Method" the leader and workers end up
pointing at different (duplicate) SharedSortInfo slots and the leader
looks at the empty one.

To fix this, the least convoluted way I could think of is to invent a
new mechanism to collect a deduplicated list of plan states, and then
call the existing logic on each list member. This also allows some
refactoring to remove logic for counting how many nodes we've visited.

With the attached patch, the instrumentation now shows correctly, and
the added regression test no longer fails with an assertion failure.

...
                       SubPlan any_1
                         ->  Sort (actual rows=1000.00 loops=4)
                               Sort Key: o.f
                               Worker 0:  Sort Method: quicksort  Memory: 48kB
                               Worker 1:  Sort Method: quicksort  Memory: 48kB
                               ->  Seq Scan on other o (actual
rows=1000.00 loops=4)

We could alternatively deal with this in the existing walkers, but
then we'd have to modify the walker context in four different places
to carry the identities of already visited plan states. I also assume
that changing how ExecInitHashJoin works is off the table, but I admit
that logic goes a bit over my head.

Note that technically the ExecShutdownNode_walker suffers from the
same problem, but it doesn't appear to be a practical problem with
in-core code today, and it's a bit more complicated to fix with the
current nested InstrStartNode assumptions (that I intend to fix
separately in the stack-based instrumentation series).

Thoughts?

Thanks,
Lukas

[0]: https://www.postgresql.org/message-id/flat/CAP53PkzdBK8VJ1fS4AZ481LgMN8f9mJiC39ZRHqkFUSYq6KWmg%40mai...

-- 
Lukas Fittl

Attachments:

v1-0001-Process-each-PlanState-only-once-during-parallel-.patch (text/x-patch, 31.4K) download From be8b48a7d1a74d0a3c3a2485757add0344186d94 Mon Sep 17 00:00:00 2001 From: Lukas Fittl <lukas@fittl.com> Date: Mon, 26 Oct 2026 07:17:12 -0700 Subject: [PATCH v1] Process each PlanState only once during parallel query setup 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: --- src/backend/executor/execParallel.c | 245 ++++++++---------- src/backend/nodes/nodeFuncs.c | 52 ++++ src/include/executor/execParallel.h | 2 + src/include/nodes/nodeFuncs.h | 2 + src/test/regress/expected/select_parallel.out | 66 +++++ src/test/regress/sql/select_parallel.sql | 46 ++++ src/tools/pgindent/typedefs.list | 3 +- 7 files changed, 273 insertions(+), 143 deletions(-) diff --git a/src/backend/executor/execParallel.c b/src/backend/executor/execParallel.c index 316dd3f67c6..49e40e08a6b 100644 --- a/src/backend/executor/execParallel.c +++ b/src/backend/executor/execParallel.c @@ -115,33 +115,12 @@ struct SharedExecutorInstrumentation (StaticAssertVariableIsOfTypeMacro(sei, SharedExecutorInstrumentation *), \ (NodeInstrumentation *) (((char *) sei) + sei->instrument_offset)) -/* Context object for ExecParallelEstimate. */ -typedef struct ExecParallelEstimateContext -{ - ParallelContext *pcxt; - int nnodes; -} ExecParallelEstimateContext; - -/* Context object for ExecParallelInitializeDSM. */ -typedef struct ExecParallelInitializeDSMContext -{ - ParallelContext *pcxt; - SharedExecutorInstrumentation *instrumentation; - int nnodes; -} ExecParallelInitializeDSMContext; - /* Helper functions that run in the parallel leader. */ static char *ExecSerializePlan(Plan *plan, EState *estate); -static bool ExecParallelEstimate(PlanState *planstate, - ExecParallelEstimateContext *e); -static bool ExecParallelInitializeDSM(PlanState *planstate, - ExecParallelInitializeDSMContext *d); static shm_mq_handle **ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize); -static bool ExecParallelReInitializeDSM(PlanState *planstate, +static void ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt); -static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, - SharedExecutorInstrumentation *instrumentation); /* Helper function that runs in the parallel worker. */ static DestReceiver *ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc); @@ -239,115 +218,104 @@ ExecSerializePlan(Plan *plan, EState *estate) * which is shared across all parallel workers. Before we size the DSM, give * them a chance to call shm_toc_estimate_chunk or shm_toc_estimate_keys on * &pcxt->estimator. - * - * While we're at it, count the number of PlanState nodes in the tree, so - * we know how many Instrumentation structures we need. */ -static bool -ExecParallelEstimate(PlanState *planstate, ExecParallelEstimateContext *e) +static void +ExecParallelEstimate(PlanState *planstate, ParallelContext *pcxt) { - if (planstate == NULL) - return false; - - /* Count this node. */ - e->nnodes++; - switch (nodeTag(planstate)) { case T_SeqScanState: if (planstate->plan->parallel_aware) ExecSeqScanEstimate((SeqScanState *) planstate, - e->pcxt); + pcxt); /* even when not parallel-aware, for EXPLAIN ANALYZE */ ExecSeqScanInstrumentEstimate((SeqScanState *) planstate, - e->pcxt); + pcxt); break; case T_IndexScanState: if (planstate->plan->parallel_aware) ExecIndexScanEstimate((IndexScanState *) planstate, - e->pcxt); + pcxt); /* even when not parallel-aware, for EXPLAIN ANALYZE */ ExecIndexScanInstrumentEstimate((IndexScanState *) planstate, - e->pcxt); + pcxt); break; case T_IndexOnlyScanState: if (planstate->plan->parallel_aware) ExecIndexOnlyScanEstimate((IndexOnlyScanState *) planstate, - e->pcxt); + pcxt); /* even when not parallel-aware, for EXPLAIN ANALYZE */ ExecIndexOnlyScanInstrumentEstimate((IndexOnlyScanState *) planstate, - e->pcxt); + pcxt); break; case T_BitmapIndexScanState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ ExecBitmapIndexScanEstimate((BitmapIndexScanState *) planstate, - e->pcxt); + pcxt); break; case T_ForeignScanState: if (planstate->plan->parallel_aware) ExecForeignScanEstimate((ForeignScanState *) planstate, - e->pcxt); + pcxt); break; case T_TidRangeScanState: if (planstate->plan->parallel_aware) ExecTidRangeScanEstimate((TidRangeScanState *) planstate, - e->pcxt); + pcxt); /* even when not parallel-aware, for EXPLAIN ANALYZE */ ExecTidRangeScanInstrumentEstimate((TidRangeScanState *) planstate, - e->pcxt); + pcxt); break; case T_AppendState: if (planstate->plan->parallel_aware) ExecAppendEstimate((AppendState *) planstate, - e->pcxt); + pcxt); break; case T_CustomScanState: if (planstate->plan->parallel_aware) ExecCustomScanEstimate((CustomScanState *) planstate, - e->pcxt); + pcxt); break; case T_BitmapHeapScanState: if (planstate->plan->parallel_aware) ExecBitmapHeapEstimate((BitmapHeapScanState *) planstate, - e->pcxt); + pcxt); /* even when not parallel-aware, for EXPLAIN ANALYZE */ ExecBitmapHeapInstrumentEstimate((BitmapHeapScanState *) planstate, - e->pcxt); + pcxt); break; case T_HashJoinState: if (planstate->plan->parallel_aware) ExecHashJoinEstimate((HashJoinState *) planstate, - e->pcxt); + pcxt); break; case T_HashState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecHashEstimate((HashState *) planstate, e->pcxt); + ExecHashEstimate((HashState *) planstate, pcxt); break; case T_SortState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecSortEstimate((SortState *) planstate, e->pcxt); + ExecSortEstimate((SortState *) planstate, pcxt); break; case T_IncrementalSortState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecIncrementalSortEstimate((IncrementalSortState *) planstate, e->pcxt); + ExecIncrementalSortEstimate((IncrementalSortState *) planstate, pcxt); break; case T_AggState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecAggEstimate((AggState *) planstate, e->pcxt); + ExecAggEstimate((AggState *) planstate, pcxt); break; case T_MemoizeState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecMemoizeEstimate((MemoizeState *) planstate, e->pcxt); + ExecMemoizeEstimate((MemoizeState *) planstate, pcxt); break; case T_MaterialState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecMaterialEstimate((MaterialState *) planstate, e->pcxt); + ExecMaterialEstimate((MaterialState *) planstate, pcxt); break; default: break; } - - return planstate_tree_walker(planstate, ExecParallelEstimate, e); } /* @@ -481,21 +449,9 @@ RestoreParamExecParams(char *start_address, EState *estate) * Initialize the dynamic shared memory segment that will be used to control * parallel execution. */ -static bool -ExecParallelInitializeDSM(PlanState *planstate, - ExecParallelInitializeDSMContext *d) +static void +ExecParallelInitializeDSM(PlanState *planstate, ParallelContext *pcxt) { - if (planstate == NULL) - return false; - - /* If instrumentation is enabled, initialize slot for this node. */ - if (d->instrumentation != NULL) - d->instrumentation->plan_node_id[d->nnodes] = - planstate->plan->plan_node_id; - - /* Count this node. */ - d->nnodes++; - /* * Call initializers for DSM-using plan nodes. * @@ -510,96 +466,94 @@ ExecParallelInitializeDSM(PlanState *planstate, case T_SeqScanState: if (planstate->plan->parallel_aware) ExecSeqScanInitializeDSM((SeqScanState *) planstate, - d->pcxt); + pcxt); /* even when not parallel-aware, for EXPLAIN ANALYZE */ ExecSeqScanInstrumentInitDSM((SeqScanState *) planstate, - d->pcxt); + pcxt); break; case T_IndexScanState: if (planstate->plan->parallel_aware) ExecIndexScanInitializeDSM((IndexScanState *) planstate, - d->pcxt); + pcxt); /* even when not parallel-aware, for EXPLAIN ANALYZE */ ExecIndexScanInstrumentInitDSM((IndexScanState *) planstate, - d->pcxt); + pcxt); break; case T_IndexOnlyScanState: if (planstate->plan->parallel_aware) ExecIndexOnlyScanInitializeDSM((IndexOnlyScanState *) planstate, - d->pcxt); + pcxt); /* even when not parallel-aware, for EXPLAIN ANALYZE */ ExecIndexOnlyScanInstrumentInitDSM((IndexOnlyScanState *) planstate, - d->pcxt); + pcxt); break; case T_BitmapIndexScanState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecBitmapIndexScanInitializeDSM((BitmapIndexScanState *) planstate, d->pcxt); + ExecBitmapIndexScanInitializeDSM((BitmapIndexScanState *) planstate, pcxt); break; case T_ForeignScanState: if (planstate->plan->parallel_aware) ExecForeignScanInitializeDSM((ForeignScanState *) planstate, - d->pcxt); + pcxt); break; case T_TidRangeScanState: if (planstate->plan->parallel_aware) ExecTidRangeScanInitializeDSM((TidRangeScanState *) planstate, - d->pcxt); + pcxt); /* even when not parallel-aware, for EXPLAIN ANALYZE */ ExecTidRangeScanInstrumentInitDSM((TidRangeScanState *) planstate, - d->pcxt); + pcxt); break; case T_AppendState: if (planstate->plan->parallel_aware) ExecAppendInitializeDSM((AppendState *) planstate, - d->pcxt); + pcxt); break; case T_CustomScanState: if (planstate->plan->parallel_aware) ExecCustomScanInitializeDSM((CustomScanState *) planstate, - d->pcxt); + pcxt); break; case T_BitmapHeapScanState: if (planstate->plan->parallel_aware) ExecBitmapHeapInitializeDSM((BitmapHeapScanState *) planstate, - d->pcxt); + pcxt); /* even when not parallel-aware, for EXPLAIN ANALYZE */ ExecBitmapHeapInstrumentInitDSM((BitmapHeapScanState *) planstate, - d->pcxt); + pcxt); break; case T_HashJoinState: if (planstate->plan->parallel_aware) ExecHashJoinInitializeDSM((HashJoinState *) planstate, - d->pcxt); + pcxt); break; case T_HashState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecHashInitializeDSM((HashState *) planstate, d->pcxt); + ExecHashInitializeDSM((HashState *) planstate, pcxt); break; case T_SortState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecSortInitializeDSM((SortState *) planstate, d->pcxt); + ExecSortInitializeDSM((SortState *) planstate, pcxt); break; case T_IncrementalSortState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecIncrementalSortInitializeDSM((IncrementalSortState *) planstate, d->pcxt); + ExecIncrementalSortInitializeDSM((IncrementalSortState *) planstate, pcxt); break; case T_AggState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecAggInitializeDSM((AggState *) planstate, d->pcxt); + ExecAggInitializeDSM((AggState *) planstate, pcxt); break; case T_MemoizeState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecMemoizeInitializeDSM((MemoizeState *) planstate, d->pcxt); + ExecMemoizeInitializeDSM((MemoizeState *) planstate, pcxt); break; case T_MaterialState: /* even when not parallel-aware, for EXPLAIN ANALYZE */ - ExecMaterialInitializeDSM((MaterialState *) planstate, d->pcxt); + ExecMaterialInitializeDSM((MaterialState *) planstate, pcxt); break; default: break; } - - return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d); } /* @@ -664,8 +618,9 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, { ParallelExecutorInfo *pei; ParallelContext *pcxt; - ExecParallelEstimateContext e; - ExecParallelInitializeDSMContext d; + List *nodes; + ListCell *lc; + int nnodes; FixedParallelExecutorState *fpes; char *pstmt_data; char *pstmt_space; @@ -758,24 +713,28 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, shm_toc_estimate_keys(&pcxt->estimator, 1); /* - * Give parallel-aware nodes a chance to add to the estimates, and get a - * count of how many PlanState nodes there are. + * Give parallel-aware nodes a chance to add to the estimates. The node + * count determines how many Instrumentation structures we need. The node + * list is kept around for the later walks (DSM reinitialization and + * instrumentation retrieval). */ - e.pcxt = pcxt; - e.nnodes = 0; - ExecParallelEstimate(planstate, &e); + nodes = planstate_tree_collect(planstate); + nnodes = list_length(nodes); + pei->planstate_nodes = nodes; + foreach(lc, nodes) + ExecParallelEstimate((PlanState *) lfirst(lc), pcxt); /* Estimate space for instrumentation, if required. */ if (estate->es_instrument) { instrumentation_len = offsetof(SharedExecutorInstrumentation, plan_node_id) + - sizeof(int) * e.nnodes; + sizeof(int) * nnodes; instrumentation_len = MAXALIGN(instrumentation_len); instrument_offset = instrumentation_len; instrumentation_len += mul_size(sizeof(NodeInstrumentation), - mul_size(e.nnodes, nworkers)); + mul_size(nnodes, nworkers)); shm_toc_estimate_chunk(&pcxt->estimator, instrumentation_len); shm_toc_estimate_keys(&pcxt->estimator, 1); @@ -867,9 +826,9 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, instrumentation->instrument_options = estate->es_instrument; instrumentation->instrument_offset = instrument_offset; instrumentation->num_workers = nworkers; - instrumentation->num_plan_nodes = e.nnodes; + instrumentation->num_plan_nodes = nnodes; instrument = GetInstrumentationArray(instrumentation); - for (i = 0; i < nworkers * e.nnodes; ++i) + for (i = 0; i < nworkers * nnodes; ++i) InstrInitNode(&instrument[i], estate->es_instrument, false); shm_toc_insert(pcxt->toc, PARALLEL_KEY_INSTRUMENTATION, instrumentation); @@ -922,21 +881,25 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, * This also initializes the elements of instrumentation->ps_instrument, * if it exists. */ - d.pcxt = pcxt; - d.instrumentation = instrumentation; - d.nnodes = 0; - - /* Install our DSA area while initializing the plan. */ - estate->es_query_dsa = pei->area; - ExecParallelInitializeDSM(planstate, &d); - estate->es_query_dsa = NULL; /* - * Make sure that the world hasn't shifted under our feet. This could - * probably just be an Assert(), but let's be conservative for now. + * Install our DSA area while initializing the plan. Fill the + * instrumentation array's plan_node_id slots in the same node order that + * ExecParallelRetrieveInstrumentation and the workers will see, since all + * of them iterate over planstate_tree_collect results. */ - if (e.nnodes != d.nnodes) - elog(ERROR, "inconsistent count of PlanState nodes"); + estate->es_query_dsa = pei->area; + foreach(lc, nodes) + { + PlanState *ps = (PlanState *) lfirst(lc); + + if (instrumentation != NULL) + instrumentation->plan_node_id[foreach_current_index(lc)] = + ps->plan->plan_node_id; + + ExecParallelInitializeDSM(ps, pcxt); + } + estate->es_query_dsa = NULL; /* OK, we're ready to rock and roll. */ return pei; @@ -979,6 +942,7 @@ ExecParallelReinitialize(PlanState *planstate, Bitmapset *sendParams) { EState *estate = planstate->state; + ListCell *lc; FixedParallelExecutorState *fpes; /* Old workers must already be shut down */ @@ -1015,20 +979,18 @@ ExecParallelReinitialize(PlanState *planstate, /* Traverse plan tree and let each child node reset associated state. */ estate->es_query_dsa = pei->area; - ExecParallelReInitializeDSM(planstate, pei->pcxt); + foreach(lc, pei->planstate_nodes) + ExecParallelReInitializeDSM((PlanState *) lfirst(lc), pei->pcxt); estate->es_query_dsa = NULL; } /* - * Traverse plan tree to reinitialize per-node dynamic shared memory state + * Reinitialize per-node dynamic shared memory state */ -static bool +static void ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt) { - if (planstate == NULL) - return false; - /* * Call reinitializers for DSM-using plan nodes. */ @@ -1091,14 +1053,13 @@ ExecParallelReInitializeDSM(PlanState *planstate, break; } - return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt); } /* * Copy instrumentation information about this node and its descendants from * dynamic shared memory. */ -static bool +static void ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation) { @@ -1181,8 +1142,6 @@ ExecParallelRetrieveInstrumentation(PlanState *planstate, break; } - return planstate_tree_walker(planstate, ExecParallelRetrieveInstrumentation, - instrumentation); } /* @@ -1284,10 +1243,13 @@ ExecParallelFinish(ParallelExecutorInfo *pei) void ExecParallelCleanup(ParallelExecutorInfo *pei) { + ListCell *lc; + /* Accumulate instrumentation, if any. */ if (pei->instrumentation) - ExecParallelRetrieveInstrumentation(pei->planstate, - pei->instrumentation); + foreach(lc, pei->planstate_nodes) + ExecParallelRetrieveInstrumentation((PlanState *) lfirst(lc), + pei->instrumentation); /* Accumulate JIT instrumentation, if any. */ if (pei->jit_instrumentation) @@ -1310,6 +1272,7 @@ ExecParallelCleanup(ParallelExecutorInfo *pei) DestroyParallelContext(pei->pcxt); pei->pcxt = NULL; } + list_free(pei->planstate_nodes); pfree(pei); } @@ -1365,7 +1328,7 @@ ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, * Copy instrumentation information from this node and its descendants into * dynamic shared memory, so that the parallel leader can retrieve it. */ -static bool +static void ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation) { @@ -1397,8 +1360,6 @@ ExecParallelReportInstrumentation(PlanState *planstate, Assert(ParallelWorkerNumber < instrumentation->num_workers); InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument); - return planstate_tree_walker(planstate, ExecParallelReportInstrumentation, - instrumentation); } /* @@ -1406,12 +1367,10 @@ ExecParallelReportInstrumentation(PlanState *planstate, * retrieved from shared memory. This has to be done once the PlanState * is allocated and initialized by executor; that is, after ExecutorStart(). */ -static bool -ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt) +static void +ExecParallelInitializeWorker(PlanState *planstate, + ParallelWorkerContext *pwcxt) { - if (planstate == NULL) - return false; - switch (nodeTag(planstate)) { case T_SeqScanState: @@ -1505,8 +1464,6 @@ ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt) break; } - return planstate_tree_walker(planstate, ExecParallelInitializeWorker, - pwcxt); } /* @@ -1535,6 +1492,8 @@ ParallelQueryMain(dsm_segment *seg, shm_toc *toc) QueryDesc *queryDesc; SharedExecutorInstrumentation *instrumentation; SharedJitInstrumentation *jit_instrumentation; + List *nodes; + ListCell *lc; int instrument_options = 0; void *area_space; dsa_area *area; @@ -1577,7 +1536,9 @@ ParallelQueryMain(dsm_segment *seg, shm_toc *toc) } pwcxt.toc = toc; pwcxt.seg = seg; - ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt); + nodes = planstate_tree_collect(queryDesc->planstate); + foreach(lc, nodes) + ExecParallelInitializeWorker((PlanState *) lfirst(lc), &pwcxt); /* Pass down any tuple bound */ ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate); @@ -1610,8 +1571,10 @@ ParallelQueryMain(dsm_segment *seg, shm_toc *toc) /* Report instrumentation data if any instrumentation options are set. */ if (instrumentation != NULL) - ExecParallelReportInstrumentation(queryDesc->planstate, - instrumentation); + foreach(lc, nodes) + ExecParallelReportInstrumentation((PlanState *) lfirst(lc), + instrumentation); + list_free(nodes); /* Report JIT instrumentation data if any */ if (queryDesc->estate->es_jit && jit_instrumentation != NULL) diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index 678b9a046ce..935f28118fe 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -4860,3 +4860,55 @@ planstate_walk_members(PlanState **planstates, int nplans, return false; } + +typedef struct planstate_tree_collect_context +{ + List *nodes; /* result, in depth-first pre-order */ + Bitmapset *seen; /* plan_node_ids already in 'nodes' */ +} planstate_tree_collect_context; + +static bool +planstate_tree_collect_walker(PlanState *planstate, + planstate_tree_collect_context *context) +{ + /* plan_node_id is unique across the whole PlannedStmt */ + int plan_node_id = planstate->plan->plan_node_id; + + if (bms_is_member(plan_node_id, context->seen)) + return false; + context->seen = bms_add_member(context->seen, plan_node_id); + context->nodes = lappend(context->nodes, planstate); + + return planstate_tree_walker(planstate, planstate_tree_collect_walker, + context); +} + +/* + * planstate_tree_collect --- list all PlanStates of a plan state tree + * + * Returns a freshly-allocated list of every distinct PlanState reachable + * from 'planstate', including 'planstate' itself, in the order a + * planstate_tree_walker traversal would first visit them (depth-first, + * pre-order). + * + * Unlike a recursive walk with planstate_tree_walker, each node appears + * exactly once in the result. That matters for SubPlans: the same SubPlan + * (identified by plan_id) can be referenced from several expressions, e.g. + * a hash clause along with the hash key expressions derived from it. + * + * The caller is responsible for freeing the list. + */ +List * +planstate_tree_collect(PlanState *planstate) +{ + planstate_tree_collect_context context; + + context.nodes = NIL; + context.seen = NULL; + + (void) planstate_tree_collect_walker(planstate, &context); + + bms_free(context.seen); + + return context.nodes; +} diff --git a/src/include/executor/execParallel.h b/src/include/executor/execParallel.h index 5a2034811d5..074d2ef6720 100644 --- a/src/include/executor/execParallel.h +++ b/src/include/executor/execParallel.h @@ -24,6 +24,8 @@ typedef struct SharedExecutorInstrumentation SharedExecutorInstrumentation; typedef struct ParallelExecutorInfo { PlanState *planstate; /* plan subtree we're running in parallel */ + List *planstate_nodes; /* all distinct nodes of that subtree, + * from planstate_tree_collect */ ParallelContext *pcxt; /* parallel context we're using */ BufferUsage *buffer_usage; /* points to bufusage area in DSM */ WalUsage *wal_usage; /* walusage area in DSM */ diff --git a/src/include/nodes/nodeFuncs.h b/src/include/nodes/nodeFuncs.h index 696ffdebd4b..46d3d040b83 100644 --- a/src/include/nodes/nodeFuncs.h +++ b/src/include/nodes/nodeFuncs.h @@ -221,4 +221,6 @@ extern bool planstate_tree_walker_impl(PlanState *planstate, planstate_tree_walker_callback walker, void *context); +extern List *planstate_tree_collect(PlanState *planstate); + #endif /* NODEFUNCS_H */ diff --git a/src/test/regress/expected/select_parallel.out b/src/test/regress/expected/select_parallel.out index e1344215644..32c3b2ab535 100644 --- a/src/test/regress/expected/select_parallel.out +++ b/src/test/regress/expected/select_parallel.out @@ -334,6 +334,72 @@ explain (costs off) -> Seq Scan on tenk2 (4 rows) +-- test instrumentation of a subplan shared by several expressions (here a +-- hash clause and the hash keys derived from it), which the parallel-query +-- walks must process only once +set enable_nestloop = off; +set enable_mergejoin = off; +set enable_indexscan = off; +set enable_indexonlyscan = off; +set parallel_leader_participation = off; +create function explain_parallel_shared_subplan() returns setof text +language plpgsql as +$$ +declare ln text; +declare prev text; +begin + for ln in + explain (analyze, timing off, summary off, costs off, buffers off) + select count(*) from tenk1 t1 join tenk1 t2 + on t2.unique1 = case when t1.hundred not in + (select thousand from tenk2 order by twothousand limit 50) + then 1 else 2 end + loop + ln := regexp_replace(ln, 'actual rows=[0-9.]+ loops=[0-9]+', + 'actual rows=N loops=N'); + ln := regexp_replace(ln, 'Memory: \S*', 'Memory: xxx'); + ln := regexp_replace(ln, 'Memory Usage: \S*', 'Memory Usage: xxx'); + -- the launched worker count and the number of per-worker "Sort + -- Method" lines vary with how many workers start and get rows; + -- normalize both so the test only confirms that per-worker sort + -- statistics are reported at all (they vanish without the fix) + ln := regexp_replace(ln, 'Workers Launched: \d+', 'Workers Launched: N'); + ln := regexp_replace(ln, 'Worker \d+:', 'Worker N:'); + if ln is distinct from prev then + return next ln; + end if; + prev := ln; + end loop; +end; +$$; +select * from explain_parallel_shared_subplan(); + explain_parallel_shared_subplan +---------------------------------------------------------------------------------------------------------------------------------- + Finalize Aggregate (actual rows=N loops=N) + -> Gather (actual rows=N loops=N) + Workers Planned: 4 + Workers Launched: N + -> Partial Aggregate (actual rows=N loops=N) + -> Parallel Hash Join (actual rows=N loops=N) + Hash Cond: (CASE WHEN (NOT (ANY (t1.hundred = (hashed SubPlan any_1).col1))) THEN 1 ELSE 2 END = t2.unique1) + -> Parallel Seq Scan on tenk1 t1 (actual rows=N loops=N) + -> Parallel Hash (actual rows=N loops=N) + Buckets: 16384 Batches: 1 Memory Usage: xxx + -> Parallel Seq Scan on tenk1 t2 (actual rows=N loops=N) + SubPlan any_1 + -> Limit (actual rows=N loops=N) + -> Sort (actual rows=N loops=N) + Sort Key: tenk2.twothousand + Worker N: Sort Method: top-N heapsort Memory: xxx + -> Seq Scan on tenk2 (actual rows=N loops=N) +(17 rows) + +drop function explain_parallel_shared_subplan(); +reset parallel_leader_participation; +reset enable_indexscan; +reset enable_indexonlyscan; +reset enable_nestloop; +reset enable_mergejoin; alter table tenk2 reset (parallel_workers); -- test parallel plan for a query containing initplan. set enable_indexscan = off; diff --git a/src/test/regress/sql/select_parallel.sql b/src/test/regress/sql/select_parallel.sql index 71a75bc86ea..b34d650fe1d 100644 --- a/src/test/regress/sql/select_parallel.sql +++ b/src/test/regress/sql/select_parallel.sql @@ -122,6 +122,52 @@ select count(*) from tenk1 where (two, four) not in explain (costs off) select * from tenk1 where (unique1 + random())::integer not in (select ten from tenk2); + +-- test instrumentation of a subplan shared by several expressions (here a +-- hash clause and the hash keys derived from it), which the parallel-query +-- walks must process only once +set enable_nestloop = off; +set enable_mergejoin = off; +set enable_indexscan = off; +set enable_indexonlyscan = off; +set parallel_leader_participation = off; +create function explain_parallel_shared_subplan() returns setof text +language plpgsql as +$$ +declare ln text; +declare prev text; +begin + for ln in + explain (analyze, timing off, summary off, costs off, buffers off) + select count(*) from tenk1 t1 join tenk1 t2 + on t2.unique1 = case when t1.hundred not in + (select thousand from tenk2 order by twothousand limit 50) + then 1 else 2 end + loop + ln := regexp_replace(ln, 'actual rows=[0-9.]+ loops=[0-9]+', + 'actual rows=N loops=N'); + ln := regexp_replace(ln, 'Memory: \S*', 'Memory: xxx'); + ln := regexp_replace(ln, 'Memory Usage: \S*', 'Memory Usage: xxx'); + -- the launched worker count and the number of per-worker "Sort + -- Method" lines vary with how many workers start and get rows; + -- normalize both so the test only confirms that per-worker sort + -- statistics are reported at all (they vanish without the fix) + ln := regexp_replace(ln, 'Workers Launched: \d+', 'Workers Launched: N'); + ln := regexp_replace(ln, 'Worker \d+:', 'Worker N:'); + if ln is distinct from prev then + return next ln; + end if; + prev := ln; + end loop; +end; +$$; +select * from explain_parallel_shared_subplan(); +drop function explain_parallel_shared_subplan(); +reset parallel_leader_participation; +reset enable_indexscan; +reset enable_indexonlyscan; +reset enable_nestloop; +reset enable_mergejoin; alter table tenk2 reset (parallel_workers); -- test parallel plan for a query containing initplan. diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index a7a8dbdd23f..de67d4fc9d0 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -776,8 +776,6 @@ ExecForeignDelete_function ExecForeignInsert_function ExecForeignTruncate_function ExecForeignUpdate_function -ExecParallelEstimateContext -ExecParallelInitializeDSMContext ExecPhraseData ExecProcNodeMtd ExecRowMark @@ -4138,6 +4136,7 @@ pivot_field planner_hook_type planner_setup_hook_type planner_shutdown_hook_type +planstate_tree_collect_context planstate_tree_walker_callback plperl_array_info plperl_call_data -- 2.53.0
^ permalink raw reply [nested|flat] 9+ messages in thread

* Re: Assert failure in parallel query due to SubPlan double visit for hash join
@ 2026-10-29 00:17  Sami Imseih <samimseih.pg@gmail.com>
  parent: Lukas Fittl <lukas@fittl.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Sami Imseih @ 2026-10-29 00:17 UTC (permalink / raw)
  To: Lukas Fittl <lukas@fittl.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andres Freund <andres@anarazel.de>

Hi Lukas,

> The assertion failure happens because ExecParallelInitializeDSM visits
> that Sort twice and tries to shm_toc_insert() the same plan_node_id
> key.

I confirmed this also on an assert build.

> The instrumentation issue happens because (1) for loops, we
> accumulate the worker instrumentation twice in
> ExecParallelReportInstrumentation and then again in
> ExecParallelRetrieveInstrumentation for the nodes underneath the
> shared SubPlan, (2) for "Sort Method" the leader and workers end up
> pointing at different (duplicate) SharedSortInfo slots and the leader
> looks at the empty one.

In a non-assert build, this kind of case is definitely not easy to spot,
unless someone is really scrutinizing these parts of the plan.

Your patch looks fine to me. I just had a few small comments:

1/ Maybe rename planstate_tree_collect() to something like
planstate_tree_collect_distinct(). The important part here is that each
distinct PlanState appears in the returned list only once, so the name should
probably say that.

2/ Some execParallel.c comments still read as if the functions recurse. For
example ExecParallelRetrieveInstrumentation(),
ExecParallelReportInstrumentation(),
and ExecParallelInitializeWorker() now only deal with a single
PlanState at a time.

--
Sami Imseih
Amazon Web Services (AWS)






^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Assert failure in parallel query due to SubPlan double visit for hash join
@ 2026-10-29 08:26  David Geier <geidav.pg@gmail.com>
  parent: Lukas Fittl <lukas@fittl.com>
  1 sibling, 1 reply; 9+ messages in thread

From: David Geier @ 2026-10-29 08:26 UTC (permalink / raw)
  To: Lukas Fittl <lukas@fittl.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; +Cc: Andres Freund <andres@anarazel.de>

Hi Lukas!

> Whilst stress testing the next version of the stack-based
> instrumentation patch [0], I ran into what appears to be an ancient
> bug in how we walk plan state for SubPlan nodes in parallel query
> setup and instrumentation handling.

Good find.

> Specifically, the planstate_tree_walker callbacks in execParallel.c
> assume that a given PlanState is only walked once, but that doesn't
> hold true for hash joins since (I think, not tested) Postgres 10. We
> create two separate references to the same subplan PlanState, one via
> hashclauses and the other via hashkeys, each having their own
> SubPlanState, but going to the same subplan PlanState. And
> planstate_walk_subplans() descends into the subtree once per
> reference.

Yes. I've confirmed this and put some details below.

> I believe this is an issue since b8d7f053c5c (Postgres 10, CCing
> Andres as the author of that one) changed how ExecInitHashJoin calls
> ExecInitExpr, creating the second SubPlanState. More digging also
> found the very old a64846f3ada that talked about a seemingly related
> problem, but that must have happened before parallel query was in the
> picture.

(gdb) p *planstate
$17 = {
  type = T_HashJoinState,
  plan = 0x5a9ef8df0040,
  state = 0x5a9ef8ddfed0,
  ...
(gdb) p ((SubPlanState *)planstate->subPlan->elements[0])->planstate
$15 = (PlanState *) 0x5a9ef8de01e0
(gdb) p ((SubPlanState *)planstate->subPlan->elements[1])->planstate
$16 = (PlanState *) 0x5a9ef8de01e0

This happens in ExecInitSubPlanExpr() which adds the sort PlanState
twice to the subPlan list.

> We can see this cause an assertion failure on master with the
> following reproducer:
> 
> CREATE TABLE big (a int, b int);
> INSERT INTO big SELECT g, g FROM generate_series(1, 10000) g;
> CREATE TABLE small (x int PRIMARY KEY, y int);
> INSERT INTO small SELECT g, g FROM generate_series(1, 1000) g;
> CREATE TABLE other (k int PRIMARY KEY, f int);
> INSERT INTO other SELECT g, g FROM generate_series(1, 1000) g;
> ANALYZE big, small, other;
> ALTER TABLE other SET (parallel_workers = 0);
> 
> SET parallel_setup_cost = 0;
> SET parallel_tuple_cost = 0;
> SET min_parallel_table_scan_size = 0;
> SET enable_nestloop = off;
> SET enable_mergejoin = off;
> SET enable_parallel_hash = off;
> SET parallel_leader_participation = off; -- to show loop count more clearly
> 
> EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
> SELECT COUNT(*) FROM big b JOIN small s ON s.x =
>   CASE WHEN (b.a, b.b) NOT IN (SELECT o.k, o.f FROM other o ORDER BY o.f)
>   THEN 1 ELSE 2 END;
> 
> TRAP: failed Assert("toc->toc_entry[i].key != key"), File:
> "shm_toc.c", Line: 190

I can reproduce the assertion.

> Besides the assertion failure it also causes problems for
> instrumentation. In this example we get an incorrect loop count (the
> subplan is really only run in the two workers, so the loops=16 below
> should be 4), and the Sort node is missing the Sort Method reporting:
> 
>                          QUERY PLAN
>   --------------------------------------------------------------
>    Finalize Aggregate (actual rows=1.00 loops=1)
>      ->  Gather (actual rows=2.00 loops=1)
>            Workers Planned: 2
>            Workers Launched: 2
>            ->  Partial Aggregate (actual rows=1.00 loops=2)
>                  ->  Hash Join (actual rows=5000.00 loops=2)
>                        Hash Cond: (CASE WHEN (NOT (ANY ((b.a = (hashed
> SubPlan any_1).col1) AND (b.b = (hashed SubPlan any_1).col2)))) THEN 1
> ELSE 2 END = s.x)
>                        ->  Parallel Seq Scan on big b (actual
> rows=5000.00 loops=2)
>                        ->  Hash (actual rows=1000.00 loops=2)
>                              Buckets: 1024  Batches: 1  Memory Usage: 44kB
>                              ->  Seq Scan on small s (actual
> rows=1000.00 loops=2)
>                        SubPlan any_1
>                          ->  Sort (actual rows=1000.00 loops=16)
>                                Sort Key: o.f
>                                ->  Seq Scan on other o (actual
> rows=1000.00 loops=16)
> 
> The assertion failure happens because ExecParallelInitializeDSM visits
> that Sort twice and tries to shm_toc_insert() the same plan_node_id
> key. The instrumentation issue happens because (1) for loops, we
> accumulate the worker instrumentation twice in
> ExecParallelReportInstrumentation and then again in
> ExecParallelRetrieveInstrumentation for the nodes underneath the
> shared SubPlan, (2) for "Sort Method" the leader and workers end up
> pointing at different (duplicate) SharedSortInfo slots and the leader
> looks at the empty one.
> 
> To fix this, the least convoluted way I could think of is to invent a
> new mechanism to collect a deduplicated list of plan states, and then
> call the existing logic on each list member. This also allows some
> refactoring to remove logic for counting how many nodes we've visited.

While I like generally like your fix and the resulting code
simplifications, I'm wondering if it's the right fix.

It seems to me that the PlanState graph structure is broken for the case
you encountered. While it's generally possible that a PlanState is
referenced more than once (e.g. multiple CteScanStates reference the
same CteScanState::cteplanstate), it's weird that the same PlanState
references another PlanState more than once.

Beyond that, I'm wondering how much other code is broken or can break if
we don't change this structurally in the graph. You found already
another one in ExecShutdownNode().

A simple alternative fix would to ensure inExecInitSubPlanExpr() that
the subPlan list is always unique.

> With the attached patch, the instrumentation now shows correctly, and
> the added regression test no longer fails with an assertion failure.
> 
> ...
>                        SubPlan any_1
>                          ->  Sort (actual rows=1000.00 loops=4)
>                                Sort Key: o.f
>                                Worker 0:  Sort Method: quicksort  Memory: 48kB
>                                Worker 1:  Sort Method: quicksort  Memory: 48kB
>                                ->  Seq Scan on other o (actual
> rows=1000.00 loops=4)
> 
> We could alternatively deal with this in the existing walkers, but
> then we'd have to modify the walker context in four different places
> to carry the identities of already visited plan states. 

Really? Can't you check based on state you have already decide if it's
already initialized and if so bail? For example in ExecSortInitializeDSM():

if (node->shared_info != NULL)
	return;

> I also assume
> that changing how ExecInitHashJoin works is off the table, but I admit
> that logic goes a bit over my head.

It's these two calls that cause the sort PlanState to be added twice:

hjstate->hj_OuterHash =
	ExecBuildHash32Expr(hjstate->js.ps.ps_ResultTupleDesc,
			hjstate->js.ps.resultops,
			outer_hashfuncid,
			node->hashcollations,
			node->hashkeys,
			hash_strict,
			&hjstate->js.ps,
			0);

and

hjstate->hashclauses =
	ExecInitQual(node->hashclauses, (PlanState *) hjstate);

Both, the hash clauses and the qual, reference the sub-plan. How would
you avoid this? Also, it's too easy for other plan nodes to do the same.
Hence, i think it's better to fix it structurally.

> Note that technically the ExecShutdownNode_walker suffers from the
> same problem, but it doesn't appear to be a practical problem with
> in-core code today, and it's a bit more complicated to fix with the
> current nested InstrStartNode assumptions (that I intend to fix
> separately in the stack-based instrumentation series).

Can you give some more details why it's not a problem in-core and what
the problem with InstrStart/StopNode() is?

Any node sitting below the Sort node, which doesn't like having its
shutdown function called twice, might cause problems. Especially,
CustomScanStates that we don't have full control over could be problematic.

Seems to me that it would be good to fix it all in one go. But maybe I'm
not seeing the complications clearly enough.

-- 
David Geier






^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Assert failure in parallel query due to SubPlan double visit for hash join
@ 2026-10-29 22:07  Lukas Fittl <lukas@fittl.com>
  parent: Sami Imseih <samimseih.pg@gmail.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Lukas Fittl @ 2026-10-29 22:07 UTC (permalink / raw)
  To: Sami Imseih <samimseih.pg@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andres Freund <andres@anarazel.de>

Hi Sami,

On Wed, Oct 28, 2026 at 5:17 PM Sami Imseih <samimseih.pg@gmail.com> wrote:
> In a non-assert build, this kind of case is definitely not easy to spot,
> unless someone is really scrutinizing these parts of the plan.

Yeah, agreed - its also a somewhat unlikely plan to have, so I don't
think this is seen much in practice, and if it is, people won't
realize its wrong.

> Your patch looks fine to me. I just had a few small comments:

Thanks for reviewing!

> 1/ Maybe rename planstate_tree_collect() to something like
> planstate_tree_collect_distinct(). The important part here is that each
> distinct PlanState appears in the returned list only once, so the name should
> probably say that.

Yeah, good point on naming - if we stay with this structure I'll adjust.

> 2/ Some execParallel.c comments still read as if the functions recurse. For
> example ExecParallelRetrieveInstrumentation(),
> ExecParallelReportInstrumentation(),
> and ExecParallelInitializeWorker() now only deal with a single
> PlanState at a time.

Good point - there are a few that clearly need updating (e.g.
ExecParallelRetrieveInstrumentation jumps out), will revise.

Thanks,
Lukas

-- 
Lukas Fittl






^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Assert failure in parallel query due to SubPlan double visit for hash join
@ 2026-10-29 22:56  Lukas Fittl <lukas@fittl.com>
  parent: David Geier <geidav.pg@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Lukas Fittl @ 2026-10-29 22:56 UTC (permalink / raw)
  To: David Geier <geidav.pg@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andres Freund <andres@anarazel.de>

Hi David,

On Thu, Oct 29, 2026 at 1:26 AM David Geier <geidav.pg@gmail.com> wrote:
> > I believe this is an issue since b8d7f053c5c (Postgres 10, CCing
> > Andres as the author of that one) changed how ExecInitHashJoin calls
> > ExecInitExpr, creating the second SubPlanState. More digging also
> > found the very old a64846f3ada that talked about a seemingly related
> > problem, but that must have happened before parallel query was in the
> > picture.
>
> (gdb) p *planstate
> $17 = {
>   type = T_HashJoinState,
>   plan = 0x5a9ef8df0040,
>   state = 0x5a9ef8ddfed0,
>   ...
> (gdb) p ((SubPlanState *)planstate->subPlan->elements[0])->planstate
> $15 = (PlanState *) 0x5a9ef8de01e0
> (gdb) p ((SubPlanState *)planstate->subPlan->elements[1])->planstate
> $16 = (PlanState *) 0x5a9ef8de01e0
>
> This happens in ExecInitSubPlanExpr() which adds the sort PlanState
> twice to the subPlan list.

Yep, exactly - thanks for adding those details.

> > To fix this, the least convoluted way I could think of is to invent a
> > new mechanism to collect a deduplicated list of plan states, and then
> > call the existing logic on each list member. This also allows some
> > refactoring to remove logic for counting how many nodes we've visited.
>
> While I like generally like your fix and the resulting code
> simplifications, I'm wondering if it's the right fix.
>
> It seems to me that the PlanState graph structure is broken for the case
> you encountered. While it's generally possible that a PlanState is
> referenced more than once (e.g. multiple CteScanStates reference the
> same CteScanState::cteplanstate), it's weird that the same PlanState
> references another PlanState more than once.

Right - I agree.

>
> Beyond that, I'm wondering how much other code is broken or can break if
> we don't change this structurally in the graph. You found already
> another one in ExecShutdownNode().
>
> A simple alternative fix would to ensure inExecInitSubPlanExpr() that
> the subPlan list is always unique.

Yeah, that would be elegant, but I don't think it works. The problem
is that we can also have two different nodes reference the same
PlanState, like in this example with the same SubPlan being referenced
by different nodes underneath an Append (courtesy of Claude, FWIW):

  create table t2s as
    select i % 1000 as thousand, i % 2000 as twothousand
    from generate_series(0, 999) i;
  alter table t2s set (parallel_workers = 0);

  -- Hash-partitioned table. The WHERE clause below is copied to every
  -- partition scan, each with its own SubPlanState for the same subplan.
  create table tp (unique1 int, hundred int) partition by hash (unique1);
  create table tp0 partition of tp for values with (modulus 3, remainder 0);
  create table tp1 partition of tp for values with (modulus 3, remainder 1);
  create table tp2 partition of tp for values with (modulus 3, remainder 2);
  insert into tp select i, i % 100 from generate_series(0, 299999) i;

  vacuum analyze t2s;
  vacuum analyze tp;

  -- Make a parallel plan attractive on a small test table.
  set parallel_leader_participation = off;
  set parallel_setup_cost = 0;
  set parallel_tuple_cost = 0;
  set min_parallel_table_scan_size = 0;
  set max_parallel_workers_per_gather = 2;

  explain (analyze, costs off, timing off, summary off, buffers off)
  select count(*)
  from tp
  where hundred = case when 5 not in
          (select thousand from t2s order by twothousand limit 50)
        then 1 else 2 end;

On master this shows in a non-assert enabled build (note the missing
Sort Method in the SubPlan):

   Finalize Aggregate
     ->  Gather
           Workers Planned: 2
           ->  Partial Aggregate
                 ->  Parallel Append
                       ->  Parallel Seq Scan on tp0 tp_1
                             Filter: (hundred = CASE WHEN (NOT (ANY (5
= (hashed SubPlan any_1).col1))) THEN 1 ELSE 2 END)
                             SubPlan any_1
                               ->  Limit
                                     ->  Sort
                                           Sort Key: t2s.twothousand
                                           ->  Seq Scan on t2s
                       ->  Parallel Seq Scan on tp1 tp_2
                             Filter: (hundred = CASE WHEN (NOT (ANY (5
= (hashed SubPlan any_1).col1))) THEN 1 ELSE 2 END)
                       ->  Parallel Seq Scan on tp2 tp_3
                             Filter: (hundred = CASE WHEN (NOT (ANY (5
= (hashed SubPlan any_1).col1))) THEN 1 ELSE 2 END)

>
> > With the attached patch, the instrumentation now shows correctly, and
> > the added regression test no longer fails with an assertion failure.
> >
> > ...
> >                        SubPlan any_1
> >                          ->  Sort (actual rows=1000.00 loops=4)
> >                                Sort Key: o.f
> >                                Worker 0:  Sort Method: quicksort  Memory: 48kB
> >                                Worker 1:  Sort Method: quicksort  Memory: 48kB
> >                                ->  Seq Scan on other o (actual
> > rows=1000.00 loops=4)
> >
> > We could alternatively deal with this in the existing walkers, but
> > then we'd have to modify the walker context in four different places
> > to carry the identities of already visited plan states.
>
> Really? Can't you check based on state you have already decide if it's
> already initialized and if so bail? For example in ExecSortInitializeDSM():
>
> if (node->shared_info != NULL)
>         return;

I suppose, but that becomes a bit tedious since we're reinventing the
wheel on how to avoid duplicates in a customized way for each walker.

>
> > I also assume
> > that changing how ExecInitHashJoin works is off the table, but I admit
> > that logic goes a bit over my head.
>
> It's these two calls that cause the sort PlanState to be added twice:
>
> hjstate->hj_OuterHash =
>         ExecBuildHash32Expr(hjstate->js.ps.ps_ResultTupleDesc,
>                         hjstate->js.ps.resultops,
>                         outer_hashfuncid,
>                         node->hashcollations,
>                         node->hashkeys,
>                         hash_strict,
>                         &hjstate->js.ps,
>                         0);
>
> and
>
> hjstate->hashclauses =
>         ExecInitQual(node->hashclauses, (PlanState *) hjstate);
>
> Both, the hash clauses and the qual, reference the sub-plan. How would
> you avoid this? Also, it's too easy for other plan nodes to do the same.
> Hence, i think it's better to fix it structurally.

Yeah, that's fair - I was mainly thinking "I don't see if there is a
way to solve it at that level, maybe someone else (Andres?) does", but
it seemed unlikely to me too.

> > Note that technically the ExecShutdownNode_walker suffers from the
> > same problem, but it doesn't appear to be a practical problem with
> > in-core code today, and it's a bit more complicated to fix with the
> > current nested InstrStartNode assumptions (that I intend to fix
> > separately in the stack-based instrumentation series).
>
> Can you give some more details why it's not a problem in-core and what
> the problem with InstrStart/StopNode() is?

The problem is that InstrStart/StopNode expect to be called in a
nested manner, so that if shutdown activity occurs in a node, the
parent node also gets its instrumentation updated. If we were to
process each plan state one by one in a list, that nested aspect gets
lost, and parent nodes would no longer carry the instrumentation
information (we'd have to invent a mechanism to add it back, which is
a bunch of code). This gets a lot easier in a world where we are more
explicit about accumulation of instrumentation data (as is the case in
the stack-based instrumentation patch series), but otherwise seems a
bit overkill.

> Any node sitting below the Sort node, which doesn't like having its
> shutdown function called twice, might cause problems. Especially,
> CustomScanStates that we don't have full control over could be problematic.

Yeah, that's a fair point - custom scans could certainly get
unexpected failures here.

Thanks,
Lukas

-- 
Lukas Fittl






^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Assert failure in parallel query due to SubPlan double visit for hash join
@ 2026-11-02 20:18  David Geier <geidav.pg@gmail.com>
  parent: Lukas Fittl <lukas@fittl.com>
  0 siblings, 1 reply; 9+ messages in thread

From: David Geier @ 2026-11-02 20:18 UTC (permalink / raw)
  To: Lukas Fittl <lukas@fittl.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andres Freund <andres@anarazel.de>

> Yeah, that would be elegant, but I don't think it works. The problem
> is that we can also have two different nodes reference the same
> PlanState, like in this example with the same SubPlan being referenced
> by different nodes underneath an Append (courtesy of Claude, FWIW):
> 
>   create table t2s as
>     select i % 1000 as thousand, i % 2000 as twothousand
>     from generate_series(0, 999) i;
>   alter table t2s set (parallel_workers = 0);
> 
>   -- Hash-partitioned table. The WHERE clause below is copied to every
>   -- partition scan, each with its own SubPlanState for the same subplan.
>   create table tp (unique1 int, hundred int) partition by hash (unique1);
>   create table tp0 partition of tp for values with (modulus 3, remainder 0);
>   create table tp1 partition of tp for values with (modulus 3, remainder 1);
>   create table tp2 partition of tp for values with (modulus 3, remainder 2);
>   insert into tp select i, i % 100 from generate_series(0, 299999) i;
> 
>   vacuum analyze t2s;
>   vacuum analyze tp;
> 
>   -- Make a parallel plan attractive on a small test table.
>   set parallel_leader_participation = off;
>   set parallel_setup_cost = 0;
>   set parallel_tuple_cost = 0;
>   set min_parallel_table_scan_size = 0;
>   set max_parallel_workers_per_gather = 2;
> 
>   explain (analyze, costs off, timing off, summary off, buffers off)
>   select count(*)
>   from tp
>   where hundred = case when 5 not in
>           (select thousand from t2s order by twothousand limit 50)
>         then 1 else 2 end;
> 
> On master this shows in a non-assert enabled build (note the missing
> Sort Method in the SubPlan):
> 
>    Finalize Aggregate
>      ->  Gather
>            Workers Planned: 2
>            ->  Partial Aggregate
>                  ->  Parallel Append
>                        ->  Parallel Seq Scan on tp0 tp_1
>                              Filter: (hundred = CASE WHEN (NOT (ANY (5
> = (hashed SubPlan any_1).col1))) THEN 1 ELSE 2 END)
>                              SubPlan any_1
>                                ->  Limit
>                                      ->  Sort
>                                            Sort Key: t2s.twothousand
>                                            ->  Seq Scan on t2s
>                        ->  Parallel Seq Scan on tp1 tp_2
>                              Filter: (hundred = CASE WHEN (NOT (ANY (5
> = (hashed SubPlan any_1).col1))) THEN 1 ELSE 2 END)
>                        ->  Parallel Seq Scan on tp2 tp_3
>                              Filter: (hundred = CASE WHEN (NOT (ANY (5
> = (hashed SubPlan any_1).col1))) THEN 1 ELSE 2 END)
> 

Just realized that for CTEs it's different. The CTE is only stored in
initPlan and planstate_tree_walker() doesn't descend into
CTEScanState::cteplanstate. So every CTEScanState is only visited once.

But the example you found is a good one. Each PlanState only has a
single reference but in total there's more than a single reference to
the same PlanState.

So it seems like we cannot avoid this case by being smart about what we
store per node. In that case I would prefer if planstate_tree_walker()
would take care not further descending.

Do you know if there's any code that relies on nodes being visited
multiple times? If not, I would change planstate_tree_walker() such that
it visits every node only once. If there's such code, I would make
planstate_tree_walker() have two modes.

We could use Bitmapset on the plan_node_id to check if a node has been
visited already. That should be pretty cheap. Only question is how to
pass this through the recursion levels in a nice way.

>> Really? Can't you check based on state you have already decide if it's
>> already initialized and if so bail? For example in ExecSortInitializeDSM():
>>
>> if (node->shared_info != NULL)
>>         return;
> I suppose, but that becomes a bit tedious since we're reinventing the
> wheel on how to avoid duplicates in a customized way for each walker.

I would think in almost all code it's pretty easy to check for "is
initialized" but it makes the code hard to read and it's easy to forget
without having such corner-case tests in place.

I would prefer a solution that avoids the problem in the first place.

>> Can you give some more details why it's not a problem in-core and what
>> the problem with InstrStart/StopNode() is?
> 
> The problem is that InstrStart/StopNode expect to be called in a
> nested manner, so that if shutdown activity occurs in a node, the
> parent node also gets its instrumentation updated. If we were to
> process each plan state one by one in a list, that nested aspect gets
> lost, and parent nodes would no longer carry the instrumentation
> information (we'd have to invent a mechanism to add it back, which is
> a bunch of code). This gets a lot easier in a world where we are more
> explicit about accumulation of instrumentation data (as is the case in
> the stack-based instrumentation patch series), but otherwise seems a
> bit overkill.

That would naturally work with a solution that doesn't visit already
visited children, does it?

>> Any node sitting below the Sort node, which doesn't like having its
>> shutdown function called twice, might cause problems. Especially,
>> CustomScanStates that we don't have full control over could be problematic.
> 
> Yeah, that's a fair point - custom scans could certainly get
> unexpected failures here.
OK.

-- 
David Geier






^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Assert failure in parallel query due to SubPlan double visit for hash join
@ 2026-11-04 01:19  Lukas Fittl <lukas@fittl.com>
  parent: David Geier <geidav.pg@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Lukas Fittl @ 2026-11-04 01:19 UTC (permalink / raw)
  To: David Geier <geidav.pg@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andres Freund <andres@anarazel.de>

On Mon, Nov 2, 2026 at 12:18 PM David Geier <geidav.pg@gmail.com> wrote:
> But the example you found is a good one. Each PlanState only has a
> single reference but in total there's more than a single reference to
> the same PlanState.
>
> So it seems like we cannot avoid this case by being smart about what we
> store per node. In that case I would prefer if planstate_tree_walker()
> would take care not further descending.

Thanks for confirming - I do agree that if we can make
planstate_tree_walker do this consistently that would be good - I'm
mainly not sure how awkward we make the API with that.

> Do you know if there's any code that relies on nodes being visited
> multiple times? If not, I would change planstate_tree_walker() such that
> it visits every node only once. If there's such code, I would make
> planstate_tree_walker() have two modes.

From my read I don't think any of the callers expect it to visit the
same node multiple times, but if we go this way we should review one
more time.

> We could use Bitmapset on the plan_node_id to check if a node has been
> visited already. That should be pretty cheap. Only question is how to
> pass this through the recursion levels in a nice way.

Yeah, exactly - I think there are two ways I can think of to solve it:

1) Make context not be generic, but rather a common base context
struct (with the Bitmapset) that is embedded as the first field in the
other context structs

2) Pass an extra argument to the walker functions and callback that
contains the Bitmapset

Preferences on these / can you think of a different way?

>
> >> Really? Can't you check based on state you have already decide if it's
> >> already initialized and if so bail? For example in ExecSortInitializeDSM():
> >>
> >> if (node->shared_info != NULL)
> >>         return;
> > I suppose, but that becomes a bit tedious since we're reinventing the
> > wheel on how to avoid duplicates in a customized way for each walker.
>
> I would think in almost all code it's pretty easy to check for "is
> initialized" but it makes the code hard to read and it's easy to forget
> without having such corner-case tests in place.
>
> I would prefer a solution that avoids the problem in the first place.

Ack.

>
> >> Can you give some more details why it's not a problem in-core and what
> >> the problem with InstrStart/StopNode() is?
> >
> > The problem is that InstrStart/StopNode expect to be called in a
> > nested manner, so that if shutdown activity occurs in a node, the
> > parent node also gets its instrumentation updated. If we were to
> > process each plan state one by one in a list, that nested aspect gets
> > lost, and parent nodes would no longer carry the instrumentation
> > information (we'd have to invent a mechanism to add it back, which is
> > a bunch of code). This gets a lot easier in a world where we are more
> > explicit about accumulation of instrumentation data (as is the case in
> > the stack-based instrumentation patch series), but otherwise seems a
> > bit overkill.
>
> That would naturally work with a solution that doesn't visit already
> visited children, does it?

Yes, it would. For me it was mostly an argument to not invent a custom
way of solving it there.

Thanks,
Lukas

-- 
Lukas Fittl






^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Assert failure in parallel query due to SubPlan double visit for hash join
@ 2026-11-04 14:39  David Geier <geidav.pg@gmail.com>
  parent: Lukas Fittl <lukas@fittl.com>
  0 siblings, 1 reply; 9+ messages in thread

From: David Geier @ 2026-11-04 14:39 UTC (permalink / raw)
  To: Lukas Fittl <lukas@fittl.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andres Freund <andres@anarazel.de>

> Thanks for confirming - I do agree that if we can make
> planstate_tree_walker do this consistently that would be good - I'm
> mainly not sure how awkward we make the API with that.

I think it won't be too bad. There are also only very fw call sites for
planstate_tree_walker().

>> Do you know if there's any code that relies on nodes being visited
>> multiple times? If not, I would change planstate_tree_walker() such that
>> it visits every node only once. If there's such code, I would make
>> planstate_tree_walker() have two modes.
> 
> From my read I don't think any of the callers expect it to visit the
> same node multiple times, but if we go this way we should review one
> more time.

- ExplainPreScanNode() is fine either way.
- ExecShutdownNode_walker() should be fixed as discussed.
- The remaining calls are all from execParallel.c and should also be
  fixed, shouldn't they?

These are the only call sites I can see. Based on that we could change
planstate_tree_walker() to visit every node only once.

>> We could use Bitmapset on the plan_node_id to check if a node has been
>> visited already. That should be pretty cheap. Only question is how to
>> pass this through the recursion levels in a nice way.
> 
> Yeah, exactly - I think there are two ways I can think of to solve it:
> 
> 1) Make context not be generic, but rather a common base context
> struct (with the Bitmapset) that is embedded as the first field in the
> other context structs

That sounds good. Maybe it's enough to require that the first member of
any context is a Bitmapset. Bitmapset is a node so we could assert on
that as well.

> 2) Pass an extra argument to the walker functions and callback that
> contains the Bitmapset

That requires then always two state variables at the call sites. I
prefer (1).

> Preferences on these / can you think of a different way?

An alternative to the Bitmapset would be storing traversal state inside
struct PlanState. The advantage of that is performance. A "has been
visited" check would be as cheap as comparing two variables.

A visited flag has the disadvantage that it needs to be reset after the
traversal has completed. What usually works quite nicely is a "traversal
generation" counter that is incremented prior to each traversal. A node
is considered visited if that node's counter is equal to the current
traversal generation (which means it has been incremented already during
the current traversal). The counter could be as small as 1 bit. We can
rely on wrap around to be different for each two successive traversals.

I'm leaning towards the less intrusive Bitmapset variant. The
is-member-of checks should be fast enough and likely be negligible
compared to all the cache misses when traversing the graph. We can
profile that.

>>> The problem is that InstrStart/StopNode expect to be called in a
>>> nested manner, so that if shutdown activity occurs in a node, the
>>> parent node also gets its instrumentation updated. If we were to
>>> process each plan state one by one in a list, that nested aspect gets
>>> lost, and parent nodes would no longer carry the instrumentation
>>> information (we'd have to invent a mechanism to add it back, which is
>>> a bunch of code). This gets a lot easier in a world where we are more
>>> explicit about accumulation of instrumentation data (as is the case in
>>> the stack-based instrumentation patch series), but otherwise seems a
>>> bit overkill.
>>
>> That would naturally work with a solution that doesn't visit already
>> visited children, does it?
> 
> Yes, it would. For me it was mostly an argument to not invent a custom
> way of solving it there.

Cool!

-- 
David Geier






^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Assert failure in parallel query due to SubPlan double visit for hash join
@ 2026-11-17 20:42  Tomas Vondra <tomas@vondra.me>
  parent: David Geier <geidav.pg@gmail.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Tomas Vondra @ 2026-11-17 20:42 UTC (permalink / raw)
  To: David Geier <geidav.pg@gmail.com>; Lukas Fittl <lukas@fittl.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andres Freund <andres@anarazel.de>

On 11/4/26 15:39, David Geier wrote:
>> Thanks for confirming - I do agree that if we can make
>> planstate_tree_walker do this consistently that would be good - I'm
>> mainly not sure how awkward we make the API with that.
> 
> I think it won't be too bad. There are also only very fw call sites for
> planstate_tree_walker().
> 
>>> Do you know if there's any code that relies on nodes being visited
>>> multiple times? If not, I would change planstate_tree_walker() such that
>>> it visits every node only once. If there's such code, I would make
>>> planstate_tree_walker() have two modes.
>>
>> From my read I don't think any of the callers expect it to visit the
>> same node multiple times, but if we go this way we should review one
>> more time.
> 
> - ExplainPreScanNode() is fine either way.
> - ExecShutdownNode_walker() should be fixed as discussed.
> - The remaining calls are all from execParallel.c and should also be
>   fixed, shouldn't they?
> 
> These are the only call sites I can see. Based on that we could change
> planstate_tree_walker() to visit every node only once.
> 
>>> We could use Bitmapset on the plan_node_id to check if a node has been
>>> visited already. That should be pretty cheap. Only question is how to
>>> pass this through the recursion levels in a nice way.
>>
>> Yeah, exactly - I think there are two ways I can think of to solve it:
>>
>> 1) Make context not be generic, but rather a common base context
>> struct (with the Bitmapset) that is embedded as the first field in the
>> other context structs
> 
> That sounds good. Maybe it's enough to require that the first member of
> any context is a Bitmapset. Bitmapset is a node so we could assert on
> that as well.
> 

Wouldn't it be easier to just have a special context struct for each of
the functions in execParallel.c that need it? ExecParallelEstimate
already has ExecParallelEstimateContext (so we could add the bitmap
there), and we'd get similar contexts for the other functions that need
to check if the node was already visited (with the bitmap + their
current context). So we'd get something like

  /* Context object for ExecParallelReInitializeDSM. */
  typedef struct ExecParallelReInitializeDSMContext
  {
      ParallelContext *pcxt;
      Bitmapset  *visited;
  } ExecParallelReInitializeDSMContext;

  typedef struct ExecParallelInstrumentationContext
  {
      SharedExecutorInstrumentation *instrumentation;
      Bitmapset  *visited;
  } ExecParallelInstrumentationContext;

  ...

I don't see a point to have some "context" ancestor for all these
contexts, they don't seem to have all that common, no? What would be the
point of that? All the functions already get a context, and we'd just
replace that with the new struct (and then get the original one from
it). Maybe this is what's proposed, though?

>> 2) Pass an extra argument to the walker functions and callback that
>> contains the Bitmapset
> 
> That requires then always two state variables at the call sites. I
> prefer (1).
> 
>> Preferences on these / can you think of a different way?
> 
> An alternative to the Bitmapset would be storing traversal state inside
> struct PlanState. The advantage of that is performance. A "has been
> visited" check would be as cheap as comparing two variables.
> 
> A visited flag has the disadvantage that it needs to be reset after the
> traversal has completed. What usually works quite nicely is a "traversal
> generation" counter that is incremented prior to each traversal. A node
> is considered visited if that node's counter is equal to the current
> traversal generation (which means it has been incremented already during
> the current traversal). The counter could be as small as 1 bit. We can
> rely on wrap around to be different for each two successive traversals.
> 
> I'm leaning towards the less intrusive Bitmapset variant. The
> is-member-of checks should be fast enough and likely be negligible
> compared to all the cache misses when traversing the graph. We can
> profile that.
> 

IMO the Bitmapset is cleaner, because it's isolated to execParallel,
stuff added to PlanState is not.

>>>> The problem is that InstrStart/StopNode expect to be called in a
>>>> nested manner, so that if shutdown activity occurs in a node, the
>>>> parent node also gets its instrumentation updated. If we were to
>>>> process each plan state one by one in a list, that nested aspect gets
>>>> lost, and parent nodes would no longer carry the instrumentation
>>>> information (we'd have to invent a mechanism to add it back, which is
>>>> a bunch of code). This gets a lot easier in a world where we are more
>>>> explicit about accumulation of instrumentation data (as is the case in
>>>> the stack-based instrumentation patch series), but otherwise seems a
>>>> bit overkill.
>>>
>>> That would naturally work with a solution that doesn't visit already
>>> visited children, does it?
>>
>> Yes, it would. For me it was mostly an argument to not invent a custom
>> way of solving it there.
> 
> Cool!
> 

Are we maybe addressing a symptom and not the root cause? I mean, this
code mostly assumes that nodes are a tree, and a single plan can't be
referenced from multiple nodes. Maybe having the bitmap fixes the
immediate symptom, but it needs a bigger change? I don't know what that
would be, though :-(

ISTM all of this sharing can only happen through a subplan. Or can it
happen in other ways too?


regards

-- 
Tomas Vondra







^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2026-11-17 20:42 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-26 23:21 Assert failure in parallel query due to SubPlan double visit for hash join Lukas Fittl <lukas@fittl.com>
2026-10-29 00:17 ` Sami Imseih <samimseih.pg@gmail.com>
2026-10-29 22:07   ` Lukas Fittl <lukas@fittl.com>
2026-10-29 08:26 ` David Geier <geidav.pg@gmail.com>
2026-10-29 22:56   ` Lukas Fittl <lukas@fittl.com>
2026-11-02 20:18     ` David Geier <geidav.pg@gmail.com>
2026-11-04 01:19       ` Lukas Fittl <lukas@fittl.com>
2026-11-04 14:39         ` David Geier <geidav.pg@gmail.com>
2026-11-17 20:42           ` Tomas Vondra <tomas@vondra.me>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox