transaction
FOR UPDATE
Acquires a row lock conflicting with other row-lock modes and writes on selected rows; ordinary snapshot reads may proceed. Hold the transaction open to protect subsequent work. Target: PostgreSQL 17. Example is a connection-specific schedule, not a single executable script.
Formal Syntax & Structure
sql syntaxsql
SELECT ... FOR UPDATEWorking Example
Example Snippetsql
-- Fresh fixture: CREATE TABLE t(id INT PRIMARY KEY, n INT NOT NULL);-- INSERT INTO t VALUES (1,10),(2,10);-- Use two independent connections A and B; setup committed first.-- A: BEGIN;-- A: SELECT * FROM t WHERE id=1 FOR UPDATE;-- B: BEGIN;-- B: SELECT * FROM t WHERE id=1 FOR UPDATE NOWAIT;-- Roll back any open transactions after observation.