Home Explore Blog CI



postgresql

10th chunk of `doc/src/sgml/pltcl.sgml`
4f961364c1a178f901851dde311af49794e736fed6ec77420000000100000fa3
 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.
         This is not set for statement-level triggers.
        </para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term><varname>$args</varname></term>
       <listitem>
        <para>
         A Tcl list of the arguments to the function as given in the
         <command>CREATE TRIGGER</command> statement. These arguments are also accessible as
         <literal>$1</literal> ... <literal>$<replaceable>n</replaceable></literal> in the function body.
        </para>
       </listitem>
      </varlistentry>

     </variablelist>
    </para>

    <para>
     The return value from a trigger function can be one of the strings
     <literal>OK</literal> or <literal>SKIP</literal>, or a list of column name/value pairs.
     If the return value is <literal>OK</literal>,
     the operation (<command>INSERT</command>/<command>UPDATE</command>/<command>DELETE</command>)
     that fired the trigger will proceed
     normally. <literal>SKIP</literal> tells the trigger manager to silently suppress
     the operation for this row. If a list is returned, it tells PL/Tcl to
     return a modified row to the trigger manager; the contents of the
     modified row are specified by the column names and values in the list.
     Any columns not mentioned in the list are set to null.
     Returning a modified row is only meaningful
     for row-level <literal>BEFORE</literal> <command>INSERT</command> or <command>UPDATE</command>
     triggers, for which the modified row will be inserted instead of the one
     given in <varname>$NEW</varname>; or for row-level <literal>INSTEAD OF</literal>
     <command>INSERT</command> or <command>UPDATE</command> triggers where the returned row
     is used as the source data for <command>INSERT RETURNING</command> or
     <command>UPDATE RETURNING</command> clauses.
     In row-level <literal>BEFORE</literal> <command>DELETE</command> or <literal>INSTEAD
     OF</literal> <command>DELETE</command> triggers, returning a modified row has the same
     effect as returning <literal>OK</literal>, that is the operation proceeds.
     The trigger return value is ignored for all other types of triggers.
    </para>

    <tip>
     <para>
      The result list can be made from an array representation of the
      modified tuple with the <literal>array get</literal> Tcl command.
     </para>
    </tip>

    <para>
     Here's a little example trigger function that forces an integer value
     in a table to keep track of the number of updates that are performed on the
     row. For new rows inserted, the value is initialized to 0 and then
     incremented on every update operation.

<programlisting>
CREATE FUNCTION trigfunc_modcount() RETURNS trigger AS $$
    switch $TG_op {
        INSERT {
            set NEW($1) 0
        }
        UPDATE {
            set NEW($1) $OLD($1)
            incr NEW($1)
        }
        default {
            return OK
        }
    }
    return [array get NEW]
$$ LANGUAGE pltcl;

CREATE

Title: PL/Tcl Trigger Function Arguments, Return Values, and Example
Summary
This section describes the remaining variables passed to PL/Tcl trigger functions, specifically the function arguments passed in the CREATE TRIGGER statement ($args or $1...$n). It also details the possible return values from a trigger function: 'OK' (proceed with operation), 'SKIP' (suppress operation), or a list of column name/value pairs (modified row for BEFORE INSERT/UPDATE or INSTEAD OF INSERT/UPDATE triggers). The section then provides a tip for creating the result list using the `array get` command, and concludes with an example trigger function that tracks the number of updates performed on a row.