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 1x4Jh3-0074Hu-01 for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Sep 2026 14:54:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x4Jh1-00Fekw-37 for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Sep 2026 14:54:31 +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 1x4Jh1-00Feko-29 for pgsql-hackers@lists.postgresql.org; Wed, 09 Sep 2026 14:54:31 +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 1x4Jgv-00000004n3L-2ozL for pgsql-hackers@lists.postgresql.org; Wed, 09 Sep 2026 14:54:30 +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 689EsIGP614241; Wed, 9 Sep 2026 10:54:18 -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> Comments: In-reply-to Richard Guo message dated "Wed, 09 Sep 2026 17:04:54 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <614239.1788965658.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Wed, 09 Sep 2026 10:54:18 -0400 Message-ID: <614240.1788965658@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Richard Guo writes: > On Thu, Sep 3, 2026 at 11:55 PM Tom Lane wrote: >> 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. > Hmm, not for all of them. My v2 patch only preprocesses the > outer-level PHVs that were pushed into LATERAL subquery RTEs, since > that is where find_lateral_references pulls them from. It does not > touch PHVs that were pushed into a SubLink's subselect, and for those > the subquery's own preprocessing is still the only thing that folds > them. Ugh. That sounds like a recipe for bugs; I don't think we want different PHVs in the same query level to be folded at different times if we can help it. > I think I didn't state my earlier point clearly, sorry about that. > What I meant is that preprocess_expression as a whole is not safe to > run twice, because its SS_process_sublinks step would hit > Assert(!IsA(node, SubPlan)) on the SubPlans that the first run > created. Right. We could of course drop that Assert in favor of ignoring SubPlans there. But the interaction between SS_process_sublinks and SS_replace_correlation_vars is delicate enough that I doubt we want to risk running that sequence twice. > eval_const_expressions by itself does seem safe to run > twice, and I'm pretty certain that there are existing code paths where > that can happen, for example preprocess_expression deliberately > re-runs it for EXPRKIND_RTFUNC_LATERAL. The hole in this logic is the assumption that eval_const_expressions will act the same with Vars of upper query levels as with Vars of the current query level. That's visibly not so at present --- see for example const-simplification of NullTest nodes on known-not-null Vars. var_is_nonnullable just throws up its hands if var->varlevelsup != 0. (Arguably that's a bug, or at least an oversight: couldn't it crawl up the PlannerInfo tree to the appropriate level? But my point is that we shouldn't assume that this will always work perfectly.) > I'm not too worried about the wasted cycles. ISTM a walk over an > already-simplified expression is cheap. It's not *that* cheap; we will for example be repeating operator volatility lookups, not to mention copying the tree. But I agree that this concern is much weaker than correctness. >> 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 went through the steps that run before SS_replace_correlation_vars. Thanks for doing that analysis. I remain concerned though about inconsistent processing of PHV contents. I think the sketch I'd rather see is that we simply ignore the contents of any uplevel PHV during subquery processing, relying on the singly-preprocessed version sitting in the owning query level's PlaceHolderInfo when it comes time to do something interesting with it. regards, tom lane