Home Explore Blog CI



postgresql

16th chunk of `doc/src/sgml/plperl.sgml`
38c09457ab0c40d2e3aa7c97490400f2a4b3e8d0fb1323090000000100000fac
 <term><literal>$_TD-&gt;{table_schema}</literal></term>
     <listitem>
      <para>
       Name of the schema in which the table on which the trigger fired, is
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><literal>$_TD-&gt;{argc}</literal></term>
     <listitem>
      <para>
       Number of arguments of the trigger function
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><literal>@{$_TD-&gt;{args}}</literal></term>
     <listitem>
      <para>
       Arguments of the trigger function.  Does not exist if <literal>$_TD-&gt;{argc}</literal> is 0.
      </para>
     </listitem>
    </varlistentry>

   </variablelist>
  </para>

  <para>
   Row-level triggers can return one of the following:

   <variablelist>
    <varlistentry>
     <term><literal>return;</literal></term>
     <listitem>
      <para>
       Execute the operation
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><literal>"SKIP"</literal></term>
     <listitem>
      <para>
       Don't execute the operation
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><literal>"MODIFY"</literal></term>
     <listitem>
      <para>
       Indicates that the <literal>NEW</literal> row was modified by
       the trigger function
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>

  <para>
   Here is an example of a trigger function, illustrating some of the
   above:
<programlisting>
CREATE TABLE test (
    i int,
    v varchar
);

CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$
    if (($_TD-&gt;{new}{i} &gt;= 100) || ($_TD-&gt;{new}{i} &lt;= 0)) {
        return "SKIP";    # skip INSERT/UPDATE command
    } elsif ($_TD-&gt;{new}{v} ne "immortal") {
        $_TD-&gt;{new}{v} .= "(modified by trigger)";
        return "MODIFY";  # modify row and execute INSERT/UPDATE command
    } else {
        return;           # execute INSERT/UPDATE command
    }
$$ LANGUAGE plperl;

CREATE TRIGGER test_valid_id_trig
    BEFORE INSERT OR UPDATE ON test
    FOR EACH ROW EXECUTE FUNCTION valid_id();
</programlisting>
  </para>
 </sect1>

 <sect1 id="plperl-event-triggers">
  <title>PL/Perl Event Triggers</title>

  <para>
   PL/Perl can be used to write event trigger functions.  In an event trigger
   function, the hash reference <varname>$_TD</varname> contains information
   about the current trigger event.  <varname>$_TD</varname> is a global variable,
   which gets a separate local value for each invocation of the trigger.  The
   fields of the <varname>$_TD</varname> hash reference are:

   <variablelist>
    <varlistentry>
     <term><literal>$_TD-&gt;{event}</literal></term>
     <listitem>
      <para>
       The name of the event the trigger is fired for.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><literal>$_TD-&gt;{tag}</literal></term>
     <listitem>
      <para>
       The command tag for which the trigger is fired.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>

  <para>
   The return value of the trigger function is ignored.
  </para>

  <para>
   Here is an example of an event trigger function, illustrating some of the
   above:
<programlisting>
CREATE OR REPLACE FUNCTION perlsnitch() RETURNS event_trigger AS $$
  elog(NOTICE, "perlsnitch: " . $_TD->{event} . " " . $_TD->{tag} . " ");
$$ LANGUAGE plperl;

CREATE EVENT TRIGGER perl_a_snitch
    ON ddl_command_start
    EXECUTE FUNCTION perlsnitch();
</programlisting>
  </para>
 </sect1>

 <sect1 id="plperl-under-the-hood">
  <title>PL/Perl Under the Hood</title>

 <sect2 id="plperl-config">
  <title>Configuration</title>

  <para>
  This section lists configuration parameters that affect <application>PL/Perl</application>.
  </para>

  <variablelist>

     <varlistentry id="guc-plperl-on-init" xreflabel="plperl.on_init">
      <term>
       <varname>plperl.on_init</varname> (<type>string</type>)

Title: PL/Perl Triggers and Event Triggers in PostgreSQL
Summary
This section covers PL/Perl triggers and event triggers in PostgreSQL. It explains the return values for row-level triggers: 'return' to execute the operation, 'SKIP' to not execute, and 'MODIFY' to indicate the NEW row was modified. An example trigger function is provided to illustrate usage. For event triggers, it describes the $_TD hash reference containing event information, including the event name and command tag. An example event trigger function is also given. The section concludes with a brief mention of PL/Perl configuration parameters, specifically the plperl.on_init setting.