Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit883ac5c

Browse files
committed
More minor updates and copy-editing.
1 parent1fbdb6b commit883ac5c

File tree

8 files changed

+513
-394
lines changed

8 files changed

+513
-394
lines changed

‎doc/src/sgml/plhandler.sgml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/plhandler.sgml,v 1.2 2003/11/29 19:51:37 pgsql Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/plhandler.sgml,v 1.3 2004/12/30 21:45:36 tgl Exp $
33
-->
44

55
<chapter id="plhandler">
@@ -154,6 +154,12 @@ CREATE LANGUAGE plsample
154154
</programlisting>
155155
</para>
156156

157+
<para>
158+
The procedural languages included in the standard distribution
159+
are good references when trying to write your own call handler.
160+
Look into the <filename>src/pl</> subdirectory of the source tree.
161+
</para>
162+
157163
</chapter>
158164

159165
<!-- Keep this comment at the end of the file

‎doc/src/sgml/plperl.sgml

Lines changed: 104 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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-&gt;{basesalary} + $emp-&gt;{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 =&gt; 'hello', f1 =&gt; 1, f3 =&gt; '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 =&gt; 1, f2 =&gt; 'Hello', f3 =&gt; 'World' },
175+
{ f1 =&gt; 2, f2 =&gt; 'Hello', f3 =&gt; 'PostgreSQL' },
176+
{ f1 =&gt; 3, f2 =&gt; 'Hello', f3 =&gt; '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-&gt;{status};
202259
To get the number of rows affected, do:
203260
<programlisting>
204261
$nrows = $rv-&gt;{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-&gt;{status};
282+
my $nrows = $rv-&gt;{processed};
283+
foreach my $rn (0 .. $nrows - 1) {
284+
my $row = $rv-&gt;{rows}[$rn];
285+
$row-&gt;{i} += 200 if defined($row-&gt;{i});
286+
$row-&gt;{v} =~ tr/A-Za-z/a-zA-Z/ if (defined($row-&gt;{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-&gt;{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-&gt;{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-&gt;{status};
269-
my $nrows = $rv-&gt;{processed};
270-
foreach my $rn (0 .. $nrows - 1) {
271-
my $row = $rv-&gt;{rows}[$rn];
272-
$row-&gt;{i} += 200 if defined($row-&gt;{i});
273-
$row-&gt;{v} =~ tr/A-Za-z/a-zA-Z/ if (defined($row-&gt;{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 =&gt; 'hello', f1 =&gt; 1, f3 =&gt; '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 =&gt; 1, f2 =&gt; 'Hello', f3 =&gt; 'World' },
306-
{ f1 =&gt; 2, f2 =&gt; 'Hello', f3 =&gt; 'PostgreSQL' },
307-
{ f1 =&gt; 3, f2 =&gt; 'Hello', f3 =&gt; '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 */
360389
CREATE 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 &amp;$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}-&gt;($_[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>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp