pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Simplify COALESCE() with one surviving argument.
Date: Thu, 03 Jul 2025 21:40:26 +0000
Message-ID: <E1uXRfO-004Zru-1i@gemulon.postgresql.org> (raw)

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.milyutin@tantorlabs.ru>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.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(-)



Message-ID: <E1uXRfO-004Zru-1i@gemulon.postgresql.org>
Permalink:  ../E1uXRfO-004Zru-1i@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1uXRfO-004Zru-1i@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: tgl@sss.pgh.pa.us, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Simplify COALESCE() with one surviving argument.
  In-Reply-To: <E1uXRfO-004Zru-1i@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