Sub topic 4
effective_cache_size and the OS page cache
Assume you can read basic EXPLAIN plans and understand that PostgreSQL has a shared buffer cache. This chapter focuses on a planner estimate, not on reserving memory for the server.
What effective_cache_size means
effective_cache_size is a PostgreSQL setting that estimates how much data PostgreSQL can find in memory during a query. It includes the PostgreSQL shared buffer cache and the portion of the operating system's page cache likely to hold database files.
It does not allocate memory. It does not limit query memory. It only influences the query planner's cost calculations.
The operating system's page cache stores recently read file blocks in RAM. PostgreSQL may read a relation block from this cache instead of waiting for storage. The planner cannot know the exact current contents of that cache, so effective_cache_size gives it a working assumption.
SELECT name, setting, unit, context, pending_restart
FROM pg_settings
WHERE name IN ('shared_buffers', 'effective_cache_size',
'random_page_cost', 'seq_page_cost');
SHOW shared_buffers;
SHOW effective_cache_size;A higher value tells the planner that more table and index pages may already be cached. This usually makes index scans look less expensive. A lower value makes disk reads seem more likely and can make sequential scans more attractive.
A practical starting estimate
Start with the RAM available to PostgreSQL and the operating system. Reserve memory for connections, application processes, monitoring, the kernel, and other services. The remaining memory is a reasonable upper bound for PostgreSQL's shared buffers plus useful file cache.
For example, on a dedicated 64 GB machine:
| Item | Example amount |
|---|---|
| Total RAM | 64 GB |
| `shared_buffers` | 16 GB |
| Operating system and other services | 8 GB |
| Starting `effective_cache_size` estimate | 48 GB |
The 48 GB estimate represents 16 GB of shared buffers plus roughly 32 GB that the operating system could use for database file caching. It is an estimate, not a promise that 48 GB is currently cached.
ALTER SYSTEM SET effective_cache_size = '48GB';
SELECT pg_reload_conf();
SHOW effective_cache_size;effective_cache_size normally takes effect after a reload. Confirm the active value with SHOW or pg_settings, rather than assuming that the configuration file was loaded successfully.
Check the estimate with EXPLAIN
Use EXPLAIN (ANALYZE, BUFFERS) on representative queries. Compare the estimated plan with actual buffer activity. shared hit means a block came from PostgreSQL's shared buffers. shared read means PostgreSQL had to bring it into shared buffers, although the operating system may still have supplied it from its page cache.
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 20;Change one setting at a time and test the same workload. A plan change alone is not proof of improvement. Check execution time, read and hit counts, CPU use, and storage latency. Adjust random_page_cost separately when the storage device makes random reads much cheaper or slower than PostgreSQL's default assumption.
Exercise
A server has 64 GB of RAM, 16 GB of sharedbuffers, and 8 GB reserved for the OS and other services. What is a reasonable first effectivecache_size estimate, and what does that setting do?
Show answer
Start around 48 GB as a measured estimate. It tells the planner how much data may be cached; it does not reserve or allocate 48 GB.