1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>SET CONSTRAINTS</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-set.html" title="SET" /><link rel="next" href="sql-set-role.html" title="SET ROLE" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">SET CONSTRAINTS</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-set.html" title="SET">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-set-role.html" title="SET ROLE">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-SET-CONSTRAINTS"><div class="titlepage"></div><a id="id-1.9.3.175.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">SET CONSTRAINTS</span></h2><p>SET CONSTRAINTS — set constraint check timing for the current transaction</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 SET CONSTRAINTS { ALL | <em class="replaceable"><code>name</code></em> [, ...] } { DEFERRED | IMMEDIATE }
4 </pre></div><div class="refsect1" id="id-1.9.3.175.5"><h2>Description</h2><p>
5 <code class="command">SET CONSTRAINTS</code> sets the behavior of constraint
6 checking within the current transaction. <code class="literal">IMMEDIATE</code>
7 constraints are checked at the end of each
8 statement. <code class="literal">DEFERRED</code> constraints are not checked until
9 transaction commit. Each constraint has its own
10 <code class="literal">IMMEDIATE</code> or <code class="literal">DEFERRED</code> mode.
12 Upon creation, a constraint is given one of three
13 characteristics: <code class="literal">DEFERRABLE INITIALLY DEFERRED</code>,
14 <code class="literal">DEFERRABLE INITIALLY IMMEDIATE</code>, or
15 <code class="literal">NOT DEFERRABLE</code>. The third
16 class is always <code class="literal">IMMEDIATE</code> and is not affected by the
17 <code class="command">SET CONSTRAINTS</code> command. The first two classes start
18 every transaction in the indicated mode, but their behavior can be changed
19 within a transaction by <code class="command">SET CONSTRAINTS</code>.
21 <code class="command">SET CONSTRAINTS</code> with a list of constraint names changes
22 the mode of just those constraints (which must all be deferrable). Each
23 constraint name can be schema-qualified. The
24 current schema search path is used to find the first matching name if
25 no schema name is specified. <code class="command">SET CONSTRAINTS ALL</code>
26 changes the mode of all deferrable constraints.
28 When <code class="command">SET CONSTRAINTS</code> changes the mode of a constraint
29 from <code class="literal">DEFERRED</code>
30 to <code class="literal">IMMEDIATE</code>, the new mode takes effect
31 retroactively: any outstanding data modifications that would have
32 been checked at the end of the transaction are instead checked during the
33 execution of the <code class="command">SET CONSTRAINTS</code> command.
34 If any such constraint is violated, the <code class="command">SET CONSTRAINTS</code>
35 fails (and does not change the constraint mode). Thus, <code class="command">SET
36 CONSTRAINTS</code> can be used to force checking of constraints to
37 occur at a specific point in a transaction.
39 Currently, only <code class="literal">UNIQUE</code>, <code class="literal">PRIMARY KEY</code>,
40 <code class="literal">REFERENCES</code> (foreign key), and <code class="literal">EXCLUDE</code>
41 constraints are affected by this setting.
42 <code class="literal">NOT NULL</code> and <code class="literal">CHECK</code> constraints are
43 always checked immediately when a row is inserted or modified
44 (<span class="emphasis"><em>not</em></span> at the end of the statement).
45 Uniqueness and exclusion constraints that have not been declared
46 <code class="literal">DEFERRABLE</code> are also checked immediately.
48 The firing of triggers that are declared as <span class="quote">“<span class="quote">constraint triggers</span>”</span>
49 is also controlled by this setting — they fire at the same time
50 that the associated constraint should be checked.
51 </p></div><div class="refsect1" id="id-1.9.3.175.6"><h2>Notes</h2><p>
52 Because <span class="productname">PostgreSQL</span> does not require constraint
53 names to be unique within a schema (but only per-table), it is possible
54 that there is more than one match for a specified constraint name.
55 In this case <code class="command">SET CONSTRAINTS</code> will act on all matches.
56 For a non-schema-qualified name, once a match or matches have been found in
57 some schema in the search path, schemas appearing later in the path are not
60 This command only alters the behavior of constraints within the
61 current transaction. Issuing this outside of a transaction block
62 emits a warning and otherwise has no effect.
63 </p></div><div class="refsect1" id="id-1.9.3.175.7"><h2>Compatibility</h2><p>
64 This command complies with the behavior defined in the SQL
65 standard, except for the limitation that, in
66 <span class="productname">PostgreSQL</span>, it does not apply to
67 <code class="literal">NOT NULL</code> and <code class="literal">CHECK</code> constraints.
68 Also, <span class="productname">PostgreSQL</span> checks non-deferrable
69 uniqueness constraints immediately, not at end of statement as the
70 standard would suggest.
71 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-set.html" title="SET">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-set-role.html" title="SET ROLE">Next</a></td></tr><tr><td width="40%" align="left" valign="top">SET </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> SET ROLE</td></tr></table></div></body></html>