The idea · Lab 3.5.1shared_buffers sets aside a chunk of memory PostgreSQL manages directly for caching table and index pages — too small, and the same pages get re-read from the OS cache (or disk) far more than necessary; too large, and it starves the OS's own filesystem cache, which PostgreSQL also depends on for everything shared_buffers itself does not hold. Roughly 25% of real available memory is the standard starting point precisely because it leaves the OS enough room to do useful caching of its own. Changing shared_buffers requires a genuine restart — it is fixed at postmaster startup, not reloadable — which this lab performs for real, not just edits a config file and hopes.
work_mem governs memory for a single sort or hash operation; get it too low and a query that could fit comfortably in memory spills to temporary files on disk instead, genuinely slower. effective_cache_size is different in kind from both: it never allocates anything at all — it only tells the planner how much total caching (shared_buffers plus OS cache) to assume exists when estimating a plan's cost, which is why this lab proves its effect shows up entirely in EXPLAIN's cost numbers, never in real memory usage or actual execution behavior.
Check the Kernel Memory Record
$ dmesg | grep 'Memory:' || echo 'No kernel Memory: record in cxemu'
No kernel Memory: record in cxemu
cxemu has an empty kernel log because it does not run a guest kernel. Kernel RAM cannot be inspected with dmesg in cxemu. This lab uses 64MB as an exercise setting and verifies it with SHOW after a restart. Size production shared_buffers using the actual server memory and workload; do not infer a RAM measurement from this emulator.
shared_buffers: A Genuine Restart
sed -i 's/^shared_buffers = 16MB/shared_buffers = 64MB/' /var/lib/postgresql/19/data/postgresql.conf
pg_ctl restart -D /var/lib/postgresql/19/data -w
SHOW shared_buffers; -- 64MB
shared_buffers cannot be changed with a plain SET or even a config reload — it is fixed at postmaster startup, which is exactly why this required a real pg_ctl restart, not just an edit to the file.
work_mem: A Real Sort, Genuinely Fixed
SET work_mem = '4MB'; -- the previous default
EXPLAIN (ANALYZE, TIMING OFF) SELECT * FROM sql_cfg_sort ORDER BY val;
Sort Method: external merge Disk: 7400kB
Execution Time: 7139.965 ms
SET work_mem = '16MB';
EXPLAIN (ANALYZE, TIMING OFF) SELECT * FROM sql_cfg_sort ORDER BY val;
Sort Method: quicksort Memory: 14218kB
Execution Time: 4242.965 ms
The identical sort of 300,000 rows, roughly 40% faster once given enough memory to complete without touching disk at all.
effective_cache_size: Advisory, Proven Directly
SET effective_cache_size = '128kB'; -- "assume almost nothing is cached"
EXPLAIN (ANALYZE, TIMING OFF) SELECT * FROM sql_cfg_sort WHERE val BETWEEN 100 AND 150;
-- cost=0.42..59390.45, Execution Time: 203.422 ms
SET effective_cache_size = '4GB'; -- "assume everything is cached"
EXPLAIN (ANALYZE, TIMING OFF) SELECT * FROM sql_cfg_sort WHERE val BETWEEN 100 AND 150;
-- cost=0.42..7032.46, Execution Time: 141.500 ms
The cost estimate swings by more than 8x between the two settings. The real execution time and real buffer access barely move at all — because effective_cache_size never allocates a single byte of memory; it only changes what the planner assumes when estimating cost.
Key concepts
shared_buffers
A block of memory PostgreSQL allocates at startup and manages directly for caching table and index pages, shared across every backend process. It requires a full server restart to change — unlike most GUCs, it is fixed for the lifetime of the running postmaster, since the shared memory segment it reserves is sized once at startup.
effective_cache_size
A GUC that allocates nothing at all — it only tells the query planner how much total caching capacity (shared_buffers plus whatever the OS is likely caching) to assume when estimating the cost of repeated random-access reads, such as an index scan. Setting it does not reserve, allocate, or reduce any real memory; it only changes the planner's cost model, which is why its entire measurable effect shows up in EXPLAIN's cost numbers rather than in real execution behavior.
What to remember
- Always check a server's own real memory directly (dmesg, /proc/meminfo, or the provider's spec) before setting shared_buffers — a ratio copied from a different machine's absolute number is meaningless
- shared_buffers requires a genuine restart to change, since it reserves one fixed shared memory segment at postmaster startup
- work_mem genuinely determines whether a sort or hash completes in memory or spills to disk — measurably slower when it spills, for real, sizable operations
- effective_cache_size allocates nothing; it only recalibrates the planner's cost model for repeated random-access reads, which is why its effect shows up entirely in EXPLAIN's cost estimates and not in real execution behavior
- maintenance_work_mem is an ordinary session-settable GUC, safe to raise temporarily around a specific large operation like an index build or VACUUM FULL