Reduce LEFT/FULL JOIN to ANTI JOIN in more cases

Поиск
Список
Период
Сортировка
Искать

Reduce LEFT/FULL JOIN to ANTI JOIN in more cases

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
Вложения:
reduce_outer_joins() already recognizes that a LEFT JOIN is really an
anti-join when an upper qual forces a nullable-side Var to be NULL
while that Var is actually non-null in every matching row.  In that
case only the null-extended (unmatched) rows can satisfy the upper
qual, which is exactly anti-join semantics, so we switch JOIN_LEFT to
JOIN_ANTI.  This is worth detecting because an anti-join is usually
much cheaper than computing the outer join and then filtering the
result with the IS NULL clause.

Currently we recognize the Var as non-null only if the join's own
clauses are strict for it, or if it is defined NOT NULL.  But a strict
qual inside the RHS subtree is just as good a proof, since it holds
for every row the RHS emits.  For example,

    SELECT * FROM t1 LEFT JOIN
        (t2 JOIN t3 ON t2.c = t3.c) ON t1.a = t2.b
    WHERE t3.c IS NULL;

is an anti-join (t2.c = t3.c makes t3.c non-null), but we don't spot
it today.  0001 does that.

0002 does the same for FULL joins, where a forced-null Var on either
side is enough: once it is proven non-null, every row on that side
drops out and only the other side's unmatched rows remain.  So, with
t2.a defined NOT NULL

    SELECT * FROM t1 FULL JOIN t2 ON t1.x = t2.x
    WHERE t2.a IS NULL;

or

    SELECT * FROM t1 FULL JOIN
        (t2 JOIN t3 ON t2.c = t3.c) ON t1.a = t2.b
    WHERE t3.c IS NULL;

becomes t1 ANTI t2.  But note that we can't reuse the join's own ON
quals as the proof here, the way the LEFT JOIN code does, since they
don't hold for the unmatched rows we need to cover.

Regarding cost: the extra planning work is small.  The proving quals
ride along on the pass-1 walk that reduce_outer_joins() already does
for nullable_rels, so there is no new traversal, and the non-null
proof in pass 2 only runs when a join actually has a forced-null Var
sitting above it.  Given that an anti-join is usually much cheaper
than a left or full join, it looks like a good trade.

Thoughts?

- Richard

Re: Reduce LEFT/FULL JOIN to ANTI JOIN in more cases

От:
Isaac Morland <isaac.morland@gmail.com>
Дата:
On Thu, 4 Jun 2026 at 22:55, Richard Guo <guofenglinux@gmail.com> wrote:
reduce_outer_joins() already recognizes that a LEFT JOIN is really an
anti-join when an upper qual forces a nullable-side Var to be NULL
while that Var is actually non-null in every matching row.  In that
case only the null-extended (unmatched) rows can satisfy the upper
qual, which is exactly anti-join semantics, so we switch JOIN_LEFT to
JOIN_ANTI.  This is worth detecting because an anti-join is usually
much cheaper than computing the outer join and then filtering the
result with the IS NULL clause.

While you’re at it, any chance of changing it so that "a LEFT JOIN b WHERE b IS NULL" is guaranteed not to have worse performance than " a LEFT JOIN b WHERE b.f IS NULL"? I've had this and asked about it here and it was suggested that I should specify the primary key fields of b; but surely if I want an antijoin it's clearer and better implicit documentation if I don't zero in on specific fields. I think somebody else is suggesting "a LEFT ANTI JOIN b" which would be even better.

Re: Reduce LEFT/FULL JOIN to ANTI JOIN in more cases

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Fri, Jun 5, 2026 at 12:00 PM Isaac Morland  wrote:
> On Thu, 4 Jun 2026 at 22:55, Richard Guo  wrote:
>> reduce_outer_joins() already recognizes that a LEFT JOIN is really an
>> anti-join when an upper qual forces a nullable-side Var to be NULL
>> while that Var is actually non-null in every matching row.  In that
>> case only the null-extended (unmatched) rows can satisfy the upper
>> qual, which is exactly anti-join semantics, so we switch JOIN_LEFT to
>> JOIN_ANTI.  This is worth detecting because an anti-join is usually
>> much cheaper than computing the outer join and then filtering the
>> result with the IS NULL clause.

