Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2017371

Browse files
committed
pg_trigger's index on tgrelid is replaced by a unique index on
(tgrelid, tgname). This provides an additional check on trigger nameuniqueness per-table (which was already enforced by the code anyway).With this change, RelationBuildTriggers will read the triggers inorder by tgname, since it's scanning using this index. Since apredictable trigger ordering has been requested for some time, documentthis behavior as a feature. Also document that rules fire in nameorder, since yesterday's changes to pg_rewrite indexing cause that too.
1 parent87d0036 commit2017371

File tree

11 files changed

+225
-167
lines changed

11 files changed

+225
-167
lines changed

‎doc/src/sgml/ref/create_rule.sgml

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_rule.sgml,v 1.33 2002/03/22 19:20:39 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_rule.sgml,v 1.34 2002/04/19 16:36:08 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -22,7 +22,7 @@ PostgreSQL documentation
2222
</refsynopsisdivinfo>
2323
<synopsis>
2424
CREATE RULE <replaceable class="parameter">name</replaceable> AS ON <replaceable class="parameter">event</replaceable>
25-
TO <replaceable class="parameter">object</replaceable> [ WHERE <replaceable class="parameter">condition</replaceable> ]
25+
TO <replaceable class="parameter">table</replaceable> [ WHERE <replaceable class="parameter">condition</replaceable> ]
2626
DO [ INSTEAD ] <replaceable class="parameter">action</replaceable>
2727

2828
where <replaceable class="PARAMETER">action</replaceable> can be:
@@ -48,7 +48,8 @@ NOTHING
4848
<term><replaceable class="parameter">name</replaceable></term>
4949
<listitem>
5050
<para>
51-
The name of a rule to create.
51+
The name of a rule to create. This must be distinct from the name
52+
of any other rule for the same table.
5253
</para>
5354
</listitem>
5455
</varlistentry>
@@ -63,14 +64,11 @@ NOTHING
6364
</listitem>
6465
</varlistentry>
6566
<varlistentry>
66-
<term><replaceable class="parameter">object</replaceable></term>
67+
<term><replaceable class="parameter">table</replaceable></term>
6768
<listitem>
6869
<para>
69-
Object is either <replaceable class="parameter">table</replaceable>
70-
or <replaceable class="parameter">table</replaceable>.<replaceable
71-
class="parameter">column</replaceable>. (Currently, only the
72-
<replaceable class="parameter">table</replaceable> form is
73-
actually implemented.)
70+
The name (optionally schema-qualified) of the table or view the rule
71+
applies to.
7472
</para>
7573
</listitem>
7674
</varlistentry>
@@ -103,8 +101,7 @@ NOTHING
103101
Within the <replaceable class="parameter">condition</replaceable>
104102
and <replaceable class="PARAMETER">action</replaceable>, the special
105103
table names <literal>new</literal> and <literal>old</literal> may be
106-
used to refer to values in the referenced table (the
107-
<replaceable class="parameter">object</replaceable>).
104+
used to refer to values in the referenced table.
108105
<literal>new</literal> is valid in ON INSERT and ON UPDATE rules
109106
to refer to the new row being inserted or updated.
110107
<literal>old</literal> is valid in ON UPDATE and ON DELETE
@@ -159,7 +156,7 @@ CREATE
159156
accessed, inserted, updated, or deleted, there is an old instance (for
160157
selects, updates and deletes) and a new instance (for inserts and
161158
updates). All the rules for the given event type and the given target
162-
object (table) are examined,inan unspecified order. If the
159+
table are examined successively (inorder by name). If the
163160
<replaceable class="parameter">condition</replaceable> specified in the
164161
WHERE clause (if any) is true, the
165162
<replaceable class="parameter">action</replaceable> part of the rule is
@@ -178,8 +175,7 @@ CREATE
178175
The <replaceable class="parameter">action</replaceable> part of the
179176
rule can consist of one or more queries. To write multiple queries,
180177
surround them with parentheses. Such queries will be performed in the
181-
specified order (whereas there are no guarantees about the execution
182-
order of multiple rules for an object). The <replaceable
178+
specified order. The <replaceable
183179
class="parameter">action</replaceable> can also be NOTHING indicating
184180
no action. Thus, a DO INSTEAD NOTHING rule suppresses the original
185181
query from executing (when its condition is true); a DO NOTHING rule
@@ -191,6 +187,20 @@ CREATE
191187
executes with the same command and transaction identifier as the user
192188
command that caused activation.
193189
</para>
190+
191+
<para>
192+
It is important to realize that a rule is really a query transformation
193+
mechanism, or query macro. The entire query is processed to convert it
194+
into a series of queries that include the rule actions. This occurs
195+
before evaluation of the query starts. So, conditional rules are
196+
handled by adding the rule condition to the WHERE clause of the action(s)
197+
derived from the rule. The above description of a rule as an operation
198+
that executes for each row is thus somewhat misleading. If you actually
199+
want an operation that fires independently for each physical row, you
200+
probably want to use a trigger not a rule. Rules are most useful for
201+
situations that call for transforming entire queries independently of
202+
the specific data being handled.
203+
</para>
194204

195205
<refsect2 id="R2-SQL-CREATERULE-3">
196206
<refsect2info>
@@ -202,7 +212,7 @@ CREATE
202212
<para>
203213
Presently, ON SELECT rules must be unconditional INSTEAD rules and must
204214
have actions that consist of a single SELECT query. Thus, an ON SELECT
205-
rule effectively turns theobjecttable into a view, whose visible
215+
rule effectively turns the table into a view, whose visible
206216
contents are the rows returned by the rule's SELECT query rather than
207217
whatever had been stored in the table (if anything). It is considered
208218
better style to write a CREATE VIEW command than to create a real table

‎doc/src/sgml/ref/create_trigger.sgml

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_trigger.sgml,v 1.22 2002/01/20 22:19:56 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_trigger.sgml,v 1.23 2002/04/19 16:36:08 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -44,31 +44,45 @@ CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTE
4444
<term><replaceable class="parameter">name</replaceable></term>
4545
<listitem>
4646
<para>
47-
The name to give the new trigger.
47+
The name to give the new trigger. This must be distinct from the name
48+
of any other trigger for the same table.
4849
</para>
4950
</listitem>
5051
</varlistentry>
5152
<varlistentry>
52-
<term><replaceable class="parameter">table</replaceable></term>
53+
<term><replaceable class="parameter">event</replaceable></term>
5354
<listitem>
5455
<para>
55-
The nameofan existing table.
56+
OneofINSERT, DELETE or UPDATE.
5657
</para>
5758
</listitem>
5859
</varlistentry>
5960
<varlistentry>
60-
<term><replaceable class="parameter">event</replaceable></term>
61+
<term><replaceable class="parameter">table</replaceable></term>
6162
<listitem>
6263
<para>
63-
OneofINSERT, DELETE or UPDATE.
64+
The name (optionally schema-qualified)ofthe table the trigger is for.
6465
</para>
6566
</listitem>
6667
</varlistentry>
6768
<varlistentry>
6869
<term><replaceable class="parameter">func</replaceable></term>
6970
<listitem>
7071
<para>
71-
A user-supplied function.
72+
A user-supplied function that is declared as taking no arguments
73+
and returning type <literal>opaque</>.
74+
</para>
75+
</listitem>
76+
</varlistentry>
77+
<varlistentry>
78+
<term><replaceable class="parameter">arguments</replaceable></term>
79+
<listitem>
80+
<para>
81+
An optional comma-separated list of arguments to be provided to the
82+
function when the trigger is executed, along with the standard trigger
83+
data such as old and new tuple contents. The arguments are literal
84+
string constants. Simple names and numeric constants may be written
85+
here too, but they will all be converted to strings.
7286
</para>
7387
</listitem>
7488
</varlistentry>
@@ -130,6 +144,12 @@ CREATE
130144
after the event, all changes, including the last insertion, update,
131145
or deletion, are <quote>visible</quote> to the trigger.
132146
</para>
147+
148+
<para>
149+
If multiple triggers of the same kind are defined for the same event,
150+
they will be fired in alphabetical order by name.
151+
</para>
152+
133153
<para>
134154
<command>SELECT</command> does not modify any rows so you can not
135155
create <command>SELECT</command> triggers. Rules and views are more
@@ -262,6 +282,12 @@ CREATE TABLE distributors (
262282
</listitem>
263283
</itemizedlist>
264284
</para>
285+
286+
<para>
287+
SQL99 specifies that multiple triggers should be fired in
288+
time-of-creation order. <productname>PostgreSQL</productname>
289+
uses name order, which was judged more convenient to work with.
290+
</para>
265291
</listitem>
266292
</varlistentry>
267293
</variablelist>

‎doc/src/sgml/trigger.sgml

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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>
3640
CREATE 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 argumentto the<command>DROP TRIGGER</command> command.
55+
Thetrigger must have anamedistinct from all other triggers on
56+
the same table. The name is needed
57+
if you ever havetodeletethetrigger.
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 determinesonwhat event(s) will trigger
79+
The next element of the command determines what event(s) will trigger
7680
the 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>
9599
The FOR EACH clause determines whether the trigger is fired for each
96100
affected 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 becalled.
106111
</para>
107112
</listitem>
108113
</varlistentry>
@@ -112,32 +117,32 @@ CREATE TRIGGER <replaceable>trigger</replaceable> [ BEFORE | AFTER ] [ INSERT |
112117
<listitem>
113118
<para>
114119
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 requirementstocallthesamefunction.
120+
This is either empty or a list of one or more simple literal
121+
constants (which will be passedto the function as strings).
117122
</para>
118123

119124
<para>
120-
Also, <replaceable>procedure</replaceable>
121-
may be used for triggeringdifferent 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 couldbea general
127-
function that takes as its arguments two field names and putsthecurrent
128-
user in one and the current timestamp in the other. This allows triggers to
129-
be written on INSERT eventsto automatically track creation of records in a
130-
transaction table forexample. It could also be usedas a <quote>last updated</>
131-
function if used inan UPDATEevent.
125+
The purpose of including arguments in the trigger definition
126+
is to allowdifferent
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 wouldbeindependent of
132+
the specific table it is triggering on. Sothesame 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 tracklast-update events if
136+
defined asan 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 UPDATEoperation
144+
Trigger functions returnaHeapTuple to the calling Executor.The return
145+
valueis 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>
327340
is a pointer to structure describing the triggered relation. Look at
328341
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
330343
tuples) and tg_relation->rd_rel->relname (relation's name. This is not
331344
char*, but NameData. Use SPI_getrelname(tg_relation) to get char* if
332345
you need a copy of name).

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp