Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit dd0a64e

Browse files
committed
doc: show functions returning record types and use of ROWS FROM
Previously it was unclear exactly how ROWS FROM behaved and how to cast the data types of columns returned by FROM functions. Also document that only non-OUT record functions can have their columns cast to data types. Reported-by: guyren@gmail.com Discussion: https://postgr.es/m/158638264419.662.2482095087061084020@wrigleys.postgresql.org Backpatch-through: 9.5
1 parent 253f102 commit dd0a64e

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

doc/src/sgml/queries.sgml

+29-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,8 @@ SELECT * FROM vw_getfoo;
762762
In some cases it is useful to define table functions that can
763763
return different column sets depending on how they are invoked.
764764
To support this, the table function can be declared as returning
765-
the pseudo-type <type>record</type>. When such a function is used in
765+
the pseudo-type <type>record</type> with no <literal>OUT</literal>
766+
parameters. When such a function is used in
766767
a query, the expected row structure must be specified in the
767768
query itself, so that the system can know how to parse and plan
768769
the query. This syntax looks like:
@@ -803,6 +804,33 @@ SELECT *
803804
that the parser knows, for example, what <literal>*</literal> should
804805
expand to.
805806
</para>
807+
808+
<para>
809+
This example uses <literal>ROWS FROM</literal>:
810+
<programlisting>
811+
SELECT *
812+
FROM ROWS FROM
813+
(
814+
json_to_recordset('[{"a":40,"b":"foo"},{"a":"100","b":"bar"}]')
815+
AS (a INTEGER, b TEXT),
816+
generate_series(1, 3)
817+
) AS x (p, q, s)
818+
ORDER BY p;
819+
820+
p | q | s
821+
-----+-----+---
822+
40 | foo | 1
823+
100 | bar | 2
824+
| | 3
825+
</programlisting>
826+
It joins two functions into a single <literal>FROM</literal>
827+
target. <function>json_to_recordset()</function> is instructed
828+
to return two columns, the first <type>integer</type>
829+
and the second <type>text</type>. The result of
830+
<function>generate_series()</function> is used directly.
831+
The <literal>ORDER BY</literal> clause sorts the column values
832+
as integers.
833+
</para>
806834
</sect3>
807835

808836
<sect3 id="queries-lateral">

0 commit comments

Comments
 (0)