Lesson 3 of 13 · 14m
What a Scan Actually Costs
Seq scans beat index scans past a fraction of the table because random_page_cost prices heap fetches — tune it for NVMe and VACUUM for index-only scans.
A Postgres table is an unordered pile of 8kB pages on disk called the heap. A sequential scan reads every page of the heap start to finish and costs seq_page_cost = 1.0 per page. An index scan walks a B-tree to find TIDs (tuple identifiers — page number plus offset), then fetches each heap page it points at — and the planner prices those fetches at random_page_cost = 4.0 each, four times the sequential price, because the defaults were calibrated for spinning disks where a random seek genuinely costs several times a sequential read.
That ratio drives almost every scan decision the planner makes.
The wrong model: index scan means fast
The reflex is "the query is slow, so it needs an index." Sometimes that's right. But an index scan is not magic — it's a sorted lookup structure plus a random heap fetch per matching row. When your predicate matches 5% of a table, the index path is a B-tree descent and a bounded number of random fetches; it wins. When your predicate matches 40% of the table, the index path is a B-tree descent plus random fetches of nearly every page in the heap anyway — at 4x the per-page price and with the index reads on top. The planner looks at pg_statistic, sees the selectivity, and correctly chooses the sequential scan you were about to "fix."
Watch EXPLAIN (ANALYZE, BUFFERS) on a wide predicate: the seq scan shows shared read=N fetching pages once each. The index plan shows the same heap pages fetched randomly, plus index pages. Same heap traffic, worse ordering, extra structure on top. Postgres wasn't ignoring your index. It did the arithmetic.
The tuning knobs the arithmetic runs on
random_page_cost and seq_page_cost are per-cluster settings, and the defaults assume rotational storage. On the NVMe volumes anything modern runs on — including every cloud gp3-class volume — random I/O is nearly as cheap as sequential. Leaving random_page_cost = 4 on NVMe makes the planner systematically overprice index scans and pick seq scans it shouldn't. The standard correction is random_page_cost = 1.1, paired with effective_io_concurrency = 200 or so so the planner knows the device can sustain parallel prefetch.
effective_cache_size is the other one people miss. It's not memory Postgres allocates — it's the planner's estimate of total cache available (shared_buffers plus OS page cache) used to decide whether an index page is likely already in memory. Set it to roughly 50–75% of the box's RAM. A tiny value makes index scans look artificially expensive; a huge value does the reverse.
Index-only scans: the exception that proves the rule
An index scan normally still hits the heap because the index stores keys and TIDs, not the row. The exception is the index-only scan: if every column the query needs is in the index itself — explicitly added via INCLUDE — Postgres can answer from the index alone. The catch is MVCC: the index doesn't carry visibility information, so the engine must check the visibility map to confirm the heap page has no newer tuples. The visibility map only gets set by VACUUM.
That's the "my index-only scan shows Heap Fetches: 94000" mystery. Freshly bulk-loaded or heavily-updated tables have dirty visibility maps, and the index-only scan degrades into heap fetches until vacuum catches up. If you load tables for analytics, VACUUM ANALYZE after load isn't housekeeping — it's the step that makes index-only scans real.
What to actually do
- On SSD/NVMe:
ALTER SYSTEM SET random_page_cost = 1.1;andeffective_io_concurrency = 200. Re-check your EXPLAIN plans — some seq scans will flip to index scans. - Set
effective_cache_sizeto about half the machine's RAM. It's a hint, not an allocation; being off by 2x still beats the 4GB default on a 128GB box. - If a predicate selects a third or more of the table and the planner chose a seq scan, believe it — don't force the index with
enable_seqscan = offoutside of diagnostics. - For index-only scans on reporting tables:
VACUUMthe table after bulk changes, add non-key columns withINCLUDE, and watchHeap Fetchesin EXPLAIN drop toward zero.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
PostgreSQL Fixed 52 CVEs in One Release. The Upgrade Is the Easy Part.
PostgreSQL 18.7, 17.12, 16.16, 15.20 and 14.25 fix 52 security vulnerabilities, almost twice August's record of 28. The binary swap took under a second in my test. The post-update steps are where the work is, and one of them makes some indexes several times bigger.
What PostgreSQL 15 Changed
A new version of the popular open-source database PostgreSQL is now available, offering performance improvements over PostgreSQL 14 and new functions and capabilities for managing workloads in local and distributed deployments. Released on October 13 by the PostgreSQL Global Development Group,…
PHP API Performance: N+1 Queries, cURL, and Connection Pools
A PHP API case study: fixing an N+1 query, twenty serial cURL calls, missing connection pooling, and stats recalculated on every request with Redis caching.