=== Applying patches on top of PostgreSQL commit ID 388f2db2994e216f15f004c4addbe4851871f954 === /etc/rc.d/jail: WARNING: Per-jail configuration via jail_* variables is obsolete. Please consider migrating to /etc/jail.conf. Thu Nov 26 12:52:34 UTC 2026 On branch cf/7716 nothing to commit, working tree clean === using 'git am' to apply patch ./REL_18-v1-0001-Support-I-O-coercion-when-comparing-foreign-key-v.patch === Applying: Support I/O coercion when comparing foreign key values Using index info to reconstruct a base tree... M src/backend/utils/adt/ri_triggers.c M src/test/regress/expected/foreign_key.out M src/test/regress/sql/foreign_key.sql Falling back to patching base and 3-way merge... Auto-merging src/test/regress/sql/foreign_key.sql CONFLICT (content): Merge conflict in src/test/regress/sql/foreign_key.sql Auto-merging src/test/regress/expected/foreign_key.out CONFLICT (content): Merge conflict in src/test/regress/expected/foreign_key.out Auto-merging src/backend/utils/adt/ri_triggers.c error: Failed to merge in the changes. hint: Use 'git am --show-current-patch=diff' to see the failed patch Patch failed at 0001 Support I/O coercion when comparing foreign key values When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort". === using patch(1) to apply patch ./REL_18-v1-0001-Support-I-O-coercion-when-comparing-foreign-key-v.patch === patching file src/backend/utils/adt/ri_triggers.c Hunk #1 succeeded at 213 (offset 38 lines). Hunk #2 succeeded at 3995 (offset 759 lines). Hunk #3 succeeded at 4103 (offset 759 lines). Hunk #4 succeeded at 4116 (offset 759 lines). patching file src/test/regress/expected/foreign_key.out Hunk #1 FAILED at 3692. 1 out of 1 hunk FAILED -- saving rejects to file src/test/regress/expected/foreign_key.out.rej patching file src/test/regress/sql/foreign_key.sql Hunk #1 FAILED at 2623. 1 out of 1 hunk FAILED -- saving rejects to file src/test/regress/sql/foreign_key.sql.rej Unstaged changes after reset: M src/backend/utils/adt/ri_triggers.c Removing src/test/regress/expected/foreign_key.out.rej Removing src/test/regress/sql/foreign_key.sql.rej === using 'git apply' to apply patch ./REL_18-v1-0001-Support-I-O-coercion-when-comparing-foreign-key-v.patch === Applied patch to 'src/backend/utils/adt/ri_triggers.c' cleanly. Applied patch to 'src/test/regress/expected/foreign_key.out' with conflicts. Applied patch to 'src/test/regress/sql/foreign_key.sql' with conflicts. U src/test/regress/expected/foreign_key.out U src/test/regress/sql/foreign_key.sql diff --cc src/test/regress/expected/foreign_key.out index 95a0531f3a0,ae284c039f6..00000000000 --- a/src/test/regress/expected/foreign_key.out +++ b/src/test/regress/expected/foreign_key.out @@@ -4228,222 -3692,39 +4228,261 @@@ DROP CAST (fk_err_vch AS int) DROP FUNCTION fk_err_vcast(fk_err_vch); DROP TYPE fk_err_vch; DROP ROLE regress_fk_err_role; ++<<<<<<< ours +-- +-- Cache invalidation arriving during a fast-path check. +-- +-- The equality function of the referenced index's opclass runs while the +-- fast path scans the index. One that performs DDL raises an invalidation +-- there, detaching the constraint's fast-path metadata while the check is +-- still using it. This is done in a transaction that is rolled back, so that +-- other tests never see the operator class. +-- +BEGIN; +-- Renames the constraint the first time it is called, and so raises an +-- invalidation partway through the check. Guarded on the catalog so the +-- second and later calls are no-ops. +CREATE FUNCTION fp_inval_eq(int4, int4) RETURNS bool AS $$ +BEGIN + IF EXISTS (SELECT 1 FROM pg_constraint + WHERE conrelid = 'fktable_inval'::regclass + AND conname = 'fktable_inval_fk') THEN + EXECUTE 'ALTER TABLE fktable_inval' + ' RENAME CONSTRAINT fktable_inval_fk TO fktable_inval_fk2'; + END IF; + RETURN $1 OPERATOR(pg_catalog.=) $2; +END $$ LANGUAGE plpgsql; +CREATE OPERATOR =@= (LEFTARG = int4, RIGHTARG = int4, FUNCTION = fp_inval_eq); +CREATE OPERATOR CLASS fp_inval_ops FOR TYPE int4 USING btree AS + OPERATOR 1 pg_catalog.<, OPERATOR 2 pg_catalog.<=, OPERATOR 3 =@=, + OPERATOR 4 pg_catalog.>=, OPERATOR 5 pg_catalog.>, + FUNCTION 1 btint4cmp(int4, int4); +CREATE TABLE pktable_inval (a int4, b int4); +CREATE UNIQUE INDEX pktable_inval_a_b ON pktable_inval (a fp_inval_ops, b); +INSERT INTO pktable_inval VALUES (1, 1), (2, 2); +CREATE TABLE fktable_inval (a int4, b int4, + CONSTRAINT fktable_inval_fk FOREIGN KEY (a, b) + REFERENCES pktable_inval (a, b)); +-- The first row invalidates the metadata; the second check must repopulate +-- and use it safely. +INSERT INTO fktable_inval VALUES (1, 1), (2, 2); +-- Confirms the equality function actually ran and raised the invalidation. +-- Without this the insert above could pass merely by not exercising the path +-- at all. +SELECT conname FROM pg_constraint + WHERE conrelid = 'fktable_inval'::regclass AND contype = 'f'; + conname +------------------- + fktable_inval_fk2 +(1 row) + +SELECT count(*) FROM fktable_inval; + count +------- + 2 +(1 row) + +ROLLBACK; +-- An FK check locks the referenced row, which a read-only transaction does +-- not allow unless the referenced table is temporary. +CREATE TABLE fp_pk_ro (a int PRIMARY KEY); +INSERT INTO fp_pk_ro VALUES (1); +CREATE TABLE fp_fk_ro (a int REFERENCES fp_pk_ro DEFERRABLE INITIALLY DEFERRED); +BEGIN; +INSERT INTO fp_fk_ro VALUES (1); +SET TRANSACTION READ ONLY; +COMMIT; -- fails +ERROR: cannot execute SELECT FOR KEY SHARE in a read-only transaction +CREATE TEMP TABLE fp_pk_ro_tmp (a int PRIMARY KEY); +INSERT INTO fp_pk_ro_tmp VALUES (1); +CREATE TEMP TABLE fp_fk_ro_tmp (a int REFERENCES fp_pk_ro_tmp + DEFERRABLE INITIALLY DEFERRED); +BEGIN; +INSERT INTO fp_fk_ro_tmp VALUES (1); +SET TRANSACTION READ ONLY; +COMMIT; -- succeeds +SELECT count(*) FROM fp_fk_ro; + count +------- + 0 +(1 row) + +SELECT count(*) FROM fp_fk_ro_tmp; + count +------- + 1 +(1 row) + +DROP TABLE fp_fk_ro, fp_pk_ro, fp_fk_ro_tmp, fp_pk_ro_tmp; +-- The row lock must use the command ID the check's snapshot was taken +-- with. An equality function that updates the referenced row advances the +-- command counter before the lock; the row must then count as updated by +-- the check's own command, so that the check reports a violation, as the +-- SPI path does, rather than failing to lock an "invisible" tuple. The +-- equality function runs while the scan holds the index page locked; its +-- UPDATE is a HOT update, since v isn't indexed, so it doesn't touch the +-- index. This is done in a transaction that is rolled back, so that other +-- tests never see the operator class. +BEGIN; +CREATE TABLE fp_pk_cid (id int, v int DEFAULT 0); +CREATE FUNCTION fp_cid_eq(int, int) RETURNS bool LANGUAGE plpgsql AS $$ +BEGIN + UPDATE fp_pk_cid SET v = v + 1 WHERE id OPERATOR(pg_catalog.=) $1; + RETURN $1 OPERATOR(pg_catalog.=) $2; +END$$; +CREATE OPERATOR =~= (LEFTARG = int, RIGHTARG = int, FUNCTION = fp_cid_eq); +CREATE OPERATOR CLASS fp_cid_ops FOR TYPE int USING btree AS + OPERATOR 1 pg_catalog.<, OPERATOR 2 pg_catalog.<=, OPERATOR 3 =~=, + OPERATOR 4 pg_catalog.>=, OPERATOR 5 pg_catalog.>, + FUNCTION 1 btint4cmp(int, int); +CREATE UNIQUE INDEX fp_pk_cid_id ON fp_pk_cid (id fp_cid_ops); +INSERT INTO fp_pk_cid VALUES (1); +CREATE TABLE fp_fk_cid (a int REFERENCES fp_pk_cid (id)); +SAVEPOINT fp_cid; +INSERT INTO fp_fk_cid VALUES (1); -- fails, as a violation +ERROR: insert or update on table "fp_fk_cid" violates foreign key constraint "fp_fk_cid_a_fkey" +DETAIL: Key (a)=(1) is not present in table "fp_pk_cid". +ROLLBACK TO SAVEPOINT fp_cid; +ROLLBACK; +-- +-- FK values that need a coercion to reach the equality operator's input type +-- are checked through SPI, which applies the coercion as part of the query. +-- The fast path used to call the cast function directly. +-- +BEGIN; +CREATE SCHEMA ri_cast; +SET LOCAL search_path = ri_cast, pg_catalog; +-- A cast that returns NULL for a non-NULL key: an FK violation, as for any +-- other key without a match, not an internal error. +CREATE TYPE ri_word AS ENUM ('one', 'none'); +CREATE FUNCTION ri_word_to_int4(ri_word) RETURNS int4 + LANGUAGE sql IMMUTABLE AS $$ SELECT CASE WHEN $1::text = 'one' THEN 1 END $$; +CREATE CAST (ri_word AS int4) WITH FUNCTION ri_word_to_int4(ri_word) AS IMPLICIT; +CREATE TABLE ri_pk (id int4 PRIMARY KEY); +INSERT INTO ri_pk VALUES (1); +CREATE TABLE ri_fk (a ri_word REFERENCES ri_pk); +INSERT INTO ri_fk VALUES ('one'); +SAVEPOINT s; +INSERT INTO ri_fk VALUES ('none'); -- fails +ERROR: insert or update on table "ri_fk" violates foreign key constraint "ri_fk_a_fkey" +DETAIL: Key (a)=(none) is not present in table "ri_pk". +ROLLBACK TO SAVEPOINT s; +-- Replacing the cast and dropping its old function must not leave a later +-- check in this session calling the dropped function. +CREATE FUNCTION ri_word_to_int4_v2(ri_word) RETURNS int4 + LANGUAGE sql IMMUTABLE AS $$ SELECT 1 $$; +DROP CAST (ri_word AS int4); +CREATE CAST (ri_word AS int4) WITH FUNCTION ri_word_to_int4_v2(ri_word) AS IMPLICIT; +DROP FUNCTION ri_word_to_int4(ri_word); +INSERT INTO ri_fk VALUES ('none'); -- succeeds through the new cast +-- A collation-sensitive cast from a collatable type gets the input +-- collation an expression would give it. +CREATE TYPE ri_ctext; +CREATE FUNCTION ri_ctext_in(cstring) RETURNS ri_ctext + AS 'textin' LANGUAGE internal IMMUTABLE STRICT; +NOTICE: return type ri_ctext is only a shell +CREATE FUNCTION ri_ctext_out(ri_ctext) RETURNS cstring + AS 'textout' LANGUAGE internal IMMUTABLE STRICT; +NOTICE: argument type ri_ctext is only a shell +LINE 1: CREATE FUNCTION ri_ctext_out(ri_ctext) RETURNS cstring + ^ +CREATE TYPE ri_ctext (INPUT = ri_ctext_in, OUTPUT = ri_ctext_out, + LIKE = text, COLLATABLE = true); +CREATE FUNCTION ri_ctext_lower(ri_ctext) RETURNS text + AS 'lower' LANGUAGE internal IMMUTABLE STRICT; +CREATE CAST (ri_ctext AS text) WITH FUNCTION ri_ctext_lower(ri_ctext) AS IMPLICIT; +CREATE TABLE ri_pk_text (k text PRIMARY KEY); +INSERT INTO ri_pk_text VALUES ('abc'); +CREATE TABLE ri_fk_text (a ri_ctext REFERENCES ri_pk_text); +INSERT INTO ri_fk_text VALUES ('ABC'); -- succeeds; the cast lowercases +-- An array coercion (int2[] to int4[], allowed with an opclass declared for +-- int4[]) has no single cast function to call. +CREATE FUNCTION ri_i4a_lt(int4[], int4[]) RETURNS bool + AS 'array_lt' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_i4a_le(int4[], int4[]) RETURNS bool + AS 'array_le' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_i4a_eq(int4[], int4[]) RETURNS bool + AS 'array_eq' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_i4a_ge(int4[], int4[]) RETURNS bool + AS 'array_ge' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_i4a_gt(int4[], int4[]) RETURNS bool + AS 'array_gt' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_i4a_cmp(int4[], int4[]) RETURNS int4 + AS 'btarraycmp' LANGUAGE internal IMMUTABLE STRICT; +CREATE OPERATOR ri_cast.<<< (LEFTARG = int4[], RIGHTARG = int4[], FUNCTION = ri_i4a_lt); +CREATE OPERATOR ri_cast.<<= (LEFTARG = int4[], RIGHTARG = int4[], FUNCTION = ri_i4a_le); +CREATE OPERATOR ri_cast.=== (LEFTARG = int4[], RIGHTARG = int4[], FUNCTION = ri_i4a_eq); +CREATE OPERATOR ri_cast.>>= (LEFTARG = int4[], RIGHTARG = int4[], FUNCTION = ri_i4a_ge); +CREATE OPERATOR ri_cast.>>> (LEFTARG = int4[], RIGHTARG = int4[], FUNCTION = ri_i4a_gt); +CREATE OPERATOR CLASS ri_i4a_ops FOR TYPE int4[] USING btree AS + OPERATOR 1 ri_cast.<<<, OPERATOR 2 ri_cast.<<=, OPERATOR 3 ri_cast.===, + OPERATOR 4 ri_cast.>>=, OPERATOR 5 ri_cast.>>>, + FUNCTION 1 ri_i4a_cmp(int4[], int4[]); +CREATE TABLE ri_pk_arr (k int4[]); +CREATE UNIQUE INDEX ON ri_pk_arr (k ri_i4a_ops); +INSERT INTO ri_pk_arr VALUES ('{1,2}'); +CREATE TABLE ri_fk_arr (a int2[] REFERENCES ri_pk_arr (k)); +INSERT INTO ri_fk_arr VALUES ('{1,2}'); -- succeeds +SAVEPOINT s; +INSERT INTO ri_fk_arr VALUES ('{3}'); -- fails +ERROR: insert or update on table "ri_fk_arr" violates foreign key constraint "ri_fk_arr_a_fkey" +DETAIL: Key (a)=({3}) is not present in table "ri_pk_arr". +ROLLBACK TO SAVEPOINT s; +-- A coercion via I/O has no cast function to call either. +CREATE TYPE ri_ioint; +CREATE FUNCTION ri_ioint_in(cstring) RETURNS ri_ioint + AS 'int4in' LANGUAGE internal IMMUTABLE STRICT; +NOTICE: return type ri_ioint is only a shell +CREATE FUNCTION ri_ioint_out(ri_ioint) RETURNS cstring + AS 'int4out' LANGUAGE internal IMMUTABLE STRICT; +NOTICE: argument type ri_ioint is only a shell +LINE 1: CREATE FUNCTION ri_ioint_out(ri_ioint) RETURNS cstring + ^ +CREATE TYPE ri_ioint (INPUT = ri_ioint_in, OUTPUT = ri_ioint_out, LIKE = int4); +CREATE CAST (ri_ioint AS int4) WITH INOUT AS IMPLICIT; +CREATE TABLE ri_fk_io (a ri_ioint REFERENCES ri_pk); +INSERT INTO ri_fk_io VALUES ('1'); -- succeeds +SAVEPOINT s; +INSERT INTO ri_fk_io VALUES ('2'); -- fails +ERROR: insert or update on table "ri_fk_io" violates foreign key constraint "ri_fk_io_a_fkey" +DETAIL: Key (a)=(2) is not present in table "ri_pk". +ROLLBACK TO SAVEPOINT s; +ROLLBACK; ++======= + -- An UPDATE of an FK column whose type reaches the PK type only through an + -- implicit I/O cast must compare the old and new values through I/O, too. + -- The FK row must not be inserted in the same transaction as the UPDATE, + -- or the comparison is skipped. + CREATE TYPE ri_ioint2; + CREATE FUNCTION ri_ioint2_in(cstring) RETURNS ri_ioint2 + AS 'int4in' LANGUAGE internal IMMUTABLE STRICT; + NOTICE: return type ri_ioint2 is only a shell + CREATE FUNCTION ri_ioint2_out(ri_ioint2) RETURNS cstring + AS 'int4out' LANGUAGE internal IMMUTABLE STRICT; + NOTICE: argument type ri_ioint2 is only a shell + LINE 1: CREATE FUNCTION ri_ioint2_out(ri_ioint2) RETURNS cstring + ^ + CREATE TYPE ri_ioint2 (INPUT = ri_ioint2_in, OUTPUT = ri_ioint2_out, LIKE = int4); + CREATE CAST (ri_ioint2 AS int4) WITH INOUT AS IMPLICIT; + CREATE TABLE ri_pk_io2 (id int4 PRIMARY KEY); + CREATE TABLE ri_fk_io2 (a ri_ioint2 REFERENCES ri_pk_io2, b int); + INSERT INTO ri_pk_io2 VALUES (1), (2); + INSERT INTO ri_fk_io2 VALUES ('1', 10); + UPDATE ri_fk_io2 SET a = a, b = 11; -- succeeds + UPDATE ri_fk_io2 SET a = '2'; -- succeeds + UPDATE ri_fk_io2 SET a = '3'; -- fails + ERROR: insert or update on table "ri_fk_io2" violates foreign key constraint "ri_fk_io2_a_fkey" + DETAIL: Key (a)=(3) is not present in table "ri_pk_io2". + SELECT a::text, b FROM ri_fk_io2; + a | b + ---+---- + 2 | 11 + (1 row) + + DROP TABLE ri_fk_io2, ri_pk_io2; + DROP TYPE ri_ioint2 CASCADE; + NOTICE: drop cascades to 3 other objects + DETAIL: drop cascades to function ri_ioint2_in(cstring) + drop cascades to function ri_ioint2_out(ri_ioint2) + drop cascades to cast from ri_ioint2 to integer ++>>>>>>> theirs diff --cc src/test/regress/sql/foreign_key.sql index 65b68f46325,bc8e57594b4..00000000000 --- a/src/test/regress/sql/foreign_key.sql +++ b/src/test/regress/sql/foreign_key.sql @@@ -3151,197 -2624,24 +3151,221 @@@ DROP FUNCTION fk_err_vcast(fk_err_vch) DROP TYPE fk_err_vch; DROP ROLE regress_fk_err_role; ++<<<<<<< ours +-- +-- Cache invalidation arriving during a fast-path check. +-- +-- The equality function of the referenced index's opclass runs while the +-- fast path scans the index. One that performs DDL raises an invalidation +-- there, detaching the constraint's fast-path metadata while the check is +-- still using it. This is done in a transaction that is rolled back, so that +-- other tests never see the operator class. +-- +BEGIN; +-- Renames the constraint the first time it is called, and so raises an +-- invalidation partway through the check. Guarded on the catalog so the +-- second and later calls are no-ops. +CREATE FUNCTION fp_inval_eq(int4, int4) RETURNS bool AS $$ +BEGIN + IF EXISTS (SELECT 1 FROM pg_constraint + WHERE conrelid = 'fktable_inval'::regclass + AND conname = 'fktable_inval_fk') THEN + EXECUTE 'ALTER TABLE fktable_inval' + ' RENAME CONSTRAINT fktable_inval_fk TO fktable_inval_fk2'; + END IF; + RETURN $1 OPERATOR(pg_catalog.=) $2; +END $$ LANGUAGE plpgsql; +CREATE OPERATOR =@= (LEFTARG = int4, RIGHTARG = int4, FUNCTION = fp_inval_eq); +CREATE OPERATOR CLASS fp_inval_ops FOR TYPE int4 USING btree AS + OPERATOR 1 pg_catalog.<, OPERATOR 2 pg_catalog.<=, OPERATOR 3 =@=, + OPERATOR 4 pg_catalog.>=, OPERATOR 5 pg_catalog.>, + FUNCTION 1 btint4cmp(int4, int4); + +CREATE TABLE pktable_inval (a int4, b int4); +CREATE UNIQUE INDEX pktable_inval_a_b ON pktable_inval (a fp_inval_ops, b); +INSERT INTO pktable_inval VALUES (1, 1), (2, 2); +CREATE TABLE fktable_inval (a int4, b int4, + CONSTRAINT fktable_inval_fk FOREIGN KEY (a, b) + REFERENCES pktable_inval (a, b)); + +-- The first row invalidates the metadata; the second check must repopulate +-- and use it safely. +INSERT INTO fktable_inval VALUES (1, 1), (2, 2); + +-- Confirms the equality function actually ran and raised the invalidation. +-- Without this the insert above could pass merely by not exercising the path +-- at all. +SELECT conname FROM pg_constraint + WHERE conrelid = 'fktable_inval'::regclass AND contype = 'f'; + +SELECT count(*) FROM fktable_inval; +ROLLBACK; + +-- An FK check locks the referenced row, which a read-only transaction does +-- not allow unless the referenced table is temporary. +CREATE TABLE fp_pk_ro (a int PRIMARY KEY); +INSERT INTO fp_pk_ro VALUES (1); +CREATE TABLE fp_fk_ro (a int REFERENCES fp_pk_ro DEFERRABLE INITIALLY DEFERRED); +BEGIN; +INSERT INTO fp_fk_ro VALUES (1); +SET TRANSACTION READ ONLY; +COMMIT; -- fails +CREATE TEMP TABLE fp_pk_ro_tmp (a int PRIMARY KEY); +INSERT INTO fp_pk_ro_tmp VALUES (1); +CREATE TEMP TABLE fp_fk_ro_tmp (a int REFERENCES fp_pk_ro_tmp + DEFERRABLE INITIALLY DEFERRED); +BEGIN; +INSERT INTO fp_fk_ro_tmp VALUES (1); +SET TRANSACTION READ ONLY; +COMMIT; -- succeeds +SELECT count(*) FROM fp_fk_ro; +SELECT count(*) FROM fp_fk_ro_tmp; +DROP TABLE fp_fk_ro, fp_pk_ro, fp_fk_ro_tmp, fp_pk_ro_tmp; + +-- The row lock must use the command ID the check's snapshot was taken +-- with. An equality function that updates the referenced row advances the +-- command counter before the lock; the row must then count as updated by +-- the check's own command, so that the check reports a violation, as the +-- SPI path does, rather than failing to lock an "invisible" tuple. The +-- equality function runs while the scan holds the index page locked; its +-- UPDATE is a HOT update, since v isn't indexed, so it doesn't touch the +-- index. This is done in a transaction that is rolled back, so that other +-- tests never see the operator class. +BEGIN; +CREATE TABLE fp_pk_cid (id int, v int DEFAULT 0); +CREATE FUNCTION fp_cid_eq(int, int) RETURNS bool LANGUAGE plpgsql AS $$ +BEGIN + UPDATE fp_pk_cid SET v = v + 1 WHERE id OPERATOR(pg_catalog.=) $1; + RETURN $1 OPERATOR(pg_catalog.=) $2; +END$$; +CREATE OPERATOR =~= (LEFTARG = int, RIGHTARG = int, FUNCTION = fp_cid_eq); +CREATE OPERATOR CLASS fp_cid_ops FOR TYPE int USING btree AS + OPERATOR 1 pg_catalog.<, OPERATOR 2 pg_catalog.<=, OPERATOR 3 =~=, + OPERATOR 4 pg_catalog.>=, OPERATOR 5 pg_catalog.>, + FUNCTION 1 btint4cmp(int, int); +CREATE UNIQUE INDEX fp_pk_cid_id ON fp_pk_cid (id fp_cid_ops); +INSERT INTO fp_pk_cid VALUES (1); +CREATE TABLE fp_fk_cid (a int REFERENCES fp_pk_cid (id)); +SAVEPOINT fp_cid; +INSERT INTO fp_fk_cid VALUES (1); -- fails, as a violation +ROLLBACK TO SAVEPOINT fp_cid; +ROLLBACK; + +-- +-- FK values that need a coercion to reach the equality operator's input type +-- are checked through SPI, which applies the coercion as part of the query. +-- The fast path used to call the cast function directly. +-- +BEGIN; +CREATE SCHEMA ri_cast; +SET LOCAL search_path = ri_cast, pg_catalog; + +-- A cast that returns NULL for a non-NULL key: an FK violation, as for any +-- other key without a match, not an internal error. +CREATE TYPE ri_word AS ENUM ('one', 'none'); +CREATE FUNCTION ri_word_to_int4(ri_word) RETURNS int4 + LANGUAGE sql IMMUTABLE AS $$ SELECT CASE WHEN $1::text = 'one' THEN 1 END $$; +CREATE CAST (ri_word AS int4) WITH FUNCTION ri_word_to_int4(ri_word) AS IMPLICIT; +CREATE TABLE ri_pk (id int4 PRIMARY KEY); +INSERT INTO ri_pk VALUES (1); +CREATE TABLE ri_fk (a ri_word REFERENCES ri_pk); +INSERT INTO ri_fk VALUES ('one'); +SAVEPOINT s; +INSERT INTO ri_fk VALUES ('none'); -- fails +ROLLBACK TO SAVEPOINT s; + +-- Replacing the cast and dropping its old function must not leave a later +-- check in this session calling the dropped function. +CREATE FUNCTION ri_word_to_int4_v2(ri_word) RETURNS int4 + LANGUAGE sql IMMUTABLE AS $$ SELECT 1 $$; +DROP CAST (ri_word AS int4); +CREATE CAST (ri_word AS int4) WITH FUNCTION ri_word_to_int4_v2(ri_word) AS IMPLICIT; +DROP FUNCTION ri_word_to_int4(ri_word); +INSERT INTO ri_fk VALUES ('none'); -- succeeds through the new cast + +-- A collation-sensitive cast from a collatable type gets the input +-- collation an expression would give it. +CREATE TYPE ri_ctext; +CREATE FUNCTION ri_ctext_in(cstring) RETURNS ri_ctext + AS 'textin' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_ctext_out(ri_ctext) RETURNS cstring + AS 'textout' LANGUAGE internal IMMUTABLE STRICT; +CREATE TYPE ri_ctext (INPUT = ri_ctext_in, OUTPUT = ri_ctext_out, + LIKE = text, COLLATABLE = true); +CREATE FUNCTION ri_ctext_lower(ri_ctext) RETURNS text + AS 'lower' LANGUAGE internal IMMUTABLE STRICT; +CREATE CAST (ri_ctext AS text) WITH FUNCTION ri_ctext_lower(ri_ctext) AS IMPLICIT; +CREATE TABLE ri_pk_text (k text PRIMARY KEY); +INSERT INTO ri_pk_text VALUES ('abc'); +CREATE TABLE ri_fk_text (a ri_ctext REFERENCES ri_pk_text); +INSERT INTO ri_fk_text VALUES ('ABC'); -- succeeds; the cast lowercases + +-- An array coercion (int2[] to int4[], allowed with an opclass declared for +-- int4[]) has no single cast function to call. +CREATE FUNCTION ri_i4a_lt(int4[], int4[]) RETURNS bool + AS 'array_lt' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_i4a_le(int4[], int4[]) RETURNS bool + AS 'array_le' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_i4a_eq(int4[], int4[]) RETURNS bool + AS 'array_eq' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_i4a_ge(int4[], int4[]) RETURNS bool + AS 'array_ge' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_i4a_gt(int4[], int4[]) RETURNS bool + AS 'array_gt' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_i4a_cmp(int4[], int4[]) RETURNS int4 + AS 'btarraycmp' LANGUAGE internal IMMUTABLE STRICT; +CREATE OPERATOR ri_cast.<<< (LEFTARG = int4[], RIGHTARG = int4[], FUNCTION = ri_i4a_lt); +CREATE OPERATOR ri_cast.<<= (LEFTARG = int4[], RIGHTARG = int4[], FUNCTION = ri_i4a_le); +CREATE OPERATOR ri_cast.=== (LEFTARG = int4[], RIGHTARG = int4[], FUNCTION = ri_i4a_eq); +CREATE OPERATOR ri_cast.>>= (LEFTARG = int4[], RIGHTARG = int4[], FUNCTION = ri_i4a_ge); +CREATE OPERATOR ri_cast.>>> (LEFTARG = int4[], RIGHTARG = int4[], FUNCTION = ri_i4a_gt); +CREATE OPERATOR CLASS ri_i4a_ops FOR TYPE int4[] USING btree AS + OPERATOR 1 ri_cast.<<<, OPERATOR 2 ri_cast.<<=, OPERATOR 3 ri_cast.===, + OPERATOR 4 ri_cast.>>=, OPERATOR 5 ri_cast.>>>, + FUNCTION 1 ri_i4a_cmp(int4[], int4[]); +CREATE TABLE ri_pk_arr (k int4[]); +CREATE UNIQUE INDEX ON ri_pk_arr (k ri_i4a_ops); +INSERT INTO ri_pk_arr VALUES ('{1,2}'); +CREATE TABLE ri_fk_arr (a int2[] REFERENCES ri_pk_arr (k)); +INSERT INTO ri_fk_arr VALUES ('{1,2}'); -- succeeds +SAVEPOINT s; +INSERT INTO ri_fk_arr VALUES ('{3}'); -- fails +ROLLBACK TO SAVEPOINT s; + +-- A coercion via I/O has no cast function to call either. +CREATE TYPE ri_ioint; +CREATE FUNCTION ri_ioint_in(cstring) RETURNS ri_ioint + AS 'int4in' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION ri_ioint_out(ri_ioint) RETURNS cstring + AS 'int4out' LANGUAGE internal IMMUTABLE STRICT; +CREATE TYPE ri_ioint (INPUT = ri_ioint_in, OUTPUT = ri_ioint_out, LIKE = int4); +CREATE CAST (ri_ioint AS int4) WITH INOUT AS IMPLICIT; +CREATE TABLE ri_fk_io (a ri_ioint REFERENCES ri_pk); +INSERT INTO ri_fk_io VALUES ('1'); -- succeeds +SAVEPOINT s; +INSERT INTO ri_fk_io VALUES ('2'); -- fails +ROLLBACK TO SAVEPOINT s; +ROLLBACK; ++======= + -- An UPDATE of an FK column whose type reaches the PK type only through an + -- implicit I/O cast must compare the old and new values through I/O, too. + -- The FK row must not be inserted in the same transaction as the UPDATE, + -- or the comparison is skipped. + CREATE TYPE ri_ioint2; + CREATE FUNCTION ri_ioint2_in(cstring) RETURNS ri_ioint2 + AS 'int4in' LANGUAGE internal IMMUTABLE STRICT; + CREATE FUNCTION ri_ioint2_out(ri_ioint2) RETURNS cstring + AS 'int4out' LANGUAGE internal IMMUTABLE STRICT; + CREATE TYPE ri_ioint2 (INPUT = ri_ioint2_in, OUTPUT = ri_ioint2_out, LIKE = int4); + CREATE CAST (ri_ioint2 AS int4) WITH INOUT AS IMPLICIT; + CREATE TABLE ri_pk_io2 (id int4 PRIMARY KEY); + CREATE TABLE ri_fk_io2 (a ri_ioint2 REFERENCES ri_pk_io2, b int); + INSERT INTO ri_pk_io2 VALUES (1), (2); + INSERT INTO ri_fk_io2 VALUES ('1', 10); + UPDATE ri_fk_io2 SET a = a, b = 11; -- succeeds + UPDATE ri_fk_io2 SET a = '2'; -- succeeds + UPDATE ri_fk_io2 SET a = '3'; -- fails + SELECT a::text, b FROM ri_fk_io2; + DROP TABLE ri_fk_io2, ri_pk_io2; + DROP TYPE ri_ioint2 CASCADE; ++>>>>>>> theirs