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
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_memset 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_aggorstring_aggover millions of rows. - Too many connections for the machine's memory.
- A memory leak in an extension, or very large
maintenance_work_memduring index builds.
How to fix it
-
Read the memory context dump in the server log to see which context grew.
-
Lower
work_memglobally, 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';
-
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 ...;
-
Cap concurrency with a connection pooler, and on Linux set
vm.overcommit_memory = 2so allocations fail cleanly instead of triggering the OOM killer.
Related errors
53100disk full53300too many connections08006connection failure701SQL Server: Insufficient memory in resource poolORA-04031Oracle: unable to allocate bytes of shared memory
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.
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.