Skip to content

Commit 54ec775

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 e3bd026 commit 54ec775

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

doc/src/sgml/queries.sgml

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

810838
<sect3 id="queries-lateral">

0 commit comments

Comments
 (0)