What this page is for

Intent: the full picture behind the quick Transactions page. Same sections, same order, with the wrong version’s tables, exact retry rules, slow-work math and the harder questions.
Read the quick page first. Each section here starts where the quick one stops.
Time: about 20 minutes.

Words you need first

Everything from the quick page (transaction, atomic, isolation, read-modify-write, lost update, snapshot, READ COMMITTED, REPEATABLE READ, 40001, Drizzle), plus:

  • ACID: the four transaction promises. Atomicity (all or nothing), Consistency (rules such as “stock is never negative” hold), Isolation (concurrent work is hidden from each other, to a degree set by the level), Durability (a commit survives a crash).
  • Autocommit: without BEGIN, most clients run each statement as its own transaction.
  • MVCC (multi-version concurrency control): Postgres keeps several versions of a row, so plain reads do not wait for writers.
  • SQLSTATE: the five-character error code Postgres sends. 40001 is a serialization failure, 40P01 a deadlock.
  • Deadlock: two transactions each hold a lock the other needs. The database aborts one.
  • Connection pool: a set of open database connections the app reuses. A waiting request holds one.
  • Write skew: two transactions each check a rule correctly, write different rows, and together break the rule.

This page uses the same shoe shop: products(id, stock), stock = pairs left.

Why it exists

Three promises to keep: group the statements (atomicity), read a consistent view (isolation), and never silently drop one write (locks, a failed write, or one atomic statement). The app breaks the third most often, because the app does the read, the arithmetic and the write.

The picture

Lost update: A and B both read stock 5, A writes 4, B writes 4 from its stale 5, so A's stock update is lost. Below: FOR UPDATE makes B wait, then read 0

The top half is the bug. The bottom half is the lock that stops it.

How it works, with an example

Simply: Checkout is a few steps: read the stock, check it, save the order, and write the new stock. It is like a shop counter for one buyer. With many buyers at once, the steps can mix, and a sale can go wrong.

What to know: The gap between a read and a write is where races happen. Transactions, isolation, and locks keep the stock count right.

The checkout code is the same in every section: read the stock, check it, insert an order, write the new stock. Each section below asks one question about that code, and gives the table at each step.

The most common mistake

BEGIN and COMMIT don't keep others out

A transaction only promises that its own steps happen together. Other transactions keep running, reading and writing. Three slips follow:

  • Outside calls are not rolled back. ROLLBACK undoes database changes only. An email or a charge made inside the callback stays done. Send those after COMMIT, or use an outbox table and an idempotency key.
  • No BEGIN, no transaction. Without BEGIN, each statement autocommits. Statements that must go together need one transaction.
  • Retry scope. After a failure, the whole transaction runs again from BEGIN, not one query.

Transactions: all or nothing

Simply: A transaction is a group of steps that all save together, or none do. It is like a bank transfer: money leaves one account and arrives in the other, or neither happens. The database calls this atomic: all or nothing.

What to know: Statements between BEGIN and COMMIT are saved together, or on ROLLBACK none are. A transaction does not stop other transactions from reading or writing at the same time. That is isolation’s job.

Example:

-- SQL
BEGIN;
INSERT INTO shoe_orders (product_id) VALUES (1);
UPDATE products SET stock = stock - 1 WHERE id = 1;
COMMIT;
// Drizzle
await db.transaction(async (tx) => {
  await tx.insert(shoeOrders).values({ productId: 1 });
  await tx.update(products).set({ stock: sql`${products.stock} - 1` }).where(eq(products.id, 1));
});

Drizzle sends BEGIN before the callback and COMMIT after it. If the callback throws, it sends ROLLBACK.

stepshoe_ordersstock
before BEGIN0 rows1
after INSERT (only this transaction sees it)1 row1
after UPDATE (still only this transaction)1 row0
after COMMIT (everyone sees it)1 row0

When the UPDATE fails and the transaction rolls back:

