1 <!-- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.59 2006/11/13 17:13:56 adunstan Exp $ -->
4 <title>PL/Perl - Perl Procedural Language</title>
6 <indexterm zone="plperl">
7 <primary>PL/Perl</primary>
10 <indexterm zone="plperl">
11 <primary>Perl</primary>
15 PL/Perl is a loadable procedural language that enables you to write
16 <productname>PostgreSQL</productname> functions in the
17 <ulink url="http://www.perl.com">Perl programming language</ulink>.
20 <para> The usual advantage to using PL/Perl is that this allows use,
21 within stored functions, of the manyfold <quote>string
22 munging</quote> operators and functions available for Perl. Parsing
23 complex strings may be be easier using Perl than it is with the
24 string functions and control structures provided in PL/pgSQL.</para>
27 To install PL/Perl in a particular database, use
28 <literal>createlang plperl <replaceable>dbname</></literal>.
33 If a language is installed into <literal>template1</>, all subsequently
34 created databases will have the language installed automatically.
40 Users of source packages must specially enable the build of
41 PL/Perl during the installation process. (Refer to <xref
42 linkend="install-short"> for more information.) Users of
43 binary packages might find PL/Perl in a separate subpackage.
47 <sect1 id="plperl-funcs">
48 <title>PL/Perl Functions and Arguments</title>
51 To create a function in the PL/Perl language, use the standard
52 <xref linkend="sql-createfunction" endterm="sql-createfunction-title">
56 CREATE FUNCTION <replaceable>funcname</replaceable> (<replaceable>argument-types</replaceable>) RETURNS <replaceable>return-type</replaceable> AS $$
57 # PL/Perl function body
60 The body of the function is ordinary Perl code. In fact, the PL/Perl
61 glue code wraps it inside a Perl subroutine. A PL/Perl function must
62 always return a scalar value. You can return more complex structures
63 (arrays, records, and sets) by returning a reference, as discussed below.
69 The use of named nested subroutines is dangerous in Perl, especially if
70 they refer to lexical variables in the enclosing scope. Because a PL/Perl
71 function is wrapped in a subroutine, any named subroutine you create will
72 be nested. In general, it is far safer to create anonymous subroutines
73 which you call via a coderef. See the <literal>perldiag</literal>
74 man page for more details.
79 The syntax of the <command>CREATE FUNCTION</command> command requires
80 the function body to be written as a string constant. It is usually
81 most convenient to use dollar quoting (see <xref
82 linkend="sql-syntax-dollar-quoting">) for the string constant.
83 If you choose to use regular single-quoted string constant syntax,
84 you must escape single quote marks (<literal>'</>) and backslashes
85 (<literal>\</>) used in the body of the function, typically by
86 doubling them (see <xref linkend="sql-syntax-strings">).
90 Arguments and results are handled as in any other Perl subroutine:
91 arguments are passed in <varname>@_</varname>, and a result value
92 is returned with <literal>return</> or as the last expression
93 evaluated in the function.
97 For example, a function returning the greater of two integer values
101 CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
102 if ($_[0] > $_[1]) { return $_[0]; }
109 If an SQL null value<indexterm><primary>null value</><secondary
110 sortas="PL/Perl">in PL/Perl</></indexterm> is passed to a function,
111 the argument value will appear as <quote>undefined</> in Perl. The
112 above function definition will not behave very nicely with null
113 inputs (in fact, it will act as though they are zeroes). We could
114 add <literal>STRICT</> to the function definition to make
115 <productname>PostgreSQL</productname> do something more reasonable:
116 if a null value is passed, the function will not be called at all,
117 but will just return a null result automatically. Alternatively,
118 we could check for undefined inputs in the function body. For
119 example, suppose that we wanted <function>perl_max</function> with
120 one null and one nonnull argument to return the nonnull argument,
121 rather than a null value:
124 CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
127 if (! defined $y) { return undef; }
130 if (! defined $y) { return $x; }
131 if ($x > $y) { return $x; }
135 As shown above, to return an SQL null value from a PL/Perl
136 function, return an undefined value. This can be done whether the
137 function is strict or not.
141 Perl can return <productname>PostgreSQL</productname> arrays as
142 references to Perl arrays. Here is an example:
145 CREATE OR REPLACE function returns_array()
146 RETURNS text[][] AS $$
147 return [['a"b','c,d'],['e\\f','g']];
150 select returns_array();
155 Composite-type arguments are passed to the function as references
156 to hashes. The keys of the hash are the attribute names of the
157 composite type. Here is an example:
160 CREATE TABLE employee (
166 CREATE FUNCTION empcomp(employee) RETURNS integer AS $$
168 return $emp->{basesalary} + $emp->{bonus};
171 SELECT name, empcomp(employee.*) FROM employee;
176 A PL/Perl function can return a composite-type result using the same
177 approach: return a reference to a hash that has the required attributes.
181 CREATE TYPE testrowperl AS (f1 integer, f2 text, f3 text);
183 CREATE OR REPLACE FUNCTION perl_row() RETURNS testrowperl AS $$
184 return {f2 => 'hello', f1 => 1, f3 => 'world'};
187 SELECT * FROM perl_row();
190 Any columns in the declared result data type that are not present in the
191 hash will be returned as null values.
195 PL/Perl functions can also return sets of either scalar or
196 composite types. Usually you'll want to return rows one at a
197 time, both to speed up startup time and to keep from queueing up
198 the entire result set in memory. You can do this with
199 <function>return_next</function> as illustrated below. Note that
200 after the last <function>return_next</function>, you must put
201 either <literal>return</literal> or (better) <literal>return
205 CREATE OR REPLACE FUNCTION perl_set_int(int)
206 RETURNS SETOF INTEGER AS $$
213 SELECT * FROM perl_set_int(5);
215 CREATE OR REPLACE FUNCTION perl_set()
216 RETURNS SETOF testrowperl AS $$
217 return_next({ f1 => 1, f2 => 'Hello', f3 => 'World' });
218 return_next({ f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL' });
219 return_next({ f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' });
224 For small result sets, you can return a reference to an array that
225 contains either scalars, references to arrays, or references to
226 hashes for simple types, array types, and composite types,
227 respectively. Here are some simple examples of returning the entire
228 result set as an array reference:
231 CREATE OR REPLACE FUNCTION perl_set_int(int) RETURNS SETOF INTEGER AS $$
235 SELECT * FROM perl_set_int(5);
237 CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testrowperl AS $$
239 { f1 => 1, f2 => 'Hello', f3 => 'World' },
240 { f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL' },
241 { f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' }
245 SELECT * FROM perl_set();
250 If you wish to use the <literal>strict</> pragma with your code,
251 the easiest way to do so is to <command>SET</>
252 <literal>plperl.use_strict</literal> to true. This parameter affects
253 subsequent compilations of <application>PL/Perl</> functions, but not
254 functions already compiled in the current session. To set the
255 parameter before <application>PL/Perl</> has been loaded, it is
256 necessary to have added <quote><literal>plperl</></> to the <xref
257 linkend="guc-custom-variable-classes"> list in
258 <filename>postgresql.conf</filename>.
262 Another way to use the <literal>strict</> pragma is to put
266 in the function body. But this only works in <application>PL/PerlU</>
267 functions, since <literal>use</> is not a trusted operation. In
268 <application>PL/Perl</> functions you can instead do
270 BEGIN { strict->import(); }
275 <sect1 id="plperl-database">
276 <title>Database Access from PL/Perl</title>
279 Access to the database itself from your Perl function can be done
280 via the function <function>spi_exec_query</function> described
281 below, or via an experimental module
282 <ulink url="http://www.cpan.org/modules/by-module/DBD/APILOS/">
283 <literal>DBD::PgSPI</literal></ulink>
284 (also available at <ulink url="http://www.cpan.org/SITES.html">
285 <acronym>CPAN mirror sites</></ulink>). This module makes available a
286 <acronym>DBI</>-compliant database-handle named
287 <varname>$pg_dbh</varname> that can be used to perform queries with
288 normal <acronym>DBI</>
289 syntax.<indexterm><primary>DBI</></indexterm>
293 PL/Perl provides additional Perl commands:
298 <primary>spi_exec_query</primary>
299 <secondary>in PL/Perl</secondary>
302 <term><literal><function>spi_exec_query</>(<replaceable>query</replaceable> [, <replaceable>max-rows</replaceable>])</literal></term>
303 <term><literal><function>spi_query</>(<replaceable>command</replaceable>)</literal></term>
304 <term><literal><function>spi_fetchrow</>(<replaceable>cursor</replaceable>)</literal></term>
305 <term><literal><function>spi_prepare</>(<replaceable>command</replaceable>, <replaceable>argument types</replaceable>)</literal></term>
306 <term><literal><function>spi_exec_prepared</>(<replaceable>plan</replaceable>)</literal></term>
307 <term><literal><function>spi_query_prepared</>(<replaceable>plan</replaceable> [, <replaceable>attributes</replaceable>], <replaceable>arguments</replaceable>)</literal></term>
308 <term><literal><function>spi_cursor_close</>(<replaceable>cursor</replaceable>)</literal></term>
309 <term><literal><function>spi_freeplan</>(<replaceable>plan</replaceable>)</literal></term>
313 <literal>spi_exec_query</literal> executes an SQL command and
314 returns the entire row set as a reference to an array of hash
315 references. <emphasis>You should only use this command when you know
316 that the result set will be relatively small.</emphasis> Here is an
317 example of a query (<command>SELECT</command> command) with the
318 optional maximum number of rows:
321 $rv = spi_exec_query('SELECT * FROM my_table', 5);
323 This returns up to 5 rows from the table
324 <literal>my_table</literal>. If <literal>my_table</literal>
325 has a column <literal>my_column</literal>, you can get that
326 value from row <literal>$i</literal> of the result like this:
328 $foo = $rv->{rows}[$i]->{my_column};
330 The total number of rows returned from a <command>SELECT</command>
331 query can be accessed like this:
333 $nrows = $rv->{processed}
338 Here is an example using a different command type:
340 $query = "INSERT INTO my_table VALUES (1, 'test')";
341 $rv = spi_exec_query($query);
343 You can then access the command status (e.g.,
344 <literal>SPI_OK_INSERT</literal>) like this:
346 $res = $rv->{status};
348 To get the number of rows affected, do:
350 $nrows = $rv->{processed};
355 Here is a complete example:
362 INSERT INTO test (i, v) VALUES (1, 'first line');
363 INSERT INTO test (i, v) VALUES (2, 'second line');
364 INSERT INTO test (i, v) VALUES (3, 'third line');
365 INSERT INTO test (i, v) VALUES (4, 'immortal');
367 CREATE OR REPLACE FUNCTION test_munge() RETURNS SETOF test AS $$
368 my $rv = spi_exec_query('select i, v from test;');
369 my $status = $rv->{status};
370 my $nrows = $rv->{processed};
371 foreach my $rn (0 .. $nrows - 1) {
372 my $row = $rv->{rows}[$rn];
373 $row->{i} += 200 if defined($row->{i});
374 $row->{v} =~ tr/A-Za-z/a-zA-Z/ if (defined($row->{v}));
380 SELECT * FROM test_munge();
384 <literal>spi_query</literal> and <literal>spi_fetchrow</literal>
385 work together as a pair for row sets which may be large, or for cases
386 where you wish to return rows as they arrive.
387 <literal>spi_fetchrow</literal> works <emphasis>only</emphasis> with
388 <literal>spi_query</literal>. The following example illustrates how
389 you use them together:
392 CREATE TYPE foo_type AS (the_num INTEGER, the_text TEXT);
394 CREATE OR REPLACE FUNCTION lotsa_md5 (INTEGER) RETURNS SETOF foo_type AS $$
395 use Digest::MD5 qw(md5_hex);
396 my $file = '/usr/share/dict/words';
398 elog(NOTICE, "opening file $file at $t" );
399 open my $fh, '<', $file # ooh, it's a file access!
400 or elog(ERROR, "can't open $file for reading: $!");
401 my @words = <$fh>;
404 elog(NOTICE, "closed file $file at $t");
407 my $sth = spi_query("SELECT * FROM generate_series(1,$_[0]) AS b(a)");
408 while (defined ($row = spi_fetchrow($sth))) {
410 the_num => $row->{a},
411 the_text => md5_hex($words[rand @words])
417 SELECT * from lotsa_md5(500);
422 <literal>spi_prepare</literal>, <literal>spi_query_prepared</literal>, <literal>spi_exec_prepared</literal>,
423 and <literal>spi_freeplan</literal> implement the same functionality but for prepared queries. Once
424 a query plan is prepared by a call to <literal>spi_prepare</literal>, the plan can be used instead
425 of the string query, either in <literal>spi_exec_prepared</literal>, where the result is the same as returned
426 by <literal>spi_exec_query</literal>, or in <literal>spi_query_prepared</literal> which returns a cursor
427 exactly as <literal>spi_query</literal> does, which can be later passed to <literal>spi_fetchrow</literal>.
431 The advantage of prepared queries is that is it possible to use one prepared plan for more
432 than one query execution. After the plan is not needed anymore, it may be freed with
433 <literal>spi_freeplan</literal>:
438 CREATE OR REPLACE FUNCTION init() RETURNS INTEGER AS $$
439 $_SHARED{my_plan} = spi_prepare( 'SELECT (now() + $1)::date AS now', 'INTERVAL');
442 CREATE OR REPLACE FUNCTION add_time( INTERVAL ) RETURNS TEXT AS $$
443 return spi_exec_prepared(
446 )->{rows}->[0]->{now};
449 CREATE OR REPLACE FUNCTION done() RETURNS INTEGER AS $$
450 spi_freeplan( $_SHARED{my_plan});
451 undef $_SHARED{my_plan};
455 SELECT add_time('1 day'), add_time('2 days'), add_time('3 days');
458 add_time | add_time | add_time
459 ------------+------------+------------
460 2005-12-10 | 2005-12-11 | 2005-12-12
465 Note that the parameter subscript in <literal>spi_prepare</literal> is defined via
466 $1, $2, $3, etc, so avoid declaring query strings in double quotes that might easily
467 lead to hard-to-catch bugs.
471 Normally, <function>spi_fetchrow</> should be repeated until it
472 returns <literal>undef</literal>, indicating that there are no more
473 rows to read. The cursor is automatically freed when
474 <function>spi_fetchrow</> returns <literal>undef</literal>.
475 If you do not wish to read all the rows, instead call
476 <function>spi_cursor_close</> to free the cursor.
477 Failure to do so will result in memory leaks.
484 <primary>elog</primary>
485 <secondary>in PL/Perl</secondary>
488 <term><literal><function>elog</>(<replaceable>level</replaceable>, <replaceable>msg</replaceable>)</literal></term>
491 Emit a log or error message. Possible levels are
492 <literal>DEBUG</>, <literal>LOG</>, <literal>INFO</>,
493 <literal>NOTICE</>, <literal>WARNING</>, and <literal>ERROR</>.
495 raises an error condition; if this is not trapped by the surrounding
496 Perl code, the error propagates out to the calling query, causing
497 the current transaction or subtransaction to be aborted. This
498 is effectively the same as the Perl <literal>die</> command.
499 The other levels only generate messages of different
501 Whether messages of a particular priority are reported to the client,
502 written to the server log, or both is controlled by the
503 <xref linkend="guc-log-min-messages"> and
504 <xref linkend="guc-client-min-messages"> configuration
505 variables. See <xref linkend="runtime-config"> for more
514 <sect1 id="plperl-data">
515 <title>Data Values in PL/Perl</title>
518 The argument values supplied to a PL/Perl function's code are
519 simply the input arguments converted to text form (just as if they
520 had been displayed by a <command>SELECT</command> statement).
521 Conversely, the <literal>return</> command will accept any string
522 that is acceptable input format for the function's declared return
523 type. So, within the PL/Perl function,
524 all values are just text strings.
528 <sect1 id="plperl-global">
529 <title>Global Values in PL/Perl</title>
532 You can use the global hash <varname>%_SHARED</varname> to store
533 data, including code references, between function calls for the
534 lifetime of the current session.
538 Here is a simple example for shared data:
540 CREATE OR REPLACE FUNCTION set_var(name text, val text) RETURNS text AS $$
541 if ($_SHARED{$_[0]} = $_[1]) {
544 return "can't set shared variable $_[0] to $_[1]";
548 CREATE OR REPLACE FUNCTION get_var(name text) RETURNS text AS $$
549 return $_SHARED{$_[0]};
552 SELECT set_var('sample', 'Hello, PL/Perl! How's tricks?');
553 SELECT get_var('sample');
558 Here is a slightly more complicated example using a code reference:
561 CREATE OR REPLACE FUNCTION myfuncs() RETURNS void AS $$
562 $_SHARED{myquote} = sub {
564 $arg =~ s/(['\\])/\\$1/g;
569 SELECT myfuncs(); /* initializes the function */
571 /* Set up a function that uses the quote function */
573 CREATE OR REPLACE FUNCTION use_quote(TEXT) RETURNS text AS $$
574 my $text_to_quote = shift;
575 my $qfunc = $_SHARED{myquote};
576 return &$qfunc($text_to_quote);
580 (You could have replaced the above with the one-liner
581 <literal>return $_SHARED{myquote}->($_[0]);</literal>
582 at the expense of readability.)
586 <sect1 id="plperl-trusted">
587 <title>Trusted and Untrusted PL/Perl</title>
589 <indexterm zone="plperl-trusted">
590 <primary>trusted</primary>
591 <secondary>PL/Perl</secondary>
595 Normally, PL/Perl is installed as a <quote>trusted</> programming
596 language named <literal>plperl</>. In this setup, certain Perl
597 operations are disabled to preserve security. In general, the
598 operations that are restricted are those that interact with the
599 environment. This includes file handle operations,
600 <literal>require</literal>, and <literal>use</literal> (for
601 external modules). There is no way to access internals of the
602 database server process or to gain OS-level access with the
603 permissions of the server process,
604 as a C function can do. Thus, any unprivileged database user may
605 be permitted to use this language.
609 Here is an example of a function that will not work because file
610 system operations are not allowed for security reasons:
612 CREATE FUNCTION badfunc() RETURNS integer AS $$
613 my $tmpfile = "/tmp/badfile";
614 open my $fh, '>', $tmpfile
615 or elog(ERROR, qq{could not open the file "$tmpfile": $!});
616 print $fh "Testing writing to a file\n";
617 close $fh or elog(ERROR, qq{could not close the file "$tmpfile": $!});
621 The creation of this function will fail as its use of a forbidden
622 operation will be be caught by the validator.
626 Sometimes it is desirable to write Perl functions that are not
627 restricted. For example, one might want a Perl function that sends
628 mail. To handle these cases, PL/Perl can also be installed as an
629 <quote>untrusted</> language (usually called
630 <application>PL/PerlU</application><indexterm><primary>PL/PerlU</></indexterm>).
631 In this case the full Perl language is available. If the
632 <command>createlang</command> program is used to install the
633 language, the language name <literal>plperlu</literal> will select
634 the untrusted PL/Perl variant.
638 The writer of a <application>PL/PerlU</> function must take care that the function
639 cannot be used to do anything unwanted, since it will be able to do
640 anything that could be done by a user logged in as the database
641 administrator. Note that the database system allows only database
642 superusers to create functions in untrusted languages.
646 If the above function was created by a superuser using the language
647 <literal>plperlu</>, execution would succeed.
652 For security reasons, to stop a leak of privileged operations from
653 <application>PL/PerlU</> to <application>PL/Perl</>, these two languages
654 have to run in separate instances of the Perl interpreter. If your
655 Perl installation has been appropriately compiled, this is not a problem.
656 However, not all installations are compiled with the requisite flags.
657 If <productname>PostgreSQL</> detects that this is the case then it will
658 not start a second interpreter, but instead create an error. In
659 consequence, in such an installation, you cannot use both
660 <application>PL/PerlU</> and <application>PL/Perl</> in the same backend
661 process. The remedy for this is to obtain a Perl installation created
662 with the appropriate flags, namely either <literal>usemultiplicity</> or
663 both <literal>usethreads</> and <literal>useithreads</>.
664 For more details,see the <literal>perlembed</> manual page.
670 <sect1 id="plperl-triggers">
671 <title>PL/Perl Triggers</title>
674 PL/Perl can be used to write trigger functions. In a trigger function,
675 the hash reference <varname>$_TD</varname> contains information about the
676 current trigger event. <varname>$_TD</> is a global variable,
677 which gets a separate local value for each invocation of the trigger.
678 The fields of the <varname>$_TD</varname> hash reference are:
682 <term><literal>$_TD->{new}{foo}</literal></term>
685 <literal>NEW</literal> value of column <literal>foo</literal>
691 <term><literal>$_TD->{old}{foo}</literal></term>
694 <literal>OLD</literal> value of column <literal>foo</literal>
700 <term><literal>$_TD->{name}</literal></term>
703 Name of the trigger being called
709 <term><literal>$_TD->{event}</literal></term>
712 Trigger event: <literal>INSERT</>, <literal>UPDATE</>, <literal>DELETE</>, or <literal>UNKNOWN</>
718 <term><literal>$_TD->{when}</literal></term>
721 When the trigger was called: <literal>BEFORE</literal>, <literal>AFTER</literal>, or <literal>UNKNOWN</literal>
727 <term><literal>$_TD->{level}</literal></term>
730 The trigger level: <literal>ROW</literal>, <literal>STATEMENT</literal>, or <literal>UNKNOWN</literal>
736 <term><literal>$_TD->{relid}</literal></term>
739 OID of the table on which the trigger fired
745 <term><literal>$_TD->{table_name}</literal></term>
748 Name of the table on which the trigger fired
754 <term><literal>$_TD->{relname}</literal></term>
757 Name of the table on which the trigger fired. This has been deprecated,
758 and could be removed in a future release.
759 Please use $_TD->{table_name} instead.
765 <term><literal>$_TD->{table_schema}</literal></term>
768 Name of the schema in which the table on which the trigger fired, is
774 <term><literal>$_TD->{argc}</literal></term>
777 Number of arguments of the trigger function
783 <term><literal>@{$_TD->{args}}</literal></term>
786 Arguments of the trigger function. Does not exist if <literal>$_TD->{argc}</literal> is 0.
795 Triggers can return one of the following:
799 <term><literal>return;</literal></term>
802 Execute the statement
808 <term><literal>"SKIP"</literal></term>
811 Don't execute the statement
817 <term><literal>"MODIFY"</literal></term>
820 Indicates that the <literal>NEW</literal> row was modified by
829 Here is an example of a trigger function, illustrating some of the
837 CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$
838 if (($_TD->{new}{i} >= 100) || ($_TD->{new}{i} <= 0)) {
839 return "SKIP"; # skip INSERT/UPDATE command
840 } elsif ($_TD->{new}{v} ne "immortal") {
841 $_TD->{new}{v} .= "(modified by trigger)";
842 return "MODIFY"; # modify row and execute INSERT/UPDATE command
844 return; # execute INSERT/UPDATE command
848 CREATE TRIGGER test_valid_id_trig
849 BEFORE INSERT OR UPDATE ON test
850 FOR EACH ROW EXECUTE PROCEDURE valid_id();
855 <sect1 id="plperl-missing">
856 <title>Limitations and Missing Features</title>
859 The following features are currently missing from PL/Perl, but they
860 would make welcome contributions.
865 PL/Perl functions cannot call each other directly (because they
866 are anonymous subroutines inside Perl).
872 SPI is not yet fully implemented.
878 If you are fetching very large data sets using
879 <literal>spi_exec_query</literal>, you should be aware that
880 these will all go into memory. You can avoid this by using
881 <literal>spi_query</literal>/<literal>spi_fetchrow</literal> as
885 A similar problem occurs if a set-returning function passes a
886 large set of rows back to PostgreSQL via <literal>return</literal>. You
887 can avoid this problem too by instead using
888 <literal>return_next</literal> for each row returned, as shown