Prompt
Why does my PostgreSQL app keep hitting connection limits?
Latest observation
Oct 7, 2026
OpenAI APIWeb search: off
PostgreSQL connection limits are usually hit because the app opens too many concurrent database sessions or doesn’t reuse them efficiently. Common causes:
Typical reasons
-
No connection pooling
- The app opens a new connection for each request/job and leaves it open.
- In web apps, traffic spikes can create many simultaneous connections quickly.
-
Connection leaks
- Connections are created but not closed/released back to the pool.
- This happens when errors/exceptions skip cleanup.
-
Pool too large
- Each app instance may have its own pool.
- If you run many app replicas, the total connections can exceed PostgreSQL’s
max_connections.
-
Long-running transactions
- Connections stay occupied while transactions are open.
- Even idle-in-transaction sessions count against the limit.
-
Background jobs / workers
- Cron jobs, queues, workers, and admin scripts may each open their own connections.
- These can pile up with the main app.
-
Serverless or autoscaling environments
- Many short-lived instances can create bursts of connections.
- This is a very common cause in cloud setups.
-
Monitoring or admin tools
- Some tools open persistent connections and add to the total.
How to check
Run:
SELECT state, count(*)
FROM pg_stat_activity
GROUP BY state;
To find the biggest consumers:
SELECT usename, application_name, client_addr, state, count(*)
FROM pg_stat_activity
GROUP BY 1,2,3,4
ORDER BY count(*) DESC;
To identify idle transactions:
SELECT pid, usename, state, query, xact_start, state_change
FROM pg_stat_activity
WHERE state = 'idle in transaction';
What to do
- Use a connection pool and ensure connections are always returned.
- Limit pool size per app instance.
- Add cleanup in
finallyblocks / defer / context managers. - Avoid holding transactions open longer than needed.
- Consider PgBouncer if you have many app instances or serverless workloads.
- Lower application pool sizes if you have many replicas.
Rule of thumb
If your app instances × pool size is greater than PostgreSQL’s allowed connections, you’ll likely hit the limit.
If you want, I can help you diagnose this from:
- your app framework/language,
- your
pg_stat_activity, - or your pool config.
Brands