@@ -21,8 +21,10 @@ PostgreSQL documentation
2121
2222 <refsynopsisdiv>
2323<synopsis>
24- CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTER | INSTEAD OF } { <replaceable class="PARAMETER">event</replaceable> [ OR ... ] }
24+ CREATE [ CONSTRAINT ] TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTER | INSTEAD OF } { <replaceable class="PARAMETER">event</replaceable> [ OR ... ] }
2525 ON <replaceable class="PARAMETER">table</replaceable> [ FOR [ EACH ] { ROW | STATEMENT } ]
26+ [ FROM <replaceable class="parameter">referenced_table_name</replaceable> ]
27+ { NOT DEFERRABLE | [ DEFERRABLE ] { INITIALLY IMMEDIATE | INITIALLY DEFERRED } }
2628 [ WHEN ( <replaceable class="parameter">condition</replaceable> ) ]
2729 EXECUTE PROCEDURE <replaceable class="PARAMETER">function_name</replaceable> ( <replaceable class="PARAMETER">arguments</replaceable> )
2830</synopsis>
@@ -149,6 +151,20 @@ CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTE
149151 they will be fired in alphabetical order by name.
150152 </para>
151153
154+ <para>
155+ When the <literal>CONSTRAINT</> option is specified, this command creates a
156+ <firstterm>constraint trigger</>. This is the same as a regular trigger
157+ except that the timing of the trigger firing can be adjusted using
158+ <xref linkend="SQL-SET-CONSTRAINTS">.
159+ Constraint triggers must be <literal>AFTER ROW</> triggers. They can
160+ be fired either at the end of the statement causing the triggering event,
161+ or at the end of the containing transaction; in the latter case they are
162+ said to be <firstterm>deferred</>. A pending deferred-trigger firing can
163+ also be forced to happen immediately by using <command>SET CONSTRAINTS</>.
164+ Constraint triggers are expected to raise an exception when the constraints
165+ they implement are violated.
166+ </para>
167+
152168 <para>
153169 <command>SELECT</command> does not modify any rows so you cannot
154170 create <command>SELECT</command> triggers. Rules and views are more
@@ -170,6 +186,10 @@ CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTE
170186 <para>
171187 The name to give the new trigger. This must be distinct from
172188 the name of any other trigger for the same table.
189+ The name cannot be schema-qualified — the trigger inherits the
190+ schema of its table. For a constraint trigger, this is also the name to
191+ use when modifying the trigger's behavior using
192+ <command>SET CONSTRAINTS</>.
173193 </para>
174194 </listitem>
175195 </varlistentry>
@@ -181,7 +201,8 @@ CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTE
181201 <listitem>
182202 <para>
183203 Determines whether the function is called before, after, or instead of
184- the event.
204+ the event. A constraint trigger can only be specified as
205+ <literal>AFTER</>.
185206 </para>
186207 </listitem>
187208 </varlistentry>
@@ -222,6 +243,33 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
222243 </listitem>
223244 </varlistentry>
224245
246+ <varlistentry>
247+ <term><replaceable class="PARAMETER">referenced_table_name</replaceable></term>
248+ <listitem>
249+ <para>
250+ The (possibly schema-qualified) name of another table referenced by the
251+ constraint. This option is used for foreign-key constraints and is not
252+ recommended for general use. This can only be specified for
253+ constraint triggers.
254+ </para>
255+ </listitem>
256+ </varlistentry>
257+
258+ <varlistentry>
259+ <term><literal>DEFERRABLE</literal></term>
260+ <term><literal>NOT DEFERRABLE</literal></term>
261+ <term><literal>INITIALLY IMMEDIATE</literal></term>
262+ <term><literal>INITIALLY DEFERRED</literal></term>
263+ <listitem>
264+ <para>
265+ The default timing of the trigger.
266+ See the <xref linkend="SQL-CREATETABLE"> documentation for details of
267+ these constraint options. This can only be specified for constraint
268+ triggers.
269+ </para>
270+ </listitem>
271+ </varlistentry>
272+
225273 <varlistentry>
226274 <term><literal>FOR EACH ROW</literal></term>
227275 <term><literal>FOR EACH STATEMENT</literal></term>
@@ -231,7 +279,8 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
231279 This specifies whether the trigger procedure should be fired
232280 once for every row affected by the trigger event, or just once
233281 per SQL statement. If neither is specified, <literal>FOR EACH
234- STATEMENT</literal> is the default.
282+ STATEMENT</literal> is the default. Constraint triggers can only
283+ be specified <literal>FOR EACH ROW</>.
235284 </para>
236285 </listitem>
237286 </varlistentry>
@@ -263,6 +312,13 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
263312 Currently, <literal>WHEN</literal> expressions cannot contain
264313 subqueries.
265314 </para>
315+
316+ <para>
317+ Note that for constraint triggers, evaluation of the <literal>WHEN</>
318+ condition is not deferred, but occurs immediately after the row update
319+ operation is performed. If the condition does not evaluate to true then
320+ the trigger is not queued for deferred execution.
321+ </para>
266322 </listitem>
267323 </varlistentry>
268324
@@ -481,6 +537,12 @@ CREATE TRIGGER view_insert
481537 ability to define statement-level triggers on views.
482538 </para>
483539
540+ <para>
541+ <command>CREATE CONSTRAINT TRIGGER</command> is a
542+ <productname>PostgreSQL</productname> extension of the <acronym>SQL</>
543+ standard.
544+ </para>
545+
484546 </refsect1>
485547
486548 <refsect1>
@@ -490,6 +552,7 @@ CREATE TRIGGER view_insert
490552 <member><xref linkend="sql-createfunction"></member>
491553 <member><xref linkend="sql-altertrigger"></member>
492554 <member><xref linkend="sql-droptrigger"></member>
555+ <member><xref linkend="sql-set-constraints"></member>
493556 </simplelist>
494557 </refsect1>
495558</refentry>
0 commit comments