<literal>FROM</literal>
item; the names in the column definitions serve as column aliases.
When using the <literal>ROWS FROM()</literal> syntax,
a <replaceable>column_definition</replaceable> list can be attached to
each member function separately; or if there is only one member function
and no <literal>WITH ORDINALITY</literal> clause,
a <replaceable>column_definition</replaceable> list can be written in
place of a column alias list following <literal>ROWS FROM()</literal>.
</para>
<para>
Consider this example:
<programlisting>
SELECT *
FROM dblink('dbname=mydb', 'SELECT proname, prosrc FROM pg_proc')
AS t1(proname name, prosrc text)
WHERE proname LIKE 'bytea%';
</programlisting>
The <xref linkend="contrib-dblink-function"/> function
(part of the <xref linkend="dblink"/> module) executes
a remote query. It is declared to return
<type>record</type> since it might be used for any kind of query.
The actual column set must be specified in the calling query so
that the parser knows, for example, what <literal>*</literal> should
expand to.
</para>
<para>
This example uses <literal>ROWS FROM</literal>:
<programlisting>
SELECT *
FROM ROWS FROM
(
json_to_recordset('[{"a":40,"b":"foo"},{"a":"100","b":"bar"}]')
AS (a INTEGER, b TEXT),
generate_series(1, 3)
) AS x (p, q, s)
ORDER BY p;
p | q | s
-----+-----+---
40 | foo | 1
100 | bar | 2
| | 3
</programlisting>
It joins two functions into a single <literal>FROM</literal>
target. <function>json_to_recordset()</function> is instructed
to return two columns, the first <type>integer</type>
and the second <type>text</type>. The result of
<function>generate_series()</function> is used directly.
The <literal>ORDER BY</literal> clause sorts the column values
as integers.
</para>
</sect3>
<sect3 id="queries-lateral">
<title><literal>LATERAL</literal> Subqueries</title>
<indexterm zone="queries-lateral">
<primary>LATERAL</primary>
<secondary>in the FROM clause</secondary>
</indexterm>
<para>
Subqueries appearing in <literal>FROM</literal> can be
preceded by the key word <literal>LATERAL</literal>. This allows them to
reference columns provided by preceding <literal>FROM</literal> items.
(Without <literal>LATERAL</literal>, each subquery is
evaluated independently and so cannot cross-reference any other
<literal>FROM</literal> item.)
</para>
<para>
Table functions appearing in <literal>FROM</literal> can also be
preceded by the key word <literal>LATERAL</literal>, but for functions the
key word is optional; the function's arguments can contain references
to columns provided by preceding <literal>FROM</literal> items in any case.
</para>
<para>
A <literal>LATERAL</literal> item can appear at the top level in the
<literal>FROM</literal> list, or within a <literal>JOIN</literal> tree. In the latter
case it can also refer to any items that are on the left-hand side of a
<literal>JOIN</literal> that it is on the right-hand side of.
</para>
<para>
When a <literal>FROM</literal> item contains <literal>LATERAL</literal>
cross-references, evaluation proceeds as follows: for each row of the
<literal>FROM</literal> item providing the cross-referenced column(s), or
set of rows of multiple <literal>FROM</literal> items providing the
columns, the <literal>LATERAL</literal> item is evaluated using that
row or row set's values of the columns. The resulting row(s) are
joined as usual with the rows they were computed from. This is
repeated for each row or set of rows from the column source table(s).
</para>
<para>
A trivial example of <literal>LATERAL</literal> is
<programlisting>
SELECT * FROM foo, LATERAL (SELECT * FROM bar