1
1
<!--
2
- $Header: /cvsroot/pgsql/doc/src/sgml/plpgsql.sgml,v 1.8 2002/09/21 18:32:53 petere Exp $
2
+ $Header: /cvsroot/pgsql/doc/src/sgml/plpgsql.sgml,v 1.9 2002/11/10 00:35:58 momjian Exp $
3
3
-->
4
4
5
5
<chapter id="plpgsql">
102
102
</programlisting>
103
103
If you execute the above function, it will reference the OID for
104
104
<function>my_function()</function> in the query plan produced for
105
- the PERFORM statement. Later, if you
105
+ the <command> PERFORM</command> statement. Later, if you
106
106
drop and re-create <function>my_function()</function>, then
107
107
<function>populate()</function> will not be able to find
108
108
<function>my_function()</function> anymore. You would then have to
@@ -117,17 +117,19 @@ END;
117
117
same tables and fields on every execution; that is, you cannot use
118
118
a parameter as the name of a table or field in a query. To get
119
119
around this restriction, you can construct dynamic queries using
120
- the <application>PL/pgSQL</application> EXECUTE statement --- at
121
- the price of constructing a new query plan on every execution.
120
+ the <application>PL/pgSQL</application> <command>EXECUTE</command>
121
+ statement --- at the price of constructing a new query plan on
122
+ every execution.
122
123
</para>
123
124
124
125
<note>
125
126
<para>
126
- The <application>PL/pgSQL</application> EXECUTE statement is not
127
- related to the EXECUTE statement supported by the
127
+ The <application>PL/pgSQL</application>
128
+ <command>EXECUTE</command> statement is not related to the
129
+ <command>EXECUTE</command> statement supported by the
128
130
<productname>PostgreSQL</productname> backend. The backend
129
- EXECUTE statement cannot be used within <application>PL/pgSQL</> functions (and
130
- is not needed).
131
+ <command> EXECUTE</command> statement cannot be used within
132
+ <application>PL/pgSQL</> functions (and is not needed).
131
133
</para>
132
134
</note>
133
135
@@ -173,13 +175,12 @@ END;
173
175
</para>
174
176
175
177
<para>
176
- That means that your client application must send each
177
- query to the database server, wait for it to process it,
178
- receive the results, do some computation, then send
179
- other queries to the server. All this incurs inter-process communication
180
- and may also incur network
181
- overhead if your client is on a different machine than
182
- the database server.
178
+ That means that your client application must send each query to
179
+ the database server, wait for it to process it, receive the
180
+ results, do some computation, then send other queries to the
181
+ server. All this incurs inter-process communication and may also
182
+ incur network overhead if your client is on a different machine
183
+ than the database server.
183
184
</para>
184
185
185
186
<para>
@@ -753,14 +754,14 @@ CREATE FUNCTION logfunc2 (TEXT) RETURNS TIMESTAMP AS '
753
754
754
755
<para>
755
756
The mutable nature of record variables presents a problem in this
756
- connection. When fields of a record variable are used in expressions or
757
- statements, the data types of the
758
- fields must not change between calls of one and the same expression,
759
- since the expression will be planned using the data type that is present
760
- when the expression is first reached.
761
- Keep this in mind when writing trigger procedures that handle events
762
- for more than one table. (EXECUTE can be used to get around this
763
- problem when necessary.)
757
+ connection. When fields of a record variable are used in
758
+ expressions or statements, the data types of the fields must not
759
+ change between calls of one and the same expression, since the
760
+ expression will be planned using the data type that is present
761
+ when the expression is first reached. Keep this in mind when
762
+ writing trigger procedures that handle events for more than one
763
+ table. (<command> EXECUTE</command> can be used to get around
764
+ this problem when necessary.)
764
765
</para>
765
766
</sect1>
766
767
@@ -904,10 +905,11 @@ END;
904
905
<title>Executing an expression or query with no result</title>
905
906
906
907
<para>
907
- Sometimes one wishes to evaluate an expression or query but discard
908
- the result (typically because one is calling a function that has
909
- useful side-effects but no useful result value). To do this in
910
- <application>PL/pgSQL</application>, use the PERFORM statement:
908
+ Sometimes one wishes to evaluate an expression or query but
909
+ discard the result (typically because one is calling a function
910
+ that has useful side-effects but no useful result value). To do
911
+ this in <application>PL/pgSQL</application>, use the
912
+ <command>PERFORM</command> statement:
911
913
912
914
<synopsis>
913
915
PERFORM <replaceable>query</replaceable>;
@@ -922,11 +924,12 @@ PERFORM <replaceable>query</replaceable>;
922
924
</para>
923
925
924
926
<note>
925
- <para>
926
- One might expect that SELECT with no INTO clause would accomplish
927
- this result, but at present the only accepted way to do it is PERFORM.
928
- </para>
929
- </note>
927
+ <para>
928
+ One might expect that <command>SELECT</command> with no INTO
929
+ clause would accomplish this result, but at present the only
930
+ accepted way to do it is <command>PERFORM</command>.
931
+ </para>
932
+ </note>
930
933
931
934
<para>
932
935
An example:
@@ -940,13 +943,13 @@ PERFORM create_mv(''cs_session_page_requests_mv'', my_query);
940
943
<title>Executing dynamic queries</title>
941
944
942
945
<para>
943
- Oftentimes you will want to generate dynamic queries inside
944
- your <application>PL/pgSQL</application> functions, that is,
945
- queries that will involve different tables or different data types
946
- each time they are executed. <application>PL/pgSQL</application>'s
946
+ Oftentimes you will want to generate dynamic queries inside your
947
+ <application>PL/pgSQL</application> functions, that is, queries
948
+ that will involve different tables or different data types each
949
+ time they are executed. <application>PL/pgSQL</application>'s
947
950
normal attempts to cache plans for queries will not work in such
948
- scenarios. To handle this sort of problem, the EXECUTE statement
949
- is provided:
951
+ scenarios. To handle this sort of problem, the
952
+ <command>EXECUTE</command> statement is provided:
950
953
951
954
<synopsis>
952
955
EXECUTE <replaceable class="command">query-string</replaceable>;
@@ -973,20 +976,22 @@ EXECUTE <replaceable class="command">query-string</replaceable>;
973
976
974
977
<para>
975
978
Unlike all other queries in <application>PL/pgSQL</>, a
976
- <replaceable>query</replaceable> run by an EXECUTE statement is
977
- not prepared and saved just once during the life of the server.
978
- Instead, the <replaceable>query</replaceable> is prepared each
979
- time the statement is run. The
980
- <replaceable>query-string</replaceable> can be dynamically
981
- created within the procedure to perform actions on variable
982
- tables and fields.
979
+ <replaceable>query</replaceable> run by an
980
+ <command>EXECUTE</command> statement is not prepared and saved
981
+ just once during the life of the server. Instead, the
982
+ <replaceable>query</replaceable> is prepared each time the
983
+ statement is run. The <replaceable>query-string</replaceable> can
984
+ be dynamically created within the procedure to perform actions on
985
+ variable tables and fields.
983
986
</para>
984
987
985
988
<para>
986
- The results from SELECT queries are discarded by EXECUTE, and
987
- SELECT INTO is not currently supported within EXECUTE. So, the
988
- only way to extract a result from a dynamically-created SELECT is
989
- to use the FOR-IN-EXECUTE form described later.
989
+ The results from <command>SELECT</command> queries are discarded
990
+ by <command>EXECUTE</command>, and <command>SELECT INTO</command>
991
+ is not currently supported within <command>EXECUTE</command>.
992
+ So, the only way to extract a result from a dynamically-created
993
+ <command>SELECT</command> is to use the FOR-IN-EXECUTE form
994
+ described later.
990
995
</para>
991
996
992
997
<para>
@@ -1017,7 +1022,8 @@ EXECUTE ''UPDATE tbl SET ''
1017
1022
</para>
1018
1023
1019
1024
<para>
1020
- Here is a much larger example of a dynamic query and EXECUTE:
1025
+ Here is a much larger example of a dynamic query and
1026
+ <command>EXECUTE</command>:
1021
1027
<programlisting>
1022
1028
CREATE FUNCTION cs_update_referrer_type_proc() RETURNS INTEGER AS '
1023
1029
DECLARE
@@ -1159,9 +1165,9 @@ GET DIAGNOSTICS <replaceable>variable</replaceable> = <replaceable>item</replace
1159
1165
RETURN <replaceable>expression</replaceable>;
1160
1166
</synopsis>
1161
1167
1162
- RETURN with an expression is used to return from a
1163
- <application>PL/pgSQL</> function that does not return a set.
1164
- The function terminates and the value of
1168
+ <command> RETURN</command> with an expression is used to return
1169
+ from a <application>PL/pgSQL</> function that does not return a
1170
+ set. The function terminates and the value of
1165
1171
<replaceable>expression</replaceable> is returned to the caller.
1166
1172
</para>
1167
1173
@@ -1176,22 +1182,24 @@ RETURN <replaceable>expression</replaceable>;
1176
1182
</para>
1177
1183
1178
1184
<para>
1179
- The return value of a function cannot be left undefined. If control
1180
- reaches the end of the top-level block of
1181
- the function without hitting a RETURN statement, a run-time error
1182
- will occur.
1185
+ The return value of a function cannot be left undefined. If
1186
+ control reaches the end of the top-level block of the function
1187
+ without hitting a <command> RETURN</command> statement, a run-time
1188
+ error will occur.
1183
1189
</para>
1184
1190
1185
1191
<para>
1186
1192
When a <application>PL/pgSQL</> function is declared to return
1187
1193
<literal>SETOF</literal> <replaceable>sometype</>, the procedure
1188
1194
to follow is slightly different. In that case, the individual
1189
- items to return are specified in RETURN NEXT commands, and then a
1190
- final RETURN command with no arguments is used to indicate that
1191
- the function has finished executing. RETURN NEXT can be used with
1192
- both scalar and composite data types; in the later case, an
1193
- entire "table" of results will be returned. Functions that use
1194
- RETURN NEXT should be called in the following fashion:
1195
+ items to return are specified in <command>RETURN NEXT</command>
1196
+ commands, and then a final <command>RETURN</command> command with
1197
+ no arguments is used to indicate that the function has finished
1198
+ executing. <command>RETURN NEXT</command> can be used with both
1199
+ scalar and composite data types; in the later case, an entire
1200
+ "table" of results will be returned. Functions that use
1201
+ <command>RETURN NEXT</command> should be called in the following
1202
+ fashion:
1195
1203
1196
1204
<programlisting>
1197
1205
SELECT * FROM some_func();
@@ -1203,19 +1211,19 @@ SELECT * FROM some_func();
1203
1211
RETURN NEXT <replaceable>expression</replaceable>;
1204
1212
</synopsis>
1205
1213
1206
- RETURN NEXT does not actually return from the function; it simply
1207
- saves away the value of the expression (or record or row variable,
1208
- as appropriate for the data type being returned).
1209
- Execution then continues with the next statement in the
1210
- <application>PL/pgSQL</> function. As successive RETURN NEXT
1211
- commands are executed, the result set is built up. A final
1212
- RETURN, which need have no argument, causes control to exit
1213
- the function.
1214
+ <command> RETURN NEXT</command> does not actually return from the
1215
+ function; it simply saves away the value of the expression (or
1216
+ record or row variable, as appropriate for the data type being
1217
+ returned). Execution then continues with the next statement in
1218
+ the <application>PL/pgSQL</> function. As successive
1219
+ <command>RETURN NEXT</command> commands are executed, the result
1220
+ set is built up. A final <command>RETURN</commmand>, which need
1221
+ have no argument, causes control to exit the function.
1214
1222
</para>
1215
1223
1216
1224
<note>
1217
1225
<para>
1218
- The current implementation of RETURN NEXT for
1226
+ The current implementation of <command> RETURN NEXT</command> for
1219
1227
<application>PL/pgSQL</> stores the entire result set before
1220
1228
returning from the function, as discussed above. That means that
1221
1229
if a <application>PL/pgSQL</> function produces a very large result set,
@@ -1586,12 +1594,12 @@ FOR <replaceable>record | row</replaceable> IN EXECUTE <replaceable>text_express
1586
1594
<replaceable>statements</replaceable>
1587
1595
END LOOP;
1588
1596
</synopsis>
1589
- This is like the previous form, except that the source SELECT
1590
- statement is specified as a string expression, which is evaluated
1591
- and re-planned on each entry to the FOR loop. This allows the
1592
- programmer to choose the speed of a pre-planned query or the
1593
- flexibility of a dynamic query, just as with a plain EXECUTE
1594
- statement.
1597
+ This is like the previous form, except that the source
1598
+ <command>SELECT</command> statement is specified as a string
1599
+ expression, which is evaluated and re-planned on each entry to
1600
+ the FOR loop. This allows the programmer to choose the speed of
1601
+ a pre-planned query or the flexibility of a dynamic query, just
1602
+ as with a plain <command>EXECUTE</command> statement.
1595
1603
</para>
1596
1604
1597
1605
<note>
@@ -1700,18 +1708,18 @@ OPEN curs1 FOR SELECT * FROM foo WHERE key = mykey;
1700
1708
<sect3>
1701
1709
<title>OPEN FOR EXECUTE</title>
1702
1710
1703
- <para>
1711
+ <para>
1704
1712
<synopsis>
1705
1713
OPEN <replaceable>unbound-cursor</replaceable> FOR EXECUTE <replaceable class="command">query-string</replaceable>;
1706
1714
</synopsis>
1707
1715
1708
- The cursor variable is opened and given the specified query
1709
- to execute. The cursor cannot be open already, and it must
1710
- have been declared as an unbound cursor (that is, as a simple
1711
- <type>refcursor</> variable). The query is specified as a
1712
- string expression in the same way as in the EXECUTE command.
1713
- As usual, this gives flexibility so the query can vary
1714
- from one run to the next.
1716
+ The cursor variable is opened and given the specified query to
1717
+ execute. The cursor cannot be open already, and it must have been
1718
+ declared as an unbound cursor (that is, as a simple
1719
+ <type>refcursor</> variable). The query is specified as a string
1720
+ expression in the same way as in the <command> EXECUTE</ command>
1721
+ command. As usual, this gives flexibility so the query can vary
1722
+ from one run to the next.
1715
1723
1716
1724
<programlisting>
1717
1725
OPEN curs1 FOR EXECUTE ''SELECT * FROM '' || quote_ident($1);
@@ -1722,19 +1730,18 @@ OPEN curs1 FOR EXECUTE ''SELECT * FROM '' || quote_ident($1);
1722
1730
<sect3>
1723
1731
<title>Opening a bound cursor</title>
1724
1732
1725
- <para>
1733
+ <para>
1726
1734
<synopsis>
1727
1735
OPEN <replaceable>bound-cursor</replaceable> <optional> ( <replaceable>argument_values</replaceable> ) </optional>;
1728
1736
</synopsis>
1729
1737
1730
- This form of OPEN is used to open a cursor variable whose query
1731
- was bound to it when it was declared.
1732
- The cursor cannot be open already. A list of actual argument
1733
- value expressions must appear if and only if the cursor was
1734
- declared to take arguments. These values will be substituted
1735
- in the query.
1736
- The query plan for a bound cursor is always considered
1737
- cacheable --- there is no equivalent of EXECUTE in this case.
1738
+ This form of <command>OPEN</command> is used to open a cursor
1739
+ variable whose query was bound to it when it was declared. The
1740
+ cursor cannot be open already. A list of actual argument value
1741
+ expressions must appear if and only if the cursor was declared to
1742
+ take arguments. These values will be substituted in the query.
1743
+ The query plan for a bound cursor is always considered cacheable
1744
+ --- there is no equivalent of <command>EXECUTE</command> in this case.
1738
1745
1739
1746
<programlisting>
1740
1747
OPEN curs2;
@@ -1771,16 +1778,17 @@ OPEN curs3(42);
1771
1778
<sect3>
1772
1779
<title>FETCH</title>
1773
1780
1774
- <para>
1781
+ <para>
1775
1782
<synopsis>
1776
1783
FETCH <replaceable>cursor</replaceable> INTO <replaceable>target</replaceable>;
1777
1784
</synopsis>
1778
1785
1779
- FETCH retrieves the next row from the cursor into a target,
1780
- which may be a row variable, a record variable, or a comma-separated
1781
- list of simple variables, just like SELECT INTO. As with
1782
- SELECT INTO, the special variable <literal>FOUND</literal> may be
1783
- checked to see whether a row was obtained or not.
1786
+ <command>FETCH</command> retrieves the next row from the
1787
+ cursor into a target, which may be a row variable, a record
1788
+ variable, or a comma-separated list of simple variables, just like
1789
+ <command>SELECT INTO</command>. As with <command>SELECT
1790
+ INTO</command>, the special variable <literal>FOUND</literal> may
1791
+ be checked to see whether a row was obtained or not.
1784
1792
1785
1793
<programlisting>
1786
1794
FETCH curs1 INTO rowvar;
0 commit comments