FULL AI ANALYSIS

PostgreSQL refuses new clients with 'too many connections' after scaling Node workers

Generated Nov 2, 2026, 9:26:30 AM

Analysis summary

Error appears when number of concurrent client connections exceeds server limit. Likely causes: no connection pooling, connection leak (not closed), very high concurrency, or too-low max_connections relative to workload and resources.

Possible causes

No connection pooling: every request opens a new DB connection and overwhelms server under concurrency.

90%

Application connection leak where sockets are not released back to pool or closed.

60%

max_connections set too low for combined app and background jobs.

50%

Idle transactions hold connections (long-running transactions, autovacuum conflicts).

40%

Suggested fixes

1

Diagnostic step 1

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;" Result meaning: 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.

2

Diagnostic step 2

Check current max and active: sudo -u postgres psql -c "SHOW max_connections;" and compare to count from pg_stat_activity. Result meaning: 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.

3

Diagnostic step 3

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. Result meaning: 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).

4

Diagnostic step 4

Use lsof or ss on DB host to inspect sockets: sudo ss -tnp | grep 5432 or sudo lsof -i :5432 Result meaning: If many sockets from numerous source ports or hosts exist, you can correlate with containers/hosts creating many ephemeral connections.

5

Diagnostic step 5

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. Result meaning: If connections remain after requests finish, likely leak; if they track concurrency and drop, it's pooling/scale sizing issue.