Database connection pooling explained: why apps fall over at scale
Every PostgreSQL connection costs a backend process. What a pooler does, session vs transaction vs statement modes, and the failure modes to test.
An application that behaves perfectly in development can fall over in production with one blunt error: "too many connections". The cause is usually architectural, not a bug. Every client connection to PostgreSQL costs the database real resources, and the default instinct - let every worker, thread or serverless function open its own connection - does not survive contact with production concurrency. Connection pooling is the standard fix.
This guide explains why connections are expensive, what a pooler does, and the behavior differences that matter before you point an app at one. It follows the PgBouncer and PostgreSQL documentation, checked on 19 November 2026. Pooler configuration values for your workload should come from your own load tests, not from this article.
Why PostgreSQL connections are expensive
PostgreSQL uses a process-per-connection model: each client connection is served by its own backend process, with its own memory. The PostgreSQL documentation's connection and authentication settings exist precisely because the number of concurrent connections is a limited resource with a configured ceiling. Hundreds of mostly-idle connections still hold backend processes open, and every new connection pays a startup cost - process creation, authentication, and session setup - before a single query runs.
Two common architectures make this worse. Horizontally scaled app servers multiply connections by instance count, and scale-to-zero platforms (containers, functions) churn connections constantly, so the database spends its effort opening and closing sessions instead of answering queries.
What a pooler actually does
A connection pooler sits between your application and the database. Applications open as many client connections to the pooler as they like; the pooler maintains a small, bounded set of real server connections to PostgreSQL and assigns them to client work as needed. From the database's perspective, a thousand app connections become a few dozen long-lived ones.
The three pooling modes, and why the choice changes app behavior
PgBouncer's documentation defines three modes, and they are not interchangeable:
- Session pooling. A server connection is assigned to a client for the life of the client session. This is the most conservative mode - everything that works in a direct connection keeps working - but it saves the least, because connections are still held while clients idle.
- Transaction pooling. A server connection is assigned to a client only for the duration of a transaction, then returned to the pool. Idle-time waste disappears, which is why this is the common production choice. The trade-off: anything session-scoped - session-level settings, advisory locks, temporary tables that outlive a transaction - cannot be relied on. Prepared statements historically broke in this mode; PgBouncer supports named prepared statement tracking (the
max_prepared_statementssetting) so clients can keep using them, with the pooler tracking and re-creating them on the server side. - Statement pooling. The most aggressive mode: the server connection returns to the pool as soon as a query completes. Multi-statement transactions are disallowed because they would break. Few real applications can use it.
Application-level pools vs an external pooler
Most frameworks have a built-in pool, and it is worth having - it bounds connections per app instance and reuses them. It does not solve the multiplication problem: twenty app instances with a pool of ten each is still two hundred database connections. An external pooler bounds the total at the database edge. Larger deployments commonly run both: a small in-app pool for locality, an external pooler as the hard ceiling.
Sizing and failure modes to test
- Pool size is a throttle, not a target. The right server-pool size is related to what your database can execute concurrently, not to how many clients exist. Oversizing the pool re-creates the problem inside the pooler.
- Queued clients need timeouts. When the pool is exhausted, clients wait. Decide how long they may wait, and what error the user sees, instead of letting requests hang indefinitely.
- Test transaction-mode surprises. If you adopt transaction pooling, exercise the flows that use session state - the ones that fail tend to fail only under the pooler, not in development.
- Watch the pooler's own stats. PgBouncer exposes pooled transaction counts, average transaction duration and wait-related statistics; they are the difference between "the database is slow" and "clients are queueing for a connection".
Pooling sits next to the rest of database hygiene, not in place of it. For the query side, see how indexes fix slow queries and reading a query plan without guesswork; for correctness under concurrency, PostgreSQL isolation levels; and for recovery, logical backups vs point-in-time recovery.
Sources
- PgBouncer documentation, usage and pooling modes: https://www.pgbouncer.org/usage.html
- PostgreSQL 18 documentation, 19.3 Connections and Authentication: https://www.postgresql.org/docs/current/runtime-config-connection.html
Drafted with AI assistance from these sources. Pool sizes and modes depend on your schema, framework and workload; verify with your own load tests before changing production settings.