how to determine max_client_conn for pgbouncer

https://pgbouncer.github.io/config.html

max_client_conn Maximum number of client connections allowed.

default_pool_size How many server connections to allow per user/database pair.

so max_client_conn should be way larger then postgres max_connections, otherwise why you use connection pooler at all?..

If you have 20 databases and set default_pool_size to 20, you will allow pgbouncer to open 400 connections to db, so you need to adjust posgtres.conf max_connections to 400 and set pgbouncer max_client_conn to smth like 4000 (to have average 10 connections in pool for each actual db connection)

This answer is only meant to provide an example for understanding the settings, not as a statement to literally follow. (eg I just saw a config with:

max_client_conn = 10000
default_pool_size = 100
max_db_connections = 100
max_user_connections = 100

for cluster with two databases and max_connections set to 100). Here the logic is different, also mind max_db_connections is set and in fact connection limits are set individually per database in pgbouncer [database] section.

So - play with small settings to get the idea of how config influence each other - this is "how to determine max_client_conn for pgbouncer" the best


Like almost everyone, then you are setting your pool size way to high. Don't let your postgresql server do the connection pooling. If you do then it severely hurts your performance.

The optimal setting for how many concurrent connection to postgresql is

connections = ((core_count * 2) + effective_spindle_count)

That means that if you are running your database on a 2 core server, then your total pool size from pgbouncer should be no more than 5. Pgbouncer is a lot better at handling pooling than postgresql, so let it do that.

So, leave the max_connections in my postgresql.conf to it's default 100 (no reason to change as it is a max. Also this should always be higher than what your application needs as some logging, admin and backup processes needs connections as well)

And in your pgbouncer.ini file set

max_db_connections=5
default_pool_size=5
max_client_conn=400

For more information https://www.percona.com/blog/2018/06/27/scaling-postgresql-with-pgbouncer-you-may-need-a-connection-pooler-sooner-than-you-expect/