mirror of
https://github.com/postgres/postgres.git
synced 2025-07-31 22:04:40 +03:00
Provide the OR REPLACE option for CREATE TRIGGER.
This is mostly straightforward. However, we disallow replacing constraint triggers or changing the is-constraint property; perhaps that can be added later, but the complexity versus benefit tradeoff doesn't look very good. Also, no special thought is taken here for whether replacing an existing trigger should result in changes to queued-but-not-fired trigger actions. We just document that if you're surprised by the results, too bad, don't do that. (Note that any such pending trigger activity would have to be within the current session.) Takamichi Osumi, reviewed at various times by Surafel Temesgen, Peter Smith, and myself Discussion: https://postgr.es/m/0DDF369B45A1B44B8A687ED43F06557C010BC362@G01JPEXMBYT03
This commit is contained in:
@ -26,7 +26,7 @@ PostgreSQL documentation
|
||||
|
||||
<refsynopsisdiv>
|
||||
<synopsis>
|
||||
CREATE [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable> { BEFORE | AFTER | INSTEAD OF } { <replaceable class="parameter">event</replaceable> [ OR ... ] }
|
||||
CREATE [ OR REPLACE ] [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable> { BEFORE | AFTER | INSTEAD OF } { <replaceable class="parameter">event</replaceable> [ OR ... ] }
|
||||
ON <replaceable class="parameter">table_name</replaceable>
|
||||
[ FROM <replaceable class="parameter">referenced_table_name</replaceable> ]
|
||||
[ NOT DEFERRABLE | [ DEFERRABLE ] [ INITIALLY IMMEDIATE | INITIALLY DEFERRED ] ]
|
||||
@ -48,13 +48,21 @@ CREATE [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable>
|
||||
<title>Description</title>
|
||||
|
||||
<para>
|
||||
<command>CREATE TRIGGER</command> creates a new trigger. The
|
||||
<command>CREATE TRIGGER</command> creates a new trigger.
|
||||
<command>CREATE OR REPLACE TRIGGER</command> will either create a
|
||||
new trigger, or replace an existing trigger. The
|
||||
trigger will be associated with the specified table, view, or foreign table
|
||||
and will execute the specified
|
||||
function <replaceable class="parameter">function_name</replaceable> when
|
||||
certain operations are performed on that table.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
To replace the current definition of an existing trigger, use
|
||||
<command>CREATE OR REPLACE TRIGGER</command>, specifying the existing
|
||||
trigger's name and parent table. All other properties are replaced.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
The trigger can be specified to fire before the
|
||||
operation is attempted on a row (before constraints are checked and
|
||||
@ -436,7 +444,7 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
|
||||
<title>Notes</title>
|
||||
|
||||
<para>
|
||||
To create a trigger on a table, the user must have the
|
||||
To create or replace a trigger on a table, the user must have the
|
||||
<literal>TRIGGER</literal> privilege on the table. The user must
|
||||
also have <literal>EXECUTE</literal> privilege on the trigger function.
|
||||
</para>
|
||||
@ -445,6 +453,17 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
|
||||
Use <link linkend="sql-droptrigger"><command>DROP TRIGGER</command></link> to remove a trigger.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Creating a row-level trigger on a partitioned table will cause an
|
||||
identical <quote>clone</quote> trigger to be created on each of its
|
||||
existing partitions; and any partitions created or attached later will have
|
||||
an identical trigger, too. If there is a conflictingly-named trigger on a
|
||||
child partition already, an error occurs unless <command>CREATE OR REPLACE
|
||||
TRIGGER</command> is used, in which case that trigger is replaced with a
|
||||
clone trigger. When a partition is detached from its parent, its clone
|
||||
triggers are removed.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
A column-specific trigger (one defined using the <literal>UPDATE OF
|
||||
<replaceable>column_name</replaceable></literal> syntax) will fire when any
|
||||
@ -457,12 +476,6 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
|
||||
value did not change.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
There are a few built-in trigger functions that can be used to
|
||||
solve common problems without having to write your own trigger code;
|
||||
see <xref linkend="functions-trigger"/>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
In a <literal>BEFORE</literal> trigger, the <literal>WHEN</literal> condition is
|
||||
evaluated just before the function is or would be executed, so using
|
||||
@ -528,14 +541,6 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
|
||||
the ones that are fired.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Creating a row-level trigger on a partitioned table will cause identical
|
||||
triggers to be created in all its existing partitions; and any partitions
|
||||
created or attached later will contain an identical trigger, too.
|
||||
If the partition is detached from its parent, the trigger is removed.
|
||||
Triggers on partitioned tables may not be <literal>INSTEAD OF</literal>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Modifying a partitioned table or a table with inheritance children fires
|
||||
statement-level triggers attached to the explicitly named table, but not
|
||||
@ -546,9 +551,32 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
|
||||
named by a <literal>REFERENCING</literal> clause, then before and after
|
||||
images of rows are visible from all affected partitions or child tables.
|
||||
In the case of inheritance children, the row images include only columns
|
||||
that are present in the table that the trigger is attached to. Currently,
|
||||
row-level triggers with transition relations cannot be defined on
|
||||
partitions or inheritance child tables.
|
||||
that are present in the table that the trigger is attached to.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Currently, row-level triggers with transition relations cannot be defined
|
||||
on partitions or inheritance child tables. Also, triggers on partitioned
|
||||
tables may not be <literal>INSTEAD OF</literal>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Currently, the <literal>OR REPLACE</literal> option is not supported for
|
||||
constraint triggers.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Replacing an existing trigger within a transaction that has already
|
||||
performed updating actions on the trigger's table is not recommended.
|
||||
Trigger firing decisions, or portions of firing decisions, that have
|
||||
already been made will not be reconsidered, so the effects could be
|
||||
surprising.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
There are a few built-in trigger functions that can be used to
|
||||
solve common problems without having to write your own trigger code;
|
||||
see <xref linkend="functions-trigger"/>.
|
||||
</para>
|
||||
</refsect1>
|
||||
|
||||
@ -566,11 +594,12 @@ CREATE TRIGGER check_update
|
||||
EXECUTE FUNCTION check_account_update();
|
||||
</programlisting>
|
||||
|
||||
The same, but only execute the function if column <literal>balance</literal>
|
||||
is specified as a target in the <command>UPDATE</command> command:
|
||||
Modify that trigger definition to only execute the function if
|
||||
column <literal>balance</literal> is specified as a target in
|
||||
the <command>UPDATE</command> command:
|
||||
|
||||
<programlisting>
|
||||
CREATE TRIGGER check_update
|
||||
CREATE OR REPLACE TRIGGER check_update
|
||||
BEFORE UPDATE OF balance ON accounts
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION check_account_update();
|
||||
@ -728,6 +757,7 @@ CREATE TRIGGER paired_items_update
|
||||
<command>CREATE CONSTRAINT TRIGGER</command> is a
|
||||
<productname>PostgreSQL</productname> extension of the <acronym>SQL</acronym>
|
||||
standard.
|
||||
So is the <literal>OR REPLACE</literal> option.
|
||||
</para>
|
||||
|
||||
</refsect1>
|
||||
|
Reference in New Issue
Block a user