Table of Contents
The Incident: Understanding PostgreSQL OOM Crashes
Why PostgreSQL OOM Crashes Happen
Top 11 Diagnostic Queries
Query 1: Active Backends by Runtime
Query 2: Your Session Memory Breakdown
Query 3: Parallel Query Workers Detection
Query 4: Current Parallel Execution Configuration
Query 5: Idle Connections by State
Query 6: Individual Long-Idle Connections
Query 7: Calculate Safe work_mem
Query 8: High-Memory Query Patterns
Query 9: Memory Health Summary
Query 10: Connections by Application
Query 11: Current Memory Configuration
Configuration Reference
OOM Prevention Settings
Role-Based work_mem Strategy
Session-Level Override
Core Principles
Get production‑grade database insights
Real incident breakdowns, tuning playbooks, and hard‑earned lessons from running databases at scale.
👉 Subscribe to get new posts directly in your inbox
👉 [The PostgreSQL Health Check That Prevents Sev-1s] Price: $29
60+ health indicators. One function call. No agents, no subscriptions, no data leaving your infrastructure
Incident: Understanding PostgreSQL OOM Crashes
Your monitoring shows a sudden drop in connections followed by a service restart. The system log reveals:
Out of memory: Kill process 12345 (postgres) score 950The Linux kernel terminated your database because PostgreSQL attempted to consume more RAM than available. This triggers crash recovery, causing 30 seconds to 20+ minutes of complete unavailability depending on WAL volume.
Important context: PostgreSQL memory consumption is dynamic and non-deterministic. The queries in this post rank risk and identify suspects. All memory figures represent PostgreSQL internal allocations, not OS-level RSS.
Why PostgreSQL OOM Crashes Happen
The primary cause is misconfigured work_mem combined with parallel query execution.
The work_mem multiplier effect:
work_memallocates per executor node, not globallyA single query with 3 hash joins, 2 sorts, and 4 parallel workers consumes:
work_mem x 5 operations x 4 workers = 20x work_memLinux memory overcommit allows allocation promises until physical RAM runs out
The OOM killer terminates the process with the highest memory badness score
Query 1: Active Backends by Runtime (Run First)
Purpose: Identify which active connections are running the longest and may be consuming memory.
Version: PostgreSQL 14+
Scope: ALL active backends on the server
🆕 The Sev-1 Database is now a platform
This newsletter is where I write about incidents. thesev1database.com is where I keep the fixes: 102 PostgreSQL error pages with the cause, the fix and the captured output, free to read. The 105 production runbooks (10 free end to end) and 37 engine lessons (5 free end to end) unlock with Pro, a one-time pass from $12 with a 14-day money-back guarantee.
📖 Go deeper on The Sev-1 Database
The reference pages behind this issue. Error pages, settings and the glossary are free, with no card and no signup.
Runbook: Cap parallel workers that multiply CPU and memory (scenario free; fix steps in Pro)
Lesson: work_mem and Spills: Where Sorts and Hashes Go to Disk (outline and sources free; full lesson in Pro)
Setting: work_mem (GUC reference (free))



