mirror of
https://github.com/postgres/postgres.git
synced 2025-08-31 17:02:12 +03:00
Add a WHEN clause to CREATE TRIGGER, allowing a boolean expression to be
checked to determine whether the trigger should be fired. For BEFORE triggers this is mostly a matter of spec compliance; but for AFTER triggers it can provide a noticeable performance improvement, since queuing of a deferred trigger event and re-fetching of the row(s) at end of statement can be short-circuited if the trigger does not need to be fired. Takahiro Itagaki, reviewed by KaiGai Kohei.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<!-- $PostgreSQL: pgsql/doc/src/sgml/trigger.sgml,v 1.59 2009/10/14 22:14:21 tgl Exp $ -->
|
||||
<!-- $PostgreSQL: pgsql/doc/src/sgml/trigger.sgml,v 1.60 2009/11/20 20:38:09 tgl Exp $ -->
|
||||
|
||||
<chapter id="triggers">
|
||||
<title>Triggers</title>
|
||||
@@ -140,6 +140,25 @@
|
||||
triggers are not fired.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
A trigger definition can also specify a boolean <literal>WHEN</>
|
||||
condition, which will be tested to see whether the trigger should
|
||||
be fired. In row-level triggers the <literal>WHEN</> condition can
|
||||
examine the old and/or new values of columns of the row. (Statement-level
|
||||
triggers can also have <literal>WHEN</> conditions, although the feature
|
||||
is not so useful for them.) In a before trigger, the <literal>WHEN</>
|
||||
condition is evaluated just before the function is or would be executed,
|
||||
so using <literal>WHEN</> is not materially different from testing the
|
||||
same condition at the beginning of the trigger function. However, in
|
||||
an after trigger, the <literal>WHEN</> condition is evaluated just after
|
||||
the row update occurs, and it determines whether an event is queued to
|
||||
fire the trigger at the end of statement. So when an after trigger's
|
||||
<literal>WHEN</> condition does not return true, it is not necessary
|
||||
to queue an event nor to re-fetch the row at end of statement. This
|
||||
can result in significant speedups in statements that modify many
|
||||
rows, if the trigger only needs to be fired for a few of the rows.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Typically, row before triggers are used for checking or
|
||||
modifying the data that will be inserted or updated. For example,
|
||||
@@ -497,6 +516,7 @@ typedef struct Trigger
|
||||
int16 tgnattr;
|
||||
int16 *tgattr;
|
||||
char **tgargs;
|
||||
char *tgqual;
|
||||
} Trigger;
|
||||
</programlisting>
|
||||
|
||||
|
Reference in New Issue
Block a user