Skip to content

Fix audit retention queries that exceed the command timeout on a large SQL Server table - #6202

Open
ramonsmits wants to merge 3 commits into
masterfrom
audit-retention-delete-plan
Open

ramonsmits wants to merge 3 commits into
masterfrom
audit-retention-delete-plan

Conversation

@ramonsmits

@ramonsmits ramonsmits commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Audit retention on SQL Server fails on a large table. The retention queries exceed the 30-second command timeout, and the retention loop then waits for its next hourly tick. No row is removed and the database grows.

  • Expired hours: the DISTINCT query reads all expired rows. The new query seeks the next expired hour on the clustered key, one seek for each hour.
  • Delete, plan: the optimizer can read IX_AuditMessages_Id in Id order and filter on the hour. The last batch of an hour has fewer rows than the batch size, so the scan reads the complete index. WITH (FORCESEEK) forces the seek on the clustered key.
  • Delete, compile: a compile can wait for a synchronous update of the statistics of the table. OPTION (KEEPFIXED PLAN) removes that wait.
  • Retry: a failed sweep starts again after 10 seconds. The delay doubles for each next failure, up to the sweep interval, with jitter in the upper half of each delay.

The delete is raw SQL with the statement shape that EF Core generates, because ExecuteDeleteAsync cannot add the hints.

Measured on a 1.1 TB database with 115 million rows: without the query changes no retention pass completes. With them, more than 1,700 delete batches ran without an error.

Notes for the review

  • The retry also applies to PostgreSQL. A pass that leaves rows because the table is too busy is now tried again after seconds, not after one hour.
  • The tests cover the delay calculation. The retry loop itself has no test.
  • The wrong delete plan only shows on a large table, so no test covers the plan.

The DISTINCT query over the expired rows reads all of them.
On a large table it exceeds the command timeout, and the retention pass fails before it deletes a row.
The new query seeks the next expired hour on the clustered key.
The delete that EF Core generates failed on a large table in two ways:

- The compile waited for a synchronous update of the statistics of the table.
- The plan read IX_AuditMessages_Id in Id order with a filter on the hour. The last batch of an hour has fewer rows than the batch size, so the scan read the complete index.

Both exceeded the command timeout, and the retention loop then waited for its next hourly tick.
The statement is now raw SQL with WITH (FORCESEEK) and OPTION (KEEPFIXED PLAN), because EF Core cannot add these hints to ExecuteDeleteAsync.
A failed sweep waited for the next hourly tick, so one command timeout stopped the purge for up to one hour.
The loop now starts the sweep again after 10 seconds, and doubles the delay for each next failure up to the sweep interval.
Jitter spreads each delay over the upper half of its range.
@ramonsmits
ramonsmits added this pull request to stack #6204 November 27, 2026 08:25
@ramonsmits ramonsmits changed the title audit retention delete plan Fix audit retention queries that exceed the command timeout on a large SQL Server table Nov 27, 2026
@ramonsmits
ramonsmits force-pushed the audit-retention-delete-plan branch from 82a8ca7 to 8fa07f3 Compare November 27, 2026 08:26
@ramonsmits
ramonsmits marked this pull request as ready for review November 27, 2026 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant