Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

SET CONSTRAINTS

Changes, within the current transaction, when foreign keys declared as DEFERRABLE are checked.

SET CONSTRAINTS ALL DEFERRED;
SET CONSTRAINTS ALL IMMEDIATE;
SET CONSTRAINTS child_parent DEFERRED;
SET CONSTRAINTS child_parent, other_parent IMMEDIATE;

DEFERRED allows a deferrable foreign key to be temporarily violated during the transaction. The check runs when it is changed back to IMMEDIATE or at COMMIT, according to the effective constraint state.

IMMEDIATE validates pending deferrable foreign keys before changing the mode. If a reference is still invalid, the command fails with SQLSTATE 23503 and the mode is not changed.

The command can only run inside an explicit transaction. Its mode belongs to the current transaction, is saved by SAVEPOINT, and is restored by ROLLBACK TO SAVEPOINT.

Non-deferrable constraints remain immediate. The foreign key must be declared DEFERRABLE, for example:

CREATE TABLE child (
    parent_id int4 REFERENCES parent(id) DEFERRABLE INITIALLY DEFERRED
);

SET CONSTRAINTS does not make a non-deferrable foreign key deferrable and does not change the persisted catalog definition.