2000-03-31 11:27:42 +08:00
|
|
|
<!--
|
2005-06-05 11:16:42 +08:00
|
|
|
$PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.41 2005/06/05 03:16:29 momjian Exp $
|
2000-03-31 11:27:42 +08:00
|
|
|
-->
|
|
|
|
|
2002-01-26 03:13:15 +08:00
|
|
|
<chapter id="plperl">
|
|
|
|
<title>PL/Perl - Perl Procedural Language</title>
|
|
|
|
|
|
|
|
<indexterm zone="plperl">
|
|
|
|
<primary>PL/Perl</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<indexterm zone="plperl">
|
|
|
|
<primary>Perl</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<para>
|
2002-09-19 04:09:32 +08:00
|
|
|
PL/Perl is a loadable procedural language that enables you to write
|
2005-04-09 11:52:43 +08:00
|
|
|
<productname>PostgreSQL</productname> functions in the
|
|
|
|
<ulink url="http://www.perl.com">Perl programming language</ulink>.
|
2002-01-26 03:13:15 +08:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2002-09-19 04:09:32 +08:00
|
|
|
To install PL/Perl in a particular database, use
|
|
|
|
<literal>createlang plperl <replaceable>dbname</></literal>.
|
2000-03-31 06:13:30 +08:00
|
|
|
</para>
|
|
|
|
|
2002-01-26 03:13:15 +08:00
|
|
|
<tip>
|
|
|
|
<para>
|
|
|
|
If a language is installed into <literal>template1</>, all subsequently
|
|
|
|
created databases will have the language installed automatically.
|
|
|
|
</para>
|
|
|
|
</tip>
|
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
Users of source packages must specially enable the build of
|
2004-07-22 04:44:52 +08:00
|
|
|
PL/Perl during the installation process. (Refer to <xref
|
|
|
|
linkend="install-short"> for more information.) Users of
|
|
|
|
binary packages might find PL/Perl in a separate subpackage.
|
2002-09-19 04:09:32 +08:00
|
|
|
</para>
|
|
|
|
</note>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<sect1 id="plperl-funcs">
|
|
|
|
<title>PL/Perl Functions and Arguments</title>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<para>
|
2005-05-20 09:52:25 +08:00
|
|
|
To create a function in the PL/Perl language, use the standard
|
|
|
|
<xref linkend="sql-createfunction" endterm="sql-createfunction-title">
|
|
|
|
syntax:
|
2002-09-19 04:09:32 +08:00
|
|
|
<programlisting>
|
2004-09-21 06:48:29 +08:00
|
|
|
CREATE FUNCTION <replaceable>funcname</replaceable> (<replaceable>argument-types</replaceable>) RETURNS <replaceable>return-type</replaceable> AS $$
|
2002-01-26 03:13:15 +08:00
|
|
|
# PL/Perl function body
|
2004-05-17 07:22:08 +08:00
|
|
|
$$ LANGUAGE plperl;
|
2002-09-19 04:09:32 +08:00
|
|
|
</programlisting>
|
2004-09-21 06:48:29 +08:00
|
|
|
The body of the function is ordinary Perl code.
|
2002-09-19 04:09:32 +08:00
|
|
|
</para>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2004-09-21 06:48:29 +08:00
|
|
|
<para>
|
|
|
|
The syntax of the <command>CREATE FUNCTION</command> command requires
|
|
|
|
the function body to be written as a string constant. It is usually
|
|
|
|
most convenient to use dollar quoting (see <xref
|
|
|
|
linkend="sql-syntax-dollar-quoting">) for the string constant.
|
|
|
|
If you choose to use regular single-quoted string constant syntax,
|
|
|
|
you must escape single quote marks (<literal>'</>) and backslashes
|
|
|
|
(<literal>\</>) used in the body of the function, typically by
|
|
|
|
doubling them (see <xref linkend="sql-syntax-strings">).
|
|
|
|
</para>
|
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<para>
|
|
|
|
Arguments and results are handled as in any other Perl subroutine:
|
2004-11-20 07:22:54 +08:00
|
|
|
arguments are passed in <varname>@_</varname>, and a result value
|
2002-09-19 04:09:32 +08:00
|
|
|
is returned with <literal>return</> or as the last expression
|
2003-04-07 09:29:26 +08:00
|
|
|
evaluated in the function.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
For example, a function returning the greater of two integer values
|
|
|
|
could be defined as:
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<programlisting>
|
2004-05-17 07:22:08 +08:00
|
|
|
CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
|
2004-11-20 07:22:54 +08:00
|
|
|
if ($_[0] > $_[1]) { return $_[0]; }
|
2002-01-26 03:13:15 +08:00
|
|
|
return $_[1];
|
2004-05-17 07:22:08 +08:00
|
|
|
$$ LANGUAGE plperl;
|
2002-09-19 04:09:32 +08:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2004-11-06 22:32:10 +08:00
|
|
|
If an SQL null value<indexterm><primary>null value</><secondary
|
|
|
|
sortas="PL/Perl">in PL/Perl</></indexterm> is passed to a function,
|
|
|
|
the argument value will appear as <quote>undefined</> in Perl. The
|
|
|
|
above function definition will not behave very nicely with null
|
|
|
|
inputs (in fact, it will act as though they are zeroes). We could
|
|
|
|
add <literal>STRICT</> to the function definition to make
|
|
|
|
<productname>PostgreSQL</productname> do something more reasonable:
|
|
|
|
if a null value is passed, the function will not be called at all,
|
|
|
|
but will just return a null result automatically. Alternatively,
|
|
|
|
we could check for undefined inputs in the function body. For
|
|
|
|
example, suppose that we wanted <function>perl_max</function> with
|
|
|
|
one null and one nonnull argument to return the nonnull argument,
|
|
|
|
rather than a null value:
|
2002-09-19 04:09:32 +08:00
|
|
|
|
|
|
|
<programlisting>
|
2004-05-17 07:22:08 +08:00
|
|
|
CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
|
2002-01-26 03:13:15 +08:00
|
|
|
my ($a,$b) = @_;
|
|
|
|
if (! defined $a) {
|
|
|
|
if (! defined $b) { return undef; }
|
|
|
|
return $b;
|
|
|
|
}
|
|
|
|
if (! defined $b) { return $a; }
|
2004-11-20 07:22:54 +08:00
|
|
|
if ($a > $b) { return $a; }
|
2002-01-26 03:13:15 +08:00
|
|
|
return $b;
|
2004-05-17 07:22:08 +08:00
|
|
|
$$ LANGUAGE plperl;
|
2002-09-19 04:09:32 +08:00
|
|
|
</programlisting>
|
2004-11-06 22:32:10 +08:00
|
|
|
As shown above, to return an SQL null value from a PL/Perl
|
|
|
|
function, return an undefined value. This can be done whether the
|
|
|
|
function is strict or not.
|
2002-09-19 04:09:32 +08:00
|
|
|
</para>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<para>
|
|
|
|
Composite-type arguments are passed to the function as references
|
|
|
|
to hashes. The keys of the hash are the attribute names of the
|
|
|
|
composite type. Here is an example:
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<programlisting>
|
2002-01-07 10:29:15 +08:00
|
|
|
CREATE TABLE employee (
|
2000-03-31 06:13:30 +08:00
|
|
|
name text,
|
2000-12-20 02:16:26 +08:00
|
|
|
basesalary integer,
|
|
|
|
bonus integer
|
|
|
|
);
|
2000-03-31 06:13:30 +08:00
|
|
|
|
2004-05-17 07:22:08 +08:00
|
|
|
CREATE FUNCTION empcomp(employee) RETURNS integer AS $$
|
2002-01-26 03:13:15 +08:00
|
|
|
my ($emp) = @_;
|
2004-11-20 07:22:54 +08:00
|
|
|
return $emp->{basesalary} + $emp->{bonus};
|
2004-05-17 07:22:08 +08:00
|
|
|
$$ LANGUAGE plperl;
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2004-12-31 05:45:37 +08:00
|
|
|
SELECT name, empcomp(employee.*) FROM employee;
|
2002-09-19 04:09:32 +08:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
2004-12-31 05:45:37 +08:00
|
|
|
|
|
|
|
<para>
|
|
|
|
A PL/Perl function can return a composite-type result using the same
|
|
|
|
approach: return a reference to a hash that has the required attributes.
|
|
|
|
For example,
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE TYPE testrowperl AS (f1 integer, f2 text, f3 text);
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION perl_row() RETURNS testrowperl AS $$
|
|
|
|
return {f2 => 'hello', f1 => 1, f3 => 'world'};
|
|
|
|
$$ LANGUAGE plperl;
|
|
|
|
|
|
|
|
SELECT * FROM perl_row();
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
Any columns in the declared result data type that are not present in the
|
|
|
|
hash will be returned as NULLs.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
PL/Perl functions can also return sets of either scalar or composite
|
|
|
|
types. To do this, return a reference to an array that contains
|
|
|
|
either scalars or references to hashes, respectively. Here are
|
|
|
|
some simple examples:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE OR REPLACE FUNCTION perl_set_int(int) RETURNS SETOF INTEGER AS $$
|
|
|
|
return [0..$_[0]];
|
|
|
|
$$ LANGUAGE plperl;
|
|
|
|
|
|
|
|
SELECT * FROM perl_set_int(5);
|
|
|
|
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testrowperl AS $$
|
|
|
|
return [
|
|
|
|
{ f1 => 1, f2 => 'Hello', f3 => 'World' },
|
|
|
|
{ f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL' },
|
|
|
|
{ f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' }
|
|
|
|
];
|
|
|
|
$$ LANGUAGE plperl;
|
|
|
|
|
|
|
|
SELECT * FROM perl_set();
|
|
|
|
</programlisting>
|
|
|
|
|
2005-06-05 11:16:42 +08:00
|
|
|
When you do this, Perl will have to build the entire array in memory;
|
|
|
|
therefore the technique does not scale to very large result sets. You
|
|
|
|
can instead call <function>return_next</function> for each element of
|
|
|
|
the result set, passing it either a scalar or a reference to a hash,
|
|
|
|
as appropriate to your function's return type.
|
2004-12-31 05:45:37 +08:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<application>PL/Perl</> does not currently have full support for
|
|
|
|
domain types: it treats a domain the same as the underlying scalar
|
|
|
|
type. This means that constraints associated with the domain will
|
|
|
|
not be enforced. This is not an issue for function arguments, but
|
|
|
|
it is a hazard if you declare a <application>PL/Perl</> function
|
|
|
|
as returning a domain type.
|
|
|
|
</para>
|
2002-09-19 04:09:32 +08:00
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="plperl-database">
|
|
|
|
<title>Database Access from PL/Perl</title>
|
|
|
|
|
|
|
|
<para>
|
2004-11-06 22:32:10 +08:00
|
|
|
Access to the database itself from your Perl function can be done
|
|
|
|
via the function <function>spi_exec_query</function> described
|
2005-04-09 11:52:43 +08:00
|
|
|
below, or via an experimental module
|
|
|
|
<ulink url="http://www.cpan.org/modules/by-module/DBD/APILOS/">
|
|
|
|
<literal>DBD::PgSPI</literal></ulink>
|
|
|
|
(also available at <ulink url="http://www.cpan.org/SITES.html">
|
|
|
|
<acronym>CPAN mirror sites</></ulink>). This module makes available a
|
2002-09-19 04:09:32 +08:00
|
|
|
<acronym>DBI</>-compliant database-handle named
|
2004-11-06 22:32:10 +08:00
|
|
|
<varname>$pg_dbh</varname> that can be used to perform queries with
|
|
|
|
normal <acronym>DBI</>
|
|
|
|
syntax.<indexterm><primary>DBI</></indexterm>
|
2002-09-19 04:09:32 +08:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2004-07-22 04:44:52 +08:00
|
|
|
PL/Perl itself presently provides two additional Perl commands:
|
2002-09-19 04:09:32 +08:00
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
2004-07-22 04:44:52 +08:00
|
|
|
<indexterm>
|
|
|
|
<primary>spi_exec_query</primary>
|
|
|
|
<secondary>in PL/Perl</secondary>
|
|
|
|
</indexterm>
|
2002-09-19 04:09:32 +08:00
|
|
|
|
2004-11-06 22:32:10 +08:00
|
|
|
<term><literal><function>spi_exec_query</>(<replaceable>query</replaceable> [, <replaceable>max-rows</replaceable>])</literal></term>
|
|
|
|
<term><literal><function>spi_exec_query</>(<replaceable>command</replaceable>)</literal></term>
|
2004-07-22 04:44:52 +08:00
|
|
|
<listitem>
|
2004-11-06 22:32:10 +08:00
|
|
|
<para>
|
|
|
|
Executes an SQL command. Here is an example of a query
|
|
|
|
(<command>SELECT</command> command) with the optional maximum
|
|
|
|
number of rows:
|
2004-07-22 04:44:52 +08:00
|
|
|
<programlisting>
|
2004-11-06 22:32:10 +08:00
|
|
|
$rv = spi_exec_query('SELECT * FROM my_table', 5);
|
2004-07-22 04:44:52 +08:00
|
|
|
</programlisting>
|
2004-11-20 07:22:54 +08:00
|
|
|
This returns up to 5 rows from the table
|
|
|
|
<literal>my_table</literal>. If <literal>my_table</literal>
|
|
|
|
has a column <literal>my_column</literal>, you can get that
|
|
|
|
value from row <literal>$i</literal> of the result like this:
|
2004-07-22 04:44:52 +08:00
|
|
|
<programlisting>
|
2004-11-20 07:22:54 +08:00
|
|
|
$foo = $rv->{rows}[$i]->{my_column};
|
2004-07-22 04:44:52 +08:00
|
|
|
</programlisting>
|
2004-11-20 07:22:54 +08:00
|
|
|
The total number of rows returned from a <command>SELECT</command>
|
|
|
|
query can be accessed like this:
|
2004-07-22 04:44:52 +08:00
|
|
|
<programlisting>
|
2004-11-20 07:22:54 +08:00
|
|
|
$nrows = $rv->{processed}
|
2004-07-22 04:44:52 +08:00
|
|
|
</programlisting>
|
2004-11-06 22:32:10 +08:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Here is an example using a different command type:
|
2004-07-22 04:44:52 +08:00
|
|
|
<programlisting>
|
|
|
|
$query = "INSERT INTO my_table VALUES (1, 'test')";
|
|
|
|
$rv = spi_exec_query($query);
|
|
|
|
</programlisting>
|
2004-11-06 22:32:10 +08:00
|
|
|
You can then access the command status (e.g.,
|
|
|
|
<literal>SPI_OK_INSERT</literal>) like this:
|
2004-07-22 04:44:52 +08:00
|
|
|
<programlisting>
|
2004-11-20 07:22:54 +08:00
|
|
|
$res = $rv->{status};
|
2004-07-22 04:44:52 +08:00
|
|
|
</programlisting>
|
2004-11-06 22:32:10 +08:00
|
|
|
To get the number of rows affected, do:
|
2004-07-22 04:44:52 +08:00
|
|
|
<programlisting>
|
2004-11-20 07:22:54 +08:00
|
|
|
$nrows = $rv->{processed};
|
2004-12-31 05:45:37 +08:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Here is a complete example:
|
|
|
|
<programlisting>
|
|
|
|
CREATE TABLE test (
|
|
|
|
i int,
|
|
|
|
v varchar
|
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO test (i, v) VALUES (1, 'first line');
|
|
|
|
INSERT INTO test (i, v) VALUES (2, 'second line');
|
|
|
|
INSERT INTO test (i, v) VALUES (3, 'third line');
|
|
|
|
INSERT INTO test (i, v) VALUES (4, 'immortal');
|
|
|
|
|
|
|
|
CREATE FUNCTION test_munge() RETURNS SETOF test AS $$
|
|
|
|
my $res = [];
|
|
|
|
my $rv = spi_exec_query('select i, v from test;');
|
|
|
|
my $status = $rv->{status};
|
|
|
|
my $nrows = $rv->{processed};
|
|
|
|
foreach my $rn (0 .. $nrows - 1) {
|
|
|
|
my $row = $rv->{rows}[$rn];
|
|
|
|
$row->{i} += 200 if defined($row->{i});
|
|
|
|
$row->{v} =~ tr/A-Za-z/a-zA-Z/ if (defined($row->{v}));
|
|
|
|
push @$res, $row;
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
$$ LANGUAGE plperl;
|
|
|
|
|
|
|
|
SELECT * FROM test_munge();
|
2004-07-22 04:44:52 +08:00
|
|
|
</programlisting>
|
2004-11-06 22:32:10 +08:00
|
|
|
</para>
|
2004-07-22 04:44:52 +08:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2004-11-06 22:32:10 +08:00
|
|
|
|
2004-07-22 04:44:52 +08:00
|
|
|
<varlistentry>
|
2004-11-06 22:32:10 +08:00
|
|
|
<indexterm>
|
|
|
|
<primary>elog</primary>
|
|
|
|
<secondary>in PL/Perl</secondary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<term><literal><function>elog</>(<replaceable>level</replaceable>, <replaceable>msg</replaceable>)</literal></term>
|
2002-09-19 04:09:32 +08:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Emit a log or error message. Possible levels are
|
|
|
|
<literal>DEBUG</>, <literal>LOG</>, <literal>INFO</>,
|
|
|
|
<literal>NOTICE</>, <literal>WARNING</>, and <literal>ERROR</>.
|
2004-11-22 05:17:07 +08:00
|
|
|
<literal>ERROR</>
|
|
|
|
raises an error condition; if this is not trapped by the surrounding
|
|
|
|
Perl code, the error propagates out to the calling query, causing
|
|
|
|
the current transaction or subtransaction to be aborted. This
|
|
|
|
is effectively the same as the Perl <literal>die</> command.
|
2004-12-31 05:45:37 +08:00
|
|
|
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
|
|
|
|
<xref linkend="guc-log-min-messages"> and
|
|
|
|
<xref linkend="guc-client-min-messages"> configuration
|
|
|
|
variables. See <xref linkend="runtime-config"> for more
|
|
|
|
information.
|
2002-09-19 04:09:32 +08:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
2004-07-22 04:44:52 +08:00
|
|
|
<sect1 id="plperl-data">
|
|
|
|
<title>Data Values in PL/Perl</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The argument values supplied to a PL/Perl function's code are
|
|
|
|
simply the input arguments converted to text form (just as if they
|
|
|
|
had been displayed by a <command>SELECT</command> statement).
|
|
|
|
Conversely, the <literal>return</> command will accept any string
|
|
|
|
that is acceptable input format for the function's declared return
|
2004-12-31 05:45:37 +08:00
|
|
|
type. So, within the PL/Perl function,
|
|
|
|
all values are just text strings.
|
2004-07-22 04:44:52 +08:00
|
|
|
</para>
|
|
|
|
</sect1>
|
2004-11-06 22:32:10 +08:00
|
|
|
|
2004-07-22 04:44:52 +08:00
|
|
|
<sect1 id="plperl-global">
|
|
|
|
<title>Global Values in PL/Perl</title>
|
2004-11-06 22:32:10 +08:00
|
|
|
|
2004-07-22 04:44:52 +08:00
|
|
|
<para>
|
2004-12-12 04:03:37 +08:00
|
|
|
You can use the global hash <varname>%_SHARED</varname> to store
|
|
|
|
data, including code references, between function calls for the
|
2004-12-31 05:45:37 +08:00
|
|
|
lifetime of the current session.
|
2004-12-12 04:03:37 +08:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Here is a simple example for shared data:
|
2004-07-22 04:44:52 +08:00
|
|
|
<programlisting>
|
2004-11-06 22:32:10 +08:00
|
|
|
CREATE OR REPLACE FUNCTION set_var(name text, val text) RETURNS text AS $$
|
2004-08-18 11:37:56 +08:00
|
|
|
if ($_SHARED{$_[0]} = $_[1]) {
|
|
|
|
return 'ok';
|
|
|
|
} else {
|
2004-11-06 22:32:10 +08:00
|
|
|
return "can't set shared variable $_[0] to $_[1]";
|
2004-08-18 11:37:56 +08:00
|
|
|
}
|
2004-07-22 04:44:52 +08:00
|
|
|
$$ LANGUAGE plperl;
|
|
|
|
|
2004-11-06 22:32:10 +08:00
|
|
|
CREATE OR REPLACE FUNCTION get_var(name text) RETURNS text AS $$
|
2004-08-18 11:37:56 +08:00
|
|
|
return $_SHARED{$_[0]};
|
2004-07-22 04:44:52 +08:00
|
|
|
$$ LANGUAGE plperl;
|
|
|
|
|
2004-11-06 22:32:10 +08:00
|
|
|
SELECT set_var('sample', 'Hello, PL/Perl! How's tricks?');
|
2004-08-18 11:37:56 +08:00
|
|
|
SELECT get_var('sample');
|
2004-07-22 04:44:52 +08:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
2004-12-12 04:03:37 +08:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Here is a slightly more complicated example using a code reference:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE OR REPLACE FUNCTION myfuncs() RETURNS void AS $$
|
|
|
|
$_SHARED{myquote} = sub {
|
|
|
|
my $arg = shift;
|
|
|
|
$arg =~ s/(['\\])/\\$1/g;
|
|
|
|
return "'$arg'";
|
|
|
|
};
|
|
|
|
$$ LANGUAGE plperl;
|
|
|
|
|
|
|
|
SELECT myfuncs(); /* initializes the function */
|
|
|
|
|
|
|
|
/* Set up a function that uses the quote function */
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION use_quote(TEXT) RETURNS text AS $$
|
|
|
|
my $text_to_quote = shift;
|
|
|
|
my $qfunc = $_SHARED{myquote};
|
2004-12-31 05:45:37 +08:00
|
|
|
return &$qfunc($text_to_quote);
|
2004-12-12 04:03:37 +08:00
|
|
|
$$ LANGUAGE plperl;
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
(You could have replaced the above with the one-liner
|
2004-12-31 05:45:37 +08:00
|
|
|
<literal>return $_SHARED{myquote}->($_[0]);</literal>
|
2004-12-12 04:03:37 +08:00
|
|
|
at the expense of readability.)
|
|
|
|
</para>
|
2004-07-22 04:44:52 +08:00
|
|
|
</sect1>
|
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<sect1 id="plperl-trusted">
|
|
|
|
<title>Trusted and Untrusted PL/Perl</title>
|
|
|
|
|
2003-09-01 01:32:24 +08:00
|
|
|
<indexterm zone="plperl-trusted">
|
|
|
|
<primary>trusted</primary>
|
|
|
|
<secondary>PL/Perl</secondary>
|
|
|
|
</indexterm>
|
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<para>
|
|
|
|
Normally, PL/Perl is installed as a <quote>trusted</> programming
|
|
|
|
language named <literal>plperl</>. In this setup, certain Perl
|
|
|
|
operations are disabled to preserve security. In general, the
|
|
|
|
operations that are restricted are those that interact with the
|
|
|
|
environment. This includes file handle operations,
|
|
|
|
<literal>require</literal>, and <literal>use</literal> (for
|
|
|
|
external modules). There is no way to access internals of the
|
2003-04-07 09:29:26 +08:00
|
|
|
database server process or to gain OS-level access with the
|
|
|
|
permissions of the server process,
|
2002-09-19 04:09:32 +08:00
|
|
|
as a C function can do. Thus, any unprivileged database user may
|
|
|
|
be permitted to use this language.
|
|
|
|
</para>
|
2000-12-20 02:16:26 +08:00
|
|
|
|
|
|
|
<para>
|
2000-12-23 02:57:50 +08:00
|
|
|
Here is an example of a function that will not work because file
|
2000-12-20 02:16:26 +08:00
|
|
|
system operations are not allowed for security reasons:
|
|
|
|
<programlisting>
|
2004-05-17 07:22:08 +08:00
|
|
|
CREATE FUNCTION badfunc() RETURNS integer AS $$
|
2004-11-20 07:22:54 +08:00
|
|
|
open(TEMP, ">/tmp/badfile");
|
2000-12-20 02:16:26 +08:00
|
|
|
print TEMP "Gotcha!\n";
|
|
|
|
return 1;
|
2004-05-17 07:22:08 +08:00
|
|
|
$$ LANGUAGE plperl;
|
2000-12-20 02:16:26 +08:00
|
|
|
</programlisting>
|
|
|
|
The creation of the function will succeed, but executing it will not.
|
2002-01-07 10:29:15 +08:00
|
|
|
</para>
|
2002-09-19 04:09:32 +08:00
|
|
|
|
2002-01-07 10:29:15 +08:00
|
|
|
<para>
|
2002-09-19 04:09:32 +08:00
|
|
|
Sometimes it is desirable to write Perl functions that are not
|
2003-09-01 01:32:24 +08:00
|
|
|
restricted. For example, one might want a Perl function that sends
|
|
|
|
mail. To handle these cases, PL/Perl can also be installed as an
|
|
|
|
<quote>untrusted</> language (usually called
|
|
|
|
<application>PL/PerlU</application><indexterm><primary>PL/PerlU</></indexterm>).
|
|
|
|
In this case the full Perl language is available. If the
|
|
|
|
<command>createlang</command> program is used to install the
|
|
|
|
language, the language name <literal>plperlu</literal> will select
|
|
|
|
the untrusted PL/Perl variant.
|
2001-06-23 05:37:14 +08:00
|
|
|
</para>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<para>
|
2002-09-22 02:32:54 +08:00
|
|
|
The writer of a <application>PL/PerlU</> function must take care that the function
|
2002-09-19 04:09:32 +08:00
|
|
|
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. Note that the database system allows only database
|
|
|
|
superusers to create functions in untrusted languages.
|
|
|
|
</para>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<para>
|
|
|
|
If the above function was created by a superuser using the language
|
|
|
|
<literal>plperlu</>, execution would succeed.
|
|
|
|
</para>
|
|
|
|
</sect1>
|
2004-11-06 22:32:10 +08:00
|
|
|
|
2004-07-22 04:44:52 +08:00
|
|
|
<sect1 id="plperl-triggers">
|
|
|
|
<title>PL/Perl Triggers</title>
|
|
|
|
|
|
|
|
<para>
|
2004-11-20 07:22:54 +08:00
|
|
|
PL/Perl can be used to write trigger functions. In a trigger function,
|
|
|
|
the hash reference <varname>$_TD</varname> contains information about the
|
|
|
|
current trigger event. The fields of the <varname>$_TD</varname> hash
|
2004-11-06 22:32:10 +08:00
|
|
|
reference are:
|
2004-07-22 04:44:52 +08:00
|
|
|
|
2004-11-06 22:32:10 +08:00
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
2004-11-20 07:22:54 +08:00
|
|
|
<term><literal>$_TD->{new}{foo}</literal></term>
|
2004-11-06 22:32:10 +08:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>NEW</literal> value of column <literal>foo</literal>
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2004-07-22 04:44:52 +08:00
|
|
|
|
2004-11-06 22:32:10 +08:00
|
|
|
<varlistentry>
|
2004-11-20 07:22:54 +08:00
|
|
|
<term><literal>$_TD->{old}{foo}</literal></term>
|
2004-11-06 22:32:10 +08:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>OLD</literal> value of column <literal>foo</literal>
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2005-01-17 11:04:17 +08:00
|
|
|
<term><literal>$_TD->{name}</literal></term>
|
2004-11-06 22:32:10 +08:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Name of the trigger being called
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2005-01-17 11:04:17 +08:00
|
|
|
<term><literal>$_TD->{event}</literal></term>
|
2004-11-06 22:32:10 +08:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Trigger event: <literal>INSERT</>, <literal>UPDATE</>, <literal>DELETE</>, or <literal>UNKNOWN</>
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2005-01-17 11:04:17 +08:00
|
|
|
<term><literal>$_TD->{when}</literal></term>
|
2004-11-06 22:32:10 +08:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
When the trigger was called: <literal>BEFORE</literal>, <literal>AFTER</literal>, or <literal>UNKNOWN</literal>
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2005-01-17 11:04:17 +08:00
|
|
|
<term><literal>$_TD->{level}</literal></term>
|
2004-11-06 22:32:10 +08:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The trigger level: <literal>ROW</literal>, <literal>STATEMENT</literal>, or <literal>UNKNOWN</literal>
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2005-01-17 11:04:17 +08:00
|
|
|
<term><literal>$_TD->{relid}</literal></term>
|
2004-11-06 22:32:10 +08:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
OID of the table on which the trigger fired
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2004-07-22 04:44:52 +08:00
|
|
|
|
2004-11-06 22:32:10 +08:00
|
|
|
<varlistentry>
|
2005-01-17 11:04:17 +08:00
|
|
|
<term><literal>$_TD->{relname}</literal></term>
|
2004-11-06 22:32:10 +08:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Name of the table on which the trigger fired
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2005-01-18 01:29:49 +08:00
|
|
|
<term><literal>$_TD->{argc}</literal></term>
|
2004-11-06 22:32:10 +08:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2005-01-18 01:29:49 +08:00
|
|
|
Number of arguments of the trigger function
|
2004-11-06 22:32:10 +08:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2005-01-18 01:29:49 +08:00
|
|
|
<term><literal>@{$_TD->{args}}</literal></term>
|
2004-11-06 22:32:10 +08:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2005-01-18 01:29:49 +08:00
|
|
|
Arguments of the trigger function. Does not exist if $_TD->{argc} is 0.
|
2004-11-06 22:32:10 +08:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2005-01-18 01:29:49 +08:00
|
|
|
|
2004-11-06 22:32:10 +08:00
|
|
|
</variablelist>
|
2004-07-22 04:44:52 +08:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Triggers can return one of the following:
|
2004-11-06 22:32:10 +08:00
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>return;</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Execute the statement
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>"SKIP"</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Don't execute the statement
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>"MODIFY"</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-11-20 07:22:54 +08:00
|
|
|
Indicates that the <literal>NEW</literal> row was modified by
|
2004-11-06 22:32:10 +08:00
|
|
|
the trigger function
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
2004-07-22 04:44:52 +08:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2004-11-06 22:32:10 +08:00
|
|
|
Here is an example of a trigger function, illustrating some of the
|
|
|
|
above:
|
2004-07-22 04:44:52 +08:00
|
|
|
<programlisting>
|
|
|
|
CREATE TABLE test (
|
2004-11-06 22:32:10 +08:00
|
|
|
i int,
|
|
|
|
v varchar
|
2004-07-22 04:44:52 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$
|
2004-11-20 07:22:54 +08:00
|
|
|
if (($_TD->{new}{i} >= 100) || ($_TD->{new}{i} <= 0)) {
|
2004-11-06 22:32:10 +08:00
|
|
|
return "SKIP"; # skip INSERT/UPDATE command
|
2004-11-20 07:22:54 +08:00
|
|
|
} elsif ($_TD->{new}{v} ne "immortal") {
|
|
|
|
$_TD->{new}{v} .= "(modified by trigger)";
|
2004-11-06 22:32:10 +08:00
|
|
|
return "MODIFY"; # modify row and execute INSERT/UPDATE command
|
2004-07-22 04:44:52 +08:00
|
|
|
} else {
|
2004-11-06 22:32:10 +08:00
|
|
|
return; # execute INSERT/UPDATE command
|
2004-07-22 04:44:52 +08:00
|
|
|
}
|
|
|
|
$$ LANGUAGE plperl;
|
|
|
|
|
2004-11-06 22:32:10 +08:00
|
|
|
CREATE TRIGGER test_valid_id_trig
|
|
|
|
BEFORE INSERT OR UPDATE ON test
|
|
|
|
FOR EACH ROW EXECUTE PROCEDURE valid_id();
|
2004-07-22 04:44:52 +08:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect1>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<sect1 id="plperl-missing">
|
2004-07-22 04:44:52 +08:00
|
|
|
<title>Limitations and Missing Features</title>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2001-06-23 05:37:14 +08:00
|
|
|
<para>
|
2002-09-19 04:09:32 +08:00
|
|
|
The following features are currently missing from PL/Perl, but they
|
2003-04-07 09:29:26 +08:00
|
|
|
would make welcome contributions.
|
2002-09-19 04:09:32 +08:00
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
PL/Perl functions cannot call each other directly (because they
|
2004-07-22 04:44:52 +08:00
|
|
|
are anonymous subroutines inside Perl).
|
2002-09-19 04:09:32 +08:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-11-06 22:32:10 +08:00
|
|
|
SPI is not yet fully implemented.
|
2002-09-19 04:09:32 +08:00
|
|
|
</para>
|
|
|
|
</listitem>
|
2004-11-06 22:32:10 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<listitem>
|
2004-11-06 22:32:10 +08:00
|
|
|
<para>
|
|
|
|
In the current implementation, if you are fetching or returning
|
|
|
|
very large data sets, you should be aware that these will all go
|
2004-12-31 05:45:37 +08:00
|
|
|
into memory.
|
2004-11-06 22:32:10 +08:00
|
|
|
</para>
|
2002-09-19 04:09:32 +08:00
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
2000-12-20 02:16:26 +08:00
|
|
|
</para>
|
2002-09-19 04:09:32 +08:00
|
|
|
</sect1>
|
2000-12-20 02:16:26 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
</chapter>
|
2000-03-31 06:13:30 +08:00
|
|
|
|
|
|
|
<!-- 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 11:27:42 +08:00
|
|
|
sgml-local-catalogs:("/usr/lib/sgml/catalog")
|
2000-03-31 06:13:30 +08:00
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|