// SQL snippet

Find blocking chains in SQL Server

Every blocked request with the session blocking it and that session's last batch, in one result set.

Main statement
SQL Server

The task

sys.dm_exec_requests exposes blocking_session_id: the session holding the lock each waiting request wants. Joining that back to the blocker's own request text turns a session id into a readable chain, and often reveals that the real head of the chain is an innocuous-looking SELECT inside an uncommitted transaction.

The snippet

Written for SQL Server.

SQL Server
SELECT r.session_id AS blocked_session,
       r.blocking_session_id,
       r.wait_type, r.wait_time AS wait_ms,
       r.wait_resource,
       LEFT(t.text, 140) AS blocked_query,
       bs.status AS blocker_status,
       bs.open_transaction_count AS blocker_open_trans,
       LEFT(bt.text, 140) AS blocker_last_batch
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
LEFT JOIN sys.dm_exec_sessions bs ON bs.session_id = r.blocking_session_id
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;

How it works

sys.dm_exec_requests has one row per running request, and blocking_session_id is nonzero when that request is waiting on another session. The CROSS APPLY decodes the waiting request's own SQL. The blocker is looked up through sys.dm_exec_sessions and sys.dm_exec_connections rather than dm_exec_requests, because the most common head blocker is not running anything: it ran an UPDATE inside a transaction and never committed. Such a session has no request row but does have most_recent_sql_handle, the last batch it sent, and OUTER APPLY keeps the row even when that text is unavailable. blocker_status = sleeping with blocker_open_trans above 0 is the abandoned-transaction signature. blocking_session_id > 0 skips the negative special values described below.

Variations

Head blockers and everyone queued behind them

WITH waits AS (
    SELECT session_id, blocking_session_id
    FROM sys.dm_exec_requests
    WHERE blocking_session_id > 0
), heads AS (
    SELECT DISTINCT w.blocking_session_id AS session_id
    FROM waits w
    WHERE w.blocking_session_id NOT IN (SELECT session_id FROM waits)
), chain AS (
    SELECT h.session_id, CAST(NULL AS SMALLINT) AS blocked_by, h.session_id AS head_blocker, 0 AS depth
    FROM heads h
    UNION ALL
    SELECT w.session_id, CAST(w.blocking_session_id AS SMALLINT), c.head_blocker, c.depth + 1
    FROM chain c
    JOIN waits w ON w.blocking_session_id = c.session_id
    WHERE c.depth < 50
)
SELECT head_blocker, depth, session_id, blocked_by
FROM chain
ORDER BY head_blocker, depth, session_id;

Questions that come up

The blocking_session_id is -2 or a session with no request. What now?

-2 means the lock belongs to an orphaned distributed transaction, -3 a deferred recovery transaction, and -4 that the owner could not be identified at that instant because of latch state changes. A positive id with no row in dm_exec_requests is an idle session holding locks in an open transaction; the main query still shows its last batch. sys.dm_tran_locks WHERE request_session_id = that id lists what it holds, and sys.dm_exec_input_buffer(session_id, NULL) shows its last statement.

Should I kill the head blocker?

Sometimes it is the right call, but KILL rolls back the session's open transaction, and in SQL Server a large rollback can take as long as the work did, holding its locks until it finishes. Check what the blocker is doing and how much it has written first (sys.dm_tran_database_transactions shows log bytes used per transaction). KILL <id> WITH STATUSONLY reports rollback progress afterwards.

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.