PostgreSQL refuses new clients with 'too many connections' after scaling Node workers
Environment
Ubuntu 22.04 hosts running Node 16 app in Docker, PostgreSQL 13 on separate VM, no connection pooler currently configured. Peak concurrent workers multiplied during load test.
Symptoms
App logs show connection errors and timeouts; PostgreSQL logs show repeated 'FATAL: remaining connection slots are reserved for non-replication superuser connections' and accept stops.
Observations
SELECT count(*) FROM pg_stat_activity; shows connections near max_connections (100). No PgBouncer or pooling; ephemeral connections created per request. No long idle_in_transaction backends seen in first glance.
Already attempted
Restarted application processes and PostgreSQL service to temporarily relieve connections; errors reappear under load. Considered raising max_connections in postgresql.conf but did not change.
Diagnostic steps
- On Postgres host, run: sudo -u postgres psql -c "SELECT count(*) AS total, state, usename, client_addr, query_start, now()-query_start AS duration FROM pg_stat_activity GROUP BY state, usename, client_addr, query_start ORDER BY total DESC;"
- What the result means: If many active/idle connections from same app user or host spike during load, it's concurrency. If many connections stuck in 'idle in transaction' with long durations, application transactions leak or mis-handle commits.
- Check current max and active: sudo -u postgres psql -c "SHOW max_connections;" and compare to count from pg_stat_activity.
- What the result means: If active connections routinely approach max_connections, either reduce app concurrency per instance or increase server capacity. Raising max_connections without RAM planning may cause OOM.
- From app side, search for connection acquisition/release patterns or absence of pooling: review code and environment for any pooling library (e.g., pg Pool, PgBouncer) and inspect connection open/close logs.
- What the result means: If code opens new client per request and does not close it, you have a leak. Presence of pooling suggests misconfiguration (pool too large or not reused across workers).
- Use lsof or ss on DB host to inspect sockets: sudo ss -tnp | grep 5432 or sudo lsof -i :5432
- What the result means: If many sockets from numerous source ports or hosts exist, you can correlate with containers/hosts creating many ephemeral connections.
- If safe, simulate a controlled load while monitoring pg_stat_activity to see whether connections accumulate beyond expected and whether they return to idle or release after requests complete.
- What the result means: If connections remain after requests finish, likely leak; if they track concurrency and drop, it's pooling/scale sizing issue.
Confirmed fixes
No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.
Question
Should I increase max_connections, add a pooler, or find leaks? What safe diagnostics identify whether connections are leaked or simply too many concurrent clients?
Verified community troubleshooting
Start by confirming connection counts from Postgres: sudo -u postgres psql -c "SELECT count(*) FROM pg_stat_activity;" and inspect states and ages to see if connections linger. If connections track request concurrency and drop after requests, the app needs pooling (PgBouncer or app-level pool). If many 'idle in transaction' entries appear, search code paths for missing COMMIT/ROLLBACK. Before changing max_connections, estimate per-backend memory and verify host RAM; increasing limits without capacity planning can cause instability. Implement pooling in staging and test a controlled ramp to verify fewer DB backends while maintaining throughput.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags