1SET CONSTRAINTS(7) PostgreSQL 15.4 Documentation SET CONSTRAINTS(7)
2
3
4
6 SET_CONSTRAINTS - set constraint check timing for the current
7 transaction
8
10 SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }
11
13 SET CONSTRAINTS sets the behavior of constraint checking within the
14 current transaction. IMMEDIATE constraints are checked at the end of
15 each statement. DEFERRED constraints are not checked until transaction
16 commit. Each constraint has its own IMMEDIATE or DEFERRED mode.
17
18 Upon creation, a constraint is given one of three characteristics:
19 DEFERRABLE INITIALLY DEFERRED, DEFERRABLE INITIALLY IMMEDIATE, or NOT
20 DEFERRABLE. The third class is always IMMEDIATE and is not affected by
21 the SET CONSTRAINTS command. The first two classes start every
22 transaction in the indicated mode, but their behavior can be changed
23 within a transaction by SET CONSTRAINTS.
24
25 SET CONSTRAINTS with a list of constraint names changes the mode of
26 just those constraints (which must all be deferrable). Each constraint
27 name can be schema-qualified. The current schema search path is used to
28 find the first matching name if no schema name is specified. SET
29 CONSTRAINTS ALL changes the mode of all deferrable constraints.
30
31 When SET CONSTRAINTS changes the mode of a constraint from DEFERRED to
32 IMMEDIATE, the new mode takes effect retroactively: any outstanding
33 data modifications that would have been checked at the end of the
34 transaction are instead checked during the execution of the SET
35 CONSTRAINTS command. If any such constraint is violated, the SET
36 CONSTRAINTS fails (and does not change the constraint mode). Thus, SET
37 CONSTRAINTS can be used to force checking of constraints to occur at a
38 specific point in a transaction.
39
40 Currently, only UNIQUE, PRIMARY KEY, REFERENCES (foreign key), and
41 EXCLUDE constraints are affected by this setting. NOT NULL and CHECK
42 constraints are always checked immediately when a row is inserted or
43 modified (not at the end of the statement). Uniqueness and exclusion
44 constraints that have not been declared DEFERRABLE are also checked
45 immediately.
46
47 The firing of triggers that are declared as “constraint triggers” is
48 also controlled by this setting — they fire at the same time that the
49 associated constraint should be checked.
50
52 Because PostgreSQL does not require constraint names to be unique
53 within a schema (but only per-table), it is possible that there is more
54 than one match for a specified constraint name. In this case SET
55 CONSTRAINTS will act on all matches. For a non-schema-qualified name,
56 once a match or matches have been found in some schema in the search
57 path, schemas appearing later in the path are not searched.
58
59 This command only alters the behavior of constraints within the current
60 transaction. Issuing this outside of a transaction block emits a
61 warning and otherwise has no effect.
62
64 This command complies with the behavior defined in the SQL standard,
65 except for the limitation that, in PostgreSQL, it does not apply to NOT
66 NULL and CHECK constraints. Also, PostgreSQL checks non-deferrable
67 uniqueness constraints immediately, not at end of statement as the
68 standard would suggest.
69
70
71
72PostgreSQL 15.4 2023 SET CONSTRAINTS(7)