Home Explore Blog CI



postgresql

4th chunk of `doc/src/sgml/event-trigger.sgml`
e5003021d99914fb68e1d40fd817adcc29b8f66b7ebcadbb0000000100000fa5
 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)-&gt;context != NULL &amp;&amp; IsA((fcinfo)-&gt;context, EventTriggerData))
</programlisting>
    If this returns true, then it is safe to cast
    <literal>fcinfo-&gt;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

Title: C Language Interface for Event Triggers
Summary
This section details the C language interface for event trigger functions, emphasizing that it's primarily needed for C-based triggers and recommending procedural languages instead. Event trigger functions use the 'version 1' function manager interface and receive a 'context' pointer to an 'EventTriggerData' structure. The macro 'CALLED_AS_EVENT_TRIGGER(fcinfo)' verifies the function call origin. The 'EventTriggerData' structure contains event-related information like event name, parse tree, and command tag. Event trigger functions must return a NULL pointer, not an SQL NULL value.