This is a surprisingly simple scenario leading to a (to me) surprising deadlock. Since I'm not an expert on InnoDB I have already tried other ways to find out whether it is a bug (see https://dba.stackexchange.com/questions/216450/is-this-surprising-deadlock-reasonable-or-a-mariadb-bug), but unfortunately didn't get any helpful responses, so I'm filing it now.
Here is the setup. The isolation level is left at its default (REPEATABLE_READ):
create database deadlock;
use deadlock;
create table foo(id int primary key) engine=InnoDB;
insert into foo values (10);
Now we can start. Let's open two connections A and B to the database.
-- Connection A
begin;
select * from foo for update;
-- Connection B
begin;
select * from foo for update;
At this point, the select issued by B is waiting. This is as expected because A is holding the relevant locks. Note that the StackExchange question used "delete from foo;" instead in both transactions - it makes no difference to the result, but a select seems easier to analyze.
-- Connection A
insert into foo values (9);
And at this point, mariadb kills the waiting query of B due to a deadlock. This is very surprising to me, because it would mean that B has already managed to acquire some locks.
As an additional point of information, if you use the id 11 instead of 9 in the last statement, there is no deadlock.
This test is a simplification of a production scenario where this deadlock kept occurring - there was a transaction that first deleted "invalid" records before inserting a new one. Whether a record was "invalid" was determined in a way that did not use an index, so the entire table had to be scanned. I did not understand how the deadlock could occur, since my mental model was that the delete would essentially get an exclusive lock on everything relevant in the table, so a second transaction running the same queries would have to wait before being able to acquire any locks, and thus could not possibly contribute to a deadlock.
{"report":{"fcp":939.5999999046326,"ttfb":156.89999985694885,"pageVisibility":"visible","entityId":70388,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"6a53a767-d07b-4958-ab9c-f5873c8f0ddd","navigationType":0,"readyForUser":997.1999998092651,"redirectCount":0,"resourceLoadedEnd":687.1999998092651,"resourceLoadedStart":162.69999980926514,"resourceTiming":[{"duration":314.60000014305115,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":162.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":162.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":477.2999999523163,"responseStart":0,"secureConnectionStart":0},{"duration":314.40000009536743,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":162.89999985694885,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":162.89999985694885,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":477.2999999523163,"responseStart":0,"secureConnectionStart":0},{"duration":323.2000000476837,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":163.09999990463257,"connectEnd":163.09999990463257,"connectStart":163.09999990463257,"domainLookupEnd":163.09999990463257,"domainLookupStart":163.09999990463257,"fetchStart":163.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":163.09999990463257,"responseEnd":486.2999999523163,"responseStart":486.2999999523163,"secureConnectionStart":163.09999990463257},{"duration":366.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/099b33461394b8015fc36c0a4b96e19f-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":163.09999990463257,"connectEnd":163.09999990463257,"connectStart":163.09999990463257,"domainLookupEnd":163.09999990463257,"domainLookupStart":163.09999990463257,"fetchStart":163.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":163.09999990463257,"responseEnd":529.5999999046326,"responseStart":529.5999999046326,"secureConnectionStart":163.09999990463257},{"duration":369.7999999523163,"initiatorType":"script","name":"https://jira.mariadb.org/s/94c15bff32baef80f4096a08aceae8bc-CDN/lu2bu7/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":163.29999995231628,"connectEnd":163.29999995231628,"connectStart":163.29999995231628,"domainLookupEnd":163.29999995231628,"domainLookupStart":163.29999995231628,"fetchStart":163.29999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":163.29999995231628,"responseEnd":533.0999999046326,"responseStart":533.0999999046326,"secureConnectionStart":163.29999995231628},{"duration":370.39999985694885,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":163.29999995231628,"connectEnd":163.29999995231628,"connectStart":163.29999995231628,"domainLookupEnd":163.29999995231628,"domainLookupStart":163.29999995231628,"fetchStart":163.29999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":163.29999995231628,"responseEnd":533.6999998092651,"responseStart":533.6999998092651,"secureConnectionStart":163.29999995231628},{"duration":370.69999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":163.5,"connectEnd":163.5,"connectStart":163.5,"domainLookupEnd":163.5,"domainLookupStart":163.5,"fetchStart":163.5,"redirectEnd":0,"redirectStart":0,"requestStart":163.5,"responseEnd":534.1999998092651,"responseStart":534.1999998092651,"secureConnectionStart":163.5},{"duration":371.09999990463257,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bu7/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":163.59999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":163.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":534.6999998092651,"responseStart":0,"secureConnectionStart":0},{"duration":371,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":163.69999980926514,"connectEnd":163.69999980926514,"connectStart":163.69999980926514,"domainLookupEnd":163.69999980926514,"domainLookupStart":163.69999980926514,"fetchStart":163.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":163.69999980926514,"responseEnd":534.6999998092651,"responseStart":534.6999998092651,"secureConnectionStart":163.69999980926514},{"duration":371.2999999523163,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bu7/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":163.79999995231628,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":163.79999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":535.0999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":371.40000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/3339d87fa2538a859872f2df449bf8d0-CDN/lu2bu7/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":163.89999985694885,"connectEnd":163.89999985694885,"connectStart":163.89999985694885,"domainLookupEnd":163.89999985694885,"domainLookupStart":163.89999985694885,"fetchStart":163.89999985694885,"redirectEnd":0,"redirectStart":0,"requestStart":163.89999985694885,"responseEnd":535.2999999523163,"responseStart":535.2999999523163,"secureConnectionStart":163.89999985694885},{"duration":474.60000014305115,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":164.69999980926514,"connectEnd":164.69999980926514,"connectStart":164.69999980926514,"domainLookupEnd":164.69999980926514,"domainLookupStart":164.69999980926514,"fetchStart":164.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":164.69999980926514,"responseEnd":639.2999999523163,"responseStart":639.2999999523163,"secureConnectionStart":164.69999980926514},{"duration":522.3999998569489,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":164.79999995231628,"connectEnd":164.79999995231628,"connectStart":164.79999995231628,"domainLookupEnd":164.79999995231628,"domainLookupStart":164.79999995231628,"fetchStart":164.79999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":164.79999995231628,"responseEnd":687.1999998092651,"responseStart":687.1999998092651,"secureConnectionStart":164.79999995231628},{"duration":326.90000009536743,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":711.5999999046326,"connectEnd":711.5999999046326,"connectStart":711.5999999046326,"domainLookupEnd":711.5999999046326,"domainLookupStart":711.5999999046326,"fetchStart":711.5999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":711.5999999046326,"responseEnd":1038.5,"responseStart":1038.5,"secureConnectionStart":711.5999999046326}],"fetchStart":1,"domainLookupStart":1,"domainLookupEnd":1,"connectStart":1,"connectEnd":1,"requestStart":4,"responseStart":157,"responseEnd":160,"domLoading":161,"domInteractive":1053,"domContentLoadedEventStart":1053,"domContentLoadedEventEnd":1092,"domComplete":2171,"loadEventStart":2171,"loadEventEnd":2172,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1030.8999998569489},{"name":"bigPipe.sidebar-id.end","time":1031.5},{"name":"bigPipe.activity-panel-pipe-id.start","time":1031.6999998092651},{"name":"bigPipe.activity-panel-pipe-id.end","time":1034.1999998092651},{"name":"activityTabFullyLoaded","time":1097.7999999523163}],"measures":[],"correlationId":"ed17c9dc6aab4a","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":96,"dbReadsTimeInMs":12,"dbConnsTimeInMs":20,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}