How to find SQL Server Replication related jobs and T-SQL statements

Verbose log is heavily used  in replication troubleshooting. You need to find the right job to enable to verbose log. However, it's not easy to find the  jobs when you have hundreds replication jobs in one server.

 

 

Here is how:

1.Distribution agent

Following queries list all the distribution agent jobs, including push subscriptions and pull subscriptions. (You may need add more clause to customize your queries). By default, the SQL Server agent job names equal to the agent names for push subscription, unless you explicitly modify the job names.

use distribution---in distributor server
if not exists(select 1 from sys.tables where name ='MSreplservers')
begin
	select job.name JobName,a.name AgentName, a.publisher_db,a.publication as publicationName,sp.name as publisherName ,ss.name as subscriber,a.subscriber_db, a.local_job From MSdistribution_agents a
	inner join sys.servers sp on a.publisher_id=sp.server_id--publisher
	inner join sys.servers ss on a.subscriber_id =ss.server_id--subscriber
	left join msdb..sysjobs job on job.job_id=a.job_id
	where a.subscription_type <>2--- filter out the anonymous subscriber
end
else
begin
	select job.name JobName,a.name AgentName, a.publisher_db,a.publication as publicationName,sp.srvname as publisherName ,ss.srvname as subscriber,a.subscriber_db, a.local_job From MSdistribution_agents a
	inner join msreplservers sp on a.publisher_id=sp.srvid--publisher
	inner join msreplservers ss on a.subscriber_id =ss.srvid--subscriber
	left join msdb..sysjobs job on job.job_id=a.job_id
	where a.subscription_type <>2--- filter out the anonymous subscriber
end

 

Results Messages 
Job Name 
NODE 1 Test 1-NODE3\SQLAG-6 
NULL 
NO DEI 
NULL 
NULL 
Agent Nama 
NO DEI 
NO DEI 
publisher_db 
AdventureWorks 
AdventuraWorks 
AdventureWorks 
AdventureWorks 
AOVD8 
AOVD8 
publication Nama 
Tran Pub Tast I 
Tran Pub Test I 
Tran Pub Test 2 
Tran Pub Test 2 
Tran Pub Test 3 
Tran Pub Tast3 
publisherNama 
NODEI\SQLAG 
NODEI\SQLAG 
NODEI\SQLAG 
NODEI\SQLAG 
NODEI\SQLAG 
NODEI\SQLAG 
sub scriber 
NODE3\SQLAG 
NODE4\SQLAG 
NODE3\SQLAG 
NODE4\SQLAG 
NODE3\SQLAG 
NODE4\SQLAG 
subscriber db 
subdb 
subdb2 
subdb 3 
subdb 4 
subdb 5 
subdb5 
local _iob

 

For push subscription, you can use the job name directly to find the job in distributor server.

node2\sqIag (SQL Server 14.0302534 - SQLREPRO\Administrator) 
Databases 
Security 
Server Objects 
Replication 
Poly8ase 
Always On High Availability 
Management 
Integration Services Catalogs 
SQL Server Agent 
Jobs (filtered) 
Job Activity Monitor

 

For  pull subscriptions(local_job=0), you need to run following query in the subscription database in subscriber server.

use subdb2        ---in subscriber server
go
select job.name as JobName,distribution_agent as AgentName, *From MSreplication_subscriptions s inner join msdb.dbo.sysjobs job 
on s.agent_id=job.job_id 

By default, the SQL Server agent job names equal to the agent names for pull subscription, unless you explicitly modify the job names.

Results Massages 
Job Name 
NODE 1 \SGLAG-AdventuraWorks-TranPub Test 1 ASF830 
E6A7 
Agent Name 
NO DE I \SQ LAG -AdventuraWorks-Tran Pub Test I 
FD6-44659E22-FIA9F830E6A7 
publisher 
NODEI\SQLAG 
publisher_db 
AdventureWorks 
publication 
Tran Pub Test I 
independent

 

