// SQL snippet

Show who blocks whom in PostgreSQL

PostgreSQL will tell you exactly which pids block a waiting session. This query turns that into one readable line per pair.

Main statement
PostgreSQL
Also covers
SQL Server MySQL

The task

pg_blocking_pids(pid) returns every session whose locks the given pid is waiting for, including across lock queues, where a session can wait behind another waiter that itself waits on a third. Before it existed (PostgreSQL 9.6), answering the same question meant a long self-join of pg_locks that matched lock modes by hand.

The snippet

Written for PostgreSQL. Versions for SQL Server and MySQL are under Variations.

PostgreSQL
SELECT blocked.pid AS blocked_pid,
       blocked.usename AS blocked_user,
       blocked.datname,
       now() - blocked.query_start AS blocked_for,
       LEFT(blocked.query, 100) AS blocked_query,
       blocking.pid AS blocking_pid,
       blocking.usename AS blocking_user,
       blocking.state AS blocking_state,
       LEFT(blocking.query, 100) AS blocking_query
FROM pg_stat_activity blocked
JOIN LATERAL unnest(pg_blocking_pids(blocked.pid)) AS bp(pid) ON true
JOIN pg_stat_activity blocking ON blocking.pid = bp.pid
ORDER BY blocked_for DESC;

How it works

pg_blocking_pids returns an empty array for a session that is not waiting on a heavyweight lock, so the LATERAL unnest yields rows only for waiters, one per (blocked, blocking) pair. blocking_state shows whether the blocker is running a statement or sitting idle in transaction; the second is the classic forgotten-transaction case. blocked_for is measured from the start of the blocked statement, which is close to the lock wait time for a statement that blocked right away. A pid that appears as a blocker but never as blocked is the head of a chain and the one to deal with. Calling pg_blocking_pids often on a busy server has a cost, because it briefly locks the lock manager's shared state, so do not run this every second from a monitor.

Variations

SQL Server equivalent

SQL Server
SELECT r.session_id AS blocked_session,
       r.blocking_session_id,
       r.wait_time AS wait_ms,
       LEFT(bt.text, 100) AS blocker_last_batch
FROM sys.dm_exec_requests r
LEFT JOIN sys.dm_exec_connections bc ON bc.session_id = r.blocking_session_id
OUTER APPLY sys.dm_exec_sql_text(bc.most_recent_sql_handle) bt
WHERE r.blocking_session_id > 0
ORDER BY r.wait_time DESC;

MySQL InnoDB lock waits

MySQL
SELECT * FROM sys.innodb_lock_waits
ORDER BY wait_age_secs DESC;

Questions that come up

Can one query have several blockers?

Yes, which is why pg_blocking_pids returns an array. If three sessions hold ACCESS SHARE locks on a table (plain SELECTs) and a fourth asks for ACCESS EXCLUSIVE to run ALTER TABLE, the fourth is blocked by all three. Anyone who queues behind the ALTER is blocked by it too, even though their own lock would not conflict with the SELECTs. The LATERAL join produces one row per blocker.

What is the difference between wait_event_type Lock and LWLock?

Lock is a heavyweight lock on a table, row, transaction id or similar object, normally held until the owning transaction ends; that is what pg_blocking_pids reports on. LWLock is an internal lightweight lock protecting shared memory structures, usually held for microseconds. Occasional LWLock waits are normal, while many sessions stuck on the same LWLock point to an internal bottleneck such as WAL insertion or buffer mapping, not to another transaction.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.