Skip to content

Lesson 3 of 9 · 22m

Locks, Transactions, and MVCC in Production

How row locks, table locks, snapshots, and isolation levels interact — and how to diagnose the everyday production deadlock without guessing.

Most lock incidents look mysterious only because people inspect the victim instead of the dependency graph. PostgreSQL is doing exactly what you asked: two transactions each hold something the other needs. The database resolves that dispute by shooting one transaction.

Your job is to shorten lock lifetimes, make access order predictable, and read the evidence before it disappears.

Row locks are cheap until queueing begins

BEGIN;
SELECT balance FROM accounts WHERE id = 42 FOR UPDATE;
UPDATE accounts SET balance = balance - 100 WHERE id = 42;
COMMIT;

SELECT ... FOR UPDATE is explicit pessimistic locking. It is correct for money movement and inventory claims, but dangerous when the transaction then waits on application logic, network calls, or a browser session. A row lock held for ten seconds can become an outage if every checkout touches the same row.

Keep the critical section short: read inputs, open the transaction, lock rows in deterministic order, update, commit, then do network I/O.

Table locks are not all equal

Ordinary reads take ACCESS SHARE. ACCESS EXCLUSIVE blocks almost everything and is used by operations such as DROP TABLE, TRUNCATE, many ALTER TABLE forms, and VACUUM FULL.

The dangerous case is a migration waiting for ACCESS EXCLUSIVE behind a long query. Even if the migration would last one second, every later query queues behind it.

SELECT blocked.pid AS blocked_pid,
       blocker.pid AS blocker_pid,
       blocker.query AS blocker_query
FROM pg_locks blocked_locks
JOIN pg_stat_activity blocked ON blocked.pid = blocked_locks.pid
JOIN pg_locks blocker_locks
  ON blocker_locks.locktype = blocked_locks.locktype
 AND blocker_locks.relation = blocked_locks.relation
 AND blocker_locks.pid <> blocked_locks.pid
JOIN pg_stat_activity blocker ON blocker.pid = blocker_locks.pid
WHERE NOT blocked_locks.granted AND blocker_locks.granted;

Deadlocks are cycles, and Postgres logs the cycle

ERROR: deadlock detected
DETAIL: Process 18234 waits for ShareLock on transaction 91821; blocked by process 18235.
Process 18235 waits for ShareLock on transaction 91820; blocked by process 18234.

If transaction A updates rows in order (1, 2) and transaction B updates (2, 1), retries merely replay the collision. Make ordering consistent or reduce transaction scope. Retry logic is still required, but it is not the design fix.

Isolation changes truth, not just performance

READ COMMITTED gives each statement a fresh snapshot. REPEATABLE READ gives the whole transaction one snapshot. SERIALIZABLE tracks dangerous dependencies and can abort valid-looking application logic with serialization failures.

Stronger isolation is not automatically safer; it changes failure modes. With SERIALIZABLE, bounded idempotent retries are part of correctness.

Field notes

  • idle in transaction often means locks are held while application code does unrelated work.
  • Set lock_timeout for migrations so they fail fast instead of creating a queue.
  • Set statement_timeout deliberately; forever-running application mistakes are operational incidents.
  • pg_stat_activity.wait_event_type = 'Lock' identifies waiters; pg_locks identifies blockers.
  • Deadlock retry logic must be bounded and idempotent or it becomes a retry amplifier.

Was this lesson helpful?

Sign in to rate