node4\sqIag (SQL Server 14.0.IDDO.16g - SQLREPRO\Administrator) 
Databases 
Security 
Server Objects 
Replication 
Poly8ase 
Always On High Availability 
Management 
Integration Services Catalogs 
SQL Server Agent 
Jobs (filtered) 
Job Activity Monitor

 

 

 

 

2.Merge agent

Following queries list all the merge agent jobs, including push subscriptions and pull subscriptions. (You may need add more clause to customize your queries). By default, the SQL Server agent job names equal to the merge agent names.

use distribution---in distributor server

if not exists(select 1 from sys.tables where name ='MSreplservers')

begin

select job.name JobName,a.name AgentName, a.publisher_db,a.publication as publicationName,sp.name as publisherName ,ss.name as subscriber,a.subscriber_db, a.local_job From MSmerge_agents a

inner join sys.servers sp on a.publisher_id=sp.server_id--publisher

inner join sys.servers ss on a.subscriber_id =ss.server_id--subscriber

left join msdb..sysjobs job on job.job_id=a.job_id

 

end

else

begin

select job.name JobName,a.name AgentName, a.publisher_db,a.publication as publicationName,sp.srvname as publisherName ,ss.srvname as subscriber,a.subscriber_db, a.local_job From MSmerge_agents a

inner join msreplservers sp on a.publisher_id=sp.srvid--publisher

inner join msreplservers ss on a.subscriber_id =ss.srvid--subscriber

left join msdb..sysjobs job on job.job_id=a.job_id

end


Results Messages 
Job Name 
NODE I \SQLAG -AdventureWorks-MergePub Test node3\sqIag - I 
NULL 
Agent Nama 
NO DE I \SQ LAG -AdventuraWorks-MargaPub Tast I 
NO DEI LAG-AZvanturaWorks-MargaPub Tastooda4\sqIag-2 
publisher_db 
AdventureWorks 
AdventuraWorks 
publication Nama 
Marga Pub Tast 
MargaPubTast 
publisherNama 
NODEI\SQLAG 
NODEI\SQLAG 
subscriber 
NODE3\SQLAG 
NODE4\SQLAG 
subscriber db 
subdb6 
subdb6 
local _iob

 

For push subscription, you can use the job name directly to find the job in distributor server.

node2\sqIag (SQL Server 14.0302534 - SQLREPRO\Administrator) 
Databases 
Security 
Server Objects 
Replication 
Poly8ase 
Always On High Availability 
Management 
Integration Services Catalogs 
SQL Server Agent 
¯ Jobs (filtered) 
Job Activity Monitor

 

 

For  pull subscriptions(local_job=0), you need to run following query in the subscription database in subscriber server.

use subdb6--in subscriber server

go

select job.name, sub.publisher,sub.publisher_db,sub.publication from  msdb..sysjobs job inner join msdb..sysjobsteps jobStep on job.job_id=jobStep.job_id

inner join MSsubscription_properties sub on sub.job_step_uid=jobStep.step_uid

 

Results Messages 
NODEI \SQLAG-AdventureWorks-MergePub O 
publisher 
NODEI\SQLAG 
publisher_db 
AdventureWorks 
publication 
Merge Pub Test

 

You can use the job name directly to find the job in subscriber server.

node4\sqIag (SQL Server 14.0.IDDO.16g - SQLREPRO\Administrator) 
Databases 
Security 
Server Objects 
Replication 
Poly8ase 
Always On High Availability 
Management 
Integration Services Catalogs 
SQL Server Agent 
Jobs (filtered) 
Job Activity Monitor

 

 

 

 

3.Snapshot agent

Following queries list all the snapshot agent jobs. (You may need add more clause to customize your queries). By default, the SQL Server agent job names equal to the snapshot agent names, unless you explicitly modify the job names.

use distribution--in distributor server

if not exists(select 1 from sys.tables where name ='MSreplservers')

begin

select job.name JobName, a.name AgentName , publisher_db,publication, s.data_source as publisher,

case publication_type

when 0 then 'Transactional'

when 1 then 'snapshot'

when 2 then 'Merge'

end as publication_type

   From MSsnapshot_agents a inner join sys.servers s on a.publisher_id=s.server_id

   inner join msdb..sysjobs job on a.job_id=job.job_id

 

