9393 </para>
9494
9595 <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
9898 <replaceable>sometype</></literal>, or equivalently by declaring it as
9999 <literal>RETURNS TABLE(<replaceable>columns</>)</literal>. In this case
100100 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);
927927 </para>
928928
929929 <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 thefunction's result set. Note ,
934- however, that this capability isdeprecated and might be removed infuture
935- releases. The following is an examplefunction 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 isdescribed in<xref linkend="queries-lateral">.
935+ Here is an exampleusing a set-returning function to enumerate
936+ elements of a tree structure :
937937
938938<screen>
939- CREATE FUNCTION listchildren(text) RETURNS SETOF text AS $$
940- SELECT name FROM nodes WHERE parent = $1
941- $$ LANGUAGE SQL;
942-
943939SELECT * FROM nodes;
944940 name | parent
945941-----------+--------
@@ -951,6 +947,44 @@ SELECT * FROM nodes;
951947 SubChild2 | Child1
952948(6 rows)
953949
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>
954988SELECT listchildren('Top');
955989 listchildren
956990--------------
@@ -973,7 +1007,9 @@ SELECT name, listchildren(name) FROM nodes;
9731007 In the last <command>SELECT</command>,
9741008 notice that no output row appears for <literal>Child2</>, <literal>Child3</>, etc.
9751009 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.
9771013 </para>
9781014
9791015 <note>
@@ -987,6 +1023,19 @@ SELECT name, listchildren(name) FROM nodes;
9871023 still happen (and are all completed before returning from the function).
9881024 </para>
9891025 </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>
9901039 </sect2>
9911040
9921041 <sect2 id="xfunc-sql-functions-returning-table">