Azure SQL Database: blocking history with Extended Events (blocked process report, lock timeout, escalation)
A second XEvents session to Blob to find out afterwards who blocked whom: blocked process reports, lock timeouts and lock escalations, with the XML read query and the Azure SQL Database limits.
A five-minute blocking at 3 a.m., and the next morning everybody asks: "who was blocking whom?". DMVs only answer in the present. For the morning after, I use a second Extended Events session, in the same Blob container and with the same managed identity credential as my errors and deadlocks session.
| Event | What it captures |
|---|---|
sqlserver.blocked_process_report |
One XML report per task blocked too long: the blocked and the blocking session (spid, login, host, application, isolation, wait resource, last statement) |
sqlserver.lock_escalation |
Escalations to a table lock: object, mode, cause, number of escalated locks |
What about lock timeouts? The lock_timeout_greater_than_0 event is refused by Azure SQL Database (Msg 25743). No harm done: every lock timeout raises error 1222, already captured by error_reported in my errors and deadlocks session.
flowchart LR
A["Blocking session"] -->|"lock held"| B["Blocked session"]
B --> C["blocked_process_report"]
C --> D["XEvents session xe_locks"]
D --> E["Blob container (.xel)"]
E --> F["XML query: who blocked whom"]
0. What Azure SQL Database really accepts
sys.dm_xe_objects lists events that Azure SQL Database then refuses: the only real test is CREATE EVENT SESSION. When it fails nothing is created: drop the event and run it again. blocked_process_report is accepted; Microsoft Learn does not list it among the advised events and the blocked process threshold cannot be set (no sp_configure): it fires at the platform threshold. lock_escalation: remove it if your database refuses it.
The real field names, to adapt the read queries if needed:
SELECT c.object_name, c.name AS field_name, c.type_name
FROM sys.dm_xe_object_columns AS c
WHERE c.object_name IN (N'blocked_process_report', N'lock_escalation')
AND c.column_type = 'data'
ORDER BY c.object_name, c.column_id;
1. Create
Master key and credential: see the article on the errors and deadlocks session (they are shared).
CREATE EVENT SESSION [xe_locks]
ON DATABASE
ADD EVENT sqlserver.blocked_process_report
(
ACTION (sqlserver.database_name)
),
ADD EVENT sqlserver.lock_escalation -- remove it if refused, or if the files grow too fast
(
ACTION
(
sqlserver.database_name, sqlserver.username,
sqlserver.client_hostname, sqlserver.client_app_name,
sqlserver.session_id, sqlserver.sql_text
)
)
ADD TARGET package0.event_file
(
SET filename = N'https://stdbacontoso.blob.core.windows.net/xevents/xe_locks.xel',
max_file_size = 100,
max_rollover_files = 10
)
WITH
(
STARTUP_STATE = ON,
EVENT_RETENTION_MODE = ALLOW_SINGLE_EVENT_LOSS,
MAX_DISPATCH_LATENCY = 30 SECONDS
);
ALTER EVENT SESSION [xe_locks] ON DATABASE STATE = START;
2. Read: who blocked whom
Run it in the traced database: object names are resolved in the current database.
DECLARE @file nvarchar(4000) =
(
SELECT CAST(t.target_data AS xml).value('(EventFileTarget/File/@name)[1]', 'nvarchar(4000)')
FROM sys.dm_xe_database_session_targets AS t
JOIN sys.dm_xe_database_sessions AS s ON s.address = t.event_session_address
WHERE s.name = N'xe_locks' AND t.target_name = N'event_file'
);
SELECT
x.value('(event/@timestamp)[1]', 'datetime2(3)') AS ts_utc,
x.value('(event/data[@name="duration"]/value)[1]', 'bigint') / 1000 AS blocked_ms,
COALESCE(x.value('(event/data[@name="lock_mode"]/text)[1]', 'nvarchar(60)'),
x.value('(event/data[@name="lock_mode"]/value)[1]', 'nvarchar(60)')) AS lock_mode,
OBJECT_SCHEMA_NAME(x.value('(event/data[@name="object_id"]/value)[1]', 'int')) + N'.'
+ OBJECT_NAME(x.value('(event/data[@name="object_id"]/value)[1]', 'int')) AS object_name,
r.value('(blocked-process/process/@spid)[1]', 'int') AS blocked_spid,
r.value('(blocked-process/process/@loginname)[1]', 'nvarchar(256)') AS blocked_login,
r.value('(blocked-process/process/@clientapp)[1]', 'nvarchar(256)') AS blocked_app,
r.value('(blocked-process/process/@waitresource)[1]', 'nvarchar(256)') AS wait_resource,
r.value('(blocked-process/process/inputbuf)[1]', 'nvarchar(max)') AS blocked_statement,
r.value('(blocking-process/process/@spid)[1]', 'int') AS blocking_spid,
r.value('(blocking-process/process/@loginname)[1]', 'nvarchar(256)') AS blocking_login,
r.value('(blocking-process/process/@clientapp)[1]', 'nvarchar(256)') AS blocking_app,
r.value('(blocking-process/process/@status)[1]', 'nvarchar(60)') AS blocking_status,
r.value('(blocking-process/process/@trancount)[1]', 'int') AS blocking_trancount,
r.value('(blocking-process/process/inputbuf)[1]', 'nvarchar(max)') AS blocking_statement
FROM (SELECT CAST(event_data AS xml) AS x
FROM sys.fn_xe_file_target_read_file(@file, NULL, NULL, NULL)
WHERE object_name = N'blocked_process_report') AS b
CROSS APPLY b.x.nodes('(event/data[@name="blocked_process"]/value/blocked-process-report)[1]') AS n(r)
ORDER BY ts_utc DESC;
Escalations are read the same way, with object_name = N'lock_escalation' and the fields mode, object_id, escalation_cause, escalated_lock_count.
3. Clean up
ALTER EVENT SESSION [xe_locks] ON DATABASE STATE = STOP;
DROP EVENT SESSION [xe_locks] ON DATABASE;
-- Credential and master key: only when no other session writes to the container
Lessons learned
- A
blocking_statusofsleepingwithblocking_trancount> 0 is almost always an application that hit a timeout and never issued aROLLBACK. The attentions of the errors session confirm it to the second. - The blocker's
inputbufis only its last statement, not necessarily the one that took the lock earlier in the transaction. For the full "why", you have to trace the session's own statements. - Tracing every lock (
lock_acquired) is a false good idea: millions of events, a slower database and a full container within minutes. - For a blocking happening now, the trace comes too late: I use my DMV query who, why, how.