2000-03-31 05:27:42 +02:00
|
|
|
<!--
|
2003-06-22 18:17:01 +02:00
|
|
|
$Header: /cvsroot/pgsql/doc/src/sgml/pltcl.sgml,v 2.24 2003/06/22 16:17:00 tgl Exp $
|
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
|
2002-01-23 22:08:17 +01:00
|
|
|
that enables the Tcl language 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>
|
|
|
|
PL/Tcl offers most of the capabilities a function
|
|
|
|
writer has in the C language, except for some restrictions.
|
|
|
|
</para>
|
|
|
|
<para>
|
2001-02-09 04:06:38 +01:00
|
|
|
The good restriction is that everything is executed in a safe
|
|
|
|
Tcl interpreter. In addition to the limited command set of safe Tcl, only
|
|
|
|
a few commands are available to access the database via SPI and to raise
|
2001-09-10 23:58:47 +02:00
|
|
|
messages via <function>elog()</>. There is no way to access internals of the
|
2003-04-07 03:29:26 +02:00
|
|
|
database server or to gain OS-level access under the permissions of the
|
|
|
|
<productname>PostgreSQL</productname> server process, as a C function can do.
|
2000-03-31 00:13:30 +02:00
|
|
|
Thus, any unprivileged database user may be
|
|
|
|
permitted to use this language.
|
|
|
|
</para>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
The other, implementation restriction is that Tcl functions cannot
|
2001-02-09 04:06:38 +01:00
|
|
|
be used to create input/output functions for new data 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
|
|
|
|
email. To handle these cases, there is a variant of <application>PL/Tcl</> called <literal>PL/TclU</>
|
2001-02-09 04:06:38 +01:00
|
|
|
(for untrusted Tcl). This is the exact same language except that a full
|
2002-09-21 20:32:54 +02:00
|
|
|
Tcl interpreter is used. <emphasis>If <application>PL/TclU</> is used, it must be
|
2001-02-09 04:06:38 +01:00
|
|
|
installed as an untrusted procedural language</emphasis> so that only
|
2002-09-21 20:32:54 +02:00
|
|
|
database superusers can create functions in it. The writer of a <application>PL/TclU</>
|
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>
|
2002-09-21 20:32:54 +02:00
|
|
|
The shared object for the <application>PL/Tcl</> and <application>PL/TclU</> call handlers is
|
2001-02-09 04:06:38 +01:00
|
|
|
automatically built and installed in the
|
2001-11-21 06:53:41 +01:00
|
|
|
<productname>PostgreSQL</productname>
|
2001-02-09 04:06:38 +01:00
|
|
|
library directory if Tcl/Tk support is specified
|
|
|
|
in the configuration step of the installation procedure. To install
|
2002-09-21 20:32:54 +02:00
|
|
|
<application>PL/Tcl</> and/or <application>PL/TclU</> in a particular database, use the
|
2003-04-07 03:29:26 +02:00
|
|
|
<command>createlang</command> program, for example
|
2002-01-23 22:08:17 +01:00
|
|
|
<literal>createlang pltcl <replaceable>dbname</></literal> or
|
|
|
|
<literal>createlang pltclu <replaceable>dbname</></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>
|
2003-04-07 03:29:26 +02:00
|
|
|
To create a function in the <application>PL/Tcl</> language, use the standard syntax:
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<programlisting>
|
2001-02-09 04:06:38 +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
|
2003-04-07 03:29:26 +02:00
|
|
|
' LANGUAGE pltcl;
|
|
|
|
</programlisting>
|
2000-03-31 00:13:30 +02:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<application>PL/TclU</> is the same, except that the language has to be specified as
|
2002-09-21 20:32:54 +02:00
|
|
|
<literal>pltclu</>.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The body of the function is simply a piece of Tcl script.
|
|
|
|
When the function is called, the argument values are passed as
|
2003-04-07 03:29:26 +02:00
|
|
|
variables <literal>$1</literal> ... <literal>$<replaceable>n</replaceable></literal> to the
|
2002-01-23 22:08:17 +01:00
|
|
|
Tcl script. The result is returned
|
2001-05-01 00:22:34 +02:00
|
|
|
from the Tcl code in the usual way, with a <literal>return</literal>
|
2003-04-07 03:29:26 +02:00
|
|
|
statement.
|
|
|
|
</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>
|
|
|
|
CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS '
|
2002-01-23 22:08:17 +01:00
|
|
|
if {$1 > $2} {return $1}
|
|
|
|
return $2
|
2003-04-07 03:29:26 +02:00
|
|
|
' LANGUAGE pltcl STRICT;
|
|
|
|
</programlisting>
|
2002-01-23 22:08:17 +01:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
Note the clause <literal>STRICT</>, which saves us from
|
|
|
|
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
|
2002-03-22 20:20:45 +01:00
|
|
|
<literal>argisnull</>. 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>
|
|
|
|
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 }
|
2000-03-31 00:13:30 +02:00
|
|
|
if {$1 > $2} {return $1}
|
|
|
|
return $2
|
2003-04-07 03:29:26 +02:00
|
|
|
' LANGUAGE pltcl;
|
|
|
|
</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,
|
|
|
|
to return a NULL value from a PL/Tcl function, execute
|
|
|
|
<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
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE FUNCTION overpaid(employee) RETURNS boolean AS '
|
2000-03-31 00:13:30 +02:00
|
|
|
if {200000.0 < $1(salary)} {
|
|
|
|
return "t"
|
|
|
|
}
|
|
|
|
if {$1(age) < 30 && 100000.0 < $1(salary)} {
|
|
|
|
return "t"
|
|
|
|
}
|
|
|
|
return "f"
|
2003-04-07 03:29:26 +02:00
|
|
|
' LANGUAGE pltcl;
|
|
|
|
</programlisting>
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
|
2002-01-23 22:08:17 +01:00
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
There is currently no support for returning a composite-type
|
2002-01-23 22:08:17 +01:00
|
|
|
result value.
|
|
|
|
</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
|
2003-04-07 03:29:26 +02:00
|
|
|
displayed by a <command>SELECT</> statement). Conversely, the <literal>return</>
|
2002-01-23 22:08:17 +01:00
|
|
|
command will accept any string that is acceptable input format for
|
|
|
|
the function's declared return type. So, the PL/Tcl programmer can
|
|
|
|
manipulate data values as if they were just text.
|
|
|
|
</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>
|
|
|
|
|
|
|
|
<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.
|
2002-01-23 22:08:17 +01:00
|
|
|
This is easily done since
|
2003-04-07 03:29:26 +02:00
|
|
|
all PL/Tcl functions executed in one session share the same
|
2002-01-23 22:08:17 +01:00
|
|
|
safe Tcl interpreter. So, any global Tcl variable is accessible to
|
2003-04-07 03:29:26 +02:00
|
|
|
all PL/Tcl function calls and will persist for the duration of the
|
|
|
|
SQL session. (Note that <application>PL/TclU</> functions likewise share
|
2002-01-23 22:08:17 +01:00
|
|
|
global data, but they are in a different Tcl interpreter and cannot
|
|
|
|
communicate with PL/Tcl functions.)
|
2001-02-09 04:06:38 +01:00
|
|
|
</para>
|
|
|
|
<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
|
2003-04-07 03:29:26 +02:00
|
|
|
array is made available to each function via the <function>upvar</>
|
|
|
|
command. The global name of this variable is the function's internal
|
|
|
|
name, and the local name is <literal>GD</>. It is recommended that
|
2002-01-23 22:08:17 +01:00
|
|
|
<literal>GD</> be used
|
2003-04-07 03:29:26 +02:00
|
|
|
for private data of a function. Use regular Tcl global variables
|
2001-02-09 04:06:38 +01:00
|
|
|
only for values that you specifically intend to be shared among multiple
|
2003-04-07 03:29:26 +02:00
|
|
|
functions.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
<para>
|
|
|
|
An example of using <literal>GD</> appears in the
|
|
|
|
<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-04-07 03:29:26 +02:00
|
|
|
<term><function>spi_exec</function> <literal>?-count <replaceable>n</replaceable>? ?-array <replaceable>name</replaceable>? <replaceable>command</replaceable> ?<replaceable>loop-body</replaceable>?</literal></term>
|
2002-01-23 22:08:17 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02: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>
|
2002-01-23 22:08:17 +01:00
|
|
|
is the number of rows processed (selected, inserted, updated, or
|
2003-04-07 03:29:26 +02:00
|
|
|
deleted) by the command, or zero if the command is a utility
|
|
|
|
statement. In addition, if the command is a <command>SELECT</> statement, the
|
2002-01-23 22:08:17 +01:00
|
|
|
values of the selected columns are placed in Tcl variables as
|
|
|
|
described below.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The optional <literal>-count</> value tells
|
|
|
|
<function>spi_exec</function> the maximum number of rows
|
2003-04-07 03:29:26 +02:00
|
|
|
to process in the command. The effect of this is comparable to
|
|
|
|
setting up a query as a cursor and then saying <literal>FETCH <replaceable>n</></>.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
If the command is a <command>SELECT</> statement, the values of the
|
2002-01-23 22:08:17 +01:00
|
|
|
result columns are placed into Tcl variables named after the columns.
|
|
|
|
If the <literal>-array</> option is given, the column values are
|
2003-04-07 03:29:26 +02:00
|
|
|
instead stored into the named associative array, with the
|
2002-01-23 22:08:17 +01:00
|
|
|
column names used as array indexes.
|
|
|
|
</para>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
If the command is a <command>SELECT</> statement and no <replaceable>loop-body</>
|
2002-01-23 22:08:17 +01:00
|
|
|
script is given, then only the first row of results are stored into
|
2003-04-07 03:29:26 +02:00
|
|
|
Tcl variables; remaining rows, if any, are ignored. No storing occurs
|
2002-01-23 22:08:17 +01:00
|
|
|
if the
|
2003-04-07 03:29:26 +02:00
|
|
|
query returns no rows. (This case can be detected by checking the
|
|
|
|
result of <function>spi_exec</function>.) For example,
|
2002-01-23 22:08:17 +01:00
|
|
|
|
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>
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
will set the Tcl variable <literal>$cnt</> to the number of rows in
|
2002-09-21 20:32:54 +02:00
|
|
|
the <structname>pg_proc</> system catalog.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
If the optional <replaceable>loop-body</> argument is given, it is
|
|
|
|
a piece of Tcl script that is executed once for each row in the
|
2003-04-07 03:29:26 +02:00
|
|
|
query result. (<replaceable>loop-body</> is ignored if the given
|
|
|
|
command is not a <command>SELECT</>.) The values of the current row's columns
|
2002-01-23 22:08:17 +01:00
|
|
|
are stored into Tcl variables 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>
|
2002-01-23 22:08:17 +01:00
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
will print a log message for every row of <literal>pg_class</>. This
|
2002-01-23 22:08:17 +01:00
|
|
|
feature works similarly to other Tcl looping constructs; in
|
|
|
|
particular <literal>continue</> and <literal>break</> work in the
|
|
|
|
usual way inside the loop body.
|
|
|
|
</para>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
If a column of a query result is null, the target
|
2002-01-23 22:08:17 +01:00
|
|
|
variable for it is <quote>unset</> rather than being set.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><function>spi_prepare</function> <replaceable>query</replaceable> <replaceable>typelist</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Prepares and saves a query plan for later execution. The saved plan
|
2003-04-07 03:29:26 +02:00
|
|
|
will be retained for the life of the current session.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
The query may use parameters, that is, placeholders for
|
2002-01-23 22:08:17 +01:00
|
|
|
values to be supplied whenever the plan is actually executed.
|
2003-04-07 03:29:26 +02:00
|
|
|
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
|
2002-01-23 22:08:17 +01:00
|
|
|
must be given as a Tcl list. (Write an empty list for
|
2003-04-07 03:29:26 +02:00
|
|
|
<replaceable>typelist</replaceable> if no parameters are used.)
|
|
|
|
Presently, the parameter types must be identified by the internal
|
|
|
|
type names shown in the system table <literal>pg_type</>; for example <literal>int4</> not
|
2002-01-23 22:08:17 +01:00
|
|
|
<literal>integer</>.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The return value from <function>spi_prepare</function> is a query ID
|
|
|
|
to be used in subsequent calls to <function>spi_execp</function>. See
|
|
|
|
<function>spi_execp</function> for an example.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><function>spi_execp</> <literal>?-count <replaceable>n</replaceable>? ?-array <replaceable>name</replaceable>? ?-nulls <replaceable>string</replaceable>? <replaceable>queryid</replaceable> ?<replaceable>value-list</replaceable>? ?<replaceable>loop-body</replaceable>?</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
Executes a query previously prepared with <function>spi_prepare</>.
|
2002-01-23 22:08:17 +01:00
|
|
|
<replaceable>queryid</replaceable> is the ID returned by
|
2003-04-07 03:29:26 +02:00
|
|
|
<function>spi_prepare</>. If the query references parameters,
|
|
|
|
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
|
2002-01-23 22:08:17 +01:00
|
|
|
<function>spi_prepare</>. Omit <replaceable>value-list</replaceable>
|
2003-04-07 03:29:26 +02:00
|
|
|
if the query has no parameters.
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The optional value for <literal>-nulls</> is a string of spaces and
|
|
|
|
<literal>'n'</> characters telling <function>spi_execp</function>
|
2003-04-07 03:29:26 +02:00
|
|
|
which of the parameters are null values. If given, it must have exactly the
|
2002-01-23 22:08:17 +01:00
|
|
|
same length as the <replaceable>value-list</replaceable>. If it
|
2003-04-07 03:29:26 +02:00
|
|
|
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,
|
2002-01-23 22:08:17 +01:00
|
|
|
<function>spi_execp</> works just like <function>spi_exec</>.
|
|
|
|
The <literal>-count</>, <literal>-array</>, and
|
|
|
|
<replaceable>loop-body</replaceable> options are the same,
|
|
|
|
and so is the result value.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Here's an example of a PL/Tcl function using a prepared plan:
|
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<programlisting>
|
2002-01-23 22:08:17 +01:00
|
|
|
CREATE FUNCTION t1_count(integer, integer) RETURNS integer AS '
|
|
|
|
if {![ info exists GD(plan) ]} {
|
|
|
|
# prepare the saved plan on the first call
|
|
|
|
set GD(plan) [ spi_prepare \\
|
|
|
|
"SELECT count(*) AS cnt FROM t1 WHERE num >= \\$1 AND num <= \\$2" \\
|
|
|
|
[ list int4 int4 ] ]
|
|
|
|
}
|
|
|
|
spi_execp -count 1 $GD(plan) [ list $1 $2 ]
|
|
|
|
return $cnt
|
2003-04-07 03:29:26 +02:00
|
|
|
' LANGUAGE pltcl;
|
|
|
|
</programlisting>
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
Note that each backslash that Tcl should see must be doubled when
|
|
|
|
we type in the function, since the main parser processes
|
2003-04-07 03:29:26 +02:00
|
|
|
backslashes, too, in <command>CREATE FUNCTION</>. We need backslashes inside
|
2002-01-23 22:08:17 +01:00
|
|
|
the query string given to <function>spi_prepare</> to ensure that
|
2003-04-07 03:29:26 +02:00
|
|
|
the <literal>$<replaceable>n</replaceable></> markers will be passed through to
|
2002-01-23 22:08:17 +01:00
|
|
|
<function>spi_prepare</> as-is, and not
|
|
|
|
replaced by Tcl variable substitution.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<indexterm>
|
|
|
|
<primary>spi_lastoid</primary>
|
|
|
|
</indexterm>
|
|
|
|
<term><function>spi_lastoid</></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Returns the OID of the row inserted by the last
|
2003-04-07 03:29:26 +02:00
|
|
|
<function>spi_exec</> or <function>spi_execp</>,
|
|
|
|
if the command was a single-row <command>INSERT</>. (If not, you get zero.)
|
2002-01-23 22:08:17 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><function>quote</> <replaceable>string</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Duplicates all occurrences of single quote and backslash characters
|
|
|
|
in the given string. This may be used to safely quote strings
|
2003-04-07 03:29:26 +02:00
|
|
|
that are to be inserted into SQL commands given
|
2002-01-23 22:08:17 +01:00
|
|
|
to <function>spi_exec</function> or
|
|
|
|
<function>spi_prepare</function>.
|
2003-04-07 03:29:26 +02:00
|
|
|
For example, think about an SQL command string like
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
"SELECT '$val' AS ret"
|
|
|
|
</programlisting>
|
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
where the Tcl variable <literal>val</> actually contains
|
2002-01-23 22:08:17 +01:00
|
|
|
<literal>doesn't</literal>. This would result
|
2003-04-07 03:29:26 +02:00
|
|
|
in the final command string
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
SELECT 'doesn't' AS ret
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
which would cause a parse error during
|
|
|
|
<function>spi_exec</function> or
|
|
|
|
<function>spi_prepare</function>.
|
2003-04-07 03:29:26 +02:00
|
|
|
The submitted command should contain
|
2002-01-23 22:08:17 +01:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
SELECT 'doesn''t' AS ret
|
|
|
|
</programlisting>
|
|
|
|
|
2003-04-07 03:29:26 +02: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
|
2003-04-07 03:29:26 +02: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>
|
|
|
|
<indexterm>
|
|
|
|
<primary>elog</primary>
|
2003-06-22 18:17:01 +02:00
|
|
|
<secondary>PL/Tcl</secondary>
|
2002-01-23 22:08:17 +01:00
|
|
|
</indexterm>
|
|
|
|
<term><function>elog</> <replaceable>level</replaceable> <replaceable>msg</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
Emits a log or error message. Possible levels are
|
2002-03-06 20:05:58 +01:00
|
|
|
<literal>DEBUG</>, <literal>LOG</>, <literal>INFO</>,
|
|
|
|
<literal>NOTICE</>, <literal>WARNING</>, <literal>ERROR</>, and
|
|
|
|
<literal>FATAL</>. Most simply emit the given message just like
|
2003-04-07 03:29:26 +02:00
|
|
|
the <literal>elog</> C function. <literal>ERROR</>
|
2002-03-06 20:05:58 +01:00
|
|
|
raises an error condition: further execution of the function is
|
|
|
|
abandoned, and the current transaction is aborted.
|
|
|
|
<literal>FATAL</> aborts the transaction and causes the current
|
2003-04-07 03:29:26 +02:00
|
|
|
session to shut down. (There is probably no good reason to use
|
2002-03-06 20:05:58 +01:00
|
|
|
this error level in PL/Tcl functions, but it's provided for
|
2003-04-07 03:29:26 +02:00
|
|
|
completeness.)
|
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>
|
|
|
|
<primary>triggers</primary>
|
|
|
|
<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
|
2002-08-22 02:01:51 +02:00
|
|
|
and a return type of <literal>trigger</>.
|
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>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
The name of the trigger from the <command>CREATE TRIGGER</command> statement.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</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>
|
|
|
|
<para>
|
|
|
|
The object ID of the table that caused the trigger procedure
|
|
|
|
to be invoked.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2003-04-07 03:29:26 +02:00
|
|
|
<term><varname>$TG_relatts</varname></term>
|
2000-03-31 00:13:30 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
A Tcl list of the table column names, prefixed with an empty list
|
|
|
|
element. So looking up a column name in the list with <application>Tcl</>'s
|
2002-01-23 22:08:17 +01:00
|
|
|
<function>lsearch</> command returns the element's number starting
|
2003-04-07 03:29:26 +02:00
|
|
|
with 1 for the first column, the same way the columns are customarily
|
2002-01-23 22:08:17 +01:00
|
|
|
numbered in <productname>PostgreSQL</productname>.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</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>
|
|
|
|
<para>
|
2002-01-23 22:08:17 +01:00
|
|
|
The string <literal>BEFORE</> or <literal>AFTER</> depending on the
|
|
|
|
type of trigger call.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</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>
|
|
|
|
<para>
|
2002-01-23 22:08:17 +01:00
|
|
|
The string <literal>ROW</> or <literal>STATEMENT</> depending on the
|
|
|
|
type of trigger call.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</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>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
The string <literal>INSERT</>, <literal>UPDATE</>, or
|
2002-01-23 22:08:17 +01:00
|
|
|
<literal>DELETE</> depending on the type of trigger call.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</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>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
An associative array containing the values of the new table
|
|
|
|
row for <command>INSERT</> or <command>UPDATE</> actions, or
|
|
|
|
empty for <command>DELETE</>. The array is indexed by column
|
|
|
|
name. Columns that are null will not appear in the array.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</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>
|
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
An associative array containing the values of the old table
|
|
|
|
row for <command>UPDATE</> or <command>DELETE</> actions, or
|
|
|
|
empty for <command>INSERT</>. The array is indexed by column
|
|
|
|
name. Columns that are null will not appear in the array.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</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>
|
|
|
|
<para>
|
|
|
|
A Tcl list of the arguments to the procedure as given in the
|
2003-04-07 03:29:26 +02:00
|
|
|
<command>CREATE TRIGGER</command> statement. These arguments are also accessible as
|
|
|
|
<literal>$1</literal> ... <literal>$<replaceable>n</replaceable></literal> in the procedure body.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
</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
|
|
|
|
<literal>OK</> or <literal>SKIP</>, or a list as returned by the
|
|
|
|
<literal>array get</> Tcl command. If the return value is <literal>OK</>,
|
2003-04-07 03:29:26 +02:00
|
|
|
the operation (<command>INSERT</>/<command>UPDATE</>/<command>DELETE</>) that fired the trigger will proceed
|
2002-01-23 22:08:17 +01:00
|
|
|
normally. <literal>SKIP</> tells the trigger manager to silently suppress
|
|
|
|
the operation for this row. If a list is returned, it tells PL/Tcl to
|
|
|
|
return a modified row to the trigger manager that will be inserted
|
2003-04-07 03:29:26 +02:00
|
|
|
instead of the one given in <varname>$NEW</>. (This works for <command>INSERT</> and <command>UPDATE</>
|
|
|
|
only.) Needless to say that all this is only meaningful when the trigger
|
|
|
|
is <literal>BEFORE</> and <command>FOR EACH ROW</>; otherwise the return value is ignored.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
<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>
|
|
|
|
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]
|
2003-04-07 03:29:26 +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
|
|
|
|
2003-04-07 03:29:26 +02:00
|
|
|
<sect1 id="pltcl-unknown">
|
|
|
|
<title>Modules and the <function>unknown</> command</title>
|
2000-03-31 00:13:30 +02:00
|
|
|
<para>
|
2003-04-07 03:29:26 +02:00
|
|
|
PL/Tcl has support for autoloading Tcl code when used.
|
2002-01-23 22:08:17 +01:00
|
|
|
It recognizes a special table, <literal>pltcl_modules</>, which
|
|
|
|
is presumed to contain modules of Tcl code. If this table
|
|
|
|
exists, the module <literal>unknown</> is fetched from the table
|
|
|
|
and loaded into the Tcl interpreter immediately after creating
|
|
|
|
the interpreter.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
<para>
|
2002-01-23 22:08:17 +01:00
|
|
|
While the <literal>unknown</> module could actually contain any
|
|
|
|
initialization script you need, it normally defines a Tcl
|
2003-04-07 03:29:26 +02:00
|
|
|
<function>unknown</> procedure that is invoked whenever Tcl does
|
2002-09-21 20:32:54 +02:00
|
|
|
not recognize an invoked procedure name. <application>PL/Tcl</>'s standard version
|
2002-01-23 22:08:17 +01:00
|
|
|
of this procedure tries to find a module in <literal>pltcl_modules</>
|
|
|
|
that will define the required procedure. If one is found, it is
|
|
|
|
loaded into the interpreter, and then execution is allowed to
|
|
|
|
proceed with the originally attempted procedure call. A
|
|
|
|
secondary table <literal>pltcl_modfuncs</> provides an index of
|
|
|
|
which functions are defined by which modules, so that the lookup
|
|
|
|
is reasonably quick.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
<para>
|
2002-01-23 22:08:17 +01:00
|
|
|
The <productname>PostgreSQL</productname> distribution includes
|
|
|
|
support scripts to maintain these tables:
|
|
|
|
<command>pltcl_loadmod</>, <command>pltcl_listmod</>,
|
|
|
|
<command>pltcl_delmod</>, as well as source for the standard
|
2003-04-07 03:29:26 +02:00
|
|
|
<literal>unknown</> module in <filename>share/unknown.pltcl</>. This module
|
2002-01-23 22:08:17 +01:00
|
|
|
must be loaded
|
|
|
|
into each database initially to support the autoloading mechanism.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
|
|
|
<para>
|
2002-01-23 22:08:17 +01:00
|
|
|
The tables <literal>pltcl_modules</> and <literal>pltcl_modfuncs</>
|
|
|
|
must be readable by all, but it is wise to make them owned and
|
|
|
|
writable only by the database administrator.
|
2000-03-31 00:13:30 +02:00
|
|
|
</para>
|
2003-04-07 03:29:26 +02:00
|
|
|
</sect1>
|
2001-10-13 06:23:50 +02:00
|
|
|
|
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>
|
|
|
|
In <productname>PostgreSQL</productname>, one and the
|
|
|
|
same function name can be used for
|
|
|
|
different functions as long as the number of arguments or their types
|
|
|
|
differ. Tcl, however, requires all procedure names to be distinct.
|
|
|
|
PL/Tcl deals with this by making the internal Tcl procedure names contain
|
|
|
|
the object
|
2003-04-07 03:29:26 +02:00
|
|
|
ID of the function from the system table <structname>pg_proc</> 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>
|
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode:sgml
|
|
|
|
sgml-omittag:nil
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"./reference.ced"
|
|
|
|
sgml-exposed-tags:nil
|
2000-03-31 05:27:42 +02:00
|
|
|
sgml-local-catalogs:("/usr/lib/sgml/catalog")
|
2000-03-31 00:13:30 +02:00
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|