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 1muvyo-0002vC-Er for pgsql-admin@arkaria.postgresql.org; Wed, 08 Dec 2021 12:23:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1muvym-0005W4-Fr for pgsql-admin@arkaria.postgresql.org; Wed, 08 Dec 2021 12:23:24 +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 1muvyl-0005Vv-V2 for pgsql-admin@lists.postgresql.org; Wed, 08 Dec 2021 12:23:24 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1muvyi-0004It-Ng for pgsql-admin@lists.postgresql.org; Wed, 08 Dec 2021 12:23:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=3d6R43SSb6KvQDRu0lBM02xS9Cu1PFgQ3vX+He+WJXg=; b=r4uidAQntEZ9s5numq6aMePCON ekiVvwPc/WLqf9emw87OUNICxpJq2zxqqgqdyVGMFYa5tClxOKW0Vx1HSOf5ug9b1+ARgilAuvmzB c6wkCWpTfJcOCKnmlgUEtMCUmY+dsThviqzEqBCLeP+bIshH4rrbO4jEni/E4YB+JJ4g=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1muvye-0000Cl-Uf; Wed, 08 Dec 2021 13:23:16 +0100 Date: Wed, 8 Dec 2021 13:23:16 +0100 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Ron Cc: pgsql-admin@lists.postgresql.org Subject: Re: PostgreSQL(v9.6.5) not releasing old/inactive IDLE connections Message-ID: <20211208122316.GB28627@depesz.com> Reply-To: depesz@depesz.com References: <7ab7add9-9c1b-3842-e4eb-f78107964db3@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <7ab7add9-9c1b-3842-e4eb-f78107964db3@gmail.com> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Dec 08, 2021 at 01:23:32AM -0600, Ron wrote: > I set up a cron job that regularly kills old idle connections. > SELECT pg_terminate_backend(pid) > FROM pg_stat_activity > WHERE datname = 'databasename' >   AND pid <> pg_backend_pid() >   AND state = 'idle' >   and extract(epoch from (current_timestamp - query_start)) > 20*60 -- 20 > minutes > ; Two notes: 1. it's better to use state_change and not query_start. For example, if I'd start 2 hours query, 3 hours ago, then "now" it would still have query_start at (now() - 3 hours), despite the fact that it would be idle only for 1 hour. 2. doing calculations in epoch is bad idea. it can become your muscle memory, and it will cause problems with indexing. instead: and state_change < current_timestamp - '20 minutes'::interval depesz