| From: | Tomas Vondra <tomas(at)vondra(dot)me> |
|---|---|
| To: | David Geier <geidav(dot)pg(at)gmail(dot)com>, 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-17 20:42:06 |
| Message-ID: | 48d30590-f9c4-4161-903d-08f984836df4@vondra.me |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
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
| From | Date | Subject | |
|---|---|---|---|
| Next Message | surya poondla | 2026-11-17 21:32:33 | Re: pg_xmin_horizon: a system view of everything pinning the xmin horizon |
| Previous Message | Greg Burd | 2026-11-17 20:21:09 | Re: Hash index: stop registering pin-only bucket pages in WAL (remove REGBUF_NO_CHANGE) |