pgsql: Simplify COALESCE() with one surviving argument.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Simplify COALESCE() with one surviving argument.
Date: 2025-07-03 21:40:26
Message-ID: E1uXRfO-004Zru-1i@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Simplify COALESCE() with one surviving argument.

If, after removal of useless null-constant arguments, a CoalesceExpr
has exactly one remaining argument, we can just take that argument as
the result, without bothering to wrap a new CoalesceExpr around it.
This isn't likely to produce any great improvement in runtime per se,
but it can lead to better plans since the planner no longer has to
treat the expression as non-strict.

However, there were a few regression test cases that intentionally
wrote COALESCE(x) as a shorthand way of creating a non-strict
subexpression. To avoid ruining the intent of those tests, write
COALESCE(x,x) instead. (If anyone ever proposes de-duplicating
COALESCE arguments, we'll need another iteration of this arms race.
But it seems pretty unlikely that such an optimization would be
worthwhile.)

Author: Maksim Milyutin <maksim(dot)milyutin(at)tantorlabs(dot)ru>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/8e8573c3-1411-448d-877e-53258b7b2be0@tantorlabs.ru

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/931766aaec58b2ce09c82203456877e0b05e1271

Modified Files
--------------
src/backend/optimizer/util/clauses.c | 7 +++++
src/test/regress/expected/join.out | 32 +++++++++++------------
src/test/regress/expected/subselect.out | 46 ++++++++++++++++-----------------
src/test/regress/sql/join.sql | 10 +++----
src/test/regress/sql/subselect.sql | 8 +++---
5 files changed, 55 insertions(+), 48 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2025-07-04 00:05:54 pgsql: Speed up truncation of temporary relations.
Previous Message Tom Lane 2025-07-03 20:30:46 pgsql: Add more cross-type comparisons to contrib/btree_gin.