FOR UPDATE
constructsupportedQueries & DMLLocks the rows that the query selects until the transaction that contains the query commits or aborts. This clause is supported for use with hybrid tables only, and is useful for transactional workloads in which multiple transactions attempt to access the same rows at the same time. Rows are locked for update in the sense that other transactions cannot write data to these rows until the transaction doing the locking has been fully committed or rolled back. However, other transactions can read the locked rows, and other rows in the same table can be read, updated, or deleted.
Parameters
| Name | Required | Description |
|---|---|---|
NOWAIT | Returns an error if the transaction cannot lock the selected rows immediately. NOWAIT is the default. | |
WAIT wait_time | Specifies the maximum time (in seconds) that the query waits to acquire row-level locks. If the wait time expires, the query returns an error. |
Usage notes
- Because hybrid tables support the READ COMMITTED isolation level, FOR UPDATE clauses do not guarantee read stability. For example, assume that a table T with a single column named ID contains two rows with values 5 and 10.
- The following query is run in transaction T1: Copy code SELECT * FROM T WHERE ID < 20 FOR UPDATE; The query returns the values 5 and 10 and locks those two rows.
- Another transaction, T2, runs the following DELETE operation: Copy code DELETE FROM T WHERE ID = 5; Transaction T2 has to wait until T1 completes (that is, until it commits or rolls back).
- However, a third transaction, T3, can complete the following INSERT operation: Copy code INSERT INTO T VALUES 12;
- A subsequent query in T1 now returns three values (rows), not two: 5, 10, and 12: Copy code SELECT * FROM T WHERE ID < 20;
Parameters
NOWAIT: Returns an error if the transaction cannot lock the selected rows immediately. NOWAIT is the default. WAIT wait_time: Specifies the maximum time (in seconds) that the query waits to acquire row-level locks. If the wait time expires, the query returns an error.
Restrictions
The FOR UPDATE clause: - Cannot be used with AUTOCOMMIT transactions. - Must be the last clause in the SELECT statement. - Cannot be used in a CTAS statement. - Cannot be used inside subqueries. - Cannot select from multiple tables (joins) or set operations. - Cannot be used when the query contains: DISTINCT - Aggregation functions - GROUP BY - HAVING - Sequences