Skip to content

Commit 58ae3cf

Browse files
author
Neil Conway
committed
Minor improvements to the trigger documentation, and a few SGML fixes.
1 parent 5ad7d65 commit 58ae3cf

File tree

2 files changed

+74
-44
lines changed

2 files changed

+74
-44
lines changed

doc/src/sgml/plpgsql.sgml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.32 2003/11/30 05:45:22 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.33 2004/01/22 19:50:21 neilc Exp $
33
-->
44

55
<chapter id="plpgsql">
@@ -729,7 +729,7 @@ RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
729729
<para>
730730
Using the <literal>RENAME</literal> declaration you can change the
731731
name of a variable, record or row. This is primarily useful if
732-
<literal>NEW</literal> or <literal>OLD</literal> should be
732+
<varname>NEW</varname> or <varname>OLD</varname> should be
733733
referenced by another name inside a trigger procedure. See also
734734
<literal>ALIAS</literal>.
735735
</para>
@@ -2176,7 +2176,7 @@ RAISE EXCEPTION ''Inexistent ID --> %'', user_id;
21762176
<para>
21772177
Data type <type>RECORD</type>; variable holding the new
21782178
database row for <command>INSERT</>/<command>UPDATE</> operations in row-level
2179-
triggers. This variable is null in statement-level triggers.
2179+
triggers. This variable is <symbol>NULL</symbol> in statement-level triggers.
21802180
</para>
21812181
</listitem>
21822182
</varlistentry>
@@ -2187,7 +2187,7 @@ RAISE EXCEPTION ''Inexistent ID --> %'', user_id;
21872187
<para>
21882188
Data type <type>RECORD</type>; variable holding the old
21892189
database row for <command>UPDATE</>/<command>DELETE</> operations in row-level
2190-
triggers. This variable is null in statement-level triggers.
2190+
triggers. This variable is <symbol>NULL</symbol> in statement-level triggers.
21912191
</para>
21922192
</listitem>
21932193
</varlistentry>
@@ -2207,7 +2207,7 @@ RAISE EXCEPTION ''Inexistent ID --> %'', user_id;
22072207
<listitem>
22082208
<para>
22092209
Data type <type>text</type>; a string of either
2210-
<literal>BEFORE</literal> or <literal>AFTER</literal>
2210+
<literal>BEFORE</literal> or <literal>AFTER</literal>
22112211
depending on the trigger's definition.
22122212
</para>
22132213
</listitem>
@@ -2281,9 +2281,9 @@ RAISE EXCEPTION ''Inexistent ID --> %'', user_id;
22812281
</para>
22822282

22832283
<para>
2284-
A trigger function must return either null or a record/row value
2285-
having exactly the structure of the table the trigger was fired
2286-
for.
2284+
A trigger function must return either <symbol>NULL</symbol> or a
2285+
record/row value having exactly the structure of the table the
2286+
trigger was fired for.
22872287
</para>
22882288

22892289
<para>

doc/src/sgml/trigger.sgml

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/trigger.sgml,v 1.33 2003/11/29 19:51:38 pgsql Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/trigger.sgml,v 1.34 2004/01/22 19:50:21 neilc Exp $
33
-->
44

55
<chapter id="triggers">
@@ -45,50 +45,69 @@ $PostgreSQL: pgsql/doc/src/sgml/trigger.sgml,v 1.33 2003/11/29 19:51:38 pgsql Ex
4545
</para>
4646

4747
<para>
48-
Trigger functions return a table row (a value of type
49-
<structname>HeapTuple</>) to the calling executor.
50-
A trigger fired before an operation has the following choices:
48+
There are two types of triggers: per-row triggers and
49+
per-statement triggers. In a per-row trigger, the trigger function
50+
is invoked once for every row that is affected by the statement
51+
that fired the trigger. In contrast, a per-statement trigger is
52+
invoked only once when an appropriate statement is executed,
53+
regardless of the number of rows affected by that statement. In
54+
particular, a statement that affects zero rows will still result
55+
in the execution of any applicable per-statement triggers. These
56+
two types of triggers are sometimes called <quote>row-level
57+
triggers</quote> and <quote>statement-level triggers</quote>,
58+
respectively.
59+
</para>
60+
61+
<para>
62+
Trigger functions invoked by per-statement triggers should always
63+
return <symbol>NULL</symbol>. Trigger functions invoked by per-row
64+
triggers can return a table row (a value of
65+
type <structname>HeapTuple</structname>) to the calling executor,
66+
if they choose. A row-level trigger fired before an operation has
67+
the following choices:
5168

