skip to content

In PostgreSQL, what does shared_buffers control, what is its default, and why does changing it require a server restart?

level: juniorimportance: must knowfreq 68%

answer

  1. shared, not per connection
  2. a unitless value counts 8 kB blocks
  3. sized once when the postmaster starts
  4. the context column in pg_settings

basics

~10 s

shared_buffers sizes PostgreSQL's own cache of 8 kB data pages in shared memory, used by every backend. It defaults to 128MB and has postmaster context, because that segment is allocated once at server start.

solid answer

~40 s

`shared_buffers` sets how much shared memory PostgreSQL reserves for its own cache of data pages; every backend reads and dirties pages there instead of keeping a private copy. The default is `128MB` (initdb may choose less if the kernel cannot supply it), and a value written without units counts 8 kB blocks, so `shared_buffers = 16384` is 128MB. Its context is `postmaster`: the segment is created once when the server starts and is never resized, so a new value needs a full restart; a reload leaves the old size in place. For a dedicated server the manual suggests starting near 25% of RAM and says more than 40% rarely helps, because PostgreSQL also reads through the operating system's page cache.

code

ini · 4 lines
ini
# postgresql.conf on a dedicated 64 GB server
shared_buffers = 16GB          # postmaster context: restart to apply
huge_pages = try               # default; on refuses to start without them
effective_cache_size = 48GB    # planner estimate only, allocates nothing

go deeper

for a junior

Recall that shared_buffers is one shared page cache, 128MB by default, and that changing it needs a restart rather than a reload.

for a middle

Explain the 8 kB block unit, the postmaster context, the 25% starting point and why PostgreSQL still leans on the kernel page cache.

for a senior

Show you size it against everything else on the host: per-operation memory times concurrency, huge pages, and the checkpoint settings a larger pool drags along.

for a principal

Frame the trade between a bigger shared pool and memory left for sorts, hashes and the OS cache as a measured decision per workload, not a fixed percentage.

## What shared_buffers is `shared_buffers` is the size of PostgreSQL's **shared buffer pool**: an area of shared memory, created by the postmaster, that holds copies of 8 kB pages from tables and indexes. Every backend process that reads a row looks for its page there first, and every change is made to the page in that pool before it is written back to the data file, usually by the checkpointer or the background writer. Because the pool is **shared by all sessions**, its size is paid once for the whole instance, not once per connection. That makes it different from the other memory settings in this area: - `work_mem` and `maintenance_work_mem` are budgets that each backend may use privately, per operation. - `effective_cache_size` allocates nothing at all; it is the planner's assumption about how much data the operating system and `shared_buffers` together can keep cached (default `4GB`). - `wal_buffers` is a separate, much smaller shared area for WAL; its default is derived from `shared_buffers`. ## Default, units and how to read it The compiled-in default is **16384 blocks**, which is `128MB` with the standard 8 kB block size; the sample `postgresql.conf` shows `#shared_buffers = 128MB # min 128kB`. The manual notes initdb may pick less on a kernel that cannot provide it. Units are the classic trap. A value **without a unit is read as blocks**, not kilobytes: | line in postgresql.conf | memory reserved | |---|---| | `shared_buffers = 16384` | 128 MB (16384 x 8 kB) | | `shared_buffers = 4096` | 32 MB | | `shared_buffers = 16GB` | 16 GB | So always write the unit. `SHOW shared_buffers` prints the value with a unit; `pg_settings` shows the raw `setting` (in blocks) next to `unit` = `8kB`. ## Why a change needs a restart `shared_buffers` has the **postmaster** context. The sequence at start-up is: 1. The postmaster reads the configuration and computes the size of the main shared memory area, of which `shared_buffers` is the largest part. 2. It allocates that area once (on huge pages, if configured and available). 3. Every backend it forks later inherits the same mapping. There is no mechanism to grow or shrink that area while backends are attached to it, so a new value is only picked up by a full restart. A reload re-reads the file but leaves the running size alone, and the parameter is flagged as waiting for a restart (how to see that belongs with the parameter-setting mechanics). Trying `SET shared_buffers` in a session fails with *parameter "shared_buffers" cannot be changed without restarting the server*. ## A starting size The manual's guidance for a dedicated database server with at least 1 GB of RAM: - start at about **25% of RAM**; - going beyond about **40%** is unlikely to beat a smaller setting, because PostgreSQL deliberately relies on the **operating system cache** as a second layer; - a larger value usually needs a larger `max_wal_size`, so checkpoints can spread the extra dirty pages over a longer interval (checkpoint sizing belongs with WAL and durability); - on a machine with less than 1 GB, use a smaller share and leave room for the operating system. On a 64 GB dedicated server that suggests roughly 16 GB as the opening value, then measuring rather than guessing further. Whatever is not in `shared_buffers` is not wasted: it serves `work_mem` for sorts and hashes, maintenance operations, and the kernel page cache that PostgreSQL also reads through. ## Practical notes - **Huge pages.** A pool of many gigabytes is usually placed on Linux huge pages (`huge_pages`, default `try`) to cut page-table overhead. - **Its size is fixed.** Unlike per-query memory, `shared_buffers` does not grow with the number of sessions or queries, so it is rarely what pushes a busy server into the kernel's out-of-memory killer; the per-operation budgets multiplied by concurrency are. - **How pages are chosen for eviction** (the clock sweep, usage counts, ring buffers for large scans) is the buffer manager's business, covered with PostgreSQL's process architecture, and the general case for an engine-managed cache is buffer-pool theory. ## Traps interviewers listen for - **Per-connection thinking.** Multiplying `shared_buffers` by `max_connections` double-counts it; the multiplication belongs to `work_mem`, not to the shared pool. - **Reload expectations.** Editing the file and calling `pg_reload_conf()` changes nothing for this setting; plan a restart window. - **Unit slips.** `shared_buffers = 4096` is 32 MB, a value smaller than the default, which can quietly hurt a production server after a configuration-management template drops the unit. - **Confusing it with the planner hint.** Raising `effective_cache_size` makes index scans look cheaper to the planner; it never enlarges any cache.

  • Why not give shared_buffers 75% of a 64 GB PostgreSQL server?
    PostgreSQL reads data files through the operating system, so the kernel page cache is a second cache layer; the manual says going past about 40% of RAM rarely beats a smaller value. A huge pool also leaves less memory for per-operation `work_mem`, maintenance work and the kernel cache, and it usually forces larger checkpoint settings so the extra dirty pages can be written out smoothly.
  • Does raising effective_cache_size to 48GB on that server allocate memory?
    No. `effective_cache_size` (default `4GB`) is only the planner's estimate of how much of the data `shared_buffers` and the kernel cache can keep cached; a higher value makes index scans look cheaper. It reserves nothing, can be changed per session, and is usually set to `shared_buffers` plus the page cache you expect the OS to keep, for example about 48GB on a 64 GB host.

  • shared_buffers is allocated per connection, so it multiplies by max_connections.
  • Running SELECT pg_reload_conf() applies a new shared_buffers value immediately.
  • shared_buffers = 4096 without a unit means 4096 kB of cache.
  • Give shared_buffers 75-80% of RAM, since PostgreSQL bypasses the OS cache.
  • Raising effective_cache_size makes PostgreSQL allocate a larger cache.