pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix ON CONFLICT inference for index expressions simplified to a
Date: Sat, 10 Oct 2026 00:23:29 +0000
Message-ID: <E1xFKs5-0000000116e-2JdO@gemulon.postgresql.org> (raw)

Fix ON CONFLICT inference for index expressions simplified to a Var

infer_arbiter_indexes() classifies each inference element by its
const-simplified form: a plain Var is matched against the index's
plain attributes, and anything else against its expressions.  Index
columns, however, were classified by indkey alone, so an expression
column stayed an expression even when const-simplification reduced it
to a plain Var.  Since commit 10c4fe074, COALESCE on a NOT NULL column
is reduced to the column itself, so an inference element such as
COALESCE(b, '') became a plain Var that no longer matched the unique
index on that same expression, and the statement failed with "there is
no unique or exclusion constraint matching the ON CONFLICT
specification".

To fix, classify index columns the same way as inference elements: an
index expression that is simplified to a plain Var is treated as a
plain attribute.  Also, teach infer_collation_opclass_match() to
compare such a column as an expression, so that an inference element
that specifies a collation or operator class can still match it.

As a consequence, an inference element naming the column directly now
matches such an index too, which is consistent with how an element
written as the expression already matches a plain index on the column.

Author: Shihao Zhong <zhong950419@gmail.com>
Reviewed-by: jian he <jian.universality@gmail.com>
Reviewed-by: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAGRkXqR7VXacmRP41UBaM5u4aseeCH5ECVmQgn_YS=L7=xdhTw@mail.gmail.com
Backpatch-through: 19

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/f9ccc5e4f92bc39f7c31905ff611785ff29ab9bb

Modified Files
--------------
src/backend/optimizer/util/plancat.c          | 64 +++++++++++++++++++--------
src/test/regress/expected/insert_conflict.out | 27 +++++++++++
src/test/regress/sql/insert_conflict.sql      | 13 ++++++
3 files changed, 85 insertions(+), 19 deletions(-)



view thread (2+ messages)  latest in thread

Message-ID: <E1xFKs5-0000000116e-2JdO@gemulon.postgresql.org>
Permalink:  ../E1xFKs5-0000000116e-2JdO@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xFKs5-0000000116e-2JdO@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: rguo@postgresql.org
  Subject: Re: pgsql: Fix ON CONFLICT inference for index expressions simplified to a
  In-Reply-To: <E1xFKs5-0000000116e-2JdO@gemulon.postgresql.org>
  References: <E1xFKs5-0000000116e-2JdO@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