Pulling up sublink may break join-removal logic

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Pulling up sublink may break join-removal logic
Date: 2020-04-28 07:04:30
Message-ID: CAMbWs4-THacv3DdMpiTrvg5ZY7sNViFF1pTU=kOKmtPBrE9-0Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

I happened to notice $subject and not sure if it's an issue or not. When
we're trying to remove a LEFT JOIN, one of the requirements is the inner
side needs to be a single baserel. If there is a join qual that is a
sublink and can be converted to a semi join with the inner side rel, the
inner side would no longer be a single baserel and as a result the LEFT
JOIN can no longer be removed.

Here is an example to illustrate this behavior:

create table a(i int, j int);
create table b(i int UNIQUE, j int);
create table c(i int, j int);

# explain (costs off) select a.i from a left join b on a.i = b.i and
b.j in (select j from c where b.i = c.i);
QUERY PLAN
---------------
Seq Scan on a
(1 row)

For the query above, we do not pull up the sublink and the LEFT JOIN is
removed.

# explain (costs off) select a.i from a left join b on a.i = b.i and
b.j in (select j from c);
QUERY PLAN
---------------------------------------
Hash Left Join
Hash Cond: (a.i = b.i)
-> Seq Scan on a
-> Hash
-> Hash Semi Join
Hash Cond: (b.j = c.j)
-> Seq Scan on b
-> Hash
-> Seq Scan on c
(9 rows)

Now for this above query, the sublink is pulled up to be a semi-join
with inner side rel 'b', which makes the inner side no longer a single
baserel. That causes the LEFT JOIN failing to be removed.

That is to say, pulling up sublink sometimes breaks join-removal logic.
Is this an issue that bothers you too?

Thanks
Richard

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2020-04-28 07:10:00 Handling of concurrent aborts in logical decoding of in-progress xacts
Previous Message Michael Paquier 2020-04-28 06:09:58 Re: Why are wait events not reported even though it reads/writes a timeline history file?