MySQL 5.7.4 changed the behaviour of REPLACE and INSERT…ON DUPLICATE KEY UPDATE in the InnoDB storage engine. Upon encountering a duplicate key, it would no longer directly fall back to UPDATE, but instead it would proceed to acquire an exclusive lock on every index record for the row on which the INSERT failed.
The extra locking was motivated by a public bug report: MySQL Bug#50413 insert on duplicate key update sometimes writes binlog position incorrectly (Oracle internal BUG#11758237). The fix was followed up by a couple of regression fixes.
For one MariaDB user, reverting these changes significantly reduced the deadlock rate of INSERT…ON DUPLICATE KEY UPDATE.
MDEV-17073 disabled the changes except when statement-based replication is being used. Even with statement-based replication, the locking could be replaced with additional logging: extending the replication event with information on which index records were locked when the duplicate was detected. (Note that the dict_table_t::indexes may be sorted differently on master and slave, especially if CREATE UNIQUE INDEX was executed with ALGORITHM=INPLACE.
The documentation at https://mariadb.com/kb/en/binary-log-formats/ states that even if one is using Statement based logging, it's assumed that the tables are the same on master and slave and both are using the same storage engine. In this case Statement logging should work.
In mixed mode, we prefer security over speed and in this case statements using ON DUPLICATE KEY UPDATE should use binary logging to ensure it works even if the slave is using different storage engines or different index definitions than the master.
{"report":{"fcp":908.2999997138977,"ttfb":293.19999980926514,"pageVisibility":"visible","entityId":70585,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"26beff9b-a106-47c7-af04-38e6f3e66d8e","navigationType":0,"readyForUser":979.3999996185303,"redirectCount":0,"resourceLoadedEnd":464.59999990463257,"resourceLoadedStart":298.3999996185303,"resourceTiming":[{"duration":8,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":298.3999996185303,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":298.3999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":306.3999996185303,"responseStart":0,"secureConnectionStart":0},{"duration":8.199999809265137,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/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&whisper-enabled=true","startTime":298.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":298.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":306.8999996185303,"responseStart":0,"secureConnectionStart":0},{"duration":159.2000002861023,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":298.7999997138977,"connectEnd":298.7999997138977,"connectStart":298.7999997138977,"domainLookupEnd":298.7999997138977,"domainLookupStart":298.7999997138977,"fetchStart":298.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":404.59999990463257,"responseEnd":458,"responseStart":433.7999997138977,"secureConnectionStart":298.7999997138977},{"duration":165.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/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&whisper-enabled=true","startTime":299,"connectEnd":299,"connectStart":299,"domainLookupEnd":299,"domainLookupStart":299,"fetchStart":299,"redirectEnd":0,"redirectStart":0,"requestStart":399.5,"responseEnd":464.59999990463257,"responseStart":413.2999997138977,"secureConnectionStart":299},{"duration":114.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":299.09999990463257,"connectEnd":299.09999990463257,"connectStart":299.09999990463257,"domainLookupEnd":299.09999990463257,"domainLookupStart":299.09999990463257,"fetchStart":299.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":401.19999980926514,"responseEnd":413.19999980926514,"responseStart":412.2999997138977,"secureConnectionStart":299.09999990463257},{"duration":140.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":299.2999997138977,"connectEnd":299.2999997138977,"connectStart":299.2999997138977,"domainLookupEnd":299.2999997138977,"domainLookupStart":299.2999997138977,"fetchStart":299.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":407.2999997138977,"responseEnd":439.3999996185303,"responseStart":438.7999997138977,"secureConnectionStart":299.2999997138977},{"duration":110.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":299.59999990463257,"connectEnd":299.59999990463257,"connectStart":299.59999990463257,"domainLookupEnd":299.59999990463257,"domainLookupStart":299.59999990463257,"fetchStart":299.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":399,"responseEnd":410.5,"responseStart":409.8999996185303,"secureConnectionStart":299.59999990463257},{"duration":14.800000190734863,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":299.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":299.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":314.5,"responseStart":0,"secureConnectionStart":0},{"duration":135.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":299.7999997138977,"connectEnd":299.7999997138977,"connectStart":299.7999997138977,"domainLookupEnd":299.7999997138977,"domainLookupStart":299.7999997138977,"fetchStart":299.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":404.59999990463257,"responseEnd":435.3999996185303,"responseStart":434.7999997138977,"secureConnectionStart":299.7999997138977},{"duration":15.799999713897705,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/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":300,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":300,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":315.7999997138977,"responseStart":0,"secureConnectionStart":0},{"duration":142.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/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":300.09999990463257,"connectEnd":300.09999990463257,"connectStart":300.09999990463257,"domainLookupEnd":300.09999990463257,"domainLookupStart":300.09999990463257,"fetchStart":300.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":407.8999996185303,"responseEnd":443,"responseStart":442.19999980926514,"secureConnectionStart":300.09999990463257},{"duration":130.69999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":310.59999990463257,"connectEnd":310.59999990463257,"connectStart":310.59999990463257,"domainLookupEnd":310.59999990463257,"domainLookupStart":310.59999990463257,"fetchStart":310.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":406.8999996185303,"responseEnd":441.2999997138977,"responseStart":440.69999980926514,"secureConnectionStart":310.59999990463257},{"duration":129.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":315.69999980926514,"connectEnd":315.69999980926514,"connectStart":315.69999980926514,"domainLookupEnd":315.69999980926514,"domainLookupStart":315.69999980926514,"fetchStart":315.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":407.5,"responseEnd":444.7999997138977,"responseStart":444.19999980926514,"secureConnectionStart":315.69999980926514},{"duration":216.19999980926514,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":670.5999999046326,"connectEnd":851.6999998092651,"connectStart":851.6999998092651,"domainLookupEnd":851.6999998092651,"domainLookupStart":851.6999998092651,"fetchStart":670.5999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":852,"responseEnd":886.7999997138977,"responseStart":885.7999997138977,"secureConnectionStart":851.6999998092651},{"duration":130.09999990463257,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":886.7999997138977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":886.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1016.8999996185303,"responseStart":0,"secureConnectionStart":0},{"duration":73.69999980926514,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":909.0999999046326,"connectEnd":909.0999999046326,"connectStart":909.0999999046326,"domainLookupEnd":909.0999999046326,"domainLookupStart":909.0999999046326,"fetchStart":909.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":945.5,"responseEnd":982.7999997138977,"responseStart":982.1999998092651,"secureConnectionStart":909.0999999046326},{"duration":72.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/e65b778d185daf5aee24936755b43da6/_/download/contextbatch/js/browser-metrics-plugin.contrib,-_super,-project.issue.navigator,-jira.view.issue,-atl.general/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":1019.1999998092651,"connectEnd":1019.1999998092651,"connectStart":1019.1999998092651,"domainLookupEnd":1019.1999998092651,"domainLookupStart":1019.1999998092651,"fetchStart":1019.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":1078,"responseEnd":1091.2999997138977,"responseStart":1089.3999996185303,"secureConnectionStart":1019.1999998092651}],"fetchStart":0,"domainLookupStart":31,"domainLookupEnd":91,"connectStart":91,"connectEnd":115,"secureConnectionStart":99,"requestStart":115,"responseStart":293,"responseEnd":316,"domLoading":297,"domInteractive":1050,"domContentLoadedEventStart":1050,"domContentLoadedEventEnd":1101,"domComplete":1149,"loadEventStart":1149,"loadEventEnd":1149,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1022.2999997138977},{"name":"bigPipe.sidebar-id.end","time":1023},{"name":"bigPipe.activity-panel-pipe-id.start","time":1023.1999998092651},{"name":"bigPipe.activity-panel-pipe-id.end","time":1025.3999996185303},{"name":"activityTabFullyLoaded","time":1125.1999998092651}],"measures":[],"correlationId":"d6373ec1b9fd40","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":120,"dbReadsTimeInMs":20,"dbConnsTimeInMs":30,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
I reverted MDEV-17073 and the upstream change and implemented and tested a simplified version of what appeared in MySQL 5.7.26.
Neither our test innodb.auto_increment_dup,log-bin nor the upstream test innodb.iodku would pass with those changes. (OK, I missed follow-up fixes to their test case.)
What happens in innodb.auto_increment_dup,log-bin is that the newly converted explicit lock for the record will be released when that record is deleted by the rollback moments later.
The idea of converting implicit locks to explicit on rollback is somewhat expensive. One motivation of MDEV-16232 is to reduce the amount of explicit locks by holding page latches across some handler API calls.
Ideally, this problem would be fixed by changing something in replication, perhaps simply by refusing statement-based replication for these operations.