Thread: max_connections parameter: too_many_connections error

max_connections parameter: too_many_connections error

From
Shital A
Date:

Hello,

We are working on a payments systems which uses postgreSql 9.6 as backend DB and blockchain technology. The database is setup in HA in master-standby mode using pacemaker on Linux 7.6.

We are new to postgres and need help in deciding how to set value for max_connections on DB.

1. How can we decide on optimal value for max_connections for a given setup/server? I checked many posts saying that even 1000 is considered as a very high value but we are hitting the error too_many_connections due to Max_connections value limit.

2. We usee hikari pool on the client side but that even has limitations. Implementing pg_bouncer is another suggestion in the posts. In a HA setup, do we have to set it up on both primary standby?

Please provide your advice/suggestions.

Thanks.

RE: max_connections parameter: too_many_connections error

From
Scot Kreienkamp
Date:

1. How can we decide on optimal value for max_connections for a given setup/server? I checked many posts saying that even 1000 is considered as a very high value but we are hitting the error too_many_connections due to Max_connections value limit.

 

I have one set at 1000 but I usually top out around 500 right now.  As long as you have the CPU and memory to handle whatever you’ve set I would think it’s ok.  Hopefully others with more experience with big hardware will chime in.

 

2. We usee hikari pool on the client side but that even has limitations. Implementing pg_bouncer is another suggestion in the posts. In a HA setup, do we have to set it up on both primary standby?

 

Client side pooling is a different proposition than server side.  Each server maintaining its own pool VS the server maintaining a common pool, the server side pooling would lower your connection count.  The value of pooling also depends on your app.  If it’s maintaining persistent connections for hours at a time then any kind of pooling is going to be of limited value depending on how the pooler is operating, IE connection pooling VS transactional pooling.    If your transactions are simple you could use transactional pooling which could greatly reduce the connection count. 

 

For an HA setup you would need to set it up on both PG nodes.  If you’re using the standby for read-only queries then it would have to be running on both nodes at all times and pacemaker would start/stop it as part of the failover process.  If you’re only using the active node then pacemaker would have to start the pooler on the active node and stop it on the passive node, and again control it on both nodes for failover.  If it’s that large of a setup you may want to make the pooler its own cluster with pacemaker to relieve the PG cluster of the additional load. 

Scot Kreienkamp | Senior Systems Engineer | La-Z-Boy Corporate
One La-Z-Boy Drive | Monroe, Michigan 48162 | ( 734-384-6403 | |  ) 7349151444  | * Scot.Kreienkamp@la-z-boy.com
www.la-z-boy.com  | facebook.com/lazboy  | twitter.com/lazboy | youtube.com/lazboy

This message is intended only for the individual or entity to which it is addressed. It may contain privileged, confidential information which is exempt from disclosure under applicable laws. If you are not the intended recipient, you are strictly prohibited from disseminating or distributing this information (other than to the intended recipient) or copying this information. If you have received this communication in error, please notify us immediately by e-mail or by telephone at the above number. Thank you.

Attachment

RE: max_connections parameter: too_many_connections error

From
Scot Kreienkamp
Date:

1. How can we decide on optimal value for max_connections for a given setup/server? I checked many posts saying that even 1000 is considered as a very high value but we are hitting the error too_many_connections due to Max_connections value limit.

 

I have one set at 1000 but I usually top out around 500 right now.  As long as you have the CPU and memory to handle whatever you’ve set I would think it’s ok.  Hopefully others with more experience with big hardware will chime in.

 

2. We usee hikari pool on the client side but that even has limitations. Implementing pg_bouncer is another suggestion in the posts. In a HA setup, do we have to set it up on both primary standby?

 

Client side pooling is a different proposition than server side.  Each server maintaining its own pool VS the server maintaining a common pool, the server side pooling would lower your connection count.  The value of pooling also depends on your app.  If it’s maintaining persistent connections for hours at a time then any kind of pooling is going to be of limited value depending on how the pooler is operating, IE connection pooling VS transactional pooling.    If your transactions are simple you could use transactional pooling which could greatly reduce the connection count. 

 

