]> begriffs open source - ai-pg/blob - full-docs/html/event-trigger-example.html
Include latest toc output
[ai-pg] / full-docs / html / event-trigger-example.html
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>38.3. A Complete Event Trigger Example</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-trigger-interface.html" title="38.2. Writing Event Trigger Functions in C" /><link rel="next" href="event-trigger-table-rewrite-example.html" title="38.4. A Table Rewrite Event Trigger Example" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">38.3. A Complete Event Trigger Example</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="event-trigger-interface.html" title="38.2. Writing Event Trigger Functions in C">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="event-triggers.html" title="Chapter 38. Event Triggers">Up</a></td><th width="60%" align="center">Chapter 38. Event Triggers</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="event-trigger-table-rewrite-example.html" title="38.4. A Table Rewrite Event Trigger Example">Next</a></td></tr></table><hr /></div><div class="sect1" id="EVENT-TRIGGER-EXAMPLE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">38.3. A Complete Event Trigger Example <a href="#EVENT-TRIGGER-EXAMPLE" class="id_link">#</a></h2></div></div></div><p>
3     Here is a very simple example of an event trigger function written in C.
4     (Examples of triggers written in procedural languages can be found in
5     the documentation of the procedural languages.)
6    </p><p>
7     The function <code class="function">noddl</code> raises an exception each time it is called.
8     The event trigger definition associated the function with
9     the <code class="literal">ddl_command_start</code> event.  The effect is that all DDL
10     commands (with the exceptions mentioned
11     in <a class="xref" href="event-trigger-definition.html" title="38.1. Overview of Event Trigger Behavior">Section 38.1</a>) are prevented from running.
12    </p><p>
13     This is the source code of the trigger function:
14 </p><pre class="programlisting">
15 #include "postgres.h"
16
17 #include "commands/event_trigger.h"
18 #include "fmgr.h"
19
20 PG_MODULE_MAGIC;
21
22 PG_FUNCTION_INFO_V1(noddl);
23
24 Datum
25 noddl(PG_FUNCTION_ARGS)
26 {
27     EventTriggerData *trigdata;
28
29     if (!CALLED_AS_EVENT_TRIGGER(fcinfo))  /* internal error */
30         elog(ERROR, "not fired by event trigger manager");
31
32     trigdata = (EventTriggerData *) fcinfo-&gt;context;
33
34     ereport(ERROR,
35             (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
36              errmsg("command \"%s\" denied",
37                     GetCommandTagName(trigdata-&gt;tag))));
38
39     PG_RETURN_NULL();
40 }
41 </pre><p>
42    </p><p>
43     After you have compiled the source code (see <a class="xref" href="xfunc-c.html#DFUNC" title="36.10.5. Compiling and Linking Dynamically-Loaded Functions">Section 36.10.5</a>),
44     declare the function and the triggers:
45 </p><pre class="programlisting">
46 CREATE FUNCTION noddl() RETURNS event_trigger
47     AS 'noddl' LANGUAGE C;
48
49 CREATE EVENT TRIGGER noddl ON ddl_command_start
50     EXECUTE FUNCTION noddl();
51 </pre><p>
52    </p><p>
53     Now you can test the operation of the trigger:
54 </p><pre class="screen">
55 =# \dy
56                      List of event triggers
57  Name  |       Event       | Owner | Enabled | Function | Tags
58 -------+-------------------+-------+---------+----------+------
59  noddl | ddl_command_start | dim   | enabled | noddl    |
60 (1 row)
61
62 =# CREATE TABLE foo(id serial);
63 ERROR:  command "CREATE TABLE" denied
64 </pre><p>
65    </p><p>
66     In this situation, in order to be able to run some DDL commands when you
67     need to do so, you have to either drop the event trigger or disable it.  It
68     can be convenient to disable the trigger for only the duration of a
69     transaction:
70 </p><pre class="programlisting">
71 BEGIN;
72 ALTER EVENT TRIGGER noddl DISABLE;
73 CREATE TABLE foo (id serial);
74 ALTER EVENT TRIGGER noddl ENABLE;
75 COMMIT;
76 </pre><p>
77     (Recall that DDL commands on event triggers themselves are not affected by
78     event triggers.)
79    </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="event-trigger-interface.html" title="38.2. Writing Event Trigger Functions in C">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="event-triggers.html" title="Chapter 38. Event Triggers">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="event-trigger-table-rewrite-example.html" title="38.4. A Table Rewrite Event Trigger Example">Next</a></td></tr><tr><td width="40%" align="left" valign="top">38.2. Writing Event Trigger Functions in C </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"> 38.4. A Table Rewrite Event Trigger Example</td></tr></table></div></body></html>