All glossary terms

Connection pooling

What is connection pooling in PostgreSQL?

Connection pooling keeps a set of database connections open and shares them among many clients, because each PostgreSQL connection is served by its own server process.

PostgreSQL uses a process-per-user model: every client connection is served by its own backend process on the server, and the max_connections setting caps how many connections can exist at once. A connection pool sits between the application and the database, keeps a limited number of server connections open and hands them out to clients as needed.

The pool can live inside the application (most drivers and ORMs include one) or in a separate pooler in front of the server, such as PgBouncer. In transaction-level pooling, a server connection goes back to the pool after each transaction, so the next transaction may run on a different connection. Anything that depends on session state, such as session-level SET commands or temporary tables, needs care in that mode.

Example

The example shows the server's connection limit and counts the current client connections by state. Many idle connections are a common sign that a pool would help.

SHOW max_connections;

SELECT state, count(*)
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY state;

Plain SQL for stock PostgreSQL 16 or 17, no extensions needed. Run it in an empty database you can throw away.

Sources

Official PostgreSQL documentation (in English).

Related terms