disks, memory and why pages exist
a random read on SSD vs RAM, and why databases move 8 KB at a time.
- address
- everything.storage.why_pages
- part
- part 12, pages and the buffer pool
- chapter
- chapter 1 of 12
- reading time
- 9 min
memory answers in about a tenth of a microsecond. an SSD answers a random read in tens of microseconds: on the machine this was measured on, hundreds of times slower. but most of an SSD's time goes on the request, not the bytes: reading 8 KB costs the same as 4 KB, and 64 KB only 1.6 times as much.
so databases never read a row. they read a fixed-size block, a page (8 KB in postgres), and every table and index is a file of them. ask for one row, and its whole page comes with it.
the picture
you know how fetching one row by its id is fast, and reading a whole table is slow, even though both are just "reading the table"? the difference isn't the rows. it's how many trips to storage each one makes.
think of a warehouse across town. the drive is what costs you: half an hour there, half an hour back. once you're there, carrying out a box of books takes hardly longer than carrying out one. so nobody drives over for a single book. you bring the box, and if the next book you want happens to be in it, you already have it. the box is a page.
here's how long the drive is, measured on one machine:
two gaps matter. memory against a random SSD read: 122 ns against 82 µs, about 670 times. and a random SSD read against reading the same SSD in order: 82 µs against 2.8 µs per 8 KB. storage loves big, sequential, predictable reads, and punishes small scattered ones. a database's whole storage layer is shaped around those two facts.
the cost is the trip, not the bytes
the same SSD, random reads of different sizes, one at a time:
doubling from 4 KB to 8 KB is free. 64 KB, eight times the data, takes 1.6 times as long. that's the shape of every storage device: a large fixed cost per request (finding the data, the command round trip, on a hard disk the seek), and a small cost per byte after that. so the unit of I/O should be big enough that the fixed cost is shared by a lot of useful data, and small enough that a random lookup doesn't drag in megabytes it won't use. postgres picked 8 KB.
one level deeperhow these were measured
two small C programs, compiled with cc -O2, run on an idle M4 Pro with 24 GB of RAM, macOS, the internal SSD. RAM: chase pointers through a random cycle over 1 GiB (each hop is one dependent load, so nothing can be prefetched), and memcpy 8 KB from random spots of the same buffer. OS cache: pread 8 KB at random aligned offsets of a 2 GiB file that was just written and read back. SSD: pread at random aligned offsets of a 4 GiB file opened with F_NOCACHE and F_RDAHEAD off, written the same way so it was never cached, 20,000 reads for the ladder and 5,000 per size for the sweep, median reported. three runs of the ladder agreed within 4%, including the random 8 KB SSD read, at 80.6, 82.0 and 82.1 µs. the size sweep, a separate program run separately, got 72 µs for the same read: the SSD's own mood varies that much between runs. treat them as one machine's numbers, not a law: a server SSD, a cloud volume and a laptop will all differ, but the ratios hold roughly everywhere.
// the heart of the SSD measurement
int fd = open(path, O_RDONLY);
fcntl(fd, F_NOCACHE, 1); // don't serve from or fill the OS cache
fcntl(fd, F_RDAHEAD, 0); // no read-ahead
for (int i = 0; i < reps; i++) {
double t0 = now();
pread(fd, buf, 8192, (rnd() % blocks) * 8192);
lat[i] = now() - t0;
}
// sort lat, report lat[reps / 2]so, pages
in postgres every table and index is a file cut into 8 KB pages, numbered from 0. a row lives on one page, and the page is the smallest thing that's ever read or written:
SHOW block_size;
SELECT pg_relation_size('notes') AS bytes, pg_relation_size('notes') / 8192 AS pages,
count(*) AS rows
FROM notes;| block_sizetext |
|---|
| 8192 |
| bytesint8 | pagesint8 | rowsint8 |
|---|---|---|
| 40960 | 5 | 400 |
the lab's 400 notes are 5 pages: 40,960 bytes. so how much does reading one of them cost? pick a row:
note 200 lives on page 2. asking for it reads the whole 8 KB page, so 96 other notes (ids 195 to 291) come along for free.
SELECT * FROM notes; -- 400 rows
SELECT * FROM notes WHERE id = 200; -- 1 row, through the indexEXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT * FROM notes;EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT * FROM notes WHERE id = 200;each block starts from a cold copy, and the Buffers: line counts pages: read=5 for all 400 rows, read=2 for one. (dirtied=5 is the scan setting hint bits on pages nobody had read since they were written, and the Planning: reads are the catalog and the index's metapage.) this is why the planner thinks in pages, not rows, and why a query that reads a few percent of a table through an index can lose to just reading the whole thing in order (when postgres uses an index). and it's why everything in this part of the book, from the layout of a page to the cache in front of the disk, is about pages.
why 8 KB, and not 4 or 16
8 KB is a compile-time choice in postgres (--with-blocksize), and almost nobody changes it. it sits between the sizes around it:
SHOW block_size;
SHOW wal_block_size;
SHOW segment_size;| block_sizetext |
|---|
| 8192 |
| wal_block_sizetext |
|---|
| 8192 |
| segment_sizetext |
|---|
| 1GB |
on this mac the SSD reports 4,096-byte blocks and the OS manages memory in 16 KB pages (diskutil info and sysctl hw.pagesize say so), so a postgres page is two device blocks and half an OS page. other engines pick differently: InnoDB's default page is 16 KB (mysql 8.4 manual), sqlite's is 4 KB since version 3.12.0 (sqlite docs). bigger pages mean more rows per read and shallower indexes. smaller pages mean less waste when you only wanted one row, and less to write when one row changes. there's no free lunch, just a choice made decades ago that has held up.
keylines
- a random SSD read is hundreds of times slower than memory: 82 µs against 122 ns on this mac.
- the cost is per request: 8 KB costs what 4 KB costs, 64 KB only 1.6× more.
- so postgres reads and writes 8 KB pages, never single rows.
- reading one row reads its whole page, and an index lookup reads several.
- EXPLAIN's Buffers counts pages. the planner thinks in pages too.