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

Commit0631017

Browse files
committed
More documentation updates for LATERAL.
Extend xfunc.sgml's discussion of set-returning functions to show anexample of using LATERAL, and recommend that over putting SRFs in thetargetlist.In passing, reword func.sgml's section on set-returning functions sothat it doesn't claim that the functions listed therein are all thebuilt-in set-returning functions. That hasn't been true for a longtime, and trying to make it so doesn't seem like it would be animprovement. (Perhaps we should rename that section?)Both per suggestions from Merlin Moncure.
1 parent5cad024 commit0631017

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

‎doc/src/sgml/func.sgml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12548,9 +12548,10 @@ AND
1254812548

1254912549
<para>
1255012550
This section describes functions that possibly return more than one row.
12551-
Currently the only functions in this class are series generating functions,
12552-
as detailed in <xref linkend="functions-srf-series"> and
12553-
<xref linkend="functions-srf-subscripts">.
12551+
The most widely used functions in this class are series generating
12552+
functions, as detailed in <xref linkend="functions-srf-series"> and
12553+
<xref linkend="functions-srf-subscripts">. Other, more specialized
12554+
set-returning functions are described elsewhere in this manual.
1255412555
</para>
1255512556

1255612557
<table id="functions-srf-series">

‎doc/src/sgml/xfunc.sgml

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@
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 ofthefunction's result set. Note,
934-
however, that this capabilityisdeprecated and might be removedinfuture
935-
releases. The followingis an examplefunction returninga 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 usethe<literal>LATERAL</> key word,
934+
whichisdescribedin<xref linkend="queries-lateral">.
935+
Hereis an exampleusinga 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-
943939
SELECT * 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>
954988
SELECT 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">

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp