section describes the low-level details of the interface to an
event trigger function. This information is only needed when writing
event trigger functions in C. If you are using a higher-level language
then these details are handled for you. In most cases you should
consider using a procedural language before writing your event triggers
in C. The documentation of each procedural language explains how to
write an event trigger in that language.
</para>
<para>
Event trigger functions must use the <quote>version 1</quote> function
manager interface.
</para>
<para>
When a function is called by the event trigger manager, it is not passed
any normal arguments, but it is passed a <quote>context</quote> pointer
pointing to a <structname>EventTriggerData</structname> structure. C functions can
check whether they were called from the event trigger manager or not by
executing the macro:
<programlisting>
CALLED_AS_EVENT_TRIGGER(fcinfo)
</programlisting>
which expands to:
<programlisting>
((fcinfo)->context != NULL && IsA((fcinfo)->context, EventTriggerData))
</programlisting>
If this returns true, then it is safe to cast
<literal>fcinfo->context</literal> to type <literal>EventTriggerData
*</literal> and make use of the pointed-to
<structname>EventTriggerData</structname> structure. The function must
<emphasis>not</emphasis> alter the <structname>EventTriggerData</structname>
structure or any of the data it points to.
</para>
<para>
<structname>struct EventTriggerData</structname> is defined in
<filename>commands/event_trigger.h</filename>:
<programlisting>
typedef struct EventTriggerData
{
NodeTag type;
const char *event; /* event name */
Node *parsetree; /* parse tree */
CommandTag tag; /* command tag */
} EventTriggerData;
</programlisting>
where the members are defined as follows:
<variablelist>
<varlistentry>
<term><structfield>type</structfield></term>
<listitem>
<para>
Always <literal>T_EventTriggerData</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><structfield>event</structfield></term>
<listitem>
<para>
Describes the event for which the function is called, one of
<literal>"login"</literal>, <literal>"ddl_command_start"</literal>,
<literal>"ddl_command_end"</literal>, <literal>"sql_drop"</literal>,
<literal>"table_rewrite"</literal>.
See <xref linkend="event-trigger-definition"/> for the meaning of these
events.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><structfield>parsetree</structfield></term>
<listitem>
<para>
A pointer to the parse tree of the command. Check the PostgreSQL
source code for details. The parse tree structure is subject to change
without notice.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><structfield>tag</structfield></term>
<listitem>
<para>
The command tag associated with the event for which the event trigger
is run, for example <literal>"CREATE FUNCTION"</literal>.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>
An event trigger function must return a <symbol>NULL</symbol> pointer
(<emphasis>not</emphasis> an SQL null value, that is, do not
set <parameter>isNull</parameter> true).
</para>
</sect1>
<sect1 id="event-trigger-example">
<title>A Complete Event Trigger Example</title>
<para>
Here is a very simple example of an event trigger function written in C.
(Examples of triggers written in procedural languages can be found in
the documentation of the procedural languages.)
</para>
<para>
The function <function>noddl</function> raises an exception