From: Tom Lane <tgl@sss.pgh.pa.us>
To: Robert Haas <robertmhaas@gmail.com>
Cc: hackerzheng666@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #19924: unrecognized node type in create_plan_recurse: UNION, LIMIT, json_populate_recordset, inheritance
Date: Fri, 20 Nov 2026 17:47:40 -0500
Message-ID: <1722840.1795214860@sss.pgh.pa.us> (raw)
In-Reply-To: <1714675.1795209626@sss.pgh.pa.us>
References: <19924-8aecd924385fafcc@postgresql.org>
<1713251.1795207985@sss.pgh.pa.us>
<1714675.1795209626@sss.pgh.pa.us>
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
Attachments:test-not-freeing-shared-partial-paths.patch (text/x-diff, 1015B) downloaddiff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.cindex 3af494f269d..faeb66b95f8 100644--- a/src/backend/optimizer/util/pathnode.c+++ b/src/backend/optimizer/util/pathnode.c@@ -863,7 +863,13 @@ add_partial_path(RelOptInfo *parent_rel, Path *new_path)
{
parent_rel->partial_pathlist =
foreach_delete_current(parent_rel->partial_pathlist, p1);
- pfree(old_path);+ if (old_path->parent == parent_rel)+ {+ elog(LOG, "freeing dominated old path %p", old_path);+ pfree(old_path);+ }+ else+ elog(LOG, "conservatively keeping shared old path %p", old_path);
}
else
{
@@ -895,7 +901,13 @@ add_partial_path(RelOptInfo *parent_rel, Path *new_path)
else
{
/* Reject and recycle the new path */
- pfree(new_path);+ if (new_path->parent == parent_rel)+ {+ elog(LOG, "freeing dominated new path %p", new_path);+ pfree(new_path);+ }+ else+ elog(LOG, "conservatively keeping shared new path %p", new_path);
}
}
=
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, robertmhaas@gmail.com, hackerzheng666@gmail.com
Subject: Re: BUG #19924: unrecognized node type in create_plan_recurse: UNION, LIMIT, json_populate_recordset, inheritance
In-Reply-To: <1722840.1795214860@sss.pgh.pa.us>
References: <19924-8aecd924385fafcc@postgresql.org> <1713251.1795207985@sss.pgh.pa.us> <1714675.1795209626@sss.pgh.pa.us> <1722840.1795214860@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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