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: Sun, 13 Sep 2026 13:32:15 -0400
Message-ID: <9752.1789320735@sss.pgh.pa.us> (raw)
In-Reply-To: <CAMbWs4_ZMbqMq5UxMUZi-pZV59G+zHaa-rXhbVSSbC9wvd1AhQ@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>
	<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>
	<CAMbWs4_ZMbqMq5UxMUZi-pZV59G+zHaa-rXhbVSSbC9wvd1AhQ@mail.gmail.com>

Richard Guo <guofenglinux@gmail.com> 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






view thread (74+ messages)  latest in thread

Message-ID: <9752.1789320735@sss.pgh.pa.us>
Permalink:  ../9752.1789320735@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/9752.1789320735@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: <9752.1789320735@sss.pgh.pa.us>
  References: <1186816.1784573544@sss.pgh.pa.us> <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_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> <CAMbWs4_ZMbqMq5UxMUZi-pZV59G+zHaa-rXhbVSSbC9wvd1AhQ@mail.gmail.com> <9752.1789320735@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