Reduce LEFT/FULL JOIN to ANTI JOIN in more cases
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
Attachments:
t139865_1v1-0002-Reduce-FULL-JOIN-to-ANTI-JOIN.patchapplication/octet-stream; name=v1-0002-Reduce-FULL-JOIN-to-ANTI-JOIN.patchDownload+335-44
v1-0001-Reduce-LEFT-JOIN-to-ANTI-JOIN-using-quals-within-.patchapplication/octet-stream; name=v1-0001-Reduce-LEFT-JOIN-to-ANTI-JOIN-using-quals-within-.patchDownload+303-20
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.
On Fri, Jun 5, 2026 at 12:00 PM Isaac Morland <isaac.morland@gmail.com> wrote:
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.
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
Hi Richard
Thanks for working on this. I think the overall idea makes sense: if
a
a strict qual inside the RHS subtree proves that a RHS Var is non-null for
every row emitted by that subtree, then using that proof to recognize the
LEFT JOIN ... IS NULL case as an anti join seems valid .
The propagation rules also look conservative: inner/semi joins can carry
proofs from both sides plus their own quals, while outer joins only carry
proofs from the non-nullable side.
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. `safe_quals` seems to be used later
only
to call `find_nonnullable_vars()`, so the state could perhaps keep something
like:
```
List *safe_nonnullable_vars;
then, when a safe quail is found:
result->safe_nonnullable_vars =
mbms_add_members(result->safe_nonnullable_vars,
find_nonnullable_vars(f->quals));
And in the anti-join check:
nonnullable_vars =
mbms_add_members(NIL, right_state->safe_nonnullable_vars);
nonnullable_vars =
mbms_add_members(nonnullable_vars,
find_nonnullable_vars(j->quals));
That would keep the pass1 state closer to what pass2 actually needs, and
avoid
carrying/concatenating implicit-AND qual lists only to rescan them later.
One
small detail is that mbms_add_members() mutates its first argument, so when
propagating a child state through an outer join we'd want to copy it, e.g.
with
mbms_add_members(NIL, child_state->safe_nonnullable_vars), rather than
assign
the child list directly.
This is not a correctness objection to the current patch, just a possible
way
to make the stored state match its only consumer more closely.
Thanks
On Fri, Jun 5, 2026 at 3:50 PM Richard Guo <guofenglinux@gmail.com> wrote:
Show quoted text
On Fri, Jun 5, 2026 at 12:00 PM Isaac Morland <isaac.morland@gmail.com>
wrote: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.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
On Fri, Jun 5, 2026 at 6:33 PM wenhui qiu <qiuwenhuifx@gmail.com> 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
On Fri, Jun 5, 2026 at 4:50 PM Richard Guo <guofenglinux@gmail.com> wrote:
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.
After a second thought, I don't think we need to explicitly expand the
whole-row Var in order to support "WHERE b IS NULL": proving any one
column of b non-null in matching rows is sufficient to treat it as an
anti-join. I tried it and it coped with the existing machinery fairly
cleanly. See 0003.
A subtlety is that a non-null whole-row datum can still have all
columns NULL. So a datum-level proof does not establish that the
columns are non-null, and must be ignored for the whole-row case.
(Consider "t1 left join t2 on t1 = t2 where t2 is null", where t2 has
no NOT NULL column.) Relatedly, a row-format test on a composite-type
column is not handled, since "b.c IS NULL" does not force the column c
null.
While at it, I extended the same idea to "WHERE b IS NOT NULL", which
lets a left join reduce to an inner join. See 0004. The subtlety
there is the reverse: although "IS NOT NULL" being true implies every
field is non-null, we exploit only the weaker "datum is non-null",
since that whole-row fact is shared with datum-level strict contexts
like record comparisons.
0001 and 0002 are as before.
- Richard
Attachments:
t139865_6v2-0001-Reduce-LEFT-JOIN-to-ANTI-JOIN-using-quals-within-.patchapplication/octet-stream; name=v2-0001-Reduce-LEFT-JOIN-to-ANTI-JOIN-using-quals-within-.patchDownload+303-20
v2-0002-Reduce-FULL-JOIN-to-ANTI-JOIN.patchapplication/octet-stream; name=v2-0002-Reduce-FULL-JOIN-to-ANTI-JOIN.patchDownload+335-44
v2-0003-Reduce-outer-joins-to-anti-joins-for-whole-row-IS.patchapplication/octet-stream; name=v2-0003-Reduce-outer-joins-to-anti-joins-for-whole-row-IS.patchDownload+311-10
v2-0004-Relax-strictness-detection-for-row-format-IS-NOT-.patchapplication/octet-stream; name=v2-0004-Relax-strictness-detection-for-row-format-IS-NOT-.patchDownload+149-5
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
The patchset no longer applies, so here is a rebased version. There
are no changes other than the rebase and some cosmetic tweaks.
I plan to give this another self-review and push it soon, barring
objections.
- Richard
Attachments:
t139865_8v3-0001-Reduce-LEFT-JOIN-to-ANTI-JOIN-using-quals-within-.patchapplication/octet-stream; name=v3-0001-Reduce-LEFT-JOIN-to-ANTI-JOIN-using-quals-within-.patchDownload+319-23
v3-0002-Reduce-FULL-JOIN-to-ANTI-JOIN.patchapplication/octet-stream; name=v3-0002-Reduce-FULL-JOIN-to-ANTI-JOIN.patchDownload+429-44
v3-0003-Reduce-outer-joins-to-anti-joins-for-whole-row-IS.patchapplication/octet-stream; name=v3-0003-Reduce-outer-joins-to-anti-joins-for-whole-row-IS.patchDownload+314-23
v3-0004-Relax-strictness-detection-for-row-format-IS-NOT-.patchapplication/octet-stream; name=v3-0004-Relax-strictness-detection-for-row-format-IS-NOT-.patchDownload+149-5
hi Richard
Thank you for updating the path. The test went well. LGTM
Thanks
On Tue, Aug 18, 2026 at 5:40 PM wenhui qiu <qiuwenhuifx@gmail.com> 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
I can't help pondering how 0001 collects the quals used to prove a
forced-null Var non-nullable. Pass 1 gathers them into every
subtree's state so that pass 2 doesn't have to re-walk the jointree at
decision time.
Two things bother me about that. First, we already have this
traversal: find_subquery_safe_quals() does the same job for
query_outputs_are_not_nullable(). Two copies with the same rule seems
like something we should avoid. Second, pass 1 builds a list at every
FromExpr and every inner or semi join, for every query containing an
outer join, and each level copies what its children collected, so in a
deep chain of inner joins the same quals get copied over and over on
the way up. What is worse, if there are no forced-null Vars in upper
quals (which is quite common), all of the lists we build in pass 1 are
just wasted.
So I think a better approach is to keep just the jointree node in the
pass 1 state, and have pass 2 call find_subquery_safe_quals() where it
needs the proof. The walk is cheap enough to do on demand.
Attached is a patch showing what I mean.
- Richard