Last active
February 1, 2018 13:57
-
-
Save mravey/0ddd90cf4418b5985eaf6d37b52fd528 to your computer and use it in GitHub Desktop.
Kill all query running for more than 1 hour on PostgreSQL
This file contains 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
DO $$ | |
DECLARE | |
r RECORD; | |
BEGIN | |
FOR r IN | |
SELECT pid | |
FROM pg_stat_activity | |
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%' AND usename = 'write_1' AND | |
age(query_start, clock_timestamp()) < -(INTERVAL '1 hour') | |
ORDER BY age(query_start, clock_timestamp()) ASC | |
LOOP | |
PERFORM pg_cancel_backend(r.pid); | |
END LOOP; | |
END $$; |
This file contains 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
SELECT | |
pid, | |
age(query_start, clock_timestamp()) | |
FROM pg_stat_activity | |
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%' AND usename = 'write_1' AND | |
age(query_start, clock_timestamp()) < -(INTERVAL '1 hour') | |
ORDER BY age(query_start, clock_timestamp()) ASC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment