the above statements apply only if the function is
declared <literal>VOLATILE</literal>. Functions that are declared
<literal>STABLE</literal> or <literal>IMMUTABLE</literal> will not see changes made by
the calling command in any case.
</para>
<para>
Further information about data visibility rules can be found in
<xref linkend="spi-visibility"/>. The example in <xref
linkend="trigger-example"/> contains a demonstration of these rules.
</para>
</sect1>
<sect1 id="trigger-interface">
<title>Writing Trigger Functions in C</title>
<indexterm zone="trigger-interface">
<primary>trigger</primary>
<secondary>in C</secondary>
</indexterm>
<indexterm>
<primary>transition tables</primary>
<secondary>referencing from C trigger</secondary>
</indexterm>
<para>
This section describes the low-level details of the interface to a
trigger function. This information is only needed when writing
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 triggers in C. The
documentation of each procedural language explains how to write a
trigger in that language.
</para>
<para>
Trigger functions must use the <quote>version 1</quote> function manager
interface.
</para>
<para>
When a function is called by the trigger manager, it is not passed
any normal arguments, but it is passed a <quote>context</quote>
pointer pointing to a <structname>TriggerData</structname> structure. C
functions can check whether they were called from the trigger
manager or not by executing the macro:
<programlisting>
CALLED_AS_TRIGGER(fcinfo)
</programlisting>
which expands to:
<programlisting>
((fcinfo)->context != NULL && IsA((fcinfo)->context, TriggerData))
</programlisting>
If this returns true, then it is safe to cast
<literal>fcinfo->context</literal> to type <literal>TriggerData
*</literal> and make use of the pointed-to
<structname>TriggerData</structname> structure. The function must
<emphasis>not</emphasis> alter the <structname>TriggerData</structname>
structure or any of the data it points to.
</para>
<para>
<structname>struct TriggerData</structname> is defined in
<filename>commands/trigger.h</filename>:
<programlisting>
typedef struct TriggerData
{
NodeTag type;
TriggerEvent tg_event;
Relation tg_relation;
HeapTuple tg_trigtuple;
HeapTuple tg_newtuple;
Trigger *tg_trigger;
TupleTableSlot *tg_trigslot;
TupleTableSlot *tg_newslot;
Tuplestorestate *tg_oldtable;
Tuplestorestate *tg_newtable;
const Bitmapset *tg_updatedcols;
} TriggerData;
</programlisting>
where the members are defined as follows:
<variablelist>
<varlistentry>
<term><structfield>type</structfield></term>
<listitem>
<para>
Always <literal>T_TriggerData</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><structfield>tg_event</structfield></term>
<listitem>
<para>
Describes the event for which the function is called. You can use the
following macros to examine <literal>tg_event</literal>:
<variablelist>
<varlistentry>
<term><literal>TRIGGER_FIRED_BEFORE(tg_event)</literal></term>
<listitem>
<para>
Returns true if the trigger fired before the operation.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>TRIGGER_FIRED_AFTER(tg_event)</literal></term>
<listitem>
<para>
Returns true if the trigger fired after the operation.
</para>
</listitem>
</varlistentry>
<varlistentry>