Quick revision
The short answer
InnoDB stores each row inside the primary key B-tree, and its secondary indexes store the primary key value, so a secondary lookup walks two B-trees. PostgreSQL stores rows in an unordered heap, and every index, including the primary key, points to a tuple id (page, offset), so a lookup walks one index and then reads one heap page. PostgreSQL's CLUSTER rewrites the table once in index order under an exclusive lock, and that order is not maintained afterwards.
What to remember
In this answer, we compared InnoDB's clustered index, where rows live in the primary key B-tree and secondary indexes hold primary key values, with PostgreSQL's heap, where every index holds tuple ids. A secondary lookup is two tree walks in InnoDB and one walk plus a heap read in PostgreSQL. Primary key range scans are naturally compact in InnoDB and depend on physical correlation in PostgreSQL. CLUSTER only rewrites the table once under an exclusive lock. Updates are in place with undo in InnoDB, and new tuple versions with vacuum and possible HOT in PostgreSQL.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
InnoDB stores each row inside the primary key B-tree, and its secondary indexes store the primary key value, so a secondary lookup walks two B-trees. PostgreSQL stores rows in an unordered heap, and every index, including the primary key, points to a tuple id (page, offset), so a lookup walks one index and then reads one heap page. PostgreSQL's CLUSTER rewrites the table once in index order under an exclusive lock, and that order is not maintained afterwards.
Why this question is asked
MySQL and PostgreSQL both use B-tree indexes, so it is easy to assume they store data the same way. They do not, and the difference decides how we choose primary keys, why some range scans are fast in one engine and slow in the other, and how updates and cleanup work. Interviewers ask this to check that we can reason from the storage model instead of repeating advice that was written for the other engine. In this answer, we will recap the two layouts, follow a secondary index lookup in each, compare range scans, explain what CLUSTER really does, and compare how updates work.
1A Quick Recap of the Two Storage Models
A clustered index, also called an index-organised table, is a table whose rows are stored in the leaf pages of its primary key B-tree. InnoDB works this way: the table is the primary key index. Rows are kept in primary key order, and finding a row by primary key means descending that one tree to the leaf that holds the whole row.
A heap table stores rows wherever there is free space, in no particular order. PostgreSQL works this way. Each row version has an address called a tuple id, or ctid, made of a page number and a slot on that page, such as (1042, 7). Every index, including the primary key index, is a separate B-tree whose entries hold the key and a tuple id.
| InnoDB | PostgreSQL | |
|---|---|---|
| Row storage | leaf pages of the primary key B-tree | heap pages, in no order |
| Primary key index entry | the row itself | key -> tuple id (page, slot) |
| Secondary index entry | key -> primary key value | key -> tuple id (page, slot) |
| Old row versions | undo log | dead tuples in the heap, removed by vacuum |
2Following a Secondary Index Lookup
Suppose we look up an order by its email, WHERE email = 'a@b.c', with a secondary index on email. In InnoDB, the database first descends the email index and finds the entry ('a@b.c', 981). The value 981 is the primary key. It then descends the primary key B-tree to find row 981 and reads it from the leaf. That is two B-tree descents, each touching about three or four pages on a large table.
In PostgreSQL, the database descends the email index and finds ('a@b.c', (1042, 7)). It then reads heap page 1042 directly and takes slot 7. That is one B-tree descent and one page read, and then a visibility check on the tuple.
5 messages in order. Play them, or step through them one at a time.
InnoDB pays a second tree walk; PostgreSQL pays a direct page read and a visibility check.
3Range Scans and Physical Order
Physical order matters most for range scans. In InnoDB, WHERE id BETWEEN 1000 AND 2000 reads neighbouring leaf pages of the primary key tree, so 1,000 rows might sit on 10 to 20 consecutive pages. Rows with close primary keys are always stored together.
In PostgreSQL, the same range scan on the primary key index finds 1,000 tuple ids, but those rows could be on 1,000 different heap pages if the table was filled in a random order. If the rows were inserted in id order and rarely moved, they are well correlated with the index and the scan is cheap. The planner tracks this as the correlation statistic in pg_stats; a value near 1 or -1 means index order matches physical order.
4What PostgreSQL's CLUSTER Really Does
CLUSTER sounds like it turns a PostgreSQL table into a clustered index. It does not. It rewrites the whole table once, sorted by the chosen index, and rebuilds all indexes on it.
So CLUSTER is a one-time reorganisation, not a property of the table. It blocks all access for the duration, which on a large table can be hours, and the order starts to decay with the next insert. Tools such as pg_repack can do a similar reordering mostly online. For data that is naturally appended in order, such as events by time, PostgreSQL keeps good correlation without CLUSTER.
5How Updates Differ
In InnoDB, an update changes the row in place in the clustered index and writes the old version to the undo log, which serves older transactions that still need it and is purged later. Secondary indexes change only if their columns change, because they point to the primary key, which does not move.
In PostgreSQL, an update writes a whole new row version, possibly on another page, and leaves the old one as a dead tuple for vacuum to remove. Because the new version has a new tuple id, every index needs a new entry, unless the update is HOT, heap-only tuple. A HOT update applies when no indexed column changed and the new version fits on the same page; then the indexes keep pointing to the old slot, which links to the new version. This is why fillfactor and unindexed hot columns matter so much in PostgreSQL.
6Follow-up questions
Why is a random UUIDv4 primary key worse in InnoDB than in PostgreSQL?
In InnoDB the primary key decides where the row is physically stored, so random keys insert into random leaf pages all over the clustered index, causing page reads from disk once the tree is bigger than memory, page splits and fragmentation. Every secondary index also stores that 16 or 36 byte key. In PostgreSQL the row goes to the heap wherever there is space, usually at the end, so only the primary key index itself suffers from random inserts, and secondary indexes store a 6 byte tuple id regardless of the key type. Random UUIDs still hurt PostgreSQL's primary key index, but much less.
Does InnoDB have anything like HOT updates?
It does not need them in the same way. Because secondary indexes point to the primary key, not a physical location, an in-place update that does not change indexed columns never touches secondary indexes. The cost InnoDB pays instead is the second tree walk on every secondary lookup and the undo log that must be purged.
7Conclusion
In this answer, we compared InnoDB's clustered index, where rows live in the primary key B-tree and secondary indexes hold primary key values, with PostgreSQL's heap, where every index holds tuple ids. A secondary lookup is two tree walks in InnoDB and one walk plus a heap read in PostgreSQL. Primary key range scans are naturally compact in InnoDB and depend on physical correlation in PostgreSQL. CLUSTER only rewrites the table once under an exclusive lock. Updates are in place with undo in InnoDB, and new tuple versions with vacuum and possible HOT in PostgreSQL.
Open with
InnoDB is an index-organised table. The rows live in the leaf pages of the primary key B-tree, so the table is the primary key index. Secondary indexes store the primary key value, which means a secondary lookup descends the secondary index to get the key and then descends the primary key tree to get the row: two B-tree walks.
Then, if they want more
PostgreSQL stores rows in an unordered heap, and every index, including the primary key, maps the key to a tuple id, a page and slot. A lookup is one index walk and then one heap page read, plus a visibility check.
The consequence for range scans is that InnoDB primary key ranges read neighbouring pages, while in PostgreSQL it depends on whether the physical order happens to correlate with the index, which pg_stats shows as correlation.
CLUSTER in PostgreSQL is often misunderstood: it rewrites the table once in index order under an access exclusive lock, and later inserts and updates do not keep that order. Updates also differ. InnoDB updates in place and keeps old versions in undo, and secondary indexes only change if their columns change.
PostgreSQL writes a new tuple version and needs new entries in every index unless the update qualifies as HOT, so it relies on vacuum and fillfactor.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare InnoDB's clustered index (index-organised table) with PostgreSQL's heap tables....": https://scrivio-interview-library.netlify.app/databases/indexing/042b63ef63/