Getting error "too many clients already" despite having a db connection limit set

Started by adolfo flores10 months ago7 messagesgeneral
Jump to latest
#1adolfo flores
adolfoflores2211@gmail.com

Hello Team,

I hope you can help me with an issue we're experiencing. We have an app
running on Kubernetes that opens a huge number of connections within a
couple of seconds.

The database that the app connects to, is configured with a connection
limit of 30% of the max_connections setting. Despite this limit being set
for that database, we're seeing the following errors in the PostgreSQL logs:

FATAL: sorry, too many clients already
FATAL: remaining connection slots are reserved for roles with the
SUPERUSER attribute

Is it expected behavior to reach the max_connections limit when that app
opens many connections in a short period of time, even if a connection
limit is set for that database and everything else uses no more than 10% of
the max_connections?

Or any idea on what could be happening?

Regards.

Adolfo F.

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: adolfo flores (#1)
Re: Getting error "too many clients already" despite having a db connection limit set

adolfo flores <adolfoflores2211@gmail.com> writes:

I hope you can help me with an issue we're experiencing. We have an app
running on Kubernetes that opens a huge number of connections within a
couple of seconds.

You need to fix that app to be less unfriendly, or maybe put it behind
a connection pooler.

Is it expected behavior to reach the max_connections limit when that app
opens many connections in a short period of time, even if a connection
limit is set for that database and everything else uses no more than 10% of
the max_connections?

It takes a finite amount of time for a new backend process to figure
out which database it's supposed to connect to and then detect whether
the per-DB connection limit is exceeded. In the meantime, that
session does count against the global limit, so yeah this isn't
surprising if the connection arrival rate is high enough.

regards, tom lane

#3Marco Torres
mtors25@gmail.com
In reply to: Tom Lane (#2)
Re: Getting error "too many clients already" despite having a db connection limit set

You might want to explore pgpool and pgbouncer. Depending in your use case
you might want to glue them togeter.

On Mon, Jun 16, 2025, 10:39 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Show quoted text

adolfo flores <adolfoflores2211@gmail.com> writes:

I hope you can help me with an issue we're experiencing. We have an app
running on Kubernetes that opens a huge number of connections within a
couple of seconds.

You need to fix that app to be less unfriendly, or maybe put it behind
a connection pooler.

Is it expected behavior to reach the max_connections limit when that app
opens many connections in a short period of time, even if a connection
limit is set for that database and everything else uses no more than 10%

of

the max_connections?

It takes a finite amount of time for a new backend process to figure
out which database it's supposed to connect to and then detect whether
the per-DB connection limit is exceeded. In the meantime, that
session does count against the global limit, so yeah this isn't
surprising if the connection arrival rate is high enough.

regards, tom lane

#4Richard Welty
rwelty@salesium.com
In reply to: Tom Lane (#2)
Re: Getting error "too many clients already" despite having a db connection limit set

a coding error i've seen from inexperienced devs with little database experience

is inattentiveness to how the DB connections were being opened. last time i

saw this, a smart young dev with no DB background did not understand the cost

of opening connections and had on the order of 30 php calls to open connections

to PostgreSQL in a single page. he did not understand why it was slow.

richard

---- On Mon, 16 Jun 2025 12:39:09 -0400 Tom Lane <tgl@sss.pgh.pa.us> wrote ---

adolfo flores < mailto:adolfoflores2211@gmail.com > writes:

I hope you can help me with an issue we're experiencing. We have an app
running on Kubernetes that opens a huge number of connections within a
couple of seconds.

You need to fix that app to be less unfriendly, or maybe put it behind
a connection pooler.

Is it expected behavior to reach the max_connections limit when that app
opens many connections in a short period of time, even if a connection
limit is set for that database and everything else uses no more than 10% of
the max_connections?

It takes a finite amount of time for a new backend process to figure
out which database it's supposed to connect to and then detect whether
the per-DB connection limit is exceeded. In the meantime, that
session does count against the global limit, so yeah this isn't
surprising if the connection arrival rate is high enough.

            regards, tom lane

#5Adrian Klaver
adrian.klaver@aklaver.com
In reply to: adolfo flores (#1)
Re: Getting error "too many clients already" despite having a db connection limit set

On 6/16/25 09:29, adolfo flores wrote:

Hello Team,

I hope you can help me with an issue we're experiencing. We have an app
running on Kubernetes that opens a huge number of connections within a
couple of seconds.

The database that the app connects to, is configured with a connection
limit of 30% of the max_connections setting. Despite this limit being

I am not understanding the above. The connection limit from the database
side is going to be the value for max_connections. It is not clear to me
what "... connection limit of 30% of the max_connections setting" is
referring to?

I suggest providing the actual numbers you are working with as well as
the locations where the numbers are being set.

set for that database, we're seeing the following errors in the
PostgreSQL logs:

FATAL:  sorry, too many clients already
FATAL:  remaining connection slots are reserved for roles with the
SUPERUSER attribute

Is it expected behavior to reach the max_connections limit when that app
opens many connections in a short period of time, even if a connection
limit is set for that database and everything else uses no more than 10%
of the max_connections?

Or any idea on what could be happening?

Regards.

Adolfo F.

--
Adrian Klaver
adrian.klaver@aklaver.com

#6David G. Johnston
david.g.johnston@gmail.com
In reply to: Adrian Klaver (#5)
Re: Getting error "too many clients already" despite having a db connection limit set

On Mon, Jun 16, 2025, 10:21 Adrian Klaver <adrian.klaver@aklaver.com> wrote:

On 6/16/25 09:29, adolfo flores wrote:

Hello Team,

I hope you can help me with an issue we're experiencing. We have an app
running on Kubernetes that opens a huge number of connections within a
couple of seconds.

The database that the app connects to, is configured with a connection
limit of 30% of the max_connections setting. Despite this limit being

I am not understanding the above. The connection limit from the database
side is going to be the value for max_connections. It is not clear to me
what "... connection limit of 30% of the max_connections setting" is
referring to?

See "alter database ... set"

David J.

#7Adrian Klaver
adrian.klaver@aklaver.com
In reply to: David G. Johnston (#6)
Re: Getting error "too many clients already" despite having a db connection limit set

On 6/16/25 11:09 AM, David G. Johnston wrote:

I am not understanding the above. The connection limit from the
database
side is going to be the value for max_connections. It is not clear
to me
what "...  connection limit of 30% of the max_connections setting" is
referring to?

See "alter database ... set"

Aah, forgot about that.

David J.

--
Adrian Klaver
adrian.klaver@aklaver.com