Checking Replication Latency with T-SQL

November 20, 2008 by Michelle Ufford
Filed under: SQL Tips, T-SQL Scripts 

This post may only appeal to small subset of DBA’s. :)

This particular script was born of a need to view replication latency in production. I had to investigate whether the replication latency issues we were experiencing in production were occurring on the publisher (us) or the subscriber (them), and address accordingly. “Why just not use Replication Monitor?”, you might ask. Good question! I would definitely use Replication Monitor if it were available to me; however, I do not have the necessary permissions to the Distributor. :(

Fortunately, SQL 2005 provides us with some tools to help: sp_postTracerToken and sp_helpTracerTokenHistory. This allows us to access the same Tracer Tokens feature that is in Replication Monitor.


Replication Monitor - Tracer Tokens

 

So that’s the backstory, and here’s the script I came up with. For those interested, it’d be pretty easy to modify this script to use a permanent table and regularly log replication latency.

CREATE PROCEDURE dbo.dba_replicationLatencyGet_sp
 
        /* Declare Parameters */
          @publicationToTest sysname        = N'yourPublicationName'
        , @replicationDelay  VARCHAR(10)    = N'00:00:30'
        , @iterations        INT            = 5
        , @iterationDelay    VARCHAR(10)    = N'00:00:30'
        , @deleteTokens      BIT            = 1
        , @deleteTempTable   BIT            = 1
AS
/*********************************************************************************
    Name:       dba_replicationLatencyGet_sp
 
    Author:     Michelle F. Ufford
 
    Purpose:    Retrieves the amount of replication latency in seconds
 
    Notes:      Default settings will run 1 test every minute for 5 minutes.
 
                @publicationToTest = change the default to your publication
 
                @replicationDelay = how long to wait for the token to replicate;
                    probably should not set to anything less than 10 (in seconds)
 
                @iterations = how many tokens you want to test
 
                @iterationDelay = how long to wait between sending test tokens
                    (in seconds)
 
                @deleteTokens = whether you want to retain tokens when done
 
                @deleteTempTable = whether or not to retain the temporary table
                    when done.  Data stored to ##tokenResults; set @deleteTempTable 
                    flag to 0 if you do not want to delete when done.
 
    Called by:  DBA
 
    Date        Initials    Description
    ----------------------------------------------------------------------------
    2008-11-20   MFU        Initial Release
*********************************************************************************
    Exec dbo.dba_replicationLatencyGet_sp
          @publicationToTest    = N'yourPublicationName'
        , @replicationDelay     = N'00:00:05'
        , @iterations           = 1
        , @iterationDelay       = N'00:00:05'
        , @deleteTokens         = 1
        , @deleteTempTable      = 1;
*********************************************************************************/
 
SET NOCOUNT ON;
SET XACT_Abort ON;
 
BEGIN
 
    /* Declare Variables */
    DECLARE @currentIteration   INT
          , @tokenID            BIGINT
          , @currentDateTime    SMALLDATETIME;
 
    IF OBJECT_ID('tempdb.dbo.##tokenResults') IS Null
    BEGIN
        CREATE TABLE ##tokenResults
                        ( iteration           INT             Null
                        , tracer_id           INT             Null
                        , distributor_latency INT             Null
                        , subscriber          VARCHAR(1000)   Null
                        , subscriber_db       VARCHAR(1000)   Null
                        , subscriber_latency  INT             Null
                        , overall_latency     INT             Null );
    END;
 
    /* Initialize our variables */
    SELECT @currentIteration = 0
         , @currentDateTime  = GETDATE();
 
    WHILE @currentIteration < @iterations
    BEGIN
 
        /* Insert a new tracer token in the publication database */
        EXECUTE sys.sp_postTracerToken 
          @publication = @publicationToTest,
          @tracer_token_id = @tokenID OUTPUT;
 
        /* Give a few seconds to allow the record to reach the subscriber */
        WAITFOR Delay @replicationDelay;
 
        /* Store our results in a temp table for retrieval later */
        INSERT INTO ##tokenResults
        (
            distributor_latency
          , subscriber
          , subscriber_db
          , subscriber_latency
          , overall_latency
        )
        EXECUTE sys.sp_helpTracerTokenHistory @publicationToTest, @tokenID;
 
        /* Assign the iteration and token id to the results for easier investigation */
        UPDATE ##tokenResults
        SET iteration = @currentIteration + 1
          , tracer_id = @tokenID
        WHERE iteration IS Null;
 
        /* Wait for the specified time period before creating another token */
        WAITFOR Delay @iterationDelay;
 
        /* Avoid endless looping... :) */
        SET @currentIteration = @currentIteration + 1;
 
    END;
 
    SELECT * FROM ##tokenResults;
 
    IF @deleteTempTable = 1
    BEGIN
        DROP TABLE ##tokenResults;
    END;
 
    IF @deleteTokens = 1
    BEGIN
       EXECUTE sp_deleteTracerTokenHistory @publication = @publicationToTest, @cutoff_date = @currentDateTime;
    END;
 
    SET NOCOUNT OFF;
    RETURN 0;
END
Go

Comments

5 Comments on Checking Replication Latency with T-SQL

  1. ILKirk on Sat, 22nd Nov 2008 6:04 pm
  2. You know, I was sitting on the plane coming home from PASS and I thought – I really need to write a script with those two procedures to log and monitor replication latency… and then this.

    Thanks for sharing – I’ll likely be testing this Monday and will let you know if I spot anything wonky!

  3. Michelle Ufford on Sat, 22nd Nov 2008 6:47 pm
  4. You’re welcome! Yes, please do let me know if anything pops up. Right now this proc just tells me how replication is currently performing… I’m toying with the idea of modifying this proc to log regularly for long-term analysis.

    So… any favorite tips / moments from PASS? :)

  5. ILKirk on Sat, 22nd Nov 2008 7:36 pm
  6. A) The Transactional Replication session – I was so happy to have chosen that one, considering how much I’ve had to deal with replication in my current post lately.

    B) Realizing I’ve been missing out on so much by sort of skipping the online community – the people in Twitter, the various communities, blogs, etc. It’s driving me to be more active…

    C) Buck Woody’s amazingly thorough and amusing presentation on Powershell – I was already excited about PS and now I am moreso.

  7. Replication Monitor : SQL Fool on Tue, 25th Nov 2008 3:05 pm
  8. [...] my last blog post, I provided a script to view replication latency.  Ian Kirk took the script and ran with it, adding centralized execution and permanent logging. [...]

  9. jessica on Wed, 20th Jan 2010 11:36 pm
  10. this is a great post!, how would this sp change if i have over a 100 publications?

Tell me what you're thinking...
and oh, if you've enjoyed the post, please consider subscribing to my blog.