2000-03-31 11:27:42 +08:00
|
|
|
<!--
|
2003-04-07 09:29:26 +08:00
|
|
|
$Header: /cvsroot/pgsql/doc/src/sgml/plperl.sgml,v 2.19 2003/04/07 01:29:25 petere 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
|
|
|
|
<productname>PostgreSQL</productname> functions in the <ulink
|
|
|
|
url="http://www.perl.com">Perl</ulink> programming language.
|
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
|
2003-04-07 09:29:26 +08:00
|
|
|
PL/Perl during the installation process. (Refer to the installation
|
|
|
|
instructions for more information.) Users of binary packages
|
2002-09-19 04:09:32 +08:00
|
|
|
might find PL/Perl in a separate subpackage.
|
|
|
|
</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>
|
|
|
|
To create a function in the PL/Perl language, use the standard syntax:
|
|
|
|
<programlisting>
|
2002-01-26 03:13:15 +08:00
|
|
|
CREATE FUNCTION <replaceable>funcname</replaceable> (<replaceable>argument-types</replaceable>) RETURNS <replaceable>return-type</replaceable> AS '
|
|
|
|
# PL/Perl function body
|
|
|
|
' LANGUAGE plperl;
|
2002-09-19 04:09:32 +08:00
|
|
|
</programlisting>
|
|
|
|
The body of the function is ordinary Perl code.
|
|
|
|
</para>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<para>
|
|
|
|
Arguments and results are handled as in any other Perl subroutine:
|
|
|
|
Arguments are passed in <varname>@_</varname>, and a result value
|
|
|
|
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>
|
2002-01-26 03:13:15 +08:00
|
|
|
CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS '
|
|
|
|
if ($_[0] > $_[1]) { return $_[0]; }
|
|
|
|
return $_[1];
|
|
|
|
' LANGUAGE plperl;
|
2002-09-19 04:09:32 +08:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
If an SQL null value 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 non-null argument to return the non-null argument,
|
|
|
|
rather than a null value:
|
|
|
|
|
|
|
|
<programlisting>
|
2002-01-26 03:13:15 +08:00
|
|
|
CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS '
|
|
|
|
my ($a,$b) = @_;
|
|
|
|
if (! defined $a) {
|
|
|
|
if (! defined $b) { return undef; }
|
|
|
|
return $b;
|
|
|
|
}
|
|
|
|
if (! defined $b) { return $a; }
|
|
|
|
if ($a > $b) { return $a; }
|
|
|
|
return $b;
|
|
|
|
' LANGUAGE plperl;
|
2002-09-19 04:09:32 +08:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<para>
|
|
|
|
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.
|
|
|
|
</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
|
|
|
|
2000-12-20 02:16:26 +08:00
|
|
|
CREATE FUNCTION empcomp(employee) RETURNS integer AS '
|
2002-01-26 03:13:15 +08:00
|
|
|
my ($emp) = @_;
|
2000-12-20 02:16:26 +08:00
|
|
|
return $emp->{''basesalary''} + $emp->{''bonus''};
|
2002-01-07 10:29:15 +08:00
|
|
|
' LANGUAGE plperl;
|
2002-01-26 03:13:15 +08:00
|
|
|
|
|
|
|
SELECT name, empcomp(employee) FROM employee;
|
2002-09-19 04:09:32 +08:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<para>
|
|
|
|
There is currently no support for returning a composite-type result
|
|
|
|
value.
|
|
|
|
</para>
|
2000-12-20 02:16:26 +08:00
|
|
|
|
|
|
|
<tip>
|
2000-03-31 06:13:30 +08:00
|
|
|
<para>
|
2000-12-20 02:16:26 +08:00
|
|
|
Because the function body is passed as an SQL string literal to
|
2002-01-26 03:13:15 +08:00
|
|
|
<command>CREATE FUNCTION</command>, you have to escape single
|
2002-09-19 04:09:32 +08:00
|
|
|
quotes and backslashes within your Perl source, typically by
|
|
|
|
doubling them as shown in the above example. Another possible
|
|
|
|
approach is to avoid writing single quotes by using Perl's
|
|
|
|
extended quoting operators (<literal>q[]</literal>,
|
|
|
|
<literal>qq[]</literal>, <literal>qw[]</literal>).
|
2000-03-31 06:13:30 +08:00
|
|
|
</para>
|
2000-12-20 02:16:26 +08:00
|
|
|
</tip>
|
2002-09-19 04:09:32 +08:00
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="plperl-data">
|
|
|
|
<title>Data Values in PL/Perl</title>
|
|
|
|
|
|
|
|
<para>
|
2003-04-07 09:29:26 +08:00
|
|
|
The argument values supplied to a PL/Perl function's code are
|
2002-09-19 04:09:32 +08:00
|
|
|
simply the input arguments converted to text form (just as if they
|
|
|
|
had been displayed by a <literal>SELECT</literal> statement).
|
|
|
|
Conversely, the <literal>return</> command will accept any string
|
|
|
|
that is acceptable input format for the function's declared return
|
|
|
|
type. So, the PL/Perl programmer can manipulate data values as if
|
|
|
|
they were just text.
|
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="plperl-database">
|
|
|
|
<title>Database Access from PL/Perl</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Access to the database itself from your Perl function can be done via
|
|
|
|
an experimental module <ulink
|
|
|
|
url="http://www.cpan.org/modules/by-module/DBD/APILOS/"><literal>DBD::PgSPI</literal></ulink>
|
2002-09-22 02:32:54 +08:00
|
|
|
(also available at <ulink url="http://www.cpan.org/SITES.html"><acronym>CPAN</>
|
2002-09-19 04:09:32 +08:00
|
|
|
mirror sites</ulink>). This module makes available a
|
|
|
|
<acronym>DBI</>-compliant database-handle named
|
|
|
|
<varname>$pg_dbh</varname> that can be used to perform queries
|
|
|
|
with normal <acronym>DBI</> syntax.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
PL/Perl itself presently provides only one additional Perl command:
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<indexterm>
|
|
|
|
<primary>elog</primary>
|
|
|
|
<secondary>PL/Perl</secondary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<term><function>elog</> <replaceable>level</replaceable>, <replaceable>msg</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Emit a log or error message. Possible levels are
|
|
|
|
<literal>DEBUG</>, <literal>LOG</>, <literal>INFO</>,
|
|
|
|
<literal>NOTICE</>, <literal>WARNING</>, and <literal>ERROR</>.
|
|
|
|
<literal>ERROR</> raises an error condition: further execution
|
|
|
|
of the function is abandoned, and the current transaction is
|
|
|
|
aborted.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="plperl-trusted">
|
|
|
|
<title>Trusted and Untrusted PL/Perl</title>
|
|
|
|
|
|
|
|
<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>
|
|
|
|
CREATE FUNCTION badfunc() RETURNS integer AS '
|
|
|
|
open(TEMP, ">/tmp/badfile");
|
|
|
|
print TEMP "Gotcha!\n";
|
|
|
|
return 1;
|
2002-01-07 10:29:15 +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-04-07 09:29:26 +08:00
|
|
|
restricted. For example, one might want a Perl function that
|
2002-09-19 04:09:32 +08:00
|
|
|
sends mail. To handle these cases, PL/Perl can also be installed
|
|
|
|
as an <quote>untrusted</> language (usually called
|
2002-09-22 02:32:54 +08:00
|
|
|
<application>PL/PerlU</application>). In this case the full Perl language is
|
2002-09-19 04:09:32 +08:00
|
|
|
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>
|
2002-01-26 03:13:15 +08:00
|
|
|
|
2002-09-19 04:09:32 +08:00
|
|
|
<sect1 id="plperl-missing">
|
|
|
|
<title>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
|
|
|
|
are anonymous subroutines inside Perl). There's presently no
|
|
|
|
way for them to share global variables, either.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
PL/Perl cannot be used to write trigger functions.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<application>DBD::PgSPI</applicatioN> or similar capability
|
|
|
|
should be integrated into the standard
|
|
|
|
<productname>PostgreSQL</productname> distribution.
|
|
|
|
</para>
|
|
|
|
</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:
|
|
|
|
-->
|