pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Collect quals for outer-join reduction on demand
Date: Fri, 21 Aug 2026 06:52:25 +0000
Message-ID: <E1wxJ72-00000001Tc4-325W@gemulon.postgresql.org> (raw)

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@gmail.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(-)



Message-ID: <E1wxJ72-00000001Tc4-325W@gemulon.postgresql.org>
Permalink:  ../E1wxJ72-00000001Tc4-325W@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1wxJ72-00000001Tc4-325W@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: rguo@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Collect quals for outer-join reduction on demand
  In-Reply-To: <E1wxJ72-00000001Tc4-325W@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox