Skip to main content

How to See the Last Deadlock in MySQL

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A deadlock error tells you that you lost, and not to whom. MySQL keeps the full story of its latest deadlock: both transactions, what each held, what each wanted, and which one it chose to roll back. Here's how to get that report, and how to read a real one.

How to See the Locks a MySQL Transaction Holds

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

When a lock problem makes no sense, stop guessing and look at the locks. performance_schema.data_locks lists each one InnoDB holds. Run the same UPDATE three ways, by primary key, by unique index and by a plain index, and you get three different sets, which explains most of the surprises people have with row locks.

FOR SHARE vs FOR UPDATE in MySQL

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

FOR UPDATE locks a row so only you can touch it. FOR SHARE locks it so nobody can change it, while others can hold the same lock with you. They look like small variations, and picking the wrong one can deadlock two sessions that seem to be doing nothing wrong.