Additional SQL Server features and topics not covered by specific categories
The Distribution Cleanup job removes replicated transactions from the distribution database and also deactivates subscriptions that have not synchronized within the maximum distribution retention period. If this job is not making progress, the distribution database will continue to grow because rows in MSrepl_commands and MSrepl_transactions are not being removed.
From the provided information, the following points are relevant:
- The Distribution Cleanup job is expected to:
- Remove replicated transactions from the distribution database on a frequent schedule.
- Deactivate subscriptions that have not synchronized within the maximum distribution retention period (
@max_distretention).
- Subscription expiration and deactivation are controlled by:
- The maximum distribution retention period (
@max_distretentionon the distribution database). - The publication retention period (
@retentionon each publication).
- The maximum distribution retention period (
- Replication maintenance jobs and their roles:
- Distribution Clean Up: Distribution
- Removes replicated transactions from the distribution database.
- Expired Subscription Clean Up
- Detects and removes expired subscriptions from publication databases.
- On the Distributor, deactivates subscriptions that were not synchronized within the maximum distribution retention period.
- Distribution Clean Up: Distribution
- To assess whether reinitialization or more aggressive cleanup is appropriate, use replication monitoring tools:
- Use
sp_replmonitorsubscriptionpendingcmdsor the Undistributed Commands tab in Replication Monitor to determine:- How many commands are still pending in the distribution database for each subscription.
- The estimated time to deliver those commands.
- If the estimated time to deliver pending commands is greater than the time required to generate and apply a new snapshot, consider reinitializing the affected subscriptions. Reinitialization will allow the Distribution Agent to apply a fresh snapshot and then resume with a smaller backlog of commands.
- Use
- Recommended and safer approaches to reduce distribution database size and recover from excessive growth, based on the documented behavior:
- Verify and, if necessary, adjust retention settings:
- Ensure
@max_distretention(maximum distribution retention) is not set excessively high. - Ensure publication
@retentionvalues are appropriate for business requirements.
- Ensure
- Ensure the following jobs are enabled and completing successfully:
- Distribution Clean Up: Distribution (runs every 10 minutes by default).
- Expired Subscription Clean Up (runs daily at 1:00 A.M. by default).
- Use Replication Monitor and
sp_replmonitorsubscriptionpendingcmdsto:- Identify subscriptions with very large numbers of undistributed commands.
- Decide whether to reinitialize those subscriptions instead of allowing the Distribution Agent to process an extremely large backlog.
- For subscriptions that are no longer needed or are far behind and not required, allow them to expire or drop them so that the cleanup jobs can remove their associated metadata.
- Verify and, if necessary, adjust retention settings:
- Validating replication health before aggressive cleanup:
- Confirm that Distribution Agents are running and successfully applying commands to Subscribers.
- Confirm that there are no subscriptions that must retain historical commands for business or recovery reasons.
- Use Replication Monitor to verify that publications show a healthy status (green) and that undistributed commands are not increasing unexpectedly.
- Regarding manual invocation of cleanup procedures and aggressive cleanup:
- The documented and supported way to clean up the distribution database is to rely on the built-in maintenance jobs (Distribution Clean Up: Distribution and Expired Subscription Clean Up) with appropriate retention settings.
- Before making retention more aggressive (shorter periods), ensure that all required Subscribers are synchronizing within the new window; otherwise, subscriptions may be deactivated or expire and require reinitialization.
In summary, the core levers available are:
- Correct and possibly reduce retention settings (
@max_distretentionand publication@retention). - Ensure the Distribution Cleanup and Expired Subscription Cleanup jobs are running and completing.
- Use
sp_replmonitorsubscriptionpendingcmdsand Replication Monitor to identify large backlogs and decide where reinitialization is preferable to processing a huge volume of old commands. - Remove or allow expiration of obsolete or non-synchronizing subscriptions so that their metadata can be cleaned up.
References: