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 < 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