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

Commit47888fe

Browse files
committed
First phase of OUT-parameters project. We can now define and use SQL
functions with OUT parameters. The various PLs still need work, as doespg_dump. Rudimentary docs and regression tests included.
1 parentfb13881 commit47888fe

File tree

23 files changed

+1503
-532
lines changed

23 files changed

+1503
-532
lines changed

‎doc/src/sgml/ref/create_function.sgml

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.64 2005/01/04 00:39:53 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.65 2005/03/31 22:45:59 tgl Exp $
33
-->
44

55
<refentry id="SQL-CREATEFUNCTION">
@@ -19,8 +19,9 @@ $PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.64 2005/01/04 00:39
1919

2020
<refsynopsisdiv>
2121
<synopsis>
22-
CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
23-
RETURNS <replaceable class="parameter">rettype</replaceable>
22+
CREATE [ OR REPLACE ] FUNCTION
23+
<replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
24+
[ RETURNS <replaceable class="parameter">rettype</replaceable> ]
2425
{ LANGUAGE <replaceable class="parameter">langname</replaceable>
2526
| IMMUTABLE | STABLE | VOLATILE
2627
| CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
@@ -57,7 +58,9 @@ CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable>
5758
tried, you would actually be creating a new, distinct function).
5859
Also, <command>CREATE OR REPLACE FUNCTION</command> will not let
5960
you change the return type of an existing function. To do that,
60-
you must drop and recreate the function.
61+
you must drop and recreate the function. (When using <literal>OUT</>
62+
parameters, that means you can't change the names or types of any
63+
<literal>OUT</> parameters except by dropping the function.)
6164
</para>
6265

6366
<para>
@@ -88,14 +91,28 @@ CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable>
8891
</listitem>
8992
</varlistentry>
9093

94+
<varlistentry>
95+
<term><replaceable class="parameter">argmode</replaceable></term>
96+
97+
<listitem>
98+
<para>
99+
The mode of an argument: either <literal>IN</>, <literal>OUT</>,
100+
or <literal>INOUT</>. If omitted, the default is <literal>IN</>.
101+
</para>
102+
</listitem>
103+
</varlistentry>
104+
91105
<varlistentry>
92106
<term><replaceable class="parameter">argname</replaceable></term>
93107

94108
<listitem>
95109
<para>
96110
The name of an argument. Some languages (currently only PL/pgSQL) let
97111
you use the name in the function body. For other languages the
98-
argument name is just extra documentation.
112+
name of an input argument is just extra documentation. But the name
113+
of an output argument is significant, since it defines the column
114+
name in the result row type. (If you omit the name for an output
115+
argument, the system will choose a default column name.)
99116
</para>
100117
</listitem>
101118
</varlistentry>
@@ -137,6 +154,13 @@ CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable>
137154
Depending on the implementation language it may also be allowed
138155
to specify <quote>pseudotypes</> such as <type>cstring</>.
139156
</para>
157+
<para>
158+
When there are <literal>OUT</> or <literal>INOUT</> parameters,
159+
the <literal>RETURNS</> clause may be omitted. If present, it
160+
must agree with the result type implied by the output parameters:
161+
<literal>RECORD</> if there are multiple output parameters, or
162+
the same type as the single output parameter.
163+
</para>
140164
<para>
141165
The <literal>SETOF</literal>
142166
modifier indicates that the function will return a set of
@@ -361,6 +385,16 @@ CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable>
361385
names).
362386
</para>
363387

388+
<para>
389+
Two functions are considered the same if they have the same names and
390+
<emphasis>input</> argument types, ignoring any <literal>OUT</>
391+
parameters. Thus for example these declarations conflict:
392+
<programlisting>
393+
CREATE FUNCTION foo(int) ...
394+
CREATE FUNCTION foo(int, out text) ...
395+
</programlisting>
396+
</para>
397+
364398
<para>
365399
When repeated <command>CREATE FUNCTION</command> calls refer to
366400
the same object file, the file is only loaded once. To unload and
@@ -393,7 +427,7 @@ CREATE [ OR REPLACE ] FUNCTION <replaceable class="parameter">name</replaceable>
393427
<title>Examples</title>
394428

