Use it when connection counts hit limits or new connections are slow.
Database Connections and Pooling
Database connections are expensive server resources; pooling (application pools, PgBouncer, ProxySQL) reuses a smaller number of connections for many clients.
1. Current vs max: SELECT count(*) FROM pg_stat_activity; / SHOW STATUS LIKE 'Threads_connected';.
2. Idle connections per app.
3. Pool size × instances ≤ limit.
4. Pooler stats (SHOW POOLS; in PgBouncer).
5. Errors "too many connections".
app → pool → limited server connections → queries
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Watch connections during peak load. Look for idle in transaction sessions that hold connections and locks. Size the pool from database CPU and I/O capacity.
Step 2 Common failures Narrow the fault
Raising max_connections without memory planning can make the server swap. Many application instances each with a large pool exceed the server limit. Leaked connections (never returned) exhaust the pool.
Size pools so all app instances together stay under max_connections. PgBouncer transaction mode breaks session features (prepared statements, temp tables) unless supported.
SELECT count(*) FROM pg_stat_activity;— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
A pool keeps connections open and hands one to each request for its duration. Transaction-mode pooling in PgBouncer returns the connection after each transaction, allowing far more clients than server connections, but session state such as prepared statements may not carry over.
ReferenceImportant terms & boundaries3 sections · depth on demand
Pooling and limits
| Database | Limit | Pooler |
|---|---|---|
| PostgreSQL | max_connections (each is a process) | PgBouncer (transaction pooling), application pools |
| MySQL/MariaDB | max_connections (threads) | ProxySQL, application pools |
| SQL Server | Effectively high | ADO.NET/JDBC pools |
Check current use: SELECT count(*) FROM pg_stat_activity; or SHOW STATUS LIKE 'Threads_connected';. "Too many connections" usually means leaked connections or no pooling, not a need to raise the limit.
In depth
Why pooling matters
Opening a connection costs a TCP and TLS handshake plus authentication; in PostgreSQL each connection is also a separate backend process with its own memory. Many short requests that each open a connection spend more time connecting than querying, and thousands of idle connections use memory and slow snapshot handling.
PgBouncer pool modes
| Mode | Server connection returned to pool | Breaks |
|---|---|---|
session | When the client disconnects | Nothing; least saving |
transaction | At COMMIT/ROLLBACK | Session state: SET, advisory locks, LISTEN, temp tables; prepared statements unless supported by the PgBouncer version and driver |
statement | After each statement | Multi-statement transactions |
Sizing
Total connections = application instances × pool size per instance. Ten pods with a pool of 20 already make 200 connections. A useful starting point for the database side is a pool of a few connections per CPU core of the database server; more connections than the server can run in parallel only adds queueing and lock contention inside the database.
Worked check — who holds the connections?
SELECT usename, application_name, state, count(*)
FROM pg_stat_activity GROUP BY 1,2,3 ORDER BY 4 DESC;
-- many 'idle in transaction' → application keeps transactions open
SHOW STATUS LIKE 'Threads_connected'; SHOW VARIABLES LIKE 'max_connections'; -- MySQL
Set idle_in_transaction_session_timeout (PostgreSQL) or wait_timeout (MySQL) so leaked connections are closed, and pool timeouts on the client so callers fail fast instead of queuing forever.
Common mistakes
- No connection pooling, exhausting max connections.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.