Last active
May 28, 2018 02:25
-
-
Save Shidfar/e88ef6d19a0d2a310e1cff498a21cca3 to your computer and use it in GitHub Desktop.
List and kill queries that are running more than XX minutes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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