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