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 1jnsz2-0004HA-1P for pgsql-general@arkaria.postgresql.org; Wed, 24 Jun 2020 00:09:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jnsz0-0005fE-3x for pgsql-general@arkaria.postgresql.org; Wed, 24 Jun 2020 00:09:42 +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 1jnsyz-0005f7-97 for pgsql-general@lists.postgresql.org; Wed, 24 Jun 2020 00:09:41 +0000 Received: from mail-pj1-x1041.google.com ([2607:f8b0:4864:20::1041]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jnsyw-0000x4-3a for pgsql-general@lists.postgresql.org; Wed, 24 Jun 2020 00:09:40 +0000 Received: by mail-pj1-x1041.google.com with SMTP id b7so1628974pju.0 for ; Tue, 23 Jun 2020 17:09:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:subject:in-reply-to:message-id:date :mime-version:content-transfer-encoding; bh=DN7nE/MdX4paB/1gJNruzMbqKyOt4ed9ZTAXrNkGzBw=; b=aJCrKSEHijlGrpOBh1gcTQBmBNUAHqA1CT8fJw9HE6zsbd6Z9W1JrAmop2pjEFhtt6 +acramMNzra4d39kdE5E1RSI6dQE/rsqzx8tjMP69b6L+0xkF4gq39IcibH9BPhnnZ6j doo/o+dlRB1M7X6yAZkbqtVvnANRUxExrNIA7X1dJN1TRQk7RUnYywsgvbF49clbEOTn TysxwU1W4k/NbI2sp9L/V2X5Bji/j2eo57FjQH2jO4xEBUmMJZdQ9LRyE5TmocbbjNo4 1dqnkCnIXUnOXpmMWwxFC0GGWRxzmj20zFpSMy/7Z4fyDaMfvvKUNQm093JLixaAHoIU 96Bw== 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:subject :in-reply-to:message-id:date:mime-version:content-transfer-encoding; bh=DN7nE/MdX4paB/1gJNruzMbqKyOt4ed9ZTAXrNkGzBw=; b=jZPNmT/h0MhrpSe0n6xJBN2hJ2kCrAPzITit6SSufT+bNFf8KsDVt6U7TWRp7d4+tJ CeAX/o5wOq4CGowYiaxHVWt+A9muLl43MVvs0J2nip71/r6a1JtwgKOB+adGQpsbjT7H KiyQHnmMlsBDntCIDlW9X9ogMImiSJJ+XGeHaqYVPd7c/2CTwHqLqGHrg7wg33AuJw4j caw75vlF7IQNDIzhbyk2Wqi1kG4Zv+YCvOqNEXSl5CK2UhAtfHZEUiJD42XuD/jpAtIZ 2JfEp/8QclIoxkWL+cZwmcGD7LNJ6jqByyYwhto0MJhLEpMqhlRBo4uhFeDJvVlgcvQO 2vWQ== X-Gm-Message-State: AOAM532B1uD+YdPNOL7UFsgRzEStFpzjNuEwWInV88G26zcAbI4keqDm 2EKxhMAdFJ/hroI2RcTA5hRZ908lk1Y= X-Google-Smtp-Source: ABdhPJyAFannWV85TtuyvxQyWxQNYXFH0i839vHliei4fhqEr4PPveKPlPa8S0IWuwa8WjOamW+hSg== X-Received: by 2002:a17:902:8344:: with SMTP id z4mr20022311pln.280.1592957376112; Tue, 23 Jun 2020 17:09:36 -0700 (PDT) Received: from tim-desktop (220-235-166-200.dyn.iinet.net.au. [220.235.166.200]) by smtp.gmail.com with ESMTPSA id 200sm18625577pfb.15.2020.06.23.17.09.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jun 2020 17:09:35 -0700 (PDT) References: User-agent: mu4e 1.5.3; emacs 27.0.91 From: Tim Cross To: pgsql-general@lists.postgresql.org Subject: Re: Persistent Connections In-reply-to: Message-ID: <87366lpbno.fsf@gmail.com> Date: Wed, 24 Jun 2020 10:09:31 +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: > I have an issue with a server (v10) that=E2=80=99s seeing increasing conn= ections until it=E2=80=99s maxxed-out.=20=20 > > max_connections for my 4-core server is set to 12.=20=20 > > I=E2=80=99ve installed pg_stat_activity and pg_stat_statements. > > I access this server through a web app, and another client on two machine= s. I also send queries through SSH on the LAN. psql queries indicate ther= e are too many clients already. I=E2=80=99ve been getting errors from my w= orkstation through a Ruby gem that says dropped connections happen. Essent= ially, the server isn=E2=80=99t 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 simpl= e. I=E2=80=99ve tested those queries and they seem fine on a workstation c= lient. I=E2=80=99m assuming these queries somehow don=E2=80=99t finish and= leave the connection open, but I could be wrong. All of this is new to me= .=20=20 > > 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.=20=20 > > I am running no connection pool tool of any sort, and I=E2=80=99ve never = used one.=20=20 > > At this point I don=E2=80=99t know what to even be watching for. Connect= ions increase and connections =E2=80=9Cdrop=E2=80=9D, according to the gem = I=E2=80=99m using. I have simple queries that are idle and won=E2=80=99t d= isappear for some reason.=20=20 > > How can I figure this out so the connections remain within the max_connec= tions limit, and connections are not dropped?=20=20 > > Any insight appreciated.=20=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. 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.=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.