Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a467832

Browse files
committed
Doc: note that statement-level view triggers require an INSTEAD OF trigger.
If a view lacks an INSTEAD OF trigger, DML on it can only work by rewriting the command into a command on the underlying base table(s). Then we will fire triggers attached to those table(s), not those for the view. This seems appropriate from a consistency standpoint, but nowhere was the behavior explicitly documented, so let's do that. There was some discussion of throwing an error or warning if a statement trigger is created on a view without creating a row INSTEAD OF trigger. But a simple implementation of that would result in dump/restore ordering hazards. Given that it's been like this all along, and we hadn't heard a complaint till now, a documentation improvement seems sufficient. Per bug #15106 from Pu Qun. Back-patch to all supported branches. Discussion: https://postgr.es/m/152083391168.1215.16892140713507052796@wrigleys.postgresql.org
1 parent 8d2814f commit a467832

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

doc/src/sgml/ref/create_trigger.sgml

+13
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,19 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
499499
each affected row in a transition relation once and only once.
500500
</para>
501501

502+
<para>
503+
Statement-level triggers on a view are fired only if the action on the
504+
view is handled by a row-level <literal>INSTEAD OF</literal> trigger.
505+
If the action is handled by an <literal>INSTEAD</literal> rule, then
506+
whatever statements are emitted by the rule are executed in place of the
507+
original statement naming the view, so that the triggers that will be
508+
fired are those on tables named in the replacement statements.
509+
Similarly, if the view is automatically updatable, then the action is
510+
handled by automatically rewriting the statement into an action on the
511+
view's base table, so that the base table's statement-level triggers are
512+
the ones that are fired.
513+
</para>
514+
502515
<para>
503516
Modifying a partitioned table or a table with inheritance children fires
504517
statement-level triggers attached to the explicitly named table, but not

doc/src/sgml/trigger.sgml

+9-3
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,21 @@
5353
<para>
5454
On views, triggers can be defined to execute instead of
5555
<command>INSERT</command>, <command>UPDATE</command>, or
56-
<command>DELETE</command> operations. <literal>INSTEAD OF</literal> triggers
56+
<command>DELETE</command> operations.
57+
Such <literal>INSTEAD OF</literal> triggers
5758
are fired once for each row that needs to be modified in the view.
5859
It is the responsibility of the
59-
trigger's function to perform the necessary modifications to the
60-
underlying base tables and, where appropriate, return the modified
60+
trigger's function to perform the necessary modifications to the view's
61+
underlying base table(s) and, where appropriate, return the modified
6162
row as it will appear in the view. Triggers on views can also be defined
6263
to execute once per <acronym>SQL</acronym> statement, before or after
6364
<command>INSERT</command>, <command>UPDATE</command>, or
6465
<command>DELETE</command> operations.
66+
However, such triggers are fired only if there is also
67+
an <literal>INSTEAD OF</literal> trigger on the view. Otherwise,
68+
any statement targeting the view must be rewritten into a statement
69+
affecting its underlying base table(s), and then the triggers
70+
that will be fired are the ones attached to the base table(s).
6571
</para>
6672

6773
<para>

0 commit comments

Comments
 (0)