Skip to content

Commit b4fe866

Browse files
Fix deadlock count not resetting between collections (#803) (#820)
When no new deadlocks occurred for a database, the MERGE source was empty and no row was created, leaving stale delta values in the last row. Expanded the MERGE source with a combined_source CTE that carries forward databases from the previous collection with zero counts, so the delta calculation properly resets to 0. Fixes #803 Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a25c90b commit b4fe866

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

install/26_blocking_deadlock_analyzer.sql

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ BEGIN
178178
Aggregate deadlock data by database
179179
Update rows if database already exists from blocking aggregation
180180
Otherwise insert new rows
181+
Include databases from previous collection with zero counts so
182+
deltas reset to 0 when no new events occur (#803)
181183
*/
182184
WITH
183185
deadlock_aggregates AS
@@ -192,9 +194,41 @@ BEGIN
192194
AND bl.collection_time < @start_time
193195
GROUP BY
194196
bl.database_name
197+
),
198+
combined_source AS
199+
(
200+
SELECT
201+
da.database_name,
202+
da.deadlock_count,
203+
da.total_deadlock_wait_time_ms,
204+
da.victim_count
205+
FROM deadlock_aggregates AS da
206+
207+
UNION ALL
208+
209+
/*
210+
Carry forward databases from previous collection with zero
211+
counts so delta calculation can reset them to 0
212+
*/
213+
SELECT DISTINCT
214+
bds.database_name,
215+
0,
216+
0,
217+
0
218+
FROM collect.blocking_deadlock_stats AS bds
219+
WHERE bds.collection_time >= @last_deadlock_collection
220+
AND bds.collection_time < @start_time
221+
AND bds.database_name <> N'(none)'
222+
AND NOT EXISTS
223+
(
224+
SELECT
225+
1/0
226+
FROM deadlock_aggregates AS da
227+
WHERE da.database_name = bds.database_name
228+
)
195229
)
196230
MERGE collect.blocking_deadlock_stats WITH (SERIALIZABLE) AS target
197-
USING deadlock_aggregates AS source
231+
USING combined_source AS source
198232
ON target.database_name = source.database_name
199233
AND target.collection_time >= @start_time
200234
WHEN MATCHED

0 commit comments

Comments
 (0)