This procedure allows you set a time threshold for collecting blocking information. When a process is blocked for longer than the time threshold, a record will be added to the blocking table. The XML results can be viewed by running the sample TSQL script.
How to set up SQL Agent to collect blocking information:
1. Set the sp_configure ‘Blocked Process Threshold’ option to the number of seconds you want to allow a process to wait before the blocking information is collected:
SP_CONFIGURE 'blocked process threshold', 2
Go
RECONFIGURE
Go
2. Create a database ‘Block_Check’ and table ‘Block_Info’ to contain the data collected:
CREATE DATABASE Block_Check
Go
USE Block_Check
Go
CREATE TABLE Block_Info ([Time] datetime, [Data] XML)
Go
3. Create a SQL Agent Job to process the SQL Agent Alert and the Alert.
Note that you will need to set the variable ‘@owner_login_name’ in the script to a valid login on your SQL Server:
USE [msdb]
GO
/****** Object: Job [Respond to blocks] Script Date: 05/17/2013 13:13:31 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [[Uncategorized (Local)]]] Script Date: 05/17/2013 13:13:31 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'Respond to blocks',
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'No description available.',
@category_name=N'[Uncategorized (Local)]',
@owner_login_name=N'NORTHAMERICA\jackcon', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [Step1] Script Date: 05/17/2013 13:13:31 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Step1',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'INSERT INTO Block_Info
([Time], Data)
VALUES (getdate(), N''$(ESCAPE_SQUOTE(WMI(TextData)))'')',
@database_name=N'Block_Check',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
/****** Object: Alert [Blocked Process] Script Date: 05/17/2013 12:52:06 ******/
EXEC msdb.dbo.sp_add_alert @name=N'Blocked Process',
@message_id=0,
@severity=0,
@enabled=1,
@delay_between_responses=0,
@include_event_description_in=0,
@category_name=N'[Uncategorized]',
@wmi_namespace=N'\\.\root\Microsoft\SqlServer\ServerEvents\SQL2008R2',
@wmi_query=N'SELECT * FROM BLOCKED_PROCESS_REPORT',
@job_id= @jobid
GO
4. Set up SQL Agent so values are substituted for tokens in alerts:
5. After creating a test blocking scenario, you can use the below script to view the captured data:
Use Block_Check
Go
WITH BlockingTransactions
AS (SELECT [Time], Data AS message_in_xml FROM Block_Check.dbo.Block_Info)
SELECT
[Time]
,block.value('(blocked-process/process/@currentdb)[1]', 'integer') AS Database_ID
,block.value('(blocked-process/process/@spid)[1]', 'integer') AS Blocked_SPID
,block.value('(blocked-process/process/@loginname)[1]', 'nvarchar(max)') AS Blocked_LoginName
,block.value('(blocked-process/process/inputbuf)[1]', 'nvarchar(max)') AS Blocked_Input_Buffer
,block.value('(blocking-process/process/@spid)[1]', 'integer') AS Blockng_SPID
,block.value('(blocking-process/process/@loginname)[1]', 'nvarchar(max)') AS Blocking_LoginName
,block.value('(blocking-process/process/inputbuf)[1]', 'nvarchar(max)') AS Blocking_Input_Buffer
FROM BlockingTransactions b
CROSS APPLY message_in_xml.nodes ('//TextData/blocked-process-report') AS t(block)
Order by [Time]
Note: This query can be modified to display other info contained in the XML column of the Block_Info table.