Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1joAmx-0006nb-CH for pgsql-general@arkaria.postgresql.org; Wed, 24 Jun 2020 19:10:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1joAmw-0005K1-9W for pgsql-general@arkaria.postgresql.org; Wed, 24 Jun 2020 19:10:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1joAmw-0005Js-13 for pgsql-general@lists.postgresql.org; Wed, 24 Jun 2020 19:10:26 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1joAmm-0002Gx-BJ for pgsql-general@lists.postgresql.org; Wed, 24 Jun 2020 19:10:24 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 0A026330D; Wed, 24 Jun 2020 21:10:14 +0200 (CEST) Date: Wed, 24 Jun 2020 21:10:14 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Persistent Connections Message-ID: <20200624191014.GA15882@hjp.at> References: <87366lpbno.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="WIyZ46R2i8wDzkSu" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --WIyZ46R2i8wDzkSu Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2020-06-24 13:45:21 -0400, Bee.Lists wrote: > > Sounds like your web app may not be closing connections once it has > > finished with them? The fact your seeing idle connections would seem to > > support this. I would be verifying there isn't a code path in your > > client application which is failing to close a connection correctly. Se= eing > > connections go up and down in the gem may not be telling you the full s= tory - could > > be that your client connection objects are being destroyed in your app, > > but are not closing the connection correctly so PG is unaware the client > > has finished.=20 >=20 > Hi Tim. I can=E2=80=99t speak for the gem. I=E2=80=99m assuming its gar= bage > collection is working. But yes, it does look that way. I found > someone else who was having similar issues as myself: >=20 > https://stackoverflow.com/questions/60843123/djangopostgres-fatal-sorry-t= oo-many-clients-already >=20 > I=E2=80=99m also seeing the connection count rise overnight from crontabs= =2E =20 Unlike a web-server which runs continuously and can therefore keep a connection open indefinitely, a cron job normally has a finite live-time. So if the number of connections from your cron jobs keeps rising that means that your cron jobs don't finish (or at least some of them). You should investigate why they are stuck.=20 > The issue here is that it=E2=80=99s all in the gem. The gem is actually = an > ORM, built on top of the PG gem. So all the database connection stuff > is inside there. It=E2=80=99s so abstracted that I don=E2=80=99t know wh= en the actual > calls are made. Yeah, that's one reason why I'm not overly fond of ORMs.=20 > It was suggested I install a logger to see what and when queries are > actually made. =20 That would probably help. Alternatively you can tell PostgreSQL to log that stuff.=20 > > A connection pool can help in tracking down such issues as well. Most > > pooling solutions will allow you to set a max pool size. In addition to > > enabling you to 'reserve' a set number of connections for a client, you > > will know which client seems to be running out of connections, helping > > to identify the culprit.=20 >=20 > That SO link above suggests I=E2=80=99m not alone. Well, yes. Just about everyone who has ever run a web-application against a database has run into this problem. It's part of the learning process :-). What you should learn from this is how to analyze the situation and how to mitigate it. The solution might be a setting in the webserver or gem configuration. It might be a connection pooler. It even be resetting max_connections to the default and not caring about a few idle connections. What you shouldn't learn from this is that a pooler will make your problems magically go away. Because it won't. jp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --WIyZ46R2i8wDzkSu Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl7zpRAACgkQ8g5IURL+ KF1egw//RHMhUdKrRfrYrj/3hdlaQUawgkNyKZXYLAxx0A7oiLHNu68tLlG9BM57 rwcaxttZzMDWIbQzfngexzZRwosp0iAGlq6pMxSml1k9/OSNL0TFADK/ruwAJWqQ X0XasQwY0dcRm9FEUiq9/Dvlk43Hli1VgvjFDYZZO3Hv6g+hCWsIaB1MixF8vxN0 ynnKkbkqdf598kYdfqPWDNdVksjaDFyqHchDouXQh2CblfnHcNl+rFy7w/cQPsKE M3XHe1ghQR0nkEnkaJ4o4lITMHaiaIHTsC0dJP1PfeKMbOU7wAVgRrt/eIVgn7zp 9xYqktXluzxfzGWooMsehK4btaa1KkVSAHncf7cZUcVahuh7XLiumDRNAJrrBNzj Y194Kw4ZMbzWgdgVERbkpaOgKN6qQqn2ZKdGuI27RZBPVa1YnniubAEnvAug+0Vr RngjkSXWmAZN1O1Dv3R22cLxka9B36ygP5t/j9miZYY96XJHSU0xEUJOblQrpRzw 4FoYsEb7HPqNVeh9nn66/LH3mtq0S/vMU6i1OtXBGFrWocaarx11Mhr1+m8/KBUP 0APr45iYDTYh3g5jztKMfVDBYDKmsSqgHosBNUfXCVmPLMJ/LZGzYgjjqsTuPB/e 38mg8CRBQ19bQVeiZrOJCsyJVPRfDR0hfUwEGUGB/Q8kezELKUg= =7kFF -----END PGP SIGNATURE----- --WIyZ46R2i8wDzkSu--