pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Amit Langote <amitlan@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Use SPI for RI checks whose FK values need a cast function
Date: Thu, 15 Oct 2026 07:34:58 +0000
Message-ID: <E1xHFzO-00000001hjd-3I3q@gemulon.postgresql.org> (raw)

Use SPI for RI checks whose FK values need a cast function

When the FK column's type needs a cast function to reach the equality
operator's input type, the RI fast path calls that function directly.
The SPI path instead applies the cast as an expression in its query,
which takes care of the function's declared arguments, its input
collation, a NULL result, volatility, and replanning when the function
changes.  The direct call doesn't cover all of that; for example, a
NULL result raises an internal error rather than a foreign key
violation, and if the cast's function is dropped and replaced, later
checks fail with "cache lookup failed for function".  Coercions that
aren't a call to one function, array coercion and coercion via I/O,
fail outright with "no conversion function".

Rather than replicate expression evaluation, leave FK values that
aren't binary-coercible to the operator's input type to SPI.  The fast
path keeps handling cross-type operators, domains and binary-compatible
types.  As eligibility now depends on pg_cast, invalidate cached
constraint information on pg_cast changes too.

Because the check needs catalog lookups, it is made in
ri_check_fastpath_index(), which caches its result in fastpath_state
until the constraint's cached information is invalidated, rather than
for every row in ri_fastpath_is_applicable().

The two tests that used a cast function to exercise fast-path code that
still exists (metadata invalidation and the active snapshot) now use
the equality function of a custom opclass, the only user code a
fast-path check still runs.

The fast-path code that calls cast functions is now unused; the next
commit removes it.

Reported-by: Nikolay Samokhvalov <nik@postgres.ai>
Reported-by: Amit Langote <amitlangote09@gmail.com>
Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com>
Discussion: https://postgr.es/m/CAM527d9BgPjeOOYmbCBTd57R145qHCk-dzw9qNq+nOrDq1j__A@mail.gmail.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/6aecabcd5ed54ac055b4df031336e997c84f7450

Modified Files
--------------
src/backend/utils/adt/ri_triggers.c       |  70 +++++++--
src/test/regress/expected/foreign_key.out | 239 ++++++++++++++++++++----------
src/test/regress/sql/foreign_key.sql      | 228 +++++++++++++++++++---------
3 files changed, 372 insertions(+), 165 deletions(-)



view thread (2+ messages)

Message-ID: <E1xHFzO-00000001hjd-3I3q@gemulon.postgresql.org>
Permalink:  ../E1xHFzO-00000001hjd-3I3q@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xHFzO-00000001hjd-3I3q@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: amitlan@postgresql.org
  Subject: Re: pgsql: Use SPI for RI checks whose FK values need a cast function
  In-Reply-To: <E1xHFzO-00000001hjd-3I3q@gemulon.postgresql.org>
  References: <E1xHFzO-00000001hjd-3I3q@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