Question

Are there any good open source or otherwise free MS SQL Server transaction blocking monitor tools? One that would detect a blocking transaction that lasts longer than X and then email an alert somewhere would be ideal.

Simple for one server. For MS SQL Express 2008 specifically but should apply to all really, or all recent.

Was it helpful?

Solution

Yes, actually SQL Server comes with such an option out-of-the box, but very few know about it and even fewer know how to use it. It is called the blocked process threshold:

Use the blocked process threshold option to specify the threshold, in seconds, at which blocked process reports are generated. The threshold can be set from 0 to 86,400. By default, no blocked process reports are produced. This event is not generated for system tasks or for tasks that are waiting on resources that do not generate detectable deadlocks.

You can define an alert to be executed when this event is generated. So for example, you can choose to page the administrator to take appropriate action to handle the blocking situation.

Once this option is enabled the system will generate a profiler event of the Blocked Process Report Event Class. The next piece of the puzzle is that this (as well as many more) Profiler event can be captured using DDL Event Notifications. Here is a practical example:

use msdb;
go

create queue events;
go

create service events on queue [events] (
    [http://schemas.microsoft.com/SQL/Notifications/PostEventNotification]);
go

create event notification [blocked_threshold_exceeded]
    on server for BLOCKED_PROCESS_REPORT
    to service N'events', N'current database';
go

exec sp_configure 'show advanced options', 1;
reconfigure
go


exec sp_configure 'blocked process threshold', 10;
reconfigure
go

-- simulate blocking
use tempdb;
go

begin transaction;
create table x (a int);
go

now, on a different session, runa query that blocks on the uncommitted transaction started above:

select * from tempdb..x

And sure enough, in 10 seconds (our configured threshold) we get a notification:

use msdb;
receive cast(message_body as xml) from [events];

<EVENT_INSTANCE>
  <EventType>BLOCKED_PROCESS_REPORT</EventType>
  <PostTime>2013-02-12T16:19:55.610</PostTime>
  <SPID>5</SPID>
  <TextData>
    <blocked-process-report monitorLoop="104441">
      <blocked-process>
        <process id="process47b946cf8" 
             waitresource="OBJECT: 2:373576369:0 " 
             waittime="18952" ...>
          <executionStack>
            <frame line="1" stmtstart="-1" ... />
          </executionStack>
          <inputbuf>
select * from x   </inputbuf>
        </process>
      </blocked-process>
      <blocking-process>
        <process status="sleeping" ....>
          <executionStack />
          <inputbuf>
create table x (a int)   </inputbuf>
        </process>
      </blocking-process>
    </blocked-process-report>
  </TextData>
...
</EVENT_INSTANCE>

You can see the last executed statement by the blocker, the current executing statement from the blocked, along with wait times and a lot more.

Wiring up the event notification to activate a procedure that sends mail using sp_send_db_mail is left as an exercise to the reader. And yes, everything mentioned above is available in Express Edition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top