Problem
sp_StatUpdate_Diag crashes with:
Violation of PRIMARY KEY constraint 'PK_runs'. Cannot insert duplicate key in object 'dbo.#runs'.
The duplicate key value is (servername_20260304_220104).
Root Cause
The #runs temp table uses RunLabel as its PK, but CommandLog can contain duplicate RunLabels when:
- A run is killed and restarted quickly (same second)
- Multiple runs start in the same second on the same server
- Orphan cleanup (
@CleanupOrphanedRuns) creates END markers that pair with different START entries
Repro
EXEC dbo.sp_StatUpdate_Diag @DaysBack = 30;
-- Fails if CommandLog has duplicate RunLabels
Fix Options
- Add
ROW_NUMBER() dedup to the START/END join query (keep latest END per RunLabel)
- Change PK to
IDENTITY and add a unique index on RunLabel that ignores duplicates (IGNORE_DUP_KEY)
- Use
GROUP BY RunLabel with MAX(StartTime) to collapse duplicates
Discovered
2026-03-06 during testing of obfuscation round-trip feature.
Problem
sp_StatUpdate_Diagcrashes with:Root Cause
The
#runstemp table usesRunLabelas its PK, but CommandLog can contain duplicate RunLabels when:@CleanupOrphanedRuns) creates END markers that pair with different START entriesRepro
Fix Options
ROW_NUMBER()dedup to the START/END join query (keep latest END per RunLabel)IDENTITYand add a unique index on RunLabel that ignores duplicates (IGNORE_DUP_KEY)GROUP BY RunLabelwithMAX(StartTime)to collapse duplicatesDiscovered
2026-03-06 during testing of obfuscation round-trip feature.