end

else

begin

select job.name JobName, a.name AgentName, publisher_db,publication, s.srvname as publisher,

case publication_type

when 0 then 'Transactional'

when 1 then 'snapshot'

when 2 then 'Merge'

end as publication_type

   From MSsnapshot_agents a inner join MSreplservers s on a.publisher_id=s.srvid

   inner join msdb..sysjobs job on a.job_id=job.job_id

end

Results Messages 
Job Name 
NO DEI \SQLAGÆventureWorks-MergePub Test 4 
NO DEI LAG-AZvanturaWorks-Tran Pub Tast I -5 
NO DEI\SQLAG-AdventureWorks-TranPubTest2-6 
NODE1\SQLAG-AOVD8-TranPubTast3-7 
Agent Nama 
NO DE I \SQ LAG -AdventuraWorks-MargaPub Test 
NO DEI \SQLAG-AdventuraWorks-TranPubTest1-5 
NO DEI\SQLAG-AdventureWorks-TranPubTest2-6 
NODE1\SQLAG-AOVD8-TranPubTast3-7 
publisher_db 
AdventureWorks 
AdventureWorks 
AdventureWorks 
AOVD8 
publication 
Merge Pub Test 
Tran Pub Test I 
Tran Pub Test 2 
Tran Pub Tast3 
publisher 
NODEI\SQLAG 
NODEI\SQLAG 
NODEI\SQLAG 
NODEI\SQLAG 
publication _type 
Marga 
Transactional 
Transactional 
Transactional

You can use the job name directly to find the job in distributor server.

node2\sqIag (SQL Server 14.0302534 - SQLREPRO\Administrator) 
Databases 
Security 
Server Objects 
Replication 
Poly Base 
Always On High Availability 
Management 
Integration Services Catalogs 
SQL Server Agent 
Jobs (filtered) 
NODEI\SQLAG-AdventureWorks-MergePubTest-4 
Job Activity Monitor

 

4.Logreader agent

Following queries list all the log agent jobs, including push subscriptions and pull subscriptions. (You may need add more clause to customize your queries). By default, the SQL Server agent job names equal to the log reader agent names.

use distribution

if not exists(select 1 from sys.tables where name ='MSreplservers')

begin

select job.name JobName, a.name AgentName, publisher_db,s.name as publisher

From MSlogreader_agents a inner join sys.servers s on a.publisher_id=s.server_id

Inner join msdb..sysjobs job on job.job_id=a.job_id

end

else

begin

select job.name JobName, a.name AgentName, publisher_db,s.srvname as publisher

From MSlogreader_agents a inner join MSreplservers s on a.publisher_id=s.srvid

Inner join msdb..sysjobs job on job.job_id=a.job_id

end

Please note, all publications of same publication database share same agent job

Please note, the agent name equals to job name by default, unless user modifies the job.

Results Messages 
Job Nama 
NO DEI\SGLAG-AdventuraWorks-3 
NODE1\SQLAG-AOV084 
Agent Nama 
NO DEI\SQLAG-AdventuraWorks-3 
NODE1\SQLAG-AOVD84 
publisher_db 
AdventureWorks 
AOVD8 
publisher 
NODEI\SQLAG 
NODEI\SQLAG

 

 

node2\sqIag (SQL Server 14.0302534 - SQLREPRO\Administrator) 
Databases 
Security 
Server Objects 
Replication 
Poly8ase 
Always On High Availability 
Management 
Integration Services Catalogs 
SQL Server Agent 
Jobs (filtered) 
NODEI\SQLAG-AdventureWorks-3 
Job Activitv Monitor

 

 

 

In some complex cases, you may need to review all the T-SQL statements issued by these agent jobs.

Here is how:

 

One agent have more than one connections to the servers, they have different session id but share the same process id. Once the process id is identified, you can use this process id to filter, using SQL Server profiler trace or xevent.

1. Distribution agent

Distribution agent connects both distributor and subscriber. It has more than one connections in distributor. The application name of one connections equals to the agent name, that's how I get all the connections of one specific agent.

 

