summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/event-trigger-definition.html
blob: 83f7ca066f131e56bfdb21a3f5a727677bfedadc (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?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>40.1. Overview of Event Trigger Behavior</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="event-triggers.html" title="Chapter 40. Event Triggers" /><link rel="next" href="event-trigger-matrix.html" title="40.2. Event Trigger Firing Matrix" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">40.1. Overview of Event Trigger Behavior</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="event-triggers.html" title="Chapter 40. Event Triggers">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="event-triggers.html" title="Chapter 40. Event Triggers">Up</a></td><th width="60%" align="center">Chapter 40. Event Triggers</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.5 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="event-trigger-matrix.html" title="40.2. Event Trigger Firing Matrix">Next</a></td></tr></table><hr /></div><div class="sect1" id="EVENT-TRIGGER-DEFINITION"><div class="titlepage"><div><div><h2 class="title" style="clear: both">40.1. Overview of Event Trigger Behavior</h2></div></div></div><p>
     An event trigger fires whenever the event with which it is associated
     occurs in the database in which it is defined. Currently, the only
     supported events are
     <code class="literal">ddl_command_start</code>,
     <code class="literal">ddl_command_end</code>,
     <code class="literal">table_rewrite</code>
     and <code class="literal">sql_drop</code>.
     Support for additional events may be added in future releases.
   </p><p>
     The <code class="literal">ddl_command_start</code> event occurs just before the
     execution of a <code class="literal">CREATE</code>, <code class="literal">ALTER</code>, <code class="literal">DROP</code>,
     <code class="literal">SECURITY LABEL</code>,
     <code class="literal">COMMENT</code>, <code class="literal">GRANT</code> or <code class="literal">REVOKE</code>
     command.  No check whether the affected object exists or doesn't exist is
     performed before the event trigger fires.
     As an exception, however, this event does not occur for
     DDL commands targeting shared objects — databases, roles, and tablespaces
     — or for commands targeting event triggers themselves.  The event trigger
     mechanism does not support these object types.
     <code class="literal">ddl_command_start</code> also occurs just before the execution of a
     <code class="literal">SELECT INTO</code> command, since this is equivalent to
     <code class="literal">CREATE TABLE AS</code>.
   </p><p>
    The <code class="literal">ddl_command_end</code> event occurs just after the execution of
    this same set of commands.  To obtain more details on the <acronym class="acronym">DDL</acronym>
    operations that took place, use the set-returning function
    <code class="literal">pg_event_trigger_ddl_commands()</code> from the
    <code class="literal">ddl_command_end</code> event trigger code (see
    <a class="xref" href="functions-event-triggers.html" title="9.29. Event Trigger Functions">Section 9.29</a>).  Note that the trigger fires
    after the actions have taken place (but before the transaction commits),
    and thus the system catalogs can be read as already changed.
   </p><p>
    The <code class="literal">sql_drop</code> event occurs just before the
    <code class="literal">ddl_command_end</code> event trigger for any operation that drops
    database objects.  To list the objects that have been dropped, use the
    set-returning function <code class="literal">pg_event_trigger_dropped_objects()</code> from the
    <code class="literal">sql_drop</code> event trigger code (see
    <a class="xref" href="functions-event-triggers.html" title="9.29. Event Trigger Functions">Section 9.29</a>). Note that
    the trigger is executed after the objects have been deleted from the
    system catalogs, so it's not possible to look them up anymore.
   </p><p>
    The <code class="literal">table_rewrite</code> event occurs just before a table is
    rewritten by some actions of the commands <code class="literal">ALTER TABLE</code> and
    <code class="literal">ALTER TYPE</code>.  While other
    control statements are available to rewrite a table,
    like <code class="literal">CLUSTER</code> and <code class="literal">VACUUM</code>,
    the <code class="literal">table_rewrite</code> event is not triggered by them.
   </p><p>
     Event triggers (like other functions) cannot be executed in an aborted
     transaction.  Thus, if a DDL command fails with an error, any associated
     <code class="literal">ddl_command_end</code> triggers will not be executed.  Conversely,
     if a <code class="literal">ddl_command_start</code> trigger fails with an error, no
     further event triggers will fire, and no attempt will be made to execute
     the command itself.  Similarly, if a <code class="literal">ddl_command_end</code> trigger
     fails with an error, the effects of the DDL statement will be rolled
     back, just as they would be in any other case where the containing
     transaction aborts.
   </p><p>
     For a complete list of commands supported by the event trigger mechanism,
     see <a class="xref" href="event-trigger-matrix.html" title="40.2. Event Trigger Firing Matrix">Section 40.2</a>.
   </p><p>
     Event triggers are created using the command <a class="xref" href="sql-createeventtrigger.html" title="CREATE EVENT TRIGGER"><span class="refentrytitle">CREATE EVENT TRIGGER</span></a>.
     In order to create an event trigger, you must first create a function with
     the special return type <code class="literal">event_trigger</code>.  This function
     need not (and may not) return a value; the return type serves merely as
     a signal that the function is to be invoked as an event trigger.
   </p><p>
     If more than one event trigger is defined for a particular event, they will
     fire in alphabetical order by trigger name.
   </p><p>
     A trigger definition can also specify a <code class="literal">WHEN</code>
     condition so that, for example, a <code class="literal">ddl_command_start</code>
     trigger can be fired only for particular commands which the user wishes
     to intercept. A common use of such triggers is to restrict the range of
     DDL operations which users may perform.
   </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="event-triggers.html" title="Chapter 40. Event Triggers">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="event-triggers.html" title="Chapter 40. Event Triggers">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="event-trigger-matrix.html" title="40.2. Event Trigger Firing Matrix">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 40. Event Triggers </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.5 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 40.2. Event Trigger Firing Matrix</td></tr></table></div></body></html>