Learn SQL
-- performance.locking_deadlock_forupdate

Locks, FOR UPDATE & Deadlocks in SQL

Ordered locking + retry — pessimism with discipline

--concept

Writers take row locks; DDL takes strong table locks; SELECT … FOR UPDATE locks chosen rows for the txn (pessimistic read-modify-write guard). NOWAIT/SKIP LOCKED avoid blocking (queue-skipping jobs). Deadlock: two txns want each other’s locks — engine detects cycle, aborts one (retryable); prevention is consistent lock order (parents before children, id order) + short txns.

--how it works
1

Row locks on write

UPDATE/DELETE lock touched rows till COMMIT/ROLLBACK — readers (MVCC) unaffected.

2

FOR UPDATE guards

SELECT … FOR UPDATE claims rows for staged multi-statement invariants.

3

SKIP LOCKED queues

Workers claim without blocking — NOWAIT errors instead of waiting.

4

Order + retry doctrine

Parents before children, id order everywhere; short txns; retry victims whole.

--visualization
Both paths lock 1→2 — engine never picks a victim
patternrisk
lock 1 then 2no cycle
Stage 1 / 1
SKIP LOCKED queues without blocking. Short txns + retry make pessimism safe.
--example
query.sql
1BEGIN;
2SELECT balance FROM accounts WHERE id IN (1,2) ORDER BY id FOR UPDATE;
Result
patternrisk
ordered locksno cycle possible
random orderdeadlock risk

Both transfer directions lock 1 then 2 — never 2-then-1 vs 1-then-2. The cycle cannot form; the engine never must choose a victim.

--variants

Ordered transfer locks

Cycle impossible by construction.

query.sql
1BEGIN;
2SELECT balance FROM accounts WHERE id IN (1,2) ORDER BY id FOR UPDATE;
Result
ordereffect
lock 1 then 2both paths agree
1Opposite direction uses same order.
2No 1→2 vs 2→1 crossing.
3Engine never picks a victim.

Queue claim without blocking

Concurrent workers skip claimed rows.

query.sql
1SELECT * FROM jobs WHERE status = 'queued' ORDER BY id FOR UPDATE SKIP LOCKED LIMIT 1;
Result
workerclaimed
worker Ajob 101
worker Bjob 102
1A locks 101; B skips it to 102.
2No waiting, no double-claim.
3NOWAIT twin would error instead of skipping.
--key facts
FOR UPDATEpessimistic invariant guard
SKIP LOCKEDqueue-claim without blocking
Order + retrydeadlock doctrine
--use cases
Money/inventory invariants across statements
Job queues with concurrent claimers
--under the hood
Row locks live in memory + WAL — long holders bloat and block DDL; keep guarded sections tight.
Deadlock detector runs on lock waits — victim abort is retryable by design.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.