Fix trace-file accumulation and Data Retention job failure (#972)#973
Merged
Conversation
xp_delete_file cannot delete SQL Trace (.trc) files - it only accepts backup files and Maintenance Plan reports and validates the header - so the #951 trace cleanup in config.data_retention never worked, and its malformed wildcard path raised an uncatchable Msg 22049 that failed the Data Retention Agent job on every run. - Remove the broken xp_delete_file block from config.data_retention. - collect.trace_management_collector now creates the trace with a rollover file-count cap (@filecount, via the new @max_files param), so SQL Server prunes old .trc files itself. START also replaces an unbounded trace left by an older version, so the fix self-heals without waiting for a SQL Server restart. - scheduled_master_collector calls START instead of RESTART, so it no longer tears the trace down and orphans its files every cycle. - Add tools/Remove-OrphanedTraceFiles.ps1 to sweep trace files left on disk by versions <= 2.11.0; document it in the README troubleshooting section. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The trace-file cleanup added in v2.11.0 (#951) failed the
PerformanceMonitor - Data RetentionAgent job on every run withMsg 22049once anyMonitor_LongQueries_*.trcfiles existed.Root causes found while investigating #972:
xp_delete_filecannot delete.trcfiles at all — it only accepts SQL Server backup files and Maintenance Plan report files, and validates the file header. The [BUG] Trace files never get cleaned up #951 cleanup could never have worked.Msg 22049(extended-proc errors bypassTRY...CATCH) that failed the whole Agent job step.scheduled_master_collectorissuedRESTARTevery cycle (tearing down the trace and spawning a fresh timestamped one), and the trace was created with no rollover file-count cap.Changes
config.data_retention— removed the brokenxp_delete_fileblock (the crash).collect.trace_management_collector— new@max_filesparameter (default 5) →sp_trace_create @filecount, so SQL Server prunes old.trcfiles itself as the trace rolls.STARTnow also replaces an unbounded trace left by an older version, so the fix self-heals without waiting for a SQL Server restart.scheduled_master_collector— callsSTARTinstead ofRESTART; keeps one bounded trace running instead of orphaning files every cycle.tools/Remove-OrphanedTraceFiles.ps1— new one-time cleanup for.trcfiles left on disk by versions ≤ 2.11.0; referenced from the README troubleshooting section.No version bump (release-time step).
CHANGELOG.mdupdated under[Unreleased].Test plan
Tested live on SQL Server 2016 and 2019:
@max_files = 1→ validation error;START→ trace created withmax_files = 5and rollover on;STARTagainst an unbounded trace → replaced with a bounded one;STARTagainst a bounded trace → idempotent no-op;RESTART/STATUS/STOPall work.config.data_retentionrunsSUCCESS — Cleaned 51 tables, noMsg 22049.Remove-OrphanedTraceFiles.ps1:-WhatIfpreview + real run deleted 181 (2019) / 280 (2016) orphaned files, correctly skipping the running trace's file and locked files.@filecountrollover-delete behavior verified against MS Learnsp_trace_createdocs.trace_management_collectoruse named parameters, so the new parameter is safe.🤖 Generated with Claude Code