Learn more about SQL Server tools

mssqltips logo
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

 
Untangle TempDB Performance with SQL Diagnostic Manager - Free Webinar
 

Script to find oldest open SQL Server transaction


By:   |   Last Updated: 2016-11-22   |   Comments (2)   |   Related Tips: More > Scripts

Problem

You have a database that is set to use the SIMPLE recovery model, but nevertheless you notice that the transaction log file continues to grow even though the simple recovery model is supposed to delete the inactive part of the log file when transactions are committed. Clearly there are open transactions that are filling up the log file. You want a simple tool to quickly identify which statements are currently running as the open transactions are filling up the database log file. How can this be accomplished using basic T-SQL?

Solution

The solution involves creating a stored procedure that uses two important DBCC statements: DBCC OPENTRAN and DBCC INPUTBUFFER.

DBCC OPENTRAN helps identify active transactions that may be preventing log truncation. DBCC OPENTRAN displays information about the oldest active transaction and the oldest distributed and non-distributed replicated transactions, if any, within the transaction log of the specified database, so that it can be terminated or examined.

DBCC INPUTBUFFER allows us to display the last T-SQL statement that was sent from the client to an instance of SQL Server by using the session ID we can see what statements were last issued.

Stored Procedure to Identify Open Transactions in SQL Server

The procedure runs DBCC OPENTRAN and gets its details and then takes the oldest active session id as the input parameter for DBCC INPUTBUFFER. This gives us the T-SQL statement for the oldest transaction in our database.

Here is the stored procedure. I created in the Northwind database for this demo.

USE Northwind
go

CREATE PROCEDURE [dbo].[GetOldestOpenTransactionCommand]
AS
BEGIN
 SET NOCOUNT ON

 DECLARE @OpenTransactions TABLE (
  ActiveTransaction VARCHAR(25)
  ,Details SQL_VARIANT
  );
 DECLARE @OpenTransactionsCommands TABLE (
  EventType VARCHAR(100)
  ,Params INT
  ,ActiveTransactionText VARCHAR(500)
  );

 -- Execute the command, putting the results in the table.  
 INSERT INTO @OpenTransactions
 EXEC ('DBCC OPENTRAN WITH TABLERESULTS, NO_INFOMSGS');

 DECLARE c_OpenTransactionsSID CURSOR
 FOR
 SELECT Details AS transSID
 FROM @OpenTransactions
 WHERE upper(rtrim(ltrim(ActiveTransaction))) LIKE '%OLDACT%SPID%';

 DECLARE @tranSID SQL_VARIANT;
 DECLARE @dbcc VARCHAR(100);

 OPEN c_OpenTransactionsSID;

 FETCH NEXT
 FROM c_OpenTransactionsSID
 INTO @tranSID;

 WHILE (@@FETCH_STATUS = 0)
 BEGIN
         PRINT str(cast(@tranSID AS INT))
  SET @dbcc = 'DBCC INPUTBUFFER(' + str(cast(@tranSID AS INT)) + ') WITH NO_INFOMSGS'

  INSERT INTO @OpenTransactionsCommands
  EXEC (@dbcc)

  FETCH NEXT
  FROM c_OpenTransactionsSID
  INTO @tranSID;
 END

 CLOSE c_OpenTransactionsSID;

 DEALLOCATE c_OpenTransactionsSID;

 SELECT ActiveTransactionText
 FROM @OpenTransactionsCommands;

 SET NOCOUNT OFF

 RETURN
END 
GO

Example Use

Let's assume that in the Northwind database someone opens a transaction, but never closed it:

CREATE TABLE X (A INT);
GO
begin tran
  insert into x values (100);

In another session we can execute our new procedure:

use Northwind
go
exec dbo.GetOldestOpenTransactionCommand
go

Here we can see the T-SQL statement and the SPID which is 55 shown on the Messages tab.

Code for Active Transaction in SQL Server

SPID for Active Transaction in SQL Server
Next Steps
  • The procedure was tested with SQL 2012 Developer edition on my personal server.
  • The procedure uses the legacy DBCC OPENTRAN and DBCC INPUTBUFFER commands, so it is compatible with older versions of SQL Server including 2000, 2005, 2008, etc.
  • In the case of several open transactions that fills up the log, you can execute the procedure several times and after each time issue a KILL statement for the oldest transaction found by the procedure. The next procedure execution will show you the next oldest transaction and so on.


Last Updated: 2016-11-22


next webcast button


next tip button



About the author
MSSQLTips author Eli Leiba Eli Leiba is a senior application DBA, a teacher and a senior database consultant with 19 years of RDBMS experience.

View all my tips





Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Email me updates 


Signup for our newsletter
 I agree by submitting my data to receive communications, account updates and/or special offers about SQL Server from MSSQLTips and/or its Sponsors. I have read the privacy statement and understand I may unsubscribe at any time.



    



Thursday, December 01, 2016 - 3:43:33 AM - Thomas Franz Back To Top

Nice procedure. The benefit of this procedure is, that it shows me the main statement / first call, while other procedures (as sp_WhoIsActive or the other linked function) shows only the current.

And if you have an "master" procedure that calls x sub procedures which may call other procedures which do some havy write jobs, which would cause triggers to fire, which calls other procedures again... it could be very nice to know the main culprit....


Tuesday, November 22, 2016 - 1:32:01 AM - jeff_yao Back To Top

 Here is probably another concise and rich-in-content solution

 

http://www.sqlskills.com/blogs/paul/script-open-transactions-with-text-and-plans/


Learn more about SQL Server tools