postgres vs InnoDB
a heap vs a clustered index, versions vs undo, gap locks vs SSI. same SQL, different machine.
- address
- everything.deep_dives.postgres_vs_innodb
- part
- part 18, deep dives and interviews
- chapter
- chapter 2 of 10
- reading time
- 10 min
postgres keeps rows in a heap and every index, the primary key included, points at a row's physical address. InnoDB stores rows inside the primary key's b-tree (a clustered index), and secondary indexes point at the primary key value. that one choice decides how updates, old versions and range locks work in each.
same SQL. different machine underneath.
the picture
you know how people say "mysql and postgres are basically the same, they both run SQL"? at the SQL level, mostly. one floor down, they made opposite choices at almost every fork, and you can feel it in production: why one wants short primary keys, why the other needs VACUUM, why one blocks an INSERT the other lets through.
think of two libraries. one stacks books wherever there's shelf space and keeps card catalogues that say "row 4, shelf 2" (postgres: a heap, and indexes holding a ctid). the other keeps the books themselves in call-number order, and its other catalogues say "call number 813.54" so you walk to the main shelves to find it (InnoDB: rows in the primary key's b-tree, and secondary indexes holding the key).
- rows in whatever page had room, in no key order
- └── page 0: (0,1) id 3 · (0,2) id 1 · (0,3) id 2
- key → ctid
- └── 1 → (0,2) · 2 → (0,3) · 3 → (0,1)
- key → ctid, exactly the same kind of entry
- └── 'a' → (0,3) · 'b' → (0,2) · 'c' → (0,1)
- the rows are the leaves, in key order
- └── leaf: id 1 (whole row) · id 2 (whole row) · id 3 (whole row)
- key → primary key value, not an address
- └── 'a' → id 2 · 'b' → id 1 · 'c' → id 3
- no PRIMARY KEY?first UNIQUE NOT NULL index, else a hidden 6-byte row id (GEN_CLUST_INDEX)
a heap vs a clustered index
postgres first. insert three rows out of key order and ask where they went:
CREATE TABLE notes (id int PRIMARY KEY, body text NOT NULL);
CREATE INDEX notes_body ON notes (body);
INSERT INTO notes VALUES (3, 'c'), (1, 'b'), (2, 'a');
SELECT ctid, id, body FROM notes ORDER BY ctid;| ctidtid | idint4 | bodytext |
|---|---|---|
| (0,1) | 3 | c |
| (0,2) | 1 | b |
| (0,3) | 2 | a |
the heap is in arrival order: id 3 is at (0,1). the primary key doesn't decide where a row lives, it's just another index (the files behind a table, what an index actually is). and both indexes hold the same kind of thing, a physical address:
SELECT 'notes_pkey' AS index, itemoffset, ctid FROM bt_page_items('notes_pkey', 1)
UNION ALL
SELECT 'notes_body', itemoffset, ctid FROM bt_page_items('notes_body', 1)
ORDER BY index, itemoffset;| indextext | itemoffsetint2 | ctidtid |
|---|---|---|
| notes_body | 1 | (0,3) |
| notes_body | 2 | (0,2) |
| notes_body | 3 | (0,1) |
| notes_pkey | 1 | (0,2) |
| notes_pkey | 2 | (0,3) |
| notes_pkey | 3 | (0,1) |
in InnoDB, by the manual's "clustered and secondary indexes" section, the table is the primary key's b-tree: the leaf pages hold the whole rows, in key order. with no primary key it uses the first UNIQUE index whose columns are all NOT NULL, and failing that a hidden, monotonically increasing 6-byte row id. every other index is a secondary index, and each of its records holds the indexed columns plus the row's primary key columns.
hover a stage.
hover a stage.
each design wins somewhere. a primary key lookup or range scan in InnoDB lands on the row itself, with neighbouring keys on the same page; in postgres it's an index plus scattered heap pages (unless the table was CLUSTERed recently, and it doesn't stay that way). a secondary lookup in InnoDB costs two descents, and a long primary key makes every secondary index fatter, which is why the manual advises short ones. in postgres, any index gets you to the row in one hop.
the bill comes due on UPDATE. when a postgres row moves to a new address, every index needs an entry for the new address (unless the update is HOT):
UPDATE notes SET body = 'd' WHERE id = 1;
SELECT itemoffset, ctid, data FROM bt_page_items('notes_pkey', 1) ORDER BY itemoffset;| itemoffsetint2 | ctidtid | datatext |
|---|---|---|
| 1 | (0,2) | 01 00 00 00 00 00 00 00 |
| 2 | (0,4) | 01 00 00 00 00 00 00 00 |
| 3 | (0,3) | 02 00 00 00 00 00 00 00 |
| 4 | (0,1) | 03 00 00 00 00 00 00 00 |
two entries for key 1 in the primary key, though id didn't change: one for the old version at (0,2), one for the new one at (0,4). that's write amplification, and it's the price of indexes holding addresses. in InnoDB the same UPDATE changes the row in place in the clustered index and touches only the index on body: the other secondary indexes store id, and id didn't change.
versions vs undo
that new tuple at (0,4) is postgres's MVCC: the old version stays in the heap until VACUUM (an UPDATE is an INSERT). InnoDB updates the row in place and writes the old values to an undo log; a reader that needs the old version rebuilds it from there (MVCC with undo logs has the whole comparison, with the manual's details). the short version:
| postgres | InnoDB | |
|---|---|---|
| row storage | heap, any order | clustered index, primary key order |
| index entries point at | a ctid (page, slot) | the primary key value |
| an UPDATE writes | a new tuple, plus an entry in every index (unless HOT) | the row in place, plus undo, plus entries only in indexes whose columns changed |
| old versions live | in the table | in undo logs |
| cleanup | VACUUM | purge |
| ROLLBACK | nothing to undo: the xid is marked aborted | applies the undo |
| default isolation | READ COMMITTED | REPEATABLE READ |
| phantoms at REPEATABLE READ | prevented by the snapshot | prevented by next-key locks for locking reads; plain reads use the snapshot |
gap locks vs SSI
the last fork is about ranges. "is room 7 free on christmas eve? if so, book it" is a read of a range followed by a write into it, and two transactions doing it at once is the classic write skew.
InnoDB's answer is to lock the range. the manual's "InnoDB locking" section: at REPEATABLE READ (its default), locking reads and index scans take next-key locks, a record lock plus a gap lock on the gap before the record, so nobody can insert into the range you scanned. an INSERT first takes an insert intention lock on its gap, which waits if someone holds a gap lock there. at SERIALIZABLE, plain SELECTs are turned into SELECT ... FOR SHARE when autocommit is off ("transaction isolation levels"). so the second booking transaction waits, and if both took the gap first, one of them gets a deadlock error.
postgres at SERIALIZABLE does something else: serializable snapshot isolation. reads leave SIRead locks, which block nothing. postgres watches for read-write dependencies, and when two of them line up in the dangerous pattern, it cancels a transaction with SQLSTATE 40001. captured on a real server:
-- T1 and T2: BEGIN ISOLATION LEVEL SERIALIZABLE;
-- T1 and T2: SELECT count(*) FROM bookings WHERE room = 7 AND night = '2026-12-24'; -- 0
/* T1 */ INSERT INTO bookings VALUES (5001, 7, '2026-12-24');
/* T2 */ INSERT INTO bookings VALUES (5002, 7, '2026-12-24'); -- ?- T1ExclusiveLock4/4
T1 starts a serializable transaction.
the error's detail line says "canceled on identification as a pivot, during commit attempt". retry T2 and its check sees T1's booking (retrying serialization failures). so the two engines trade the same thing in opposite directions: InnoDB makes you wait and sometimes deadlock, postgres lets you run and sometimes makes you retry. for the booking case specifically, both are beaten by a constraint that can't be raced: postgres's exclusion constraint, which the schema interview builds.
keylines
- postgres: heap plus indexes of addresses. InnoDB: the table is the primary key's b-tree, and secondary indexes hold the key.
- InnoDB secondary lookups descend twice. postgres updates that move a row touch every index.
- old versions: in the postgres heap (VACUUM) vs in InnoDB undo (purge).
- ranges: InnoDB blocks inserts with gap and next-key locks; postgres SERIALIZABLE lets them run and cancels one at commit.
- same isolation level name, different guarantees. read your engine's manual, not the standard.