shared_buffers

shared_buffers is both PostgreSQL's buffer pool and the setting that sets its size. All backend processes read and modify 8KB pages in this shared-memory area. Unlike other DBMSs, PostgreSQL reads data files through the operating system cache, so a page missed in shared_buffers is often found in the operating system cache.

Key specs

ItemValue
Default128MB (initdb may pick a smaller value to fit the system)
Common sizeAbout 25% of server memory. Going above 40% is considered to give little benefit
How to changepostgresql.conf or ALTER SYSTEM, restart required
Unit8KB buffers. A bare number means a buffer count
Eviction ruleClock sweep, with a usage count per buffer (usage_count, maximum 5)
Large scan protectionRing buffer (reuses only a small fixed area)
Dirty page writesbackground writer, checkpointer
Prefill after restartpg_prewarm extension

How it works

Lookup. When a backend needs a page, it looks it up by (file, block number) in the buffer mapping hash table. If the page is there, it increments usage_count and uses it.

Eviction. If the page is not there, the clock hand sweeps the buffers, decrementing usage_count one at a time, picks a buffer that is at 0 and unused, and reads the new page into it. A frequently used page stays for a long time because the hand must pass over it several times before it reaches 0. If the chosen buffer holds a dirty page, it must be written to disk first, which delays the read; the background writer writes pages ahead of time to reduce such cases.

Ring buffer. A sequential scan of a table larger than a quarter of shared_buffers reuses only a small 256KB ring, not the whole buffer pool. VACUUM and bulk writes (COPY, CREATE TABLE AS) also each use their own ring. This keeps pages that are read only once from pushing out frequently used pages.

Operating system cache. A page evicted from the buffer pool usually still remains in the operating system page cache. So shared_buffers is not grown to most of memory, and the rest is left to the operating system cache. effective_cache_size tells the optimizer the combined size of the two caches. The cost is double caching, where the same page sits in both caches.

Settings and checks

SHOW shared_buffers;
ALTER SYSTEM SET shared_buffers = '8GB';     -- takes effect after restart

-- Cache hit ratio (by table and index blocks; operating system cache hits count as 'reads')
SELECT datname, blks_hit, blks_read,
       round(100.0 * blks_hit / nullif(blks_hit + blks_read, 0), 2) AS hit_pct
FROM pg_stat_database WHERE datname = current_database();

-- Tables currently in the buffer pool (pg_buffercache extension)
CREATE EXTENSION IF NOT EXISTS pg_buffercache;
SELECT c.relname, count(*) * 8 / 1024 AS mb
FROM pg_buffercache b JOIN pg_class c ON c.relfilenode = b.relfilenode
GROUP BY c.relname ORDER BY 2 DESC LIMIT 10;

In an execution plan, EXPLAIN (ANALYZE, BUFFERS) shows the buffer usage of a single statement through shared hit (buffer pool hit) and read (read from outside the buffer pool).

  • With large memory, turn on huge_pages to reduce page table overhead.
  • If slowness right after a restart is the problem, read frequently used tables ahead of time with pg_prewarm. With pg_prewarm.autoprewarm on, the list is saved before shutdown and read again at startup.

Comparison with other DBMSs

PostgreSQLInnoDBOracleSQL Server
Common sizeAbout 25% of memory50-80%Allocated within the SGAMost of the permitted memory
Operating system cacheUsed togetherUsually bypassedUsually bypassedNot used

In the articles

NOTE The default value and ring size can vary by version.

Related pages