Subject: Deleting idle connections

From: Yongye Serkfem <[email-masked]>
Date: Mon, 24 Feb 2025 17:49:51 -0500

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.

Regards
Yong

---

From: Ron Johnson <[email-masked]>
Date: Mon, 24 Feb 2025 18:12:13 -0500

> 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 = '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 <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!