Re: Assert failure in parallel query due to SubPlan double visit for hash join

From: David Geier <geidav(dot)pg(at)gmail(dot)com>
To: Tomas Vondra <tomas(at)vondra(dot)me>, Lukas Fittl <lukas(at)fittl(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Andres Freund <andres(at)anarazel(dot)de>
Subject: Re: Assert failure in parallel query due to SubPlan double visit for hash join
Date: 2026-11-24 09:19:31
Message-ID: 5400228f-63f4-4934-b800-125aefe245bc@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Tomas!

> 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?

I'm intending to change planstate_tree_walker() to never visited the
same node twice; not the specific code that has problems with it in
execParallel.c.

I had thought about something like the following code, with the
motivation of making it obvious to client code that extra state needs to
be passed. I thought that without it, it's too easy to forget embedding
the extra Bitmapset in the context struct.
We cannot simply check for the presence of a Bitmapset member via
Assert(IsA(context, Bitmapset)) in planstate_tree_walker() because
initially the Bitmapset is NULL.

struct PlanStateWalkerContext
{
Bitmapset *visited;
};

struct MyContext
{
PlanStateWalkerContext walker_ctx;
int my_data;
};

planstate_tree_walker_impl(PlanState *planstate,
planstate_tree_walker_callback walker,
PlanStateWalkerContext *context)

MyContext ctx;
planstate_tree_walker(node, &ctx.walker_ctx);

Attached patch does what you proposed as a starting point for more
targeted discussion. The Bitmapset *visited is not cleaned up by
planstate_tree_walker() because it doesn't know when the recursion is
over. For now I'm leaving cleanup to the surrounding memory context. If
we want to free the Bitmapset after traversal, we would need some
wrapper around planstate_tree_walker().

The patch passes both test cases from Lukas and the regression tests.

> 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?
That's what we had discussed a few mails back. There are two cases:

1. A single PlanState contains more than a single reference to the same
PlanState. This case is bogus and could be fixed by not adding the
PlanState again if it exists already.

2. Multiple PlanStates each having a single reference to the same
PlanState. Lukas showed how this can happen. That case makes sense
and I wouldn't know of how to avoid that.

--
David Geier

Attachment Content-Type Size
v1-0001-planstate_tree_walker-visits-every-node-only-once.patch text/plain 16.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2026-11-24 09:25:59 Re: Fix a wrong comment in UpdateDeadTupleRetentionStatus()
Previous Message Kirill Reshke 2026-11-24 09:18:47 Fix network subset support with custom operator classes