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.
No workflow transitions have been executed yet.
{"report":{"fcp":725.0999999046326,"ttfb":147,"pageVisibility":"visible","entityId":70388,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"48645afe-d3d6-4baf-a5b2-8b3c5696b48b","navigationType":0,"readyForUser":789.9000000953674,"redirectCount":0,"resourceLoadedEnd":763.5999999046326,"resourceLoadedStart":152.29999995231628,"resourceTiming":[{"duration":35.299999952316284,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":152.29999995231628,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":152.29999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":187.59999990463257,"responseStart":0,"secureConnectionStart":0},{"duration":35.09999990463257,"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":152.70000004768372,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":152.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":187.79999995231628,"responseStart":0,"secureConnectionStart":0},{"duration":99,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":152.90000009536743,"connectEnd":152.90000009536743,"connectStart":152.90000009536743,"domainLookupEnd":152.90000009536743,"domainLookupStart":152.90000009536743,"fetchStart":152.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":152.90000009536743,"responseEnd":251.90000009536743,"responseStart":251.90000009536743,"secureConnectionStart":152.90000009536743},{"duration":145.20000004768372,"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":153,"connectEnd":153,"connectStart":153,"domainLookupEnd":153,"domainLookupStart":153,"fetchStart":153,"redirectEnd":0,"redirectStart":0,"requestStart":153,"responseEnd":298.2000000476837,"responseStart":298.2000000476837,"secureConnectionStart":153},{"duration":148.90000009536743,"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":153.09999990463257,"connectEnd":153.09999990463257,"connectStart":153.09999990463257,"domainLookupEnd":153.09999990463257,"domainLookupStart":153.09999990463257,"fetchStart":153.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":153.09999990463257,"responseEnd":302,"responseStart":302,"secureConnectionStart":153.09999990463257},{"duration":149.29999995231628,"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":153.20000004768372,"connectEnd":153.20000004768372,"connectStart":153.20000004768372,"domainLookupEnd":153.20000004768372,"domainLookupStart":153.20000004768372,"fetchStart":153.20000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":153.20000004768372,"responseEnd":302.5,"responseStart":302.5,"secureConnectionStart":153.20000004768372},{"duration":149.60000014305115,"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":153.29999995231628,"connectEnd":153.29999995231628,"connectStart":153.29999995231628,"domainLookupEnd":153.29999995231628,"domainLookupStart":153.29999995231628,"fetchStart":153.29999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":153.29999995231628,"responseEnd":302.90000009536743,"responseStart":302.90000009536743,"secureConnectionStart":153.29999995231628},{"duration":150,"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":153.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":153.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":303.40000009536743,"responseStart":0,"secureConnectionStart":0},{"duration":149.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":153.59999990463257,"connectEnd":153.59999990463257,"connectStart":153.59999990463257,"domainLookupEnd":153.59999990463257,"domainLookupStart":153.59999990463257,"fetchStart":153.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":153.59999990463257,"responseEnd":303.40000009536743,"responseStart":303.40000009536743,"secureConnectionStart":153.59999990463257},{"duration":150,"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":153.70000004768372,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":153.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":303.7000000476837,"responseStart":0,"secureConnectionStart":0},{"duration":150.09999990463257,"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":153.90000009536743,"connectEnd":153.90000009536743,"connectStart":153.90000009536743,"domainLookupEnd":153.90000009536743,"domainLookupStart":153.90000009536743,"fetchStart":153.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":153.90000009536743,"responseEnd":304,"responseStart":304,"secureConnectionStart":153.90000009536743},{"duration":352.7000000476837,"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":163.59999990463257,"connectEnd":163.59999990463257,"connectStart":163.59999990463257,"domainLookupEnd":163.59999990463257,"domainLookupStart":163.59999990463257,"fetchStart":163.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":163.59999990463257,"responseEnd":516.2999999523163,"responseStart":516.2999999523163,"secureConnectionStart":163.59999990463257},{"duration":599.8999998569489,"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":163.70000004768372,"connectEnd":163.70000004768372,"connectStart":163.70000004768372,"domainLookupEnd":163.70000004768372,"domainLookupStart":163.70000004768372,"fetchStart":163.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":163.70000004768372,"responseEnd":763.5999999046326,"responseStart":763.5999999046326,"secureConnectionStart":163.70000004768372},{"duration":160.59999990463257,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":470.90000009536743,"connectEnd":470.90000009536743,"connectStart":470.90000009536743,"domainLookupEnd":470.90000009536743,"domainLookupStart":470.90000009536743,"fetchStart":470.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":470.90000009536743,"responseEnd":631.5,"responseStart":631.5,"secureConnectionStart":470.90000009536743}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":28,"responseStart":147,"responseEnd":163,"domLoading":151,"domInteractive":842,"domContentLoadedEventStart":842,"domContentLoadedEventEnd":885,"domComplete":1220,"loadEventStart":1220,"loadEventEnd":1220,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":825.7000000476837},{"name":"bigPipe.sidebar-id.end","time":826.4000000953674},{"name":"bigPipe.activity-panel-pipe-id.start","time":826.5999999046326},{"name":"bigPipe.activity-panel-pipe-id.end","time":827},{"name":"activityTabFullyLoaded","time":892.0999999046326}],"measures":[],"correlationId":"ebcf947e2a7e28","effectiveType":"4g","downlink":9.1,"rtt":0,"serverDuration":71,"dbReadsTimeInMs":15,"dbConnsTimeInMs":21,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}