Skip to content

Instantly share code, notes, and snippets.

@Shidfar
Last active May 28, 2018 02:25
Show Gist options
  • Save Shidfar/e88ef6d19a0d2a310e1cff498a21cca3 to your computer and use it in GitHub Desktop.
Save Shidfar/e88ef6d19a0d2a310e1cff498a21cca3 to your computer and use it in GitHub Desktop.
List and kill queries that are running more than XX minutes
-- What queries are running more than a second? --
SELECT pid FROM pg_stat_activity
WHERE pg_stat_activity.usename = 'username'
AND query != '<IDLE>'
AND query NOT ILIKE '%pg_stat_activity%'
AND age(clock_timestamp(), query_start) > '00:00:01';
-- Kill queries that are running more than 5 minutes --
WITH pgg AS (
SELECT pid
FROM pg_stat_activity
WHERE pg_stat_activity.usename = ‘username’
AND query !=<IDLE>AND query NOT ILIKE ‘%pg_stat_activity%’
AND age(clock_timestamp(), query_start) >00:05:00’)
SELECT pg_terminate_backend(pgg.pid) FROM pgg;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment