SQL Server Replication Troubleshooting Tips - Part 1
Field Notes: Essential SQL Server Replication Troubleshooting Tips
Managing SQL Server replication often involves encountering specific, sometimes frustrating, issues with agents, subscriptions, and synchronization. Drawing from a collection of practical solutions observed in the field, this post provides actionable tips and SQL code snippets to help diagnose and resolve common replication problems.
I. Subscription and Server Validation
Ensuring subscriptions are correctly configured and active is fundamental to successful replication.
1. Validating Subscription Server Names (Tip 1)
To verify if all subscriptions are created on the correct server, execute the following SQL code in the distribution database:
use distribution
go
select distinct ' if exists (select distinct srvname from ' + publisher_db + '..syssubscriptions where dest_db<>''virtual'' and srvname<>@@ServerName) print ''Invalid server'' ' from MSpublications
After executing the resulting query, check the output for the server name.
2. Reactivating Inactive Subscriptions (Tip 2)
If you encounter the message: “The subscription(s) have been marked inactive and must be reinitialized”, you can manually update a system table in the distribution database to change the subscription status to active (status=2).
For a single subscription, use the following code, replacing publisher_db and article_id:
use distribution
go
update MSsubscriptions set status=2 where publisher_db='PublicsherName' and article_id=4
If several subscriptions share the same agent (independent_agent=0), you must update the status of all of them:
select * from MSsubscriptions where publisher_db='PubDB' and [status]=0 and independent_agent=0
update MSsubscriptions set [status]=2 where publisher_db='PubDB' and [status]=0 and independent_agent=0
However, if the publisher and subscriber are truly out of sync, re-initialization is necessary.
3. Recreating Uninitialized Subscriptions (Tip 3)
If you receive the error, “[0%] A snapshot was not generated because no subscriptions needed initialization,” you can drop the existing subscription and then recreate it using @synch_type =’automatic’. This action allows the subscription to drop the destination, create the schema, and synchronize the data. Use sp_addsubscription for this process.
II. Agent Management and Configuration
Replication agents (Snapshot, Log Reader, Push/Pull) sometimes require manual status changes or enhanced logging to diagnose failures.
4. Setting Up Agent Output Logging (Tips 4)
To enable output logging for a replication agent (like the Snapshot Agent, Log Reader Agent, or Push/Pull Subscription Agent), navigate to the Replication Monitor node in SQL Enterprise Manager on the Distributor.
- Identify the agent related to the problematic publication.
- Right-click the agent and select Agent Properties.
- Go to the Steps tab and edit the Run Agent step.
- At the end of the command string, add the following parameters:
-Output C:\Temp\OUTPUTFILE.txt -Outputverboselevel [0|1|2]You must specify either 0, 1, or 2 after the
-Outputverboselevelparameter. - Save changes. If the agent runs continuously, stop and restart it for SQL Server to begin logging messages to the specified file, appending output if the file already exists.
(Note: For SQL Server 2005, consult the documentation on “How to: Work with Replication Agent Profiles” for setting up output parameters.)
5. Changing Snapshot Agent Status (Tip 5)
If you need to manually change a snapshot agent’s status, for example, to mark it as successful (runstatus=2), use the following code in the distribution database:
use distribution
go
/* Status codes: 1 = Start. 2 = Succeed. 3 = In progress. 4 = Idle. 5 = Retry. 6 = Fail. */
select * from MSsnapshot_agents where publication like '%TrueFalse%'
update MSsnapshot_history set runstatus=2 where agent_id=<agent_id>
6. Adding a Missing Snapshot Agent (Tip 6)
If a Snapshot Agent is missing from a publication, the subscription cannot be re-initialized. You can add it using Sp_addpublication_snapshot in the Publisher database:
use <Publisher_DB> go
EXEC Sp_addpublication_snapshot @publication = N'PublicationName', @frequency_type = 1, @frequency_interval = 0, @frequency_relative_interval = 0, @frequency_recurrence_factor = 0, @frequency_subday = 0, @frequency_subday_interval = 0, @active_start_time_of_day = 0, @active_end_time_of_day = 235959, @active_start_date = 0, @active_end_date = 0, @job_login = NULL, @job_password = NULL, @publisher_security_mode = 1
7. Addressing “Login failed for sa” during Setup (Tip 7)
When setting up replication on a new server, if replication fails with the error “Login failed for sa” (specifically failing on LogReader step 2), the Log Reader Agent cannot start. The fix is to Right click on the Replication folder in SSMS, select Update Replication Passwords…, and update the password for the login used by SQL Server Agent.
III. Synchronization and Data Issues
When replication errors occur due to log sequence number issues or missing stored procedures, specific commands are needed.
8. Dealing with LSN Errors (Tip 8)
If the Log Reader Agent failed to construct a replicated command from log sequence number (LSN), you can try resetting replication and flushing commands using these system procedures:
EXEC sp_repldone @xactid = NULL, @xact_segno = NULL, @numtrans = 0, @time = 0, @reset = 1
EXEC sp_replflush
9. Generating Missing Replication Stored Procedures (Tip 9)
If you find that replication stored procedures (repl sprocs) are missing for a subscription, you can generate them using sp_scriptpublicationcustomprocs:
Exec sp_scriptpublicationcustomprocs 'PublicationName' – Publication Name
10. Monitoring Pending Transactions (Tip 10)
The distribution database contains several dynamic management views (DMVs) useful for viewing pending transactions and their details:
sys.dm_repl_traninfosys.dm_tran_active_transactionssys.dm_tran_current_transactionsys.dm_tran_database_transactions
IV. Setup and Cleanup Procedures
These tips cover initialization types, managing jobs, and completely removing replication.
11. Initializing Subscriptions and Snapshot Generation (Tip 11)
If you create a fresh replication setup and encounter “0% snapshot was generated because no subscriptions needed initialization,” it indicates the destination object was not present. Enabling the option “initialized immediately” will ensure the destination object is created and the table is synced. Replication stored procedures are created during snapshot generation.
12. Inserting Tracer Tokens and the Distribution Cleanup Job (Tip 12)
If you try to insert a tracer token but receive the error “No active subscriptions,” and manual subscription creation fails, it might be necessary to temporarily disable the “distribution clean up job”. After disabling the job manually, proceed with creating the replication, and then re-enable the job.
13. Disabling All Replication Snapshot Jobs (Tip 13)
To disable all replication snapshot jobs, use the following code in the msdb database:
use msdb
go
select * from sysjobs where category_id=15
update sysjobs set enabled=0 where category_id=15
14. Removing Invalid Publications (Tip 14)
Invalid publications can linger in the replication monitor. This information is stored in the distribution database. To locate them, run:
select * from MSsnapshot_agents where publication like '_RealCustom%'
Then, run a delete statement based on the results of the query.
15. Manually Removing Replication (Tip 15)
To manually remove replication from a database, use the system procedure sp_removedbreplication:
sp_removedbreplication '<Database name>'
(Reference to additional Microsoft documentation is available for a comprehensive guide to manual removal: http://support.microsoft.com/default.aspx?scid=kb;en-us;324401.)
16. Setting Up a Distributor (Tip 16)
After setting up the distributor, you should check from each publisher whether the distributor is visible by executing sp_helpdistributor. If this returns a null record, you need to execute sp_adddistributor:
USE [master];
exec sp_adddistributor @distributor = N'SQL2012XL610B03', @password = N'password'
V. AlwaysOn Replication Setup (Tip 17)
When setting up replication within an AlwaysOn Availability Group (AG), with a standalone distribution server, specific parameters are required for pull subscriptions. If you received an “Uninitialized subscription error,” note that setting up the subscriber often requires T-SQL rather than the GUI.
Crucially, you must ensure the following parameters are used correctly for the distribution agent:
- The Listener name must be used as the correct value for the Subscriber parameter.
- The
-Continuousparameter may not be added automatically and needs to be included.
Once these two parameters are corrected, and the distribution agent is started, the replication should function properly.