stepshoe_ordersstock
UPDATE fails, ROLLBACK0 rows1

The wrong version, with no transaction (autocommit):

stepshoe_ordersstock
INSERT (autocommit: saved at once)1 row1
UPDATE fails1 row1

Caption: the order exists, but the stock was never reduced. The shelf still shows a pair that is already sold.

Edge cases: every query in the callback needs await, or it can run after the transaction has ended.

Why this, not that:

  • Atomicity: all steps succeed or none do. It works on one connection’s work and does not stop others.
  • Isolation: controls what other connections see. It is the separate job.

Trigger card

When I see several writes that must go together, I use one transaction, because it makes them all-or-nothing. It does not keep other transactions out.

Lost update

Simply: Two cashiers read “5 pairs left” from one notebook page. Each sells one and writes the new count. The second write covers the first, so one sale vanishes. This is a lost update.

What to know: A lost update happens when two read-modify-writes touch the same row, and the later write, built on a stale value, overwrites the earlier one. READ COMMITTED does not block it, and no error is raised. A row lock, one atomic UPDATE, or a retry under REPEATABLE READ stops it.

Example: both buyers run the same read-modify-write.

-- SQL, run by A and by B
SELECT stock FROM products WHERE id = 1;     -- app gets 5
UPDATE products SET stock = $1 WHERE id = 1; -- $1 = 5 - 1, computed in the app
// Drizzle, run by A and by B
const [p] = await db.select().from(products).where(eq(products.id, 1));
await db.update(products).set({ stock: p.stock - 1 }).where(eq(products.id, 1));

Inside a transaction under READ COMMITTED, the result is the same: each statement sees the newest row, but the app keeps its old number.

timeArowB
t2SELECT, gets 55
t35SELECT, gets 5
t5UPDATE to 4, COMMIT4
t64UPDATE to 4 (5 - 1 from its copy), COMMIT

Caption: final stock 4. Two pairs were sold, so it should be 3. The stale value is B’s copy, read at t3.

The wrong version with stock = 1 (oversell):

timeArowBorders
t2SELECT, gets 1, passes check10
t31SELECT, gets 1, passes check0
t5INSERT order, UPDATE to 0, COMMIT01
t60INSERT order, UPDATE to 0, COMMIT2

Caption: stock 0 and two orders. The check was right when each buyer read it, and wrong by the time they wrote.

Edge case: saving a whole object writes every column. Update only the columns you changed, or another request’s change to a different column gets reverted.

Why this, not that:

  • The earlier write is lost: A’s sale.
  • The later write survives: B’s, built on a stale 5. Last writer wins.
  • One buyer alone never loses an update. The race needs two.

Trigger card

When I see a SELECT of a value, then an UPDATE of the same row with a number computed in app code, I suspect a lost update. READ COMMITTED does not block that.

Isolation levels and retry

Simply: Isolation decides what a running transaction may see of the others. Under READ COMMITTED, each statement sees the latest saved (committed) data. Under REPEATABLE READ, the transaction keeps one snapshot, a photo of the data taken at its first query. If it tries to write a row that changed after that photo, the database refuses with error 40001, and the app must retry the whole transaction.

What to know: READ COMMITTED (the Postgres default) lets each statement see the newest committed data, so a lost update passes silently. REPEATABLE READ takes one snapshot at the first query, and a write that conflicts with a later change fails with 40001. The fix is to retry the whole transaction from BEGIN.

  • READ COMMITTED (default): each statement sees the latest committed data. Nothing re-checks the earlier decision, so the lost update happens silently.
  • REPEATABLE READ: one snapshot, taken at the first query (not at BEGIN). A write to a row that changed after that snapshot fails with 40001. The fix is to retry the whole transaction.

Example: the checkout under REPEATABLE READ with a retry loop.

