Skip to content
Ameen Abozaid
MID-015

Isolation and locking

  • 7 Lessons
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Read committed
  • repeatable read
  • serializable
  • deadlocks
  • row locks

Learn first

3 h of practice (estimate)

Learning outcomes

  1. Describe the anomalies (dirty read, non-repeatable read, phantom, lost update, write skew) and which isolation levels prevent them in PostgreSQL.
  2. Use row locks (FOR UPDATE, FOR SHARE, SKIP LOCKED) correctly.
  3. Diagnose lock waits and deadlocks from pg_locks and logs.
  4. Handle serialization failures with retries.

Lessons

  1. 01Concurrency anomalies
  2. 02Isolation levels in PostgreSQL
  3. 03Row-level locks
  4. 04SKIP LOCKED job queues
  5. 05Deadlocks
  6. 06Demo: lost update in two terminals
  7. 07Demo: write skew under serializable

Lab and exercises

Lab

MID-015-LAB1 · Last item in stock

Outcomes: 1–4 · Time: ~3 h Reproduce overselling in Shop: 50 threads each try to buy the last 10 units. Fix it three ways: (1) atomic UPDATE ... WHERE qty >= n, (2) SELECT ... FOR UPDATE, (3) SERIALIZABLE with a bounded retry. Compare throughput and error rate. Acceptance: a concurrent test never oversells for all three fixes; a short comparison table of throughput.

Exercises (assessment: anomaly-identification scenarios)

IDOutcomeTask
MID-015-EX011For five short timelines, name the anomaly.
MID-015-EX021Fill a table: which anomalies each PostgreSQL level allows.
MID-015-EX032Write a transaction that reserves one unit using FOR UPDATE; explain what a concurrent session experiences.
MID-015-EX042Implement a job claim query with SKIP LOCKED and run two workers.
MID-015-EX053Create a deadlock with two sessions, read the log message, and fix it with ordering.
MID-015-EX063Find a blocked session and its blocker using pg_locks / pg_stat_activity (or pg_blocking_pids).
MID-015-EX074Write a retry wrapper (max 3 attempts, backoff) for SQLSTATE 40001 and 40P01.
Show solutions
  • EX01 For example: stale re-read = non-repeatable; new rows in a repeated query = phantom; both write from the same read = lost update; two writers on different rows breaking a shared rule = write skew; seeing uncommitted data = dirty read.
  • EX02 READ COMMITTED allows all but dirty; REPEATABLE READ allows only write skew; SERIALIZABLE allows none.
  • EX03 SELECT ... FOR UPDATE; UPDATE ...; COMMIT; the other session blocks on its write or lock until commit.
  • EX04 ... FOR UPDATE SKIP LOCKED LIMIT 1 inside a CTE with UPDATE ... RETURNING; the workers get different jobs.
  • EX05 Two sessions update rows A,B and B,A; the log shows "deadlock detected"; both update in ascending id order.
  • EX06 SELECT pid, pg_blocking_pids(pid), query FROM pg_stat_activity WHERE cardinality(pg_blocking_pids(pid)) > 0;
  • EX07 Loop up to 3 times catching those SQLSTATEs, rerun the whole transaction, sleep with jitter, rethrow at the end.
  • The "retrying without limits" mistake is covered by EX07 and lab fix 3.

Quiz

Outcomes: O1 anomalies and isolation levels · O2 row locks · O3 diagnosing locks and deadlocks · O4 retries.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Two transactions read stock 5 and each write 4. This is a:O1
  2. 2PostgreSQL's default isolation level is:O1
  3. 3Which level prevents write skew in PostgreSQL?O1
  4. 4Does READ COMMITTED prevent a lost update in read-then-write application code?O1
  5. 5SELECT ... FOR UPDATE makes a concurrent FOR UPDATE on the same row:O2
  6. 6SKIP LOCKED is ideal for:O2
  7. 7The simplest oversell-safe stock decrement:O2
  8. 8PostgreSQL resolves a deadlock by:O3
  9. 9Best way to prevent most deadlocks:O3
  10. 10A serialization failure (40001) should be handled by:O4

Practical part

Anomaly-identification scenarios (five timelines to classify and fix) plus exercises MID-015-EX01 to EX07 and the lab. Pass: quiz 8/10, four of five scenarios correct, and the lab concurrency test never overselling.

Common mistakes

Assuming READ COMMITTED prevents lost updates; retrying without limits.

Assessment

Anomaly-identification scenarios.

Career relevance

Data integrity (SRC-014).

Type to search levels, phases, modules and lessons.