1
1
<!--
2
- $Header: /cvsroot/pgsql/doc/src/sgml/plpgsql.sgml,v 1.26 2003/09/12 22:17:23 tgl Exp $
2
+ $Header: /cvsroot/pgsql/doc/src/sgml/plpgsql.sgml,v 1.27 2003/09/23 19:58:50 tgl Exp $
3
3
-->
4
4
5
5
<chapter id="plpgsql">
156
156
157
157
<para>
158
158
That means that your client application must send each query to
159
- the database server, wait for it to process it , receive the
159
+ the database server, wait for it to be processed , receive the
160
160
results, do some computation, then send other queries to the
161
161
server. All this incurs interprocess communication and may also
162
162
incur network overhead if your client is on a different machine
@@ -652,7 +652,7 @@ user_id users.user_id%TYPE;
652
652
an existing table or view, by using the
653
653
<replaceable>table_name</replaceable><literal>%ROWTYPE</literal>
654
654
notation; or it can be declared by giving a composite type's name.
655
- (Since every table has an associated datatype of the same name,
655
+ (Since every table has an associated composite type of the same name,
656
656
it actually does not matter in <productname>PostgreSQL</> whether you
657
657
write <literal>%ROWTYPE</literal> or not. But the form with
658
658
<literal>%ROWTYPE</literal> is more portable.)
@@ -916,12 +916,13 @@ tax := subtotal * 0.06;
916
916
variable, or list of scalar variables. This is done by:
917
917
918
918
<synopsis>
919
- SELECT INTO <replaceable>target</replaceable> <replaceable>expressions </replaceable> FROM ...;
919
+ SELECT INTO <replaceable>target</replaceable> <replaceable>select_expressions </replaceable> FROM ...;
920
920
</synopsis>
921
921
922
922
where <replaceable>target</replaceable> can be a record variable, a row
923
923
variable, or a comma-separated list of simple variables and
924
- record/row fields.
924
+ record/row fields. The <replaceable>select_expressions</replaceable>
925
+ and the remainder of the command are the same as in regular SQL.
925
926
</para>
926
927
927
928
<para>
@@ -1085,9 +1086,11 @@ EXECUTE <replaceable class="command">command-string</replaceable>;
1085
1086
The results from <command>SELECT</command> commands are discarded
1086
1087
by <command>EXECUTE</command>, and <command>SELECT INTO</command>
1087
1088
is not currently supported within <command>EXECUTE</command>.
1088
- So, the only way to extract a result from a dynamically-created
1089
- <command>SELECT</command> is to use the <command>FOR-IN-EXECUTE</> form
1090
- described later.
1089
+ There are two ways to extract a result from a dynamically-created
1090
+ <command>SELECT</command>: one is to use the <command>FOR-IN-EXECUTE</>
1091
+ loop form described in <xref linkend="plpgsql-records-iterating">,
1092
+ and the other is to use a cursor with <command>OPEN-FOR-EXECUTE</>, as
1093
+ described in <xref linkend="plpgsql-cursor-opening">.
1091
1094
</para>
1092
1095
1093
1096
<para>
@@ -1107,8 +1110,8 @@ EXECUTE ''UPDATE tbl SET ''
1107
1110
<function>quote_literal(<type>text</type>)</function>.<indexterm><primary>quote_ident</><secondary>use
1108
1111
in
1109
1112
PL/pgSQL</></indexterm><indexterm><primary>quote_literal</><secondary>use
1110
- in PL/pgSQL</></indexterm> Variables containing column and table
1111
- identifiers should be passed to function
1113
+ in PL/pgSQL</></indexterm> For safety, variables containing column and
1114
+ table identifiers should be passed to function
1112
1115
<function>quote_ident</function>. Variables containing values
1113
1116
that should be literal strings in the constructed command should
1114
1117
be passed to <function>quote_literal</function>. Both take the
@@ -1157,8 +1160,8 @@ END;
1157
1160
1158
1161
<para>
1159
1162
There are several ways to determine the effect of a command. The
1160
- first method is to use the <command>GET DIAGNOSTICS</command>,
1161
- which has the form:
1163
+ first method is to use the <command>GET DIAGNOSTICS</command>
1164
+ command, which has the form:
1162
1165
1163
1166
<synopsis>
1164
1167
GET DIAGNOSTICS <replaceable>variable</replaceable> = <replaceable>item</replaceable> <optional> , ... </optional> ;
@@ -1179,15 +1182,15 @@ GET DIAGNOSTICS <replaceable>variable</replaceable> = <replaceable>item</replace
1179
1182
<para>
1180
1183
An example:
1181
1184
<programlisting>
1182
- GET DIAGNOSTICS var_integer = ROW_COUNT;
1185
+ GET DIAGNOSTICS integer_var = ROW_COUNT;
1183
1186
</programlisting>
1184
1187
</para>
1185
1188
1186
1189
<para>
1187
- The second method to determine the effects of a command is the
1188
- special variable named <literal>FOUND</literal> of
1190
+ The second method to determine the effects of a command is to check the
1191
+ special variable named <literal>FOUND</literal>, which is of
1189
1192
type <type>boolean</type>. <literal>FOUND</literal> starts out
1190
- false within each <application>PL/pgSQL</application> function.
1193
+ false within each <application>PL/pgSQL</application> function call .
1191
1194
It is set by each of the following types of statements:
1192
1195
<itemizedlist>
1193
1196
<listitem>
@@ -1200,7 +1203,7 @@ GET DIAGNOSTICS var_integer = ROW_COUNT;
1200
1203
<listitem>
1201
1204
<para>
1202
1205
A <command>PERFORM</> statement sets <literal>FOUND</literal>
1203
- true if it produces (discards) a row, false if no row is
1206
+ true if it produces (and discards) a row, false if no row is
1204
1207
produced.
1205
1208
</para>
1206
1209
</listitem>
@@ -1271,7 +1274,7 @@ RETURN <replaceable>expression</replaceable>;
1271
1274
<command>RETURN</command> with an expression terminates the
1272
1275
function and returns the value of
1273
1276
<replaceable>expression</replaceable> to the caller. This form
1274
- is to be used for <application>PL/pgSQL</> functions that does
1277
+ is to be used for <application>PL/pgSQL</> functions that do
1275
1278
not return a set.
1276
1279
</para>
1277
1280
@@ -1287,11 +1290,14 @@ RETURN <replaceable>expression</replaceable>;
1287
1290
The return value of a function cannot be left undefined. If
1288
1291
control reaches the end of the top-level block of the function
1289
1292
without hitting a <command>RETURN</command> statement, a run-time
1290
- error will occur. Note that if you have declared the function to
1293
+ error will occur.
1294
+ </para>
1295
+
1296
+ <para>
1297
+ If you have declared the function to
1291
1298
return <type>void</type>, a <command>RETURN</command> statement
1292
- must still be specified; the expression following
1293
- <command>RETURN</command> is, however, optional and will be ignored in
1294
- any case.
1299
+ must still be specified; but in this case the expression following
1300
+ <command>RETURN</command> is optional and will be ignored if present.
1295
1301
</para>
1296
1302
</sect3>
1297
1303
@@ -1308,9 +1314,9 @@ RETURN NEXT <replaceable>expression</replaceable>;
1308
1314
to follow is slightly different. In that case, the individual
1309
1315
items to return are specified in <command>RETURN NEXT</command>
1310
1316
commands, and then a final <command>RETURN</command> command
1311
- with no arguments is used to indicate that the function has
1317
+ with no argument is used to indicate that the function has
1312
1318
finished executing. <command>RETURN NEXT</command> can be used
1313
- with both scalar and composite data types; in the later case, an
1319
+ with both scalar and composite data types; in the latter case, an
1314
1320
entire <quote>table</quote> of results will be returned.
1315
1321
</para>
1316
1322
@@ -1347,7 +1353,7 @@ SELECT * FROM some_func();
1347
1353
written to disk to avoid memory exhaustion, but the function
1348
1354
itself will not return until the entire result set has been
1349
1355
generated. A future version of <application>PL/pgSQL</> may
1350
- allow users to allow users to define set-returning functions
1356
+ allow users to define set-returning functions
1351
1357
that do not have this limitation. Currently, the point at
1352
1358
which data begins being written to disk is controlled by the
1353
1359
<varname>sort_mem</> configuration variable. Administrators
@@ -1585,19 +1591,19 @@ EXIT <optional> <replaceable>label</replaceable> </optional> <optional> WHEN <re
1585
1591
<programlisting>
1586
1592
LOOP
1587
1593
-- some computations
1588
- IF count > 0 THEN
1594
+ IF count > 0 THEN
1589
1595
EXIT; -- exit loop
1590
1596
END IF;
1591
1597
END LOOP;
1592
1598
1593
1599
LOOP
1594
1600
-- some computations
1595
- EXIT WHEN count > 0;
1601
+ EXIT WHEN count > 0; -- same result as previous example
1596
1602
END LOOP;
1597
1603
1598
1604
BEGIN
1599
1605
-- some computations
1600
- IF stocks > 100000 THEN
1606
+ IF stocks > 100000 THEN
1601
1607
EXIT; -- invalid; cannot use EXIT outside of LOOP
1602
1608
END IF;
1603
1609
END;
@@ -1625,7 +1631,7 @@ END LOOP;
1625
1631
<para>
1626
1632
For example:
1627
1633
<programlisting>
1628
- WHILE amount_owed > 0 AND gift_certificate_balance > 0 LOOP
1634
+ WHILE amount_owed > 0 AND gift_certificate_balance > 0 LOOP
1629
1635
-- some computations here
1630
1636
END LOOP;
1631
1637
@@ -1660,19 +1666,20 @@ END LOOP;
1660
1666
Some examples of integer <literal>FOR</> loops:
1661
1667
<programlisting>
1662
1668
FOR i IN 1..10 LOOP
1663
- -- some expressions here
1669
+ -- some computations here
1664
1670
RAISE NOTICE ''i is %'', i;
1665
1671
END LOOP;
1666
1672
1667
1673
FOR i IN REVERSE 10..1 LOOP
1668
- -- some expressions here
1674
+ -- some computations here
1669
1675
END LOOP;
1670
1676
</programlisting>
1671
1677
</para>
1672
1678
1673
1679
<para>
1674
- If the lower bound is greater than the upper bound, the loop body is not
1675
- executed at all, but no error is raised.
1680
+ If the lower bound is greater than the upper bound (or less than,
1681
+ in the <literal>REVERSE</> case), the loop body is not
1682
+ executed at all. No error is raised.
1676
1683
</para>
1677
1684
</sect3>
1678
1685
</sect2>
@@ -1744,7 +1751,9 @@ END LOOP;
1744
1751
declared as a record or row variable. If not, it's presumed to be
1745
1752
an integer <literal>FOR</> loop. This can cause rather nonintuitive error
1746
1753
messages when the true problem is, say, that one has
1747
- misspelled the variable name after the <literal>FOR</>.
1754
+ misspelled the variable name after the <literal>FOR</>. Typically
1755
+ the complaint will be something like <literal>missing ".." at end of SQL
1756
+ expression</>.
1748
1757
</para>
1749
1758
</note>
1750
1759
</sect2>
@@ -1766,7 +1775,7 @@ END LOOP;
1766
1775
rows. (However, <application>PL/pgSQL</> users do not normally need
1767
1776
to worry about that, since <literal>FOR</> loops automatically use a cursor
1768
1777
internally to avoid memory problems.) A more interesting usage is to
1769
- return a reference to a cursor that it has created, allowing the
1778
+ return a reference to a cursor that a function has created, allowing the
1770
1779
caller to read the rows. This provides an efficient way to return
1771
1780
large row sets from functions.
1772
1781
</para>
@@ -1819,8 +1828,8 @@ DECLARE
1819
1828
Before a cursor can be used to retrieve rows, it must be
1820
1829
<firstterm>opened</>. (This is the equivalent action to the SQL
1821
1830
command <command>DECLARE CURSOR</>.) <application>PL/pgSQL</> has
1822
- three forms of the <command>OPEN</> statement, two of which use unbound cursor
1823
- variables and the other uses a bound cursor variable.
1831
+ three forms of the <command>OPEN</> statement, two of which use unbound
1832
+ cursor variables while the third uses a bound cursor variable.
1824
1833
</para>
1825
1834
1826
1835
<sect3>
@@ -1958,7 +1967,7 @@ CLOSE <replaceable>cursor</replaceable>;
1958
1967
</synopsis>
1959
1968
1960
1969
<para>
1961
- <command>CLOSE</command> closes the Portal underlying an open
1970
+ <command>CLOSE</command> closes the portal underlying an open
1962
1971
cursor. This can be used to release resources earlier than end of
1963
1972
transaction, or to free up the cursor variable to be opened again.
1964
1973
</para>
@@ -1976,18 +1985,41 @@ CLOSE curs1;
1976
1985
1977
1986
<para>
1978
1987
<application>PL/pgSQL</> functions can return cursors to the
1979
- caller. This is used to return multiple rows or columns from
1980
- the function. To do this, the function opens the cursor and returns the
1981
- cursor name to the caller. The caller can then
1982
- fetch rows from the cursor. The cursor can
1983
- be closed by the caller, or it will be closed automatically
1988
+ caller. This is useful to return multiple rows or columns,
1989
+ especially with very large result sets. To do this, the function
1990
+ opens the cursor and returns the cursor name to the caller (or simply
1991
+ opens the cursor using a portal name specified by or otherwise known
1992
+ to the caller). The caller can then fetch rows from the cursor. The
1993
+ cursor can be closed by the caller, or it will be closed automatically
1984
1994
when the transaction closes.
1985
1995
</para>
1986
1996
1987
1997
<para>
1988
- The cursor name returned by the function can be specified by the
1989
- caller or automatically generated. The following example shows
1990
- how a cursor name can be supplied by the caller:
1998
+ The portal name used for a cursor can be specified by the
1999
+ programmer or automatically generated. To specify a portal name,
2000
+ simply assign a string to the <type>refcursor</> variable before
2001
+ opening it. The string value of the <type>refcursor</> variable
2002
+ will be used by <command>OPEN</> as the name of the underlying portal.
2003
+ However, if the <type>refcursor</> variable is NULL,
2004
+ <command>OPEN</> automatically generates a name that does not
2005
+ conflict with any existing portal, and assigns it to the
2006
+ <type>refcursor</> variable.
2007
+ </para>
2008
+
2009
+ <note>
2010
+ <para>
2011
+ A bound cursor variable is initialized to the string value
2012
+ representing its name, so that the portal name is the same as
2013
+ the cursor variable name, unless the programmer overrides it
2014
+ by assignment before opening the cursor. But an unbound cursor
2015
+ variable defaults to an initial value of NULL, so it will receive
2016
+ an automatically-generated unique name, unless overridden.
2017
+ </para>
2018
+ </note>
2019
+
2020
+ <para>
2021
+ The following example shows one way a cursor name can be supplied by
2022
+ the caller:
1991
2023
1992
2024
<programlisting>
1993
2025
CREATE TABLE test (col text);
@@ -2128,8 +2160,8 @@ RAISE EXCEPTION ''Inexistent ID --> %'', user_id;
2128
2160
<para>
2129
2161
<application>PL/pgSQL</application> can be used to define trigger
2130
2162
procedures. A trigger procedure is created with the
2131
- <command>CREATE FUNCTION</> command as a function with no
2132
- arguments and a return type of <type>trigger</type>. Note that
2163
+ <command>CREATE FUNCTION</> command, declaring it as a function with
2164
+ no arguments and a return type of <type>trigger</type>. Note that
2133
2165
the function must be declared with no arguments even if it expects
2134
2166
to receive arguments specified in <command>CREATE TRIGGER</> ---
2135
2167
trigger arguments are passed via <varname>TG_ARGV</>, as described
@@ -2255,24 +2287,31 @@ RAISE EXCEPTION ''Inexistent ID --> %'', user_id;
2255
2287
<para>
2256
2288
A trigger function must return either null or a record/row value
2257
2289
having exactly the structure of the table the trigger was fired
2258
- for. The return value of a <literal>BEFORE</> or <literal>AFTER</> statement-level
2259
- trigger or an <literal>AFTER</> row-level trigger is ignored; it may as well
2260
- be null. However, any of these types of triggers can still
2261
- abort the entire trigger operation by raising an error.
2290
+ for.
2262
2291
</para>
2263
2292
2264
2293
<para>
2265
2294
Row-level triggers fired <literal>BEFORE</> may return null to signal the
2266
2295
trigger manager to skip the rest of the operation for this row
2267
2296
(i.e., subsequent triggers are not fired, and the
2268
- <command>INSERT</>/<command>UPDATE</>/<command>DELETE</> does not occur for this row). If a nonnull
2297
+ <command>INSERT</>/<command>UPDATE</>/<command>DELETE</> does not occur
2298
+ for this row). If a nonnull
2269
2299
value is returned then the operation proceeds with that row value.
2270
2300
Returning a row value different from the original value
2271
- of <varname>NEW</> alters the row that will be inserted or updated. It is
2272
- possible to replace single values directly in <varname>NEW</> and return <varname>NEW</>,
2301
+ of <varname>NEW</> alters the row that will be inserted or updated
2302
+ (but has no direct effect in the <command>DELETE</> case).
2303
+ To alter the row to be stored, it is possible to replace single values
2304
+ directly in <varname>NEW</> and return the modified <varname>NEW</>,
2273
2305
or to build a complete new record/row to return.
2274
2306
</para>
2275
2307
2308
+ <para>
2309
+ The return value of a <literal>BEFORE</> or <literal>AFTER</>
2310
+ statement-level trigger or an <literal>AFTER</> row-level trigger is
2311
+ always ignored; it may as well be null. However, any of these types of
2312
+ triggers can still abort the entire operation by raising an error.
2313
+ </para>
2314
+
2276
2315
<para>
2277
2316
<xref linkend="plpgsql-trigger-example"> shows an example of a
2278
2317
trigger procedure in <application>PL/pgSQL</application>.
@@ -2284,7 +2323,7 @@ RAISE EXCEPTION ''Inexistent ID --> %'', user_id;
2284
2323
<para>
2285
2324
This example trigger ensures that any time a row is inserted or updated
2286
2325
in the table, the current user name and time are stamped into the
2287
- row. And it ensures that an employee's name is given and that the
2326
+ row. And it checks that an employee's name is given and that the
2288
2327
salary is a positive value.
2289
2328
</para>
2290
2329
@@ -2343,7 +2382,7 @@ CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
2343
2382
This section explains differences between
2344
2383
<productname>PostgreSQL</>'s <application>PL/pgSQL</application>
2345
2384
language and Oracle's <application>PL/SQL</application> language,
2346
- to help developers that port applications from Oracle to
2385
+ to help developers who port applications from Oracle to
2347
2386
<productname>PostgreSQL</>.
2348
2387
</para>
2349
2388
@@ -2815,7 +2854,7 @@ END;
2815
2854
2816
2855
<para>
2817
2856
The <application>PL/pgSQL</> version of
2818
- <command>EXECUTE</command> works similar to the
2857
+ <command>EXECUTE</command> works similarly to the
2819
2858
<application>PL/SQL</> version, but you have to remember to use
2820
2859
<function>quote_literal(text)</function> and
2821
2860
<function>quote_string(text)</function> as described in <xref
0 commit comments