-- SQL, run by A and by B
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT stock FROM products WHERE id = 1;     -- snapshot: 5
UPDATE products SET stock = $1 WHERE id = 1; -- $1 = 5 - 1
COMMIT;
// Drizzle
async function buyOne(productId: number) {
  for (let attempt = 1; ; attempt++) {
    try {
      return await db.transaction(async (tx) => {
        const [p] = await tx.select().from(products).where(eq(products.id, productId));
        if (p.stock < 1) throw new Error("sold out");
        await tx.update(products).set({ stock: p.stock - 1 }).where(eq(products.id, productId));
      }, { isolationLevel: "repeatable read" });
    } catch (err) {
      const e = err as { code?: string; cause?: { code?: string } };
      const code = e.code ?? e.cause?.code;
      if ((code !== "40001" && code !== "40P01") || attempt >= 5) throw err;
    }
  }
}

The “sold out” error is not 40001, so the loop rethrows it at once.

timeArowB
t2BEGIN, snapshot, reads 55
t35BEGIN, snapshot, reads 5
t5UPDATE to 4, COMMIT (fine)4
t64UPDATE: ERROR 40001, ROLLBACK
t7retry from BEGIN: new snapshot reads 4, writes 3, COMMIT3

Caption: B’s write is refused, not silently lost. The retry takes a new snapshot and writes 3.

With stock = 2, both buyers succeed after one retry:

timeArowB
t2BEGIN, snapshot, reads 22
t32BEGIN, snapshot, reads 2
t5UPDATE to 1, COMMIT1
t61UPDATE: ERROR 40001, ROLLBACK
t71retry: reads 1, UPDATE to 0, COMMIT

Result: stock 0, two orders, no oversell. A third buyer’s retry would read 0, fail the check, and get “sold out”. The customer never sees 40001.

Details:

  • Where it comes from. Postgres raises 40001 on the UPDATE that touches a row changed after the snapshot. The transaction is then aborted until ROLLBACK.
  • Client libraries. node-postgres puts the code on the error. Newer Drizzle versions may wrap it and keep the original in cause, so the helper checks both.
  • Which codes to retry. 40001 always, and 40P01 (deadlock), since both can succeed on the next try. A SQL typo, a unique-key clash or “sold out” fails the same way every time, so do not retry it.

Why this, not that:

  • READ COMMITTED (default): each statement sees the newest data. The lost update is silent, and no retry is needed.
  • REPEATABLE READ: a conflict raises 40001. Retry the whole transaction from BEGIN.

Two one-line extras:

  • SERIALIZABLE is the strictest level. It also raises 40001, needs the same retry loop, and also catches write skew.
  • Write skew, in one line: two on-call doctors each check that the other is still on call, then each goes off call. Nobody is on call. REPEATABLE READ allows this; SERIALIZABLE catches it.

MySQL, in one line: MySQL InnoDB’s REPEATABLE READ does not stop this lost update, so “does REPEATABLE READ prevent lost updates?” depends on the database.

Trigger card

When I see error 40001, the transaction tried to write over data that changed after its snapshot. I retry the whole transaction from BEGIN, not the last query.

FOR UPDATE vs SERIALIZABLE and retry

Simply: There are two ways to stop a lost update. The pessimistic way locks the row first with SELECT … FOR UPDATE, so the second buyer waits until the first one finishes. The optimistic way takes no lock. The conflicting write fails, and the app retries it. Pick the lock for a busy row, and the retry when conflicts are rare.

What to know: SELECT … FOR UPDATE takes a row lock that lasts until COMMIT or ROLLBACK, so other writers and other FOR UPDATE calls wait. Under REPEATABLE READ or SERIALIZABLE, a conflict shows up at write time as 40001, and the loop retries.

  • Pessimistic: lock first. SELECT ... FOR UPDATE locks the row until COMMIT, and the second buyer waits at the shelf.
  • Optimistic: no lock. A conflict is found at write time (40001 under REPEATABLE READ or SERIALIZABLE), and the loop retries.

Example:

