11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/ref/declare.sgml,v 1.40 2007/01/31 23:26:03 momjian Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/ref/declare.sgml,v 1.41 2007/06/11 01:16:21 tgl Exp $
33PostgreSQL documentation
44-->
55
@@ -27,7 +27,6 @@ PostgreSQL documentation
2727<synopsis>
2828DECLARE <replaceable class="parameter">name</replaceable> [ BINARY ] [ INSENSITIVE ] [ [ NO ] SCROLL ]
2929 CURSOR [ { WITH | WITHOUT } HOLD ] FOR <replaceable class="parameter">query</replaceable>
30- [ FOR { READ ONLY | UPDATE [ OF <replaceable class="parameter">column</replaceable> [, ...] ] } ]
3130</synopsis>
3231 </refsynopsisdiv>
3332
@@ -37,50 +36,10 @@ DECLARE <replaceable class="parameter">name</replaceable> [ BINARY ] [ INSENSITI
3736 <para>
3837 <command>DECLARE</command> allows a user to create cursors, which
3938 can be used to retrieve
40- a small number of rows at a time out of a larger query. Cursors can
41- return data either in text or in binary format using
39+ a small number of rows at a time out of a larger query.
40+ After the cursor is created, rows are fetched from it using
4241 <xref linkend="sql-fetch" endterm="sql-fetch-title">.
4342 </para>
44-
45- <para>
46- Normal cursors return data in text format, the same as a
47- <command>SELECT</> would produce. Since data is stored natively in
48- binary format, the system must do a conversion to produce the text
49- format. Once the information comes back in text form, the client
50- application might need to convert it to a binary format to manipulate
51- it. In addition, data in the text format is often larger in size
52- than in the binary format. Binary cursors return the data in a
53- binary representation that might be more easily manipulated.
54- Nevertheless, if you intend to display the data as text anyway,
55- retrieving it in text form will
56- save you some effort on the client side.
57- </para>
58-
59- <para>
60- As an example, if a query returns a value of one from an integer column,
61- you would get a string of <literal>1</> with a default cursor
62- whereas with a binary cursor you would get
63- a 4-byte field containing the internal representation of the value
64- (in big-endian byte order).
65- </para>
66-
67- <para>
68- Binary cursors should be used carefully. Many applications,
69- including <application>psql</application>, are not prepared to
70- handle binary cursors and expect data to come back in the text
71- format.
72- </para>
73-
74- <note>
75- <para>
76- When the client application uses the <quote>extended query</> protocol
77- to issue a <command>FETCH</> command, the Bind protocol message
78- specifies whether data is to be retrieved in text or binary format.
79- This choice overrides the way that the cursor is defined. The concept
80- of a binary cursor as such is thus obsolete when using extended query
81- protocol — any cursor can be treated as either text or binary.
82- </para>
83- </note>
8443 </refsect1>
8544
8645 <refsect1>
@@ -110,10 +69,10 @@ DECLARE <replaceable class="parameter">name</replaceable> [ BINARY ] [ INSENSITI
11069 <listitem>
11170 <para>
11271 Indicates that data retrieved from the cursor should be
113- unaffected by updates to thetables underlying the cursorwhile
114- the cursorexists . In <productname>PostgreSQL</productname>,
115- all cursors are insensitive; this key word currently has no
116- effect and ispresent for compatibility with the SQL standard.
72+ unaffected by updates to thetable(s) underlying the cursorthat occur
73+ after the cursoris created . In <productname>PostgreSQL</productname>,
74+ this is the default behavior; so this key word has no
75+ effect and isonly accepted for compatibility with the SQL standard.
11776 </para>
11877 </listitem>
11978 </varlistentry>
@@ -163,34 +122,6 @@ DECLARE <replaceable class="parameter">name</replaceable> [ BINARY ] [ INSENSITI
163122 </para>
164123 </listitem>
165124 </varlistentry>
166-
167- <varlistentry>
168- <term><literal>FOR READ ONLY</literal></term>
169- <term><literal>FOR UPDATE</literal></term>
170- <listitem>
171- <para>
172- <literal>FOR READ ONLY</literal> indicates that the cursor will
173- be used in a read-only mode. <literal>FOR UPDATE</literal>
174- indicates that the cursor will be used to update tables. Since
175- cursor updates are not currently supported in
176- <productname>PostgreSQL</productname>, specifying <literal>FOR
177- UPDATE</literal> will cause an error message and specifying
178- <literal>FOR READ ONLY</literal> has no effect.
179- </para>
180- </listitem>
181- </varlistentry>
182-
183- <varlistentry>
184- <term><replaceable class="parameter">column</replaceable></term>
185- <listitem>
186- <para>
187- Column(s) to be updated by the cursor. Since cursor updates are
188- not currently supported in
189- <productname>PostgreSQL</productname>, the <literal>FOR
190- UPDATE</literal> clause provokes an error message.
191- </para>
192- </listitem>
193- </varlistentry>
194125 </variablelist>
195126
196127 <para>
@@ -203,6 +134,38 @@ DECLARE <replaceable class="parameter">name</replaceable> [ BINARY ] [ INSENSITI
203134 <refsect1 id="sql-declare-notes">
204135 <title id="sql-declare-notes-title">Notes</title>
205136
137+ <para>
138+ Normal cursors return data in text format, the same as a
139+ <command>SELECT</> would produce. The <literal>BINARY</> option
140+ specifies that the cursor should return data in binary format.
141+ This reduces conversion effort for both the server and client,
142+ at the cost of more programmer effort to deal with platform-dependent
143+ binary data formats.
144+ As an example, if a query returns a value of one from an integer column,
145+ you would get a string of <literal>1</> with a default cursor,
146+ whereas with a binary cursor you would get
147+ a 4-byte field containing the internal representation of the value
148+ (in big-endian byte order).
149+ </para>
150+
151+ <para>
152+ Binary cursors should be used carefully. Many applications,
153+ including <application>psql</application>, are not prepared to
154+ handle binary cursors and expect data to come back in the text
155+ format.
156+ </para>
157+
158+ <note>
159+ <para>
160+ When the client application uses the <quote>extended query</> protocol
161+ to issue a <command>FETCH</> command, the Bind protocol message
162+ specifies whether data is to be retrieved in text or binary format.
163+ This choice overrides the way that the cursor is defined. The concept
164+ of a binary cursor as such is thus obsolete when using extended query
165+ protocol — any cursor can be treated as either text or binary.
166+ </para>
167+ </note>
168+
206169 <para>
207170 Unless <literal>WITH HOLD</literal> is specified, the cursor
208171 created by this command can only be used within the current
@@ -232,6 +195,11 @@ DECLARE <replaceable class="parameter">name</replaceable> [ BINARY ] [ INSENSITI
232195 transactions.
233196 </para>
234197
198+ <para>
199+ <literal>WITH HOLD</literal> may not be specified when the query
200+ includes <literal>FOR UPDATE</> or <literal>FOR SHARE</>.
201+ </para>
202+
235203 <para>
236204 The <literal>SCROLL</> option should be specified when defining a
237205 cursor that will be used to fetch backwards. This is required by
@@ -245,6 +213,23 @@ DECLARE <replaceable class="parameter">name</replaceable> [ BINARY ] [ INSENSITI
245213 specified, then backward fetches are disallowed in any case.
246214 </para>
247215
216+ <para>
217+ If the cursor's query includes <literal>FOR UPDATE</> or <literal>FOR
218+ SHARE</>, then returned rows are locked at the time they are first
219+ fetched, in the same way as for a regular
220+ <xref linkend="sql-select" endterm="sql-select-title"> command with
221+ these options.
222+ In addition, the returned rows will be the most up-to-date versions;
223+ therefore these options provide the equivalent of what the SQL standard
224+ calls a <quote>sensitive cursor</>. It is often wise to use <literal>FOR
225+ UPDATE</> if the cursor is intended to be used with <command>UPDATE
226+ ... WHERE CURRENT OF</> or <command>DELETE ... WHERE CURRENT OF</>,
227+ since this will prevent other sessions from changing the rows between
228+ the time they are fetched and the time they are updated. Without
229+ <literal>FOR UPDATE</>, a subsequent <literal>WHERE CURRENT OF</> command
230+ will have no effect if the row was changed meanwhile.
231+ </para>
232+
248233 <para>
249234 The SQL standard only makes provisions for cursors in embedded
250235 <acronym>SQL</acronym>. The <productname>PostgreSQL</productname>
@@ -280,14 +265,16 @@ DECLARE liahona CURSOR FOR SELECT * FROM films;
280265 <title>Compatibility</title>
281266
282267 <para>
283- The SQL standard allows cursors only in embedded
284- <acronym>SQL</acronym> and in modules. <productname>PostgreSQL</>
285- permits cursors to be used interactively.
268+ The SQL standard specifies that by default, cursors are sensitive to
269+ concurrent updates of the underlying data. In
270+ <productname>PostgreSQL</productname>, cursors are insensitive by default,
271+ and can be made sensitive by specifying <literal>FOR UPDATE</>.
286272 </para>
287273
288274 <para>
289- The SQL standard allows cursors to update table data. All
290- <productname>PostgreSQL</> cursors are read only.
275+ The SQL standard allows cursors only in embedded
276+ <acronym>SQL</acronym> and in modules. <productname>PostgreSQL</>
277+ permits cursors to be used interactively.
291278 </para>
292279
293280 <para>