Sub topic 8
Budgeting Memory Across Connections
Assume you can run basic SQL in psql, read PostgreSQL settings, and distinguish server RAM from disk and the operating system page cache. The goal is to estimate peak memory use before increasing a setting.
Shared memory and connection memory
shared_buffers is a shared cache used by all PostgreSQL server processes. Its size does not multiply when more clients connect.
Other memory is allocated by individual server processes. work_mem is the limit for one sort, hash table, or similar operation. It is not a limit per connection. One query can contain several memory-hungry operations, and a parallel query can run copies of those operations in several workers.
maintenance_work_mem applies to operations such as VACUUM and CREATE INDEX. Autovacuum can use autovacuum_work_mem instead. These settings can therefore consume memory concurrently across maintenance workers.
A rough peak-memory estimate is:
SELECT
8 * 1024 * 1024 * 1024 AS shared_buffers_bytes,
100 * (64 * 1024 * 1024) AS example_work_mem_worst_case_bytes;
-- A planning model:
-- shared_buffers
-- + active_connections * work_mem * memory_hungry_nodes_per_query
-- + concurrent_maintenance_jobs * maintenance_work_mem
-- + PostgreSQL process overhead
-- + operating-system and page-cache headroomThis is a safety model, not a prediction of normal usage. Most connections do not use their full work_mem, and many queries have no sort or hash. However, an estimate based only on max_connections * work_mem is also incomplete because a single query may use multiple nodes.
Choosing connection limits
max_connections sets the maximum number of client connections. Each active backend needs process memory and may run operations that allocate work_mem. Raising this value can therefore make a memory shortage more likely even when typical queries are small.
Use a connection pooler when many clients need access but only a smaller number of database sessions need to execute queries at once. A lower database connection limit makes the memory budget easier to control.
For example, suppose a host has 16 GB of RAM:
| Component | Planning value |
|---|---|
| `shared_buffers` | 4 GB |
| 40 active connections | 40 |
| `work_mem` | 64 MB |
| Estimated nodes per active query | 2 |
The rough query-work-memory allowance is 40 × 64 MB × 2, or 5 GB. Add maintenance jobs, backend overhead, the operating system, and page-cache space before deciding whether this fits. If parallel queries are common, include their workers in the active-operation count.
> callout warning > work_mem is per memory operation, not per connection. A setting that looks safe for one query can cause an out-of-memory failure when many connections run multi-stage or parallel queries together.
Apply limits safely
Start with a conservative global work_mem. Give larger values only to known analytical roles or sessions:
ALTER ROLE reporting SET work_mem = '256MB';Keep maintenance_work_mem large enough to help maintenance, but account for concurrent autovacuum workers and manual index builds. Measure temporary-file creation to find operations that exceed their work memory:
SELECT datname, temp_files, pg_size_pretty(temp_bytes) AS temp_size
FROM pg_stat_database
ORDER BY temp_bytes DESC;Change one setting at a time, observe peak concurrency, and leave headroom for connection spikes and the operating system.
Exercise
A query has three sort or hash nodes, runs with two parallel workers, and uses work_mem = 64MB. What rough work-memory allowance should you budget for this query?
Show answer
Budget about 3 × (1 leader + 2 workers) × 64MB = 576MB, before backend overhead and other concurrent queries.