Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tmqqP-00BvHZ-6r for pgsql-general@arkaria.postgresql.org; Tue, 25 Feb 2025 09:03:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tmqqO-00GQ1i-CC for pgsql-general@arkaria.postgresql.org; Tue, 25 Feb 2025 09:03:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tmqqO-00GQ1a-1H for pgsql-general@lists.postgresql.org; Tue, 25 Feb 2025 09:03:12 +0000 Received: from mail-lj1-x22a.google.com ([2a00:1450:4864:20::22a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tmqqL-000hYD-1B for pgsql-general@postgresql.org; Tue, 25 Feb 2025 09:03:11 +0000 Received: by mail-lj1-x22a.google.com with SMTP id 38308e7fff4ca-30a2cdb2b98so53427081fa.0 for ; Tue, 25 Feb 2025 01:03:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740474188; x=1741078988; darn=postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=MtwQ2l6a0ij6adwljWfeJNKJMdNSVyQddaT1VMSdVzE=; b=IncNQLAPlRbVwXYFx/QvR+cX1pTEUcz7mJXlo63Cf7k5xFUfhSYnp/+p6Qa8KK4voB ul3YRc+oWFYBwLCSm5bxJfFgu/kcwQKEIJfZvBKQHA2rR0zB3/fMVBm2s2Cwx4HCu2qF 5jlIGhdaJGAjeGER9rpVMuGV20IYe5O+PiyNwx8RKIsWHgRxKVE9z9sgbmhnD/+Ysxv7 i7soK7e9xpJYFCah9mIfVrOfIeUaH2fmchKaC8DN43I3wa2ra66WtRXjo5KW0LZ/+EL9 TIBm+r3/XSZl+jDzR4W34KWrWMEoQ3N+dG02ynWG6SVOjFC9orE5cJC6pgdihNUQKucS Aqjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740474188; x=1741078988; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MtwQ2l6a0ij6adwljWfeJNKJMdNSVyQddaT1VMSdVzE=; b=nta8Ai5Gf2AIU2fs1ZJPq6xuDypjfStuh6yhUT9iGTR1GGx2GTcANbdEvjA7NHTbZ8 +QlqZIO4hBoQVVD6JvHJoV6lukKvY3uzwt6NMs7lFjJmq6Vz5UiqEqYgilKRwrpnghSh zw+Ieq623J1Z5gSa/34AW3t9PzjgJupZZnmuYzjbQdGpXaQdHwvJr5q2R5JlDSA7zhg+ lSHJCTpfKVcp33UXxoic9TTIsqotBjkVWjK0/nAxQbdeyC7vgtXuRKftcVg0QuPVSEOp CZTH18TaX0waTb/gHnQNBnG467sam1ZppSRvql47N6PcTdRrzSFHaCUGD7Y03aNkUJ2L eFOQ== X-Gm-Message-State: AOJu0YybriXmxDIuZtUbR9fkpcl4tXWYvYMY8MUAFRbHiNV+4iBFAnUi rm31xjJU7q5kuiy7/cpWwJFBMH4XODMrixRIzqVD7emkoF0oC2+jzDkSLQsNMAIJd1YMLBI50wD ucifzBN20rlcH5tpqBwnMDwkfFZKTQQ== X-Gm-Gg: ASbGncsyVpHp23yYfln9hMRPsDcjFEj9nYyiIB7YZ23bsSTu4+7fZuBM6HG+dbhl+73 eLCHw/rVJTyj35pQZ90BqMzAQhKV8WFTyP6sbuCqvHqwKvcTb+ejiaN6f2KkZ6zozFGf6QgUh49 VO8Nt8yg== X-Google-Smtp-Source: AGHT+IGHAKp7ZX6pgpUktQWOhRZm8yltic5mDt6i5Z7y1XyV45LAGDzobn9n0qED7yvsijqKrb6kdoADsVO+7gaLaEU= X-Received: by 2002:a2e:88c8:0:b0:309:231c:c676 with SMTP id 38308e7fff4ca-30a5b1a7ae3mr50021461fa.17.1740474187385; Tue, 25 Feb 2025 01:03:07 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: =?UTF-8?B?U8OhbmRvciBEYWt1?= Date: Tue, 25 Feb 2025 10:02:56 +0100 X-Gm-Features: AWEUYZmfHykclzISPGAIARvkPmWjjtjMIfxlRjnkthwJ8Yx2HAFkHnRt1B9TEYo Message-ID: Subject: Re: Deleting idle connections To: pgsql-general Content-Type: multipart/alternative; boundary="000000000000ae1ad7062ef3b943" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ae1ad7062ef3b943 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, 25 Feb 2025 at 00:12, Ron Johnson wrote: > On Mon, Feb 24, 2025 at 5:50=E2=80=AFPM Yongye Serkfem wrote: > >> Hi Everyone! >> I am having a series of idle connections and unable to delete them with = a >> single command. Any help in realizing this would be greatly appreciated. >> > > This will kill idle connections older than two hours: > select pid, pg_terminate_backend(pid) > from pg_stat_activity > where state =3D 'idle' > and (EXTRACT(epoch FROM now() - backend_start))/3600.0 > 2; > > Be warned that it might kill more than you want. Add more WHERE > predicates as filter. > > -- > Death to , and butter sauce. > Don't boil me, I'm still alive. > lobster! > You could use the state_change timestamp to be sure that the idle connection is in that state for long enough to be considered really idle. You can catch and terminate otherwise actively working connections momentarily in the idle state, if you are not careful enough. Regards, S=C3=A1ndor --000000000000ae1ad7062ef3b943 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Tue, 25 Feb = 2025 at 00:12, Ron Johnson <r= onljohnsonjr@gmail.com> wrote:
On Mon, Feb 24, 202= 5 at 5:50=E2=80=AFPM Yongye Serkfem <yserkfem@gmail.com> wrote:
Hi Everyone!
I am having a series of idle connections and unable to= delete them with a single command. Any help in realizing this would be gre= atly appreciated.

This will kil= l idle connections older than two hours:
select pid, pg_terminate_backend(pid)
from pg_stat_activity
where = state =3D 'idle'
=C2=A0 and (EXTRACT(epoch FROM now() - backend_= start))/3600.0 > 2;


Be warned that i= t might kill more than you want.=C2=A0 Add more WHERE predicates as filter.=

--
Death to &= lt;Redacted>, and butter sauce.
Don't boil me, I'm still ali= ve.
<Redacted> lobster!

You could use the state_change timestam= p to be sure that the idle connection is in that state for long enough to b= e considered really idle.
You can catch and terminate otherwise actively= working connections momentarily in the idle state, if you are not=C2=A0car= eful enough.

Regards,
S=C3=A1ndor
<= div>

--000000000000ae1ad7062ef3b943--