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: Mon, 31 Aug 2026 11:56:10 -0400
Message-ID: <1041163.1788191770@sss.pgh.pa.us> (raw)
In-Reply-To: <CAMbWs4-Jou+dkxuwO3ikf91L8nBnDSAbYJ7uucKzegmsuEw7Mw@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>
	<250168.! 1788034245@sss.pgh.pa.us>
	<CAMbWs4-5aHFeGy9ArFbozdpxy4xFEtWgXzGTW7KoYOjzX9nXGA@mail.gmail.com>
	<495449.1788106841@sss.pgh.pa.us>
	<CAHewXNk8b0TsSy4dL=CO7FXL2W3WBm0BcdP-zwNJePa-Qj4HzA@mail.gmail.com>
	<CAMbWs4-Jou+dkxuwO3ikf91L8nBnDSAbYJ7uucKzegmsuEw7Mw@mail.gmail.com>

Richard Guo <guofenglinux@gmail.com> writes:
> According to that, I can reproduce this same issue with the query
> below:

> create table t (a int primary key);

> select 1 from t t1 left join
>   (t t2 left join t t3 on t3.a = t2.a) on true
> where t2.a is null and t3.a is null;

> I think the real problem is that we leave a qual in the tree after
> deciding that it is redundant.  So the attached patch removes such
> quals from the jointree at the end of reduce_outer_joins.

+1 for concept.

> With the quals removed before deconstruct_jointree,
> check_redundant_nullability_qual has nothing left to do, so I think we
> can get rid of it in passing.

Right, what this is really doing is moving that processing out of
the deconstruct_jointree phase and requiring reduce_outer_joins to
clean up its own mess.  Our git history shows I'm to blame for the
way it's done now, but I sure don't remember why I did it like that.
This way seems clearly neater: fewer moving parts, less inconsistency
between different representations of the query.

A few nits, mostly about commentary:

* Your commit message explains this as
"check_redundant_nullability_qual has nothing left to detect, so
remove it".  But as I just said, it's better to think of this change
as moving this processing to an earlier phase, so that should be
explained differently.

* The comment for find_forced_null_var says it's used by initsplan.c.
But what that is referencing is the call from
check_redundant_nullability_qual; the usage is moving to
prepjointree.c, so that comment needs a small adjustment.

* The new comment for remove_redundant_nullability_quals_recurse says
"they would retain references to rels that join removal might later
delete", but I don't think that's quite accurate.  If we didn't do
this qual-removal in either phase, then the Var reference would still
be there and join removal would not think it can remove the join.
So it seems to me that better wording would be like "they could
prevent join removal".  But that puts the lie to the claim that this
is "not just an optimization", so the whole sentence needs a bit of
reworking.

* I feel like the naming of remove_redundant_nullability_quals
and remove_redundant_nullability_quals_recurse is a bit weird.
Usually our pattern is that a function named like foo_recurse
is an inner workhorse for a function named foo.  This is the
other way around, which seems like it'd confuse readers.

* I don't understand the coding in remove_redundant_nullability_quals.
We require quals to be in implicit-AND format by this point, so they
should be either NIL or a List.  Is the last stanza actually
reachable?  (Maybe you are comparing this to find_forced_null_vars,
but the reason that handles both Lists and not-Lists is that it
processes a List via self-recursion.  This doesn't, so I think it
could just be a simple foreach loop over the argument.  Maybe use
castNode(List, quals) to enforce the assumption.)


What I'm taking away from all this is that 2ebf25e7d is forcing us
to be neater about management of the parse tree, which is probably
a good thing overall.

			regards, tom lane






view thread (74+ messages)  latest in thread

Message-ID: <1041163.1788191770@sss.pgh.pa.us>
Permalink:  ../1041163.1788191770@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1041163.1788191770@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: <1041163.1788191770@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-5aHFeGy9ArFbozdpxy4xFEtWgXzGTW7KoYOjzX9nXGA@mail.gmail.com> <495449.1788106841@sss.pgh.pa.us> <CAHewXNk8b0TsSy4dL=CO7FXL2W3WBm0BcdP-zwNJePa-Qj4HzA@mail.gmail.com> <CAMbWs4-Jou+dkxuwO3ikf91L8nBnDSAbYJ7uucKzegmsuEw7Mw@mail.gmail.com> <1041163.1788191770@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