Sub topic 14
Operating System Settings: Overcommit and Swap
Prerequisites
Assume you can read a PostgreSQL configuration file, run SQL in psql, and distinguish RAM, shared memory, and disk storage. You should also know how to reload PostgreSQL and inspect a Linux host.
PostgreSQL does not control all memory used by the database. The operating system decides whether memory allocations are allowed, whether inactive pages move to swap, and how much memory remains available for other processes. These decisions can turn a safe PostgreSQL configuration into an out-of-memory failure.
Linux overcommit
Linux overcommit controls whether the kernel promises more virtual memory than the machine can currently provide. Virtual memory is the address space that processes reserve before they necessarily use physical RAM.
The vm.overcommit_memory setting has three useful modes:
| Mode | Behavior | Typical concern |
|---|---|---|
| `0` | The kernel uses a heuristic estimate before accepting allocations. | Large shared allocations can be rejected unexpectedly. |
| `1` | The kernel allows most allocations without checking the full commitment. | The system may later invoke the OOM killer. |
| `2` | The kernel limits committed memory to a calculated amount. | You must reserve enough for PostgreSQL and the operating system. |
For a dedicated PostgreSQL server, strict overcommit is often a reasonable starting point:
sudo sysctl -w vm.overcommit_memory=2
sudo sysctl -w vm.overcommit_ratio=80The ratio allows committed memory up to roughly 80 percent of RAM, plus eligible swap. The remaining capacity gives the operating system and administrative processes room to operate. The correct value depends on other services, container limits, huge pages, and the amount of swap.
Make persistent changes in a system configuration file, such as /etc/sysctl.d/99-postgresql.conf, then load it:
vm.overcommit_memory = 2
vm.overcommit_ratio = 80sudo sysctl --system
sysctl vm.overcommit_memory vm.overcommit_ratioDo not apply these values blindly on a shared host. Measure the host's total memory commitment and account for monitoring agents, backups, connection pools, and other services.
Swap and cgroups
Swap is disk space used as an extension of RAM. It is much slower than RAM, but a small amount of swap can absorb a short-lived memory spike and prevent immediate process termination. Heavy swapping usually causes severe query latency.
Inspect memory pressure with:
free -h
vmstat 1
cat /proc/meminfo | grep -E 'MemAvailable|SwapFree|SwapTotal'A host with continuously increasing swap-in and swap-out activity needs less memory pressure, not merely a different PostgreSQL setting. Check connection counts, work_mem, parallel queries, maintenance jobs, and other applications.
In a container or virtual machine, use the memory limit supplied by the cgroup, not the physical RAM of the host. A PostgreSQL instance can be killed when its container reaches its limit even while the host has free memory.
How to tune safely
Change one operating-system setting at a time. Record the current value, workload, available memory, swap activity, and PostgreSQL errors. Test during representative traffic. Keep enough headroom for simultaneous operations because work_mem and maintenance memory are allocated by processes and operation nodes.
Exercise
A PostgreSQL container has 16 GB of RAM available, but the physical host has 64 GB. Which memory limit should guide overcommit and PostgreSQL budgeting?
Show answer
Use the container's 16 GB limit. The host's unused RAM cannot protect the container after its cgroup limit is reached.