1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!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>CREATE EVENT TRIGGER</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="sql-createdomain.html" title="CREATE DOMAIN" /><link rel="next" href="sql-createextension.html" title="CREATE EXTENSION" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE EVENT TRIGGER</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createdomain.html" title="CREATE DOMAIN">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createextension.html" title="CREATE EXTENSION">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-CREATEEVENTTRIGGER"><div class="titlepage"></div><a id="id-1.9.3.63.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE EVENT TRIGGER</span></h2><p>CREATE EVENT TRIGGER — define a new event trigger</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 CREATE EVENT TRIGGER <em class="replaceable"><code>name</code></em>
4 ON <em class="replaceable"><code>event</code></em>
5 [ WHEN <em class="replaceable"><code>filter_variable</code></em> IN (<em class="replaceable"><code>filter_value</code></em> [, ... ]) [ AND ... ] ]
6 EXECUTE { FUNCTION | PROCEDURE } <em class="replaceable"><code>function_name</code></em>()
7 </pre></div><div class="refsect1" id="id-1.9.3.63.5"><h2>Description</h2><p>
8 <code class="command">CREATE EVENT TRIGGER</code> creates a new event trigger.
9 Whenever the designated event occurs and the <code class="literal">WHEN</code> condition
10 associated with the trigger, if any, is satisfied, the trigger function
11 will be executed. For a general introduction to event triggers, see
12 <a class="xref" href="event-triggers.html" title="Chapter 38. Event Triggers">Chapter 38</a>. The user who creates an event trigger
14 </p></div><div class="refsect1" id="id-1.9.3.63.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
15 The name to give the new trigger. This name must be unique within
17 </p></dd><dt><span class="term"><em class="replaceable"><code>event</code></em></span></dt><dd><p>
18 The name of the event that triggers a call to the given function.
19 See <a class="xref" href="event-trigger-definition.html" title="38.1. Overview of Event Trigger Behavior">Section 38.1</a> for more information
21 </p></dd><dt><span class="term"><em class="replaceable"><code>filter_variable</code></em></span></dt><dd><p>
22 The name of a variable used to filter events. This makes it possible
23 to restrict the firing of the trigger to a subset of the cases in which
24 it is supported. Currently the only supported
25 <em class="replaceable"><code>filter_variable</code></em>
26 is <code class="literal">TAG</code>.
27 </p></dd><dt><span class="term"><em class="replaceable"><code>filter_value</code></em></span></dt><dd><p>
28 A list of values for the
29 associated <em class="replaceable"><code>filter_variable</code></em>
30 for which the trigger should fire. For <code class="literal">TAG</code>, this means a
31 list of command tags (e.g., <code class="literal">'DROP FUNCTION'</code>).
32 </p></dd><dt><span class="term"><em class="replaceable"><code>function_name</code></em></span></dt><dd><p>
33 A user-supplied function that is declared as taking no argument and
34 returning type <code class="literal">event_trigger</code>.
36 In the syntax of <code class="literal">CREATE EVENT TRIGGER</code>, the keywords
37 <code class="literal">FUNCTION</code> and <code class="literal">PROCEDURE</code> are
38 equivalent, but the referenced function must in any case be a function,
39 not a procedure. The use of the keyword <code class="literal">PROCEDURE</code>
40 here is historical and deprecated.
41 </p></dd></dl></div></div><div class="refsect1" id="SQL-CREATEEVENTTRIGGER-NOTES"><h2>Notes</h2><p>
42 Only superusers can create event triggers.
44 Event triggers are disabled in single-user mode (see <a class="xref" href="app-postgres.html" title="postgres"><span class="refentrytitle"><span class="application">postgres</span></span></a>) as well as when
45 <a class="xref" href="runtime-config-client.html#GUC-EVENT-TRIGGERS">event_triggers</a> is set to <code class="literal">false</code>.
46 If an erroneous event trigger disables the database so much that you can't
47 even drop the trigger, restart with <a class="xref" href="runtime-config-client.html#GUC-EVENT-TRIGGERS">event_triggers</a>
48 set to <code class="literal">false</code> to temporarily disable event triggers, or
49 in single-user mode, and you'll be able to do that.
50 </p></div><div class="refsect1" id="SQL-CREATEEVENTTRIGGER-EXAMPLES"><h2>Examples</h2><p>
51 Forbid the execution of any <a class="link" href="ddl.html" title="Chapter 5. Data Definition">DDL</a> command:
53 </p><pre class="programlisting">
54 CREATE OR REPLACE FUNCTION abort_any_command()
59 RAISE EXCEPTION 'command % is disabled', tg_tag;
63 CREATE EVENT TRIGGER abort_ddl ON ddl_command_start
64 EXECUTE FUNCTION abort_any_command();
65 </pre></div><div class="refsect1" id="SQL-CREATEEVENTTRIGGER-COMPATIBILITY"><h2>Compatibility</h2><p>
66 There is no <code class="command">CREATE EVENT TRIGGER</code> statement in the
68 </p></div><div class="refsect1" id="id-1.9.3.63.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-altereventtrigger.html" title="ALTER EVENT TRIGGER"><span class="refentrytitle">ALTER EVENT TRIGGER</span></a>, <a class="xref" href="sql-dropeventtrigger.html" title="DROP EVENT TRIGGER"><span class="refentrytitle">DROP EVENT TRIGGER</span></a>, <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createdomain.html" title="CREATE DOMAIN">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createextension.html" title="CREATE EXTENSION">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE DOMAIN </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> CREATE EXTENSION</td></tr></table></div></body></html>