Sub topic 1
Where PostgreSQL Uses Memory
PostgreSQL uses memory in several layers. Some memory is shared by all server processes. Other memory belongs to one process or one operation. This distinction matters because a setting such as work_mem can use much more memory than its value suggests when many queries run at once.
Prerequisites
This chapter assumes that you can run basic SQL in psql, edit postgresql.conf, and understand the difference between RAM, disk, and the operating system's file cache. You should also be able to read a simple EXPLAIN plan.
The main memory areas
A PostgreSQL server has a supervising process and several child processes. A client connection normally uses one backend process. These processes use the following areas:
| Area | Shared or private | Main purpose |
|---|---|---|
| `shared_buffers` | Shared | Caches table and index pages inside PostgreSQL |
| Operating system page cache | Shared by the OS | Caches file data, including PostgreSQL data files |
| `work_mem` | Private per operation | Holds sorts, hashes, and related intermediate results |
| `maintenance_work_mem` | Private per maintenance task | Supports `VACUUM`, index creation, and similar work |
| WAL buffers | Shared | Holds records before they are written to the WAL files |
| Process memory | Private per backend | Stores connection state, plans, and other local data |
The shared buffer cache contains fixed-size pages, usually 8 KB each. When a query needs a table page, PostgreSQL first checks shared_buffers. If the page is absent, PostgreSQL reads it from a data file. The operating system may satisfy that read from its own page cache instead of physical storage.
This creates two caches:
- PostgreSQL's buffer cache.
- The operating system's page cache.
A page can therefore be in the OS cache even when it is not in shared_buffers. Increasing shared_buffers does not remove the need to leave RAM for the operating system, connections, and other applications.
Shared memory and per-operation memory
shared_buffers is allocated for the server as a whole. Its size is relatively easy to budget. By contrast, work_mem is a limit for one sort or hash operation, not for one connection.
A single query can contain several memory-consuming nodes. Concurrent connections can run those nodes at the same time. A rough upper-bound model is:
SELECT 8 * 1024 * 1024 * 1024 AS shared_buffers_bytes,
100 * (64 * 1024 * 1024) AS example_work_mem_worst_case_bytes;
-- Rough budget:
-- shared_buffers
-- + active connections * (work_mem * memory-hungry nodes per query)
-- + concurrent maintenance_work_mem users
-- + process memory and operating-system headroommaintenance_work_mem can also be large. It applies to a maintenance operation, and autovacuum workers may use autovacuum_work_mem instead when that setting is configured. Several workers or manual maintenance commands can overlap.
How the layers interact
The goal is not to make every cache as large as possible. The goal is to leave enough memory for all layers during normal and peak workloads. Later chapters use statistics, execution plans, and logs to measure which layer is limiting performance.
Exercise
Why can a server run out of memory even when maxconnections * workmem is below available RAM?
Show answer
workmem applies to each sort or hash node. One connection can use several such nodes, and many connections can run them concurrently. Maintenance tasks, backend overhead, sharedbuffers, and the operating system also need memory.