11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.62 2005/03/13 09:36:30 neilc Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.63 2005/04/05 06:22:14 tgl Exp $
33-->
44
55<chapter id="plpgsql">
@@ -83,7 +83,7 @@ $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.62 2005/03/13 09:36:30 neilc Ex
8383 that contains many statements for which execution plans might be
8484 required will only prepare and save those plans that are really
8585 used during the lifetime of the database connection. This can
86- substantially reduce the total amount of time required to parse,
86+ substantially reduce the total amount of time required to parse
8787 and generate execution plans for the statements in a
8888 <application>PL/pgSQL</> function. A disadvantage is that errors
8989 in a specific expression or command may not be detected until that
@@ -215,6 +215,7 @@ $$ LANGUAGE plpgsql;
215215 <type>void</> if it has no useful return value.
216216 </para>
217217
218+ <note>
218219 <para>
219220 <application>PL/pgSQL</> does not currently have full support for
220221 domain types: it treats a domain the same as the underlying scalar
@@ -223,6 +224,20 @@ $$ LANGUAGE plpgsql;
223224 it is a hazard if you declare a <application>PL/pgSQL</> function
224225 as returning a domain type.
225226 </para>
227+ </note>
228+
229+ <para>
230+ <application>PL/pgSQL</> functions can also be declared with output
231+ parameters in place of an explicit specification of the return type.
232+ This does not add any fundamental capability to the language, but
233+ it is often convenient, especially for returning multiple values.
234+ </para>
235+
236+ <para>
237+ Specific examples appear in
238+ <xref linkend="plpgsql-declaration-aliases"> and
239+ <xref linkend="plpgsql-statements-returning">.
240+ </para>
226241 </sect2>
227242 </sect1>
228243
@@ -631,19 +646,62 @@ DECLARE
631646 v_string ALIAS FOR $1;
632647 index ALIAS FOR $2;
633648BEGIN
634- -- some computations here
649+ -- some computationsusing v_string and index here
635650END;
636651$$ LANGUAGE plpgsql;
637652
638653
639- CREATE FUNCTION concat_selected_fields(in_ttablename ) RETURNS text AS $$
654+ CREATE FUNCTION concat_selected_fields(in_tsometablename ) RETURNS text AS $$
640655BEGIN
641656 RETURN in_t.f1 || in_t.f3 || in_t.f5 || in_t.f7;
642657END;
643658$$ LANGUAGE plpgsql;
644659</programlisting>
645660 </para>
646661
662+ <para>
663+ When a <application>PL/pgSQL</application> function is declared
664+ with output parameters, the output parameters are given
665+ <literal>$<replaceable>n</replaceable></literal> names and optional
666+ aliases in just the same way as the normal input parameters. An
667+ output parameter is effectively a variable that starts out NULL;
668+ it should be assigned to during the execution of the function.
669+ The final value of the parameter is what is returned. For instance,
670+ the sales-tax example could also be done this way:
671+
672+ <programlisting>
673+ CREATE FUNCTION sales_tax(subtotal real, OUT tax real) AS $$
674+ BEGIN
675+ tax := subtotal * 0.06;
676+ RETURN;
677+ END;
678+ $$ LANGUAGE plpgsql;
679+ </programlisting>
680+
681+ Notice that we omitted <literal>RETURNS real</> — we could have
682+ included it, but it would be redundant.
683+ </para>
684+
685+ <para>
686+ Output parameters are most useful when returning multiple values.
687+ A trivial example is:
688+
689+ <programlisting>
690+ CREATE FUNCTION sum_n_product(x int, y int, OUT sum int, OUT prod int) AS $$
691+ BEGIN
692+ sum := x + y;
693+ prod := x * y;
694+ RETURN;
695+ END;
696+ $$ LANGUAGE plpgsql;
697+ </programlisting>
698+
699+ As discussed in <xref linkend="xfunc-output-parameters">, this
700+ effectively creates an anonymous record type for the function's
701+ results. If a <literal>RETURNS</> clause is given, it must say
702+ <literal>RETURNS record</>.
703+ </para>
704+
647705 <para>
648706 When the return type of a <application>PL/pgSQL</application>
649707 function is declared as a polymorphic type (<type>anyelement</type>
@@ -658,6 +716,7 @@ $$ LANGUAGE plpgsql;
658716 though that is not required. <literal>$0</literal> can also be
659717 given an alias. For example, this function works on any data type
660718 that has a <literal>+</> operator:
719+
661720<programlisting>
662721CREATE FUNCTION add_three_values(v1 anyelement, v2 anyelement, v3 anyelement)
663722RETURNS anyelement AS $$
@@ -668,6 +727,24 @@ BEGIN
668727 RETURN result;
669728END;
670729$$ LANGUAGE plpgsql;
730+ </programlisting>
731+ </para>
732+
733+ <para>
734+ The same effect can be had by declaring one or more output parameters as
735+ <type>anyelement</type> or <type>anyarray</type>. In this case the
736+ special <literal>$0</literal> parameter is not used; the output
737+ parameters themselves serve the same purpose. For example:
738+
739+ <programlisting>
740+ CREATE FUNCTION add_three_values(v1 anyelement, v2 anyelement, v3 anyelement,
741+ OUT sum anyelement)
742+ AS $$
743+ BEGIN
744+ sum := v1 + v2 + v3;
745+ RETURN;
746+ END;
747+ $$ LANGUAGE plpgsql;
671748</programlisting>
672749 </para>
673750 </sect2>
@@ -756,18 +833,21 @@ user_id users.user_id%TYPE;
756833 </para>
757834
758835 <para>
759- Here is an example of using composite types:
836+ Here is an example of using composite types. <structname>table1</>
837+ and <structname>table2</> are existing tables having at least the
838+ mentioned fields:
839+
760840<programlisting>
761- CREATE FUNCTION merge_fields(t_rowtablename ) RETURNS text AS $$
841+ CREATE FUNCTION merge_fields(t_rowtable1 ) RETURNS text AS $$
762842DECLARE
763- t2_rowtable2name %ROWTYPE;
843+ t2_rowtable2 %ROWTYPE;
764844BEGIN
765- SELECT * INTO t2_row FROMtable2name WHERE ... ;
845+ SELECT * INTO t2_row FROMtable2 WHERE ... ;
766846 RETURN t_row.f1 || t2_row.f3 || t_row.f5 || t2_row.f7;
767847END;
768848$$ LANGUAGE plpgsql;
769849
770- SELECT merge_fields(t.*) FROMtablename t WHERE ... ;
850+ SELECT merge_fields(t.*) FROMtable1 t WHERE ... ;
771851</programlisting>
772852 </para>
773853 </sect2>
@@ -1411,6 +1491,12 @@ RETURN <replaceable>expression</replaceable>;
14111491 as the <replaceable>expression</replaceable>.
14121492 </para>
14131493
1494+ <para>
1495+ If you declared the function with output parameters, write just
1496+ <command>RETURN</command> with no expression. The current values
1497+ of the output parameter variables will be returned.
1498+ </para>
1499+
14141500 <para>
14151501 The return value of a function cannot be left undefined. If
14161502 control reaches the end of the top-level block of the function
@@ -1441,8 +1527,30 @@ RETURN NEXT <replaceable>expression</replaceable>;
14411527 commands, and then a final <command>RETURN</command> command
14421528 with no argument is used to indicate that the function has
14431529 finished executing. <command>RETURN NEXT</command> can be used
1444- with both scalar and composite data types; in the latter case, an
1445- entire <quote>table</quote> of results will be returned.
1530+ with both scalar and composite data types; with a composite result
1531+ type, an entire <quote>table</quote> of results will be returned.
1532+ </para>
1533+
1534+ <para>
1535+ <command>RETURN NEXT</command> does not actually return from the
1536+ function — it simply saves away the value of the expression.
1537+ Execution then continues with the next statement in
1538+ the <application>PL/pgSQL</> function. As successive
1539+ <command>RETURN NEXT</command> commands are executed, the result
1540+ set is built up. A final <command>RETURN</command>, which should
1541+ have no argument, causes control to exit the function.
1542+ </para>
1543+
1544+ <para>
1545+ If you declared the function with output parameters, write just
1546+ <command>RETURN NEXT</command> with no expression. The current values
1547+ of the output parameter variable(s) will be saved for eventual return.
1548+ Note that you must declare the function as returning
1549+ <literal>SETOF record</literal> when there are
1550+ multiple output parameters, or
1551+ <literal>SETOF <replaceable>sometype</></literal> when there is
1552+ just one output parameter of type <replaceable>sometype</>, in
1553+ order to create a set-returning function with output parameters.
14461554 </para>
14471555
14481556 <para>
@@ -1457,16 +1565,6 @@ SELECT * FROM some_func();
14571565 <literal>FROM</literal> clause.
14581566 </para>
14591567
1460- <para>
1461- <command>RETURN NEXT</command> does not actually return from the
1462- function; it simply saves away the value of the expression.
1463- Execution then continues with the next statement in
1464- the <application>PL/pgSQL</> function. As successive
1465- <command>RETURN NEXT</command> commands are executed, the result
1466- set is built up. A final <command>RETURN</command>, which should
1467- have no argument, causes control to exit the function.
1468- </para>
1469-
14701568 <note>
14711569 <para>
14721570 The current implementation of <command>RETURN NEXT</command>