Syntax

FOR UPDATE [WAIT n|NOWAIT|SKIP LOCKED]

Description

The 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 run the UPDATE, DELETE, or SELECT FOR UPDATE command on a selected row are blocked until the current transaction ends. If an UPDATE, DELETE, or 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 no rows.

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 n keywords to specify the number of seconds or fractional seconds that the SELECT statement will wait for a row locked by another session. Use a decimal form to specify fractional seconds. For example, WAIT 1.5 instructs 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 NOWAIT keyword to immediately report an error if a row cannot be locked by the current session.
  • Include SKIP LOCKED to instruct the server to lock rows if possible, and skip rows that are already locked by another session.