your query
depth 00 of 10
one line. you say what you want, never how to get it. that's the deal SQL makes, and everything below is postgres keeping its side of it.
read: your first queryname -------------- Ada Lovelace (1 row)
a free, visual book about SQL and the database underneath it. from your first SELECT to the bytes on disk, on a real postgres running in your browser.
or scroll, and follow one query all the way down.
a b-tree drawn as a field of dots: a root page, three internal pages and six leaf pages joined by links, with a heap page of rows underneath. the path of the last lookup, id 42, runs in amber from the root to one leaf entry, and a pointer goes from that entry to one green row on the heap page. a few rows on the heap page are hollow red rings: dead tuples.
heap: tid (0,13), the row comes back
what actually happens when you run SELECT name FROM customers WHERE id = 42. every artefact below is real output from postgres 18.6, made by the command printed above it.
depth 00 of 10
one line. you say what you want, never how to get it. that's the deal SQL makes, and everything below is postgres keeping its side of it.
read: your first queryname -------------- Ada Lovelace (1 row)
depth 01 of 10
characters become tokens, tokens become a tree. the parser knows grammar and nothing else: it has no idea whether customers exists. misspell FROM and watch where it actually complains.
word | catdesc --------+------------ from | reserved name | unreserved select | reserved where | reserved (4 rows)
=> SELECT name FORM customers WHERE id = 42; ERROR: syntax error at or near "customers" LINE 1: SELECT name FORM customers WHERE id = 42; ^
name is unreserved, so it can be a column. FORM became an alias for it (AS is optional), so the parser only got lost one word later. the chips are this site's tokenizer; the rest is postgres.
depth 02 of 10
now names become numbers. customers is relation 16385, name is column 2, of type 25 (text). this is the step that says relation "custmers" does not exist.
LOG: parse tree:the catalog says the same: oid | attnum | atttypid -------+--------+---------- | | (1 row)
depth 03 of 10
there are many ways to fetch one row. the planner prices each in made-up units and keeps the cheapest: walk the primary key, read one heap page. reading every row would cost about 6x more.
read: the cost modeldepth 04 of 10
the plan is a tree of iterators. the top asks for a row, the index scan asks the index, the index hands back an address. one next() returns Ada, the next one returns nothing, and the query is over.
→ next() → (Ada Lovelace) next() → end of scan
depth 05 of 10
the index is a b+tree on id. the root says which leaf to visit, and the leaf holds the key 42 next to an address: (0,42), page 0, slot 42 of the table. that address is a ctid.
root page 3, level 1 off child keys from 1 → 1 -∞ 2 → 2 367 3 → 4 733 4 → 5 1099 5 → 6 1465 6 → 7 1831
42 sorts before every separator, so the walk takes the first child. 2a is 42 in hex, stored little-endian.
depth 06 of 10
postgres never reads straight from disk into your query. every page goes through a shared cache first. cold, the query read 3 pages. run it again and it hit the same 3. same query, no I/O.
read: the buffer poolcold Buffers: shared read=3 again Buffers: shared hit=3
rel | block | usage | dirty ----------------+-------+-------+------- customers | 0 | 2 | f customers_pkey | 1 | 2 | f customers_pkey | 3 | 2 | f (3 rows)
depth 07 of 10
every table is a stack of 8 KB pages. slot 42 is a 4-byte line pointer near the top of page 0, pointing at its tuple further down: a header that says who wrote it, then Ada.
read: the 8 KB pagelp 42 lp_off 4648 lp_len 80 t_hoff 24 t_xmin t_xmax t_ctid (0,42)
t_xmax 752 isn't a delete. XMAX_LOCK_ONLY: it's a key-share lock, left when the load's foreign key checks looked Ada up. XMAX_INVALID: nobody deleted her.
depth 08 of 10
and at the bottom, a file: base/5/16385. page 0 is its first 8,192 bytes. the first eight are the LSN of the last WAL record that touched it, and inside Ada's tuple sits 2a 00 00 00, the 42 from your WHERE clause.
the tuple: 80 bytes at byte 4648 000 008 016 024 2a 00 00 00 1b 41 64 61 032 20 4c 6f 76 65 6c 61 63 040 65 21 61 64 61 40 65 78 048 61 6d 70 6c 65 2e 63 6f 056 6d 0f 4c 6f 6e 64 6f 6e 064 07 47 42 00 00 00 00 00 072 00 51 bc 96 a7 b1 02 00 000 016 2a 00 00 00 1b 41 64 61 032 20 4c 6f 76 65 6c 61 63 65 21 61 64 61 40 65 78 048 61 6d 70 6c 65 2e 63 6f 6d 0f 4c 6f 6e 64 6f 6e 064 07 47 42 00 00 00 00 00 00 51 bc 96 a7 b1 02 00
are the tuple header (xmin, xmax, ctid, flags), then 2a 00 00 00 is id 42 and Ada Lovelace follows. this is the page in memory, which is the page on disk after a checkpoint, except the checksum, which is stamped on the way out.
depth 09 of 10
change the row and the first thing written isn't the table, it's a log record. postgres appends to the write-ahead log, flushes it at COMMIT, and writes the page later. the log is the truth. the table catches up.
read: WAL records and LSNsstart_lsn rmgr record_type length fpi Heap LOCK 8219 8160 Heap UPDATE 8586 8500 Btree INSERT_LEAF 7453 7400 Btree INSERT_LEAF 5581 5528 Transaction COMMIT 34 0
the new version didn't fit on page 0, so it went to page 23: the old tuple is locked, the UPDATE is logged, each of the two indexes gets an entry, then the COMMIT. each page's first change since the last checkpoint carries a full page image, so one changed row logged 29 KB.
depth 10 of 10
the UPDATE didn't change Ada's row. it wrote a new version and stamped the old one with its transaction id in xmax. anyone already reading still sees the old one. once nobody can, VACUUM frees the slot.
read: an UPDATE is an INSERTthe old version's t_ctid points at the new one, on page 23. once xmax 801 committed and no snapshot could still want the old one, it was dead. VACUUM removed it and the index entries pointing at it, and slot 42 is free again.
name -------------- Ada Lovelace (1 row)
18 parts, 198 chapters. read it in order, or jump to whatever you're stuck on. your progress stays in your browser.
what a database actually is, the real postgres running in this page, and your first query.
SELECT, WHERE, NULL, ORDER BY and GROUP BY. everything you can ask of a single table.
putting tables back together. every join, semi and anti joins, subqueries, set operations and CTEs.
the queries that make spreadsheets jealous. window functions, grouping sets and recursive CTEs.
INSERT, UPDATE, DELETE, upserts, MERGE, bulk loads, and code that runs inside the database.
numbers, text, time, json, arrays and ranges. picking the type is half the schema.
keys, constraints, normal forms, and the patterns that keep data honest as it grows.
what an index is for, which queries it can help, and what it costs on every write.
parse, plan, execute. EXPLAIN, scans, joins, sorts, and the iterator that pulls one row at a time.
how postgres guesses row counts, prices every plan and picks the cheapest. and how it gets that wrong.
ACID, the anomalies, the isolation levels, and what postgres really gives you at each one.
where rows physically live. files, 8 KB pages, tuple headers, TOAST, and the cache in front of the disk.
b+trees page by page, splits under concurrency, and how hash, GIN, GiST, BRIN and LSM trees are built.
why an UPDATE writes a new row, who can see which version, and the vacuum that cleans up after everyone.
serializability theory, two-phase locking, postgres's lock manager, and the SSI that catches write skew.
write-ahead logging, LSNs, checkpoints, crash recovery, backups and replication.
pools, partitions, replicas, shards, and what changes when the data no longer fits on one machine.
one row traced end to end, other engines compared, the interview questions, and a toy database you build yourself.
what every page here promises.