Database Administration — Tue Apr 07

← Home | ← database-admin

MySQL - DBA Toolkit - Lock Waits

Tue Apr 07 — Surface active InnoDB lock waits and long-running transactions to identify what is blocking and how long it has been doing so.
#mysql #dba #monitoring #locks #innodb #tool-kit

Lock Waits: Finding What Is Blocking and Why

Lock waits are one of those problems that often look like something else. The application slows down. Queries start timing out. Connection counts climb. And somewhere in the middle of all of it, one transaction is sitting on a lock that everything else is queued behind.

InnoDB tracks this. The lock wait information lives in information_schema and tells you exactly which transaction is waiting, which transaction is blocking it, what they are both doing, and how long the blocker has been running.

The second query surfaces all long-running transactions — not just ones currently causing waits. A transaction open for several minutes that is not doing anything is still holding its locks.

LockWaits.sql
    Loading…
  

On MySQL 8.0+, innodb_lock_waits may be under performance_schema instead. Use: performance_schema.data_lock_waits and performance_schema.data_locks

Reading the Results

Lock Waits

ColumnWhat to Check
waiting_threadThe thread being blocked — this is the query timing out
waiting_queryWhat it is trying to do
blocking_threadThe thread holding the lock
blocking_queryWhat the blocker was doing when it took the lock
blocking_startedWhen the blocker’s transaction began
blocking_secondsHow long the blocking transaction has been open

Open Transactions

ColumnWhat to Check
open_secondsHow long this transaction has been open
rows_lockedHow many rows it currently holds locks on
rows_modifiedChanges made so far — indicates work that would be lost on rollback
trx_stateRUNNING, LOCK WAIT, or ROLLING BACK

What to Watch For

SignalWhat It Means
Lock wait chain (multiple waiters)One blocker is causing cascading waits across several threads
Blocker with trx_query = NULLTransaction is open but idle — application forgot to commit
High rows_locked with low rows_modifiedA SELECT ... FOR UPDATE holding many locks without doing work
Blocker running for many minutesLong-running transaction — consider killing it with KILL [thread_id]
Same application user always blockingApplication logic issue — transactions held open too long

Gareth Winterman