Skip to main content
The SELECT FOR UPDATE statement is used to order transactions by controlling concurrent access to one or more rows of a table. It works by locking the rows returned by a selection query, such that other transactions trying to access those rows are forced to wait for the transaction that locked the rows to finish. These other transactions are effectively put into a queue based on when they tried to read the value of the locked rows. Because this queueing happens during the read operation, the thrashing that would otherwise occur if multiple concurrently executing transactions attempt to SELECT the same data and then UPDATE the results of that selection is prevented. By preventing thrashing, CockroachDB also prevents transaction retries that would otherwise occur due to . As a result, using SELECT FOR UPDATE leads to increased throughput and decreased tail latency for contended operations. Note that using SELECT FOR UPDATE does not completely eliminate the chance of , which use the SQLSTATE error code 40001, and emit error messages with the string restart transaction. These errors can also arise due to . To eliminate the need for application-level retry logic, in addition to SELECT FOR UPDATE your application also needs to use a . CockroachDB does not support the FOR SHARE or FOR KEY SHARE .
By default, CockroachDB uses the SELECT FOR UPDATE locking mechanism during the initial row scan performed in and statement execution. To turn off implicit SELECT FOR UPDATE locking for UPDATE and UPSERT statements, set the enable_implicit_select_for_update to false.

Syntax

The following diagram shows the supported syntax for the optional FOR locking clause of a SELECT statement. for_locking syntax diagram For the full SELECT statement syntax documentation, see .

Parameters

Locking strengths

Locking strength dictates the row-level locking behavior on rows retrieved by a SELECT statement.
ParameterDescription
FOR SHARE/FOR KEY SHAREThis syntax is a no-op, allowed for PostgreSQL compatibility. Specifying FOR SHARE/FOR KEY SHARE does not cause CockroachDB to use shared locks over the rows retrieved by a statement.

Note that CockroachDB always , regardless of the specified locking strength.
FOR UPDATE/FOR NO KEY UPDATELock the rows returned by the statement, such that other transactions trying to access the rows must wait for the transaction to finish.

Note that in CockroachDB, the FOR NO KEY UPDATE locking strength is identical to the FOR UPDATE locking strength.

Wait policies

Wait policies determine how a SELECT FOR UPDATE statement handles conflicts with locks held by other active transactions. By default, SELECT FOR UPDATE queries on rows that are already locked by an active transaction must wait for the transaction to finish.
ParameterDescription
SKIP LOCKEDSkip rows that cannot be immediately locked.
NOWAITReturn an error if a row cannot be locked immediately.
For documentation on all other parameters of a SELECT statement, see .

Required privileges

The user must have the SELECT and UPDATE on the tables used as operands.

Known limitations

Locks acquired using SELECT ... FOR UPDATE are dropped on and . SELECT ... FOR UPDATE locks should be thought of as best-effort, and should not be relied upon for correctness, as they are implemented as fast, in-memory . If a lease transfer or range split/merge occurs on a range held by an unreplicated lock, the lock is dropped, and the following behaviors can occur:
  • The desired ordering of concurrent accesses to one or more rows of a table expressed by your use of SELECT ... FOR UPDATE may not be preserved (that is, a transaction B against some table T that was supposed to wait behind another transaction A operating on T may not wait for transaction A).
  • The transaction that acquired the (now dropped) unreplicated lock may fail to commit, leading to .
Note that is preserved despite this limitation.

Examples

Enforce transaction order when updating the same rows

This example uses SELECT FOR UPDATE to lock a row inside a transaction, forcing other transactions that want to update the same row to wait for the first transaction to complete. The other transactions that want to update the same row are effectively put into a queue based on when they first try to read the value of the row. This example assumes you are running a . First, connect to the running cluster (call this Terminal 1):
Next, create a table and insert some rows:
Next, we’ll start a and lock the row we want to operate on:
Press Enter twice in the to send the statements to be evaluated. This will result in the following output:
Now open another terminal and connect to the database from a second client (call this Terminal 2):
From Terminal 2, start a transaction and try to lock the same row for updates that is already being accessed by the transaction we opened in Terminal 1:
Press Enter twice to send the statements to be evaluated. Because Terminal 1 has already locked this row, the SELECT FOR UPDATE statement from Terminal 2 will appear to “wait”. Back in Terminal 1, update the row and commit the transaction:
Now that the transaction in Terminal 1 has committed, the transaction in Terminal 2 will be “unblocked”, generating the following output, which shows the value left by the transaction in Terminal 1:
The transaction in Terminal 2 can now receive input, so update the row in question again:
Finally, commit the transaction in Terminal 2:

See also