Re: Some optimizations for COALESCE expressions during constant folding

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Richard Guo <guofenglinux(at)gmail(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Some optimizations for COALESCE expressions during constant folding
Date: 2025-11-26 22:55:52
Message-ID: CAApHDvrpSBSCWO+J=FS0fh-echr03ORHstRWZLOgAiQKSm5E5Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, 27 Nov 2025 at 00:55, Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
>
> On Tue, Nov 25, 2025 at 10:16 PM David Rowley <dgrowleyml(at)gmail(dot)com> wrote:
> > uhh, of course it is. That's what I did in [1] for Consts. Doing it
> > this way means we'll not need to modify the constant folding code (or
> > whichever other code wants to know when an Expr can't be NULL) every
> > time we think of something new that we can prove can't be NULL.
>
> OK. Here is an updated patch that does that. (There is some overlap
> in changes to expr_is_nonnullable with the patch you mentioned.)

I've pushed 42473b3b3 now. I think you should maybe do this as 2
commits. 0001 to make eval_const_expressions_mutator() use
expr_is_nonnullable() instead of var_is_nonnullable(). That'll not
really do anything aside from the additional Const support for
NULLability checks. Otherwise, it's nearly a refactor. 0002 is to add
the COALESCE code to expr_is_nonnullable(). That way you can sell this
one for a bit more than your initial use case, as it'll also then
handle converting things like COUNT(COALESCE(nullable, notnullable))
into COUNT(*). I think doing it this way means you don't need to argue
that optimising COALESCE(...) IS NOT NULL is worthwhile since you're
really just teaching expr_is_nonnullable() about COALESCE Nodes.

David

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2025-11-26 22:59:22 Re: Refactoring: Use soft error reporting for *_opt_overflow functions of date/timestamp
Previous Message Nathan Bossart 2025-11-26 22:49:49 show size of DSAs and dshash tables in pg_dsm_registry_allocations