11<!--
2- $Header: /cvsroot/pgsql/doc/src/sgml/trigger.sgml,v 1.22 2002/04/01 22 :36:06 tgl Exp $
2+ $Header: /cvsroot/pgsql/doc/src/sgml/trigger.sgml,v 1.23 2002/04/19 16 :36:08 tgl Exp $
33-->
44
55 <chapter id="triggers">
@@ -14,8 +14,8 @@ $Header: /cvsroot/pgsql/doc/src/sgml/trigger.sgml,v 1.22 2002/04/01 22:36:06 tgl
1414 AFTER on INSERT, DELETE or UPDATE of a tuple as a trigger event.
1515 </para>
1616
17- <sect1 id="trigger-create ">
18- <title>TriggerCreation </title>
17+ <sect1 id="trigger-definition ">
18+ <title>TriggerDefinition </title>
1919
2020 <para>
2121 If a trigger event occurs, the trigger manager (called by the Executor)
@@ -24,13 +24,17 @@ $Header: /cvsroot/pgsql/doc/src/sgml/trigger.sgml,v 1.22 2002/04/01 22:36:06 tgl
2424 </para>
2525
2626 <para>
27- The trigger function must be defined before the trigger is created as a
28- function taking no arguments and returning opaque. If the function is
29- written in C, it must use the <quote>version 1</> function manager interface.
27+ The trigger function must be defined before the trigger itself can be
28+ created. The trigger function must be declared as a
29+ function taking no arguments and returning type <literal>opaque</>.
30+ (The trigger function receives its input through a TriggerData
31+ structure, not in the form of ordinary function arguments.)
32+ If the function is written in C, it must use the <quote>version 1</>
33+ function manager interface.
3034 </para>
3135
3236 <para>
33- The syntax for creating triggers is as follows :
37+ The syntax for creating triggers is:
3438
3539 <programlisting>
3640CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT | DELETE | UPDATE [ OR ... ] ]
@@ -48,9 +52,9 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
4852 </term>
4953 <listitem>
5054 <para>
51- The nameof the trigger is
52- used if you ever have to delete the trigger.
53- It is used as an argument to the<command>DROP TRIGGER</command> command .
55+ Thetrigger must have a namedistinct from all other triggers on
56+ the same table. The name is needed
57+ if you ever have todelete thetrigger .
5458 </para>
5559 </listitem>
5660 </varlistentry>
@@ -72,7 +76,7 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
7276 <term>UPDATE</term>
7377 <listitem>
7478 <para>
75- The next element of the command determineson what event(s) will trigger
79+ The next element of the command determines what event(s) will trigger
7680the function. Multiple events can be specified separated by OR.
7781 </para>
7882 </listitem>
@@ -82,7 +86,7 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
8286 <term><replaceable>relation</replaceable></term>
8387 <listitem>
8488 <para>
85- The relation namedetermines which table the event applies to.
89+ The relation nameindicates which table the event applies to.
8690 </para>
8791 </listitem>
8892 </varlistentry>
@@ -94,6 +98,7 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
9498 <para>
9599The FOR EACH clause determines whether the trigger is fired for each
96100affected row or before (or after) the entire statement has completed.
101+ Currently only the ROW case is supported.
97102 </para>
98103 </listitem>
99104 </varlistentry>
@@ -102,7 +107,7 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
102107 <term><replaceable>procedure</replaceable></term>
103108 <listitem>
104109 <para>
105- The procedure name is the function called.
110+ The procedure name is the functionto be called.
106111 </para>
107112 </listitem>
108113 </varlistentry>
@@ -112,32 +117,32 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
112117 <listitem>
113118 <para>
114119The arguments passed to the function in the TriggerData structure.
115- The purpose of passing arguments to the function is to allow different
116- triggers with similar requirements tocall thesame function.
120+ This is either empty or a list of one or more simple literal
121+ constants (which will be passed to the function as strings) .
117122 </para>
118123
119124 <para>
120- Also, <replaceable>procedure</replaceable>
121- may be used for triggering different relations (these
122- functions are named as <firstterm>general trigger functions</>) .
123- </para>
124-
125- <para>
126- As example of using both features above, there could bea general
127- function that takes as its arguments two field names and puts thecurrent
128- user in one and the current timestamp in the other. This allows triggers to
129- be written on INSERT events to automatically track creation of records in a
130- transaction table for example. It could also be usedas a <quote> last updated</>
131- function if used in an UPDATEevent .
125+ The purpose of including arguments in the trigger definition
126+ is to allow different
127+ triggers with similar requirements to call the same function .
128+ As an example, there could be a generalized trigger
129+ function that takes as its arguments two field names and puts the
130+ current user in one and the current timestamp in the other.
131+ Properly written, this trigger function would beindependent of
132+ the specific table it is triggering on. So thesame function
133+ could be used for INSERT events on any table with suitable fields,
134+ to automatically track creation of records in a transaction table for
135+ example. It could also be usedto track last-update events if
136+ defined as an UPDATEtrigger .
132137 </para>
133138 </listitem>
134139 </varlistentry>
135140 </variablelist>
136141 </para>
137142
138143 <para>
139- Trigger functions return HeapTuple to the calling Executor.This
140- is ignored for triggers firedafter anINSERT, DELETE or UPDATE operation
144+ Trigger functions returna HeapTuple to the calling Executor.The return
145+ value is ignored for triggers firedAFTER an operation,
141146 but it allows BEFORE triggers to:
142147
143148 <itemizedlist>
@@ -150,33 +155,41 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
150155
151156 <listitem>
152157 <para>
153- Return a pointer to another tuple (INSERT and UPDATE only) which will
154- be inserted (as the new version of the updated tuple if UPDATE) instead
155- of original tuple.
158+ For INSERT and UPDATE triggers only, the returned tuple becomes the
159+ tuple which will be inserted or will replace the tuple being updated.
160+ This allows the trigger function to modify the row being inserted or
161+ updated.
156162 </para>
157163 </listitem>
158164 </itemizedlist>
165+
166+ A BEFORE trigger that does not intend to cause either of these behaviors
167+ must be careful to return the same NEW tuple it is passed.
159168 </para>
160169
161170 <para>
162171 Note that there is no initialization performed by the CREATE TRIGGER
163- handler. This will be changed in the future. Also, if more than one trigger
164- is defined for the same event on the same relation, the order of trigger
165- firing is unpredictable. This may be changed in the future.
172+ handler. This may be changed in the future.
166173 </para>
167174
168175 <para>
169- If a trigger function executes SQL-queries (using SPI) then these queries
170- may fire triggers again. This is known as cascading triggers. There is no
171- explicit limitation on the number of cascade levels.
176+ If more than one trigger
177+ is defined for the same event on the same relation, the triggers will
178+ be fired in alphabetical order by name. In the case of BEFORE triggers,
179+ the possibly-modified tuple returned by each trigger becomes the input
180+ to the next trigger. If any BEFORE trigger returns NULL, the operation
181+ is abandoned and subsequent triggers are not fired.
172182 </para>
173183
174184 <para>
175- If a trigger is fired by INSERT and inserts a new tuple in the same
176- relation then this trigger will be fired again. Currently, there is nothing
177- provided for synchronization (etc) of these cases but this may change. At
178- the moment, there is function funny_dup17() in the regress tests which uses
179- some techniques to stop recursion (cascading) on itself...
185+ If a trigger function executes SQL-queries (using SPI) then these queries
186+ may fire triggers again. This is known as cascading triggers. There is no
187+ direct limitation on the number of cascade levels. It is possible for
188+ cascades to cause recursive invocation of the same trigger --- for
189+ example, an INSERT trigger might execute a query that inserts an
190+ additional tuple into the same table, causing the INSERT trigger to be
191+ fired again. It is the trigger programmer's
192+ responsibility to avoid infinite recursion in such scenarios.
180193 </para>
181194 </sect1>
182195
@@ -326,7 +339,7 @@ typedef struct TriggerData
326339 <para>
327340is a pointer to structure describing the triggered relation. Look at
328341src/include/utils/rel.h for details about this structure. The most
329- interest things are tg_relation->rd_att (descriptor of the relation
342+ interesting things are tg_relation->rd_att (descriptor of the relation
330343tuples) and tg_relation->rd_rel->relname (relation's name. This is not
331344char*, but NameData. Use SPI_getrelname(tg_relation) to get char* if
332345you need a copy of name).