All Products
Search
Document Center

PolarDB:FOR UPDATE clause

Last Updated:Mar 28, 2026

The FOR UPDATE clause locks rows returned by a SELECT statement, preventing other transactions from modifying or deleting those rows until the current transaction ends.

Syntax

FOR UPDATE [WAIT n | NOWAIT | SKIP LOCKED]

How it works

When a SELECT FOR UPDATE statement runs, it locks each returned row. Any UPDATE, DELETE, or SELECT FOR UPDATE from another transaction on those rows is blocked until the current transaction completes.

If another transaction has already locked one or more of the target rows, SELECT FOR UPDATE waits for that transaction to complete, then locks and returns the updated rows. If the rows were deleted in the meantime, SELECT FOR UPDATE locks and returns no rows.

Locking options

Use locking options to control what happens when SELECT FOR UPDATE encounters rows already locked by another session.

OptionBehavior
WAIT nWaits up to n seconds for locked rows to become available. Use a decimal value to specify fractional seconds (up to 4 decimal places). For example, WAIT 1.5 waits 1.5 seconds.
NOWAITReturns an error immediately if any target row is locked by another session.
SKIP LOCKEDLocks and returns only the rows that are not locked by another session. Rows that are already locked are silently skipped.

Usage notes

  • FOR UPDATE can be used in contexts where the returned rows can be clearly identified by individual table rows, such as aggregation.