]> begriffs open source - ai-pg/blob - full-docs/man7/SET_CONSTRAINTS.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / SET_CONSTRAINTS.7
1 '\" t
2 .\"     Title: SET CONSTRAINTS
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "SET CONSTRAINTS" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 SET_CONSTRAINTS \- set constraint check timing for the current transaction
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 SET CONSTRAINTS { ALL | \fIname\fR [, \&.\&.\&.] } { DEFERRED | IMMEDIATE }
36 .fi
37 .SH "DESCRIPTION"
38 .PP
39 \fBSET CONSTRAINTS\fR
40 sets the behavior of constraint checking within the current transaction\&.
41 IMMEDIATE
42 constraints are checked at the end of each statement\&.
43 DEFERRED
44 constraints are not checked until transaction commit\&. Each constraint has its own
45 IMMEDIATE
46 or
47 DEFERRED
48 mode\&.
49 .PP
50 Upon creation, a constraint is given one of three characteristics:
51 DEFERRABLE INITIALLY DEFERRED,
52 DEFERRABLE INITIALLY IMMEDIATE, or
53 NOT DEFERRABLE\&. The third class is always
54 IMMEDIATE
55 and is not affected by the
56 \fBSET CONSTRAINTS\fR
57 command\&. The first two classes start every transaction in the indicated mode, but their behavior can be changed within a transaction by
58 \fBSET CONSTRAINTS\fR\&.
59 .PP
60 \fBSET CONSTRAINTS\fR
61 with a list of constraint names changes the mode of just those constraints (which must all be deferrable)\&. Each constraint name can be schema\-qualified\&. The current schema search path is used to find the first matching name if no schema name is specified\&.
62 \fBSET CONSTRAINTS ALL\fR
63 changes the mode of all deferrable constraints\&.
64 .PP
65 When
66 \fBSET CONSTRAINTS\fR
67 changes the mode of a constraint from
68 DEFERRED
69 to
70 IMMEDIATE, the new mode takes effect retroactively: any outstanding data modifications that would have been checked at the end of the transaction are instead checked during the execution of the
71 \fBSET CONSTRAINTS\fR
72 command\&. If any such constraint is violated, the
73 \fBSET CONSTRAINTS\fR
74 fails (and does not change the constraint mode)\&. Thus,
75 \fBSET CONSTRAINTS\fR
76 can be used to force checking of constraints to occur at a specific point in a transaction\&.
77 .PP
78 Currently, only
79 UNIQUE,
80 PRIMARY KEY,
81 REFERENCES
82 (foreign key), and
83 EXCLUDE
84 constraints are affected by this setting\&.
85 NOT NULL
86 and
87 CHECK
88 constraints are always checked immediately when a row is inserted or modified (\fInot\fR
89 at the end of the statement)\&. Uniqueness and exclusion constraints that have not been declared
90 DEFERRABLE
91 are also checked immediately\&.
92 .PP
93 The firing of triggers that are declared as
94 \(lqconstraint triggers\(rq
95 is also controlled by this setting \(em they fire at the same time that the associated constraint should be checked\&.
96 .SH "NOTES"
97 .PP
98 Because
99 PostgreSQL
100 does not require constraint names to be unique within a schema (but only per\-table), it is possible that there is more than one match for a specified constraint name\&. In this case
101 \fBSET CONSTRAINTS\fR
102 will act on all matches\&. For a non\-schema\-qualified name, once a match or matches have been found in some schema in the search path, schemas appearing later in the path are not searched\&.
103 .PP
104 This command only alters the behavior of constraints within the current transaction\&. Issuing this outside of a transaction block emits a warning and otherwise has no effect\&.
105 .SH "COMPATIBILITY"
106 .PP
107 This command complies with the behavior defined in the SQL standard, except for the limitation that, in
108 PostgreSQL, it does not apply to
109 NOT NULL
110 and
111 CHECK
112 constraints\&. Also,
113 PostgreSQL
114 checks non\-deferrable uniqueness constraints immediately, not at end of statement as the standard would suggest\&.