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 1x28qV-005bqv-14 for pgsql-hackers@arkaria.postgresql.org; Thu, 03 Sep 2026 14:55:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x28qS-0014B5-2X for pgsql-hackers@arkaria.postgresql.org; Thu, 03 Sep 2026 14:55:16 +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 1x28qS-0014Av-1X for pgsql-hackers@lists.postgresql.org; Thu, 03 Sep 2026 14:55:16 +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 1x28qQ-00000003p5O-2WRZ for pgsql-hackers@lists.postgresql.org; Thu, 03 Sep 2026 14:55:15 +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 683Et55x1468660; Thu, 3 Sep 2026 10:55:05 -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> <250168.! 1788034245@sss.pgh.pa.us> <495449.1788106841@sss.pgh.pa.us> Comments: In-reply-to Richard Guo message dated "Mon, 31 Aug 2026 10:54:19 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1468658.1788447305.1@sss.pgh.pa.us> Date: Thu, 03 Sep 2026 10:55:05 -0400 Message-ID: <1468659.1788447305@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk [ after a good deal of navel-gazing... ] Richard Guo writes: > I'm also not sure about skipping outer-level PHVs in > eval_const_expressions. I'm worried that that can cause us to carry > the raw expressions in the outer query somewhere. It still feels to me that that's the right thing to do. With your v2 patch, subquery_planner will have taken care of preprocessing any outer-level PHVs before the subquery's own preprocessing starts. If we do that over again, the best-case result is that we waste cycles. The worst-case result is that we end up with an expression that looks different from what it looks like in the outer level and then that causes problems. (I don't have any specific ideas in mind about how the doubly-processed expression could come to be different. But if we accept your point that "it's unsafe to do preprocessing twice", it seems like this is fertile ground for trouble.) I think that your v2 patch is fundamentally the right way forward, but I'm worried that it's not complete yet. In particular it seems very strange that only flatten_join_alias_vars_mutator needs to worry about skipping outer-level PHVs. I already explained why I think eval_const_expressions should too, and I wonder if there are not other places that should as well. Fundamentally it seems like the policy across the board ought to be that subqueries keep their hands off upper-level PHVs. Now, once we've applied SS_replace_correlation_vars, the question is moot because all such PHVs will have been replaced by Params, but anything that runs before that needs a close look. regards, tom lane