Re: Incorrect result in certain expressions of anti-join

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Torbjörn Gannholm <torbjorn(dot)gannholm(at)gmail(dot)com>
Cc: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: Incorrect result in certain expressions of anti-join
Date: 2025-01-07 23:19:47
Message-ID: CAKFQuwbLzWXVfZh1zaoMV1eNUKgyuzcNOYtdbqtd14S6hu0Omw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Tuesday, January 7, 2025, Torbjörn Gannholm <torbjorn(dot)gannholm(at)gmail(dot)com>
wrote:

>
> select * from bar LEFT JOIN foo ON foo.a = bar.a WHERE foo.a IS NULL;
> select * from bar LEFT JOIN foo ON foo.a = bar.a AND foo.a IS NULL;
>
>
The where clause is evaluated after the join and thus the join sees all
rows from foo and bar.

When you place the expression in the ON clause it gets evaluated
before/during the join, and thus effectively pushed down and the rows of
foo are filtered before the join happens.

Separately, I would advise using NOT EXISTS for an anti-join and not
mention foo in the from clause at all. That is the canonical form.

David J.

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Paquier 2025-01-07 23:50:33 Re: BUG #18146: Rows reappearing in Tables after Auto-Vacuum Failure in PostgreSQL on Windows
Previous Message Pantelis Theodosiou 2025-01-07 22:03:50 Re: Incorrect result in certain expressions of anti-join