93
93
</para>
94
94
95
95
<para>
96
- Alternatively, an SQL function can be declared to return a set,
97
- by specifying the function's return type as <literal>SETOF
96
+ Alternatively, an SQL function can be declared to return a set (that is ,
97
+ multiple rows) by specifying the function's return type as <literal>SETOF
98
98
<replaceable>sometype</></literal>, or equivalently by declaring it as
99
99
<literal>RETURNS TABLE(<replaceable>columns</>)</literal>. In this case
100
100
all rows of the last query's result are returned. Further details appear
@@ -927,19 +927,15 @@ SELECT * FROM sum_n_product_with_tab(10);
927
927
</para>
928
928
929
929
<para>
930
- Currently, functions returning sets can also be called in the select list
931
- of a query. For each row that the query
932
- generates by itself, the function returning set is invoked, and an output
933
- row is generated for each element of the function's result set. Note ,
934
- however, that this capability is deprecated and might be removed in future
935
- releases. The following is an example function returning a set from the
936
- select list :
930
+ It is frequently useful to construct a query's result by invoking a
931
+ set-returning function multiple times, with the parameters for each
932
+ invocation coming from successive rows of a table or subquery. The
933
+ preferred way to do this is to use the <literal>LATERAL</> key word ,
934
+ which is described in <xref linkend="queries-lateral">.
935
+ Here is an example using a set-returning function to enumerate
936
+ elements of a tree structure :
937
937
938
938
<screen>
939
- CREATE FUNCTION listchildren(text) RETURNS SETOF text AS $$
940
- SELECT name FROM nodes WHERE parent = $1
941
- $$ LANGUAGE SQL;
942
-
943
939
SELECT * FROM nodes;
944
940
name | parent
945
941
-----------+--------
@@ -951,6 +947,44 @@ SELECT * FROM nodes;
951
947
SubChild2 | Child1
952
948
(6 rows)
953
949
950
+ CREATE FUNCTION listchildren(text) RETURNS SETOF text AS $$
951
+ SELECT name FROM nodes WHERE parent = $1
952
+ $$ LANGUAGE SQL STABLE;
953
+
954
+ SELECT * FROM listchildren('Top');
955
+ listchildren
956
+ --------------
957
+ Child1
958
+ Child2
959
+ Child3
960
+ (3 rows)
961
+
962
+ SELECT name, child FROM nodes, LATERAL listchildren(name) AS child;
963
+ name | child
964
+ --------+-----------
965
+ Top | Child1
966
+ Top | Child2
967
+ Top | Child3
968
+ Child1 | SubChild1
969
+ Child1 | SubChild2
970
+ (5 rows)
971
+ </screen>
972
+
973
+ This example does not do anything that we couldn't have done with a
974
+ simple join, but in more complex calculations the option to put
975
+ some of the work into a function can be quite convenient.
976
+ </para>
977
+
978
+ <para>
979
+ Currently, functions returning sets can also be called in the select list
980
+ of a query. For each row that the query
981
+ generates by itself, the function returning set is invoked, and an output
982
+ row is generated for each element of the function's result set. Note,
983
+ however, that this capability is deprecated and might be removed in future
984
+ releases. The previous example could also be done with queries like
985
+ these:
986
+
987
+ <screen>
954
988
SELECT listchildren('Top');
955
989
listchildren
956
990
--------------
@@ -973,7 +1007,9 @@ SELECT name, listchildren(name) FROM nodes;
973
1007
In the last <command>SELECT</command>,
974
1008
notice that no output row appears for <literal>Child2</>, <literal>Child3</>, etc.
975
1009
This happens because <function>listchildren</function> returns an empty set
976
- for those arguments, so no result rows are generated.
1010
+ for those arguments, so no result rows are generated. This is the same
1011
+ behavior as we got from an inner join to the function result when using
1012
+ the <literal>LATERAL</> syntax.
977
1013
</para>
978
1014
979
1015
<note>
@@ -987,6 +1023,19 @@ SELECT name, listchildren(name) FROM nodes;
987
1023
still happen (and are all completed before returning from the function).
988
1024
</para>
989
1025
</note>
1026
+
1027
+ <note>
1028
+ <para>
1029
+ The key problem with using set-returning functions in the select list,
1030
+ rather than the <literal>FROM</> clause, is that putting more than one
1031
+ set-returning function in the same select list does not behave very
1032
+ sensibly. (What you actually get if you do so is a number of output
1033
+ rows equal to the least common multiple of the numbers of rows produced
1034
+ by each set-returning function.) The <literal>LATERAL</> syntax
1035
+ produces less surprising results when calling multiple set-returning
1036
+ functions, and should usually be used instead.
1037
+ </para>
1038
+ </note>
990
1039
</sect2>
991
1040
992
1041
<sect2 id="xfunc-sql-functions-returning-table">
0 commit comments