As the name implies the log reader is responsible for reading the log on the publishing database. Using the fn_dblog command we can peek inside the log to see what’s happening here.
When a database is set as the publisher in a replication schema, it marks dml statements against it’s published articles as for replication. What this does is it holds the transactions within its transaction log until the log agent comes by and picks up these transactions. Where the effect of this can be seen is in checkpointing, which flushes the dirty pages to disk. If a transaction has not been grabbed by the log reader a checkpoint will not flush to disk. This can cause significant headaches for an admin in that the log will continue to grow, even if the db is in simple mode or a backup log is issued.
To see how this works you can leverage fn_dblog function. Create a db in “simple mode”, then create a sample table and publish it for transactional replication. Once this is setup view the transaction log using the following command:
select * from fn_dblog(null,null)
You should noticed several records returned. Since your db is in simple mode your seeing all the transactions that have occurred since the last “checkpoint” has occurred. You can clear your log (Flush to disk) by issuing the Checkpoint command. Execute
Checkpoint
Go
select * from fn_dblog(null,null)
You’ll now notice only two records in the log, which represent the checkpoint that has occurred.
LOP_BEGIN_CKPT
LOP_END_CKPT
Now that we see how that works insert a row into your published table then issue the fn_dblog again.
Insert into table1(charfield)
values('hello')
go
select * from fn_dblog(null,null)
Depending on how your table was setup you’ll see a few more rows. If you go to the bottom of the resultset you’ll see the following results under the operation command:
LOP_BEGIN_XACT
LOP_INSERT_ROWS
LOP_COMMIT_XACT
This is the command you just issued. If you scroll horizontally here you’ll see some keys to this operation:
- Under transaction id all three rows have the same id.
- Under AllocationUnitName you’ll see the object that was just effected by the transaction
- Under description there is the command just executed and below that the record is replicated
- Under Replicated records you see the amount of records changed by the last statement.
If you’re a geek like me you can hunt all day through the resultsets, but it’s clear the change just made is set for replication. Go ahead and clear the transaction log, and view the results using the previous command:
Checkpoint
Go
select * from fn_dblog(null,null)
B
Before moving on to the next step, take a look at a couple of columns (Oldest Replicated Begin LSN/ Next Replicated End LSN). These lsn numbers are set to 0 and indicate that no active records are slated for replication. Now go ahead and stop the log reader agent on your distributor and issue some of the same commands as above.
Insert into table1(charfield)
values('hello')
go
Checkpoint
go
select * from fn_dblog(null,null)
Now the results get really interesting. Using the checkpoint doesn’t clear the log. Even though the insert is committed, the log will not clear, this is because the records marked for replication have not been retrieved by the log reader. If you look at the checkpoint rows at the bottom you’ll also see Oldest Replicated Begin LSN/ Next Replicated End LSN point to the first and last transaction respectively for the replicated data. This is one of the biggest problems experienced in transactional replication. If the log reader stops without notice the log will grow out of control and bring the system to its knees.
We can manually issue the same commands to clear the log. Run sp_replcmds against the publisher DB. You’ll retrieve the same data the agent does. The command is picked up in a hash format. However if you run a “checkpoint” then the “fn_dblog” nothing has changed the data has not been cleared. After the agent runs the sp_replcmds, it then lets the log know these have been picked up by issuing sp_repldone. This marks the transactions as being picked up. If you issue the checkpoint/fn_dblog your back to two records representing the checkpoint.
Now try to start the log reader agent, noticed it won’t. This is due to a sql server safety measure, that only one connection can be made using these ms_repl commands. To get the log reader back issue, kill your current session. Some other cool commands that can be found in BOL are:
- sp_replcounters – Give statistics such as number of commands to be replicated, Frequency, starting and ending lsn’s.
- sp_repltrans- shows transactions waiting to be replicated
- sp_replshowcmds – Shows in text form the commands that have been executed