Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6808f1b

Browse files
committed
Support UPDATE/DELETE WHERE CURRENT OF cursor_name, per SQL standard.
Along the way, allow FOR UPDATE in non-WITH-HOLD cursors; there may oncehave been a reason to disallow that, but it seems to work now, and it'sreally rather necessary if you want to select a row via a cursor and thenupdate it in a concurrent-safe fashion.Original patch by Arul Shaji, rather heavily editorialized by Tom Lane.
1 parent85d72f0 commit6808f1b

File tree

30 files changed

+940
-127
lines changed

30 files changed

+940
-127
lines changed

‎doc/src/sgml/ref/declare.sgml

Lines changed: 68 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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 $
33
PostgreSQL documentation
44
-->
55

@@ -27,7 +27,6 @@ PostgreSQL documentation
2727
<synopsis>
2828
DECLARE <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 &mdash; 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+
afterthe cursoris created. In <productname>PostgreSQL</productname>,
74+
this is the default behavior; sothis 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 &mdash; 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>

‎doc/src/sgml/ref/delete.sgml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/delete.sgml,v 1.30 2007/02/01 00:28:19 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/delete.sgml,v 1.31 2007/06/11 01:16:21 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -22,7 +22,7 @@ PostgreSQL documentation
2222
<synopsis>
2323
DELETE FROM [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ [ AS ] <replaceable class="parameter">alias</replaceable> ]
2424
[ USING <replaceable class="PARAMETER">usinglist</replaceable> ]
25-
[ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
25+
[ WHERE <replaceable class="PARAMETER">condition</replaceable>| WHERE CURRENT OF <replaceable class="PARAMETER">cursor_name</replaceable>]
2626
[ RETURNING * | <replaceable class="parameter">output_expression</replaceable> [ AS <replaceable class="parameter">output_name</replaceable> ] [, ...] ]
2727
</synopsis>
2828
</refsynopsisdiv>
@@ -134,9 +134,23 @@ DELETE FROM [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ [ AS ]
134134
<term><replaceable class="parameter">condition</replaceable></term>
135135
<listitem>
136136
<para>
137-
An expression returning a value of type
138-
<type>boolean</type>, which determines the rows that are to be
139-
deleted.
137+
An expression that returns a value of type <type>boolean</type>.
138+
Only rows for which this expression returns <literal>true</>
139+
will be deleted.
140+
</para>
141+
</listitem>
142+
</varlistentry>
143+
144+
<varlistentry>
145+
<term><replaceable class="PARAMETER">cursor_name</replaceable></term>
146+
<listitem>
147+
<para>
148+
The name of the cursor to use in a <literal>WHERE CURRENT OF</>
149+
condition. The row to be deleted is the one most recently fetched
150+
from this cursor. The cursor must be a simple (non-join, non-aggregate)
151+
query on the <command>DELETE</>'s target table.
152+
Note that <literal>WHERE CURRENT OF</> cannot be
153+
specified together with a boolean condition.
140154
</para>
141155
</listitem>
142156
</varlistentry>
@@ -236,6 +250,14 @@ DELETE FROM films;
236250
Delete completed tasks, returning full details of the deleted rows:
237251
<programlisting>
238252
DELETE FROM tasks WHERE status = 'DONE' RETURNING *;
253+
</programlisting>
254+
</para>
255+
256+
<para>
257+
Delete the row of <structname>tasks</> on which the cursor
258+
<literal>c_tasks</> is currently positioned:
259+
<programlisting>
260+
DELETE FROM tasks WHERE CURRENT OF c_tasks;
239261
</programlisting>
240262
</para>
241263
</refsect1>

‎doc/src/sgml/ref/update.sgml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/update.sgml,v 1.43 2007/02/01 00:28:19 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/update.sgml,v 1.44 2007/06/11 01:16:22 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -24,7 +24,7 @@ UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ [ AS ] <rep
2424
SET { <replaceable class="PARAMETER">column</replaceable> = { <replaceable class="PARAMETER">expression</replaceable> | DEFAULT } |
2525
( <replaceable class="PARAMETER">column</replaceable> [, ...] ) = ( { <replaceable class="PARAMETER">expression</replaceable> | DEFAULT } [, ...] ) } [, ...]
2626
[ FROM <replaceable class="PARAMETER">fromlist</replaceable> ]
27-
[ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
27+
[ WHERE <replaceable class="PARAMETER">condition</replaceable>| WHERE CURRENT OF <replaceable class="PARAMETER">cursor_name</replaceable>]
2828
[ RETURNING * | <replaceable class="parameter">output_expression</replaceable> [ AS <replaceable class="parameter">output_name</replaceable> ] [, ...] ]
2929
</synopsis>
3030
</refsynopsisdiv>
@@ -160,6 +160,20 @@ UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ [ AS ] <rep
160160
</listitem>
161161
</varlistentry>
162162

163+
<varlistentry>
164+
<term><replaceable class="PARAMETER">cursor_name</replaceable></term>
165+
<listitem>
166+
<para>
167+
The name of the cursor to use in a <literal>WHERE CURRENT OF</>
168+
condition. The row to be updated is the one most recently fetched
169+
from this cursor. The cursor must be a simple (non-join, non-aggregate)
170+
query on the <command>UPDATE</>'s target table.
171+
Note that <literal>WHERE CURRENT OF</> cannot be
172+
specified together with a boolean condition.
173+
</para>
174+
</listitem>
175+
</varlistentry>
176+
163177
<varlistentry>
164178
<term><replaceable class="PARAMETER">output_expression</replaceable></term>
165179
<listitem>
@@ -309,6 +323,15 @@ UPDATE wines SET stock = stock + 24 WHERE winename = 'Chateau Lafite 2003';
309323
COMMIT;
310324
</programlisting>
311325
</para>
326+
327+
<para>
328+
Change the <structfield>kind</> column of the table
329+
<structname>films</structname> in the row on which the cursor
330+
<literal>c_films</> is currently positioned:
331+
<programlisting>
332+
UPDATE films SET kind = 'Dramatic' WHERE CURRENT OF c_films;
333+
</programlisting>
334+
</para>
312335
</refsect1>
313336

314337
<refsect1>

‎src/backend/executor/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
# Makefile for executor
55
#
66
# IDENTIFICATION
7-
# $PostgreSQL: pgsql/src/backend/executor/Makefile,v 1.25 2007/01/20 17:16:11 petere Exp $
7+
# $PostgreSQL: pgsql/src/backend/executor/Makefile,v 1.26 2007/06/11 01:16:22 tgl Exp $
88
#
99
#-------------------------------------------------------------------------
1010

1111
subdir = src/backend/executor
1212
top_builddir = ../../..
1313
include$(top_builddir)/src/Makefile.global
1414

15-
OBJS = execAmi.o execGrouping.o execJunk.o execMain.o\
15+
OBJS = execAmi.oexecCurrent.oexecGrouping.o execJunk.o execMain.o\
1616
execProcnode.o execQual.o execScan.o execTuples.o\
1717
execUtils.o functions.o instrument.o nodeAppend.o nodeAgg.o\
1818
nodeBitmapAnd.o nodeBitmapOr.o\

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp