@@ -26,7 +26,7 @@ PostgreSQL documentation
2626
2727 <refsynopsisdiv>
2828<synopsis>
29- CREATE [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable> { BEFORE | AFTER | INSTEAD OF } { <replaceable class="parameter">event</replaceable> [ OR ... ] }
29+ CREATE [ OR REPLACE ] [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable> { BEFORE | AFTER | INSTEAD OF } { <replaceable class="parameter">event</replaceable> [ OR ... ] }
3030 ON <replaceable class="parameter">table_name</replaceable>
3131 [ FROM <replaceable class="parameter">referenced_table_name</replaceable> ]
3232 [ NOT DEFERRABLE | [ DEFERRABLE ] [ INITIALLY IMMEDIATE | INITIALLY DEFERRED ] ]
@@ -48,13 +48,21 @@ CREATE [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable>
4848 <title>Description</title>
4949
5050 <para>
51- <command>CREATE TRIGGER</command> creates a new trigger. The
51+ <command>CREATE TRIGGER</command> creates a new trigger.
52+ <command>CREATE OR REPLACE TRIGGER</command> will either create a
53+ new trigger, or replace an existing trigger. The
5254 trigger will be associated with the specified table, view, or foreign table
5355 and will execute the specified
5456 function <replaceable class="parameter">function_name</replaceable> when
5557 certain operations are performed on that table.
5658 </para>
5759
60+ <para>
61+ To replace the current definition of an existing trigger, use
62+ <command>CREATE OR REPLACE TRIGGER</command>, specifying the existing
63+ trigger's name and parent table. All other properties are replaced.
64+ </para>
65+
5866 <para>
5967 The trigger can be specified to fire before the
6068 operation is attempted on a row (before constraints are checked and
@@ -436,7 +444,7 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
436444 <title>Notes</title>
437445
438446 <para>
439- To create a trigger on a table, the user must have the
447+ To create or replace a trigger on a table, the user must have the
440448 <literal>TRIGGER</literal> privilege on the table. The user must
441449 also have <literal>EXECUTE</literal> privilege on the trigger function.
442450 </para>
@@ -445,6 +453,17 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
445453 Use <link linkend="sql-droptrigger"><command>DROP TRIGGER</command></link> to remove a trigger.
446454 </para>
447455
456+ <para>
457+ Creating a row-level trigger on a partitioned table will cause an
458+ identical <quote>clone</quote> trigger to be created on each of its
459+ existing partitions; and any partitions created or attached later will have
460+ an identical trigger, too. If there is a conflictingly-named trigger on a
461+ child partition already, an error occurs unless <command>CREATE OR REPLACE
462+ TRIGGER</command> is used, in which case that trigger is replaced with a
463+ clone trigger. When a partition is detached from its parent, its clone
464+ triggers are removed.
465+ </para>
466+
448467 <para>
449468 A column-specific trigger (one defined using the <literal>UPDATE OF
450469 <replaceable>column_name</replaceable></literal> syntax) will fire when any
@@ -457,12 +476,6 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
457476 value did not change.
458477 </para>
459478
460- <para>
461- There are a few built-in trigger functions that can be used to
462- solve common problems without having to write your own trigger code;
463- see <xref linkend="functions-trigger"/>.
464- </para>
465-
466479 <para>
467480 In a <literal>BEFORE</literal> trigger, the <literal>WHEN</literal> condition is
468481 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</
528541 the ones that are fired.
529542 </para>
530543
531- <para>
532- Creating a row-level trigger on a partitioned table will cause identical
533- triggers to be created in all its existing partitions; and any partitions
534- created or attached later will contain an identical trigger, too.
535- If the partition is detached from its parent, the trigger is removed.
536- Triggers on partitioned tables may not be <literal>INSTEAD OF</literal>.
537- </para>
538-
539544 <para>
540545 Modifying a partitioned table or a table with inheritance children fires
541546 statement-level triggers attached to the explicitly named table, but not
@@ -546,9 +551,32 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
546551 named by a <literal>REFERENCING</literal> clause, then before and after
547552 images of rows are visible from all affected partitions or child tables.
548553 In the case of inheritance children, the row images include only columns
549- that are present in the table that the trigger is attached to. Currently,
550- row-level triggers with transition relations cannot be defined on
551- partitions or inheritance child tables.
554+ that are present in the table that the trigger is attached to.
555+ </para>
556+
557+ <para>
558+ Currently, row-level triggers with transition relations cannot be defined
559+ on partitions or inheritance child tables. Also, triggers on partitioned
560+ tables may not be <literal>INSTEAD OF</literal>.
561+ </para>
562+
563+ <para>
564+ Currently, the <literal>OR REPLACE</literal> option is not supported for
565+ constraint triggers.
566+ </para>
567+
568+ <para>
569+ Replacing an existing trigger within a transaction that has already
570+ performed updating actions on the trigger's table is not recommended.
571+ Trigger firing decisions, or portions of firing decisions, that have
572+ already been made will not be reconsidered, so the effects could be
573+ surprising.
574+ </para>
575+
576+ <para>
577+ There are a few built-in trigger functions that can be used to
578+ solve common problems without having to write your own trigger code;
579+ see <xref linkend="functions-trigger"/>.
552580 </para>
553581 </refsect1>
554582
@@ -566,11 +594,12 @@ CREATE TRIGGER check_update
566594 EXECUTE FUNCTION check_account_update();
567595</programlisting>
568596
569- The same, but only execute the function if column <literal>balance</literal>
570- is specified as a target in the <command>UPDATE</command> command:
597+ Modify that trigger definition to only execute the function if
598+ column <literal>balance</literal> is specified as a target in
599+ the <command>UPDATE</command> command:
571600
572601<programlisting>
573- CREATE TRIGGER check_update
602+ CREATE OR REPLACE TRIGGER check_update
574603 BEFORE UPDATE OF balance ON accounts
575604 FOR EACH ROW
576605 EXECUTE FUNCTION check_account_update();
@@ -728,6 +757,7 @@ CREATE TRIGGER paired_items_update
728757 <command>CREATE CONSTRAINT TRIGGER</command> is a
729758 <productname>PostgreSQL</productname> extension of the <acronym>SQL</acronym>
730759 standard.
760+ So is the <literal>OR REPLACE</literal> option.
731761 </para>
732762
733763 </refsect1>
0 commit comments