pgsql: Fall back to SPI for RI checks with mismatched index collations

From: Amit Langote <amitlan(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fall back to SPI for RI checks with mismatched index collations
Date: 2026-09-11 08:51:25
Message-ID: E1x4wyh-00000004PYd-492D@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fall back to SPI for RI checks with mismatched index collations

The fast path probes using the referenced index's collation. When that
collation differs from the referenced column's, it can reject valid
references or accept invalid ones. Changing the scan key's collation
is not sufficient, since btree navigation must use the ordering under
which the index was built.

Cache index eligibility lazily after locking the referenced table,
reloading the constraint information, and opening the index. Compare
index and column collations using the held relation descriptors, and
fall back to SPI on a mismatch. Also move the btree eligibility check
here, avoiding a lookup of an index that REINDEX CONCURRENTLY could
drop before the referenced-table lock is acquired.

Cache both acceptance and rejection until the constraint information
is reloaded, so subsequent rows need not repeat the comparisons. Check
before probing or buffering any row. If batch entry initialization
rejects the index, remove that entry while preserving any other pending
checks.

Add ICU regression coverage for per-row validation, ordinary inserts,
reordered index columns, both directions of collation mismatch, and
fallback while another foreign key check is pending.

Reported-by: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAA4eK1Lk1DfgiFPL-HccZm-Nm+exg+FcsODH8KPw6BOBuzfHtA@mail.gmail.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/0b0b413292161cf709b9dbc4d5adee23be53ee34

Modified Files
--------------
src/backend/utils/adt/ri_triggers.c | 157 ++++++++++++++++++++-----
src/test/regress/expected/collate.icu.utf8.out | 64 ++++++++++
src/test/regress/sql/collate.icu.utf8.sql | 60 ++++++++++
src/tools/pgindent/typedefs.list | 1 +
4 files changed, 254 insertions(+), 28 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2026-09-11 09:16:50 pgsql: psql: Schema-qualify catalogs in ALTER DATABASE RESET completion
Previous Message Amit Langote 2026-09-11 08:51:11 pgsql: Fall back to SPI for RI checks with mismatched index collations