out of memory

SQLSTATE 53200 condition out_of_memory class 53 — Insufficient Resources severity ERROR
Reproduced & verified on PostgreSQL 14.23, 15.18, 16.14, 17.10 and 18.4 — identical message on every version.
Last reviewed 30 May 2025 · Reproduced live with the SQL on this page.

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.

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

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

  1. Lower a too-aggressive work_mem, or set it per-session for big queries only.
  2. Reduce result size with filtering/pagination; avoid materializing huge sets client-side.
  3. 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”.