Lesson 1 of 9 · 24m
The Storage Engine Mental Model: Pages, Buffers, and Files
Understand why databases read pages instead of rows, how buffer pools hide disk latency, and why storage layout shapes every performance symptom.
The first time a production database ran out of memory, the DBA did not tune a query. He looked at the buffer pool hit ratio, noticed that a reporting scan had evicted the hot working set, and moved the report to a replica. That is the level where databases make sense: not tables and rows, but pages, memory, files, and the cost of moving bytes between them.
Rows are logical; pages are real
Applications ask for rows. Storage engines read and write fixed-size pages, often 4 KiB, 8 KiB, or 16 KiB depending on the engine. PostgreSQL defaults to 8 KiB pages. InnoDB commonly uses 16 KiB pages. If a query needs one row, the database still fetches the page containing that row.
table file
+-----------+-----------+-----------+-----------+
| page 0 | page 1 | page 2 | page 3 |
| row row | row row | row row | row row |
+-----------+-----------+-----------+-----------+This explains several production mysteries. A query touching 1,000 scattered rows can read 1,000 pages. A query touching 1,000 adjacent rows might read only a handful. The row count is not the I/O count.
The buffer pool is the real working memory
The engine caches pages in memory. PostgreSQL calls its portion shared_buffers; InnoDB calls it the buffer pool. Reads first check memory, then storage. Writes usually modify in-memory pages and mark them dirty; background processes flush them later.
SHOW shared_buffers;
SHOW effective_cache_size;# postgresql.conf
shared_buffers = 4GB
effective_cache_size = 12GB
work_mem = 64MBshared_buffers is not total memory. PostgreSQL also relies on the operating system page cache. InnoDB traditionally caches both data and indexes in its own buffer pool. Confusing these memory layers leads to bad capacity advice.
Storage is append, update, and vacuum economics
Engines differ in how they physically update rows. PostgreSQL's MVCC writes a new tuple version and leaves the old version behind until vacuum removes it. InnoDB updates clustered-index records in place and keeps undo information for older versions. Either way, update-heavy workloads create garbage or version metadata that must eventually be cleaned.
SELECT schemaname, relname, n_dead_tup, n_live_tup, last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 10;A table can be logically small but physically huge because dead row versions have not yet been reclaimed. That is bloat, and it is a storage-engine phenomenon before it is a query problem.
Why locality beats cleverness
Sequential scans are not inherently bad, and index scans are not inherently good. A sequential scan of contiguous pages can outperform thousands of random page reads, especially on remote storage. The physical order of data, fill factor, page size, and cache residency often matter more than the query's surface syntax.
Field notes
- Do not describe query cost in rows; convert it mentally to pages, cache hits, and storage round trips.
- A larger buffer pool does not fix a query that reads the entire table every minute.
- Autovacuum lag is storage maintenance debt, not just background noise.
- On cloud disks, random I/O and throughput caps make locality visible in latency percentiles, not averages.
Was this lesson helpful?