Home Explore Blog CI



postgresql

9th chunk of `doc/src/sgml/pltcl.sgml`
dedb5af7d3f0025647717d4dd6385f848ab58c4b737e7fea0000000100000fa2
 can be written in PL/Tcl.
     <productname>PostgreSQL</productname> requires that a function that is to be called
     as a trigger must be declared as a function with no arguments
     and a return type of <literal>trigger</literal>.
    </para>
    <para>
     The information from the trigger manager is passed to the function body
     in the following variables:

     <variablelist>

      <varlistentry>
       <term><varname>$TG_name</varname></term>
       <listitem>
        <para>
         The name of the trigger from the <command>CREATE TRIGGER</command> statement.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><varname>$TG_relid</varname></term>
       <listitem>
        <para>
         The object ID of the table that caused the trigger function
         to be invoked.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><varname>$TG_table_name</varname></term>
       <listitem>
        <para>
         The name of the table that caused the trigger function
         to be invoked.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><varname>$TG_table_schema</varname></term>
       <listitem>
        <para>
         The schema of the table that caused the trigger function
         to be invoked.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><varname>$TG_relatts</varname></term>
       <listitem>
        <para>
         A Tcl list of the table column names, prefixed with an empty list
         element. So looking up a column name in the list with <application>Tcl</application>'s
         <function>lsearch</function> command returns the element's number starting
         with 1 for the first column, the same way the columns are customarily
         numbered in <productname>PostgreSQL</productname>.  (Empty list
         elements also appear in the positions of columns that have been
         dropped, so that the attribute numbering is correct for columns
         to their right.)
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><varname>$TG_when</varname></term>
       <listitem>
        <para>
         The string <literal>BEFORE</literal>, <literal>AFTER</literal>, or
         <literal>INSTEAD OF</literal>, depending on the type of trigger event.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><varname>$TG_level</varname></term>
       <listitem>
        <para>
         The string <literal>ROW</literal> or <literal>STATEMENT</literal> depending on the
         type of trigger event.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><varname>$TG_op</varname></term>
       <listitem>
        <para>
         The string <literal>INSERT</literal>, <literal>UPDATE</literal>,
         <literal>DELETE</literal>, or <literal>TRUNCATE</literal> depending on the type of
         trigger event.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><varname>$NEW</varname></term>
       <listitem>
        <para>
         An associative array containing the values of the new table
         row for <command>INSERT</command> or <command>UPDATE</command> actions, or
         empty for <command>DELETE</command>.  The array is indexed by column
         name.  Columns that are null will not appear in the array.
         This is not set for statement-level triggers.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><varname>$OLD</varname></term>
       <listitem>
        <para>
         An associative array containing the values of the old table
         row for <command>UPDATE</command> or <command>DELETE</command> actions, or
         empty for <command>INSERT</command>.  The array is indexed by column
         name.  Columns that are null will not appear in the array.

Title: PL/Tcl Trigger Function Variables
Summary
This section describes the variables passed to PL/Tcl trigger functions, including the trigger name ($TG_name), the table's object ID ($TG_relid), name ($TG_table_name), and schema ($TG_table_schema), as well as a Tcl list of column names ($TG_relatts). It also covers variables related to the trigger event type: the 'when' clause ($TG_when), trigger level ($TG_level), and operation ($TG_op). Finally, it details the $NEW and $OLD associative arrays that contain the new and old row values, respectively, for row-level triggers.