Help: SQL Server

Sharing my knowlege about SQL Server Troubleshooting Skills

  • Blog Stats

    • 2,156,912 hits
  • Select GETDATE()

    March 2026
    M T W T F S S
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031  

SQL Server Internals : What is TUF File in SQL Server?

Posted by blakhani on July 24, 2014


While answering on few forums I have realized that there are some variation of same question which I used in title of the blog.

  • What is .TUF file in Log Shipping?
  • I deleted TUF file, any problem?
  • I am not using log-shipping, still I see a TUF file.
  • What is the location of TUF file in log shipping?

TUF file stands for Transaction Undo File in SQL Server. I will try to explain little more so that you can understand the usage and importance of it.

When we perform a restore of database backup files, we have three options.

  • with recovery – database would be recovered and no further backups can be applied.
  • with norecovery – database would not be recovered and further backups can be restored. NOT be accessible to users for read operations.
  • with standby– database would not be recovered and further backups can be restored. Accessible to users for read operations.

If SQL has to allow read operation to users, it has to show only those committed data. Let’s assume a scenario to understand better.

  1. Created Employee Table.
  2. Inserted 5 employees and transaction committed.
  3. Took full backup which got completed. (F1)
  4. Started Transaction and Inserted 5 more employees
  5. Took Transaction log backup and completed (T1)
  6. Committed Transaction which was started in step 4.
  7. Took Transaction log backup and completed (T2)

If we perform restore operation of F1, we have to use either norecovery or standby because if we use “with recovery”, we won’t be able to restore other backups. Lets assume that we use “with standby”. This time a TUF file is created but would not contain much information because there is no uncommitted transaction when backup was taken. Since we are able to open database, we should be able to see 5 employee. So far, we are clear. Next, if we go ahead and apply T1 with standby and then open the database, should we see those new 5 records also? No, we should not because they were part of a transaction that was uncommitted when T1 was completed. The information stays in TUF file. When the next transaction log backup T2 is restored, SQL Server would use this TUF file along with T2 to roll forward that particular transaction. So we need both the files during restore. Now you can imagine a situation and implication of missing a TUF file. Yes, you would not be able to restore the log backups chain and need a fresh backup to be taken.

In log shipping scenario if we choose “Standby” option for the secondary server then the transaction log backup file would be restored in standby mode. As discussed earlier, this would need a TUF file to be generated automatically. The location of this file is not configurable and it has been changed in few versions of SQL. Refer KB 

Now a major question would be, how can I find the location of the TUF file given in last restore? I use a little trick here. If you are aware of default trace feature in SQL Server, you might know the restore command is logged into default trace. I have taken this idea from Standard Reports in SSMS.

declare @enable int;
select @enable = convert(int, value_in_use) from sys.configurations where name = 'default trace enabled' 
print 'default trace is NOT enabled.'
if @enable = 1  
begin 
        declare @curr_tracefilename varchar(500);
        declare @base_tracefilename varchar(500);
        declare @status int; 
        declare @indx int;   
        declare @temp_trace table ( 
                Error int
        ,       StartTime datetime
        ,       HostName sysname collate database_default null
        ,       ApplicationName sysname collate database_default  null
        ,       LoginName sysname collate database_default null
        ,       Severity int
        ,       DatabaseName sysname collate database_default null
        ,       TextData nvarchar(max) collate database_default 
        ); 
        
        select @status=status, @curr_tracefilename=path from sys.traces where is_default = 1 ;
        set @curr_tracefilename = reverse(@curr_tracefilename) 
        select @indx  = patindex('%\%', @curr_tracefilename)  
        set @curr_tracefilename = reverse(@curr_tracefilename) 
        set @base_tracefilename = left( @curr_tracefilename,len(@curr_tracefilename) - @indx) + '\log.trc'; 

        
        select  StartTime
        ,        TextData  
        ,       HostName
        ,       ApplicationName
        ,       LoginName
        ,       DatabaseName    
        from ::fn_trace_gettable( @base_tracefilename, default ) 
        where EventClass = 115
        and TextData like '%Restore%Standby%' 
        and ServerName = @@servername
        order by StartTime

end

 

Here is the output from my SQL Instance.

image

If the undo file is missing, SQL Server would raise below message. Note that TUF extension is not mandatory.

Msg 3013, Level 16, State 1, Line 8

RESTORE LOG is terminating abnormally.

Msg 3441, Level 17, State 1, Line 8

During startup of warm standby database ‘TestBackup’ (database ID 6), its standby file (‘E:\Program Files\Microsoft SQL Server\MSSQL12.SQL2014\MSSQL\Backup\TestBackup_RollbackUndo_2014-07-24_06-58-39.bak’) was inaccessible to the RESTORE statement. The operating system error was ‘2(The system cannot find the file specified.)’. Diagnose the operating system error, correct the problem, and retry startup.

 

