Last active
November 30, 2022 09:27
-
-
Save checkaayush/15029ddc9baa2861df8290fefb4a88f5 to your computer and use it in GitHub Desktop.
Finding and killing long running PostgreSQL queries
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
-- List all long-running queries | |
SELECT | |
pid, | |
now() - pg_stat_activity.query_start AS duration, | |
query, | |
state | |
FROM pg_stat_activity | |
WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes'; | |
-- Soft kill query by process ID | |
SELECT pg_cancel_backend(<pid of the process>); | |
-- Hard kill query by process ID | |
SELECT pg_terminate_backend(<pid of the process>); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment