the internals round asks the same six things in different words: b-tree vs LSM, MVCC, isolation, WAL and crash recovery, VACUUM, and replication. each has a one-paragraph answer that lands, and a follow-up the interviewer uses to see if you actually know how it works.
this chapter gathers the answers from across the book, sharpens them, and points at the chapter (and the captured trace) behind each.
you know how a system design round drifts into "and how does the database actually do that?" that's where people bluff. the trick is that the questions are few, and each answer is a mechanism you can draw: a tree, a page with two versions on it, two sessions side by side, a log with a pointer.
think of it as six whiteboard drawings you practise until you can do them from memory. every one of them is a real capture somewhere in this book, and they're below, small.
b-tree vs LSMupdate in place, read-optimized vs append and merge, write-optimized
MVCCwriters make versions; readers pick one with a snapshot
isolationwhat anomalies each level allows, and how postgres stops them
WALlog the change, flush the log at commit, write pages later
VACUUMsomething has to remove versions nobody can see, and freeze old xids
replicationship the WAL (physical) or decoded row changes (logical)
the six questions and the one-line core of each answer. the sections below expand them.
a b+tree keeps keys sorted in fixed-size pages, updated in place: a lookup is one root-to-leaf descent, a few pages for millions of keys (b+trees). here's a real one postgres built, 200 keys over six leaves:
child and sibling linkshigh key
real output, postgres 18.6 (PGlite 0.5.12) · real-tree
a real nbtree from b+trees: a root over a row of leaves linked left to right. every lookup is root → one leaf.
an LSM tree buffers writes in memory (a memtable), flushes them as sorted immutable files (SSTables), and merges files in the background (compaction). writes are sequential and cheap; a read may check several files, which bloom filters mostly skip (LSM trees and bloom filters).
postgres never overwrites a row. an UPDATE writes a new version and stamps the old one's xmax; each reader's snapshot decides which version it sees, so readers never wait for writers (an UPDATE is an INSERT, the visibility rules).
byte 0 headerline pointersaccounts · block 0 · 8 KB · a dot is 16 Btuples, byte 8,192
lower 40free 8,072 B, unlitupper 8112
lp
ctid
xmin
xmax
t_ctid
id
owner
balance
redirect → lp 4
(0,2)
758
0
(0,2)
2
bob
100.00
unused
(0,4)
759
0
(0,4)
1
alice
120.00
line pointer 1 is redirect: it points at lp 4, the live end of a HOT chain, so index entries for lp 1 still work.
real postgres 18.6 output · a native server · mvcc/traces/update-twice, after the VACUUM
the page from an UPDATE is an INSERT, after its VACUUM: the two old versions of alice are gone, slot 1 is a redirect to the live version, so the index entry still works.
the same thing in the browser, in one statement. postgres 18 can return both sides of an UPDATE:
sqldb=lab
UPDATE accounts SET balance = balance + 10 WHERE id = 1RETURNING old.ctid AS old_version, new.ctid AS new_version, old.balance AS old_balance, new.balance AS new_balance;
output, from postgres 18.6
UPDATE 1
old_versiontid
new_versiontid
old_balancenumeric
new_balancenumeric
(0,1)
(0,4)
100.00
110.00
(1 row)
two addresses for one row: the old version is still on the page until VACUUM, and the new one is a whole new tuple.
the standard defines levels by the anomalies they allow; postgres implements them with snapshots, and never allows dirty reads (isolation levels: the standard vs postgres). REPEATABLE READ in postgres is snapshot isolation, which still allows write skew:
T1
·
T2
·
1
BEGIN ISOLATION LEVEL REPEATABLE READ
2
SELECT count(*) FROM doctors WHERE on_call→ 2
3
BEGIN ISOLATION LEVEL REPEATABLE READ
4
SELECT count(*) FROM doctors WHERE on_call→ 2
5
UPDATE doctors SET on_call = false WHERE name = 'asha'→ UPDATE 1
6
UPDATE doctors SET on_call = false WHERE name = 'ben'→ UPDATE 1
7
COMMIT
8
COMMIT
9
SELECT name, on_call FROM doctors ORDER BY name→ 2 rows
committed in the database
name
on_call
asha
t
ben
t
asha (T1) wants to go off call, at repeatable read. the rule: at least one doctor stays on call.
step 1/9
real output, a postgres 18.6 server · ws-rr
from write skew and SERIALIZABLE: two on-call doctors at REPEATABLE READ each check that the other is on call, each goes off call, both commit. no lock conflict, no error, and nobody is on call.
every change is logged before the page it changes may reach disk; COMMIT waits only for the log; pages are written later; after a crash, redo replays the log from the last checkpoint (the write-ahead rule, crash recovery). the capstone traces all of it for one INSERT. here's the crash half:
T1
·
T2
·
1
obsCHECKPOINT
2
BEGIN
3
INSERT INTO notes VALUES (3, 'in flight at the crash')→ INSERT 0 1
4
BEGIN
5
INSERT INTO notes VALUES (2, 'committed')→ INSERT 0 1
6
COMMIT
7
serverpg_ctl stop -m immediate -- the server crashes
8
serverpg_ctl start -- crash recovery runs
9
SELECT id, body FROM notes ORDER BY id→ 2 rows
committed in the database
id
body
1
before the checkpoint
a checkpoint writes every dirty page to disk. if we crash now, redo starts from here.
step 1/9
real output, a postgres 18.6 server · crash-recovery
from crash recovery: T1 commits, T2 doesn't, the server is killed, and recovery replays the WAL. T1's row comes back; T2's insert is on the page too, but its xid now counts as aborted.
quick check
postgres crashes mid-transaction. recovery replays the WAL. what happens to that transaction's rows?
physical replication ships the WAL itself: a standby replays it byte for byte, so it's an exact copy of the whole cluster. logical replication decodes the WAL into row changes per table and applies them as SQL on the other side.
hover a stage.
streaming replication, after streaming replication: the same WAL that makes a crash recoverable is what a standby replays. drawn by hand.
keylines
b-tree: sorted pages, in place, read-optimized. LSM: appends and merges, write-optimized.
MVCC: versions with xmin and xmax, snapshots pick one, VACUUM cleans up.
postgres REPEATABLE READ is snapshot isolation: no phantoms, but write skew. SERIALIZABLE adds SSI.
COMMIT waits for the WAL flush only. recovery is redo from the last checkpoint, no undo.