pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: Persistent Connections
Date: Wed, 24 Jun 2020 21:10:14 +0200
Message-ID: <20200624191014.GA15882@hjp.at> (raw)
In-Reply-To: <EBE1A06C-F4CA-4CA5-BFE1-FD25D828DF3A@gmail.com>
References: <E19EF208-2A79-41A2-9760-577A85B815CD@gmail.com>
	<87366lpbno.fsf@gmail.com>
	<EBE1A06C-F4CA-4CA5-BFE1-FD25D828DF3A@gmail.com>

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. Seeing
> > connections go up and down in the gem may not be telling you the full story - 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. 
> 
> Hi Tim.  I can’t speak for the gem.  I’m assuming its garbage
> collection is working.  But yes, it does look that way.  I found
> someone else who was having similar issues as myself:
> 
> https://stackoverflow.com/questions/60843123/djangopostgres-fatal-sorry-too-many-clients-already
> 
> I’m also seeing the connection count rise overnight from crontabs.  

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. 

> The issue here is that it’s 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’s so abstracted that I don’t know when the actual
> calls are made.

Yeah, that's one reason why I'm not overly fond of ORMs. 

> It was suggested I install a logger to see what and when queries are
> actually made.  

That would probably help. Alternatively you can tell PostgreSQL to log
that stuff. 


> > 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. 
> 
> That SO link above suggests I’m 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

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (12+ messages)  latest in thread

Message-ID: <20200624191014.GA15882@hjp.at>
Permalink:  ../20200624191014.GA15882@hjp.at/
Also on:    postgresql.org/message-id/20200624191014.GA15882@hjp.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: hjp-pgsql@hjp.at
  Subject: Re: Persistent Connections
  In-Reply-To: <20200624191014.GA15882@hjp.at>
  References: <E19EF208-2A79-41A2-9760-577A85B815CD@gmail.com> <87366lpbno.fsf@gmail.com> <EBE1A06C-F4CA-4CA5-BFE1-FD25D828DF3A@gmail.com> <20200624191014.GA15882@hjp.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox