Search code examples
sql-server-2005database-mirroring

How to find exact time of last received transaction on asynchronous mirror (SQL Server 2005)?


I need to provide users with exact time of data integrity in case of forced service with possible data loss.

I guess that I can find last LSN using:

SELECT [mirroring_failover_lsn] 
FROM [master].[sys].[database_mirroring]

But that won't give me exact time.


Solution

  • Read How to read and interpret the SQL Server log. You'll see that LOP_BEGIN_XACT contains a timestamp. Given an LSN, you can analize the log and find all pending transactions (that is all xact_ids that do not have a commit or rollback logged before the given LSN). All the pending transations will be rolled back in case of failover. This will be the data lost if a forced failover occurs. There will be a number of transactions pending that will be undone, and these various transactions have started at various times. If you want to attach a 'exact time of data integrity' then you can say that no data loss will occur for anything earlier than the earliest pending lop_begin_xact. Eg. given the following log stream:

    +-----+-----------+---------+------------+
    | LSN | Operation | xact_id | timestampt |
    +-----+-----------+---------+------------+
    |  1  |INSERT     |  1      |            |
    |  2  |BEGIN_XACT |  2      | 12:00      |
    |  3  |INSERT     |  1      |            |
    |  4  |BEGIN_XACT |  3      | 12:02      |
    |  5  |COMMIT_XACT|  1      |            |
    |  6  |INSERT     |  2      |            |
    |  7  |INSERT     |  3      |            |
    |  8  |COMMIT_XACT|  3      |            |
    |  9  |COMMIT_XACT|  2      |            |
    

    Lets say that the mirroring failover LSN is 8. In this case you can say that not data loss will occur earlier than 12:00, because xact_id 2 is not committed at LSN 8 and therefore it will be rolled back. Note that xact_id 3 is commited by LSN 8 so it won't be lost, even though it has a later time stamp. So your timestamp is no t absolute, this is why I say 'no data loss will occur earlier than...' rather than 'data after ... will be lost'.