pgsql: Collect quals for outer-join reduction on demand

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Collect quals for outer-join reduction on demand
Date: 2026-08-21 06:52:25
Message-ID: E1wxJ72-00000001Tc4-325W@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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 <guofenglinux(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAMbWs4-6FazY7+vCOvR0-vqNuaXOZic8O4D4ZrwMm2woi+Q3uA@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(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2026-08-21 07:40:35 pgsql: Use pg_parse_lsn() for server-supplied LSNs
Previous Message Fujii Masao 2026-08-21 03:38:03 pgsql: psql: Do not let invalid \getresults affect the next query