395429
<para>
396-
Hereis a trivialexample to help you get started. For more
430+
Hereare some trivialexamples to help you get started. For more
397431
information and examples, see <xref linkend="xfunc">.
398432
<programlisting>
399433
CREATE FUNCTION add(integer, integer) RETURNS integer
@@ -407,13 +441,34 @@ CREATE FUNCTION add(integer, integer) RETURNS integer
407441
<para>
408442
Increment an integer, making use of an argument name, in
409443
<application>PL/pgSQL</application>:
410-
411444
<programlisting>
412445
CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS $$
413446
BEGIN
414447
RETURN i + 1;
415448
END;
416449
$$ LANGUAGE plpgsql;
450+
</programlisting>
451+
</para>
452+
453+
<para>
454+
Return a record containing multiple output parameters:
455+
<programlisting>
456+
CREATE FUNCTION dup(in int, out f1 int, out f2 text)
457+
AS $$ SELECT $1, CAST($1 AS text) || ' is text' $$
458+
LANGUAGE SQL;
459+
460+
SELECT * FROM dup(42);
461+
</programlisting>
462+
You can do the same thing more verbosely with an explicitly named
463+
composite type:
464+
<programlisting>
465+
CREATE TYPE dup_result AS (f1 int, f2 text);
466+
467+
CREATE FUNCTION dup(int) RETURNS dup_result
468+
AS $$ SELECT $1, CAST($1 AS text) || ' is text' $$
469+
LANGUAGE SQL;
470+
471+
SELECT * FROM dup(42);
417472
</programlisting>
418473
</para>
419474
</refsect1>
@@ -428,6 +483,13 @@ $$ LANGUAGE plpgsql;
428483
not fully compatible. The attributes are not portable, neither are the
429484
different available languages.
430485
</para>
486+
487+
<para>
488+
For compatibility with some other database systems,
489+
<replaceable class="parameter">argmode</replaceable> can be written
490+
either before or after <replaceable class="parameter">argname</replaceable>.
491+
But only the first way is standard-compliant.
492+
</para>
431493
</refsect1>
432494

433495

‎doc/src/sgml/xfunc.sgml

Lines changed: 102 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.101 2005/03/16 21:38:04 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.102 2005/03/31 22:46:02 tgl Exp $
33
-->
44

55
<sect1 id="xfunc">
@@ -172,7 +172,7 @@ INSERT INTO $1 VALUES (42);
172172
</programlisting>
173173
</para>
174174

175-
<sect2>
175+
<sect2 id="xfunc-sql-base-functions">
176176
<title><acronym>SQL</acronym> Functions on Base Types</title>
177177

178178
<para>
@@ -484,7 +484,7 @@ SELECT emp.name, emp.double_salary FROM emp;
484484
</tip>
485485

486486
<para>
487-
Another way to use a function returning arow result is to pass the
487+
Another way to use a function returning acomposite type is to pass the
488488
result to another function that accepts the correct row type as input:
489489

490490
<screen>
@@ -501,8 +501,89 @@ SELECT getname(new_emp());
501501
</para>
502502

