Re: PostgreSQL(v9.6.5) not releasing old/inactive IDLE connections

Поиск
Список
Период
Сортировка
От hubert depesz lubaczewski
Тема Re: PostgreSQL(v9.6.5) not releasing old/inactive IDLE connections
Дата
Msg-id 20211208122316.GB28627@depesz.com
обсуждение исходный текст
Ответ на Re: PostgreSQL(v9.6.5) not releasing old/inactive IDLE connections  (Ron <ronljohnsonjr@gmail.com>)
Список pgsql-admin
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



В списке pgsql-admin по дате отправления:

Предыдущее
От: Thomas Kellerer
Дата:
Сообщение: Re: PostgreSQL(v9.6.5) not releasing old/inactive IDLE connections
Следующее
От: Mladen Gogala
Дата:
Сообщение: Re: AW: postgresql long running query