Skip to content

BUG: sp_StatUpdate_Diag PK violation on duplicate RunLabels in CommandLog #216

@nanoDBA

Description

@nanoDBA

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

  1. Add ROW_NUMBER() dedup to the START/END join query (keep latest END per RunLabel)
  2. Change PK to IDENTITY and add a unique index on RunLabel that ignores duplicates (IGNORE_DUP_KEY)
  3. Use GROUP BY RunLabel with MAX(StartTime) to collapse duplicates

Discovered

2026-03-06 during testing of obfuscation round-trip feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions