SQL ServerPostgreSQLMySQL

"Transaction was deadlocked... chosen as the deadlock victim" (Msg 1205)

Two transactions were each waiting on a lock the other one held — SQL Server broke the tie by killing one of them and rolling it back. The fix is retry logic, not a bug in either transaction.

  • SQL Server: Msg 1205, Level 13, State 56 followed by Transaction (Process ID 62) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
  • PostgreSQL: ERROR: deadlock detected DETAIL: Process 1234 waits for ShareLock on transaction 5678; blocked by process 5678.
  • MySQL (InnoDB): ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

The cause

A deadlock needs two transactions, each holding a lock the other one needs next — Transaction A holds a lock on Row 1 and wants Row 2; Transaction B holds a lock on Row 2 and wants Row 1. Neither can proceed, neither will release what it's holding, and left alone they'd wait on each other forever. SQL Server's lock monitor detects this cycle periodically and ends it by picking one transaction as the victim — rolling it back entirely and returning Msg 1205 to that session — so the other one can finish.

This is different from an ordinary lock wait: normal blocking resolves itself once the blocking transaction commits or rolls back. A deadlock can't resolve itself — that's specifically why SQL Server intervenes instead of just making both sessions wait.

How the victim is chosen

Not randomly, and not necessarily "whichever started the deadlock":

  1. DEADLOCK_PRIORITY first — if one session explicitly set a lower priority (SET DEADLOCK_PRIORITY LOW;), it's picked as the victim regardless of anything else.
  2. Rollback cost otherwise — among sessions at the same priority, SQL Server picks whichever has done less work, since rolling back a smaller transaction is cheaper than rolling back a larger one.

A session that expects to sometimes be the acceptable one to sacrifice — a background batch job, for instance, versus an interactive user request — can set DEADLOCK_PRIORITY LOW deliberately so it's consistently the one that gets retried, not whichever request a customer is waiting on.

The fix: retry logic, almost always

The victim transaction is rolled back cleanly — it didn't corrupt anything, it just didn't complete. The standard fix is to catch the 1205 and retry the whole transaction:

DECLARE @retries INT = 0;
WHILE @retries < 3
BEGIN
  BEGIN TRY
    BEGIN TRANSACTION;
      -- the transaction's actual work here
    COMMIT TRANSACTION;
    BREAK;
  END TRY
  BEGIN CATCH
    IF ERROR_NUMBER() = 1205
    BEGIN
      ROLLBACK TRANSACTION;
      SET @retries += 1;
    END
    ELSE
      THROW;  -- anything else: don't swallow it
  END CATCH
END

Retrying works because a deadlock depends on timing, not on anything inherently wrong with either transaction — the same two transactions run again rarely deadlock a second time, since the exact interleaving that produced the cycle the first time is unlikely to repeat immediately.

The actual fix, if it keeps happening to the same pair of transactions

If the same two operations deadlock repeatedly (not just occasionally under load), retry logic is masking a real pattern worth fixing directly — usually: both transactions acquiring the same two resources in a different order. Making every transaction in the codebase touch tables in the same consistent order (e.g., always customers before orders, never the reverse) removes the lock-ordering cycle that makes the deadlock possible in the first place, rather than just surviving it.

Can't demonstrate this in the playground

A deadlock is fundamentally about two concurrent sessions blocking each other — the SQLite sandbox here runs a single connection with no competing session to deadlock against. Reproduce the real thing against SQL Server with two sessions, each updating two rows in opposite order inside a transaction, timed to overlap — or read the deadlock graph from an incident that already happened via Extended Events' system_health session, which captures them automatically.

  • Deadlocks: reading the graph — how to read an actual deadlock graph once you have one, and the system_health session that captures them without you turning anything on.
  • Lock request time out period exceeded — ordinary blocking that doesn't resolve itself, the related-but-different problem this one is often confused with.
  • Transactions — BEGIN/COMMIT/ROLLBACK and isolation levels, the foundation this error sits on top of.