1
1
<!--
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 $
3
3
-->
4
4
5
5
<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
83
83
that contains many statements for which execution plans might be
84
84
required will only prepare and save those plans that are really
85
85
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
87
87
and generate execution plans for the statements in a
88
88
<application>PL/pgSQL</> function. A disadvantage is that errors
89
89
in a specific expression or command may not be detected until that
@@ -215,6 +215,7 @@ $$ LANGUAGE plpgsql;
215
215
<type>void</> if it has no useful return value.
216
216
</para>
217
217
218
+ <note>
218
219
<para>
219
220
<application>PL/pgSQL</> does not currently have full support for
220
221
domain types: it treats a domain the same as the underlying scalar
@@ -223,6 +224,20 @@ $$ LANGUAGE plpgsql;
223
224
it is a hazard if you declare a <application>PL/pgSQL</> function
224
225
as returning a domain type.
225
226
</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>
226
241
</sect2>
227
242
</sect1>
228
243
@@ -631,19 +646,62 @@ DECLARE
631
646
v_string ALIAS FOR $1;
632
647
index ALIAS FOR $2;
633
648
BEGIN
634
- -- some computations here
649
+ -- some computations using v_string and index here
635
650
END;
636
651
$$ LANGUAGE plpgsql;
637
652
638
653
639
- CREATE FUNCTION concat_selected_fields(in_t tablename ) RETURNS text AS $$
654
+ CREATE FUNCTION concat_selected_fields(in_t sometablename ) RETURNS text AS $$
640
655
BEGIN
641
656
RETURN in_t.f1 || in_t.f3 || in_t.f5 || in_t.f7;
642
657
END;
643
658
$$ LANGUAGE plpgsql;
644
659
</programlisting>
645
660
</para>
646
661
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
+
647
705
<para>
648
706
When the return type of a <application>PL/pgSQL</application>
649
707
function is declared as a polymorphic type (<type>anyelement</type>
@@ -658,6 +716,7 @@ $$ LANGUAGE plpgsql;
658
716
though that is not required. <literal>$0</literal> can also be
659
717
given an alias. For example, this function works on any data type
660
718
that has a <literal>+</> operator:
719
+
661
720
<programlisting>
662
721
CREATE FUNCTION add_three_values(v1 anyelement, v2 anyelement, v3 anyelement)
663
722
RETURNS anyelement AS $$
@@ -668,6 +727,24 @@ BEGIN
668
727
RETURN result;
669
728
END;
670
729
$$ 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;
671
748
</programlisting>
672
749
</para>
673
750
</sect2>
@@ -756,18 +833,21 @@ user_id users.user_id%TYPE;
756
833
</para>
757
834
758
835
<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
+
760
840
<programlisting>
761
- CREATE FUNCTION merge_fields(t_row tablename ) RETURNS text AS $$
841
+ CREATE FUNCTION merge_fields(t_row table1 ) RETURNS text AS $$
762
842
DECLARE
763
- t2_row table2name %ROWTYPE;
843
+ t2_row table2 %ROWTYPE;
764
844
BEGIN
765
- SELECT * INTO t2_row FROM table2name WHERE ... ;
845
+ SELECT * INTO t2_row FROM table2 WHERE ... ;
766
846
RETURN t_row.f1 || t2_row.f3 || t_row.f5 || t2_row.f7;
767
847
END;
768
848
$$ LANGUAGE plpgsql;
769
849
770
- SELECT merge_fields(t.*) FROM tablename t WHERE ... ;
850
+ SELECT merge_fields(t.*) FROM table1 t WHERE ... ;
771
851
</programlisting>
772
852
</para>
773
853
</sect2>
@@ -1411,6 +1491,12 @@ RETURN <replaceable>expression</replaceable>;
1411
1491
as the <replaceable>expression</replaceable>.
1412
1492
</para>
1413
1493
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
+
1414
1500
<para>
1415
1501
The return value of a function cannot be left undefined. If
1416
1502
control reaches the end of the top-level block of the function
@@ -1441,8 +1527,30 @@ RETURN NEXT <replaceable>expression</replaceable>;
1441
1527
commands, and then a final <command>RETURN</command> command
1442
1528
with no argument is used to indicate that the function has
1443
1529
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.
1446
1554
</para>
1447
1555
1448
1556
<para>
@@ -1457,16 +1565,6 @@ SELECT * FROM some_func();
1457
1565
<literal>FROM</literal> clause.
1458
1566
</para>
1459
1567
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
-
1470
1568
<note>
1471
1569
<para>
1472
1570
The current implementation of <command>RETURN NEXT</command>
0 commit comments