Re: [HACKERS] Removing LEFT JOINs in more cases - Mailing list pgsql-hackers

From David Rowley
Subject Re: [HACKERS] Removing LEFT JOINs in more cases
Date
Msg-id CAKJS1f9BrSy62oDx6t=pJOOKUWBNYdPmp8YJAqFaUgGp5F_aGg@mail.gmail.com
Whole thread Raw
In response to Re: [HACKERS] Removing LEFT JOINs in more cases  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: [HACKERS] Removing LEFT JOINs in more cases  (David Rowley <david.rowley@2ndquadrant.com>)
Re: [HACKERS] Removing LEFT JOINs in more cases  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
On 10 January 2018 at 08:44, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>         select distinct nextval('foo') from a left join b ...
>
> The presence of the DISTINCT again doesn't excuse changing how often
> nextval() gets called.
>
> I kinda doubt this list of counterexamples is exhaustive, either;
> it's just what occurred to me in five or ten minutes thought.
> So maybe you can make this idea work, but you need to think much
> harder about what the counterexamples are.

While working on the cases where the join removal should be disallowed
I discovered that the existing code is not too careful about this
either:

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?

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


pgsql-hackers by date:

Previous
From: Charles Cui
Date:
Subject: Re: GSOC 2018 ideas
Next
From: Pavel Stehule
Date:
Subject: Re: 2018-03 Commitfest Summary (Andres #1)