Hope this clears some questions you might had before reading this blog. For more, you can post comment and I shall try to answer.

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in backup, Restore, SQL Server | Tagged: , , | 16 Comments »

    Tips and Tricks : Restore Using TRY…CATCH might fail with error

    Posted by blakhani on July 22, 2014


    Few days back one of my friend called me with an interesting problem. She told that when she does a regular restore of a database backup file, it works but when she does using TRY/CATCH block, it fails. The very first thing which I asked her– What is the error message? She sent me email with below message. I have changed some names to save the innocents.

    <EMAIL>

    Command which fails.

    BEGIN TRY  
        RESTORE DATABASE [TestBackup] 
        FROM  DISK = N'C:\Temp\TestBackup.bak' 
        WITH  FILE = 1,  
        MOVE N'TestBackup' TO N'C:\Temp\TestBackup.mdf',  
        MOVE N'TestBackup_log' TO N'C:\Temp\TestBackup_log.LDF',  
        NOUNLOAD,  
        STATS = 5; 
    END TRY 
    BEGIN CATCH  
                 THROW; 
    END CATCH  

    Here is the output

    52 percent processed.

    100 percent processed.

    Processed 240 pages for database ‘TestBackup’, file ‘TestBackup’ on file 1.

    Processed 2 pages for database ‘TestBackup’, file ‘TestBackup_log’ on file 1.

    Converting database ‘TestBackup’ from version 611 to the current version 706.

    Database ‘TestBackup’ running the upgrade step from version 611 to version 621.

    Database ‘TestBackup’ running the upgrade step from version 621 to version 622.

    Database ‘TestBackup’ running the upgrade step from version 622 to version 625.

    Database ‘TestBackup’ running the upgrade step from version 625 to version 626.

    Database ‘TestBackup’ running the upgrade step from version 626 to version 627.

    Database ‘TestBackup’ running the upgrade step from version 627 to version 628.

    Database ‘TestBackup’ running the upgrade step from version 628 to version 629.

    Msg 102, Level 15, State 1, Procedure usp_logError, Line 11

    Incorrect syntax near ‘200001’.

    Msg 3013, Level 16, State 1, Line 11

    RESTORE DATABASE is terminating abnormally

    Command which works

    RESTORE DATABASE [TestBackup] 
    FROM  DISK = N'C:\Temp\TestBackup.bak' 
    WITH  FILE = 1,  
    MOVE N'TestBackup' TO N'C:\Temp\TestBackup.mdf',  
    MOVE N'TestBackup_log' TO N'C:\Temp\TestBackup_log.LDF',  
    NOUNLOAD,  
    STATS = 5;

    There is NO difference in restore command. Only difference is that it is wrapped in TRY block. If any exception raised, it would be caught and thrown.

    <EMAIL>

    Since I see version upgrade messages I asked if backup was taken on lower version and restore is performed on higher version? She informed that this backup is taken on SQL Server 2005 and she wants to restore on SQL Server 2012. I also asked her to share ERRORLOG and here is the snippet

    2014-07-17 17:43:41.070 spid56 Error: 928, Severity: 20, State: 1.

    2014-07-17 17:43:41.070 spid56 During upgrade, database raised exception 102, severity 25, state 1, address 00007FFCE2DC3E04. Use the exception number to determine the cause.


    Exception 102 is “Incorrect Syntax” error. If we read the message in restore output carefully, we can see that there is a procedure usp_logError which seems to be a problem. I asked her to look at the code of usp_logError and search for 200001 and she found below:

    raiserror 200001 'Debit and Credit Mismatch'

    If we execute above piece of code in SQL 2005, it works and it’s valid whereas, it gives syntax error in SQL 2012. I looked into documentation and found below at http://msdn.microsoft.com/en-us/library/ms144262.aspx (Discontinued Database Engine Functionality in SQL Server 2014)

    Transact-SQL

    RAISERROR in the format RAISERROR integer ‘string’ is discontinued.

    Rewrite the statement using the current RAISERROR(…) syntax.

     

    I asked her to change the syntax and modify the stored procedure before taking backup. Once done, the restore with and without TRY CATCH worked like a charm.

    You can easily reproduce this error by creating below database and procedure in SQL 2005.

    Create database TestBackup 
    go 
    use TestBackup 
    go 
    Create Procedure FailureProc 
    as 
    begin 
    RAISERROR 20001 'ERROR RAISED' 
    end 
    
    

    Restoring this backup in SQL 2012 using TRY CATCH would fail.

    Tip: Whenever I see any “Error: 928, Severity: 20, State: 1.” I always look for the exception code. In this case it was 102. To convert code to text, we can use sys.messages catalog view (check image below) . While searching further, I was able to find below variations.

    2014-07-17 17:43:41.070 spid56 Error: 928, Severity: 20, State: 1.

    2014-07-17 17:43:41.070 spid56 During upgrade, database raised exception 156, severity 25, state 1, address 00007FFCE2DC3E04. Use the exception number to determine the cause.


    image

    Error 156 – Incorrect syntax near the keyword ‘%.*ls’.

    There might be many exceptions raised even without try…catch, you need to read the message and look for explanation of exception code.

    You might ask, What should be done before upgrade to avoid such situations? You should run upgrade advisor. http://msdn.microsoft.com/en-us/library/ee210467.aspx (SQL Server 2014 Upgrade Advisor). There are versions available for SQL 2012 and earlier as well. They mostly catch all errors which might come after upgrade. Interestingly, RAISERROR problem is not caught by upgrade advisor.

    http://connect.microsoft.com/SQLServer/feedback/details/708167/somewhat-incompatible-t-sql-on-sql-server-2012-rc0

    http://connect.microsoft.com/SQLServer/feedback/details/694484/denali-ctp3-upgrade-advisor-misses-deprecated-raiserror-syntax

    You can vote up if you think it would help.

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in Error, Upgrade | Tagged: , , , , , | 2 Comments »

    Script : Find Currently executing Queries, Blocking, Waits, Statement, Procedure, CPU

    Posted by blakhani on July 17, 2014


    This is one of my all time favorite script which I use most of the time in troubleshooting performance issues on live systems. This helps in findings below:

  • What is the performance bottleneck?
  • Is there any blocking? If yes, who is the blocker?
  • What are the queries which are executing currently?
  • What is the name of the stored procedure running currently?
  • Which statement in stored procedure is getting executed right now?
  • Who is consuming CPU right now? What are the high CPU queries?
  • Who is doing lots of IO right now?
  • Column heading is self-explanatory. You need to adjust ORDER BY clause based on your need. In below query, I am showing top CPU consumers at the top. (Thanks for Ghufran (f) for adding more columns)

    SELECT s.session_id
        ,r.STATUS
        ,r.blocking_session_id AS 'blocked_by'
        ,r.wait_type
        ,r.wait_resource
        ,CONVERT(VARCHAR, DATEADD(ms, r.wait_time, 0), 8) AS 'wait_time'
        ,r.cpu_time
        ,r.logical_reads
        ,r.reads
        ,r.writes
        ,CONVERT(varchar, (r.total_elapsed_time/1000 / 86400))+ 'd ' +
         CONVERT(VARCHAR, DATEADD(ms, r.total_elapsed_time, 0), 8)   AS 'elapsed_time'
        ,CAST((
                '<?query --  ' + CHAR(13) + CHAR(13) + Substring(st.TEXT, (r.statement_start_offset / 2) + 1, (
                        (
                            CASE r.statement_end_offset
                                WHEN - 1
                                    THEN Datalength(st.TEXT)
                                ELSE r.statement_end_offset
                                END - r.statement_start_offset
                            ) / 2
                        ) + 1) + CHAR(13) + CHAR(13) + '--?>'
                ) AS XML) AS 'query_text'
        ,COALESCE(QUOTENAME(DB_NAME(st.dbid)) + N'.' + QUOTENAME(OBJECT_SCHEMA_NAME(st.objectid, st.dbid)) + N'.' + 
         QUOTENAME(OBJECT_NAME(st.objectid, st.dbid)), '') AS 'stored_proc'
        --,qp.query_plan AS 'xml_plan'  -- uncomment (1) if you want to see plan
        ,r.command
        ,s.login_name
        ,s.host_name
        ,s.program_name
        ,s.host_process_id
        ,s.last_request_end_time
        ,s.login_time
        ,r.open_transaction_count
    FROM sys.dm_exec_sessions AS s
    INNER JOIN sys.dm_exec_requests AS r ON r.session_id = s.session_id
    CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
    --OUTER APPLY sys.dm_exec_query_plan(r.plan_handle) AS qp -- uncomment (2) if you want to see plan
    WHERE r.wait_type NOT LIKE 'SP_SERVER_DIAGNOSTICS%'
        OR r.session_id != @@SPID
    ORDER BY r.cpu_time DESC
        ,r.STATUS
        ,r.blocking_session_id
        ,s.session_id
    
    

    Here is the partial output

    image

    Note that since it shows currently executing request, it won’t show the request which are not doing anything like sleeping connection or awaiting command.

    Hope this helps

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in Script | Tagged: , , , , , | 18 Comments »