pgsql: Teach expr_is_nonnullable() to handle more expression types

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Teach expr_is_nonnullable() to handle more expression types
Date: 2025-12-24 09:02:02
Message-ID: E1vYKkr-002PHF-1A@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Teach expr_is_nonnullable() to handle more expression types

Currently, the function expr_is_nonnullable() checks only Const and
Var expressions to determine if an expression is non-nullable. This
patch extends the detection logic to handle more expression types.

This can enable several downstream optimizations, such as reducing
NullTest quals to constant truth values (e.g., "COALESCE(var, 1) IS
NULL" becomes FALSE) and converting "COUNT(expr)" to the more
efficient "COUNT(*)" when the expression is proven non-nullable.

This breaks a test case in test_predtest.sql, since we now simplify
"ARRAY[] IS NULL" to constant FALSE, preventing it from weakly
refuting a strict ScalarArrayOpExpr ("x = any(ARRAY[])"). To ensure
the refutation logic is still exercised as intended, wrap the array
argument in opaque_array().

Author: Richard Guo <guofenglinux(at)gmail(dot)com>
Reviewed-by: Tender Wang <tndrwang(at)gmail(dot)com>
Reviewed-by: Dagfinn Ilmari Mannsåker <ilmari(at)ilmari(dot)org>
Reviewed-by: David Rowley <dgrowleyml(at)gmail(dot)com>
Reviewed-by: Matheus Alcantara <matheusssilv97(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAMbWs49UhPBjm+NRpxerjaeuFKyUZJ_AjM3NBcSYK2JgZ6VTEQ@mail.gmail.com

Branch
------
master

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

Modified Files
--------------
src/backend/optimizer/util/clauses.c | 123 ++++++++++++++++++++-
.../test_predtest/expected/test_predtest.out | 2 +-
.../modules/test_predtest/sql/test_predtest.sql | 2 +-
src/test/regress/expected/predicate.out | 117 +++++++++++++++++++-
src/test/regress/sql/predicate.sql | 50 ++++++++-
5 files changed, 284 insertions(+), 10 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Amit Kapila 2025-12-24 09:29:20 pgsql: Doc: Clarify publication privilege requirements.
Previous Message Michael Paquier 2025-12-24 08:10:42 pgsql: Improve comment in pgstatfuncs.c