Skip to content

Commit e703185

Browse files
Use per-collector retention from config.collection_schedule in data retention (#237)
The data retention procedure now reads retention_days from config.collection_schedule for each table instead of using a hardcoded 30-day value. Direct name matching handles most tables; special mappings cover HealthParser, blocking_BlockedProcessReport, and deadlocks tables. The @retention_days parameter remains as a fallback for unmatched tables. The SQL Agent job step no longer passes @retention_days = 30, letting the procedure use per-collector settings by default. Tested on sql2022: query_snapshots used 10-day retention, running_jobs used 7-day retention, batch deletion confirmed working with @batch_size = 1000. Closes #237 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0f45722 commit e703185

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

install/43_data_retention.sql

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ GO
2020

2121
/*
2222
Data retention procedure for performance monitoring system
23-
Automatically purges old data from ALL collection tables based on configurable retention periods
24-
DYNAMIC VERSION - automatically discovers tables in collect schema with collection_time column
23+
Automatically purges old data from ALL collection tables
24+
Uses per-collector retention from config.collection_schedule when available,
25+
falls back to @retention_days parameter for unmatched tables
26+
DYNAMIC VERSION - automatically discovers tables in collect schema with time columns
2527
*/
2628

2729
IF OBJECT_ID(N'config.data_retention', N'P') IS NULL
@@ -33,7 +35,7 @@ GO
3335
ALTER PROCEDURE
3436
config.data_retention
3537
(
36-
@retention_days integer = 30, /*Number of days to retain collected data*/
38+
@retention_days integer = 30, /*Fallback retention for tables without a collection_schedule entry*/
3739
@batch_size integer = 10000, /*Number of rows to delete per batch to avoid blocking*/
3840
@debug bit = 0 /*Print debugging information*/
3941
)
@@ -194,6 +196,42 @@ BEGIN
194196
WHERE ttc.table_name = t.name
195197
);
196198

199+
/*
200+
Override retention_date per-collector from config.collection_schedule.
201+
Direct match: strip _collector/_analyzer suffix and match table name prefix.
202+
*/
203+
UPDATE ttc
204+
SET ttc.retention_date = DATEADD(DAY, -cs.retention_days, SYSDATETIME())
205+
FROM #tables_to_clean AS ttc
206+
JOIN config.collection_schedule AS cs
207+
ON ttc.table_name LIKE REPLACE(REPLACE(cs.collector_name, N'_collector', N''), N'_analyzer', N'') + N'%';
208+
209+
/*
210+
Special mappings for tables whose names don't match their collector:
211+
- HealthParser_* tables -> system_health_collector
212+
- blocking_BlockedProcessReport -> process_blocked_process_xml
213+
- deadlocks (sp_BlitzLock output) -> process_deadlock_xml
214+
*/
215+
UPDATE ttc
216+
SET ttc.retention_date = DATEADD(DAY, -cs.retention_days, SYSDATETIME())
217+
FROM #tables_to_clean AS ttc
218+
CROSS JOIN config.collection_schedule AS cs
219+
WHERE
220+
(
221+
ttc.table_name LIKE N'HealthParser%'
222+
AND cs.collector_name = N'system_health_collector'
223+
)
224+
OR
225+
(
226+
ttc.table_name = N'blocking_BlockedProcessReport'
227+
AND cs.collector_name = N'process_blocked_process_xml'
228+
)
229+
OR
230+
(
231+
ttc.table_name = N'deadlocks'
232+
AND cs.collector_name = N'process_deadlock_xml'
233+
);
234+
197235
/*
198236
Special handling for config.collection_log - keep 2x longer
199237
*/
@@ -399,12 +437,13 @@ GO
399437

400438
PRINT 'Data retention procedure created successfully (DYNAMIC VERSION)';
401439
PRINT 'Use config.data_retention to automatically purge old monitoring data';
402-
PRINT 'AUTOMATICALLY discovers ALL collect schema tables with time columns';
440+
PRINT 'Uses per-collector retention from config.collection_schedule when available';
441+
PRINT 'Falls back to @retention_days parameter for unmatched tables';
403442
PRINT '';
404443
PRINT 'Examples:';
405-
PRINT ' -- Keep 30 days (default)';
444+
PRINT ' -- Use per-collector retention (default)';
406445
PRINT ' EXECUTE config.data_retention @debug = 1;';
407446
PRINT '';
408-
PRINT ' -- Keep 90 days';
447+
PRINT ' -- Override fallback to 90 days for tables without schedule entries';
409448
PRINT ' EXECUTE config.data_retention @retention_days = 90;';
410449
GO

install/45_create_agent_jobs.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ EXECUTE msdb.dbo.sp_add_jobstep
164164
@step_name = N'Run Data Retention',
165165
@subsystem = N'TSQL',
166166
@database_name = N'PerformanceMonitor',
167-
@command = N'EXECUTE config.data_retention @retention_days = 30, @debug = 1;',
167+
@command = N'EXECUTE config.data_retention @debug = 1;',
168168
@retry_attempts = 0,
169169
@on_success_action = 1; /*Quit with success*/
170170

0 commit comments

Comments
 (0)