For an HA setup you would need to set it up on both PG nodes.  If you’re using the standby for read-only queries then it would have to be running on both nodes at all times and pacemaker would start/stop it as part of the failover process.  If you’re only using the active node then pacemaker would have to start the pooler on the active node and stop it on the passive node, and again control it on both nodes for failover.  If it’s that large of a setup you may want to make the pooler its own cluster with pacemaker to relieve the PG cluster of the additional load. 

Scot Kreienkamp | Senior Systems Engineer | La-Z-Boy Corporate
One La-Z-Boy Drive | Monroe, Michigan 48162 | ( 734-384-6403 | |  ) 7349151444  | * Scot.Kreienkamp@la-z-boy.com
www.la-z-boy.com  | facebook.com/lazboy  | twitter.com/lazboy | youtube.com/lazboy

This message is intended only for the individual or entity to which it is addressed. It may contain privileged, confidential information which is exempt from disclosure under applicable laws. If you are not the intended recipient, you are strictly prohibited from disseminating or distributing this information (other than to the intended recipient) or copying this information. If you have received this communication in error, please notify us immediately by e-mail or by telephone at the above number. Thank you.

Re: max_connections parameter: too_many_connections error

From
Rui DeSousa
Date:


On Sep 17, 2019, at 8:43 AM, Shital A <brightuser2019@gmail.com> wrote:

Hello,

We are working on a payments systems which uses postgreSql 9.6 as backend DB and blockchain technology. The database is setup in HA in master-standby mode using pacemaker on Linux 7.6.

We are new to postgres and need help in deciding how to set value for max_connections on DB.

1. How can we decide on optimal value for max_connections for a given setup/server? I checked many posts saying that even 1000 is considered as a very high value but we are hitting the error too_many_connections due to Max_connections value limit.

I’d say 2x the number you expect to have connect to the database as you can not change the value without bouncing the service and downtime is usually hard to come by.  Just set the monitoring solution at 50%.   That’s what I currently do; using a max connection of 2000 with an average connection rate of around 800.  That sounds like a lot but with 96 cores that’s only around 9 connections per core.  

2. We usee hikari pool on the client side but that even has limitations. Implementing pg_bouncer is another suggestion in the posts. In a HA setup, do we have to set it up on both primary standby?

Take a look at pg_bouncer.   The problem with a high number of connections is that the idle connections will wake up from poll() and spin on the cpu.  I’m in the process of testing pg_bouncer to reduce the number PostgreSQL connections to resolve the high server load due to idle sessions.  pg_bouncer has proven to be effective as it doesn’t suffer from the idle poll() issue and it has reduced the server load due to idle sessions. 

I have been wondering if PostgreSQL used kqueues instead of poll if it would allow the server to scale better without having to introduce pg_bouncer.

Re: max_connections parameter: too_many_connections error

From
Rui DeSousa
Date:


On Sep 17, 2019, at 8:43 AM, Shital A <brightuser2019@gmail.com> wrote:

Hello,

We are working on a payments systems which uses postgreSql 9.6 as backend DB and blockchain technology. The database is setup in HA in master-standby mode using pacemaker on Linux 7.6.

We are new to postgres and need help in deciding how to set value for max_connections on DB.

1. How can we decide on optimal value for max_connections for a given setup/server? I checked many posts saying that even 1000 is considered as a very high value but we are hitting the error too_many_connections due to Max_connections value limit.

I’d say 2x the number you expect to have connect to the database as you can not change the value without bouncing the service and downtime is usually hard to come by.  Just set the monitoring solution at 50%.   That’s what I currently do; using a max connection of 2000 with an average connection rate of around 800.  That sounds like a lot but with 96 cores that’s only around 9 connections per core.  

2. We usee hikari pool on the client side but that even has limitations. Implementing pg_bouncer is another suggestion in the posts. In a HA setup, do we have to set it up on both primary standby?

Take a look at pg_bouncer.   The problem with a high number of connections is that the idle connections will wake up from poll() and spin on the cpu.  I’m in the process of testing pg_bouncer to reduce the number PostgreSQL connections to resolve the high server load due to idle sessions.  pg_bouncer has proven to be effective as it doesn’t suffer from the idle poll() issue and it has reduced the server load due to idle sessions. 

I have been wondering if PostgreSQL used kqueues instead of poll if it would allow the server to scale better without having to introduce pg_bouncer.