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 1x5o45-000HsJ-17 for pgsql-hackers@arkaria.postgresql.org; Sun, 13 Sep 2026 17:32: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 1x5o44-00A6s3-2j for pgsql-hackers@arkaria.postgresql.org; Sun, 13 Sep 2026 17:32:28 +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 1x5o44-00A6ru-1q for pgsql-hackers@lists.postgresql.org; Sun, 13 Sep 2026 17:32: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 1x5o41-00000000Cr6-2s7q for pgsql-hackers@lists.postgresql.org; Sun, 13 Sep 2026 17:32:27 +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 68DHWFlq009753; Sun, 13 Sep 2026 13:32:15 -0400 From: Tom Lane To: Richard Guo cc: Tender Wang , 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> <670538.1786924698@sss.pgh.pa.us> <839346.1787765112@sss.pgh.pa.us> <954541.1787846010@sss.pgh.pa.us> <1147476.1787924590@sss.pgh.pa.us> <495449.! 1788106841@sss.pgh.pa.us> <1468659.1788447305@sss.pgh.pa.us> <614240.1788965658@sss.pgh.pa.us> Comments: In-reply-to Richard Guo message dated "Thu, 10 Sep 2026 18:33:37 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <9751.1789320735.1@sss.pgh.pa.us> Date: Sun, 13 Sep 2026 13:32:15 -0400 Message-ID: <9752.1789320735@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Richard Guo writes: > I think a straightforward way to achieve that is that in > subquery_planner, we walk the whole query tree and preprocess all of > the current level's PHVs that got pushed down into subqueries, up > front before anything consumes them. Then the subquery's own > processing can just ignore upper-level PHVs. eval_const_expressions > returns them as-is, and flatten_join_alias_vars stops descending into > them. > This still keeps the copies around rather than relying solely on the > PlaceHolderInfo, but since they'd be folded the same way as the > owning-level version, maybe it still gets the consistency we want to > have. Yeah. Since we need to back-patch this, let's not get too aggressive about optimizing such cases. Maybe later we could look into avoiding the duplicate processing in a master-only patch. > One problem with this is that we walk the whole query tree to find > those copies, which costs some planning cycles. We could gate it on > root->glob->lastPHId != 0 to skip queries with no PHVs at all, but > beyond that I don't have a good way to narrow it down. I see you did put in that gate, and again I think that's enough for now. We could conceivably try to track which query levels own PHVs, and then only recurse down from those levels. But I fear that'd be about as hard to keep straight as hasSubLinks, which has caused us plenty of heartburn in the past. Again, let's leave such optimization for another day. In short, LGTM, please push. regards, tom lane