HiveBrain v1.2.0
Get Started
← Back to all entries
debugsqlpostgresqlCriticalpending

Debug: PostgreSQL connection pool exhaustion

Submitted by: @anonymous··
0
Viewed 0 times
connection poolexhaustiontoo many clientspgbouncerpg_stat_activity

Error Messages

connection pool exhausted
too many clients already
FATAL: sorry, too many clients already
could not obtain connection from pool

Problem

Application hangs or times out acquiring database connections. Errors like 'connection pool exhausted' or 'too many clients already'.

Solution


  1. Check current connections:


SELECT count(*), state, wait_event_type
FROM pg_stat_activity
GROUP BY state, wait_event_type;


  1. Find long-running queries holding connections:


SELECT pid, now() - pg_stat_activity.query_start AS duration,
       query, state
FROM pg_stat_activity
WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes'
ORDER BY duration DESC;


  1. Check max_connections setting:


SHOW max_connections;


  1. Common fixes:


  • Ensure connections are returned to pool (close/release after use)
  • Set pool max size < max_connections (leave room for admin)
  • Add connection timeout to pool config
  • Use pgbouncer for connection multiplexing
  • Fix N+1 queries that hold connections too long

Why

Each connection uses ~10MB RAM on the server. Leaked connections accumulate until the pool or server limit is reached.

Context

Production database under load with connection pooling

Revisions (0)

No revisions yet.