1
1
<!--
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 $
3
3
-->
4
4
5
5
<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
14
14
AFTER on INSERT, DELETE or UPDATE of a tuple as a trigger event.
15
15
</para>
16
16
17
- <sect1 id="trigger-create ">
18
- <title>TriggerCreation </title>
17
+ <sect1 id="trigger-definition ">
18
+ <title>TriggerDefinition </title>
19
19
20
20
<para>
21
21
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
24
24
</para>
25
25
26
26
<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.
30
34
</para>
31
35
32
36
<para>
33
- The syntax for creating triggers is as follows :
37
+ The syntax for creating triggers is:
34
38
35
39
<programlisting>
36
40
CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT | DELETE | UPDATE [ OR ... ] ]
@@ -48,9 +52,9 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
48
52
</term>
49
53
<listitem>
50
54
<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 .
54
58
</para>
55
59
</listitem>
56
60
</varlistentry>
@@ -72,7 +76,7 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
72
76
<term>UPDATE</term>
73
77
<listitem>
74
78
<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
76
80
the function. Multiple events can be specified separated by OR.
77
81
</para>
78
82
</listitem>
@@ -82,7 +86,7 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
82
86
<term><replaceable>relation</replaceable></term>
83
87
<listitem>
84
88
<para>
85
- The relation namedetermines which table the event applies to.
89
+ The relation nameindicates which table the event applies to.
86
90
</para>
87
91
</listitem>
88
92
</varlistentry>
@@ -94,6 +98,7 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
94
98
<para>
95
99
The FOR EACH clause determines whether the trigger is fired for each
96
100
affected row or before (or after) the entire statement has completed.
101
+ Currently only the ROW case is supported.
97
102
</para>
98
103
</listitem>
99
104
</varlistentry>
@@ -102,7 +107,7 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
102
107
<term><replaceable>procedure</replaceable></term>
103
108
<listitem>
104
109
<para>
105
- The procedure name is the function called.
110
+ The procedure name is the functionto be called.
106
111
</para>
107
112
</listitem>
108
113
</varlistentry>
@@ -112,32 +117,32 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
112
117
<listitem>
113
118
<para>
114
119
The 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) .
117
122
</para>
118
123
119
124
<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 .
132
137
</para>
133
138
</listitem>
134
139
</varlistentry>
135
140
</variablelist>
136
141
</para>
137
142
138
143
<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,
141
146
but it allows BEFORE triggers to:
142
147
143
148
<itemizedlist>
@@ -150,33 +155,41 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
150
155
151
156
<listitem>
152
157
<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.
156
162
</para>
157
163
</listitem>
158
164
</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.
159
168
</para>
160
169
161
170
<para>
162
171
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.
166
173
</para>
167
174
168
175
<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.
172
182
</para>
173
183
174
184
<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.
180
193
</para>
181
194
</sect1>
182
195
@@ -326,7 +339,7 @@ typedef struct TriggerData
326
339
<para>
327
340
is a pointer to structure describing the triggered relation. Look at
328
341
src/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
330
343
tuples) and tg_relation->rd_rel->relname (relation's name. This is not
331
344
char*, but NameData. Use SPI_getrelname(tg_relation) to get char* if
332
345
you need a copy of name).