@@ -26,7 +26,7 @@ PostgreSQL documentation
26
26
27
27
<refsynopsisdiv>
28
28
<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 ... ] }
30
30
ON <replaceable class="parameter">table_name</replaceable>
31
31
[ FROM <replaceable class="parameter">referenced_table_name</replaceable> ]
32
32
[ NOT DEFERRABLE | [ DEFERRABLE ] [ INITIALLY IMMEDIATE | INITIALLY DEFERRED ] ]
@@ -48,13 +48,21 @@ CREATE [ CONSTRAINT ] TRIGGER <replaceable class="parameter">name</replaceable>
48
48
<title>Description</title>
49
49
50
50
<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
52
54
trigger will be associated with the specified table, view, or foreign table
53
55
and will execute the specified
54
56
function <replaceable class="parameter">function_name</replaceable> when
55
57
certain operations are performed on that table.
56
58
</para>
57
59
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
+
58
66
<para>
59
67
The trigger can be specified to fire before the
60
68
operation is attempted on a row (before constraints are checked and
@@ -436,7 +444,7 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
436
444
<title>Notes</title>
437
445
438
446
<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
440
448
<literal>TRIGGER</literal> privilege on the table. The user must
441
449
also have <literal>EXECUTE</literal> privilege on the trigger function.
442
450
</para>
@@ -445,6 +453,17 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
445
453
Use <link linkend="sql-droptrigger"><command>DROP TRIGGER</command></link> to remove a trigger.
446
454
</para>
447
455
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
+
448
467
<para>
449
468
A column-specific trigger (one defined using the <literal>UPDATE OF
450
469
<replaceable>column_name</replaceable></literal> syntax) will fire when any
@@ -457,12 +476,6 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
457
476
value did not change.
458
477
</para>
459
478
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
-
466
479
<para>
467
480
In a <literal>BEFORE</literal> trigger, the <literal>WHEN</literal> condition is
468
481
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</
528
541
the ones that are fired.
529
542
</para>
530
543
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
-
539
544
<para>
540
545
Modifying a partitioned table or a table with inheritance children fires
541
546
statement-level triggers attached to the explicitly named table, but not
@@ -546,9 +551,32 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
546
551
named by a <literal>REFERENCING</literal> clause, then before and after
547
552
images of rows are visible from all affected partitions or child tables.
548
553
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"/>.
552
580
</para>
553
581
</refsect1>
554
582
@@ -566,11 +594,12 @@ CREATE TRIGGER check_update
566
594
EXECUTE FUNCTION check_account_update();
567
595
</programlisting>
568
596
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:
571
600
572
601
<programlisting>
573
- CREATE TRIGGER check_update
602
+ CREATE OR REPLACE TRIGGER check_update
574
603
BEFORE UPDATE OF balance ON accounts
575
604
FOR EACH ROW
576
605
EXECUTE FUNCTION check_account_update();
@@ -728,6 +757,7 @@ CREATE TRIGGER paired_items_update
728
757
<command>CREATE CONSTRAINT TRIGGER</command> is a
729
758
<productname>PostgreSQL</productname> extension of the <acronym>SQL</acronym>
730
759
standard.
760
+ So is the <literal>OR REPLACE</literal> option.
731
761
</para>
732
762
733
763
</refsect1>
0 commit comments