Home Explore Blog CI



postgresql

53th chunk of `doc/src/sgml/plpgsql.sgml`
52131a68090fa3ea47e3072640fc5ec7cf5259a788f9e4190000000100000fb7
 <para>
    Testing of assertions can be enabled or disabled via the configuration
    parameter <literal>plpgsql.check_asserts</literal>, which takes a Boolean
    value; the default is <literal>on</literal>.  If this parameter
    is <literal>off</literal> then <command>ASSERT</command> statements do nothing.
   </para>

   <para>
    Note that <command>ASSERT</command> is meant for detecting program
    bugs, not for reporting ordinary error conditions.  Use
    the <command>RAISE</command> statement, described above, for that.
   </para>

  </sect2>

 </sect1>

 <sect1 id="plpgsql-trigger">
  <title>Trigger Functions</title>

  <indexterm zone="plpgsql-trigger">
   <primary>trigger</primary>
   <secondary>in PL/pgSQL</secondary>
  </indexterm>

  <para>
   <application>PL/pgSQL</application> can be used to define trigger
   functions on data changes or database events.
   A trigger function is created with the <command>CREATE FUNCTION</command>
   command, declaring it as a function with no arguments and a return type of
   <type>trigger</type> (for data change triggers) or
   <type>event_trigger</type> (for database event triggers).
   Special local variables named <varname>TG_<replaceable>something</replaceable></varname> are
   automatically defined to describe the condition that triggered the call.
  </para>

  <sect2 id="plpgsql-dml-trigger">
   <title>Triggers on Data Changes</title>

  <para>
   A <link linkend="triggers">data change trigger</link> is declared as a
   function with no arguments and a return type of <type>trigger</type>.
   Note that the function must be declared with no arguments even if it
   expects to receive some arguments specified in <command>CREATE TRIGGER</command>
   &mdash; such arguments are passed via <varname>TG_ARGV</varname>, as described
   below.
  </para>

  <para>
   When a <application>PL/pgSQL</application> function is called as a
   trigger, several special variables are created automatically in the
   top-level block. They are:

   <variablelist>
    <varlistentry id="plpgsql-dml-trigger-new">
     <term><varname>NEW</varname> <type>record</type></term>
     <listitem>
      <para>
       new database row for <command>INSERT</command>/<command>UPDATE</command> operations in row-level
       triggers. This variable is null in statement-level triggers
       and for <command>DELETE</command> operations.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="plpgsql-dml-trigger-old">
     <term><varname>OLD</varname> <type>record</type></term>
     <listitem>
      <para>
       old database row for <command>UPDATE</command>/<command>DELETE</command> operations in row-level
       triggers. This variable is null in statement-level triggers
       and for <command>INSERT</command> operations.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="plpgsql-dml-trigger-tg-name">
     <term><varname>TG_NAME</varname> <type>name</type></term>
     <listitem>
      <para>
       name of the 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>,

Title: PL/pgSQL Trigger Functions: Definition and Special Variables
Summary
This section introduces PL/pgSQL trigger functions, which are created using CREATE FUNCTION and are triggered by data changes or database events. It details how to define trigger functions with a return type of trigger or event_trigger, and how special local variables like TG_something are automatically defined to describe the triggering condition. It then dives into data change triggers, explaining that they are functions with no arguments and a return type of trigger, and highlights the automatically created special variables like NEW, OLD, TG_NAME, TG_WHEN, TG_LEVEL, and TG_OP, which provide information about the triggered event (INSERT, UPDATE, DELETE).