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 1joEoD-0005tN-JH for pgsql-general@arkaria.postgresql.org; Wed, 24 Jun 2020 23:28:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1joEoC-0006pf-Aj for pgsql-general@arkaria.postgresql.org; Wed, 24 Jun 2020 23:28:00 +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 1joEoB-0006pY-Sz for pgsql-general@lists.postgresql.org; Wed, 24 Jun 2020 23:28:00 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1joEo8-0004ZL-8J for pgsql-general@lists.postgresql.org; Wed, 24 Jun 2020 23:27:58 +0000 Received: by mail-pl1-x630.google.com with SMTP id n2so1824710pld.13 for ; Wed, 24 Jun 2020 16:27:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:cc:subject:in-reply-to:message-id :date:mime-version:content-transfer-encoding; bh=b08KGilJZ9CvII6CxOXM7JvnXdKS5ZIMkdzuC2pgVRA=; b=fpDubGsSVYE1/h4Tost61XVLS8SJGl97lKOdWMXn1hwGAor5OGCWMXEeseD1R/xi0P KkmEIGUkG7dcZrhfpZPNOeDJr7u+M94aYntJVoM10TKJtq1fjCPSQK6nQmmeiPsFwKy+ XbFGCYMv81cph5/ursnpiYy3Pxu9J3eM4RyYnXrTIF8HyAwg9CgNS/6TwrjUH7tSwToq mLFzQRdXBXDJnU8AvWdfaMNRQOswG3/83vCBcGRaOyNLF5eGg4t2VzlVCk+ZwCaRvOx2 NlRf+WIgyeVy28magw/Cw5zMJILsKLRF6dF2JJtRTY+CK3RoDgUEm6w+rGeqdxH+4Y4x TH5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:message-id:date:mime-version:content-transfer-encoding; bh=b08KGilJZ9CvII6CxOXM7JvnXdKS5ZIMkdzuC2pgVRA=; b=ZzDQ2gBgEY0D2Q6N4C0kFTKZtcdcsKEZ81sq7D9rf6sFjUj0Sz2GYOa1VcTnj3+FuY /8Oh36pn93a7inFfDB5G8hjFSILubH/E75lV5Yid/p15MEWF1Kg/XyJqySmBcFkGeIzv teJUMRj5dsmrGKwJAVYnF7Rc8Jr7KCqYPqsfLYkj0KXA23PrZmYyCdHU62FF1PQ8bJck mVrHhkBgtxbhkCbItXrV18KkZZHgXa2POc5imMuQF1cdOrTaFgikMPk7H0eYWxN02qWZ nx2aMZeU5z1pKsRT1uo8N3irJCLZDclq1J6+fEJZHAxSouzEhbVmEaARyyXcA5jexiQK EQuw== X-Gm-Message-State: AOAM531pvxg8dUUC9xBXE3dmRVdFq527GYBZKYflz6okiyOa66I3/Cz0 Tihgw8UIxLbmZ2AlO0zaqvK3NAhw X-Google-Smtp-Source: ABdhPJyHtF/H3Eh5j4s0NydhK7wQS8Y7mjuNxqVkJWADSeTslul/OfmEq7/M+YTleHqm0SeVst9ZaA== X-Received: by 2002:a17:90b:3685:: with SMTP id mj5mr209286pjb.162.1593041274719; Wed, 24 Jun 2020 16:27:54 -0700 (PDT) Received: from tim-desktop (220-235-176-99.dyn.iinet.net.au. [220.235.176.99]) by smtp.gmail.com with ESMTPSA id y6sm21732651pfp.144.2020.06.24.16.27.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jun 2020 16:27:54 -0700 (PDT) References: <87366lpbno.fsf@gmail.com> User-agent: mu4e 1.5.3; emacs 27.0.91 From: Tim Cross To: "Bee.Lists" Cc: pgsql-general@lists.postgresql.org Subject: Re: Persistent Connections In-reply-to: Message-ID: <87pn9ooxhm.fsf@gmail.com> Date: Thu, 25 Jun 2020 09:27:49 +1000 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Bee.Lists writes: >> On Jun 23, 2020, at 8:09 PM, Tim Cross wrote: >>=20 >> 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. See= ing >> connections go up and down in the gem may not be telling you the full st= ory - 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 > > 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 someo= ne else who was having similar issues as myself: > > https://stackoverflow.com/questions/60843123/djangopostgres-fatal-sorry-t= oo-many-clients-already > > I=E2=80=99m also seeing the connection count rise overnight from crontabs= .=20=20 > > For some clarity, the gem is Sequel, which is on top of the PG gem (Ruby)= . I=E2=80=99ve spoken to the Sequel author and he says everything is fine.= I have some evidence it=E2=80=99s a connection issue and the gem is doing= its job, as I=E2=80=99m seeing it=E2=80=99s happening elsewhere with cront= abs and other clients.=20=20 > Rather than a problem with the libraries, I would be looking for a problem with the code which uses those libraries. If it was a problem with either the Sequel or PG gems (or with Postgres for that matter), it would be a lot more wide spread and you would be seeing a lot more reports. I'm not familiar with Sequel and haven't used Ruby for nearly 20 years, but have used plenty of other PG libraries. You mention garbage collection and I'm sure that is working fine in Ruby. However, you cannot rely on that to correctly cleanup your PG connections. Somewhere in your code, there has to be code the developer writes which tells the library you are finished with the connection. For example, the JS PG package has the command 'disconnect'. Essentially, your code needs to tell the remote PG server you have finished with the connection so that it knows it can clean up things on its end. If your code is not issuing explicit disconnect commands, what is happening is that the connection on your client side are being cleanup when the connection object goes out of scope and the garbage collector kicks in. However, on the PG side, the connections hang around until PG times them out, which takes much longer and could easily cause you to hit the limit, especially as you have such a very small limit. I think you need to verify that in all your client code, somewhere there is explicit code being called which is telling PG you are disconnecting the connection. Don't assume this is automagically happening as part of GC.=20 > >> Typically, due to the overhead of making a connection, you don't want >> your client app/web app to create a new connection for every query. >> Instead, you would use some type of connection pool. Many development >> languages support some form of pooling as part of their db connection >> library (don't know about Ruby, but JS, Python, PHP, Java, Perl all do) >> and there are external 3rd party solutions like pgbouncer which sit >> between your client and the database and work a bit like a connection >> broker which will manage a pool of connections. > > That=E2=80=99s why I=E2=80=99m thinking installing a connection pooler wo= uld solve all of this. pgbouncer is what I=E2=80=99m looking at now.=20 > I doubt this will solve your problem. It might hide the problem or it might lengthen the time between failures, but it is very unlikely to solve the problem. It may help identify the source of the problem. Have you verified the PG gem doesn't support pooling? If it does, that would be far easier to configure and use than installing pgbouncer. Based on your description of the app and the small number of connections you have PG configured for, adding pgbouncer is like putting a fighter jet engine in a family SUV.=20 >> From the description of what your doing, I would first look to see what >> level of connection pooling your development language supports. This >> will likely be easier to configure and use than setting up a whole >> additional bit of infrastructure which is probably far more powerful >> than you need. > > I will do that. This is all new.=20=20 > >> I would also go through your code and make sure that every time you >> create a database connection, there is some code which is closing that >> connection once your finished with it. This is a frequent source of >> problems, especially during development when your code might throw an >> error and the code you have to disconnect does not get executed or you >> simply forget to issue a disconnect when your finished.=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 i= s inside there. It=E2=80=99s so abstracted that I don=E2=80=99t know when = the actual calls are made. It=E2=80=99s a DSL so the workings aren=E2=80= =99t evident when making queries. It was suggested I install a logger to s= ee what and when queries are actually made.=20=20 > I have used ORMs in the past. Personally, I hate them as they always introduce limitations. However, I would strongly suggest checking the Sequel API and examples. I just had a very quick look at the Sequel API. Things I immediately noticed are 1. It does support connection pooling 2. There is a DB.database#disconnect method. You need to verify your client code is calling that method before the objects are GC'd. >> 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 > > That SO link above suggests I=E2=80=99m not alone. So installing a conne= ction pooler seems the right thing to do. Good to know about the reservati= ons. That is better feedback. > > Use the connection pool provided by the Sequel gem first and see how that goes. I'm pretty confident the issue will turn out to be in your code. I would also strongly consider increasing the PG max connections. What you have configured is extremely low. Default is 100. 50 or 25 would possibly be more reasonable in your case. It is quite possible your Ruby scripts are keeping the connections open in-=3Dbetween HTTP requests if your using something which keeps the scripts loaded in memory within the web server (common setup to reduce spin up time on requests). --=20 Tim Cross