Last active
January 24, 2019 17:10
-
-
Save akiatoji/4e6e360c2b1518d9cba2d0c4bd8f11a3 to your computer and use it in GitHub Desktop.
Hunting down blocking queries in Postgres
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
-- Who is being blocked? | |
SELECT | |
pid | |
, usename | |
, pg_blocking_pids(pid) AS blocked_by | |
, query AS blocked_query | |
FROM pg_stat_activity | |
WHERE | |
cardinality(pg_blocking_pids(pid)) > 0 | |
; | |
-- Show me all your locks | |
SELECT * | |
FROM | |
pg_locks pl | |
LEFT JOIN pg_stat_activity psa ON pl.pid = psa.pid | |
; | |
-- List queries | |
SELECT | |
pid | |
, age(query_start, clock_timestamp()) | |
, usename | |
, query | |
FROM pg_stat_activity | |
WHERE | |
query NOT ILIKE '%pg_stat_activity%' AND | |
age(query_start, clock_timestamp()) IS NOT NULL | |
ORDER BY | |
query_start DESC | |
; | |
-- Show everything | |
SELECT * | |
FROM pg_stat_activity | |
; | |
-- Kill the culprit | |
SELECT | |
pg_cancel_backend(30791) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment