Sub topic 10
Concurrent updates, row locks and serialization failures
Assume you know basic SQL and transactions, including BEGIN, COMMIT, and ROLLBACK, and can use two psql sessions. This chapter explains what happens when transactions update the same row.
Concurrent updates wait on row locks
An UPDATE changes a row version and takes a row-level lock on the target row. A second transaction that tries to update the same row must wait until the first transaction commits or rolls back.
The second transaction does not read a half-written row. After the first transaction commits, PostgreSQL checks the row again and applies the second update to the newest visible version.
At Read Committed, each statement gets a fresh snapshot. Therefore, the second UPDATE can continue after waiting. Its WHERE condition is checked against the updated row. If the condition no longer matches, PostgreSQL updates zero rows.
Observe the wait
Prepare a shared table, then run the two sessions in the order shown.
CREATE TABLE IF NOT EXISTS mvcc_accounts (
id integer PRIMARY KEY,
balance integer NOT NULL
);
INSERT INTO mvcc_accounts (id, balance)
VALUES (1, 100)
ON CONFLICT (id) DO UPDATE SET balance = EXCLUDED.balance;
-- Session A
BEGIN;
UPDATE mvcc_accounts
SET balance = balance + 10
WHERE id = 1;
-- Keep this transaction open.
-- Session B
BEGIN;
UPDATE mvcc_accounts
SET balance = balance - 5
WHERE id = 1;
-- This statement waits.
-- Session A
COMMIT;
-- Session B continues, then:
COMMIT;
SELECT * FROM mvcc_accounts;The final balance is 105. Session B used the version produced by Session A.
| Situation | Result |
|---|---|
| First updater commits | Second updater continues with the newest row version |
| First updater rolls back | Second updater can update the previously visible version |
| Predicate no longer matches | Second `UPDATE` affects zero rows |
| Deadlock cycle exists | PostgreSQL aborts one transaction with a deadlock error |
Repeatable Read and serialization failures
Repeatable Read keeps one transaction snapshot instead of taking a new snapshot for every statement. If another transaction changes a row after your snapshot and commits, your later attempt to update that row cannot safely use the old snapshot. PostgreSQL aborts the statement with a serialization failure.
-- Session A
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM mvcc_accounts WHERE id = 1;
-- Session B
UPDATE mvcc_accounts
SET balance = balance + 10
WHERE id = 1;
COMMIT;
-- Session A
UPDATE mvcc_accounts
SET balance = balance - 5
WHERE id = 1;
-- ERROR: could not serialize access due to concurrent update
ROLLBACK;Applications should catch serialization failures, roll back the failed transaction, wait briefly, and retry the complete transaction. Retrying only the failed statement is unsafe because the transaction's snapshot remains invalid.
Serializable isolation adds stronger protection. PostgreSQL uses Serializable Snapshot Isolation to detect dangerous read-write dependency patterns. Even transactions that do not update the same row can receive a serialization_failure if their combined actions could produce a result impossible in a serial order.
Exercise
Two Read Committed transactions both run UPDATE accounts SET balance = balance - 10 WHERE id = 1. What normally happens?
Show answer
One transaction obtains the row lock. The other waits, then updates the committed version and completes, unless its predicate no longer matches or a deadlock or other error occurs.