Appendix A: The Logical to Physical Map
the SSTables and sparse indexes part established that the engine speaks only logical offsets and that "the inode/extent map is the logical→physical translation" — then left that map as a black box, because the engine genuinely never looks inside it. This appendix opens the box. It is the one layer directly beneath everything in Parts I and II, and the mechanism it uses to turn an offset into a location is the same mechanism the engine itself uses one layer up.
The map is per-block, not per-byte
So the translation is two lines of arithmetic wrapped around exactly one lookup:
reading byte 9000 of a file, 4 KiB blocks
logical block number LBN = 9000 / 4096 = 2
offset within block = 9000 % 4096 = 808
map(LBN 2) → physical block 158932 ← the only lookup
disk byte = 158932 * 4096 + 808 ← identity from here onEvery byte does have to be accounted for — but not individually. Bytes inside a block keep their relative positions exactly, so the map only has to relocate blocks. A 1 GB file at 4 KiB blocks is 262,144 blocks, not a billion bytes — and if those blocks are contiguous, the map compresses to a single entry (see extents below).
Where the map lives: the inode
Per-file metadata — mode, owner, timestamps, size, link count — lives in an inode (ext2/3/4, XFS, UFS), an MFT record (NTFS), or a directory entry plus a chain (FAT). The block map is stored in that same structure. So "where is the map?" has a precise answer: in the file's own metadata record, on disk, alongside its stat fields.
What makes this fast is that nothing is ever searched for. An inode number is an index into a fixed-stride array, so its disk location is computed:
ext4 — inode number 12 → a byte offset on the device
group = (12 - 1) / inodes_per_group
index = (12 - 1) % inodes_per_group
table_start = block_group_descriptor[group].bg_inode_table
disk offset = table_start * block_size + index * inode_size /* 256 B */And the bootstrap is a hard-coded constant: the ext4 superblock sits at byte offset 1024 of the partition, always. Read it and you learn block_size, inodes_per_group, and where the group descriptors are — from which every other location follows by arithmetic. Fixed offset → computed offsets → no search anywhere in the chain.
- Read the superblock at a fixed offset; learn the geometry. (Done once at mount, then cached.)
- Walk the path: a directory is just a file whose contents are
name → inode numberpairs./var/lib/rocksdb/000123.sstis four such lookups. - Compute the inode's disk offset from its number; read the inode. The block map arrives with it — no extra I/O for small or contiguous files.
- Translate LBN → PBN using that map, and issue the read.
Scheme 1: indirect block trees (ext2/ext3, UFS)
The classic design. The inode carries 15 block pointers, and the last three are indirections:
With 4 KiB blocks and 4-byte pointers, one block holds 1024 pointers — which sets the whole scale:
| Region | Covers | Cumulative file size | Extra reads per lookup |
|---|---|---|---|
| 12 direct | 48 KiB | 48 KiB | 0 — already in the inode |
| single indirect | 4 MiB | ~4 MiB | 1 |
| double indirect | 4 GiB | ~4 GiB | 2 |
| triple indirect | 4 TiB | ~4 TiB | 3 |
Two things are worth extracting from this. First, the interior nodes of the map are ordinary data blocks, allocated from the same free pool as file contents — the map's root is in the inode, its body is out on the disk. Second, a pointer value of 0 means a hole: nothing is allocated, reads return zeros. That is exactly how truncate -s 1T sparse.dat costs no space, and it is the same "absence is a value" trick as a tombstone in the Concurrency, tombstones and compaction part.
Scheme 2: extents — ext4, XFS, NTFS, btrfs
An extent is a run: (logical_start, length, physical_start). One entry describes up to 128 MiB of contiguous file. This is the same insight as the SSTables and sparse indexes part's sparse index — don't store what you can derive — applied to block mapping instead of key lookup.
ext4 — the same 60 bytes of i_block, reinterpreted
struct ext4_extent_header { /* 12 bytes */
__le16 eh_magic, eh_entries, eh_max, eh_depth;
__le32 eh_generation;
};
struct ext4_extent { /* 12 bytes — a LEAF */
__le32 ee_block; /* first LOGICAL block of this run */
__le16 ee_len; /* length, max 32768 blocks = 128 MiB */
__le16 ee_start_hi; /* 48-bit PHYSICAL start */
__le32 ee_start_lo;
};
struct ext4_extent_idx { /* 12 bytes — an INTERIOR node */
__le32 ei_block; /* subtree covers logical ≥ ei_block */
__le32 ei_leaf_lo;
__le16 ei_leaf_hi; /* physical block holding the child */
__le16 ei_unused;
};12 + 4 × 12 = 60 bytes
the header plus four extents fit exactly in the inode's i_block field
So a file of up to four runs — which a sequentially written SSTable almost always is — has its entire map inline in the inode. Zero extra I/O to locate any byte of a 512 MiB file. Past four extents, eh_depth goes above 0 and the leaves move out into their own blocks, with ext4_extent_idx entries above them: a B+tree keyed by logical block number, searched exactly like the B-tree fundamentals part's B-tree.
resolving LBN 5000
binary search the entries on ee_block:
extent[2] = { ee_block = 4096, ee_len = 8192, ee_start = 200704 }
4096 ≤ 5000 < 4096 + 8192 → hit
PBN = 200704 + (5000 - 4096) = 201608XFS uses the same idea with a B+tree of extents throughout. NTFS encodes data runs — varint (length, LCN delta) pairs — inside the $DATA attribute of the file's MFT record, and if the file is tiny enough the attribute is resident: the file's bytes are stored in the MFT record itself and there is no map at all. ext4 has the same trick under the inline_data feature. Small files are not mapped; they are inlined, exactly as the Heap and index part inlines a small value rather than TOASTing it out of line.
Scheme 3: FAT — the instructive counterexample
FAT keeps no per-file map. It keeps one global array, indexed by cluster number, where each slot names the next cluster of whatever file that cluster belongs to:
the File Allocation Table
FAT[c] = next cluster of the file containing c (or EOF, or FREE)
directory entry stores only the FIRST clusterFinding LBN n means walking n links. Random access is O(n) in the file's length, and the table must be held entirely in RAM to be usable at all — a structure whose cost scales with the device rather than with the file being read. It is the Log and hash index part's linked-log problem in a different costume, and it was fixed the same way: replace the chain with a tree.
The in-memory half
The on-disk structures above are only consulted on a miss. Three caches sit in front of them:
| Cache | Keyed by | What it saves |
|---|---|---|
| Page cache | (inode, logical page index) | The whole translation — a hit never consults the map at all. This is the Log and hash index part's "reads can be near-instant." |
| Inode cache | inode number | Re-reading the inode, and with it the inline part of the map. |
| Extent status tree | logical block range | An in-RAM rbtree of recently resolved runs, so repeated lookups don't re-walk the on-disk B+tree. |
The write path adds one more thing worth knowing: delayed allocation. A write() in ext4 or XFS only dirties pages and reserves space; no physical blocks are chosen and no extent is recorded until writeback. That is what lets the allocator see the whole flush at once and hand out one long contiguous run — which is precisely why the SSTables and sparse indexes part's claim that a sequentially written SSTable "lands in contiguous extents" holds in practice rather than merely in principle.
Then it happens again, twice
The filesystem's "physical block" is not physical either. The same logical→physical problem is solved independently at three layers, each unaware of the others:
The SSD's flash translation layer is the interesting one, because it is solving this guide's problem with this guide's answer. NAND cannot be overwritten in place, so every write goes to a fresh page and a mapping table is updated — the drive is running an LSM tree. Its garbage collection is compaction (see Compaction strategies), its write amplification is measured the same way, and the table itself costs roughly 1 GB of DRAM per 1 TB at 4 KiB granularity, persisted across power loss by journaling it into NAND behind a capacitor-backed flush.
Three layers, three private address spaces, three translation tables — and a fourth one above, since a process's virtual memory is the same shape again. Indirection layer, private numbering, translation table is not a filesystem idea; it is the standard answer whenever a stable name must survive a relocatable thing.
What this changes in Parts I and II
- the SSTables and sparse indexes part's black box is a B+tree. "The extent map, cached in the inode" means: a header plus up to four runs inline, growing into an on-disk B+tree keyed by logical block. When the guide says the map is cheap, this is why — not because lookups are free, but because a sequentially written immutable file collapses to a handful of entries.
- Q3's answer, mechanically. Engine block size cannot affect this machinery because the machinery is indexed by 4 KiB filesystem blocks and the engine's request is always split into those first. A 64 KiB read of a contiguous SSTable resolves through one extent entry, exactly as a 4 KiB read does.
- the Heap and index part's "Postgres never sees block #X." The allocator's arbitrary choice is recorded as an extent entry; the page number stays a pure count. Two layers of stable naming over relocatable storage, stacked.
- Why append-only is rewarded twice. Sequential immutable writes produce few extents at the filesystem layer and low write amplification at the FTL layer. In-place random updates fragment the map above and trigger garbage collection below — the same bill, arriving at two layers the engine cannot see.
- Guarantees: Given a file descriptor and any byte offset, the filesystem resolves it to a device location in bounded work — usually zero extra I/O, at worst a short tree walk — while keeping the offset itself stable across defragmentation, copy-on-write and wear levelling.
- Does not guarantee: That nearby logical offsets are nearby physically, that the map stays small under random writes, or that "physical block" means anything physical — two further translation layers sit below it, each free to move the data again without telling anyone above.