Collect quals for outer-join reduction on demand

To prove that a forced-null Var is non-nullable in every matching row,
reduce_outer_joins() uses the strict quals that hold for every row a
subtree emits.  The first pass currently gathers those quals into each
subtree's state, so that the second pass need not re-walk the jointree
at decision time.

That duplicates find_subquery_safe_quals(), which already performs
this traversal for query_outputs_are_not_nullable(), and it charges
every query containing an outer join for a list build at every
FromExpr and every inner or semi join, whether or not any upper qual
turns out to force a Var null.  Since each level copies what its
children collected, the space required grows quadratically with the
depth of nested inner joins.  What it buys is small even then: the
walk it avoids is cheap enough to be called on demand.

So rename find_subquery_safe_quals() to find_safe_quals(), export it,
and drop safe_quals from the first pass's state in favor of the
subtree's jointree node.  The second pass calls find_safe_quals() at
the point where it needs the proof.

Also stop treating a semijoin there like a left join: semijoins are
reachable from the new caller, and a semijoin emits only matched
left-hand rows, so its ON quals do hold for every output row.

Author: Richard Guo <[email protected]>
Discussion: 
https://postgr.es/m/cambws4-6fazy7+vcovr0-vqnuaxozic8o4d4zrwmm2woi+q...@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/31c002aa89787e4bbe237e36f51dead96b674998

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 61 ++++++-------------------------
src/backend/optimizer/util/clauses.c      | 54 ++++++++++++++++-----------
src/include/optimizer/clauses.h           |  1 +
3 files changed, 45 insertions(+), 71 deletions(-)

Reply via email to