Learn SQL
-- performance.transactions_acid

Transactions, ACID & Locks in SQL

All-or-nothing writes under concurrency

--concept

A transaction bundles statements atomically: all COMMIT or all ROLLBACK — no partial writes. ACID: Atomicity, Consistency, Isolation, Durability (WAL). Isolation levels trade concurrency for strictness (READ COMMITTED → REPEATABLE READ → SERIALIZABLE; higher aborts more). Writers take row locks; SELECT … FOR UPDATE pessimistically guards read-modify-write; version columns do it optimistically. MVCC lets readers never block writers via row versions + VACUUM cleanup.

--how it works
1

Atomic unit

BEGIN … COMMIT or ROLLBACK — no partial writes. Single statements autocommit; sequences need explicit txn.

2

ACID guarantees

Atomicity, Consistency (constraints hold), Isolation (no partial visibility), Durability (WAL survival).

3

Isolation ladder

READ COMMITTED → REPEATABLE READ → SERIALIZABLE. Higher forbids more anomalies (dirty/non-repeatable/phantom) at concurrency + retry cost.

4

Locks + versions

Pessimistic FOR UPDATE guards invariants; optimistic version-column retries; MVCC versions let readers never block writers; ordered locking avoids deadlock.

--visualization
BEGINLOCK (FOR UPDATE)WRITE ±100COMMITACID: all COMMIT or all ROLLBACK — optimistic version-check vs pessimistic lock
committed in-flight
Stage 1 / 4
BEGIN — snapshot taken. Autocommit off for this sequence.
--example
query.sql
1BEGIN;
2SELECT balance FROM accounts WHERE id IN (1,2) ORDER BY id FOR UPDATE;
3UPDATE accounts SET balance = balance - 100 WHERE id = 1;
4UPDATE accounts SET balance = balance + 100 WHERE id = 2;
5COMMIT;
Result
result
transfer committed — both or neither

Rows locked in id order (deadlock-proof), both updates share one WAL flush, readers never see the half-moved middle. A CHECK (balance >= 0) constraint — not app math — is the final guard.

--variants

Money transfer (classic)

Ordered locks + constraint guard + retry contract.

query.sql
1BEGIN;
2SELECT balance FROM accounts WHERE id IN (1,2) ORDER BY id FOR UPDATE;
3UPDATE accounts SET balance = balance - 100 WHERE id = 1;
4UPDATE accounts SET balance = balance + 100 WHERE id = 2;
5COMMIT;
Result
result
transfer committed — both or neither
1Lock both rows in id order — deadlock-proof.
2CHECK (balance >= 0) is the final guard, not app math.
3Serialization/deadlock errors retry the whole txn.

Optimistic version check

Zero rows means someone moved first.

query.sql
1UPDATE accounts SET balance = 90, version = 6 WHERE id = 1 AND version = 5;
Result
signal
0 rows = conflict → refetch + retry
1Version travels with the read.
2WHERE pins the expected version.
3No locks held across think-time — good for app servers.
--key facts
ACIDatomic, consistent, isolated, durable
Isolation ladderhigher = fewer anomalies, more retries
MVCCversions for readers, VACUUM reclaims dead
--use cases
Transfers, multi-table writes, guarded updates — any all-or-nothing sequence
Contention-light entities (optimistic) vs multi-row invariants (pessimistic)
--under the hood
SERIALIZABLE aborts on conflicts — apps must retry idempotently; snapshot isolation (PG REPEATABLE READ) gives stable reads cheaply via MVCC.
Keep txns short: long holders bloat logs, hold locks, stall VACUUM.

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.