FOR UPDATE [WAIT n|NOWAIT|SKIP LOCKED]
FOR UPDATE clause causes the rows retrieved by the
SELECT statement to be locked. This prevents a row from being modified or deleted by other
transactions until the current transaction ends. All transactions that attempt to
SELECT FOR UPDATE command on a selected row are blocked until the current transaction ends. If an
SELECT FOR UPDATE command from another transaction has already locked a selected row or rows,
SELECT FOR UPDATE waits for the previous transaction to complete. Then, SELECT FOR UPDATE locks and
returns the updated rows. If the rows were deleted, SELECT FOR UPDATE locks and returns
FOR UPDATE cannot be used in contexts where returned rows cannot be clearly identified with
individual table rows.
You can use
FOR UPDATE options to specify locking preferences.
- Include the
WAIT nkeywords to specify the number of seconds or fractional seconds that the
SELECTstatement will wait for a row locked by another session. Use a decimal form to specify fractional seconds. For example,
WAIT 1.5instructs the server to wait one and a half seconds. You can specify a maximum of four digits to the right of the decimal point.
- Include the
NOWAITkeyword to immediately report an error if a row cannot be locked by the current session.
SKIP LOCKEDto instruct the server to lock rows if possible, and skip rows that are already locked by another session.