@ -1895,8 +1895,8 @@ WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY oid::regclass:
(5 rows)
DROP TABLE fk_partitioned_fk, fk_notpartitioned_pk;
-- NOT VALID foreign key on a non-partitioned table referencing a partitioned
-- table
-- NOT VALID and NOT ENFORCED foreign key on a non-partitioned table
-- referencing a partitioned table
CREATE TABLE fk_partitioned_pk (a int, b int, PRIMARY KEY (a, b)) PARTITION BY RANGE (a, b);
CREATE TABLE fk_partitioned_pk_1 PARTITION OF fk_partitioned_pk FOR VALUES FROM (0,0) TO (1000,1000);
CREATE TABLE fk_partitioned_pk_2 PARTITION OF fk_partitioned_pk FOR VALUES FROM (1000,1000) TO (2000,2000);
@ -1905,26 +1905,35 @@ INSERT INTO fk_partitioned_pk VALUES(100,100), (1000,1000);
INSERT INTO fk_notpartitioned_fk VALUES(100,100), (1000,1000);
ALTER TABLE fk_notpartitioned_fk ADD CONSTRAINT fk_notpartitioned_fk_a_b_fkey
FOREIGN KEY (a, b) REFERENCES fk_partitioned_pk NOT VALID;
-- All constraints will be invalid.
ALTER TABLE fk_notpartitioned_fk ADD CONSTRAINT fk_notpartitioned_fk_a_b_fkey2
FOREIGN KEY (a, b) REFERENCES fk_partitioned_pk NOT ENFORCED;
-- All constraints will be invalid, and _fkey2 constraints will not be enforced.
SELECT conname, conenforced, convalidated FROM pg_constraint
WHERE conrelid = 'fk_notpartitioned_fk'::regclass ORDER BY oid::regclass::text;
conname | conenforced | convalidated
---------------------------------+-------------+--------------
fk_notpartitioned_fk_a_b_fkey | t | f
fk_notpartitioned_fk_a_b_fkey_1 | t | f
fk_notpartitioned_fk_a_b_fkey_2 | t | f
(3 rows)
conname | conenforced | convalidated
----------------------------------+-------------+--------------
fk_notpartitioned_fk_a_b_fkey | t | f
fk_notpartitioned_fk_a_b_fkey_1 | t | f
fk_notpartitioned_fk_a_b_fkey_2 | t | f
fk_notpartitioned_fk_a_b_fkey2 | f | f
fk_notpartitioned_fk_a_b_fkey2_1 | f | f
fk_notpartitioned_fk_a_b_fkey2_2 | f | f
(6 rows)
ALTER TABLE fk_notpartitioned_fk VALIDATE CONSTRAINT fk_notpartitioned_fk_a_b_fkey;
-- All constraints are now valid.
ALTER TABLE fk_notpartitioned_fk ALTER CONSTRAINT fk_notpartitioned_fk_a_b_fkey2 ENFORCED;
-- All constraints are now valid and enforced.
SELECT conname, conenforced, convalidated FROM pg_constraint
WHERE conrelid = 'fk_notpartitioned_fk'::regclass ORDER BY oid::regclass::text;
conname | conenforced | convalidated
---------------------------------+-------------+--------------
fk_notpartitioned_fk_a_b_fkey | t | t
fk_notpartitioned_fk_a_b_fkey_1 | t | t
fk_notpartitioned_fk_a_b_fkey_2 | t | t
(3 rows)
conname | conenforced | convalidated
----------------------------------+-------------+--------------
fk_notpartitioned_fk_a_b_fkey | t | t
fk_notpartitioned_fk_a_b_fkey_1 | t | t
fk_notpartitioned_fk_a_b_fkey_2 | t | t
fk_notpartitioned_fk_a_b_fkey2 | t | t
fk_notpartitioned_fk_a_b_fkey2_1 | t | t
fk_notpartitioned_fk_a_b_fkey2_2 | t | t
(6 rows)
-- test a self-referential FK
ALTER TABLE fk_partitioned_pk ADD CONSTRAINT selffk FOREIGN KEY (a, b) REFERENCES fk_partitioned_pk NOT VALID;