1
1
<!--
2
- $PostgreSQL: pgsql/doc/src/sgml/ref/select.sgml,v 1.127 2009/10/27 17:11:18 tgl Exp $
2
+ $PostgreSQL: pgsql/doc/src/sgml/ref/select.sgml,v 1.128 2009/10/28 14:55:37 tgl Exp $
3
3
PostgreSQL documentation
4
4
-->
5
5
@@ -1092,22 +1092,12 @@ FOR SHARE [ OF <replaceable class="parameter">table_name</replaceable> [, ...] ]
1092
1092
has already locked a selected row or rows, <command>SELECT FOR
1093
1093
UPDATE</command> will wait for the other transaction to complete,
1094
1094
and will then lock and return the updated row (or no row, if the
1095
- row was deleted). For further discussion see <xref
1095
+ row was deleted). Within a <literal>SERIALIZABLE</> transaction,
1096
+ however, an error will be thrown if a row to be locked has changed
1097
+ since the transaction started. For further discussion see <xref
1096
1098
linkend="mvcc">.
1097
1099
</para>
1098
1100
1099
- <para>
1100
- To prevent the operation from waiting for other transactions to commit,
1101
- use the <literal>NOWAIT</> option. <command>SELECT FOR UPDATE
1102
- NOWAIT</command> reports an error, rather than waiting, if a selected row
1103
- cannot be locked immediately. Note that <literal>NOWAIT</> applies only
1104
- to the row-level lock(s) — the required <literal>ROW SHARE</literal>
1105
- table-level lock is still taken in the ordinary way (see
1106
- <xref linkend="mvcc">). You can use the <literal>NOWAIT</> option of
1107
- <xref linkend="sql-lock" endterm="sql-lock-title">
1108
- if you need to acquire the table-level lock without waiting.
1109
- </para>
1110
-
1111
1101
<para>
1112
1102
<literal>FOR SHARE</literal> behaves similarly, except that it
1113
1103
acquires a shared rather than exclusive lock on each retrieved
@@ -1117,13 +1107,26 @@ FOR SHARE [ OF <replaceable class="parameter">table_name</replaceable> [, ...] ]
1117
1107
from performing <command>SELECT FOR SHARE</command>.
1118
1108
</para>
1119
1109
1110
+ <para>
1111
+ To prevent the operation from waiting for other transactions to commit,
1112
+ use the <literal>NOWAIT</> option. With <literal>NOWAIT</>, the statement
1113
+ reports an error, rather than waiting, if a selected row
1114
+ cannot be locked immediately. Note that <literal>NOWAIT</> applies only
1115
+ to the row-level lock(s) — the required <literal>ROW SHARE</literal>
1116
+ table-level lock is still taken in the ordinary way (see
1117
+ <xref linkend="mvcc">). You can use
1118
+ <xref linkend="sql-lock" endterm="sql-lock-title">
1119
+ with the <literal>NOWAIT</> option first,
1120
+ if you need to acquire the table-level lock without waiting.
1121
+ </para>
1122
+
1120
1123
<para>
1121
1124
If specific tables are named in <literal>FOR UPDATE</literal>
1122
1125
or <literal>FOR SHARE</literal>,
1123
1126
then only rows coming from those tables are locked; any other
1124
1127
tables used in the <command>SELECT</command> are simply read as
1125
1128
usual. A <literal>FOR UPDATE</literal> or <literal>FOR SHARE</literal>
1126
- clause without a table list affects all tables used in the command .
1129
+ clause without a table list affects all tables used in the statement .
1127
1130
If <literal>FOR UPDATE</literal> or <literal>FOR SHARE</literal> is
1128
1131
applied to a view or sub-query, it affects all tables used in
1129
1132
the view or sub-query.
@@ -1151,6 +1154,36 @@ FOR SHARE [ OF <replaceable class="parameter">table_name</replaceable> [, ...] ]
1151
1154
individual table rows; for example they cannot be used with aggregation.
1152
1155
</para>
1153
1156
1157
+ <para>
1158
+ When <literal>FOR UPDATE</literal> or <literal>FOR SHARE</literal>
1159
+ appears at the top level of a <command>SELECT</> query, the rows that
1160
+ are locked are exactly those that are returned by the query; in the
1161
+ case of a join query, the rows locked are those that contribute to
1162
+ returned join rows. In addition, rows that satisfied the query
1163
+ conditions as of the query snapshot will be locked, although they
1164
+ will not be returned if they have since been updated to not satisfy
1165
+ the query conditions. If a <literal>LIMIT</> is used, locking stops
1166
+ once enough rows have been returned to satisfy the limit (but note that
1167
+ rows skipped over by <literal>OFFSET</> will get locked). Similarly,
1168
+ if <literal>FOR UPDATE</literal> or <literal>FOR SHARE</literal>
1169
+ is used in a cursor's query, only rows actually fetched or stepped past
1170
+ by the cursor will be locked.
1171
+ </para>
1172
+
1173
+ <para>
1174
+ When <literal>FOR UPDATE</literal> or <literal>FOR SHARE</literal>
1175
+ appears in a sub-<command>SELECT</>, the rows locked are those
1176
+ returned to the outer query by the sub-query. This might involve
1177
+ fewer rows than inspection of the sub-query alone would suggest,
1178
+ since conditions from the outer query might be used to optimize
1179
+ execution of the sub-query. For example,
1180
+ <programlisting>
1181
+ SELECT * FROM (SELECT * FROM mytable FOR UPDATE) ss WHERE col1 = 5;
1182
+ </programlisting>
1183
+ will lock only rows having <literal>col1 = 5</>, even though that
1184
+ condition is not textually within the sub-query.
1185
+ </para>
1186
+
1154
1187
<caution>
1155
1188
<para>
1156
1189
Avoid locking a row and then modifying it within a later savepoint or
@@ -1177,30 +1210,26 @@ ROLLBACK TO s;
1177
1210
1178
1211
<caution>
1179
1212
<para>
1180
- It is possible for a <command>SELECT</> command using both
1181
- <literal>LIMIT</literal> and <literal>FOR UPDATE/SHARE</literal>
1182
- clauses to return fewer rows than specified by <literal>LIMIT</literal>.
1183
- This is because <literal>LIMIT</> is applied first. The command
1184
- selects the specified number of rows,
1185
- but might then block trying to obtain a lock on one or more of them.
1186
- Once the <literal>SELECT</> unblocks, the row might have been deleted
1187
- or updated so that it does not meet the query <literal>WHERE</> condition
1188
- anymore, in which case it will not be returned.
1189
- </para>
1190
- </caution>
1191
-
1192
- <caution>
1193
- <para>
1194
- Similarly, it is possible for a <command>SELECT</> command
1195
- using <literal>ORDER BY</literal> and <literal>FOR
1196
- UPDATE/SHARE</literal> to return rows out of order. This is
1197
- because <literal>ORDER BY</> is applied first. The command
1198
- orders the result, but might then block trying to obtain a lock
1199
- on one or more of the rows. Once the <literal>SELECT</>
1200
- unblocks, one of the ordered columns might have been modified
1201
- and be returned out of order. A workaround is to perform
1202
- <command>SELECT ... FOR UPDATE/SHARE</> and then <command>SELECT
1203
- ... ORDER BY</>.
1213
+ It is possible for a <command>SELECT</> command using <literal>ORDER
1214
+ BY</literal> and <literal>FOR UPDATE/SHARE</literal> to return rows out of
1215
+ order. This is because <literal>ORDER BY</> is applied first.
1216
+ The command sorts the result, but might then block trying to obtain a lock
1217
+ on one or more of the rows. Once the <literal>SELECT</> unblocks, some
1218
+ of the ordering column values might have been modified, leading to those
1219
+ rows appearing to be out of order (though they are in order in terms
1220
+ of the original column values). This can be worked around at need by
1221
+ placing the <literal>FOR UPDATE/SHARE</literal> clause in a sub-query,
1222
+ for example
1223
+ <programlisting>
1224
+ SELECT * FROM (SELECT * FROM mytable FOR UPDATE) ss ORDER BY column1;
1225
+ </programlisting>
1226
+ Note that this will result in locking all rows of <structname>mytable</>,
1227
+ whereas <literal>FOR UPDATE</> at the top level would lock only the
1228
+ actually returned rows. This can make for a significant performance
1229
+ difference, particularly if the <literal>ORDER BY</> is combined with
1230
+ <literal>LIMIT</> or other restrictions. So this technique is recommended
1231
+ only if concurrent updates of the ordering columns are expected and a
1232
+ strictly sorted result is required.
1204
1233
</para>
1205
1234
</caution>
1206
1235
</refsect2>
@@ -1541,15 +1570,28 @@ SELECT distributors.* WHERE distributors.name = 'Westward';
1541
1570
used by <productname>MySQL</productname>. The SQL:2008 standard
1542
1571
has introduced the clauses <literal>OFFSET ... FETCH {FIRST|NEXT}
1543
1572
...</literal> for the same functionality, as shown above
1544
- in <xref linkend="sql-limit" endterm="sql-limit-title">, and this
1573
+ in <xref linkend="sql-limit" endterm="sql-limit-title">. This
1545
1574
syntax is also used by <productname>IBM DB2</productname>.
1546
1575
(Applications written for <productname>Oracle</productname>
1547
1576
frequently use a workaround involving the automatically
1548
- generated <literal>rownum</literal> column, not available in
1577
+ generated <literal>rownum</literal> column, which is not available in
1549
1578
PostgreSQL, to implement the effects of these clauses.)
1550
1579
</para>
1551
1580
</refsect2>
1552
1581
1582
+ <refsect2>
1583
+ <title><literal>FOR UPDATE</> and <literal>FOR SHARE</></title>
1584
+
1585
+ <para>
1586
+ Although <literal>FOR UPDATE</> appears in the SQL standard, the
1587
+ standard allows it only as an option of <command>DECLARE CURSOR</>.
1588
+ <productname>PostgreSQL</productname> allows it in any <command>SELECT</>
1589
+ query as well as in sub-<command>SELECT</>s, but this is an extension.
1590
+ The <literal>FOR SHARE</> variant, and the <literal>NOWAIT</> option,
1591
+ do not appear in the standard.
1592
+ </para>
1593
+ </refsect2>
1594
+
1553
1595
<refsect2>
1554
1596
<title>Nonstandard Clauses</title>
1555
1597
0 commit comments