Sub topic 2
What connection pooling does
Assume you know basic SQL, PostgreSQL roles, psql, and the purpose of max_connections. This chapter explains the working model of a connection pool and why PgBouncer can reduce database pressure.
Why PostgreSQL connections cost resources
A PostgreSQL client connection is a long-lived TCP connection between an application and the database. PostgreSQL also starts a dedicated server process for that client. The process needs memory for connection state, session settings, query work, and bookkeeping.
An application may have hundreds of worker threads, web requests, or short-lived jobs. If each one opens its own PostgreSQL connection, the database can spend more resources managing connections than executing queries. Idle connections still consume resources.
Creating a connection also adds work. The client and server establish TCP communication, authenticate the user, select a database, and initialize session state. Repeating this work for every request increases latency.
What a connection pool does
A connection pool keeps a reusable set of database connections. An application checks out a connection, uses it, and returns it to the pool. The next operation can reuse that connection instead of creating another one.
PgBouncer is a lightweight pooler that sits between clients and PostgreSQL. Clients connect to PgBouncer. PgBouncer maintains a smaller number of server connections to PostgreSQL and assigns them to clients when needed.
For example, 100 application clients might connect to PgBouncer while PgBouncer maintains only 20 active PostgreSQL connections. If most clients are idle or waiting between transactions, those 20 connections can serve the workload efficiently.
| Connection | Direct connection | Through PgBouncer |
|---|---|---|
| Application-facing connections | Limited by PostgreSQL | Can be many |
| PostgreSQL server connections | One per client | Reused by clients |
| Connection setup per request | Often repeated | Usually avoided |
The pool does not make a single query execute faster. It reduces connection-management overhead and limits concurrent database work. This often improves stability when traffic contains many idle clients or short transactions.
Client connections and server connections
PgBouncer tracks two different sides:
- A client connection is an application’s connection to PgBouncer.
- A server connection is PgBouncer’s connection to PostgreSQL.
- A pool groups server connections for a particular database and user, depending on configuration.
- A client may wait when every suitable server connection is busy.
The relationship depends on the pool mode. In session pooling, a client keeps one server connection until it disconnects. In transaction pooling, PgBouncer returns the server connection to the pool after each transaction, allowing another client to use it.
A typical application changes only its destination port:
# PostgreSQL directly
psql "postgresql://[email protected]:5432/appdb"
# The same database through PgBouncer
psql "postgresql://[email protected]:6432/appdb"The application still speaks the PostgreSQL wire protocol. PgBouncer handles the connection reuse.
Exercise
An application has 500 mostly idle clients, but only 20 clients run short transactions at any moment. Why can PgBouncer help?
Show answer
PgBouncer can accept the many client connections while reusing a smaller set of PostgreSQL server connections, reducing PostgreSQL process and connection overhead.