From: Tim Cross <theophilusx@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Persistent Connections
Date: Wed, 24 Jun 2020 10:09:31 +1000
Message-ID: <87366lpbno.fsf@gmail.com> (raw)
In-Reply-To: <E19EF208-2A79-41A2-9760-577A85B815CD@gmail.com>
References: <E19EF208-2A79-41A2-9760-577A85B815CD@gmail.com>
Bee.Lists <bee.lists@gmail.com> writes:
> I have an issue with a server (v10) that’s seeing increasing connections until it’s maxxed-out.
>
> max_connections for my 4-core server is set to 12.
>
> I’ve installed pg_stat_activity and pg_stat_statements.
>
> I access this server through a web app, and another client on two machines. I also send queries through SSH on the LAN. psql queries indicate there are too many clients already. I’ve been getting errors from my workstation through a Ruby gem that says dropped connections happen. Essentially, the server isn’t giving up connections and clients from all sides are showing some issues.
>
> pg_stat_activity has shown a handful of idle queries that are quite simple. I’ve tested those queries and they seem fine on a workstation client. I’m assuming these queries somehow don’t finish and leave the connection open, but I could be wrong. All of this is new to me.
>
> It was suggested on Slack that it sounds like my workstation had some TCP issues with these connections, and that it was a firewall. I disabled the firewall and the same happened. The firewall has been restarted.
>
> I am running no connection pool tool of any sort, and I’ve never used one.
>
> At this point I don’t know what to even be watching for. Connections increase and connections “drop”, according to the gem I’m using. I have simple queries that are idle and won’t disappear for some reason.
>
> How can I figure this out so the connections remain within the max_connections limit, and connections are not dropped?
>
> Any insight appreciated.
>
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.
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.
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: theophilusx@gmail.com
Subject: Re: Persistent Connections
In-Reply-To: <87366lpbno.fsf@gmail.com>
References: <E19EF208-2A79-41A2-9760-577A85B815CD@gmail.com> <87366lpbno.fsf@gmail.com>
* 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