A free book · First edition
Eight Kilobytes
PostgreSQL from the page up
by Chase Pierce
A free book on PostgreSQL performance and design, from the 8 KB page on disk to indexes, query plans, locking, migrations, and running it in production. Every example run against PostgreSQL 18.
- Chapters
- 44 chapters
- Reading time
- About 13 hours
- Published
- October 5, 2026
- Publisher
- Published by Flagon
Almost everything about PostgreSQL performance comes down to one number: 8,192. That's the size of a page, the unit Postgres reads, writes, and caches. A fast query touches few pages. A slow one touches many, or waits on someone else who is. Once you can see the pages, most of the mystery goes away.
You've probably met the symptoms already. The dashboard that was instant at launch and crawls now. The migration that "does nothing" and took the site down anyway. The table that keeps growing faster than the data in it. And the standing offer to fix all of it with a bigger instance. This book is the alternative to that offer.
Count the pages, then earn the bigger box
The book starts at the bytes on disk and works up: how rows are stored and changed, how indexes and the planner find them, how to change a schema without an outage, and how to run Postgres in production. Along the way it builds two habits. Before you add an index, raise a setting, or blame the ORM, ask how many pages the work touches. And before you buy a bigger database, or a second one, use up the cheap fixes and prove in numbers that you've outgrown them.
That includes knowing where Postgres stops. On the same two million events, two column stores kept the data in under a fifth of the bytes and answered about ten times faster than a tuned Postgres. The book shows how far one Postgres goes, and the signal that tells you it's time for something else.
It's written for application developers who use Postgres every day, often through an ORM. You need basic SQL, not internals.
Every claim that matters comes with a check
Every query ran against PostgreSQL 18, on a sample schema you can load in a minute, and every claim that matters comes with a lab check you can rerun on your own machine. Each chapter is a short essay you can read on its own; deep dives hold the machinery and the full measurements for when you want them.
Free, and written in the open
Read it here, chapter by chapter, or download the whole thing as a PDF. It's versioned in git like everything else we publish, so when Postgres changes, the book does too. If something's wrong or unclear, that's a bug: open an issue or send a fix.
Don’t take our word for it
Every claim in this book is backed by a check you can run. One compose file gives you the same PostgreSQL 18 server and data we used, and each chapter has a lab script that proves its numbers on your machine, or fails loudly if they don’t hold.
Contents
Front matter
- Before you startWhy this book counts pages, why every claim comes with a proof, who it's for, how to read it at two depths, and how to run the same PostgreSQL 18 server and data used in every chapter.14 min
- Know the nouns firstA primer on the SQL and PostgreSQL basics every later chapter assumes, from tables, queries, updates, joins, and indexes to the server, its processes, schemas, roles, transactions, the catalog, settings, and the files underneath.12 min
The machine
- 01Where the time goesLearn the latency numbers behind every query, so you can predict how fast a page read, a commit, or a round trip will be before you measure it.15 min
- 02Everything is a pageOpen the files and pages behind a table, read a row's header byte by byte, and design tables that fit more rows into every 8 KB page.17 min
- 03The buffer cache decides what staysHow shared_buffers finds, keeps, evicts, and writes pages, how to see what's cached and warm it after a restart, and where each backend's private memory goes.16 min
- 04Updates are insertsSee exactly what UPDATE and DELETE do to a page, why tables bloat, and why one long transaction can hurt a whole database.16 min
- 05Keep your updates HOTMeasure what a non-HOT update really costs, learn the exact rules for HOT, and design tables, indexes, and fillfactor so updates stay cheap.18 min
- 06Write it down firstWhat happens between COMMIT and "it's safe", how to measure the WAL a statement writes, and why writes get expensive right after a checkpoint.17 min
- 07ACID, honestlyWhat each ACID letter guarantees in Postgres, the mechanism behind it, what it costs, how it gets switched off, and exactly where it stops.19 min
- 08Vacuum is not optionalWhat vacuum does to your pages, when autovacuum fires, what stops it working, and how to measure and fix bloat before it becomes an outage.18 min
- 09Postgres is not MySQLThe two databases bet opposite ways on where rows live and where old versions go. Learn the differences that follow, and the habits to drop.18 min
Designing for it
- 10Types are a contractPick column types and collations that stay correct and compact for years, and know which popular choices quietly cause bugs, bloat, or corruption.14 min
- 11Pick your keys onceChoose primary keys and public ids with real numbers on index size, page density, and WAL for bigint, random UUIDs, and UUIDv7.23 min
- 12Constraints, chosen on purposeModel data with constraints that hold, index your foreign keys, and measure what each one costs, so you keep them where they pay and drop them where they don't.19 min
- 13You don't need another database (yet)Know how far Postgres stretches to cover key-value, document, graph, time-series, queue, search, and vector workloads, and when a separate system earns its place.13 min
- 14Extensions worth knowingHow Postgres extensions install, load, and upgrade, which ones earn their place, what each one costs, and how to vet one before you depend on it.17 min
Finding rows fast
- 15The B-tree does the heavy liftingHow a B-tree finds a row in three or four page reads, how to order its columns, and how to spot the indexes that cost more than they give.16 min
- 16Beyond the B-treeWhen to reach for GIN, GiST, SP-GiST, BRIN, or hash indexes, what each one costs, and how to prove it helped with a measured plan.13 min
- 17Read the plan, not the clockRead a PostgreSQL query plan node by node, find where the pages and the time actually go, and fix slow queries with measured before-and-after proof.16 min
- 18The planner is guessingRead the statistics the planner uses, find the estimate that went wrong, and fix it with ANALYZE, extended statistics, or a better query instead of a hint.15 min
- 19The cheapest plan winsFollow a query from SQL to plan, check the planner's arithmetic by hand, and see exactly why it picks each scan, join, worker count, and JIT.18 min
- 20Queries that age wellThe query shapes that stay fast as tables grow, from keyset pagination and N+1 fixes to upserts, batching, and top-N per group.15 min
- 21Search without a search engineBuild full-text and vector search in Postgres, measure what parsing, ranking, snippets, TOAST, and recall cost, and know when you need a search engine.17 min
- 22Code in the databaseKeep per-row work off the primary, write functions the planner can see through, label them honestly, and know what every trigger costs and hides.17 min
Changing it
- 23Everyone waits in linePick an isolation level on purpose, handle errors and retries in transactions, read pg_locks during an incident, and build a job queue on SKIP LOCKED.16 min
- 24Change the schema, keep the siteShip schema changes to a busy database without stalls, using lock timeouts, NOT VALID constraints, concurrent indexes, and expand and contract.21 min
- 25Load it fastLoad millions of rows in seconds with COPY, FREEZE, and late indexes, and pay the WAL, checkpoint, hint-bit, and statistics bills on your schedule.17 min
Running it
- 26Connections aren't freeSize connection pools from cores and latency, choose a pooling mode knowingly, and set the timeouts that keep one bad client from exhausting the server.15 min
- 27Measure honestlyBenchmark Postgres so the numbers mean something. pgbench done right, latency percentiles that include the stalls, and test data shaped like production.17 min
- 28The settings that matterWhich of Postgres's 400 settings deserve your attention, how to change each one safely, and a commented starter config for a 4 vCPU, 16 GB server.18 min
- 29Watch it runUse pg_stat_statements, pg_stat_activity, and the cumulative statistics views to find what is slow, what is stuck, and what to alert on.17 min
- 30An untested backup is a rumorChoose between logical and physical backups, set up point-in-time recovery, prove your restores work, and upgrade major versions safely.16 min
- 31Trust no tenantSeparate owner and app roles, lock down schemas, build multi-tenant row-level security that is fast and pooler-safe, and harden connections.14 min
- 32Someone else runs the boxWhat a managed Postgres service takes off your plate and what it leaves you, from admin roles and settings to I/O ceilings, poolers, upgrades, and failover.22 min
- 33Pick a provider you can leaveWhere Postgres can run in October 2026, why some providers benchmark faster than others, and how to choose one you can walk away from.24 min
- 34Fix the work, then the boxThe order to make Postgres fast, lever by lever, each with the number that says you need it, proven end to end on one small app.16 min
Scaling out
- 35Scale up before you scale outEarn every bigger box with data. Find the real bottleneck, take the cheap structural wins, and know when one big box is truly done.15 min
- 36Copies of the truthBuild streaming replicas, measure their lag, price synchronous commits, route reads safely, fail over without split brain, and use logical replication.19 min
- 37Partition for the deletePartition big time-series tables for retention and maintenance, read pruning in real plans, dodge the pitfalls, and convert a live table without downtime.16 min
- 38Shard lastKnow when sharding is worth its cost, pick a shard key, keep the door open from day one, and choose among the real ways to shard Postgres.20 min
- 39Where Postgres losesThe honest map of where another system beats Postgres, measured where we could, and how to add one without losing your source of truth.20 min
Back matter
- ChecklistsOne line per habit, cheapest lever first, from finding the expensive work through queries, indexes, search, schema, migrations, and production to the bigger box, scaling out, and the slow-right-now incident, each linked to the section that proves it.25 min
- GlossaryThe terms the book leans on, defined in a sentence or three, each with a link to the section that teaches it.36 min
- BibliographyEvery source the book cites, grouped by kind, with the chapters that cite it.20 min