Re: Removing unneeded self joins

From: Greg Stark <stark(at)mit(dot)edu>
To: Ronan Dunklau <ronan(dot)dunklau(at)aiven(dot)io>
Cc: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, Andrey Lepikhov <a(dot)lepikhov(at)postgrespro(dot)ru>, Hywel Carver <hywel(at)skillerwhale(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Konstantin Knizhnik <knizhnik(at)garret(dot)ru>
Subject: Re: Removing unneeded self joins
Date: 2022-02-28 22:03:25
Message-ID: CAM-w4HP9piqhdTLzmPGBLVxKvkRxdFYNchRK3+4Tkn_Rx9p1hQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, 1 Jul 2021 at 02:38, Ronan Dunklau <ronan(dot)dunklau(at)aiven(dot)io> wrote:
>
> Well in some cases they can't, when the query is not emitting redundant
> predicates by itself but they are added by something else like a view or a RLS
> policy.
> Maybe it would be worth it to allow spending a bit more time planning for
> those cases ?

Yeah, I'm generally in favour of doing more work in the optimizer to
save query authors work writing queries.

My question is whether it handles cases like:

select b.x,c.y
from t
join t2 as b on (b.id = t.id)
join t2 as c on (c.id = t.id)

That is, if you join against the same table twice on the same qual.
Does the EC mechanism turn this into a qual on b.id = c.id and then
turn this into a self-join that can be removed?

That's the usual pattern I've seen this arise. Not so much that people
write self joins explicitly but that they add a join to check some
column but that is happening in some isolated piece of code that
doesn't know that that join is already in the query. You can easily
end up with a lot of joins against the same table this way.

It's not far different from the old chestnut

select (select x from t2 where id = t.id) as x,
(select y from t2 where id = t.id) as y
from t

which is actually pretty hard to avoid sometimes.

--
greg

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Chapman Flack 2022-02-28 22:30:11 Re: real/float example for testlibpq3
Previous Message Tom Lane 2022-02-28 22:02:34 Re: Patch: Code comments: why some text-handling functions are leakproof