Sub topic 2
Locks Last Until the End of the Transaction
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE, and that you can use BEGIN, COMMIT, and ROLLBACK. You should also be able to connect to PostgreSQL through two concurrent client sessions.
A lock belongs to a transaction
PostgreSQL uses multiversion concurrency control (MVCC), so an ordinary SELECT can often read a row while another transaction changes it. However, changes still need coordination. An UPDATE, DELETE, or locking clause such as FOR UPDATE takes a lock.
PostgreSQL normally keeps that lock until the transaction ends:
COMMITreleases the lock after making the transaction's changes permanent.ROLLBACKreleases the lock and discards the transaction's changes.- Sending another statement does not release the lock.
- Closing a statement does not release the lock.
- In autocommit mode, each statement is usually its own transaction, so the lock is released when that statement finishes.
This lifetime matters because a transaction can accidentally block other sessions while it performs unrelated work.
| Operation | Lock or coordination | Usual release point |
|---|---|---|
| `UPDATE` on a row | Row-level lock | Transaction end |
| `SELECT ... FOR UPDATE` | Row-level lock without changing values | Transaction end |
| `LOCK TABLE` | Table-level lock | Transaction end |
| Ordinary `SELECT` | MVCC snapshot, plus a light table lock | Statement or transaction end, depending on mode |
Observe a row lock with two sessions
Create the table once, then use two separate sessions. Session B waits because Session A has changed the row and has not ended its transaction.
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance numeric NOT NULL
);
INSERT INTO accounts (id, balance)
VALUES (1, 100), (2, 100);
-- Session A
BEGIN;
UPDATE accounts
SET balance = balance - 10
WHERE id = 1;
-- The row lock on id = 1 remains held.
-- Session A can do other work before ending the transaction.
-- Session B
BEGIN;
UPDATE accounts
SET balance = balance + 5
WHERE id = 1;
-- This statement waits for Session A.
-- Session A
COMMIT;
-- Session B now continues, then finishes.
COMMIT;The second UPDATE is not rejected immediately. PostgreSQL places Session B in a lock wait. After Session A commits, Session B can acquire the row lock and continue.
A SELECT ... FOR UPDATE has the same transaction-level lifetime. It reads a row and locks it for a later decision:
BEGIN;
SELECT id, balance
FROM accounts
WHERE id = 1
FOR UPDATE;
-- Check the balance, then perform a conditional change.
UPDATE accounts
SET balance = balance - 10
WHERE id = 1
AND balance >= 10;
COMMIT;The lock prevents another transaction from updating or deleting that row before this transaction finishes. It does not make the row permanently unavailable. The lock exists only for the current transaction.
Savepoints do not extend the transaction
A savepoint is a named point inside a transaction. Rolling back to it can release locks acquired after that savepoint, but it does not end the outer transaction or release locks acquired before the savepoint.
BEGIN;
SELECT id
FROM accounts
WHERE id = 1
FOR UPDATE;
SAVEPOINT after_first_lock;
SELECT id
FROM accounts
WHERE id = 2
FOR UPDATE;
ROLLBACK TO SAVEPOINT after_first_lock;
-- The lock acquired for id = 2 can be released.
-- The lock for id = 1 remains held.
COMMIT;For predictable behavior, design the whole transaction as the unit that owns its locks. Commit promptly when the protected work is complete.
Lock lifetime
Exercise
Session A runs BEGIN and UPDATE, then waits for user input for five minutes before COMMIT. What happens to a conflicting UPDATE in Session B?
Show answer
Session B waits for the row lock until Session A commits or rolls back, unless a timeout or fail-fast option interrupts the wait.