|
20 | 20 |
|
21 | 21 | /* |
22 | 22 | 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 |
25 | 27 | */ |
26 | 28 |
|
27 | 29 | IF OBJECT_ID(N'config.data_retention', N'P') IS NULL |
|
33 | 35 | ALTER PROCEDURE |
34 | 36 | config.data_retention |
35 | 37 | ( |
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*/ |
37 | 39 | @batch_size integer = 10000, /*Number of rows to delete per batch to avoid blocking*/ |
38 | 40 | @debug bit = 0 /*Print debugging information*/ |
39 | 41 | ) |
@@ -194,6 +196,42 @@ BEGIN |
194 | 196 | WHERE ttc.table_name = t.name |
195 | 197 | ); |
196 | 198 |
|
| 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 | + |
197 | 235 | /* |
198 | 236 | Special handling for config.collection_log - keep 2x longer |
199 | 237 | */ |
|
399 | 437 |
|
400 | 438 | PRINT 'Data retention procedure created successfully (DYNAMIC VERSION)'; |
401 | 439 | 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'; |
403 | 442 | PRINT ''; |
404 | 443 | PRINT 'Examples:'; |
405 | | -PRINT ' -- Keep 30 days (default)'; |
| 444 | +PRINT ' -- Use per-collector retention (default)'; |
406 | 445 | PRINT ' EXECUTE config.data_retention @debug = 1;'; |
407 | 446 | PRINT ''; |
408 | | -PRINT ' -- Keep 90 days'; |
| 447 | +PRINT ' -- Override fallback to 90 days for tables without schedule entries'; |
409 | 448 | PRINT ' EXECUTE config.data_retention @retention_days = 90;'; |
410 | 449 | GO |
0 commit comments