summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/trigger-datachanges.html
blob: c2ba36e9580a50f68dddaed24aa744e75c2b6e59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>39.2. Visibility of Data Changes</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="trigger-definition.html" title="39.1. Overview of Trigger Behavior" /><link rel="next" href="trigger-interface.html" title="39.3. Writing Trigger Functions in C" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">39.2. Visibility of Data Changes</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="trigger-definition.html" title="39.1. Overview of Trigger Behavior">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="triggers.html" title="Chapter 39. Triggers">Up</a></td><th width="60%" align="center">Chapter 39. Triggers</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="trigger-interface.html" title="39.3. Writing Trigger Functions in C">Next</a></td></tr></table><hr /></div><div class="sect1" id="TRIGGER-DATACHANGES"><div class="titlepage"><div><div><h2 class="title" style="clear: both">39.2. Visibility of Data Changes</h2></div></div></div><p>
    If you execute SQL commands in your trigger function, and these
    commands access the table that the trigger is for, then
    you need to be aware of the data visibility rules, because they determine
    whether these SQL commands will see the data change that the trigger
    is fired for.  Briefly:

    </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
       Statement-level triggers follow simple visibility rules: none of
       the changes made by a statement are visible to statement-level
       <code class="literal">BEFORE</code> triggers, whereas all
       modifications are visible to statement-level <code class="literal">AFTER</code>
       triggers.
      </p></li><li class="listitem"><p>
       The data change (insertion, update, or deletion) causing the
       trigger to fire is naturally <span class="emphasis"><em>not</em></span> visible
       to SQL commands executed in a row-level <code class="literal">BEFORE</code> trigger,
       because it hasn't happened yet.
      </p></li><li class="listitem"><p>
       However, SQL commands executed in a row-level <code class="literal">BEFORE</code>
       trigger <span class="emphasis"><em>will</em></span> see the effects of data
       changes for rows previously processed in the same outer
       command.  This requires caution, since the ordering of these
       change events is not in general predictable; an SQL command that
       affects multiple rows can visit the rows in any order.
      </p></li><li class="listitem"><p>
       Similarly, a row-level <code class="literal">INSTEAD OF</code> trigger will see the
       effects of data changes made by previous firings of <code class="literal">INSTEAD
       OF</code> triggers in the same outer command.
      </p></li><li class="listitem"><p>
       When a row-level <code class="literal">AFTER</code> trigger is fired, all data
       changes made
       by the outer command are already complete, and are visible to
       the invoked trigger function.
      </p></li></ul></div><p>
   </p><p>
    If your trigger function is written in any of the standard procedural
    languages, then the above statements apply only if the function is
    declared <code class="literal">VOLATILE</code>.  Functions that are declared
    <code class="literal">STABLE</code> or <code class="literal">IMMUTABLE</code> will not see changes made by
    the calling command in any case.
   </p><p>
    Further information about data visibility rules can be found in
    <a class="xref" href="spi-visibility.html" title="47.5. Visibility of Data Changes">Section 47.5</a>.  The example in <a class="xref" href="trigger-example.html" title="39.4. A Complete Trigger Example">Section 39.4</a> contains a demonstration of these rules.
   </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="trigger-definition.html" title="39.1. Overview of Trigger Behavior">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="triggers.html" title="Chapter 39. Triggers">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="trigger-interface.html" title="39.3. Writing Trigger Functions in C">Next</a></td></tr><tr><td width="40%" align="left" valign="top">39.1. Overview of Trigger Behavior </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.4 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 39.3. Writing Trigger Functions in C</td></tr></table></div></body></html>