2010-09-20 22:08:53 +02:00
|
|
|
<!-- doc/src/sgml/pltcl.sgml -->
|
2000-03-31 05:27:42 +02:00
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<chapter id="pltcl">
|
2001-09-10 23:58:47 +02:00
|
|
|
<title>PL/Tcl - Tcl Procedural Language</title>
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2001-05-13 00:51:36 +02:00
|
|
|
<indexterm zone="pltcl">
|
|
|
|
<primary>PL/Tcl</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<indexterm zone="pltcl">
|
|
|
|
<primary>Tcl</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
2000-03-31 00:13:30 +02:00
|
|
|
<para>
|
|
|
|
PL/Tcl is a loadable procedural language for the
|
2001-11-21 06:53:41 +01:00
|
|
|
<productname>PostgreSQL</productname> database system
|
2005-04-09 05:52:43 +02:00
|
|
|
that enables the <ulink url="http://www.tcl.tk/">
|
|
|
|
Tcl language</ulink> to be used to write functions and
|
2001-02-09 04:06:38 +01:00
|
|
|
trigger procedures.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<!-- **** PL/Tcl overview **** -->
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="pltcl-overview">
|
2000-03-31 00:13:30 +02:00
|
|
|
<title>Overview</title>
|
|
|
|
|
|
|
|
<para>
|
2006-05-30 13:40:21 +02:00
|
|
|
PL/Tcl offers most of the capabilities a function writer has in
|
|
|
|
the C language, with a few restrictions, and with the addition of
|
|
|
|
the powerful string processing libraries that are available for
|
|
|
|
Tcl.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
<para>
|
2006-05-30 13:40:21 +02:00
|
|
|
One compelling <emphasis>good</emphasis> restriction is that
|
|
|
|
everything is executed from within the safety of the context of a
|
|
|
|
Tcl interpreter. In addition to the limited command set of safe
|
|
|
|
Tcl, only a few commands are available to access the database via
|
2017-10-09 03:44:17 +02:00
|
|
|
SPI and to raise messages via <function>elog()</function>. PL/Tcl
|
2006-05-30 13:40:21 +02:00
|
|
|
provides no way to access internals of the database server or to
|
|
|
|
gain OS-level access under the permissions of the
|
|
|
|
<productname>PostgreSQL</productname> server process, as a C
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
function can do. Thus, unprivileged database users can be trusted
|
2006-05-30 13:40:21 +02:00
|
|
|
to use this language; it does not give them unlimited authority.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
<para>
|
2006-05-30 13:40:21 +02:00
|
|
|
The other notable implementation restriction is that Tcl functions
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
cannot be used to create input/output functions for new data
|
2006-05-30 13:40:21 +02:00
|
|
|
types.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
<para>
|
2001-02-09 04:06:38 +01:00
|
|
|
Sometimes it is desirable to write Tcl functions that are not restricted
|
2003-04-07 03:29:26 +02:00
|
|
|
to safe Tcl. For example, one might want a Tcl function that sends
|
2017-10-09 03:44:17 +02:00
|
|
|
email. To handle these cases, there is a variant of <application>PL/Tcl</application> called <literal>PL/TclU</literal>
|
2014-08-30 17:52:36 +02:00
|
|
|
(for untrusted Tcl). This is exactly the same language except that a full
|
2017-10-09 03:44:17 +02:00
|
|
|
Tcl interpreter is used. <emphasis>If <application>PL/TclU</application> is used, it must be
|
2001-02-09 04:06:38 +01:00
|
|
|
installed as an untrusted procedural language</emphasis> so that only
|
2017-10-09 03:44:17 +02:00
|
|
|
database superusers can create functions in it. The writer of a <application>PL/TclU</application>
|
2001-02-09 04:06:38 +01:00
|
|
|
function must take care that the function cannot be used to do anything
|
|
|
|
unwanted, since it will be able to do anything that could be done by
|
|
|
|
a user logged in as the database administrator.
|
|
|
|
</para>
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
The shared object code for the <application>PL/Tcl</application> and
|
|
|
|
<application>PL/TclU</application> call handlers is automatically built and
|
2006-05-30 13:40:21 +02:00
|
|
|
installed in the <productname>PostgreSQL</productname> library
|
|
|
|
directory if Tcl support is specified in the configuration step of
|
2017-10-09 03:44:17 +02:00
|
|
|
the installation procedure. To install <application>PL/Tcl</application>
|
|
|
|
and/or <application>PL/TclU</application> in a particular database, use the
|
|
|
|
<command>CREATE EXTENSION</command> command, for example
|
2017-03-23 19:16:45 +01:00
|
|
|
<literal>CREATE EXTENSION pltcl</literal> or
|
|
|
|
<literal>CREATE EXTENSION pltclu</literal>.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<!-- **** PL/Tcl description **** -->
|
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<sect1 id="pltcl-functions">
|
2002-01-23 22:08:17 +01:00
|
|
|
<title>PL/Tcl Functions and Arguments</title>
|
2000-03-31 00:13:30 +02:00
|
|
|
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
To create a function in the <application>PL/Tcl</application> language, use
|
2017-11-23 15:39:47 +01:00
|
|
|
the standard <xref linkend="sql-createfunction"/> syntax:
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<programlisting>
|
2004-12-30 22:45:37 +01:00
|
|
|
CREATE FUNCTION <replaceable>funcname</replaceable> (<replaceable>argument-types</replaceable>) RETURNS <replaceable>return-type</replaceable> AS $$
|
2000-03-31 00:13:30 +02:00
|
|
|
# PL/Tcl function body
|
2004-05-17 01:22:08 +02:00
|
|
|
$$ LANGUAGE pltcl;
|
2003-04-07 03:29:26 +02:00
|
|
|
</programlisting>
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2017-10-09 03:44:17 +02:00
|
|
|
<application>PL/TclU</application> is the same, except that the language has to be specified as
|
|
|
|
<literal>pltclu</literal>.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The body of the function is simply a piece of Tcl script.
|
2016-11-06 23:56:05 +01:00
|
|
|
When the function is called, the argument values are passed to the
|
|
|
|
Tcl script as variables named <literal>1</literal>
|
|
|
|
... <literal><replaceable>n</replaceable></literal>. The result is
|
|
|
|
returned from the Tcl code in the usual way, with
|
2017-11-30 14:46:13 +01:00
|
|
|
a <literal>return</literal> statement. In a procedure, the return value
|
|
|
|
from the Tcl code is ignored.
|
2003-04-07 03:29:26 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
For example, a function
|
2002-01-23 22:08:17 +01:00
|
|
|
returning the greater of two integer values could be defined as:
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<programlisting>
|
2004-05-17 01:22:08 +02:00
|
|
|
CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS $$
|
2005-01-22 23:56:36 +01:00
|
|
|
if {$1 > $2} {return $1}
|
2002-01-23 22:08:17 +01:00
|
|
|
return $2
|
2004-05-17 01:22:08 +02:00
|
|
|
$$ LANGUAGE pltcl STRICT;
|
2003-04-07 03:29:26 +02:00
|
|
|
</programlisting>
|
2002-01-23 22:08:17 +01:00
|
|
|
|
2017-10-09 03:44:17 +02:00
|
|
|
Note the clause <literal>STRICT</literal>, which saves us from
|
2003-04-07 03:29:26 +02:00
|
|
|
having to think about null input values: if a null value is passed, the
|
|
|
|
function will not be called at all, but will just return a null
|
2002-01-23 22:08:17 +01:00
|
|
|
result automatically.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
In a nonstrict function,
|
|
|
|
if the actual value of an argument is null, the corresponding
|
|
|
|
<literal>$<replaceable>n</replaceable></literal> variable will be set to an empty string.
|
|
|
|
To detect whether a particular argument is null, use the function
|
2017-10-09 03:44:17 +02:00
|
|
|
<literal>argisnull</literal>. For example, suppose that we wanted <function>tcl_max</function>
|
2003-04-07 03:29:26 +02:00
|
|
|
with one null and one nonnull argument to return the nonnull
|
|
|
|
argument, rather than null:
|
2002-01-23 22:08:17 +01:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<programlisting>
|
2004-05-17 01:22:08 +02:00
|
|
|
CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS $$
|
2002-01-23 22:08:17 +01:00
|
|
|
if {[argisnull 1]} {
|
|
|
|
if {[argisnull 2]} { return_null }
|
|
|
|
return $2
|
|
|
|
}
|
|
|
|
if {[argisnull 2]} { return $1 }
|
2005-01-22 23:56:36 +01:00
|
|
|
if {$1 > $2} {return $1}
|
2000-03-31 00:13:30 +02:00
|
|
|
return $2
|
2004-05-17 01:22:08 +02:00
|
|
|
$$ LANGUAGE pltcl;
|
2003-04-07 03:29:26 +02:00
|
|
|
</programlisting>
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2002-01-23 22:08:17 +01:00
|
|
|
<para>
|
|
|
|
As shown above,
|
2003-11-12 23:47:47 +01:00
|
|
|
to return a null value from a PL/Tcl function, execute
|
2002-01-23 22:08:17 +01:00
|
|
|
<literal>return_null</literal>. This can be done whether the
|
|
|
|
function is strict or not.
|
2001-05-01 00:22:34 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
Composite-type arguments are passed to the function as Tcl
|
|
|
|
arrays. The element names of the array are the attribute names
|
|
|
|
of the composite type. If an attribute in the passed row has the
|
|
|
|
null value, it will not appear in the array. Here is an example:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE TABLE employee (
|
|
|
|
name text,
|
|
|
|
salary integer,
|
|
|
|
age integer
|
|
|
|
);
|
|
|
|
|
2004-05-17 01:22:08 +02:00
|
|
|
CREATE FUNCTION overpaid(employee) RETURNS boolean AS $$
|
2005-01-22 23:56:36 +01:00
|
|
|
if {200000.0 < $1(salary)} {
|
2000-03-31 00:13:30 +02:00
|
|
|
return "t"
|
|
|
|
}
|
2007-12-04 00:49:51 +01:00
|
|
|
if {$1(age) < 30 && 100000.0 < $1(salary)} {
|
2000-03-31 00:13:30 +02:00
|
|
|
return "t"
|
|
|
|
}
|
|
|
|
return "f"
|
2004-05-17 01:22:08 +02:00
|
|
|
$$ LANGUAGE pltcl;
|
2003-04-07 03:29:26 +02:00
|
|
|
</programlisting>
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
|
2002-01-23 22:08:17 +01:00
|
|
|
<para>
|
2016-11-06 23:56:05 +01:00
|
|
|
PL/Tcl functions can return composite-type results, too. To do this,
|
|
|
|
the Tcl code must return a list of column name/value pairs matching
|
|
|
|
the expected result type. Any column names omitted from the list
|
|
|
|
are returned as nulls, and an error is raised if there are unexpected
|
|
|
|
column names. Here is an example:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE FUNCTION square_cube(in int, out squared int, out cubed int) AS $$
|
|
|
|
return [list squared [expr {$1 * $1}] cubed [expr {$1 * $1 * $1}]]
|
|
|
|
$$ LANGUAGE pltcl;
|
|
|
|
</programlisting>
|
2004-12-30 22:45:37 +01:00
|
|
|
</para>
|
|
|
|
|
2018-03-14 16:47:21 +01:00
|
|
|
<para>
|
|
|
|
Output arguments of procedures are returned in the same way, for example:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE PROCEDURE tcl_triple(INOUT a integer, INOUT b integer) AS $$
|
|
|
|
return [list a [expr {$1 * 3}] b [expr {$2 * 3}]]
|
|
|
|
$$ LANGUAGE pltcl;
|
|
|
|
|
|
|
|
CALL tcl_triple(5, 10);
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
2016-11-06 23:56:05 +01:00
|
|
|
<tip>
|
|
|
|
<para>
|
|
|
|
The result list can be made from an array representation of the
|
2017-10-09 03:44:17 +02:00
|
|
|
desired tuple with the <literal>array get</literal> Tcl command. For example:
|
2016-11-06 23:56:05 +01:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE FUNCTION raise_pay(employee, delta int) RETURNS employee AS $$
|
|
|
|
set 1(salary) [expr {$1(salary) + $2}]
|
|
|
|
return [array get 1]
|
|
|
|
$$ LANGUAGE pltcl;
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</tip>
|
|
|
|
|
2004-12-30 22:45:37 +01:00
|
|
|
<para>
|
2016-11-06 23:56:05 +01:00
|
|
|
PL/Tcl functions can return sets. To do this, the Tcl code should
|
|
|
|
call <function>return_next</function> once per row to be returned,
|
|
|
|
passing either the appropriate value when returning a scalar type,
|
|
|
|
or a list of column name/value pairs when returning a composite type.
|
|
|
|
Here is an example returning a scalar type:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE FUNCTION sequence(int, int) RETURNS SETOF int AS $$
|
|
|
|
for {set i $1} {$i < $2} {incr i} {
|
|
|
|
return_next $i
|
|
|
|
}
|
|
|
|
$$ LANGUAGE pltcl;
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
and here is one returning a composite type:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE FUNCTION table_of_squares(int, int) RETURNS TABLE (x int, x2 int) AS $$
|
|
|
|
for {set i $1} {$i < $2} {incr i} {
|
|
|
|
return_next [list x $i x2 [expr {$i * $i}]]
|
|
|
|
}
|
|
|
|
$$ LANGUAGE pltcl;
|
|
|
|
</programlisting>
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
</sect1>
|
2002-01-23 22:08:17 +01:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<sect1 id="pltcl-data">
|
2002-01-23 22:08:17 +01:00
|
|
|
<title>Data Values in PL/Tcl</title>
|
|
|
|
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
The argument values supplied to a PL/Tcl function's code are simply
|
2002-01-23 22:08:17 +01:00
|
|
|
the input arguments converted to text form (just as if they had been
|
2017-10-09 03:44:17 +02:00
|
|
|
displayed by a <command>SELECT</command> statement). Conversely, the
|
|
|
|
<literal>return</literal> and <literal>return_next</literal> commands will accept
|
2016-11-06 23:56:05 +01:00
|
|
|
any string that is acceptable input format for the function's declared
|
|
|
|
result type, or for the specified column of a composite result type.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
</sect1>
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<sect1 id="pltcl-global">
|
2000-03-31 00:13:30 +02:00
|
|
|
<title>Global Data in PL/Tcl</title>
|
|
|
|
|
2003-08-31 19:32:24 +02:00
|
|
|
<indexterm zone="pltcl-global">
|
|
|
|
<primary>global data</primary>
|
|
|
|
<secondary>in PL/Tcl</secondary>
|
|
|
|
</indexterm>
|
|
|
|
|
2000-03-31 00:13:30 +02:00
|
|
|
<para>
|
2002-01-23 22:08:17 +01:00
|
|
|
Sometimes it
|
2003-04-07 03:29:26 +02:00
|
|
|
is useful to have some global data that is held between two
|
|
|
|
calls to a function or is shared between different functions.
|
2010-09-30 23:18:51 +02:00
|
|
|
This is easily done in PL/Tcl, but there are some restrictions that
|
|
|
|
must be understood.
|
2001-02-09 04:06:38 +01:00
|
|
|
</para>
|
2010-09-30 23:18:51 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
For security reasons, PL/Tcl executes functions called by any one SQL
|
|
|
|
role in a separate Tcl interpreter for that role. This prevents
|
|
|
|
accidental or malicious interference by one user with the behavior of
|
|
|
|
another user's PL/Tcl functions. Each such interpreter will have its own
|
2017-10-09 03:44:17 +02:00
|
|
|
values for any <quote>global</quote> Tcl variables. Thus, two PL/Tcl
|
2010-09-30 23:18:51 +02:00
|
|
|
functions will share the same global variables if and only if they are
|
|
|
|
executed by the same SQL role. In an application wherein a single
|
|
|
|
session executes code under multiple SQL roles (via <literal>SECURITY
|
2017-10-09 03:44:17 +02:00
|
|
|
DEFINER</literal> functions, use of <command>SET ROLE</command>, etc) you may need to
|
2010-09-30 23:18:51 +02:00
|
|
|
take explicit steps to ensure that PL/Tcl functions can share data. To
|
|
|
|
do that, make sure that functions that should communicate are owned by
|
2017-10-09 03:44:17 +02:00
|
|
|
the same user, and mark them <literal>SECURITY DEFINER</literal>. You must of
|
2010-09-30 23:18:51 +02:00
|
|
|
course take care that such functions can't be used to do anything
|
|
|
|
unintended.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
All PL/TclU functions used in a session execute in the same Tcl
|
|
|
|
interpreter, which of course is distinct from the interpreter(s)
|
|
|
|
used for PL/Tcl functions. So global data is automatically shared
|
|
|
|
between PL/TclU functions. This is not considered a security risk
|
|
|
|
because all PL/TclU functions execute at the same trust level,
|
|
|
|
namely that of a database superuser.
|
|
|
|
</para>
|
|
|
|
|
2001-02-09 04:06:38 +01:00
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
To help protect PL/Tcl functions from unintentionally interfering
|
2002-01-23 22:08:17 +01:00
|
|
|
with each other, a global
|
2017-10-09 03:44:17 +02:00
|
|
|
array is made available to each function via the <function>upvar</function>
|
2003-04-07 03:29:26 +02:00
|
|
|
command. The global name of this variable is the function's internal
|
2017-10-09 03:44:17 +02:00
|
|
|
name, and the local name is <literal>GD</literal>. It is recommended that
|
|
|
|
<literal>GD</literal> be used
|
2004-12-30 22:45:37 +01:00
|
|
|
for persistent private data of a function. Use regular Tcl global
|
|
|
|
variables only for values that you specifically intend to be shared among
|
2017-10-09 03:44:17 +02:00
|
|
|
multiple functions. (Note that the <literal>GD</literal> arrays are only
|
2010-09-30 23:18:51 +02:00
|
|
|
global within a particular interpreter, so they do not bypass the
|
|
|
|
security restrictions mentioned above.)
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
An example of using <literal>GD</literal> appears in the
|
2002-01-23 22:08:17 +01:00
|
|
|
<function>spi_execp</function> example below.
|
|
|
|
</para>
|
2003-04-07 03:29:26 +02:00
|
|
|
</sect1>
|
2002-01-23 22:08:17 +01:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<sect1 id="pltcl-dbaccess">
|
2002-01-23 22:08:17 +01:00
|
|
|
<title>Database Access from PL/Tcl</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The following commands are available to access the database from
|
2003-04-07 03:29:26 +02:00
|
|
|
the body of a PL/Tcl function:
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
|
|
|
|
<varlistentry>
|
2003-09-09 01:02:28 +02:00
|
|
|
<term><literal><function>spi_exec</function> <optional role="tcl">-count <replaceable>n</replaceable></optional> <optional role="tcl">-array <replaceable>name</replaceable></optional> <replaceable>command</replaceable> <optional role="tcl"><replaceable>loop-body</replaceable></optional></literal></term>
|
2002-01-23 22:08:17 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-12-30 22:45:37 +01:00
|
|
|
Executes an SQL command given as a string. An error in the command
|
|
|
|
causes an error to be raised. Otherwise, the return value of <function>spi_exec</function>
|
|
|
|
is the number of rows processed (selected, inserted, updated, or
|
|
|
|
deleted) by the command, or zero if the command is a utility
|
2017-10-09 03:44:17 +02:00
|
|
|
statement. In addition, if the command is a <command>SELECT</command> statement, the
|
2004-12-30 22:45:37 +01:00
|
|
|
values of the selected columns are placed in Tcl variables as
|
|
|
|
described below.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
The optional <literal>-count</literal> value tells
|
2004-12-30 22:45:37 +01:00
|
|
|
<function>spi_exec</function> the maximum number of rows
|
|
|
|
to process in the command. The effect of this is comparable to
|
2017-10-09 03:44:17 +02:00
|
|
|
setting up a query as a cursor and then saying <literal>FETCH <replaceable>n</replaceable></literal>.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
If the command is a <command>SELECT</command> statement, the values of the
|
2004-12-30 22:45:37 +01:00
|
|
|
result columns are placed into Tcl variables named after the columns.
|
2017-10-09 03:44:17 +02:00
|
|
|
If the <literal>-array</literal> option is given, the column values are
|
2016-11-06 20:43:13 +01:00
|
|
|
instead stored into elements of the named associative array, with the
|
|
|
|
column names used as array indexes. In addition, the current row
|
|
|
|
number within the result (counting from zero) is stored into the array
|
2017-10-09 03:44:17 +02:00
|
|
|
element named <quote><literal>.tupno</literal></quote>, unless that name is
|
2016-11-06 20:43:13 +01:00
|
|
|
in use as a column name in the result.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
If the command is a <command>SELECT</command> statement and no <replaceable>loop-body</replaceable>
|
2004-12-30 22:45:37 +01:00
|
|
|
script is given, then only the first row of results are stored into
|
2016-11-06 20:43:13 +01:00
|
|
|
Tcl variables or array elements; remaining rows, if any, are ignored.
|
|
|
|
No storing occurs if the query returns no rows. (This case can be
|
|
|
|
detected by checking the result of <function>spi_exec</function>.)
|
|
|
|
For example:
|
2003-04-07 03:29:26 +02:00
|
|
|
<programlisting>
|
2002-01-23 22:08:17 +01:00
|
|
|
spi_exec "SELECT count(*) AS cnt FROM pg_proc"
|
2003-04-07 03:29:26 +02:00
|
|
|
</programlisting>
|
2017-10-09 03:44:17 +02:00
|
|
|
will set the Tcl variable <literal>$cnt</literal> to the number of rows in
|
|
|
|
the <structname>pg_proc</structname> system catalog.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
If the optional <replaceable>loop-body</replaceable> argument is given, it is
|
2004-12-30 22:45:37 +01:00
|
|
|
a piece of Tcl script that is executed once for each row in the
|
2017-10-09 03:44:17 +02:00
|
|
|
query result. (<replaceable>loop-body</replaceable> is ignored if the given
|
|
|
|
command is not a <command>SELECT</command>.)
|
2016-11-06 20:43:13 +01:00
|
|
|
The values of the current row's columns
|
|
|
|
are stored into Tcl variables or array elements before each iteration.
|
|
|
|
For example:
|
2003-04-07 03:29:26 +02:00
|
|
|
<programlisting>
|
2002-01-23 22:08:17 +01:00
|
|
|
spi_exec -array C "SELECT * FROM pg_class" {
|
|
|
|
elog DEBUG "have table $C(relname)"
|
|
|
|
}
|
2003-04-07 03:29:26 +02:00
|
|
|
</programlisting>
|
2017-10-09 03:44:17 +02:00
|
|
|
will print a log message for every row of <literal>pg_class</literal>. This
|
2004-12-30 22:45:37 +01:00
|
|
|
feature works similarly to other Tcl looping constructs; in
|
2017-10-09 03:44:17 +02:00
|
|
|
particular <literal>continue</literal> and <literal>break</literal> work in the
|
2004-12-30 22:45:37 +01:00
|
|
|
usual way inside the loop body.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
If a column of a query result is null, the target
|
2017-10-09 03:44:17 +02:00
|
|
|
variable for it is <quote>unset</quote> rather than being set.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><function>spi_prepare</function> <replaceable>query</replaceable> <replaceable>typelist</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-12-30 22:45:37 +01:00
|
|
|
Prepares and saves a query plan for later execution. The
|
|
|
|
saved plan will be retained for the life of the current
|
2017-10-09 03:44:17 +02:00
|
|
|
session.<indexterm><primary>preparing a query</primary>
|
|
|
|
<secondary>in PL/Tcl</secondary></indexterm>
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
The query can use parameters, that is, placeholders for
|
2004-12-30 22:45:37 +01:00
|
|
|
values to be supplied whenever the plan is actually executed.
|
|
|
|
In the query string, refer to parameters
|
|
|
|
by the symbols <literal>$1</literal> ... <literal>$<replaceable>n</replaceable></literal>.
|
|
|
|
If the query uses parameters, the names of the parameter types
|
|
|
|
must be given as a Tcl list. (Write an empty list for
|
|
|
|
<replaceable>typelist</replaceable> if no parameters are used.)
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The return value from <function>spi_prepare</function> is a query ID
|
2004-12-30 22:45:37 +01:00
|
|
|
to be used in subsequent calls to <function>spi_execp</function>. See
|
|
|
|
<function>spi_execp</function> for an example.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2017-10-09 03:44:17 +02:00
|
|
|
<term><literal><function>spi_execp</function> <optional role="tcl">-count <replaceable>n</replaceable></optional> <optional role="tcl">-array <replaceable>name</replaceable></optional> <optional role="tcl">-nulls <replaceable>string</replaceable></optional> <replaceable>queryid</replaceable> <optional role="tcl"><replaceable>value-list</replaceable></optional> <optional role="tcl"><replaceable>loop-body</replaceable></optional></literal></term>
|
2002-01-23 22:08:17 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
Executes a query previously prepared with <function>spi_prepare</function>.
|
2004-12-30 22:45:37 +01:00
|
|
|
<replaceable>queryid</replaceable> is the ID returned by
|
2017-10-09 03:44:17 +02:00
|
|
|
<function>spi_prepare</function>. If the query references parameters,
|
2004-12-30 22:45:37 +01:00
|
|
|
a <replaceable>value-list</replaceable> must be supplied. This
|
|
|
|
is a Tcl list of actual values for the parameters. The list must be
|
|
|
|
the same length as the parameter type list previously given to
|
2017-10-09 03:44:17 +02:00
|
|
|
<function>spi_prepare</function>. Omit <replaceable>value-list</replaceable>
|
2004-12-30 22:45:37 +01:00
|
|
|
if the query has no parameters.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
The optional value for <literal>-nulls</literal> is a string of spaces and
|
|
|
|
<literal>'n'</literal> characters telling <function>spi_execp</function>
|
2004-12-30 22:45:37 +01:00
|
|
|
which of the parameters are null values. If given, it must have exactly the
|
|
|
|
same length as the <replaceable>value-list</replaceable>. If it
|
|
|
|
is not given, all the parameter values are nonnull.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
Except for the way in which the query and its parameters are specified,
|
2017-10-09 03:44:17 +02:00
|
|
|
<function>spi_execp</function> works just like <function>spi_exec</function>.
|
|
|
|
The <literal>-count</literal>, <literal>-array</literal>, and
|
2004-12-30 22:45:37 +01:00
|
|
|
<replaceable>loop-body</replaceable> options are the same,
|
|
|
|
and so is the result value.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
2004-12-30 22:45:37 +01:00
|
|
|
Here's an example of a PL/Tcl function using a prepared plan:
|
2002-01-23 22:08:17 +01:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<programlisting>
|
2004-05-17 01:22:08 +02:00
|
|
|
CREATE FUNCTION t1_count(integer, integer) RETURNS integer AS $$
|
2002-01-23 22:08:17 +01:00
|
|
|
if {![ info exists GD(plan) ]} {
|
|
|
|
# prepare the saved plan on the first call
|
2004-05-17 01:22:08 +02:00
|
|
|
set GD(plan) [ spi_prepare \
|
|
|
|
"SELECT count(*) AS cnt FROM t1 WHERE num >= \$1 AND num <= \$2" \
|
2002-01-23 22:08:17 +01:00
|
|
|
[ list int4 int4 ] ]
|
|
|
|
}
|
|
|
|
spi_execp -count 1 $GD(plan) [ list $1 $2 ]
|
|
|
|
return $cnt
|
2004-05-17 01:22:08 +02:00
|
|
|
$$ LANGUAGE pltcl;
|
2003-04-07 03:29:26 +02:00
|
|
|
</programlisting>
|
2002-01-23 22:08:17 +01:00
|
|
|
|
2007-01-30 23:29:23 +01:00
|
|
|
We need backslashes inside the query string given to
|
2017-10-09 03:44:17 +02:00
|
|
|
<function>spi_prepare</function> to ensure that the
|
|
|
|
<literal>$<replaceable>n</replaceable></literal> markers will be passed
|
|
|
|
through to <function>spi_prepare</function> as-is, and not replaced by Tcl
|
2007-01-30 23:29:23 +01:00
|
|
|
variable substitution.
|
2004-05-17 01:22:08 +02:00
|
|
|
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2014-05-07 03:28:58 +02:00
|
|
|
<term>
|
2017-10-09 03:44:17 +02:00
|
|
|
<function>spi_lastoid</function>
|
2014-05-07 03:28:58 +02:00
|
|
|
<indexterm>
|
|
|
|
<primary>spi_lastoid</primary>
|
|
|
|
<secondary>in PL/Tcl</secondary>
|
|
|
|
</indexterm>
|
|
|
|
</term>
|
2002-01-23 22:08:17 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-12-30 22:45:37 +01:00
|
|
|
Returns the OID of the row inserted by the last
|
2017-10-09 03:44:17 +02:00
|
|
|
<function>spi_exec</function> or <function>spi_execp</function>, if the
|
|
|
|
command was a single-row <command>INSERT</command> and the modified
|
2005-03-13 10:36:31 +01:00
|
|
|
table contained OIDs. (If not, you get zero.)
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2017-03-11 20:37:05 +01:00
|
|
|
<varlistentry>
|
|
|
|
<term><function>subtransaction</function> <replaceable>command</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The Tcl script contained in <replaceable>command</replaceable> is
|
|
|
|
executed within a SQL subtransaction. If the script returns an
|
|
|
|
error, that entire subtransaction is rolled back before returning the
|
|
|
|
error out to the surrounding Tcl code.
|
2017-11-23 15:39:47 +01:00
|
|
|
See <xref linkend="pltcl-subtransactions"/> for more details and an
|
2017-03-11 20:37:05 +01:00
|
|
|
example.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2002-01-23 22:08:17 +01:00
|
|
|
<varlistentry>
|
2017-10-09 03:44:17 +02:00
|
|
|
<term><function>quote</function> <replaceable>string</replaceable></term>
|
2002-01-23 22:08:17 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-12-30 22:45:37 +01:00
|
|
|
Doubles all occurrences of single quote and backslash characters
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
in the given string. This can be used to safely quote strings
|
2004-12-30 22:45:37 +01:00
|
|
|
that are to be inserted into SQL commands given
|
|
|
|
to <function>spi_exec</function> or
|
|
|
|
<function>spi_prepare</function>.
|
2007-02-01 01:28:19 +01:00
|
|
|
For example, think about an SQL command string like:
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
"SELECT '$val' AS ret"
|
|
|
|
</programlisting>
|
|
|
|
|
2017-10-09 03:44:17 +02:00
|
|
|
where the Tcl variable <literal>val</literal> actually contains
|
2004-12-30 22:45:37 +01:00
|
|
|
<literal>doesn't</literal>. This would result
|
2007-02-01 01:28:19 +01:00
|
|
|
in the final command string:
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
SELECT 'doesn't' AS ret
|
|
|
|
</programlisting>
|
|
|
|
|
2004-12-30 22:45:37 +01:00
|
|
|
which would cause a parse error during
|
|
|
|
<function>spi_exec</function> or
|
|
|
|
<function>spi_prepare</function>.
|
2007-02-01 01:28:19 +01:00
|
|
|
To work properly, the submitted command should contain:
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
<programlisting>
|
2004-09-21 00:48:29 +02:00
|
|
|
SELECT 'doesn''t' AS ret
|
2002-01-23 22:08:17 +01:00
|
|
|
</programlisting>
|
|
|
|
|
2007-02-01 01:28:19 +01:00
|
|
|
which can be formed in PL/Tcl using:
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
"SELECT '[ quote $val ]' AS ret"
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
One advantage of <function>spi_execp</function> is that you don't
|
2004-12-30 22:45:37 +01:00
|
|
|
have to quote parameter values like this, since the parameters are never
|
|
|
|
parsed as part of an SQL command string.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2014-05-07 03:28:58 +02:00
|
|
|
<term>
|
2017-10-09 03:44:17 +02:00
|
|
|
<function>elog</function> <replaceable>level</replaceable> <replaceable>msg</replaceable>
|
2014-05-07 03:28:58 +02:00
|
|
|
<indexterm>
|
|
|
|
<primary>elog</primary>
|
|
|
|
<secondary>in PL/Tcl</secondary>
|
|
|
|
</indexterm>
|
|
|
|
</term>
|
2002-01-23 22:08:17 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-11-21 22:17:07 +01:00
|
|
|
Emits a log or error message. Possible levels are
|
2017-10-09 03:44:17 +02:00
|
|
|
<literal>DEBUG</literal>, <literal>LOG</literal>, <literal>INFO</literal>,
|
|
|
|
<literal>NOTICE</literal>, <literal>WARNING</literal>, <literal>ERROR</literal>, and
|
|
|
|
<literal>FATAL</literal>. <literal>ERROR</literal>
|
2004-11-21 22:17:07 +01:00
|
|
|
raises an error condition; if this is not trapped by the surrounding
|
|
|
|
Tcl code, the error propagates out to the calling query, causing
|
|
|
|
the current transaction or subtransaction to be aborted. This
|
2017-10-09 03:44:17 +02:00
|
|
|
is effectively the same as the Tcl <literal>error</literal> command.
|
|
|
|
<literal>FATAL</literal> aborts the transaction and causes the current
|
2004-11-21 22:17:07 +01:00
|
|
|
session to shut down. (There is probably no good reason to use
|
|
|
|
this error level in PL/Tcl functions, but it's provided for
|
2004-12-30 22:45:37 +01:00
|
|
|
completeness.) The other levels only generate messages of different
|
|
|
|
priority levels.
|
|
|
|
Whether messages of a particular priority are reported to the client,
|
|
|
|
written to the server log, or both is controlled by the
|
2017-11-23 15:39:47 +01:00
|
|
|
<xref linkend="guc-log-min-messages"/> and
|
|
|
|
<xref linkend="guc-client-min-messages"/> configuration
|
|
|
|
variables. See <xref linkend="runtime-config"/>
|
|
|
|
and <xref linkend="pltcl-error-handling"/>
|
2016-03-25 20:52:53 +01:00
|
|
|
for more information.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
</variablelist>
|
2003-04-07 03:29:26 +02:00
|
|
|
</para>
|
2002-01-23 22:08:17 +01:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
</sect1>
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<sect1 id="pltcl-trigger">
|
2000-03-31 00:13:30 +02:00
|
|
|
<title>Trigger Procedures in PL/Tcl</title>
|
|
|
|
|
2001-05-13 00:51:36 +02:00
|
|
|
<indexterm>
|
2003-08-31 19:32:24 +02:00
|
|
|
<primary>trigger</primary>
|
2001-05-13 00:51:36 +02:00
|
|
|
<secondary>in PL/Tcl</secondary>
|
|
|
|
</indexterm>
|
|
|
|
|
2000-03-31 00:13:30 +02:00
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
Trigger procedures can be written in PL/Tcl.
|
|
|
|
<productname>PostgreSQL</productname> requires that a procedure that is to be called
|
2002-01-23 22:08:17 +01:00
|
|
|
as a trigger must be declared as a function with no arguments
|
2017-10-09 03:44:17 +02:00
|
|
|
and a return type of <literal>trigger</literal>.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
<para>
|
2002-01-23 22:08:17 +01:00
|
|
|
The information from the trigger manager is passed to the procedure body
|
2000-03-31 00:13:30 +02:00
|
|
|
in the following variables:
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
|
|
|
|
<varlistentry>
|
2003-04-07 03:29:26 +02:00
|
|
|
<term><varname>$TG_name</varname></term>
|
2000-03-31 00:13:30 +02:00
|
|
|
<listitem>
|
2004-12-30 22:45:37 +01:00
|
|
|
<para>
|
|
|
|
The name of the trigger from the <command>CREATE TRIGGER</command> statement.
|
|
|
|
</para>
|
2000-03-31 00:13:30 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2003-04-07 03:29:26 +02:00
|
|
|
<term><varname>$TG_relid</varname></term>
|
2000-03-31 00:13:30 +02:00
|
|
|
<listitem>
|
2004-12-30 22:45:37 +01:00
|
|
|
<para>
|
|
|
|
The object ID of the table that caused the trigger procedure
|
|
|
|
to be invoked.
|
|
|
|
</para>
|
2000-03-31 00:13:30 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2006-05-27 22:24:16 +02:00
|
|
|
<varlistentry>
|
|
|
|
<term><varname>$TG_table_name</varname></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the table that caused the trigger procedure
|
|
|
|
to be invoked.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><varname>$TG_table_schema</varname></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The schema of the table that caused the trigger procedure
|
|
|
|
to be invoked.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
2000-03-31 00:13:30 +02:00
|
|
|
<varlistentry>
|
2003-04-07 03:29:26 +02:00
|
|
|
<term><varname>$TG_relatts</varname></term>
|
2000-03-31 00:13:30 +02:00
|
|
|
<listitem>
|
2004-12-30 22:45:37 +01:00
|
|
|
<para>
|
|
|
|
A Tcl list of the table column names, prefixed with an empty list
|
2017-10-09 03:44:17 +02:00
|
|
|
element. So looking up a column name in the list with <application>Tcl</application>'s
|
|
|
|
<function>lsearch</function> command returns the element's number starting
|
2004-12-30 22:45:37 +01:00
|
|
|
with 1 for the first column, the same way the columns are customarily
|
|
|
|
numbered in <productname>PostgreSQL</productname>. (Empty list
|
|
|
|
elements also appear in the positions of columns that have been
|
|
|
|
dropped, so that the attribute numbering is correct for columns
|
|
|
|
to their right.)
|
|
|
|
</para>
|
2000-03-31 00:13:30 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2003-04-07 03:29:26 +02:00
|
|
|
<term><varname>$TG_when</varname></term>
|
2000-03-31 00:13:30 +02:00
|
|
|
<listitem>
|
2004-12-30 22:45:37 +01:00
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
The string <literal>BEFORE</literal>, <literal>AFTER</literal>, or
|
|
|
|
<literal>INSTEAD OF</literal>, depending on the type of trigger event.
|
2004-12-30 22:45:37 +01:00
|
|
|
</para>
|
2000-03-31 00:13:30 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2003-04-07 03:29:26 +02:00
|
|
|
<term><varname>$TG_level</varname></term>
|
2000-03-31 00:13:30 +02:00
|
|
|
<listitem>
|
2004-12-30 22:45:37 +01:00
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
The string <literal>ROW</literal> or <literal>STATEMENT</literal> depending on the
|
2008-03-28 01:21:56 +01:00
|
|
|
type of trigger event.
|
2004-12-30 22:45:37 +01:00
|
|
|
</para>
|
2000-03-31 00:13:30 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2003-04-07 03:29:26 +02:00
|
|
|
<term><varname>$TG_op</varname></term>
|
2000-03-31 00:13:30 +02:00
|
|
|
<listitem>
|
2004-12-30 22:45:37 +01:00
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
The string <literal>INSERT</literal>, <literal>UPDATE</literal>,
|
|
|
|
<literal>DELETE</literal>, or <literal>TRUNCATE</literal> depending on the type of
|
2008-03-28 01:21:56 +01:00
|
|
|
trigger event.
|
2004-12-30 22:45:37 +01:00
|
|
|
</para>
|
2000-03-31 00:13:30 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2003-04-07 03:29:26 +02:00
|
|
|
<term><varname>$NEW</varname></term>
|
2000-03-31 00:13:30 +02:00
|
|
|
<listitem>
|
2004-12-30 22:45:37 +01:00
|
|
|
<para>
|
|
|
|
An associative array containing the values of the new table
|
2017-10-09 03:44:17 +02:00
|
|
|
row for <command>INSERT</command> or <command>UPDATE</command> actions, or
|
|
|
|
empty for <command>DELETE</command>. The array is indexed by column
|
2004-12-30 22:45:37 +01:00
|
|
|
name. Columns that are null will not appear in the array.
|
2008-03-28 01:21:56 +01:00
|
|
|
This is not set for statement-level triggers.
|
2004-12-30 22:45:37 +01:00
|
|
|
</para>
|
2000-03-31 00:13:30 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2003-04-07 03:29:26 +02:00
|
|
|
<term><varname>$OLD</varname></term>
|
2000-03-31 00:13:30 +02:00
|
|
|
<listitem>
|
2004-12-30 22:45:37 +01:00
|
|
|
<para>
|
|
|
|
An associative array containing the values of the old table
|
2017-10-09 03:44:17 +02:00
|
|
|
row for <command>UPDATE</command> or <command>DELETE</command> actions, or
|
|
|
|
empty for <command>INSERT</command>. The array is indexed by column
|
2004-12-30 22:45:37 +01:00
|
|
|
name. Columns that are null will not appear in the array.
|
2008-03-28 01:21:56 +01:00
|
|
|
This is not set for statement-level triggers.
|
2004-12-30 22:45:37 +01:00
|
|
|
</para>
|
2000-03-31 00:13:30 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2003-04-07 03:29:26 +02:00
|
|
|
<term><varname>$args</varname></term>
|
2000-03-31 00:13:30 +02:00
|
|
|
<listitem>
|
2004-12-30 22:45:37 +01:00
|
|
|
<para>
|
|
|
|
A Tcl list of the arguments to the procedure as given in the
|
|
|
|
<command>CREATE TRIGGER</command> statement. These arguments are also accessible as
|
|
|
|
<literal>$1</literal> ... <literal>$<replaceable>n</replaceable></literal> in the procedure body.
|
|
|
|
</para>
|
2000-03-31 00:13:30 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2002-01-23 22:08:17 +01:00
|
|
|
The return value from a trigger procedure can be one of the strings
|
2017-10-09 03:44:17 +02:00
|
|
|
<literal>OK</literal> or <literal>SKIP</literal>, or a list of column name/value pairs.
|
|
|
|
If the return value is <literal>OK</literal>,
|
|
|
|
the operation (<command>INSERT</command>/<command>UPDATE</command>/<command>DELETE</command>)
|
2016-11-06 20:43:13 +01:00
|
|
|
that fired the trigger will proceed
|
2017-10-09 03:44:17 +02:00
|
|
|
normally. <literal>SKIP</literal> tells the trigger manager to silently suppress
|
2002-01-23 22:08:17 +01:00
|
|
|
the operation for this row. If a list is returned, it tells PL/Tcl to
|
2016-11-06 20:43:13 +01:00
|
|
|
return a modified row to the trigger manager; the contents of the
|
|
|
|
modified row are specified by the column names and values in the list.
|
|
|
|
Any columns not mentioned in the list are set to null.
|
|
|
|
Returning a modified row is only meaningful
|
2017-10-09 03:44:17 +02:00
|
|
|
for row-level <literal>BEFORE</literal> <command>INSERT</command> or <command>UPDATE</command>
|
2016-11-06 20:43:13 +01:00
|
|
|
triggers, for which the modified row will be inserted instead of the one
|
2017-10-09 03:44:17 +02:00
|
|
|
given in <varname>$NEW</varname>; or for row-level <literal>INSTEAD OF</literal>
|
|
|
|
<command>INSERT</command> or <command>UPDATE</command> triggers where the returned row
|
|
|
|
is used as the source data for <command>INSERT RETURNING</command> or
|
|
|
|
<command>UPDATE RETURNING</command> clauses.
|
|
|
|
In row-level <literal>BEFORE</literal> <command>DELETE</command> or <literal>INSTEAD
|
|
|
|
OF</literal> <command>DELETE</command> triggers, returning a modified row has the same
|
|
|
|
effect as returning <literal>OK</literal>, that is the operation proceeds.
|
2016-11-06 20:43:13 +01:00
|
|
|
The trigger return value is ignored for all other types of triggers.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
2008-03-28 01:21:56 +01:00
|
|
|
|
2016-11-06 20:43:13 +01:00
|
|
|
<tip>
|
|
|
|
<para>
|
|
|
|
The result list can be made from an array representation of the
|
2017-10-09 03:44:17 +02:00
|
|
|
modified tuple with the <literal>array get</literal> Tcl command.
|
2016-11-06 20:43:13 +01:00
|
|
|
</para>
|
|
|
|
</tip>
|
|
|
|
|
2000-03-31 00:13:30 +02:00
|
|
|
<para>
|
|
|
|
Here's a little example trigger procedure that forces an integer value
|
2001-02-09 04:06:38 +01:00
|
|
|
in a table to keep track of the number of updates that are performed on the
|
|
|
|
row. For new rows inserted, the value is initialized to 0 and then
|
2003-04-07 03:29:26 +02:00
|
|
|
incremented on every update operation.
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<programlisting>
|
2004-05-17 01:22:08 +02:00
|
|
|
CREATE FUNCTION trigfunc_modcount() RETURNS trigger AS $$
|
2000-03-31 00:13:30 +02:00
|
|
|
switch $TG_op {
|
|
|
|
INSERT {
|
|
|
|
set NEW($1) 0
|
|
|
|
}
|
|
|
|
UPDATE {
|
|
|
|
set NEW($1) $OLD($1)
|
|
|
|
incr NEW($1)
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
return OK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [array get NEW]
|
2004-05-17 01:22:08 +02:00
|
|
|
$$ LANGUAGE pltcl;
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2002-01-23 22:08:17 +01:00
|
|
|
CREATE TABLE mytab (num integer, description text, modcnt integer);
|
2000-03-31 00:13:30 +02:00
|
|
|
|
|
|
|
CREATE TRIGGER trig_mytab_modcount BEFORE INSERT OR UPDATE ON mytab
|
|
|
|
FOR EACH ROW EXECUTE PROCEDURE trigfunc_modcount('modcnt');
|
2003-04-07 03:29:26 +02:00
|
|
|
</programlisting>
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2002-01-23 22:08:17 +01:00
|
|
|
Notice that the trigger procedure itself does not know the column
|
|
|
|
name; that's supplied from the trigger arguments. This lets the
|
2003-04-07 03:29:26 +02:00
|
|
|
trigger procedure be reused with different tables.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
2003-04-07 03:29:26 +02:00
|
|
|
</sect1>
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2013-11-24 03:32:00 +01:00
|
|
|
<sect1 id="pltcl-event-trigger">
|
|
|
|
<title>Event Trigger Procedures in PL/Tcl</title>
|
|
|
|
|
|
|
|
<indexterm>
|
|
|
|
<primary>event trigger</primary>
|
|
|
|
<secondary>in PL/Tcl</secondary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Event trigger procedures can be written in PL/Tcl.
|
|
|
|
<productname>PostgreSQL</productname> requires that a procedure that is
|
|
|
|
to be called as an event trigger must be declared as a function with no
|
2017-10-09 03:44:17 +02:00
|
|
|
arguments and a return type of <literal>event_trigger</literal>.
|
2013-11-24 03:32:00 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The information from the trigger manager is passed to the procedure body
|
|
|
|
in the following variables:
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><varname>$TG_event</varname></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the event the trigger is fired for.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><varname>$TG_tag</varname></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The command tag for which the trigger is fired.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The return value of the trigger procedure is ignored.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Here's a little example event trigger procedure that simply raises
|
|
|
|
a <literal>NOTICE</literal> message each time a supported command is
|
|
|
|
executed:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE OR REPLACE FUNCTION tclsnitch() RETURNS event_trigger AS $$
|
|
|
|
elog NOTICE "tclsnitch: $TG_event $TG_tag"
|
|
|
|
$$ LANGUAGE pltcl;
|
|
|
|
|
|
|
|
CREATE EVENT TRIGGER tcl_a_snitch ON ddl_command_start EXECUTE PROCEDURE tclsnitch();
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
2016-03-25 20:52:53 +01:00
|
|
|
<sect1 id="pltcl-error-handling">
|
|
|
|
<title>Error Handling in PL/Tcl</title>
|
|
|
|
|
|
|
|
<indexterm>
|
|
|
|
<primary>exceptions</primary>
|
|
|
|
<secondary>in PL/Tcl</secondary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Tcl code within or called from a PL/Tcl function can raise an error,
|
|
|
|
either by executing some invalid operation or by generating an error
|
|
|
|
using the Tcl <function>error</function> command or
|
|
|
|
PL/Tcl's <function>elog</function> command. Such errors can be caught
|
2017-03-11 20:37:05 +01:00
|
|
|
within Tcl using the Tcl <function>catch</function> command. If an
|
|
|
|
error is not caught but is allowed to propagate out to the top level of
|
|
|
|
execution of the PL/Tcl function, it is reported as a SQL error in the
|
|
|
|
function's calling query.
|
2016-03-25 20:52:53 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2017-03-11 20:37:05 +01:00
|
|
|
Conversely, SQL errors that occur within PL/Tcl's
|
2016-03-25 20:52:53 +01:00
|
|
|
<function>spi_exec</function>, <function>spi_prepare</function>,
|
|
|
|
and <function>spi_execp</function> commands are reported as Tcl errors,
|
|
|
|
so they are catchable by Tcl's <function>catch</function> command.
|
2017-03-11 20:37:05 +01:00
|
|
|
(Each of these PL/Tcl commands runs its SQL operation in a
|
|
|
|
subtransaction, which is rolled back on error, so that any
|
|
|
|
partially-completed operation is automatically cleaned up.)
|
|
|
|
Again, if an error propagates out to the top level without being caught,
|
|
|
|
it turns back into a SQL error.
|
2016-03-25 20:52:53 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Tcl provides an <varname>errorCode</varname> variable that can represent
|
|
|
|
additional information about an error in a form that is easy for Tcl
|
|
|
|
programs to interpret. The contents are in Tcl list format, and the
|
|
|
|
first word identifies the subsystem or library reporting the error;
|
|
|
|
beyond that the contents are left to the individual subsystem or
|
|
|
|
library. For database errors reported by PL/Tcl commands, the first
|
2017-06-18 20:01:45 +02:00
|
|
|
word is <literal>POSTGRES</literal>, the second word is the PostgreSQL
|
2016-03-25 20:52:53 +01:00
|
|
|
version number, and additional words are field name/value pairs
|
|
|
|
providing detailed information about the error.
|
2017-10-09 03:44:17 +02:00
|
|
|
Fields <varname>SQLSTATE</varname>, <varname>condition</varname>,
|
|
|
|
and <varname>message</varname> are always supplied
|
2016-03-25 21:54:52 +01:00
|
|
|
(the first two represent the error code and condition name as shown
|
2017-11-23 15:39:47 +01:00
|
|
|
in <xref linkend="errcodes-appendix"/>).
|
2016-03-25 20:52:53 +01:00
|
|
|
Fields that may be present include
|
2017-10-09 03:44:17 +02:00
|
|
|
<varname>detail</varname>, <varname>hint</varname>, <varname>context</varname>,
|
|
|
|
<varname>schema</varname>, <varname>table</varname>, <varname>column</varname>,
|
|
|
|
<varname>datatype</varname>, <varname>constraint</varname>,
|
|
|
|
<varname>statement</varname>, <varname>cursor_position</varname>,
|
|
|
|
<varname>filename</varname>, <varname>lineno</varname>, and
|
|
|
|
<varname>funcname</varname>.
|
2016-03-25 20:52:53 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A convenient way to work with PL/Tcl's <varname>errorCode</varname>
|
|
|
|
information is to load it into an array, so that the field names become
|
|
|
|
array subscripts. Code for doing that might look like
|
|
|
|
<programlisting>
|
|
|
|
if {[catch { spi_exec $sql_command }]} {
|
|
|
|
if {[lindex $::errorCode 0] == "POSTGRES"} {
|
|
|
|
array set errorArray $::errorCode
|
2016-03-25 21:54:52 +01:00
|
|
|
if {$errorArray(condition) == "undefined_table"} {
|
2016-03-25 20:52:53 +01:00
|
|
|
# deal with missing table
|
|
|
|
} else {
|
|
|
|
# deal with some other type of SQL error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</programlisting>
|
|
|
|
(The double colons explicitly specify that <varname>errorCode</varname>
|
|
|
|
is a global variable.)
|
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
2017-03-11 20:37:05 +01:00
|
|
|
<sect1 id="pltcl-subtransactions">
|
|
|
|
<title>Explicit Subtransactions in PL/Tcl</title>
|
|
|
|
|
|
|
|
<indexterm>
|
|
|
|
<primary>subtransactions</primary>
|
|
|
|
<secondary>in PL/Tcl</secondary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Recovering from errors caused by database access as described in
|
2017-11-23 15:39:47 +01:00
|
|
|
<xref linkend="pltcl-error-handling"/> can lead to an undesirable
|
2017-03-11 20:37:05 +01:00
|
|
|
situation where some operations succeed before one of them fails,
|
|
|
|
and after recovering from that error the data is left in an
|
|
|
|
inconsistent state. PL/Tcl offers a solution to this problem in
|
|
|
|
the form of explicit subtransactions.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Consider a function that implements a transfer between two accounts:
|
|
|
|
<programlisting>
|
|
|
|
CREATE FUNCTION transfer_funds() RETURNS void AS $$
|
|
|
|
if [catch {
|
|
|
|
spi_exec "UPDATE accounts SET balance = balance - 100 WHERE account_name = 'joe'"
|
|
|
|
spi_exec "UPDATE accounts SET balance = balance + 100 WHERE account_name = 'mary'"
|
|
|
|
} errormsg] {
|
|
|
|
set result [format "error transferring funds: %s" $errormsg]
|
|
|
|
} else {
|
|
|
|
set result "funds transferred successfully"
|
|
|
|
}
|
|
|
|
spi_exec "INSERT INTO operations (result) VALUES ('[quote $result]')"
|
|
|
|
$$ LANGUAGE pltcl;
|
|
|
|
</programlisting>
|
|
|
|
If the second <command>UPDATE</command> statement results in an
|
|
|
|
exception being raised, this function will log the failure, but
|
|
|
|
the result of the first <command>UPDATE</command> will
|
|
|
|
nevertheless be committed. In other words, the funds will be
|
|
|
|
withdrawn from Joe's account, but will not be transferred to
|
|
|
|
Mary's account. This happens because each <function>spi_exec</function>
|
|
|
|
is a separate subtransaction, and only one of those subtransactions
|
|
|
|
got rolled back.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To handle such cases, you can wrap multiple database operations in an
|
|
|
|
explicit subtransaction, which will succeed or roll back as a whole.
|
|
|
|
PL/Tcl provides a <function>subtransaction</function> command to manage
|
|
|
|
this. We can rewrite our function as:
|
|
|
|
<programlisting>
|
|
|
|
CREATE FUNCTION transfer_funds2() RETURNS void AS $$
|
|
|
|
if [catch {
|
|
|
|
subtransaction {
|
|
|
|
spi_exec "UPDATE accounts SET balance = balance - 100 WHERE account_name = 'joe'"
|
|
|
|
spi_exec "UPDATE accounts SET balance = balance + 100 WHERE account_name = 'mary'"
|
|
|
|
}
|
|
|
|
} errormsg] {
|
|
|
|
set result [format "error transferring funds: %s" $errormsg]
|
|
|
|
} else {
|
|
|
|
set result "funds transferred successfully"
|
|
|
|
}
|
|
|
|
spi_exec "INSERT INTO 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>
|
|
|
|
|
Transaction control in PL procedures
In each of the supplied procedural languages (PL/pgSQL, PL/Perl,
PL/Python, PL/Tcl), add language-specific commit and rollback
functions/commands to control transactions in procedures in that
language. Add similar underlying functions to SPI. Some additional
cleanup so that transaction commit or abort doesn't blow away data
structures still used by the procedure call. Add execution context
tracking to CALL and DO statements so that transaction control commands
can only be issued in top-level procedure and block calls, not function
calls or other procedure or block calls.
- SPI
Add a new function SPI_connect_ext() that is like SPI_connect() but
allows passing option flags. The only option flag right now is
SPI_OPT_NONATOMIC. A nonatomic SPI connection can execute transaction
control commands, otherwise it's not allowed. This is meant to be
passed down from CALL and DO statements which themselves know in which
context they are called. A nonatomic SPI connection uses different
memory management. A normal SPI connection allocates its memory in
TopTransactionContext. For nonatomic connections we use PortalContext
instead. As the comment in SPI_connect_ext() (previously SPI_connect())
indicates, one could potentially use PortalContext in all cases, but it
seems safest to leave the existing uses alone, because this stuff is
complicated enough already.
SPI also gets new functions SPI_start_transaction(), SPI_commit(), and
SPI_rollback(), which can be used by PLs to implement their transaction
control logic.
- portalmem.c
Some adjustments were made in the code that cleans up portals at
transaction abort. The portal code could already handle a command
*committing* a transaction and continuing (e.g., VACUUM), but it was not
quite prepared for a command *aborting* a transaction and continuing.
In AtAbort_Portals(), remove the code that marks an active portal as
failed. As the comment there already predicted, this doesn't work if
the running command wants to keep running after transaction abort. And
it's actually not necessary, because pquery.c is careful to run all
portal code in a PG_TRY block and explicitly runs MarkPortalFailed() if
there is an exception. So the code in AtAbort_Portals() is never used
anyway.
In AtAbort_Portals() and AtCleanup_Portals(), we need to be careful not
to clean up active portals too much. This mirrors similar code in
PreCommit_Portals().
- PL/Perl
Gets new functions spi_commit() and spi_rollback()
- PL/pgSQL
Gets new commands COMMIT and ROLLBACK.
Update the PL/SQL porting example in the documentation to reflect that
transactions are now possible in procedures.
- PL/Python
Gets new functions plpy.commit and plpy.rollback.
- PL/Tcl
Gets new commands commit and rollback.
Reviewed-by: Andrew Dunstan <andrew.dunstan@2ndquadrant.com>
2018-01-22 14:30:16 +01:00
|
|
|
<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>
|
|
|
|
|
2017-03-07 18:40:44 +01:00
|
|
|
<sect1 id="pltcl-config">
|
|
|
|
<title>PL/Tcl Configuration</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
This section lists configuration parameters that
|
2017-10-09 03:44:17 +02:00
|
|
|
affect <application>PL/Tcl</application>.
|
2017-03-07 18:40:44 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
|
|
|
|
<varlistentry id="guc-pltcl-start-proc" xreflabel="pltcl.start_proc">
|
|
|
|
<term>
|
|
|
|
<varname>pltcl.start_proc</varname> (<type>string</type>)
|
|
|
|
<indexterm>
|
2017-10-09 03:44:17 +02:00
|
|
|
<primary><varname>pltcl.start_proc</varname> configuration parameter</primary>
|
2017-03-07 18:40:44 +01:00
|
|
|
</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
|
|
|
|
when a PL/Tcl function is first executed in a database session, or
|
|
|
|
when an additional interpreter has to be created because a PL/Tcl
|
|
|
|
function is called by a new SQL role.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
The referenced function must be written in the <literal>pltcl</literal>
|
|
|
|
language, and must not be marked <literal>SECURITY DEFINER</literal>.
|
2017-03-07 18:40:44 +01:00
|
|
|
(These restrictions ensure that it runs in the interpreter it's
|
|
|
|
supposed to initialize.) The current user must have permission to
|
|
|
|
call it, too.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
If the function fails with an error it will abort the function call
|
|
|
|
that caused the new interpreter to be created and propagate out to
|
|
|
|
the calling query, causing the current transaction or subtransaction
|
|
|
|
to be aborted. Any actions already done within Tcl won't be undone;
|
|
|
|
however, that interpreter won't be used again. If the language is
|
|
|
|
used again the initialization will be attempted again within a fresh
|
|
|
|
Tcl interpreter.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Only superusers can change this setting. Although this setting
|
|
|
|
can be changed within a session, such changes will not affect Tcl
|
|
|
|
interpreters that have already been created.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry id="guc-pltclu-start-proc" xreflabel="pltclu.start_proc">
|
|
|
|
<term>
|
|
|
|
<varname>pltclu.start_proc</varname> (<type>string</type>)
|
|
|
|
<indexterm>
|
2017-10-09 03:44:17 +02:00
|
|
|
<primary><varname>pltclu.start_proc</varname> configuration parameter</primary>
|
2017-03-07 18:40:44 +01:00
|
|
|
</indexterm>
|
|
|
|
</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
This parameter is exactly like <varname>pltcl.start_proc</varname>,
|
|
|
|
except that it applies to PL/TclU. The referenced function must
|
2017-10-09 03:44:17 +02:00
|
|
|
be written in the <literal>pltclu</literal> language.
|
2017-03-07 18:40:44 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
</variablelist>
|
|
|
|
</sect1>
|
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<sect1 id="pltcl-procnames">
|
2002-01-23 22:08:17 +01:00
|
|
|
<title>Tcl Procedure Names</title>
|
|
|
|
|
|
|
|
<para>
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
In <productname>PostgreSQL</productname>, the same function name can be used for
|
|
|
|
different function definitions as long as the number of arguments or their types
|
2002-01-23 22:08:17 +01:00
|
|
|
differ. Tcl, however, requires all procedure names to be distinct.
|
|
|
|
PL/Tcl deals with this by making the internal Tcl procedure names contain
|
2010-11-23 21:27:50 +01:00
|
|
|
the object
|
2017-10-09 03:44:17 +02:00
|
|
|
ID of the function from the system table <structname>pg_proc</structname> as part of their name. Thus,
|
2002-01-23 22:08:17 +01:00
|
|
|
<productname>PostgreSQL</productname> functions with the same name
|
2003-04-07 03:29:26 +02:00
|
|
|
and different argument types will be different Tcl procedures, too. This
|
2002-01-23 22:08:17 +01:00
|
|
|
is not normally a concern for a PL/Tcl programmer, but it might be visible
|
|
|
|
when debugging.
|
|
|
|
</para>
|
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
</sect1>
|
2000-03-31 00:13:30 +02:00
|
|
|
</chapter>
|