pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Richard Guo <guofenglinux@gmail.com>
Cc: Tender Wang <tndrwang@gmail.com>
Cc: Thom Brown <thom@linux.com>
Cc: Jacob Brazeal <jacob.brazeal@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org
Subject: Re: remove_useless_joins vs. bug #19560
Date: Wed, 09 Sep 2026 10:54:18 -0400
Message-ID: <614240.1788965658@sss.pgh.pa.us> (raw)
In-Reply-To: <CAMbWs4_BN_0+mHvS3xpH8WOU47PN3bV5zh3Ny56JLHnB-bamww@mail.gmail.com>
References: <1186816.1784573544@sss.pgh.pa.us>
	<CAMbWs49s+AdZgvrxm3bxqEWGKeFcK=r41TpfT+x+wzucVV+ORA@mail.gmail.com>
	<2727652.1784997994@sss.pgh.pa.us>
	<CAA-aLv7GGm3OwWOAGou0k-j5jyjSyOMqVc3-GxxO0NjjX+vDmg@mail.gmail.com>
	<2746010.1785014587@sss.pgh.pa.us>
	<2933135.1785032128@sss.pgh.pa.us>
	<CAA-aLv4UbdjHarp8zaK4PQjFHZVrt27hxmZqqBUn3yR_5CpgjQ@mail.gmail.com>
	<3304318.1785087206@sss.pgh.pa.us>
	<3304418.1785087314@sss.pgh.pa.us>
	<CAMbWs4_oMOZkKJF77TGwBsiN1vaKS568rxhE0Adzuwhrs6GLnQ@mail.gmail.com>
	<3968565.1785170019@sss.pgh.pa.us>
	<3975112.1785176472@sss.pgh.pa.us>
	<CAMbWs4-4JN51woVH86hMSMmdLfNVXzRav8KHShhP6QGmbtLNDg@mail.gmail.com>
	<670538.1786924698@sss.pgh.pa.us>
	<CAMbWs4-C4fwZTtY911MfAkW51G2d9TpqoRqmxCiJbjeO0GG1ug@mail.gmail.com>
	<839346.1787765112@sss.pgh.pa.us>
	<CAMbWs4-bgUE1SuNOgMmROhLJbRZz=f4bK7RJAEBQU9uCTSMBnQ@mail.gmail.com>
	<954541.1787846010@sss.pgh.pa.us>
	<CAMbWs4_mfPF5pQVT2FAdvjEMqN7pDT2MGJqkVoJLVyGLYAsU+w@mail.gmail.com>
	<1147476.1787924590@sss.pgh.pa.us>
	<CAMbWs4! -5aHFeGy9ArFbozdpxy4xFEtWgXzGTW7KoYOjzX9nXGA@mail.gmail.com>
	<495449.1788106841@sss.pgh.pa.us>
	<CAMbWs4_FkUdq1D4rwbty972=JVFgS-ip1K9x6x_izAtqv=k3Eg@mail.gmail.com>
	<1468659.1788447305@sss.pgh.pa.us>
	<CAMbWs4_BN_0+mHvS3xpH8WOU47PN3bV5zh3Ny56JLHnB-bamww@mail.gmail.com>

Richard Guo <guofenglinux@gmail.com> writes:
> On Thu, Sep 3, 2026 at 11:55 PM Tom Lane <tgl@sss.pgh.pa.us> 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






view thread (74+ messages)  latest in thread

Message-ID: <614240.1788965658@sss.pgh.pa.us>
Permalink:  ../614240.1788965658@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/614240.1788965658@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, guofenglinux@gmail.com, tndrwang@gmail.com, thom@linux.com, jacob.brazeal@gmail.com
  Subject: Re: remove_useless_joins vs. bug #19560
  In-Reply-To: <614240.1788965658@sss.pgh.pa.us>
  References: <1186816.1784573544@sss.pgh.pa.us> <CAA-aLv4UbdjHarp8zaK4PQjFHZVrt27hxmZqqBUn3yR_5CpgjQ@mail.gmail.com> <3304318.1785087206@sss.pgh.pa.us> <3304418.1785087314@sss.pgh.pa.us> <CAMbWs4_oMOZkKJF77TGwBsiN1vaKS568rxhE0Adzuwhrs6GLnQ@mail.gmail.com> <3968565.1785170019@sss.pgh.pa.us> <3975112.1785176472@sss.pgh.pa.us> <CAMbWs4-4JN51woVH86hMSMmdLfNVXzRav8KHShhP6QGmbtLNDg@mail.gmail.com> <670538.1786924698@sss.pgh.pa.us> <CAMbWs4-C4fwZTtY911MfAkW51G2d9TpqoRqmxCiJbjeO0GG1ug@mail.gmail.com> <839346.1787765112@sss.pgh.pa.us> <CAMbWs4-bgUE1SuNOgMmROhLJbRZz=f4bK7RJAEBQU9uCTSMBnQ@mail.gmail.com> <954541.1787846010@sss.pgh.pa.us> <CAMbWs4_mfPF5pQVT2FAdvjEMqN7pDT2MGJqkVoJLVyGLYAsU+w@mail.gmail.com> <1147476.1787924590@sss.pgh.pa.us> <495449.1788106841@sss.pgh.pa.us> <CAMbWs4_FkUdq1D4rwbty972=JVFgS-ip1K9x6x_izAtqv=k3Eg@mail.gmail.com> <1468659.1788447305@sss.pgh.pa.us> <CAMbWs4_BN_0+mHvS3xpH8WOU47PN3bV5zh3Ny56JLHnB-bamww@mail.gmail.com> <614240.1788965658@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox