Created
July 10, 2019 18:10
-
-
Save dfcarpenter/48cd384e1d21176041de8478fccf0e30 to your computer and use it in GitHub Desktop.
Find active long running queries and tables they lock
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 | |
psa.datname as database, | |
psa.query as current_query, | |
clock_timestamp() - psa.xact_start AS transaction_age, | |
array_agg(distinct c.relname) AS tables_with_locks | |
FROM pg_catalog.pg_stat_activity psa | |
JOIN pg_catalog.pg_locks l ON (psa.pid = l.pid) | |
JOIN pg_catalog.pg_class c ON (l.relation = c.oid) | |
JOIN pg_catalog.pg_namespace ns ON (c.relnamespace = ns.oid) | |
WHERE psa.pid != pg_backend_pid() | |
AND ns.nspname != 'pg_catalog' | |
AND c.relkind = 'r' | |
AND psa.xact_start < clock_timestamp() - '5 seconds'::interval | |
GROUP BY psa.datname, psa.query, psa.xact_start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment