• subscribe
July 24, 2003 12:00 AM

Track Down Troublemakers

Use sysprocesses to identify blocking processes
SQL Server Pro
InstantDoc ID #39453
Downloads
39453.zip

What's the Holdup?
After you determine which process is blocking the others, you need to find out what the process is doing that's making it hold up other processes. The process might be blocked by another process, it might be in the middle of a large operation, or it might just be holding locks that it hasn't released yet.

If the blocker process is also blocked, you might have a chain of processes blocking each other. In this case, you need to find out which process is at the head of the chain. The code in Listing 1 creates a new system stored procedure, sp_leadblocker, which reports any processes that are blocking others but that aren't themselves blocked. You can create this procedure in the master database and execute it from anywhere by issuing the command EXEC sp_leadblocker.

You still need to figure out why the lead blocker is blocking other processes. Several methods of investigation are available to help you; I recommend the following steps:

  1. Run sp_lock, passing it the SPID of the lead blocker as the parameter. Sp_lock will show you what locks that process is holding. Sometimes, just a table name will be enough information for you to determine why the process is blocking others. For example, if you see that the lead blocker is holding locks on your customer_statements table, you might surmise that someone is running monthly reports.
  2. If knowing which objects are locked isn't helpful, look at the sysprocesses columns last_batch and open_tran, which the sp_leadblocker procedure returns. An open_tran value greater than 0 means that the process has an active transaction. If the last_batch value (a datetime data type) indicates that this process hasn't submitted a batch for execution in a long time, the problem might be that the process has an open transaction. The transaction might still be open because of an application error in which the client didn't send a COMMIT command to SQL Server. Or the problem could be an ad hoc transaction that a user submitted through a query tool such as Query Analyzer. For example, if you submit the following batch, locks will remain on the titles table until you get around to submitting a COMMIT TRAN or a ROLLBACK TRAN command:
  3. USE pubs
    BEGIN TRAN
       UPDATE titles
       SET price = price * 1.1
    <execute>

    If you can't discover why the transaction is open, you might need to find the user who executed the blocking process and have her help you figure out why a transaction is open. As a last resort, you might have to kill the blocking connection.

  4. If you can't track down the user responsible for the process, you might be able to discover the last operation the process completed. For example, the DBCC INPUTBUFFER command, which takes a SPID as a parameter, returns the first 255 characters of the last batch that the process submitted. This command might help, but it might not. For example, if the process that submitted the preceding batch had a SPID of 52, I could run DBCC INPUTBUFFER(52) and get the result that Figure 2 shows. This information is useful because it shows a BEGIN TRAN statement that has no corresponding COMMIT or ROLLBACK statement. However, if process 52 had issued a SELECT statement or called a system procedure such as sp_lock after updating the titles table, the information would show only that subsequent SELECT statement, which wouldn't help you figure out why process 52 is holding locks.

Alternatively, you can use Enterprise Manager to get the same information that DBCC INPUTBUFFER provides. Open the Management folder in SQL Server, then select Current Activity. In the right pane, you'll see for every active process a row that contains much of the same information as the sysprocesses row for that process. If you double-click a row for a connection (or right-click and choose Properties), you'll get a message box that contains the same information that DBCC INPUTBUFFER returns.

The Last Resort
I hope that these suggestions and the information in sysprocesses will give you a start in tracking down blocking problems. In some cases, though, the information you get might not be enough, and you'll have to kill either a known blocker or the blocked process itself. The only way to find everything that a blocking process is doing is to use SQL Server Profiler to track all activity on your server. However, you can't set up a trace as an afterthought; you must define the trace and have it running before the problem occurs. Next month, I'll finish exploring sysprocesses by looking at some new columns that Microsoft added to the system table in SQL Server 2000 Service Pack 3 (SP3).



ARTICLE TOOLS

Comments
  • David
    4 years ago
    Apr 10, 2008

    Very Useful

  • Diana
    6 years ago
    Dec 05, 2006

    If all you want to know is who is blocking and who is blocked, you can of course get that information from the Current Activity window of the Enterprise Manager.
    However, there are two reasons to be aware of the code that can give you the same info:

    1. You can customize the code to give you exactly the info you want, including joining the locking table and the process table to other tables to get even more information. In addition, you can save output from queries in a tabular format for later analysis or trend evaluation.
    2. On a busy system (which you frequently have when trying to track down blocking problems) there is an enormous overhead to using the Enterprise Manager Current Activity window and some people find that using that GUI on a busy actually creates more blocking problems and the Current Activity Window itself can end up hanging and never returning the information you’re interested in.


    Kalen Delaney
    www.SolidQualityLearning.com

  • Sabin
    6 years ago
    Dec 04, 2006

    While this article is useful in determining how to use command line to get at blocking queries, isnt all this information already available in Enterprise Manager-->Current Activity-->Locks/Process ID

    This should list out all the processes, and you should be able to trace all the way to the top SPID of the blocker.

    What advantage does querying sysprocesses have over using the EM provide? Both have to be done in the same time frame?

  • DANIEL
    6 years ago
    Dec 03, 2006

    Very helpful. Karen's always been my favorite!

You must log on before posting a comment.

Are you a new visitor? Register Here