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
PostgreSQL Database Management System
This directory contains the source code distribution of the PostgreSQL database management system.
PostgreSQL is an advanced object-relational database management system that supports an extended subset of the SQL standard, including transactions, foreign keys, subqueries, triggers, user-defined types and functions. This distribution also contains C language bindings.
Copyright and license information can be found in the file COPYRIGHT.
General documentation about this version of PostgreSQL can be found at https://www.postgresql.org/docs/devel/. In particular, information about building PostgreSQL from the source code can be found at https://www.postgresql.org/docs/devel/installation.html.
The latest version of this software, and related software, may be obtained at https://www.postgresql.org/download/. For more information look at our web site located at https://www.postgresql.org/.