Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions install/43_data_retention.sql
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,55 @@ BEGIN
RAISERROR(N'Starting data retention: keeping data newer than %s', 0, 1, @retention_date_string) WITH NOWAIT;
END;

/*
Purge processed XE staging rows early.
After parsers set is_processed = 1 the raw XML is never read again.
Keep a 1-day grace period for re-parsing failures.
*/
DECLARE
@staging_deleted bigint = 0;

IF OBJECT_ID(N'collect.deadlock_xml', N'U') IS NOT NULL
AND EXISTS
(
SELECT
1/0
FROM sys.columns AS c
WHERE c.object_id = OBJECT_ID(N'collect.deadlock_xml')
AND c.name = N'is_processed'
)
BEGIN
DELETE FROM collect.deadlock_xml
WHERE is_processed = 1
AND collection_time < DATEADD(DAY, -1, SYSDATETIME());

SET @staging_deleted += ROWCOUNT_BIG();
END;

IF OBJECT_ID(N'collect.blocked_process_xml', N'U') IS NOT NULL
AND EXISTS
(
SELECT
1/0
FROM sys.columns AS c
WHERE c.object_id = OBJECT_ID(N'collect.blocked_process_xml')
AND c.name = N'is_processed'
)
BEGIN
DELETE FROM collect.blocked_process_xml
WHERE is_processed = 1
AND collection_time < DATEADD(DAY, -1, SYSDATETIME());

SET @staging_deleted += ROWCOUNT_BIG();
END;

IF @debug = 1 AND @staging_deleted > 0
BEGIN
RAISERROR(N'Purged %I64d processed XE staging rows (older than 1 day)', 0, 1, @staging_deleted) WITH NOWAIT;
END;

SET @total_deleted += @staging_deleted;

/*
Create temp table to hold list of tables to clean
*/
Expand Down
Loading