Home Explore Blog CI



postgresql

14th chunk of `doc/src/sgml/pltcl.sgml`
9c120a91bded3328b4d42e1b0afb58dc52aecbd65240d8120000000100000c94
 operations (result) VALUES ('[quote $result]')"
$$ LANGUAGE pltcl;
</programlisting>
     Note that use of <function>catch</function> is still required for this
     purpose.  Otherwise the error would propagate to the top level of the
     function, preventing the desired insertion into
     the <structname>operations</structname> table.
     The <function>subtransaction</function> command does not trap errors, it
     only assures that all database operations executed inside its scope will
     be rolled back together when an error is reported.
    </para>

    <para>
     A rollback of an explicit subtransaction occurs on any error reported
     by the contained Tcl code, not only errors originating from database
     access.  Thus a regular Tcl exception raised inside
     a <function>subtransaction</function> command will also cause the
     subtransaction to be rolled back.  However, non-error exits out of the
     contained Tcl code (for instance, due to <function>return</function>) do
     not cause a rollback.
    </para>
   </sect1>

   <sect1 id="pltcl-transactions">
    <title>Transaction Management</title>

    <para>
     In a procedure called from the top level or an anonymous code block
     (<command>DO</command> command) called from the top level it is possible
     to control transactions.  To commit the current transaction, call the
     <literal>commit</literal> command.  To roll back the current transaction,
     call the <literal>rollback</literal> command.  (Note that it is not
     possible to run the SQL commands <command>COMMIT</command> or
     <command>ROLLBACK</command> via <function>spi_exec</function> or similar.
     It has to be done using these functions.)  After a transaction is ended,
     a new transaction is automatically started, so there is no separate
     command for that.
    </para>

    <para>
     Here is an example:
<programlisting>
CREATE PROCEDURE transaction_test1()
LANGUAGE pltcl
AS $$
for {set i 0} {$i &lt; 10} {incr i} {
    spi_exec "INSERT INTO test1 (a) VALUES ($i)"
    if {$i % 2 == 0} {
        commit
    } else {
        rollback
    }
}
$$;

CALL transaction_test1();
</programlisting>
    </para>

    <para>
     Transactions cannot be ended when an explicit subtransaction is active.
    </para>
   </sect1>

   <sect1 id="pltcl-config">
    <title>PL/Tcl Configuration</title>

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

    <variablelist>

     <varlistentry id="guc-pltcl-start-proc" xreflabel="pltcl.start_proc">
      <term>
       <varname>pltcl.start_proc</varname> (<type>string</type>)
       <indexterm>
        <primary><varname>pltcl.start_proc</varname> configuration parameter</primary>
       </indexterm>
      </term>
      <listitem>
       <para>
        This parameter, if set to a nonempty string, specifies the name
        (possibly schema-qualified) of a parameterless PL/Tcl function that
        is to be executed whenever a new Tcl interpreter is created for
        PL/Tcl.  Such a function can perform per-session initialization, such
        as loading additional Tcl code.  A new Tcl interpreter is created

Title: PL/Tcl Transaction Management and Configuration
Summary
This section covers transaction management within PL/Tcl procedures and introduces configuration parameters affecting PL/Tcl behavior. It explains how to commit or rollback transactions using the 'commit' and 'rollback' commands, emphasizing that these commands must be used instead of the SQL equivalents. An example demonstrates transaction control within a loop. Additionally, it outlines the 'pltcl.start_proc' configuration parameter, which allows the specification of a PL/Tcl function to be executed upon the creation of a new Tcl interpreter for PL/Tcl, enabling per-session initialization.