1)For example, following query returns 6 agents.

use distribution

select  *  From MSdistribution_agents

name 
9 NODEI\SQLAG-%ve 「 tu 「 aWorks-TranPubTast2-NODE4\SQLAG-S 
8 NODEI\SQLAG-Adve 「 tu 「 eWorks-T 「 anPubTest2-NODE3\SGLAG-8 
10 NODEI\SQLAG-AOVD8-TranPubTest3-NODE3\SQLAG-IO 
NODEI\SQLAG-AOVD8-TranPubTast3-NODE4\SQLAG-11 
NODEI\SQLAG-Adve 「 「 eWorks-TranPubTast1-NODE4\SQLAG-7 
NODEI\SQLAG-%ve 「 tu 「 eWoks-TranPubTestI-NODE3\SQLAG-E 
publishe 「 d a publishe 「 叼 u she 「 db 
AOVD8 
AOVD8 
ve 「 tu 「 a"' TranPubTest2 3 
Adve 「 tu 「 eWorks TranPubTest2 2 
Adve 「 tu 「 eWorks TranPubTestI 3 
ve 「 tu 「 a"' TranPubTestI 2 
叼 u C 一 on 
TranPubTest3 3 
TranPubTest3 2 
subscribe 「 subscribe 「 db subscnpt'on tpe local 」 0b id 
0604708044CCI 24 78 
SgDE425944346148880 
&QFEDI B73ED64S94A93 
&KS8 D B3114SFE252 
625A56g6FAFS274g9F6E 
2D78A4F4DED84542BD

 

2)Let's say I need to check the T-SQL of first agent.

If SQL Server trace files are collected in distributor and subscriber are collected, use the agent name to filter ApplicationName column to find the process id in distributor trace.

Then Use the process id to filter queries in distributor trace and subscriber trace.

 

If you need online troubleshoot,run following statement in distributor server.

select hostprocess as PID from sys.sysprocesses where program_name ='NODE1\SQLAG-AdventureWorks-TranPubTest1-NODE3\SQLAG-6'

 

100 % 
Resutts Messages 
hostprocess

3)It returns the process id of this agent. Then you can use the process id of find all the sessions in both distributor server and subscriber server.

In distributor server

select @@servername,*From sys.sysprocesses where hostprocess=1832

Results Massages 
(No column name) 
NODE2\SQLAG 
NODE2\SQLAG 
program _n ama 
Replication Distribution History 
NO DEI 
spid 
kpid 
blocked 
wattype 
wattima 
lastwattype 
MISCELLANEOUS 
MISCELLANEOUS 
watrasource 
dbid 
uid 
cpu 
265 
physical _io 
472 
mamusaga 
login_tima 
183521 520 
183521 723

 

In subscriber server

select @@servername,program_name,*From sys.sysprocesses where hostprocess=1832

Results Massages 
183521567 
(No column name) 
NODE3\SGLAG 
NODE3\SQLAG 
program_nama 
NODEI\SQLAG 
NODEI\SQLAG 
AdventuraWorks 
AdventuraWorks 
Tran Pub Tast I 
Tran Pub Tast I 
spid 
51 
52 
kpid 
blocked 
wattype 
wattima 
lastwattype 
MISCELLANEOUS 
MISCELLANEOUS 
watrasource 
dbid 
uid 
cpu 
2379 
physical 
mamusaga 
login_tima 
last batch 
22 43 30 
22 43 30

 

4)An alternative is to run following query in subscription database, then use the hostprocess to filter…

use subdbName

select hostprocess, *From sys.sysprocesses where spid in

(

select spid From MSsubscription_agents

)

2.Merge agent

The behavior of Merge agent is as same as distribution agent.  It connects publisher, distributor and subscriber.

1.Run following query in distributor server.

use distribution

select * From MSmerge_agents

