8000 Doc: note that statement-level view triggers require an INSTEAD OF tr… · divag711/postgres@60c23ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 60c23ff

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 f1f7a85 commit 60c23ff

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

< 8000 code>‎doc/src/sgml/ref/create_trigger.sgml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,19 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
405405
rows.
406406
</para>
407407

408+
<para>
409+
Statement-level triggers on a view are fired only if the action on the
410+
view is handled by a row-level <literal>INSTEAD OF</literal> trigger.
411+
If the action is handled by an <literal>INSTEAD</literal> rule, then
412+
whatever statements are emitted by the rule are executed in place of the
413+
original statement naming the view, so that the triggers that will be
414+
fired are those on tables named in the replacement statements.
415+
Similarly, if the view is automatically updatable, then the action is
416+
handled by automatically rewriting the statement into an action on the
417+
view's base table, so that the base table's statement-level triggers are
418+
the ones that are fired.
419+
</para>
420+
408421
<para>
409422
In <productname>PostgreSQL</productname> versions before 7.3, it was
410423
necessary to declare trigger functions as returning the placeholder

doc/src/sgml/trigger.sgml

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

6672
<para>

0 commit comments

Comments
 (0)
0