We have inserted inconsistent data on purpose so that any attempt to check existing rows will be revealed by an error message.
If we attempt to create an ordinary foreign key, we get an error, since the number 3 does not appear in the ft table:
postgres=# ALTER TABLE pt ADD CONSTRAINT pc FOREIGN KEY (pk) REFERENCES ft(fk);
ERROR: insert or update on table "pt" violates foreign key constraint
pc"
DETAIL: Key (pk)=(3) is not present in table "ft".
However, the same constraint can be successfully created as NOT VALID:
postgres=# ALTER TABLE pt ADD CONSTRAINT pc FOREIGN KEY (pk) REFERENCES ft(fk) NOT VALID;
ALTER TABLE
Note that the invalid state of the foreign key is mentioned by psql.
postgres=# \d pt
Table "public.pt"
Column | Type | Modifiers
--------+---------+-----------
pk | integer |
ps | text |
Foreign-key constraints:
"pc" FOREIGN KEY (pk) REFERENCES ft(fk) NOT VALID
The violation is detected when we try to transform the NOT VALID constraint into a valid one:
postgres=# ALTER TABLE pt VALIDATE CONSTRAINT pc;
ERROR: insert or update on table "pt" violates foreign key constraint
pc"
DETAIL: Key (pk)=(3) is not present in table "ft".
Validation becomes possible after removing the inconsistency, and the foreign key is upgraded to an ordinary one:
postgres=# DELETE FROM pt WHERE pk = 3;
DELETE 1
postgres=# ALTER TABLE pt VALIDATE CONSTRAINT pc;
ALTER TABLE
postgres=# \d pt
Table "public.pt"
Column | Type | Modifiers
--------+---------+-----------
pk | integer |
ps | text |
Foreign-key constraints:
"pc" FOREIGN KEY (pk) REFERENCES ft(fk)