Diagnostic Queries
Symptoms
A backend could not allocate the memory it needed for an operation. PostgreSQL raises SQLSTATE 53200 (out_of_memory) and usually logs a memory-context dump showing where memory went.
- Often during large sorts, hash joins, or huge result materialization.
- The log includes a TopMemoryContext breakdown.
- May be a single oversized query rather than a global shortage.
What the server log shows
ERROR: out of memory
DETAIL: Failed on request of size 1073741824 in memory context "ExecutorState".
Why PostgreSQL raises this — what the manual says
Section 19.4.1 Memory (work_mem):
“Sets the base maximum amount of memory to be used by a query operation (such as a sort or hash table) before writing to temporary disk files.”
Each backend allocates from process memory governed partly by work_mem per operation. A query that demands more than the OS can give (or an over-large work_mem multiplied across operations) exhausts memory and aborts with 53200.
Common causes
- A single query with a huge sort, hash join, or result set.
- An over-large
work_memmultiplied across many concurrent operations. - OS memory pressure, tight cgroup limits, or disabled overcommit.
Relevant GUC parameters
| Parameter | Default | Effect |
|---|---|---|
work_mem |
4MB | Memory per sort/hash operation before spilling to disk. |
maintenance_work_mem |
64MB | Memory for VACUUM, CREATE INDEX, and similar. |
hash_mem_multiplier |
2.0 | Multiplies work_mem for hash-based operations (PG15+). |
How to fix it
- Lower a too-aggressive
work_mem, or set it per-session for big queries only. - Reduce result size with filtering/pagination; avoid materializing huge sets client-side.
- Check OS memory and overcommit settings; ensure swap and limits are sane.
Diagnostic query
-- Inspect current memory-related settings
SELECT name, setting, unit FROM pg_settings
WHERE name IN ('work_mem','maintenance_work_mem','hash_mem_multiplier','shared_buffers');
Related & next steps
Reference: PostgreSQL 18 Section 20.4 “Resource Consumption”.
Thanks — noted. This helps keep the database accurate.