从MSdistribution_history得到已经同步到订阅库的最后一个事务序列号

本文介绍了在SQL Server中,如何通过Logreader Agent和Distribution Agent跟踪并同步事务到订阅库,特别是在使用transactional replication时。Logreader Agent从发布者日志中获取标记为复制的事务,并将它们插入到分布数据库的系统表中。Distribution Agent随后将这些更改分发到订阅者,并在MSdistribution_history表中记录每个已分发的事务序列号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

With transactional replication, the Logreader Agent invokes the sp_MSadd_replcommands27hp stored procedure to insert the log records from the publisher to the distribution database. The Distribution Agent invokes the sp_MSget_repl_commands stored procedure  to select the commands that will distributes them to the subscriber.

 

The Logreader Agent insert the log records include xact_seqno column to the MSRepl_transactions and MSRepl_commands system table. 

 

The Distribution Agent distributes the data from the MSRepl_commands system table to the subscriber, and insert  a record (into MSdistribution_history )for every  xact_seqno which transaction had been distribute to the suscriber.

 

detial description:

The Log Reader Agent runs either continuously or according to a schedule you establish at the time the publication is created. When executing, the Log Reader Agent first reads the publication transaction log (the same database log used for transaction tracking and recovery during regular SQL Server 2000 operations) and identifies any INSERT, UPDATE, and DELETE statements, or other modifications made to the data transactions that have been marked for replication. Next, the agent batch copies those transactions to the distribution database at the Distributor. The Log Reader Agent uses the internal stored procedure sp_replcmds to get the next set of commands marked for replication from the log. The distribution database then becomes the store-and-forward queue from which changes are sent to Subscribers. Only committed transactions are sent to the distribution database.

There is a one-to-one correspondence between transactions on the Publisher and replication transactions in the distribution database. One transaction stored in MSrepl_transactions can consist of one or more commands and each command can be broken up along a 500-Unicode-character boundary in the MSrepl_commands table. After the entire batch of transactions has been written successfully to the distribution database, it is committed. Following the commit of each batch of commands to the Distributor, the Log Reader Agent calls sp_repldone to mark where replication was last completed. Finally, the agent marks the rows in the transaction log that are ready to be truncated. Rows still waiting to be replicated are not truncated. The transaction log on the Publisher can be dumped without interfering with replication, because only transactions not marked for replication are purged.

 

other web link:

http://www.yesky.com/imagesnew/software/tsql/ts_sp_repl3_7ug5.htm  

http://www.yesky.com/imagesnew/software/tsql/ts_sp_repl3_21df.htm

http://www.yesky.com/imagesnew/software/tsql/ts_sp_repl3_1d2r.htm

 

验证方法

 

declare @newtime nchar(100)
--get last_xact_seqno
select xact_seqno as last_xact_seqno from MSdistribution_history
where agent_id = 6 and timestamp in(select max(timestamp)from MSdistribution_history )

 

set @newtime = convert(nchar(100),getdate(),114)
insert into MARKET_P4116.testDenny.dbo.testTable values (@newtime,'')

select max(xact_seqno) as dist_max_xact from  dbo.MSrepl_transactions

select * from MARKET_P4116.testDenny.dbo.testTable where keyfield = @newtime
select * from [MARKET_P4116/SQL2005DV3].asdf.dbo.testTable where keyfield = @newtime

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值