Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xUXRK-000000011hZ-1gyk for pgsql-bugs@arkaria.postgresql.org; Fri, 20 Nov 2026 22:50:42 +0000 Received: from localhost ([127.0.0.1]:59764 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xUXRJ-0000000C3nb-3OIF for pgsql-bugs@arkaria.postgresql.org; Fri, 20 Nov 2026 22:50:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]:57050) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xUXOY-0000000C1nK-0N09 for pgsql-bugs@lists.postgresql.org; Fri, 20 Nov 2026 22:47:50 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xUXOV-00000000ol5-3fDj for pgsql-bugs@lists.postgresql.org; Fri, 20 Nov 2026 22:47:49 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 6AKMleGP1722841; Fri, 20 Nov 2026 17:47:40 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 sss.pgh.pa.us 6AKMleGP1722841 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sss.pgh.pa.us; s=default; t=1795214860; bh=ud/bIMiOQyMtNN4/ZBqwx3SCh70R+iBXlt8azAcbfpk=; h=From:To:cc:Subject:In-reply-to:References:Date:From; b=DtqUJO7NTsujCM8zVNReoTwqjNRXW2P/WaeD2cnYYHp4XqyHE1wuCkI1KPZHrzfzI aegCuIhGp1BstNGsrLapp8OFfKq3lTrB54DFCN2hm9teoOelezBTPOp0s4mra+1OS4 v2K260ImPiGwU8qk0lgvpYuzkza+mxxKlGfxQFPw= From: Tom Lane To: Robert Haas cc: hackerzheng666@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19924: unrecognized node type in create_plan_recurse: UNION, LIMIT, json_populate_recordset, inheritance 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> Comments: In-reply-to Tom Lane message dated "Fri, 20 Nov 2026 16:20:26 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <1722797.1795214788.0@sss.pgh.pa.us> Date: Fri, 20 Nov 2026 17:47:40 -0500 Message-ID: <1722840.1795214860@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1722797.1795214788.1@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 ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="test-not-freeing-shared-partial-paths.patch"; charset="us-ascii" Content-ID: <1722797.1795214788.2@sss.pgh.pa.us> Content-Description: test-not-freeing-shared-partial-paths.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer= /util/pathnode.c index 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_pa= th) { parent_rel->partial_pathlist =3D foreach_delete_current(parent_rel->partial_pathlist, p1); - pfree(old_path); + if (old_path->parent =3D=3D 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_pa= th) else { /* Reject and recycle the new path */ - pfree(new_path); + if (new_path->parent =3D=3D 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); } } = ------- =_aaaaaaaaaa0--