Sub topic 3
Sizing `shared_buffers`
Working assumptions
This chapter assumes you can run basic SQL in psql, read RAM and disk measurements, and edit PostgreSQL configuration. You should also understand that PostgreSQL uses both its own buffer cache and the operating system's file cache.
shared_buffers is the amount of RAM PostgreSQL reserves for its shared buffer cache. The cache stores table and index pages that PostgreSQL is actively using. All server processes can access this cache.
A larger cache can reduce disk reads, but it also leaves less RAM for connections, temporary operations, the operating system, and other services. The goal is a measured starting value, not the largest possible value.
Choose a starting value
For a dedicated database server, a common starting point is about 25% of physical RAM. For example, a server with 32 GB of RAM might start with 8 GB:
ALTER SYSTEM SET shared_buffers = '8GB';This command writes the setting to postgresql.auto.conf. shared_buffers is read when the server starts, so a reload alone does not apply the new value. Restart PostgreSQL during a planned maintenance window.
Check the current value and whether a restart is needed:
SELECT name, setting, unit, context, pending_restart
FROM pg_settings
WHERE name = 'shared_buffers';The context value is normally postmaster, which means the setting requires a server restart. The pending_restart column shows whether the configured value differs from the value currently in use.
The 25% guideline is not a rule. Use a smaller share when the host runs other important services, when PostgreSQL has many connections, or when the operating system needs substantial file-cache space. A larger cache may help a dedicated host with a large working set, but test it against a smaller value.
| Server situation | Reasonable starting direction |
|---|---|
| Dedicated PostgreSQL host | About 25% of RAM |
| PostgreSQL shares the host | Use less and reserve RAM for other services |
| Very large dedicated host | Start conservatively, then benchmark larger values |
| Small host with a small working set | Avoid reserving more RAM than the workload can use |
Understand the two caches
PostgreSQL reads pages through shared_buffers, while the operating system may also keep recently read file pages in its page cache. A page miss in PostgreSQL does not always mean a physical disk read. The operating system may satisfy the read from RAM.
This overlap is why increasing shared_buffers does not always produce a proportional improvement. The operating system still needs memory for file caching and other tasks. Compare query latency, physical I/O, and available memory after each change.
Verify the result
Use a representative workload rather than a single query. Inspect buffer activity and plan behavior before and after the restart. If a larger cache improves repeated reads but causes swapping or leaves too little memory for connections, the larger value is harmful.
Exercise
A dedicated 32 GB PostgreSQL server currently uses shared_buffers = '4GB'. What is a reasonable first experiment, and what must happen before the new value is active?
Show answer
Test about 8GB, or 25% of RAM, while measuring the workload. Because sharedbuffers requires the postmaster context, restart PostgreSQL; pgreload_conf() alone is not enough.