Re: [HACKERS] Removing LEFT JOINs in more cases

From: David Rowley <david(dot)rowley(at)2ndquadrant(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Ashutosh Bapat <ashutosh(dot)bapat(at)enterprisedb(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [HACKERS] Removing LEFT JOINs in more cases
Date: 2018-03-04 08:43:23
Message-ID: CAKJS1f_kvBhFn0C3Tu1wGBY4t7gGAFH_9VhsP2g+F3nrwSPDWA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 4 March 2018 at 18:35, David Rowley <david(dot)rowley(at)2ndquadrant(dot)com> wrote:
> drop table if exists t1;
>
> create table t1 (a int);
> insert into t1 values(1);
>
> create or replace function notice(pn int) returns int as $$
> begin
> raise notice '%', pn;
> return pn;
> end;
> $$ volatile language plpgsql;
>
> create unique index t1_a_uidx on t1(a);
>
> explain (costs off, analyze, timing off, summary off)
> select t1.a from t1 left join t1 t2 on t1.a = t2.a and notice(t2.a) = t1.a;
> QUERY PLAN
> ----------------------------------------
> Seq Scan on t1 (actual rows=1 loops=1)
> (1 row)
>
> drop index t1_a_uidx; -- drop the index to disallow left join removal.
>
> explain (costs off, analyze, timing off, summary off)
> select t1.a from t1 left join t1 t2 on t1.a = t2.a and notice(t2.a) = t1.a;
> NOTICE: 1
> QUERY PLAN
> ----------------------------------------------------------
> Nested Loop Left Join (actual rows=1 loops=1)
> Join Filter: ((t1.a = t2.a) AND (notice(t2.a) = t1.a))
> -> Seq Scan on t1 (actual rows=1 loops=1)
> -> Seq Scan on t1 t2 (actual rows=1 loops=1)
> (4 rows)
>
> Should this be fixed? or is this case somehow not worth worrying about?

Please find attached two patches. The first of which is intended to
resolve the issue mentioned above with consideration that it may need
to be back-patched to where LEFT JOIN removals where introduced.

Patch two is intended to implement LEFT JOIN removal for cases that
any duplicates rows that the join causes would be subsequently removed
again via a GROUP BY or DISTINCT clause.

--
David Rowley http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

Attachment Content-Type Size
v2-0001-Disallow-LEFT-JOIN-removal-when-join-or-base-qual.patch application/octet-stream 5.3 KB
v2-0002-Allow-LEFT-JOINs-to-be-removed-in-more-cases.patch application/octet-stream 19.0 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Fabien COELHO 2018-03-04 09:09:11 Re: pgbench - allow to specify scale as a size
Previous Message Pavel Stehule 2018-03-04 07:52:27 Re: 2018-03 Commitfest Summary (Andres #1)