Skip to content

Commit 7b544c4

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 59743de commit 7b544c4

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

doc/src/sgml/ref/create_trigger.sgml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,19 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
409409
rows.
410410
</para>
411411

412+
<para>
413+
Statement-level triggers on a view are fired only if the action on the
414+
view is handled by a row-level <literal>INSTEAD OF</literal> trigger.
415+
If the action is handled by an <literal>INSTEAD</literal> rule, then
416+
whatever statements are emitted by the rule are executed in place of the
417+
original statement naming the view, so that the triggers that will be
418+
fired are those on tables named in the replacement statements.
419+
Similarly, if the view is automatically updatable, then the action is
420+
handled by automatically rewriting the statement into an action on the
421+
view's base table, so that the base table's statement-level triggers are
422+
the ones that are fired.
423+
</para>
424+
412425
<para>
413426
In <productname>PostgreSQL</productname> versions before 7.3, it was
414427
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
@@ -56,15 +56,21 @@
5656
<para>
5757
On views, triggers can be defined to execute instead of
5858
<command>INSERT</command>, <command>UPDATE</command>, or
59-
<command>DELETE</command> operations. <literal>INSTEAD OF</> triggers
59+
<command>DELETE</command> operations.
60+
Such <literal>INSTEAD OF</literal> triggers
6061
are fired once for each row that needs to be modified in the view.
6162
It is the responsibility of the
62-
trigger's function to perform the necessary modifications to the
63-
underlying base tables and, where appropriate, return the modified
63+
trigger's function to perform the necessary modifications to the view's
64+
underlying base table(s) and, where appropriate, return the modified
6465
row as it will appear in the view. Triggers on views can also be defined
6566
to execute once per <acronym>SQL</acronym> statement, before or after
6667
<command>INSERT</command>, <command>UPDATE</command>, or
6768
<command>DELETE</command> operations.
69+
However, such triggers are fired only if there is also
70+
an <literal>INSTEAD OF</literal> trigger on the view. Otherwise,
71+
any statement targeting the view must be rewritten into a statement
72+
affecting its underlying base table(s), and then the triggers
73+
that will be fired are the ones attached to the base table(s).
6874
</para>
6975

7076
<para>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy