11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.34 2004/12/13 18:05:08 petere Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.35 2004/12/30 21:45:36 tgl Exp $
33-->
44
55 <chapter id="plperl">
@@ -132,9 +132,66 @@ CREATE FUNCTION empcomp(employee) RETURNS integer AS $$
132132 return $emp->{basesalary} + $emp->{bonus};
133133$$ LANGUAGE plperl;
134134
135- SELECT name, empcomp(employee) FROM employee;
135+ SELECT name, empcomp(employee.* ) FROM employee;
136136</programlisting>
137137 </para>
138+
139+ <para>
140+ A PL/Perl function can return a composite-type result using the same
141+ approach: return a reference to a hash that has the required attributes.
142+ For example,
143+
144+ <programlisting>
145+ CREATE TYPE testrowperl AS (f1 integer, f2 text, f3 text);
146+
147+ CREATE OR REPLACE FUNCTION perl_row() RETURNS testrowperl AS $$
148+ return {f2 => 'hello', f1 => 1, f3 => 'world'};
149+ $$ LANGUAGE plperl;
150+
151+ SELECT * FROM perl_row();
152+ </programlisting>
153+
154+ Any columns in the declared result data type that are not present in the
155+ hash will be returned as NULLs.
156+ </para>
157+
158+ <para>
159+ PL/Perl functions can also return sets of either scalar or composite
160+ types. To do this, return a reference to an array that contains
161+ either scalars or references to hashes, respectively. Here are
162+ some simple examples:
163+
164+ <programlisting>
165+ CREATE OR REPLACE FUNCTION perl_set_int(int) RETURNS SETOF INTEGER AS $$
166+ return [0..$_[0]];
167+ $$ LANGUAGE plperl;
168+
169+ SELECT * FROM perl_set_int(5);
170+
171+
172+ CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testrowperl AS $$
173+ return [
174+ { f1 => 1, f2 => 'Hello', f3 => 'World' },
175+ { f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL' },
176+ { f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' }
177+ ];
178+ $$ LANGUAGE plperl;
179+
180+ SELECT * FROM perl_set();
181+ </programlisting>
182+
183+ Note that when you do this, Perl will have to build the entire array in
184+ memory; therefore the technique does not scale to very large result sets.
185+ </para>
186+
187+ <para>
188+ <application>PL/Perl</> does not currently have full support for
189+ domain types: it treats a domain the same as the underlying scalar
190+ type. This means that constraints associated with the domain will
191+ not be enforced. This is not an issue for function arguments, but
192+ it is a hazard if you declare a <application>PL/Perl</> function
193+ as returning a domain type.
194+ </para>
138195 </sect1>
139196
140197 <sect1 id="plperl-database">
@@ -202,6 +259,37 @@ $res = $rv->{status};
202259 To get the number of rows affected, do:
203260<programlisting>
204261$nrows = $rv->{processed};
262+ </programlisting>
263+ </para>
264+
265+ <para>
266+ Here is a complete example:
267+ <programlisting>
268+ CREATE TABLE test (
269+ i int,
270+ v varchar
271+ );
272+
273+ INSERT INTO test (i, v) VALUES (1, 'first line');
274+ INSERT INTO test (i, v) VALUES (2, 'second line');
275+ INSERT INTO test (i, v) VALUES (3, 'third line');
276+ INSERT INTO test (i, v) VALUES (4, 'immortal');
277+
278+ CREATE FUNCTION test_munge() RETURNS SETOF test AS $$
279+ my $res = [];
280+ my $rv = spi_exec_query('select i, v from test;');
281+ my $status = $rv->{status};
282+ my $nrows = $rv->{processed};
283+ foreach my $rn (0 .. $nrows - 1) {
284+ my $row = $rv->{rows}[$rn];
285+ $row->{i} += 200 if defined($row->{i});
286+ $row->{v} =~ tr/A-Za-z/a-zA-Z/ if (defined($row->{v}));
287+ push @$res, $row;
288+ }
289+ return $res;
290+ $$ LANGUAGE plperl;
291+
292+ SELECT * FROM test_munge();
205293</programlisting>
206294 </para>
207295 </listitem>
@@ -224,8 +312,14 @@ $nrows = $rv->{processed};
224312 Perl code, the error propagates out to the calling query, causing
225313 the current transaction or subtransaction to be aborted. This
226314 is effectively the same as the Perl <literal>die</> command.
227- The other levels simply report the message to the system log
228- and/or client.
315+ The other levels only generate messages of different
316+ priority levels.
317+ Whether messages of a particular priority are reported to the client,
318+ written to the server log, or both is controlled by the
319+ <xref linkend="guc-log-min-messages"> and
320+ <xref linkend="guc-client-min-messages"> configuration
321+ variables. See <xref linkend="runtime-config"> for more
322+ information.
229323 </para>
230324 </listitem>
231325 </varlistentry>
@@ -242,72 +336,8 @@ $nrows = $rv->{processed};
242336 had been displayed by a <command>SELECT</command> statement).
243337 Conversely, the <literal>return</> command will accept any string
244338 that is acceptable input format for the function's declared return
245- type. So, the PL/Perl programmer can manipulate data values as if
246- they were just text.
247- </para>
248-
249- <para>
250- PL/Perl can also return row sets and composite types, and row sets
251- of composite types. Here is an example of a PL/Perl function
252- returning a row set of a row type. Note that a composite type is
253- always represented as a hash reference.
254- <programlisting>
255- CREATE TABLE test (
256- i int,
257- v varchar
258- );
259-
260- INSERT INTO test (i, v) VALUES (1, 'first line');
261- INSERT INTO test (i, v) VALUES (2, 'second line');
262- INSERT INTO test (i, v) VALUES (3, 'third line');
263- INSERT INTO test (i, v) VALUES (4, 'immortal');
264-
265- CREATE FUNCTION test_munge() RETURNS SETOF test AS $$
266- my $res = [];
267- my $rv = spi_exec_query('select i, v from test;');
268- my $status = $rv->{status};
269- my $nrows = $rv->{processed};
270- foreach my $rn (0 .. $nrows - 1) {
271- my $row = $rv->{rows}[$rn];
272- $row->{i} += 200 if defined($row->{i});
273- $row->{v} =~ tr/A-Za-z/a-zA-Z/ if (defined($row->{v}));
274- push @$res, $row;
275- }
276- return $res;
277- $$ LANGUAGE plperl;
278-
279- SELECT * FROM test_munge();
280- </programlisting>
281- </para>
282-
283- <para>
284- Here is an example of a PL/Perl function returning a composite
285- type:
286- <programlisting>
287- CREATE TYPE testrowperl AS (f1 integer, f2 text, f3 text);
288-
289- CREATE OR REPLACE FUNCTION perl_row() RETURNS testrowperl AS $$
290- return {f2 => 'hello', f1 => 1, f3 => 'world'};
291- $$ LANGUAGE plperl;
292- </programlisting>
293- </para>
294-
295- <para>
296- Here is an example of a PL/Perl function returning a row set of a
297- composite type. Since a row set is always a reference to an array
298- and a composite type is always a reference to a hash, a row set of a
299- composite type is a reference to an array of hash references.
300- <programlisting>
301- CREATE TYPE testsetperl AS (f1 integer, f2 text, f3 text);
302-
303- CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testsetperl AS $$
304- return [
305- { f1 => 1, f2 => 'Hello', f3 => 'World' },
306- { f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL' },
307- { f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' }
308- ];
309- $$ LANGUAGE plperl;
310- </programlisting>
339+ type. So, within the PL/Perl function,
340+ all values are just text strings.
311341 </para>
312342 </sect1>
313343
@@ -317,8 +347,7 @@ $$ LANGUAGE plperl;
317347 <para>
318348 You can use the global hash <varname>%_SHARED</varname> to store
319349 data, including code references, between function calls for the
320- lifetime of the current session, which is bounded from below by
321- the lifetime of the current transaction.
350+ lifetime of the current session.
322351 </para>
323352
324353 <para>
@@ -360,12 +389,12 @@ SELECT myfuncs(); /* initializes the function */
360389CREATE OR REPLACE FUNCTION use_quote(TEXT) RETURNS text AS $$
361390 my $text_to_quote = shift;
362391 my $qfunc = $_SHARED{myquote};
363- return &$qfunc($text_to_quote);
392+ return & $qfunc($text_to_quote);
364393$$ LANGUAGE plperl;
365394</programlisting>
366395
367396 (You could have replaced the above with the one-liner
368- <literal>return $_SHARED{myquote}-> ($_[0]);</literal>
397+ <literal>return $_SHARED{myquote}-> ($_[0]);</literal>
369398 at the expense of readability.)
370399 </para>
371400 </sect1>
@@ -619,9 +648,7 @@ CREATE TRIGGER test_valid_id_trig
619648 <para>
620649 In the current implementation, if you are fetching or returning
621650 very large data sets, you should be aware that these will all go
622- into memory. Future features will help with this. In the
623- meantime, we suggest that you not use PL/Perl if you will fetch
624- or return very large result sets.
651+ into memory.
625652 </para>
626653 </listitem>
627654 </itemizedlist>