// PostgreSQL error reference

PostgreSQL 53200: out of memory

A server process couldn't allocate memory for the query. Usually a huge sort or hash, a very large result built in memory, or too many busy connections.

SQLSTATE
53200
Name
out_of_memory
Category
Insufficient Resources
Error message
out of memory
DETAIL:  Failed on request of size 1048576 in memory context "ExecutorState".

What it means

PostgreSQL allocates memory in contexts. When the operating system refuses an allocation, the statement fails with 53200 and the DETAIL line names the request size and memory context. The server log gets a dump of memory context sizes that shows where the memory went.

On Linux with memory overcommit, the kernel's OOM killer may instead kill a PostgreSQL process outright; the server then restarts all sessions, and clients see their connections drop.

Common causes

  • work_mem set high and multiplied by many sort and hash nodes, in many sessions at once.
  • A query returning or aggregating a huge amount of data in one value, such as array_agg or string_agg over millions of rows.
  • Too many connections for the machine's memory.
  • A memory leak in an extension, or very large maintenance_work_mem during index builds.

How to fix it

  1. Read the memory context dump in the server log to see which context grew.

  2. Lower work_mem globally, and raise it only for the session that needs it.

    ALTER SYSTEM SET work_mem = '16MB';
    SELECT pg_reload_conf();
    -- for one heavy report:
    SET work_mem = '256MB';
  3. Check the plan for hash joins and sorts over far more rows than expected, and fix the estimates or add an index.

    EXPLAIN (ANALYZE, BUFFERS) SELECT ...;
  4. Cap concurrency with a connection pooler, and on Linux set vm.overcommit_memory = 2 so allocations fail cleanly instead of triggering the OOM killer.

Related errors

The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.

// fewer errors, faster fixes

Work on PostgreSQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.