Review
Combine qual-based and NOT NULL proofs when reducing outer joins
- Proposal version
- 1
- CommitFest entry
- 7301
- Base commit
- 458c77d74d2bab2014e97f01df19dc805dc7b42a
Review: Combine qual-based and NOT NULL proofs when reducing outer joins (v1)
Reviewed v1-0001 applied on master 458c77d74d2bab2014e97f01df19dc805dc7b42a. The patch required a 3-way merge against current prepjointree.c, join.sql and join.out (those files have moved on since v1, including the later removal of quals made redundant by anti-join reduction); there were no conflict markers, and the merged result is what was tested. Nothing from this thread is on origin/master (git log --grep on the thread Message-ID, plain and URL-encoded, and on the subject, is empty).
Summary
reduce_outer_joins() can turn a LEFT or FULL join into an anti-join when an upper qual forces a Var on the nullable side to be NULL, yet that Var is proven non-null in every matching row. There are already two proofs: quals that hold for those rows (safe quals from the subtree, plus the join's own ON quals in the LEFT case), and a table NOT NULL constraint, the latter previously ignored as soon as pass 1 recorded the rel in nullable_rels.
The patch combines the two. The quals already collected by forced_null_var_is_nonnullable() are also run through find_nonnullable_rels(). If those quals are strict for the rel, it cannot have been null-extended in any row of interest, so forced_null_var_is_attnotnull() may trust its NOT NULL constraints even though lower outer joins could null it in general.
The motivating shape, with Q.ts declared NOT NULL, is:
SELECT * FROM R LEFT JOIN (S LEFT JOIN Q ON S.c = Q.e)
ON R.a = S.c AND Q.f = 1
WHERE Q.ts IS NULL;
Q.f = 1 is strict for Q, so a matching row cannot be a null-extended Q, Q.ts cannot be NULL, and the upper join is an anti-join. The same idea applies at a FULL join, using only safe quals from one input (never that FULL join's own ON quals).
This is a small, well-motivated planner change (132+/9- after tests). No catalog, on-disk, WAL, protocol or SQL-syntax change. The only user-visible effect is that more queries get anti-join plans; results must stay the same. No documentation is needed.
The reasoning is sound. nullable_rels still answers "could this rel ever be null-extended in the subtree?"; find_nonnullable_rels over the collected quals answers the narrower question that actually matters here. The existing attnotnull walk is still per-column, so proving the rel present does not treat a nullable column as NOT NULL. find_nonnullable_rels already accepts a List as implicit-AND, which matches how find_safe_quals builds all_quals. Inheritance parents that are not partitioned tables are still skipped; partitioned tables still use the parent's constraints, which is conservative.
Testing
Build: meson, cassert + debug + injection points + TAP. No compiler warnings; git diff --check is clean.
- Suites setup, regress and isolation: 5/5 pass, including the three new EXPLAIN tests in join.sql (LEFT reduced, LEFT not reduced when the upper ON is non-strict, FULL reduced).
- Release builds, base vs patched, plus the cassert build, on a 6-row fixture and on 200k-row tables: 22 predicates covering the motivating LEFT join, non-strict ON (OR IS NULL, COALESCE, IS NOT DISTINCT FROM), IS NOT NULL and OR-of-strict proofs, an OR that is not strict for the nullable rel, FULL join with and without a strict inner join in the input, whole-row IS NULL, a nullable forced-null column, a three-level nest, a partitioned table with a parent NOT NULL, an inheritance parent with no parent-level NOT NULL, a CHECK (col IS NOT NULL) without attnotnull, a VALUES list, a pulled-up view and subquery, a LATERAL join, (qual) IS TRUE, and a strict qual placed only in a lower left-join ON. Counts and row snapshots all match the unpatched results. No assertion failures.
- Plan changes appeared only where the argument predicts them: anti-join for the strict LEFT and FULL cases, for IS NOT NULL / OR-of-strict / IS TRUE proofs, for the partitioned table, and for the inlined view and subquery; no anti-join for COALESCE, IS NOT DISTINCT FROM, the mixed OR, the bare FULL join, the inheritance parent, the CHECK-only table, VALUES, or a strict qual that lives only in a lower left-join ON (those ON quals are not safe for the subtree). Forcing IS NULL on a nullable column of a rel proven present correctly keeps the outer join.
Gaps in the submitted tests: they are EXPLAIN-only, so they would not catch a wrong anti-join that happened to produce the expected plan shape, and they do not cover IS NOT NULL as the proving qual, partitioned tables, views, or "rel proven present, other column still nullable". A short result-checking test (compare against a seqscan, or against the same query with a non-strict ON) would be cheap insurance.
Performance
Release builds, base and patched on the same master, no parallelism, jit off, data cached. Median of at least 7 alternating runs. Results were identical in every case.
On 200k-row R/S and 160k-row Q, about 16k matches rejected by Q.ts IS NULL:
query base patched change motivating LEFT join 33.8 ms 31.6 ms -6.4% non-strict control 56.2 ms 55.7 ms -0.9%
On 2M-row tables where every left row matches and is then rejected (result 0 rows):
query base patched change hash joins only 1305 ms 1210 ms -7.3% merge joins only 933 ms 667 ms -28.6% default (both chose hash anti/right + merge inner) 988 ms 1013 ms +2.6%
The merge-only win is real: the anti-join avoids emitting and then discarding 2M null-extended rows, and does not need the Materialize that the left merge join used. The first default run on this workload was slower on the patched build because the planner picked a Hash Join for the inner S/Q join instead of the Merge Join used by the unpatched Left Join; a later default series, when both sides used the same inner method, was within noise. So the reduction is beneficial at a given plan shape, but the join-type change can alter costing of the inputs and occasionally pick a slower one. Cases that are not reduced show no meaningful regression.
Code review
-
The change itself is tightly scoped and the new comments in forced_null_var_is_attnotnull() and forced_null_var_is_nonnullable() accurately describe the combined proof. Passing find_nonnullable_rels(all_quals) into the existing helper is the right factoring.
-
find_nonnullable_rels() is invoked only on the attnotnull fallback path, so there is no extra walk when a qual already proves the forced-null Var non-null. Fine at this size; not worth caching.
-
CHECK (col IS NOT NULL) without a column attnotnull, and VALUES/subquery-output columns that do not survive as a base RTE with attnotnull, still cannot use this path. That is existing find_relation_notnullatts() policy, not a regression, but it is why a semantically similar CHECK did not reduce in testing.
-
Tests should also execute the new queries, not only EXPLAIN them. The nearby anti-join tests have the same shape, so this is consistent with the file, but this patch is exactly the case where a wrong reduction would be a silent correctness bug.
Assessment
A small, clearly argued planner improvement with a correct combination of two proofs that already existed. No correctness problem found across LEFT and FULL joins, strict and non-strict proving quals, partitioned tables, pulled-up views, and the important negative case of a nullable column on a rel proven present. Regression tests pass after the 3-way merge onto current master. Remaining gaps are EXPLAIN-only coverage and the possibility that the new anti-join changes costing of the inputs enough to pick a slower plan shape. I would like a result-checking test added, but the approach is right and v1 looks committable with that and a once-over of the expected plans after rebase.