1
- <!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.115 2007/07/16 17:01:10 tgl Exp $ -->
1
+ <!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.116 2007/07/25 04:19:08 neilc Exp $ -->
2
2
3
3
<chapter id="plpgsql">
4
4
<title><application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language</title>
135
135
<application>PL/pgSQL</> functions can also be declared to return
136
136
a <quote>set</>, or table, of any data type they can return a single
137
137
instance of. Such a function generates its output by executing
138
- <literal>RETURN NEXT</> for each desired element of the result set.
138
+ <command>RETURN NEXT</> for each desired element of the result
139
+ set, or by using <command>RETURN QUERY</> to output the result of
140
+ evaluating a query.
139
141
</para>
140
142
141
143
<para>
@@ -1349,52 +1351,69 @@ RETURN <replaceable>expression</replaceable>;
1349
1351
</sect3>
1350
1352
1351
1353
<sect3>
1352
- <title><command>RETURN NEXT</></title>
1354
+ <title><command>RETURN NEXT</> and <command>RETURN QUERY</command></title>
1355
+ <indexterm>
1356
+ <primary>RETURN NEXT</primary>
1357
+ <secondary>in PL/PgSQL</secondary>
1358
+ </indexterm>
1359
+ <indexterm>
1360
+ <primary>RETURN QUERY</primary>
1361
+ <secondary>in PL/PgSQL</secondary>
1362
+ </indexterm>
1353
1363
1354
1364
<synopsis>
1355
1365
RETURN NEXT <replaceable>expression</replaceable>;
1366
+ RETURN QUERY <replaceable>query</replaceable>;
1356
1367
</synopsis>
1357
1368
1358
1369
<para>
1359
1370
When a <application>PL/pgSQL</> function is declared to return
1360
1371
<literal>SETOF <replaceable>sometype</></literal>, the procedure
1361
1372
to follow is slightly different. In that case, the individual
1362
- items to return are specified in <command>RETURN NEXT</command>
1363
- commands, and then a final <command>RETURN</command> command
1364
- with no argument is used to indicate that the function has
1365
- finished executing. <command>RETURN NEXT</command> can be used
1366
- with both scalar and composite data types; with a composite result
1367
- type, an entire <quote>table</quote> of results will be returned.
1373
+ items to return are specified by a sequence of <command>RETURN
1374
+ NEXT</command> or <command>RETURN QUERY</command> commands, and
1375
+ then a final <command>RETURN</command> command with no argument
1376
+ is used to indicate that the function has finished executing.
1377
+ <command>RETURN NEXT</command> can be used with both scalar and
1378
+ composite data types; with a composite result type, an entire
1379
+ <quote>table</quote> of results will be returned.
1380
+ <command>RETURN QUERY</command> appends the results of executing
1381
+ a query to the function's result set. <command>RETURN
1382
+ NEXT</command> and <command>RETURN QUERY</command> can be freely
1383
+ intermixed in a single set-returning function, in which case
1384
+ their results will be concatenated.
1368
1385
</para>
1369
1386
1370
1387
<para>
1371
- <command>RETURN NEXT</command> does not actually return from the
1372
- function — it simply saves away the value of the expression.
1373
- Execution then continues with the next statement in
1374
- the <application>PL/pgSQL</> function. As successive
1375
- <command>RETURN NEXT</command> commands are executed, the result
1376
- set is built up. A final <command>RETURN</command>, which should
1377
- have no argument, causes control to exit the function (or you can
1378
- just let control reach the end of the function).
1388
+ <command>RETURN NEXT</command> and <command>RETURN
1389
+ QUERY</command> do not actually return from the function —
1390
+ they simply append zero or more rows to the function's result
1391
+ set. Execution then continues with the next statement in the
1392
+ <application>PL/pgSQL</> function. As successive
1393
+ <command>RETURN NEXT</command> or <command>RETURN
1394
+ QUERY</command> commands are executed, the result set is built
1395
+ up. A final <command>RETURN</command>, which should have no
1396
+ argument, causes control to exit the function (or you can just
1397
+ let control reach the end of the function).
1379
1398
</para>
1380
1399
1381
1400
<para>
1382
1401
If you declared the function with output parameters, write just
1383
1402
<command>RETURN NEXT</command> with no expression. On each
1384
- execution, the current values
1385
- of the output parameter variable(s) will be saved for eventual return
1386
- as a row of the result.
1387
- Note that you must declare the function as returning
1388
- <literal>SETOF record</literal> when there are
1389
- multiple output parameters, or
1390
- <literal>SETOF <replaceable>sometype</></literal> when there is
1391
- just one output parameter of type <replaceable>sometype</>, in
1392
- order to create a set-returning function with output parameters.
1403
+ execution, the current values of the output parameter
1404
+ variable(s) will be saved for eventual return as a row of the
1405
+ result. Note that you must declare the function as returning
1406
+ <literal>SETOF record</literal> when there are multiple output
1407
+ parameters, or <literal>SETOF <replaceable>sometype</></literal>
1408
+ when there is just one output parameter of type
1409
+ <replaceable>sometype</>, in order to create a set-returning
1410
+ function with output parameters.
1393
1411
</para>
1394
1412
1395
1413
<para>
1396
- Functions that use <command>RETURN NEXT</command> should be
1397
- called in the following fashion:
1414
+ Functions that use <command>RETURN NEXT</command> or
1415
+ <command>RETURN QUERY</command> should be called in the
1416
+ following fashion:
1398
1417
1399
1418
<programlisting>
1400
1419
SELECT * FROM some_func();
@@ -1407,7 +1426,7 @@ SELECT * FROM some_func();
1407
1426
<note>
1408
1427
<para>
1409
1428
The current implementation of <command>RETURN NEXT</command>
1410
- for <application>PL/pgSQL</ > stores the entire result set
1429
+ and <command>RETURN QUERY</command > stores the entire result set
1411
1430
before returning from the function, as discussed above. That
1412
1431
means that if a <application>PL/pgSQL</> function produces a
1413
1432
very large result set, performance might be poor: data will be
0 commit comments