Re: [PG19] Wrong results from NOT NULL-based expression simplification - Mailing list pgsql-hackers

From Richard Guo
Subject Re: [PG19] Wrong results from NOT NULL-based expression simplification
Date
Msg-id CAMbWs48FGrad6OpYi-XX9OzEFFSMUDu37zkbySz=Ea938XHgZQ@mail.gmail.com
Whole thread
In response to Re: [PG19] Wrong results from NOT NULL-based expression simplification  (Fujii Masao <masao.fujii@gmail.com>)
Responses Re: [PG19] Wrong results from NOT NULL-based expression simplification
List pgsql-hackers
On Sun, Oct 11, 2026 at 12:37 AM Fujii Masao <masao.fujii@gmail.com> wrote:
> Codex reported that commit d8ce0050a6c causes the following INSERT to fail,
> and I confirmed it. Is this a bug?

Thanks for the report.  Yes, it is a bug.

A COALESCE's typmod is derived from its arguments, so dropping the
NULL constant changes it from -1 to that of varchar(20).  The index
expression is simplified twice (in the relcache, then in
infer_arbiter_indexes), while the ON CONFLICT expression is simplified
once, so only the latter gets the RelabelType added by d8ce0050a, and
the two no longer match.

The attached patch preserves the typmod for a partially reduced
COALESCE or CASE as well.

While at it, I noticed that an all-null COALESCE is reduced to a null
constant with typmod -1, which can cause the same failure.  That code
dates back to 2005, but before v19 it only affects the typmod the
expression exposes.  The patch fixes it too.

I plan to back-patch this to v19 only, after rc1.

- Richard

Attachment

pgsql-hackers by date:

Previous
From: Peter Geoghegan
Date:
Subject: Re: index prefetching
Next
From: Fujii Masao
Date:
Subject: Re: [PG19] Wrong results from NOT NULL-based expression simplification