PgBouncer connection pooler in a PostgreSQL cluster
In PostgreSQL, a separate process is created to handle each client connection. The greater the number of connections, the more processes that use RAM. The maximum number of connections to the PostgreSQL process is determined by the max_connections parameter.
To optimize resource consumption, you can use a connection pooler. Clients connect not directly to PostgreSQL, but to the connection pooler. In this case, a small number of connections is maintained between the pooler and the PostgreSQL server — the pooler creates a new connection or reuses an existing one. The number of connections between the pooler and the database on each of the cluster nodes is determined by the pool size (the pool_size parameter).
Cloud Databases PostgreSQL clusters use the PgBouncer connection pooler. There are three PgBouncer pooling modes available. Learn more about the connection pooler in the documentation for PgBouncer.
Maximum number of connections to the PostgreSQL process (max_connections)
The PostgreSQL max_connections DBMS parameter determines the maximum number of concurrent connections to the PostgreSQL process. By default, it is set to 100 connections.
You can change the parameter value in the DBMS settings in the control panel to the maximum number of connections under load. Keep in mind that each connection consumes RAM resources.
Changing the max_connections value results in restarting the cluster nodes of PostgreSQL.

Pool size (pool_size)
The pool size (the pool_size parameter) is the maximum number of connections between the connection pooler and each PostgreSQL database on each of the cluster nodes.
You can select the pool size when creating a cluster and change the size in an existing cluster. Available values are from 1 to 500.
Pooling modes
The pooling mode is a client connection strategy to PostgreSQL. Cloud Databases PostgreSQL clusters use the PgBouncer connection pooler. Learn more about the connection pooler in the documentation for PgBouncer.
PgBouncer supports three modes:
By default, Cloud Databases uses the transaction mode.
You can select the pooling mode when creating a cluster and change the mode in an existing cluster.
Transaction mode (transaction)
The connection to PostgreSQL is maintained until the transaction is completed. When the transaction finishes, the pooler returns the connection to the pool. Later, this connection can be reused by the same client for other connections or by another client.
The total number of client connections to PgBouncer can reach 10,000, but the number of active transactions is determined by the pool size. For example, if the pool size is 30, there will be 30 active transactions.
The number of connections between the connection pooler and each PostgreSQL database on each of the cluster nodes is also determined by the pool size.
A client can run multiple transactions simultaneously on different connections. At the same time, each connection between the connection pooler and the PostgreSQL server can execute transactions from different clients throughout its lifecycle.
Transaction mode reduces the load on DBMS resources when there are many low-load client connections.

Limitations of transaction mode
Transaction mode breaks certain PostgreSQL mechanisms. Choose another mode if clients use these options. Some connection flags may be distributed across different clients, which can lead to unpredictable behavior and incorrect results.
The following do not work in transaction mode:
- SET/RESET and LISTEN/NOTIFY commands;
- WITH HOLD CURSOR;
- PRESERVE/DELETE ROWS in temporary tables;
- prepared statements: protocol-level prepared plans, PREPARE, DEALLOCATE;
- the LOAD statement;
- Session-level advisory locks.
Learn more about incompatible options in the PgBouncer documentation.
Session mode (session)
In session mode, a client can continue sending queries as long as the session lasts — the connection between the connection pooler and the PostgreSQL server will be maintained until the client disconnects from the database.
The number of connections between the connection pooler and the PostgreSQL server is determined by the pool size. For each client connection, a connection between the pooler and the PostgreSQL server is used. The connection is returned to the pool and can be reused only after the previous client disconnects from the database.
Unlike the transaction mode, this mode is safe, mirrors a direct connection to PostgreSQL, supports all mechanisms, and is suitable for all PostgreSQL clients. When using this mode, resource load is not reduced.
This connection mode is useful for clients with many short-lived database connections because it increases the connection speed to the DBMS.

Statement mode (statement)
The pooler will return the connection to the pool as soon as the first query is processed — multi-statement transactions will be aborted, and the pooler will return an error.
This mode allows using more client connections than transaction mode. It is suitable if you know that each transaction is limited to a single query (AUTOCOMMIT is enabled).

Change pooling mode
If you select the transaction mode (transaction), review the limitations of this mode.
- In the Control Panel, in the top menu, click Products and select Cloud Databases.
- Open the tab Active.
- Open the cluster page → tab Settings.
- In the Connection pooler section, click Edit and select a pooling mode.
- Click Save.
Change pool size
- In the Control Panel, in the top menu, click Products and select Cloud Databases.
- Open the tab Active.
- Open the cluster page → tab Settings.
- In the Connection pooler section, click Edit and change the pool size.
- Click Save.