2 38.3. A Complete Event Trigger Example #
4 Here is a very simple example of an event trigger function written in
5 C. (Examples of triggers written in procedural languages can be found
6 in the documentation of the procedural languages.)
8 The function noddl raises an exception each time it is called. The
9 event trigger definition associated the function with the
10 ddl_command_start event. The effect is that all DDL commands (with the
11 exceptions mentioned in Section 38.1) are prevented from running.
13 This is the source code of the trigger function:
16 #include "commands/event_trigger.h"
21 PG_FUNCTION_INFO_V1(noddl);
24 noddl(PG_FUNCTION_ARGS)
26 EventTriggerData *trigdata;
28 if (!CALLED_AS_EVENT_TRIGGER(fcinfo)) /* internal error */
29 elog(ERROR, "not fired by event trigger manager");
31 trigdata = (EventTriggerData *) fcinfo->context;
34 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
35 errmsg("command \"%s\" denied",
36 GetCommandTagName(trigdata->tag))));
41 After you have compiled the source code (see Section 36.10.5), declare
42 the function and the triggers:
43 CREATE FUNCTION noddl() RETURNS event_trigger
44 AS 'noddl' LANGUAGE C;
46 CREATE EVENT TRIGGER noddl ON ddl_command_start
47 EXECUTE FUNCTION noddl();
49 Now you can test the operation of the trigger:
51 List of event triggers
52 Name | Event | Owner | Enabled | Function | Tags
53 -------+-------------------+-------+---------+----------+------
54 noddl | ddl_command_start | dim | enabled | noddl |
57 =# CREATE TABLE foo(id serial);
58 ERROR: command "CREATE TABLE" denied
60 In this situation, in order to be able to run some DDL commands when
61 you need to do so, you have to either drop the event trigger or disable
62 it. It can be convenient to disable the trigger for only the duration
65 ALTER EVENT TRIGGER noddl DISABLE;
66 CREATE TABLE foo (id serial);
67 ALTER EVENT TRIGGER noddl ENABLE;
70 (Recall that DDL commands on event triggers themselves are not affected