503503
<para>
504-
Another way to use a function that returns a composite type is to
505-
call it as a table function, as described below.
504+
Still another way to use a function that returns a composite type is to
505+
call it as a table function, as described in <xref
506+
linkend="xfunc-sql-table-functions">.
507+
</para>
508+
</sect2>
509+
510+
<sect2 id="xfunc-output-parameters">
511+
<title>Functions with Output Parameters</title>
512+
513+
<indexterm>
514+
<primary>function</primary>
515+
<secondary>output parameter</secondary>
516+
</indexterm>
517+
518+
<para>
519+
An alternative way of describing a function's results is to define it
520+
with <firstterm>output parameters</>, as in this example:
521+
522+
<screen>
523+
CREATE FUNCTION add_em (IN x int, IN y int, OUT sum int)
524+
AS 'SELECT $1 + $2'
525+
LANGUAGE SQL;
526+
527+
SELECT add_em(3,7);
528+
add_em
529+
--------
530+
10
531+
(1 row)
532+
</screen>
533+
534+
This is not essentially different from the version of <literal>add_em</>
535+
shown in <xref linkend="xfunc-sql-base-functions">. The real value of
536+
output parameters is that they provide a convenient way of defining
537+
functions that return several columns. For example,
538+
539+
<screen>
540+
CREATE FUNCTION sum_n_product (x int, y int, OUT sum int, OUT product int)
541+
AS 'SELECT $1 + $2, $1 * $2'
542+
LANGUAGE SQL;
543+
544+
SELECT * FROM sum_n_product(11,42);
545+
sum | product
546+
-----+---------
547+
53 | 462
548+
(1 row)
549+
</screen>
550+
551+
What has essentially happened here is that we have created an anonymous
552+
composite type for the result of the function. The above example has
553+
the same end result as
554+
555+
<screen>
556+
CREATE TYPE sum_prod AS (sum int, product int);
557+
558+
CREATE FUNCTION sum_n_product (int, int) RETURNS sum_prod
559+
AS 'SELECT $1 + $2, $1 * $2'
560+
LANGUAGE SQL;
561+
</screen>
562+
563+
but not having to bother with the separate composite type definition
564+
is often handy.
565+
</para>
566+
567+
<para>
568+
Notice that output parameters are not included in the calling argument
569+
list when invoking such a function from SQL. This is because
570+
<productname>PostgreSQL</productname> considers only the input
571+
parameters to define the function's calling signature. That means
572+
also that only the input parameters matter when referencing the function
573+
for purposes such as dropping it. We could drop the above function
574+
with either of
575+
576+
<screen>
577+
DROP FUNCTION sum_n_product (x int, y int, OUT sum int, OUT product int);
578+
DROP FUNCTION sum_n_product (int, int);
579+
</screen>
580+
</para>
581+
582+
<para>
583+
Parameters can be marked as <literal>IN</> (the default),
584+
<literal>OUT</>, or <literal>INOUT</>. An <literal>INOUT</>
585+
parameter serves as both an input parameter (part of the calling
586+
argument list) and an output parameter (part of the result record type).
506587
</para>
507588
</sect2>
508589

@@ -692,6 +773,21 @@ CREATE FUNCTION invalid_func() RETURNS anyelement AS $$
692773
$$ LANGUAGE SQL;
693774
ERROR: cannot determine result data type
694775
DETAIL: A function returning "anyarray" or "anyelement" must have at least one argument of either type.
776+
</screen>
777+
</para>
778+
779+
<para>
780+
Polymorphism can be used with functions that have output arguments.
781+
For example:
782+
<screen>
783+
CREATE FUNCTION dup (f1 anyelement, OUT f2 anyelement, OUT f3 anyarray)
784+
AS 'select $1, array[$1,$1]' LANGUAGE sql;
785+
786+
SELECT * FROM dup(22);
787+
f2 | f3
788+
----+---------
789+
22 | {22,22}
790+
(1 row)
695791
</screen>
696792
</para>
697793
</sect2>
@@ -962,7 +1058,7 @@ CREATE FUNCTION square_root(double precision) RETURNS double precision
9621058
<sect1 id="xfunc-c">
9631059
<title>C-Language Functions</title>
9641060

965-
<indexterm zone="xfunc-sql">
1061+
<indexterm zone="xfunc-c">
9661062
<primary>function</primary>
9671063
<secondary>user-defined</secondary>
9681064
<tertiary>in C</tertiary>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp