Out of memory
Symptom: server process (PID n) was terminated by signal 9: Killed, all connections dropping at once, or out of memory errors on a query.
Signal 9 means the operating system's OOM killer ended a Postgres process, and Postgres then restarts everything to be safe. The OS log (dmesg, journalctl -k) confirms it. It is not visible over SQL.
Why it happens
work_mem is a limit per sort or hash node, per query, per connection. A query with three such nodes on 200 connections can ask for 600 times work_mem.
Rough worst case: shared_buffers + connections × work_mem × nodes + autovacuum_max_workers × maintenance_work_mem. If that exceeds RAM, one busy hour can trigger the killer.
Confirm your settings
select name, setting, unit from pg_settings
where name in ('shared_buffers','work_mem','maintenance_work_mem','max_connections','hash_mem_multiplier','autovacuum_max_workers');
Fix
- Lower global
work_mem(4 to 16 MB is common) and raise it only for the roles or queries that need it:alter role reporting set work_mem = '256MB'; - Cut connection count with a pooler. Each backend also carries its own overhead.
- Keep
shared_buffersaround 25% of RAM on a dedicated server. - On Linux, set
vm.overcommit_memory = 2and enablehuge_pagesso allocation failures show up as errors on one query and not as a killed server. - Look for the query that got big:
pg_stat_statementssorted bytemp_blks_writtenandshared_blks_read.