Re: [PG19] Wrong results from NOT NULL-based expression simplification

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
Cc: Melanie Plageman <melanieplageman(at)gmail(dot)com>, shihao zhong <zhong950419(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>, Peter Eisentraut <peter(at)eisentraut(dot)org>
Subject: Re: [PG19] Wrong results from NOT NULL-based expression simplification
Date: 2026-10-11 00:35:55
Message-ID: CAMbWs48FGrad6OpYi-XX9OzEFFSMUDu37zkbySz=Ea938XHgZQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, Oct 11, 2026 at 12:37 AM Fujii Masao <masao(dot)fujii(at)gmail(dot)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 Content-Type Size
v1-0001-Preserve-typmod-when-partially-reducing-CASE-and-.patch application/octet-stream 7.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2026-10-11 01:50:05 Re: [PG19] Wrong results from NOT NULL-based expression simplification
Previous Message Peter Geoghegan 2026-10-11 00:11:35 Re: index prefetching