pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Short-circuit row estimation in NOT IN containing NULL consts
Date: Thu, 19 Mar 2026 04:17:01 +0000
Message-ID: <E1w34oe-000R9F-2l@gemulon.postgresql.org> (raw)

Short-circuit row estimation in NOT IN containing NULL consts

ScalarArrayOpExpr used for either NOT IN or <>/= ALL, when the array
contains a NULL constant, will never evaluate to true.  Here we add an
explicit short-circuit in scalararraysel() to account for this and return
0.0 rows when we see that a NULL exists.  When the array is a constant,
we can very quickly see if there are any NULL values and return early
before going to much effort in scalararraysel().  For non-const arrays,
we short-circuit after finding the first NULL and forego selectivity
estimations of any remaining elements.

In the future, it might be better to do something for this case in
constant folding.  We would need to be careful to only do this for
strict operators on expressions located in places that don't care about
distinguishing false from NULL returns. i.e. EXPRKIND_QUAL expressions.
Doing that requires a bit more thought and effort, so here we just fix
some needlessly slow selectivity estimations for ScalarArrayOpExpr
containing many array elements and at least one NULL.

Author: Ilia Evdokimov <ilya.evdokimov@tantorlabs.com>
Reviewed-by: David Geier <geidav.pg@gmail.com>
Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com>
Reviewed-by: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/eaa2598c-5356-4e1e-9ec3-5fd6eb1cd704@tantorlabs.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/c95cd2991f1e3ece689adfe662082f200126d255

Modified Files
--------------
src/backend/utils/adt/selfuncs.c          | 17 +++++++++++++++++
src/test/regress/expected/planner_est.out | 27 +++++++++++++++++++++++++++
src/test/regress/sql/planner_est.sql      | 15 +++++++++++++++
3 files changed, 59 insertions(+)



Message-ID: <E1w34oe-000R9F-2l@gemulon.postgresql.org>
Permalink:  ../E1w34oe-000R9F-2l@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1w34oe-000R9F-2l@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: drowley@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Short-circuit row estimation in NOT IN containing NULL consts
  In-Reply-To: <E1w34oe-000R9F-2l@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