Part 2 of a Microsoft guide on detecting and removing orphaned Change Tracking records in Azure SQL Database, with safety checks and remediation steps.
Detecting and Removing Orphaned Change Tracking Records
This article continues the discussion from Part 1, focusing on safely cleaning orphaned records in Change Tracking (CT) side tables in Azure SQL Database.
Why Orphaned Records Occur
A common scenario leading to orphaned records:
- Side-table cleanup attempts to delete expired metadata
- Some deletions fail due to locks, timeouts, or errors
- Commit-table cleanup proceeds anyway
- Remaining side-table rows reference transaction IDs (xdes_id) that no longer exist in sys.syscommittab
This creates orphaned records that can cause unexpected CT growth and cleanup failures.
Script Overview
The provided T-SQL script performs detection and generates delete statements:
- Computes cleanup boundary using CT retention settings via
sp_changetracking_time_to_csn - Enumerates CT side tables using
sys.internal_tableswhereinternal_type = 209 - Identifies orphaned transaction IDs that are:
- Older than the computed boundary
- Missing from sys.syscommittab at the cleanup point
- Prints orphan counts per side table using
RAISERRORwithNOWAITfor streaming output - Includes safety cross-check that aborts if any "orphan" unexpectedly exists in sys.syscommittab
- Generates DELETE statements (disabled by default for safety)
Important Prerequisites
- Azure SQL Database limitations: Direct access to internal CT artifacts may be restricted
- CHECKPOINT requirement: Ensures in-memory commit table data is persisted to disk before reading
- Supported vs. custom remediation: Official Microsoft Learn guidance provides troubleshooting steps, while this script addresses a specific failure mode
Safety Features
The script includes critical safety mechanisms:
- Cross-check abort: Re-validates that identified orphans don't exist in sys.syscommittab
- Delete disabled by default: Requires explicit uncommenting after approval
- Streaming output: Uses
RAISERROR ... WITH NOWAITfor real-time progress monitoring - Defensive programming: Includes error handling and validation steps
How to Use
- Run Part 1 first to detect orphaned records and assess scope
- Confirm CT configuration using official guidance
- Interpret output: No RAISERROR lines means no orphans found; otherwise, see counts per side table
- Consider remediation options: Prefer supported mitigations (like disabling/enabling CT on tables) before using targeted cleanup
- If proceeding with cleanup: Test thoroughly, follow organizational approval processes, and enable delete statements only after validation
Key Takeaway
Orphaned CT side-table records can cause silent storage growth and performance issues. This two-part approach helps identify problems early (Part 1) and provides a safe, targeted cleanup workflow (Part 2) with explicit safety gates and disabled delete operations by default.
Comments
Please log in or register to join the discussion