Home Explore Blog CI



postgresql

54th chunk of `doc/src/sgml/plpgsql.sgml`
85574891cd55059add6c0fd524fa002105bab206c180663b0000000100000fa1
 trigger which fired.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="plpgsql-dml-trigger-tg-when">
     <term><varname>TG_WHEN</varname> <type>text</type></term>
     <listitem>
      <para>
       <literal>BEFORE</literal>, <literal>AFTER</literal>, or
       <literal>INSTEAD OF</literal>, depending on the trigger's definition.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="plpgsql-dml-trigger-tg-level">
     <term><varname>TG_LEVEL</varname> <type>text</type></term>
     <listitem>
      <para>
       <literal>ROW</literal> or <literal>STATEMENT</literal>,
       depending on the trigger's definition.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="plpgsql-dml-trigger-tg-op">
     <term><varname>TG_OP</varname> <type>text</type></term>
     <listitem>
      <para>
       operation for which the trigger was fired:
       <literal>INSERT</literal>, <literal>UPDATE</literal>,
       <literal>DELETE</literal>, or <literal>TRUNCATE</literal>.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="plpgsql-dml-trigger-tg-relid">
     <term><varname>TG_RELID</varname> <type>oid</type> (references <link linkend="catalog-pg-class"><structname>pg_class</structname></link>.<structfield>oid</structfield>)</term>
     <listitem>
      <para>
       object ID of the table that caused the trigger invocation.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="plpgsql-dml-trigger-tg-relname">
     <term><varname>TG_RELNAME</varname> <type>name</type></term>
     <listitem>
      <para>
       table that caused the trigger
       invocation. This is now deprecated, and could disappear in a future
       release. Use <literal>TG_TABLE_NAME</literal> instead.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="plpgsql-dml-trigger-tg-table-name">
     <term><varname>TG_TABLE_NAME</varname> <type>name</type></term>
     <listitem>
      <para>
       table that caused the trigger invocation.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="plpgsql-dml-trigger-tg-table-schema">
     <term><varname>TG_TABLE_SCHEMA</varname> <type>name</type></term>
     <listitem>
      <para>
       schema of the table that caused the trigger invocation.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="plpgsql-dml-trigger-tg-nargs">
     <term><varname>TG_NARGS</varname> <type>integer</type></term>
     <listitem>
      <para>
       number of arguments given to the trigger
       function in the <command>CREATE TRIGGER</command> statement.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="plpgsql-dml-trigger-tg-argv">
     <term><varname>TG_ARGV</varname> <type>text[]</type></term>
     <listitem>
      <para>
       arguments from
       the <command>CREATE TRIGGER</command> statement.
       The index counts from 0. Invalid
       indexes (less than 0 or greater than or equal to <varname>tg_nargs</varname>)
       result in a null value.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>

   <para>
    A trigger function must return either <symbol>NULL</symbol> or a
    record/row value having exactly the structure of the table the
    trigger was fired for.
   </para>

   <para>
    Row-level triggers fired <literal>BEFORE</literal> can return null to signal the
    trigger manager to skip the rest of the operation for this row
    (i.e., subsequent triggers are not fired, and the
    <command>INSERT</command>/<command>UPDATE</command>/<command>DELETE</command> does not occur
    for this row).  If a nonnull
    value is returned then the operation proceeds with that row value.
    Returning a row value different from the original value
    of <varname>NEW</varname> alters the row that will be inserted or
    updated.  Thus, if the trigger function wants the triggering
    action to succeed normally without altering the

Title: PL/pgSQL Data Change Triggers: Special Variables (Continued) and Return Values
Summary
This section continues detailing the special variables available when a PL/pgSQL function is called as a data change trigger, elaborating on TG_RELID, TG_RELNAME, TG_TABLE_NAME, TG_TABLE_SCHEMA, TG_NARGS, and TG_ARGV. It then explains the required return values for trigger functions, specifying that they must return either NULL or a record/row with the structure of the table the trigger was fired for. It details how BEFORE row-level triggers can return NULL to skip the rest of the operation or return a modified row to alter the inserted or updated data.