Re: NOT IN subquery optimization

From: David Rowley <david(dot)rowley(at)2ndquadrant(dot)com>
To: Jim Finnerty <jfinnert(at)amazon(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: NOT IN subquery optimization
Date: 2019-02-21 20:44:14
Message-ID: CAKJS1f_a4Ek6+xU0avJ9XSp_7AJhEFZbYk56bKJUnfLdSiHF2g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, 21 Feb 2019 at 16:27, Jim Finnerty <jfinnert(at)amazon(dot)com> wrote:
> We can always correctly transform a NOT IN to a correlated NOT EXISTS. In
> almost all cases it is more efficient to do so. In the one case that we've
> found that is slower it does come down to a more general costing issue, so
> that's probably the right way to think about it.

I worked on this over 4 years ago [1]. I think the patch there is not
completely broken and seems just to need a few things fixed. I rebased
it on top of current master and looked at it. I think the main
remaining issue is fixing the code that ensures the outer side join
quals can't be NULL. The code that's there looks broken still since
it attempts to use quals from any inner joined rel for proofs that
NULLs will be removed. That might not work so well in a case like:
SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a AND t2.b NOT IN(select b
from t3), however, I'd need to think harder about that since if there
was such a qual then the planner should convert the left join into an
inner join. But anyway, the function expressions_are_not_nullable()
was more intended to work with targetlists to ensure exprs there can't
be NULL. I just had done a poor job of trying to modify that into
allowing it to take exprs from any random place, likely that should be
a new function and expressions_are_not_nullable() should be put back
to what Tom ended up with.

I've attached the rebased and still broken version.

[1] https://www.postgresql.org/message-id/CAApHDvqRB-iFBy68%3DdCgqS46aRep7AuN2pou4KTwL8kX9YOcTQ%40mail.gmail.com

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

Attachment Content-Type Size
not_in_anti_join_v1.0.patch application/octet-stream 37.6 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jerry Sievers 2019-02-21 20:57:23 Re: [patch] Add schema total size to psql \dn+
Previous Message Tom Lane 2019-02-21 20:18:03 Re: [HACKERS] WIP: Aggregation push-down