PostgreSQL "remaining connection slots are reserved": The Final Warning Before Outage
The relationship of reserved connection slots to max_connections, leveraging the superuser recovery window, and tracking down connection leaks.
The error string is a symptom, not the root cause. Do not restart the server blindly. Run the diagnostic queries below to pinpoint the actual failure mode.
▸Symptoms
FATAL: remaining connection slots are reserved for non-replication superuser connectionsThis message is the final recovery window before a complete connection lockout. The slots defined by superuser_reserved_connections (default: 3) are still available for administrative access.
▸Emergency Triage Steps
- 1Connect as Superuser immediately using
psql -U postgres. - 2Identify Top Consumers:
SELECT usename, application_name, client_addr, state, count(*)
FROM pg_stat_activity
GROUP BY 1, 2, 3, 4
ORDER BY 5 DESC;- 1Terminate Idle Sessions:
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'idle' AND pid <> pg_backend_pid();- 1Enforce Role Connection Limits:
ALTER ROLE app_user CONNECTION LIMIT 40;- 1Architectural Fix: Introduce PgBouncer in transaction mode and configure
idle_in_transaction_session_timeout.
Other Database Error Triage Playbooks
Other battle-tested diagnostic guides for high-severity database errors.
PostgreSQL too many clients already: 6-Step Resolution
Three root causes of FATAL: sorry, too many clients already, single diagnostic query to identify the cause, and permanent mitigation via max_connections and PgBouncer.
PostgreSQL password authentication failed: Root Cause and Fix
Four root causes of psql password authentication failed: incorrect password, md5/SCRAM mismatch, pg_hba.conf rule ordering, and port/cluster confusion. Step-by-step triage.
PostgreSQL connection refused: 8-Step Definitive Triage
Difference between connection refused, timeout, and no pg_hba entry; triage for listen_addresses, port conflicts, firewalls, and Docker container mappings.