brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: SHOW ENGINE INNODB STATUS shows the last database deadlock and both queries in it

Database
~/tips/tip-database-innodb-status-latest-deadlock

mariadb -e 'SHOW ENGINE INNODB STATUS\G' | grep -A 40 'LATEST DETECTED DEADLOCK'

Finds which two transactions deadlocked and which rows they were both waiting for.

An app logs Deadlock found when trying to get lock and retries, but the error says nothing about the other side. InnoDB keeps a report of the most recent one.

mariadb -e 'SHOW ENGINE INNODB STATUS\G' | grep -A 40 'LATEST DETECTED DEADLOCK'

The section lists transaction (1) and transaction (2), the statement each one was running, the locks it held and the lock it waited for. At the end it says which one InnoDB rolled back. I’d make a deadlock on purpose with two sessions that update the same two rows in opposite order, and the report shows both UPDATE statements and the index they fight over.

The usual fix is boring: touch rows in the same order in every code path, and keep transactions short.

Two limits. The section only appears after a deadlock has happened since the server started, and it only holds the latest one, so a second deadlock overwrites the first. If they happen often, turn on logging of all of them:

SET GLOBAL innodb_print_all_deadlocks = ON;

Then each one goes to the error log. That setting is lost on restart unless you put it in the server config. The status output needs the PROCESS privilege, so an app user usually cannot run it.

Further reading: SHOW ENGINE INNODB STATUS, InnoDB system variables

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link