5269
<itemizedlist>
5370
<listitem>
5471
<para>
55-
It can return a <symbol>NULL</> pointer to skip the operation
56-
for the current row (and so the row will not be
57-
inserted/updated/deleted).
72+
It can return <symbol>NULL</> to skip the operation for the
73+
current row. This instructs the executor to not perform the
74+
row-level operation that invoked the trigger (the insertion or
75+
modification of a particular table row).
5876
</para>
5977
</listitem>
6078

6179
<listitem>
6280
<para>
63-
For <command>INSERT</command> and <command>UPDATE</command>
64-
triggers only, the returned row becomes the row that will
65-
be inserted or will replace the row being updated. This
66-
allows the trigger function to modify the row being inserted or
67-
updated.
81+
For row-level <command>INSERT</command>
82+
and <command>UPDATE</command> triggers only, the returned row
83+
becomes the row that will be inserted or will replace the row
84+
being updated. This allows the trigger function to modify the
85+
row being inserted or updated.
6886
</para>
6987
</listitem>
7088
</itemizedlist>
7189

72-
A before trigger that does not intend to cause either of these
73-
behaviors must be careful to return as its result the same row that was
74-
passed in (that is, the NEW row for <command>INSERT</command> and
75-
<command>UPDATE</command> triggers, the OLD row for
90+
A row-level before trigger that does not intend to cause either of
91+
these behaviors must be careful to return as its result the same
92+
row that was passed in (that is, the <varname>NEW</varname> row
93+
for <command>INSERT</command> and <command>UPDATE</command>
94+
triggers, the <varname>OLD</varname> row for
7695
<command>DELETE</command> triggers).
7796
</para>
7897

7998
<para>
80-
The return
81-
value is ignored for triggers fired after an operation, and so
82-
they may as well return <symbol>NULL</>.
99+
The return value is ignored for row-level triggers fired after an
100+
operation, and so they may as well return <symbol>NULL</>.
83101
</para>
84102

85103
<para>
86104
If more than one trigger is defined for the same event on the same
87-
relation, the triggers will be fired in alphabetical order by trigger
88-
name. In the case of before triggers, the possibly-modified row
89-
returned by each trigger becomes the input to the next trigger.
90-
If any before trigger returns a <symbol>NULL</> pointer, the
91-
operation is abandoned and subsequent triggers are not fired.
105+
relation, the triggers will be fired in alphabetical order by
106+
trigger name. In the case of before triggers, the
107+
possibly-modified row returned by each trigger becomes the input
108+
to the next trigger. If any before trigger returns
109+
<symbol>NULL</>, the operation is abandoned and subsequent
110+
triggers are not fired.
92111
</para>
93112

94113
<para>
@@ -134,30 +153,41 @@ $PostgreSQL: pgsql/doc/src/sgml/trigger.sgml,v 1.33 2003/11/29 19:51:38 pgsql Ex
134153
is fired for. Briefly:
135154

136155
<itemizedlist>
156+
157+
<listitem>
158+
<para>
159+
Statement-level triggers follow simple visibility rules: none of
160+
the changes made by a statement are visible to statement-level
161+
triggers that are invoked before the statement, whereas all
162+
modifications are visible to statement-level after triggers.
163+
</para>
164+
</listitem>
165+
137166
<listitem>
138167
<para>
139-
The data change (insertion, update, or deletion) causing the trigger
140-
to fire is naturally
141-
<emphasis>not</emphasis> visible to SQL commands executed in a
142-
before trigger, because it hasn't happened yet.
168+
The data change (insertion, update, or deletion) causing the
169+
trigger to fire is naturally <emphasis>not</emphasis> visible
170+
to SQL commands executed in a row-level before trigger, because
171+
it hasn't happened yet.
143172
</para>
144173
</listitem>
145174

146175
<listitem>
147176
<para>
148-
However, SQL commands executed in a before trigger
149-
<emphasis>will</emphasis> see the effects of data changes
150-
for rows previously processed in the same outer command. This
151-
requires caution, since the ordering of these change events
152-
is not in general predictable; a SQL command that affects
153-
multiple rows may visit the rows in any order.
177+
However, SQL commands executed in a row-level before
178+
trigger <emphasis>will</emphasis> see the effects of data
179+
changes for rows previously processed in the same outer
180+
command. This requires caution, since the ordering of these
181+
change events is not in general predictable; a SQL command that
182+
affects multiple rows may visit the rows in any order.
154183
</para>
155184
</listitem>
156185

157186
<listitem>
158187
<para>
159-
When an after trigger is fired, all data changes made by the outer
160-
command are already complete, and are visible to executed SQL commands.
188+
When a row-level after trigger is fired, all data changes made
189+
by the outer command are already complete, and are visible to
190+
the invoked trigger function.
161191
</para>
162192
</listitem>
163193
</itemizedlist>

0 commit comments

Comments
 (0)