PostgreSQL Connection Budgets: Pooling Without Hiding the Bottleneck
More application workers can exhaust database connections long before CPU is full. Budget the whole fleet, understand pool modes and test session behaviour before adding a pooler.

A service scales from two workers to twenty and suddenly reports too many connections. Each worker's pool still looks modest. The database sees the sum, plus background jobs, administration tools and everything else nobody included in the calculation.
Connection budgeting is a fleet-level decision. Pooling helps manage concurrency, but it does not make the database capable of executing unlimited work.
Count all the clients
Inventory application instances, processes per instance and maximum pool size per process. Add job workers, migrations, monitoring and approved administrative access. Include overlapping old and new instances during a rolling deployment.
A simple upper-bound calculation is useful: instances multiplied by processes multiplied by pool size, plus other clients. It is an upper bound because pools may open connections lazily, but deployment peaks can approach it.
Reserve capacity for operations and recovery. An application consuming every allowed connection can prevent an operator from inspecting the problem through ordinary access.
Measure active work
Inspect current sessions and their state using authorised database access:
SELECT application_name, state, count(*)
FROM pg_stat_activity
GROUP BY application_name, state
ORDER BY count(*) DESC;Visibility depends on your role and PostgreSQL version. Configure useful application names so the result identifies actual clients. Avoid publishing query text or connection details from private production sessions.
Idle sessions consume resources, while idle-in-transaction sessions can also hold transaction state and impede maintenance. Find why a transaction stays open rather than merely terminating it repeatedly.
Set a realistic concurrency budget
More connections can increase contention and memory use without increasing throughput. Measure query latency, CPU, I/O and lock waits under representative load before raising max_connections.
Each connection can execute work that allocates memory beyond its baseline. Multiplying a single-session memory setting by the potential number of concurrent operations helps explain why a generous configuration fails under load.
Keep connection acquisition time visible in application metrics. A request waiting for a pool slot is different from a query executing slowly. Both matter to the user, but they point to different fixes.
Choose pooling semantics carefully
PgBouncer is a maintained option with several pooling modes. Session pooling preserves a server connection for the client session. Transaction pooling reuses server connections between transactions, which can improve multiplexing but changes session assumptions.
- Session settings — Are they reliably applied per transaction?
- Temporary tables — Does the workflow depend on session continuity?
- Advisory locks — Are they session-scoped?
- Prepared statements — Does the deployed pooler version and configuration support them?
- LISTEN workflows — Does the client require a persistent session?
Support evolves, especially around prepared statements. Check the exact PgBouncer version and settings rather than repeating a blanket claim from an older guide.
Control queueing and timeouts
Set bounded connection acquisition and query timeouts appropriate to the workload. A web request should not wait indefinitely behind a stalled batch job. Separate interactive and long-running jobs where the architecture requires different limits.
Retries can amplify saturation. If every timed-out request immediately retries, the queue grows precisely when the database is least able to serve it. Use bounded retries only for failures the operation can safely recover from.
Transaction retries require idempotent application logic and a clear understanding of whether the earlier transaction committed. A generic network error is not proof that nothing happened.
Test through the actual pooler
Run migrations, normal reads, writes, rollback paths and any session-dependent features through the intended production route. A direct database test does not establish compatibility with transaction pooling.
Exercise a deployment overlap and a job backlog. Observe whether the pool absorbs bursts without unacceptable queue age, and whether administrative capacity remains available.
Monitor client connections, server connections, wait time and database execution health. A pooler can make connection counts look healthy while queries still overwhelm storage or locks.
Improve the work before adding capacity
Investigate slow queries, missing indexes, excessive round trips and transactions that remain open during external network calls. Reducing unnecessary work often improves throughput more than a larger connection limit.
Document the fleet budget and revisit it when worker counts change. The database sees the complete system, so your capacity plan must too.
Pooling is a way to allocate scarce execution access predictably. It succeeds when the application waits within a known budget, the database remains operable and session-dependent behaviour is tested rather than assumed.
Paj Digital Labs
The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.
Related reading

Restore Drills for a Whole Website: Rebuild the Service, Not Just the Database
A database restore can succeed while the website remains unusable. Test files, configuration, identity, DNS and external dependencies in an isolated recovery environment.

TLS Certificate Renewal: Verify the Served Certificate, Not Just the Cron Job
A successful renewal command does not prove visitors see the new certificate. Trace issuance, reloads, proxy termination and external monitoring before an expiry becomes an outage.

Disk Pressure on Linux: Inodes, Deleted Files and the Cleanup You Can Defend
Free bytes are only half the story. Diagnose inode exhaustion, open deleted files and runaway logs before deleting data that merely happens to be large.