From: Tom Lane <tgl@sss.pgh.pa.us>
To: Richard Guo <guofenglinux@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: Thu, 27 Aug 2026 11:53:30 -0400
Message-ID: <954541.1787846010@sss.pgh.pa.us> (raw)
In-Reply-To: <CAMbWs4-bgUE1SuNOgMmROhLJbRZz=f4bK7RJAEBQU9uCTSMBnQ@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>
Richard Guo <guofenglinux@gmail.com> writes:
> I reviewed v6 as a whole and it LGTM. I have a few small changes to
> suggest, see the attached delta on top of v6.
> * The first one is to initialize all_result_relids/leaf_result_relids
> in subquery_planner like we did before. v5 moved that into
> query_planner, but as far as I can tell the only reason was the Assert
> in the old self-join code, which is gone now. And I think the current
> initialization can contradict the comment in pathnodes.h, as I
> mentioned in [1]:point 1.
I don't like this on the grounds of consistency. As things stand now,
we partially initialize those fields in subquery_planner and then
finish filling them in much later, during add_other_rels_to_query.
What I did in my proposed patch was to collect all of that processing
together, in successive steps near the end of query_planner. This
makes it clear that those fields are not to be relied on before that
point, and in particular that they don't need to be reset in the
restart loop in the initial part of query_planner. If we change it
back as you suggest, then it'll be really nonobvious why this derived
data is handled differently from other derived data. And the fact
that it's only partially valid during the restart loop seems to me
to be a bug hazard: if someone tried to rely on these fields in
logic within that loop, it might seem to work as long as they'd not
tested it on inheritance/partitioning cases. So I think moving this
logic is good cleanup, albeit maybe not strictly necessary to the
immediate problem.
I take your point that the early-exit-for-trivial-jointree path now
fails to set these fields at all, but I'd rather handle that by
adding a couple more lines in that path to fill them in. Yup, it'd
be duplicate logic, but it seems cleaner that way.
> * The Assert on resultRelation in remove_useless_outer_joins can be
> dropped, because join_is_removable already rejects that case.
Well, yeah, but on that argument we could drop the entire stanza,
because it's just checking that join_is_removable didn't mess up.
Maybe we should? I've not heard that anybody ever hit those Asserts.
Your other changes look good.
regards, tom lane
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, thom@linux.com, jacob.brazeal@gmail.com
Subject: Re: remove_useless_joins vs. bug #19560
In-Reply-To: <954541.1787846010@sss.pgh.pa.us>
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>
* 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