Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b1e48cc

Browse filesBrowse 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 af5fbb1 commit b1e48cc
Copy full SHA for b1e48cc

File tree

2 files changed

+22
-3
lines changed
Filter options

2 files changed

+22
-3
lines changed

‎doc/src/sgml/ref/create_trigger.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/create_trigger.sgml
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,19 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
406406
rows.
407407
</para>
408408

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

‎doc/src/sgml/trigger.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/trigger.sgml
+9-3Lines changed: 9 additions & 3 deletions
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</> 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.