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.96) (envelope-from ) id 1wvkkI-001UKN-0h for pgsql-hackers@arkaria.postgresql.org; Sun, 16 Aug 2026 23:58:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wvkkG-007I6u-0J for pgsql-hackers@arkaria.postgresql.org; Sun, 16 Aug 2026 23:58:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wvkkF-007I6l-2b for pgsql-hackers@lists.postgresql.org; Sun, 16 Aug 2026 23:58:28 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wvkkE-00000000y0w-1CUI for pgsql-hackers@lists.postgresql.org; Sun, 16 Aug 2026 23:58:28 +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 67GNwIct670539; Sun, 16 Aug 2026 19:58:18 -0400 From: Tom Lane To: Richard Guo cc: Thom Brown , Jacob Brazeal , pgsql-hackers@lists.postgresql.org Subject: Re: remove_useless_joins vs. bug #19560 In-reply-to: References: <1186816.1784573544@sss.pgh.pa.us> <2727652.1784997994@sss.pgh.pa.us> <2746010.1785014587@sss.pgh.pa.us> <2933135.1785032128@sss.pgh.pa.us> <3304318.1785087206@sss.pgh.pa.us> <3304418.1785087314@sss.pgh.pa.us> <3968565.1785170019@sss.pgh.pa.us> <3975112.1785176472@sss.pgh.pa.us> Comments: In-reply-to Richard Guo message dated "Wed, 29 Jul 2026 15:21:18 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <670537.1786924698.1@sss.pgh.pa.us> Date: Sun, 16 Aug 2026 19:58:18 -0400 Message-ID: <670538.1786924698@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I'm getting back to this task, after much too long in security-fix land. I had the idea that we could resolve the disagreement about whether to use ChangeVarNodes by inserting Asserts in it to catch any places where it'd attempt to store an invalid value into a Var's varno (or other fields that hold a relid). However, when I tried that, it blew up! Some investigation showed that it descends into the subquery fields of subquery RTEs even when those RTEs are no longer relevant to the query, and there it can find Var references that trip the assertions. The sequence is basically that we pull up a lateral-referencing subquery into the main query, and after flattening and maybe some preliminary join simplification, the query no longer references some RTE so we decide we can remove that. But ChangeVarNodes will descend into the original subquery RTE, find the since-elided lateral reference, and boom. So this is a pretty disturbing discovery: it means that as things stand, we can't put any sanity checks into ChangeVarNodes, and aside from that we might be doing quite a bit of useless work mutating no-longer-interesting subqueries. Switching to code based on remove_result_refs() doesn't help since that also scans the whole query tree. (That function doesn't have an issue itself, since at the time it runs we don't have any invalidated subqueries.) One idea for fixing this is to null out the subquery field of a subquery RTE as soon as we've pulled it up, but there is probably code that will crash on a null subquery pointer. I'm also thinking about invoking ChangeVarNodes on only portions of the query tree, not including the rangetable, but that seems messy and not very maintainable. Any thoughts? regards, tom lane