Re: remove_useless_joins vs. bug #19560

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Thom Brown <thom(at)linux(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: remove_useless_joins vs. bug #19560
Date: 2026-07-27 07:07:56
Message-ID: CAMbWs4_oMOZkKJF77TGwBsiN1vaKS568rxhE0Adzuwhrs6GLnQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Jul 27, 2026 at 2:35 AM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Drat, failed to hit control-C control-E. Now with patches attached,
> for sure.

Thanks for the patchset. Nice cleanup:

911 insertions(+), 1670 deletions(-)

I noticed that the call to remove_rels_from_query_tree() at the end of
remove_useless_joins() performs a full traversal over the query tree
to delete the removed relids. I wonder if this is a bit too
brute-force, as AFAICS only PHVs can still contain references to the
removed relids. If there were any other remaining references,
join_is_removable() wouldn't have considered the join removable in the
first place.

If that's correct, perhaps we can simplify
remove_rels_from_query_tree() to something lighter, similar to
remove_result_refs().

Also, the issue with duplicate clauses after self-join elimination
reported in bug #19435 is still present in v4. It would be great if
we could address that in passing as well.

Here is a simplified repro:

create table t (a int unique, b int);

explain (costs off)
select * from t t1 left join
(t t2 join t t3 on t2.a = t3.a) on t3.a is not null;
QUERY PLAN
-------------------------------------------------------------
Nested Loop Left Join
-> Seq Scan on t t1
-> Materialize
-> Seq Scan on t t3
Filter: ((a IS NOT NULL) AND (a IS NOT NULL))
(5 rows)

- Richard

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeevan Chalke 2026-07-27 07:31:37 Re: [PATCH] Speed up repeat() for larger counts
Previous Message Pavel Stehule 2026-07-27 07:02:34 Re: POC: PLpgSQL FOREACH IN JSON ARRAY