1
<!-- doc/src/sgml/ref/set_constraints.sgml -->
2
<refentry id="sql-set-constraints">
3
<indexterm zone="sql-set-constraints">
4
<primary>SET CONSTRAINTS</primary>
8
<refentrytitle>SET CONSTRAINTS</refentrytitle>
9
<manvolnum>7</manvolnum>
10
<refmiscinfo>SQL - Language Statements</refmiscinfo>
14
<refname>SET CONSTRAINTS</refname>
15
<refpurpose>set constraint check timing for the current transaction</refpurpose>
20
SET CONSTRAINTS { ALL | <replaceable class="parameter">name</replaceable> [, ...] } { DEFERRED | IMMEDIATE }
25
<title>Description</title>
28
<command>SET CONSTRAINTS</command> sets the behavior of constraint
29
checking within the current transaction. <literal>IMMEDIATE</literal>
30
constraints are checked at the end of each
31
statement. <literal>DEFERRED</literal> constraints are not checked until
32
transaction commit. Each constraint has its own
33
<literal>IMMEDIATE</literal> or <literal>DEFERRED</literal> mode.
37
Upon creation, a constraint is given one of three
38
characteristics: <literal>DEFERRABLE INITIALLY DEFERRED</literal>,
39
<literal>DEFERRABLE INITIALLY IMMEDIATE</literal>, or
40
<literal>NOT DEFERRABLE</literal>. The third
41
class is always <literal>IMMEDIATE</literal> and is not affected by the
42
<command>SET CONSTRAINTS</command> command. The first two classes start
43
every transaction in the indicated mode, but their behavior can be changed
44
within a transaction by <command>SET CONSTRAINTS</command>.
48
<command>SET CONSTRAINTS</command> with a list of constraint names changes
49
the mode of just those constraints (which must all be deferrable). Each
50
constraint name can be schema-qualified. The
51
current schema search path is used to find the first matching name if
52
no schema name is specified. <command>SET CONSTRAINTS ALL</command>
53
changes the mode of all deferrable constraints.
57
When <command>SET CONSTRAINTS</command> changes the mode of a constraint
58
from <literal>DEFERRED</literal>
59
to <literal>IMMEDIATE</literal>, the new mode takes effect
60
retroactively: any outstanding data modifications that would have
61
been checked at the end of the transaction are instead checked during the
62
execution of the <command>SET CONSTRAINTS</command> command.
63
If any such constraint is violated, the <command>SET CONSTRAINTS</command>
64
fails (and does not change the constraint mode). Thus, <command>SET
65
CONSTRAINTS</command> can be used to force checking of constraints to
66
occur at a specific point in a transaction.
70
Currently, only <literal>UNIQUE</literal>, <literal>PRIMARY KEY</literal>,
71
<literal>REFERENCES</literal> (foreign key), and <literal>EXCLUDE</literal>
72
constraints are affected by this setting.
73
<literal>NOT NULL</literal> and <literal>CHECK</literal> constraints are
74
always checked immediately when a row is inserted or modified
75
(<emphasis>not</emphasis> at the end of the statement).
76
Uniqueness and exclusion constraints that have not been declared
77
<literal>DEFERRABLE</literal> are also checked immediately.
81
The firing of triggers that are declared as <quote>constraint triggers</quote>
82
is also controlled by this setting — they fire at the same time
83
that the associated constraint should be checked.
91
Because <productname>PostgreSQL</productname> does not require constraint
92
names to be unique within a schema (but only per-table), it is possible
93
that there is more than one match for a specified constraint name.
94
In this case <command>SET CONSTRAINTS</command> will act on all matches.
95
For a non-schema-qualified name, once a match or matches have been found in
96
some schema in the search path, schemas appearing later in the path are not
101
This command only alters the behavior of constraints within the
102
current transaction. Issuing this outside of a transaction block
103
emits a warning and otherwise has no effect.
108
<title>Compatibility</title>
111
This command complies with the behavior defined in the SQL
112
standard, except for the limitation that, in
113
<productname>PostgreSQL</productname>, it does not apply to
114
<literal>NOT NULL</literal> and <literal>CHECK</literal> constraints.
115
Also, <productname>PostgreSQL</productname> checks non-deferrable
116
uniqueness constraints immediately, not at end of statement as the
117
standard would suggest.