mirror of
https://github.com/postgres/postgres.git
synced 2025-09-03 15:22:11 +03:00
This patch implements FOR EACH STATEMENT triggers, per my email to
-hackers a couple days ago. Notes/caveats: - added regression tests for the new functionality, all regression tests pass on my machine - added pg_dump support - updated PL/PgSQL to support per-statement triggers; didn't look at the other procedural languages. - there's (even) more code duplication in trigger.c than there was previously. Any suggestions on how to refactor the ExecXXXTriggers() functions to reuse more code would be welcome -- I took a brief look at it, but couldn't see an easy way to do it (there are several subtly-different versions of the code in question) - updated the documentation. I also took the liberty of removing a big chunk of duplicated syntax documentation in the Programmer's Guide on triggers, and moving that information to the CREATE TRIGGER reference page. - I also included some spelling fixes and similar small cleanups I noticed while making the changes. If you'd like me to split those into a separate patch, let me know. Neil Conway
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<!--
|
||||
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_trigger.sgml,v 1.29 2002/11/21 23:34:43 petere Exp $
|
||||
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_trigger.sgml,v 1.30 2002/11/23 03:59:06 momjian Exp $
|
||||
PostgreSQL documentation
|
||||
-->
|
||||
|
||||
@@ -21,8 +21,9 @@ PostgreSQL documentation
|
||||
<date>2000-03-25</date>
|
||||
</refsynopsisdivinfo>
|
||||
<synopsis>
|
||||
CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTER } { <replaceable class="PARAMETER">event</replaceable> [OR ...] }
|
||||
ON <replaceable class="PARAMETER">table</replaceable> FOR EACH { ROW | STATEMENT }
|
||||
CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> {
|
||||
BEFORE | AFTER } { <replaceable class="PARAMETER">event</replaceable> [ OR ... ] }
|
||||
ON <replaceable class="PARAMETER">table</replaceable> [ FOR EACH { ROW | STATEMENT } ]
|
||||
EXECUTE PROCEDURE <replaceable class="PARAMETER">func</replaceable> ( <replaceable class="PARAMETER">arguments</replaceable> )
|
||||
</synopsis>
|
||||
|
||||
@@ -45,11 +46,26 @@ CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTE
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term>BEFORE</term>
|
||||
<term>AFTER</term>
|
||||
<listitem>
|
||||
<para>
|
||||
Determines whether the function is called before or after the
|
||||
event.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><replaceable class="parameter">event</replaceable></term>
|
||||
<listitem>
|
||||
<para>
|
||||
One of INSERT, DELETE or UPDATE.
|
||||
One of <command>INSERT</command>, <command>DELETE</command> or
|
||||
<command>UPDATE</command>; this specifies the event that will
|
||||
fire the trigger. Multiple events can be specified using
|
||||
<literal>OR</literal>.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
@@ -57,10 +73,26 @@ CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTE
|
||||
<term><replaceable class="parameter">table</replaceable></term>
|
||||
<listitem>
|
||||
<para>
|
||||
The name (optionally schema-qualified) of the table the trigger is for.
|
||||
The name (optionally schema-qualified) of the table the
|
||||
trigger is for.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term>FOR EACH ROW</term>
|
||||
<term>FOR EACH STATEMENT</term>
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
This specifies whether the trigger procedure should be fired
|
||||
once for every row affected by the trigger event, or just once
|
||||
per SQL statement. If neither is specified, <literal>FOR EACH
|
||||
STATEMENT</literal> is the default.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><replaceable class="parameter">func</replaceable></term>
|
||||
<listitem>
|
||||
@@ -74,11 +106,15 @@ CREATE TRIGGER <replaceable class="PARAMETER">name</replaceable> { BEFORE | AFTE
|
||||
<term><replaceable class="parameter">arguments</replaceable></term>
|
||||
<listitem>
|
||||
<para>
|
||||
An optional comma-separated list of arguments to be provided to the
|
||||
function when the trigger is executed, along with the standard trigger
|
||||
data such as old and new tuple contents. The arguments are literal
|
||||
string constants. Simple names and numeric constants may be written
|
||||
here too, but they will all be converted to strings.
|
||||
An optional comma-separated list of arguments to be provided to
|
||||
the function when the trigger is executed, along with the standard
|
||||
trigger data such as old and new tuple contents. The arguments
|
||||
are literal string constants. Simple names and numeric constants
|
||||
may be written here too, but they will all be converted to
|
||||
strings. Note that these arguments are not provided as normal
|
||||
function parameters (since a trigger procedure must be declared to
|
||||
take zero parameters), but are instead accessed through the
|
||||
<literal>TG_ARGV</literal> array.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
@@ -121,7 +157,7 @@ CREATE TRIGGER
|
||||
|
||||
<para>
|
||||
<command>CREATE TRIGGER</command> will enter a new trigger into the current
|
||||
data base. The trigger will be associated with the relation
|
||||
database. The trigger will be associated with the relation
|
||||
<replaceable class="parameter">table</replaceable> and will execute
|
||||
the specified function <replaceable class="parameter">func</replaceable>.
|
||||
</para>
|
||||
@@ -141,15 +177,27 @@ CREATE TRIGGER
|
||||
or deletion, are <quote>visible</quote> to the trigger.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
A trigger that executes <literal>FOR EACH ROW</literal> of the
|
||||
specified operation is called once for every row that the operation
|
||||
modifies. For example, a <command>DELETE</command> that affects 10
|
||||
rows will cause any <literal>ON DELETE</literal> triggers on the
|
||||
target relation to be called 10 separate times, once for each
|
||||
deleted tuple. In contrast, a trigger that executes <literal>FOR
|
||||
EACH STATEMENT</literal> of the specified operation only executes
|
||||
once for any given operation, regardless of how many rows it
|
||||
modifies.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
If multiple triggers of the same kind are defined for the same event,
|
||||
they will be fired in alphabetical order by name.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
<command>SELECT</command> does not modify any rows so you can not
|
||||
create <command>SELECT</command> triggers. Rules and views are more
|
||||
appropriate in such cases.
|
||||
<command>SELECT</command> does not modify any rows so you can not
|
||||
create <command>SELECT</command> triggers. Rules and views are more
|
||||
appropriate in such cases.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
@@ -176,10 +224,6 @@ CREATE TRIGGER
|
||||
change the function's declared return type to <type>trigger</>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
As of the current release, <literal>STATEMENT</literal> triggers are not implemented.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Refer to the <xref linkend="sql-droptrigger" endterm="sql-droptrigger-title"> command for
|
||||
information on how to remove triggers.
|
||||
@@ -268,13 +312,6 @@ CREATE TABLE distributors (
|
||||
</para>
|
||||
</listitem>
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
<productname>PostgreSQL</productname> only has row-level
|
||||
triggers, no statement-level triggers.
|
||||
</para>
|
||||
</listitem>
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
<productname>PostgreSQL</productname> only allows the
|
||||
|
Reference in New Issue
Block a user