Sub topic 11
Telling the Planner About Your Storage
Prerequisites and the planner's model
This chapter assumes basic SQL, familiarity with postgresql.conf, and the ability to read a simple EXPLAIN plan. It also assumes you know that PostgreSQL can read data from shared buffers, the operating system's page cache, or disk.
The PostgreSQL planner estimates the cost of possible execution plans. It does not measure the actual speed of every read before choosing a plan. Settings such as seq_page_cost, random_page_cost, effective_cache_size, and effective_io_concurrency help it model your hardware.
These settings influence a plan choice. They do not make storage faster, reserve memory, or guarantee that PostgreSQL will use an index.
| Setting | What it describes | Common starting point |
|---|---|---|
| `seq_page_cost` | Relative cost of sequential page reads | `1.0` |
| `random_page_cost` | Relative cost of non-sequential page reads | `4.0` on older disk systems; about `1.1` to `2.0` on fast SSD storage |
| `effective_cache_size` | Planner estimate of cache available for data | Shared buffers plus a reasonable share of the OS cache |
| `effective_io_concurrency` | Number of concurrent read operations storage can handle effectively | Low on simple disks; higher on SSD or RAID |
Sequential and random page costs
seq_page_cost is the baseline cost for reading database pages in order. Its value is usually left at 1.0.
random_page_cost represents the cost of fetching a page from a different location. On spinning disks, random reads are much slower than sequential reads, so a value near 4.0 can be reasonable. On SSDs, random reads are often closer to sequential reads. A value such as 1.1 may better describe some SSD systems.
The setting is relative. Changing random_page_cost from 4.0 to 1.1 tells the planner that random access is about as inexpensive as sequential access. It does not change the operating system or PostgreSQL's actual read behavior.
Lowering the value can make index scans more attractive. Setting it too low can cause the planner to choose indexes for queries where a sequential scan would be faster.
Cache estimates
effective_cache_size tells the planner how much data cache is likely available to a query. It includes PostgreSQL's shared_buffers and the portion of the operating system page cache that PostgreSQL can reasonably use.
It is only a planning estimate. PostgreSQL does not allocate this amount when you change the setting. Do not set it to the machine's total RAM if other applications, connections, or services need that memory.
A useful starting point is the memory available for database file caching after accounting for PostgreSQL and the operating system. Increase or decrease it based on observed plans and workload measurements.
SELECT name, setting, unit, context
FROM pg_settings
WHERE name IN (
'seq_page_cost',
'random_page_cost',
'effective_cache_size',
'effective_io_concurrency'
);
BEGIN;
SET LOCAL random_page_cost = 1.5;
SET LOCAL effective_cache_size = '8GB';
SET LOCAL effective_io_concurrency = 200;
EXPLAIN (COSTS ON)
SELECT count(*)
FROM pg_class
WHERE relkind = 'r';
ROLLBACK;I/O concurrency and plan behavior
effective_io_concurrency describes how many read operations the storage system can handle at the same time. PostgreSQL uses this information mainly when planning bitmap heap scans and prefetching pages.
A simple disk array may need a low value. SSDs, NVMe devices, and arrays with parallel I/O can often use a higher value. The correct value depends on the storage driver, filesystem, workload, and queue depth. A larger value is not automatically better.
Test changes with representative queries. Compare EXPLAIN (ANALYZE, BUFFERS) output and total execution time. A plan with lower estimated cost is not proof of a faster plan.
Common errors
- Treating
effective_cache_sizeas a memory allocation. - Copying SSD settings to a workload whose data is mostly on slower network storage.
- Lowering
random_page_costuntil every query uses an index. - Comparing estimated costs without measuring execution time.
- Testing only a warm cache when production often starts with a cold cache.
- Ignoring stale statistics. Run
ANALYZEbefore judging a plan caused by incorrect row estimates.
Exercise
Your database moved from spinning disks to SSDs. Which setting should you test first, and how should you validate the change?
Show answer
Test a lower randompagecost, such as 1.5, while keeping seqpagecost at 1.0. Compare representative queries with EXPLAIN (ANALYZE, BUFFERS) before and after the change, and keep it only if measured workload performance improves.