Some optimizations for COALESCE expressions during constant folding

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Some optimizations for COALESCE expressions during constant folding
Date: 2025-11-25 10:50:51
Message-ID: CAMbWs49UhPBjm+NRpxerjaeuFKyUZJ_AjM3NBcSYK2JgZ6VTEQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Currently, we perform some simplification for Const arguments of a
COALESCE expression. For instance, if the first argument is a
non-null constant, we use that constant as the result for the entire
expression. If a subsequent argument is a non-null constant, all
following arguments are dropped since they will never be reached.

We can extend this simplification to Var arguments since the NOT NULL
attribute information is now available during constant folding. 0001
implements this.

Another optimization that can be done for a COALESCE expression is
when it is used in a NullTest. We can determine that a COALESCE
expression is non-nullable by checking if at least one of its
arguments is proven non-null. This information can then be used to
reduce the NullTest qual to a constant true or false. 0002 implements
this. (I'm wondering whether it'd be better to consolidate the
non-null check for Const, Var, and CoalesceExpr into one helper
function to simplify the code in eval_const_expressions.)

- Richard

Attachment Content-Type Size
v1-0001-Simplify-COALESCE-arguments-using-NOT-NULL-constr.patch application/octet-stream 7.2 KB
v1-0002-Reduce-COALESCE-IS-NOT-NULL-quals-during-constant.patch application/octet-stream 4.8 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2025-11-25 10:51:55 Re: BUG #19095: Test if function exit() is used fail when linked static
Previous Message Fujii Masao 2025-11-25 10:42:23 Re: CREATE/ALTER PUBLICATION improvements for syntax synopsis