Results Massages 
id 
name 
publisher_id 
-M ePubTestsnode3;s la I 
Marge Pub Testoode4\sqIag-2 
publisher_db 
uraWorks 
AdventuraWorks 
publication 
Merge Pub Test 
MargaPubTast 
subscriber id 
sub scriber 
subdb6 
db 
local Job 
job _id 
00823262AEOE5AE4AA9D7EFA982452D71 
OKFC448ASE86SS864182C5C1346F26E088 
profile 
anonymous 
NULL 
NULL 
subid 
subscriber name 
node3\sqIag 
node4\sqIag 
creation data 
17 59 51 393 
18 oo 45 837

 

2)Let's say I need to check the T-SQL of first agent.

If SQL Server trace files are collected in publisher,distributor and subscriber are collected, please use the agent name to filter ApplicationName column to find the process id in publisher trace.

Then Use the process id to filter queries in publisher, distributor trace and subscriber trace.

 

3)If you need online troubleshoot,. run following statement in publisher server.

select @@servername,hostprocess From sys.sysprocesses where program_name in

(

'NODE1\SQLAG-AdventureWorks-MergePubTest-NODE3\SQLAG-1'

)

(No column name) 
NODEI\SGLAG 
NODEI\SQLAG 
NODEI\SQLAG 
NODEI\SQLAG 
ho stprocess 
11472 
11472 
11472 
spid

 

3)Use this process id to filter queries in distributor and subscriber, the same way I used for distribution agent.

 

 

3.Snapshot agent

The behavior of Snapshot agent is as same as distribution agent.  It connects publisher and distributor

1.Run following query in distributor server.

use distribution

select * From MSsnapshot_agents

id 
name 
NODE1\SQLAG-AdvantLlEW'orks-TranPubTast1-5 
NO DEI \SQLAG-AZvanturaW'orks-TranPubTast2Æ 
NODE1\SQLAG-AOVD8-TranPubTast3-7 
publisher 
id 
publisher_db 
Adventu raWorks 
AdventureWorks 
AdventureWorks 
AOVD8 
publication 
Merge Pub Test 
Tran Pub Tast I 
Tran Pub Test 2 
Tran Pub Tast3 
publication _type 
Bcal Job 
job _id 
OK158CEF8 
1744451

2)Let's say I need to check the T-SQL of first agent.

If SQL Server trace files are collected in publisher and distributor are collected, please use the agent name to filter ApplicationName column to find the process id in publisher trace.

Then Use the process id to filter queries in publisher and distributor trace.

 

3)If you need online troubleshoot, run following statement in publisher server.

select @@servername,hostprocess From sys.sysprocesses where program_name in

(

'NODE1\SQLAG-AdventureWorks-MergePubTest-NODE3\SQLAG-1'

)

 

3)Use this process id to filter queries in publisher and distributor , the same way I used for distribution agent.

 

4.Logreader agent

Logreader agent is little bit different. The agent name of MSlogreader_agents does not match anything. You need to use  following pattern to filter.

The application name of logreaer agent consists of Repl-LogReader-number-publicationDBName-number.


For example, if you need to collect process id of logreader of AdventureWorks, run following query in both publisher server and distributor server.

select program_name, hostprocess,@@servername From sys.sysprocesses where program_name like 'Repl-LogReader%' and program_name like '%AdventureWorks%'

100 % 
Results Messages 
program_name 
Repl - Lo g Rea der-2-AdventureWorks-6 
ho stprocess 
5888 
(No column name) 
NODEI\SQLAG

 

100 % 
Results Messages 
program_nama 
Repl -Ing Reader-2-AdventuraWorks-6 
ho stprocess 
5888 
(No column name) 
NODE2\SQLAG

 


Comments

popular posts

SQL Server Polybase in Failover cluster instance

Tutorial: Create SQL Cluster(FCI) on RHEL

Configure multiple-subnet Always On Availability Groups and failover cluster instances by modifying CIB

Realcase: Failed to upgrade SQL Server 2016 SP2 CU11. (Installation success or error status: 1648)

SQL Server Extents, PFS, GAM, SGAM and IAM and related corruptions

Tutorial: Add a node to SQL cluster on RHEL

Password is required when adding a database to AG group if the database has a master key

You may fail to backup log or restore log after TDE certification/key rotation

SQL Server GAM corruption samples