// dialect comparison

Finding deadlock victims and blocked sessions in every SQL dialect

When transactions block each other, the engine kills one victim and logs the evidence. Here is where that evidence lives in each engine.

Filed under Sessions & admin

The verdict

Every server engine breaks deadlocks itself; the difference is where the report lands. SQL Server's system_health and MySQL's LATEST DETECTED DEADLOCK are the fastest reads, PostgreSQL and Oracle write server logs and trace files, and SQLite has no deadlock to report.

Where they differ

The four server engines detect deadlocks and break them; the work is finding the report afterwards. Live lock-wait chains are queryable in all four (MySQL through performance_schema). SQLite never waits in a cycle: it returns SQLITE_BUSY instead.

How each engine writes it

SQL Server

SELECT blocking_session_id, wait_type, wait_time, wait_resource
FROM sys.dm_exec_requests
WHERE blocking_session_id <> 0;
-- deadlock graphs: system_health session, event xml_deadlock_report

Live blocking via sys.dm_exec_requests; deadlock graphs are in the always-on system_health XEvent session (or SQL Server Profiler's Deadlock graph event).

PostgreSQL

SELECT pid, pg_blocking_pids(pid) AS blocked_by, wait_event_type, wait_event, query
FROM pg_stat_activity
WHERE wait_event_type = 'Lock';

pg_blocking_pids() names the blockers. Deadlocks are checked after deadlock_timeout (1s by default) and written to the server log with both queries; log_lock_waits = on also logs any lock wait longer than deadlock_timeout.

MySQL

SHOW ENGINE INNODB STATUS;
-- section: LATEST DETECTED DEADLOCK

The InnoDB monitor prints only the latest deadlock (both transactions and the victim); innodb_print_all_deadlocks = 1 logs every one to the error log. Live waits are in sys.innodb_lock_waits and performance_schema.data_lock_waits (8.0+).

SQLite

-- no deadlock reports: one writer at a time, no row locks

A transaction that cannot get the lock it needs gets SQLITE_BUSY, immediately when waiting could deadlock (two readers both trying to upgrade). Start write transactions with BEGIN IMMEDIATE and use WAL mode to cut contention.

Oracle

SELECT sid, serial#, blocking_session, event, seconds_in_wait
FROM v$session
WHERE blocking_session IS NOT NULL;

ORA-00060 rolls back only the statement that hit the deadlock, not its transaction, and writes a trace file with both sessions' SQL; the application must still commit or roll back. Live blocking chains are in v$session.blocking_session.

Questions that come up

Who is the deadlock victim?

SQL Server picks the transaction cheapest to roll back unless SET DEADLOCK_PRIORITY says otherwise, InnoDB the one that changed fewer rows, and PostgreSQL the transaction whose deadlock check found the cycle. Oracle rolls back one statement in the session that detected it. Only SQL Server lets the application set priorities.

How do I prevent deadlocks?

Touch tables and rows in the same order in every code path, keep transactions short, index the columns your UPDATE and DELETE statements filter on so they lock fewer rows, and retry the victim with backoff. Under real concurrency some deadlocks will happen, so the retry is not optional.

Why does the report show my UPDATE twice?

Deadlock graphs show each transaction's held locks and current statement; both sides usually touched the same rows in different orders.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.