> While you’re at it, any chance of changing it so that "a LEFT JOIN b WHERE b IS NULL" is guaranteed not to have worse performance than " a LEFT JOIN b WHERE b.f IS NULL"? I've had this and asked about it here and it was suggested that I should specify the primary key fields of b; but surely if I want an antijoin it's clearer and better implicit documentation if I don't zero in on specific fields. I think somebody else is suggesting "a LEFT ANTI JOIN b" which would be even better.

The reason the planner doesn't recognize "WHERE b IS NULL" during
outer-join reduction is that find_forced_null_var() skips NullTest
nodes with argisrow=true, which is what the parser produces for a
whole-row IS NULL test.  So the anti-join reduction never sees it.

To handle it we'd need to decompose the whole-row test into per-column
checks, and that expansion needs the relation's column list, which
find_forced_null_var() doesn't have.  One option would be to let
find_forced_null_var() recognize the whole-row IS NULL and hand back
the whole-row Var, then have reduce_outer_joins() expand it into the
relation's columns before running the non-null proof.

That said, I'm not sure it's worth the churn.  IMHO, the whole-row IS
NULL test seems less common than the column form.  If you want an
anti-join, it seems more natural to just write NOT EXISTS.  For the
same reason, I'm not convinced a "LEFT ANTI JOIN" would buy us much
over NOT EXISTS.

- Richard


Re: Reduce LEFT/FULL JOIN to ANTI JOIN in more cases

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Fri, Jun 5, 2026 at 6:33 PM wenhui qiu  wrote:
> Maybe I'm overthinking this, but one implementation detail worth considering
> is whether pass1 should store the derived proof directly, rather than carrying
> the original safe qual lists around.

I considered that.  It would give the same answer, but I think
carrying the raw quals is the better deal: pass2 runs
find_nonnullable_vars only when it actually has forced-null vars in
hand, so the common case of outer joins without any IS NULL quals pays
nothing, whereas deriving the set in pass1 would walk every collected
qual for every query containing an outer join.

Also, no qual ever gets walked more than once in pass2: at each
candidate join, the quals its own check consults and the quals pass1
propagates upward are complements (a LEFT join propagates the left
child's quals and consults the right child's, and so on).  A qual
consulted by one join therefore never reaches an upper join's proof
set, so pass2 walks each qual at most once.  This is the same pruning
correctness requires anyway: quals within a nullable side don't hold
for null-extended rows, and that's exactly the side the join's check
consults.

- Richard


Re: Reduce LEFT/FULL JOIN to ANTI JOIN in more cases

От:
wenhui qiu <qiuwenhuifx@gmail.com>
Дата:
HI Richard

> I considered that.  It would give the same answer, but I think
> carrying the raw quals is the better deal: pass2 runs
> find_nonnullable_vars only when it actually has forced-null vars in
> hand, so the common case of outer joins without any IS NULL quals pays
> nothing, whereas deriving the set in pass1 would walk every collected
> qual for every query containing an outer join.
Thank you for your explanation, 

> 0001 and 0002 are as before. 
   0003,0004   LGTM, I will invite other Hackers to take a look.


Thanks  

Re: Reduce LEFT/FULL JOIN to ANTI JOIN in more cases

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Tue, Aug 18, 2026 at 5:40 PM wenhui qiu  wrote:
>> Thank you for updating the path. The test went well. LGTM

Thanks!  Pushed.

Isaac, this should also address your complaint that a whole-row IS
NULL test performs worse than the equivalent per-column one.

- Richard


FAQ