-- SQL
BEGIN;
SELECT stock FROM products WHERE id = 1 FOR UPDATE; -- row lock until COMMIT
-- app checks stock >= 1 here, then:
UPDATE products SET stock = stock - 1 WHERE id = 1;
COMMIT;
// Drizzle
await db.transaction(async (tx) => {
  const [p] = await tx.select().from(products).where(eq(products.id, 1)).for("update");
  if (p.stock < 1) throw new Error("sold out");
  await tx.update(products).set({ stock: p.stock - 1 }).where(eq(products.id, 1));
});
timeArowB
t2SELECT … FOR UPDATE, gets 1, holds the lock1
t31SELECT … FOR UPDATE: waits
t5UPDATE to 0, COMMIT (lock released)0
t60lock granted, reads 0, “sold out”

Caption: B waits, then reads 0. No oversell.

Details on the lock:

  • Who it blocks. UPDATE, DELETE and other SELECT ... FOR UPDATE on that row wait. A plain SELECT is not blocked, because MVCC gives it the old version.
  • What B reads. Under READ COMMITTED, B’s query returns the newest committed version once the lock is free. That is why B reads 0.
  • Under REPEATABLE READ, a FOR UPDATE on a row that changed after the snapshot fails with 40001 instead.
  • NOWAIT. FOR UPDATE NOWAIT fails at once instead of waiting.

The single-statement option, with no lock code and no retry:

UPDATE products SET stock = stock - 1 WHERE id = 1 AND stock >= 1;
-- check that exactly 1 row changed; 0 rows means sold out

Read, check and write are one statement, so there is no gap for another buyer.

Optimistic with a version column, when the change is more than one counter:

-- read the row and its version (say 7), compute the new stock in the app, then:
UPDATE products SET stock = $1, version = version + 1 WHERE id = $2 AND version = 7;
-- 0 rows changed: someone else won first, so read again and retry

Why this, not that:

  • FOR UPDATE: waiters queue, one attempt, no retry loop. Costs wait time and locks. Pick it for a hot row, many buyers at once (a flash sale).
  • Retry (REPEATABLE READ or SERIALIZABLE): nobody waits, but a conflict costs wasted work and a capped retry loop. Pick it when conflicts are rare.

Trigger card

When I see SELECT ... FOR UPDATE, other writers queue behind it until COMMIT or ROLLBACK. Plain reads are not blocked.

timebuyer 1 (holds the lock)buyers 2 to 50
0 sFOR UPDATE, starts the 3 s payment callwaiting for the lock
3 sCOMMIT, lock releasedbuyer 2 gets it, starts its 3 s call
147 s(done long ago)buyer 50 gets the lock, last in line

Buyer 50 waits about 49 x 3 seconds, around 2.5 minutes. Most request timeouts are far shorter, so those requests fail. Each waiting request holds a connection, so the pool runs dry and other endpoints fail too. The fix: reserve the stock in a short transaction, call the payment outside any lock, then confirm or release the reservation in another short transaction.

Say it in an interview

Start with the 60-second answer from the quick page. If the interviewer goes deeper, add one of these:

  • Isolation detail: “Under REPEATABLE READ the snapshot is taken at the first query, not at BEGIN. A write to a row changed after that snapshot fails with 40001, and the whole transaction is retried.”
  • Locking detail: “FOR UPDATE blocks writers and other FOR UPDATE calls, not plain reads. Waiters hold connections, so keep the locked part short.”
  • Simplest fix: “For one counter, one atomic UPDATE with stock >= 1 in the WHERE, then check the rows changed. No gap, no retry.”

Questions interviewers ask

The eight from the quick page, plus these harder ones. Tap a question to see the answer.

Mental map

Transactions mental map

The same map as the quick page. Each branch is a section above.

Watch

The Software Mentor, YouTube: row locks and SELECT FOR UPDATE in PostgreSQL. The title covers the same topic as the last section.

Next topic: Joins.