DBAtlas · Database operations atlas

Find blocking / lock-waiting sessions

Oracle

SELECT blocking_session, sid, serial#, username,
       sql_id, seconds_in_wait, event
FROM v$session
WHERE blocking_session IS NOT NULL;

A session with blocking_session populated is blocked by the SID in the blocking_session value.

PostgreSQL

SELECT pid, usename, pg_blocking_pids(pid) AS blocked_by,
       wait_event, query
FROM pg_stat_activity
WHERE cardinality(pg_blocking_pids(pid)) > 0;

The blocked_by array gives the PIDs blocking this pid.

MySQL

SELECT * FROM sys.innodb_lock_waits\G
-- MySQL 8: SELECT * FROM performance_schema.data_lock_waits;

The sys schema presents the waiting and blocking trx_id / thread in readable form.

SQL Server

SELECT r.session_id, r.blocking_session_id,
       r.wait_type, r.wait_time, r.wait_resource
FROM sys.dm_exec_requests r
WHERE r.blocking_session_id <> 0;

Requests with blocking_session_id <> 0 are blocked. A recursive query can be built to follow the chain.

MongoDB

db.currentOp({ "waitingForLock": true })

WiredTiger locks at document/collection level. You can terminate long operations with killOp.

Redis

# Not applicable: Redis commands run atomically one at a time; there are no row locks.
# Closest diagnostic: SLOWLOG GET 10

Blocking usually stems from a single long-running command (e.g. KEYS *, a large SORT); SLOWLOG shows them.

Open in DBAtlas →