Re: BUG #19924: unrecognized node type in create_plan_recurse: UNION, LIMIT, json_populate_recordset, inheritance

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: hackerzheng666(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19924: unrecognized node type in create_plan_recurse: UNION, LIMIT, json_populate_recordset, inheritance
Date: 2026-11-20 22:47:40
Message-ID: 1722840.1795214860@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Ah, I identified the problem and a possible fix. The root culprit is
in grouping_planner():

/*
* Generate partial paths for final_rel, too, if outer query levels might
* be able to make use of them.
*/
if (final_rel->consider_parallel && root->query_level > 1 &&
!limit_needed(parse))
{
Assert(!parse->rowMarks && parse->commandType == CMD_SELECT);
foreach(lc, current_rel->partial_pathlist)
{
Path *partial_path = (Path *) lfirst(lc);

add_partial_path(final_rel, partial_path);
}
}

In this loop, calls of add_partial_path after the first one are
capable of removing partial paths from final_rel, and then it deletes
those despite their probably being referenced in Gather paths that
have already been copied into final_rel, not to mention that they're
still referenced within current_rel. So that's where the dangling
pointers come from. This behavior requires a few preconditions that
might themselves be bugs:

* It's not very clear how a set of Paths that all survived the
add_partial_path tournament for current_rel wouldn't all survive
the new tournament for final_rel. I suspect there is an
order-of-presentation bug in add_partial_path's path comparisons,
but didn't go digging for it. (I did check that final_rel's
partial_pathlist is empty at the start of this loop; but if it ever
isn't, that would offer a non-buggy way to reach the problem.)

* It's even less clear how the parent Gather of a partial path
that didn't survive this tournament would manage to win out at
the final_rel level and be chosen to become the Plan. This suggests
bug-grade inconsistencies between the costing comparisons of Gather
paths and partial paths.

I'm not planning to look into those issues myself, but probably
someone should. Whether they are bugs or no, I think we need some
kind of data structure consistency guard here.

After some thought I have an idea: grouping_planner is cheating to
some extent here by inserting Paths belonging to a lower-level rel
into final_rel. At the very least that leaves them with the wrong
parent pointer. We've been aware of that fact for awhile, and it
hasn't bit us yet, so I don't propose to do anything to change it
(yet). What I do suggest is that add_partial_path could check for
a mismatched Path.parent pointer and cowardly refuse to pfree a
Path that was originally made for some other parent rel. The
attached test patch logs the four possible cases, and what I see
in a run of the main regression tests is

$ grep 'freeing dominated old path' postmaster.log | wc
7785 77850 638474
$ grep 'conservatively keeping shared old' postmaster.log | wc
0 0 0
$ grep 'freeing dominated new path' postmaster.log | wc
23871 238710 1957478
$ grep 'conservatively keeping shared new' postmaster.log | wc
0 0 0

The submitted test case hits each of 'freeing dominated old path',
'conservatively keeping shared old', and 'freeing dominated new path'
twice, while not hitting 'conservatively keeping shared new' at all.
(Nonetheless I think we'd better check at both pfree sites.)

These numbers demonstrate that the problem is quite rare and thus
that adding this defense won't cost us much planner memory.

Comments?

regards, tom lane

Attachment Content-Type Size
test-not-freeing-shared-partial-paths.patch text/x-diff 1015 bytes

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Zsolt Parragi 2026-11-20 22:59:32 Re: Timing issue in pg_basebackup
Previous Message Masahiko Sawada 2026-11-20 22:35:41 Re: BUG #19879: Blocked COPY FROM reports NULL command and type in pg_stat_progress_copy