]> begriffs open source - ai-pg/blob - full-docs/txt/sql-set-constraints.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / sql-set-constraints.txt
1
2 SET CONSTRAINTS
3
4    SET CONSTRAINTS — set constraint check timing for the current
5    transaction
6
7 Synopsis
8
9 SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }
10
11 Description
12
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
51 Notes
52
53    Because PostgreSQL does not require constraint names to be unique
54    within a schema (but only per-table), it is possible that there is more
55    than one match for a specified constraint name. In this case SET
56    CONSTRAINTS will act on all matches. For a non-schema-qualified name,
57    once a match or matches have been found in some schema in the search
58    path, schemas appearing later in the path are not searched.
59
60    This command only alters the behavior of constraints within the current
61    transaction. Issuing this outside of a transaction block emits a
62    warning and otherwise has no effect.
63
64 Compatibility
65
66    This command complies with the behavior defined in the SQL standard,
67    except for the limitation that, in PostgreSQL, it does not apply to NOT
68    NULL and CHECK constraints. Also, PostgreSQL checks non-deferrable
69    uniqueness constraints immediately, not at end of statement as the
70    standard would suggest.