liveBook · Manning

Shared_buffers

As explained in PostgreSQL Mistakes and How to Avoid Them

shared_buffers is a PostgreSQL configuration parameter that sets the amount of shared memory allocated for database page buffers.

shared_buffers reserves shared memory for PostgreSQL buffers: blocks read from disk and blocks used in operations. The 128 MB default is a portable baseline meant to start on small machines, not a production sizing decision. Change it only as part of testing or staging for the actual server and workload; PostgreSQL must be restarted before a changed value takes effect. section 6.1

More buffer memory can help, but bigger is not automatically better. Memory settings interact: shared_buffers is shared, while work_mem is available to individual operations and maintenance_work_mem serves work such as CREATE INDEX and VACUUM. Treat a configuration as a budget rather than independently maximizing each setting. section 6.1 section 6.2

Measure a realistic restore

A pgbench database dump restored in 44m15s under default settings. After setting shared_buffers to 3GB, work_mem to 128MB, and maintenance_work_mem to 512MB, then restarting, the same restore completed in 26m17s. The result supports testing a workload-specific configuration, but it does not isolate shared_buffers as the sole cause because three settings changed together. section 6.1

shared_buffers = 3GB              #1
work_mem = 128MB                  #2
maintenance_work_mem = 512MB      #3

This configuration listing shows a moderate combined adjustment. It is a benchmark input, not a universal recipe; validate it against available RAM and production-like load. section 6.1

An aggressive setting can fail to improve throughput. Raising shared_buffers to 12GB and work_mem to 2GB produced roughly the same 100-client pgbench throughput as the earlier configuration, then a 1,000-client stress test stalled and the Linux OOM killer terminated a PostgreSQL process. The danger is system-wide memory exhaustion under concurrency, with no guarantee that the kernel kills the process you expect. section 6.2 section 6.2

Use shared_buffers as an operationally tested allocation:

  • Start from the default as a functional baseline, then test on staging or production-like hardware rather than assuming it is tuned. section 6.1
  • Restart PostgreSQL after changing shared_buffers, then compare repeatable workload results such as restore duration or pgbench TPS. section 6.1 section 6.2
  • Increase connection load separately when checking memory safety, and inspect server logs if the workload stalls or processes are killed. section 6.2

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Explains buffer allocation and restart behavior through restore and pgbench measurements, without providing a universal sizing formula.
Examples
Uses a pgbench dump restore, 100-client TPS comparisons, and a 1,000-client stress test with kernel logs.
What it assumes
Assumes PostgreSQL administration, configuration editing, and benchmark execution rather than teaching them from zero.
What you can do after
Evaluate/understand shared_buffers changes by restarting PostgreSQL, benchmarking realistic workloads, and checking memory safety under concurrency.
How it connects
Connects shared_buffers to work_mem, maintenance_work_mem, max_connections, pgbench, and the Linux OOM killer.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.