Skip to content

Blocked sessions

Requests waiting on another session, with the blocker's id, the wait type and the blocked query's text. Empty is healthy.

SQL Server · SQL
SELECT r.session_id AS blocked_session,       r.blocking_session_id AS blocking_session,       r.wait_type,       r.wait_time / 1000 AS wait_seconds,       s.login_name,       s.program_name,       LEFT(t.text, 200) AS blocked_queryFROM sys.dm_exec_requests rJOIN sys.dm_exec_sessions s ON s.session_id = r.session_idCROSS APPLY sys.dm_exec_sql_text(r.sql_handle) tWHERE r.blocking_session_id <> 0ORDER BY r.wait_time DESC

This query is part of the "SQL Server health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What the query shows

This query reads sys.dm_exec_requests and returns every request that is waiting on another session. Each row is one blocked request, with the session that blocks it, the wait type, how long it has waited, who is connected and the first 200 characters of the blocked statement.

Run it when users say the application is slow or frozen, when a job that normally takes seconds has been running for minutes, or on a schedule so you hear about blocking before the phone rings.

How to read each column

  • blocked_session: the session_id waiting for a lock.
  • blocking_session: the session_id holding what the blocked one needs. A negative value is not a session: -2 is an orphaned distributed transaction, -3 a deferred recovery transaction, -4 and -5 a latch whose owner could not be identified.
  • wait_type: usually LCK_M_S, LCK_M_U or LCK_M_X, which means a shared, update or exclusive lock request is waiting.
  • wait_seconds: wait_time in milliseconds divided by 1000, using integer division, so a wait under one second shows as 0.
  • login_name and program_name: who and which application owns the blocked session.
  • blocked_query: the text of the blocked request. The blocker text is not in this result, you look it up next.

Healthy and worrying results

An empty result is the healthy state. A row that lasts a second or two is normal, because locks are how SQL Server keeps data consistent. As a rule of thumb, look closer when the same blocked session shows up in consecutive runs, or when wait_seconds passes 30. That is a convention, not a Microsoft limit.

The head blocker is the session that appears in blocking_session but is not itself blocked. Often it is sleeping with an open transaction, because an application opened a transaction and never committed.

What to do about it

First find out what the blocker is doing. Replace 57 with the blocking session id.

Who is the blocker and what did it run last
SELECT s.session_id, s.login_name, s.host_name, s.program_name,       s.status, s.open_transaction_count, s.last_request_end_time, t.textFROM sys.dm_exec_sessions sJOIN sys.dm_exec_connections c ON c.session_id = s.session_idCROSS APPLY sys.dm_exec_sql_text(c.most_recent_sql_handle) tWHERE s.session_id = 57;

If the blocker is a forgotten transaction or a runaway job and the owner agrees, end it. KILL rolls back everything the session did, and the rollback can take as long as the work itself. Never kill a session blindly, and never kill one that is already rolling back.

End the session, then watch the rollback
KILL 57;KILL 57 WITH STATUSONLY;

For recurring blocking, fix the cause. Keep transactions short, make sure the blocked queries have an index so they lock fewer rows, and consider row versioning, which lets readers stop waiting for writers. It changes isolation behavior for the whole database and WITH ROLLBACK IMMEDIATE disconnects other sessions, so test it first.

ALTER DATABASE [YourDatabase] SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;

Limits to know

  • It is a snapshot. Blocking that starts and ends between two runs is invisible.
  • It lists only requests that are running now. A sleeping blocker has no row of its own, it appears only in the blocking_session column.
  • Seeing sessions from other users needs VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE on SQL Server 2022 and later). Without it you only see your own session.
  • On Azure SQL Database you only see requests of the connected database, and VIEW SERVER STATE cannot be granted there.

Run it on a schedule in DataRunner

Connect the SQL Server database to DataRunner, paste this query and pick a schedule with its own time zone. The result can go by e-mail, Slack, WhatsApp (beta), Telegram, Discord, a signed webhook or Google Sheets. Because a run with no rows can be skipped, you only receive a message when something is blocked. A database in a private network connects through the bridge agent, with no inbound port.

Frequently asked questions

How do I find blocked sessions in SQL Server?
Query sys.dm_exec_requests for rows where blocking_session_id is not 0. This template does that and adds the login, program and statement text. The session that blocks the others is the one that appears in blocking_session but is not blocked itself.
Is it safe to KILL a blocking session?
It is safe for the server, but the session rolls back its open transaction, and that can take as long as the work it already did. Check who owns the session and what it ran before you kill it. Use KILL with STATUSONLY to watch the rollback.
What is the difference between blocking and a deadlock?
Blocking is one session waiting for another, and it ends when the blocker finishes. A deadlock is two sessions waiting for each other, so neither can finish. SQL Server detects it and rolls back one of them as the victim.
Why is blocking_session negative?
Negative values are not session ids. They mark special owners: -2 an orphaned distributed transaction, -3 a deferred recovery transaction, -4 or -5 a latch owner that could not be identified.