Sub topic 9
Sizing Pools and Connection Limits
This chapter assumes that you know basic SQL, PostgreSQL roles, max_connections, the psql client, and basic Linux configuration. The goal is to limit PostgreSQL server connections without preventing applications from accepting client requests.
Two different kinds of connections
PgBouncer separates application connections from PostgreSQL connections:
- A client connection is a socket from an application to PgBouncer.
- A server connection is a socket from PgBouncer to PostgreSQL.
max_client_connlimits client connections accepted by PgBouncer.default_pool_sizelimits server connections in each user/database pool.
A pool is usually identified by a database and user pair. For example, connections from app_user to appdb use a different pool from connections from report_user to appdb.
This means default_pool_size = 20 does not necessarily limit all PostgreSQL connections to 20. Several pools can each create up to 20 server connections.
Start with PostgreSQL's limit
PostgreSQL's max_connections is a ceiling for backend connections. Leave room for:
- Administrative access.
- Monitoring and maintenance jobs.
- Other applications or direct connections.
- PostgreSQL's reserved superuser slots.
If PostgreSQL has max_connections = 200, do not automatically configure PgBouncer to use 200 application connections. A safer initial budget might be 120 to 160 server connections, depending on the rest of the system.
For multiple pools, estimate the maximum possible usage:
pool budget = sum of pool sizes + reserved operational connections
For example, two application pools of 20 connections and one reporting pool of 10 connections have a possible total of 50 connections if each pool also has a reserve of 5. Compare that total with the PostgreSQL connection budget, not only with the number of application clients.
Configure client and server limits
A common starting point is to allow many waiting clients while keeping the PostgreSQL side small:
[pgbouncer]
max_client_conn = 1000
default_pool_size = 20
min_pool_size = 5
reserve_pool_size = 5
reserve_pool_timeout = 3
max_db_connections = 80
max_user_connections = 60max_client_conn consumes PgBouncer operating-system resources, so set it according to expected traffic and file-descriptor limits. It does not create 1,000 PostgreSQL connections.
max_db_connections caps server connections for a database across its user pools. max_user_connections caps connections for a user across databases. These settings provide a second safety boundary when several pools share the same database or role.
Start with a small pool and increase it only when measurements show requests waiting for a server connection. A larger pool can increase concurrency, but it can also cause PostgreSQL CPU, memory, lock, or I/O contention.
Use the reserve pool carefully
The reserve pool handles brief bursts. A client waits for reserve_pool_timeout seconds before PgBouncer opens reserve connections, up to reserve_pool_size. It is not a replacement for normal capacity planning.
If clients frequently use reserve connections, inspect query latency and transaction duration. The underlying problem may be slow queries or transactions that remain open too long.
Exercise
PostgreSQL allows 150 connections, and you reserve 30 for administration and other services. Your application has three pools sized at 20, 20, and 10 connections. What is the planned application usage, and how much budget remains?
Show answer
The planned usage is 50 server connections. The application budget is 120, so 70 connections remain available for growth or additional pools.