Skip to content

Instantly share code, notes, and snippets.

@dxe4
Created October 9, 2018 09:29
Show Gist options
  • Save dxe4/1702c21544a37a2cb7b37640a4a956a7 to your computer and use it in GitHub Desktop.
Save dxe4/1702c21544a37a2cb7b37640a4a956a7 to your computer and use it in GitHub Desktop.
postgresql locked queries
WITH _locks AS (
SELECT
PL.pid, unnest(pg_blocking_pids(PL.pid)) AS blocking_pid
FROM pg_locks PL
WHERE NOT granted
)
select
PSA.pid AS query_pid,
LEFT(query, 200) AS query,
CASE
WHEN PSA.pid = L.pid THEN 'blocked'
WHEN PSA.pid = L.blocking_pid THEN 'blocking'
END AS status,
L.pid AS blocked_query_pid,
L.blocking_pid,
PSA.application_name,
PSA.state AS transaction_state,
PSA.backend_xid,
PSA.query_start
FROM _locks L
JOIN pg_stat_activity PSA ON (
PSA.pid = L.pid OR
PSA.pid = L.blocking_pid
)
ORDER BY PSA.query_start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment