Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uRCuJ-008XnC-Hh for pgsql-general@arkaria.postgresql.org; Mon, 16 Jun 2025 16:42:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uRCuH-001a2g-4V for pgsql-general@arkaria.postgresql.org; Mon, 16 Jun 2025 16:42:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uRCuG-001a2Y-Qj for pgsql-general@lists.postgresql.org; Mon, 16 Jun 2025 16:42:01 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uRCuF-002OcO-0r for pgsql-general@lists.postgresql.org; Mon, 16 Jun 2025 16:42:01 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-adb2e9fd208so958478966b.3 for ; Mon, 16 Jun 2025 09:41:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750092116; x=1750696916; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=aDZbkX+NSa0BId0/Rl+t5AluF+0njFN3S6Ox7Njx3O0=; b=cP5j1ceNLMAIWVtp92qqVTRPyWU/vbUd5mQluLM6pJoKaBX5y3BQR4kbHNPKaPhBA1 cftyw0QByRUFlAkXcDS/+kZrrE+9+Da+Uvjolv2BOZI+zNxn7vOevV02B4SKLi8sK4sz 4+PHEiyxVBCGKYPamJSUwSwecH3FPu3VMUm8EaK3Ufza5lWqaD5KoPPRp2rctE51lMz9 +YvZ5C1p4lrzthrCROkWGeiktCWZSkC8k2yuTGBx9Qkgw+gjlpFWB6fBcxVtIZlfIcfx rG6DN5wT+x24kK390qh0RfMhUwBwbbwf9X6rAwNjvuuLP5WPQ1NKJG9zVigiVcrVh+xg fG9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750092116; x=1750696916; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=aDZbkX+NSa0BId0/Rl+t5AluF+0njFN3S6Ox7Njx3O0=; b=IGAOwzeYTXWsPtdCabH/64VlXNyAZoHWExxMI2fkjWeQxzDF8UbY+uEoxHcrAvhr0J VArZkOkS8dZaLYpROuqpkdIYJPpmbyOCfPxCaCYj1d6mWYx+QM9qO9V/4JEryl7qZzAa N8OKsoB7MIrV3dtXDDeb3FN8MWvkPrL2+aq1/QHgytoUFEpXZNozN6drjxsm3wOLm1Qb di9hy5AAClKVsSR+I2flAHmQenq7c8Y1WfKTMxNVYk5aDjUQuJTldbJdoIc6z0LtMa5Q cGQvHxnxGIpG4j0dwHy1y27qtzoU+zXyl0CQEPm/bhMOA59YxssrhBoYkoFf/d4PFwiA bTFw== X-Forwarded-Encrypted: i=1; AJvYcCV0DRaR9xYGbK8ccKZN7U11bDj7HBa/e0tP8Bl6I97K5vRHrty6UTFJ6zv0ebz/dCYZBnFDOwHIyaV6A9cc@lists.postgresql.org X-Gm-Message-State: AOJu0Yyn35+S2NuiZQ2QoMTIGB3Svb1hzCShs0lT8vJ21YFq0QBTYYR9 q6W/taJ5K4m6ziNYQMuwwPvKPBtnowtT8q7/wYVR3joUwf+0XqvqH4OgZUgHsusYGwDLRMQMlKr j+55hJw/RuOGlUnA16EaHllFENKEBlzE= X-Gm-Gg: ASbGncvld+zsRFfZ9hdtM81ZzJfnKlBAbeD/dipSCDINtNmVX9SqJmYdVNbet6uQEDF bVkzYqtx8WN2vdDxg9ryPPkp4y3FJBIEj3IjaVzpWsyvpu9yIx7HPXd25kiX0rotur9aJNxR4oh MtmvNPkR3qGfza/3dgcTm1NGv3ikU852Pw/ED9dvKtKNLi X-Google-Smtp-Source: AGHT+IF+Yj0ezz0ZhCP+PUzSjJUbwHse3++cQ/kulib8mHlsUP1l3ljPwq0pJeA29lfKO3GuvB3k0bc2XpcVmsWRnkU= X-Received: by 2002:a17:907:3c94:b0:ad5:5210:749c with SMTP id a640c23a62f3a-adfad3cda7dmr876053366b.22.1750092115879; Mon, 16 Jun 2025 09:41:55 -0700 (PDT) MIME-Version: 1.0 References: <536522.1750091949@sss.pgh.pa.us> In-Reply-To: <536522.1750091949@sss.pgh.pa.us> From: Marco Torres Date: Mon, 16 Jun 2025 10:41:44 -0600 X-Gm-Features: AX0GCFuKHnj-8D7ldb92KuijaVJyJSOe1djwRCH-hRnzqLO-k641DZxnGxjrlD0 Message-ID: Subject: Re: Getting error "too many clients already" despite having a db connection limit set To: Tom Lane Cc: adolfo flores , pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000e42cf70637b312a8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000e42cf70637b312a8 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=E2=80=AFAM Tom Lane wrote: > adolfo flores 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 ap= p > > 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 > > > --000000000000e42cf70637b312a8 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

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=E2=80=AFAM Tom Lane <tgl@sss.pgh.pa.us> wrote:
adolfo flores <adolfoflores2211@gmai= l.com> writes:
> I hope you can help me with an issue we're experiencing. We have a= n 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 a= pp
> 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 1= 0% 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<= br> the per-DB connection limit is exceeded.=C2=A0 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.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane


--000000000000e42cf70637b312a8--