InnoDB should clean n_foreign_key_checks usage and assume that SQL is already holding
appropriate MDL. But an Insert into the child table doesn't seem to acquire MDL on the parent. When vice-versa is happening that there is MDL on the child when insert is happening
for the parent table.
The following test case could repeat the issue:
--source include/have_innodb.inc
|
create table t1(f1 int not null, primary key(f1))engine=innodb;
|
create table t2(f1 int not null primary key,
|
f2 int not null, foreign key(f2) references t1(f1))engine=innodb;
|
set DEBUG_SYNC='ib_after_row_insert_step SIGNAL alter WAIT_FOR dml';
|
--send insert into t1 values(1)
|
connect(con1,localhost,root,,,);
|
set DEBUG_SYNC="now WAIT_FOR alter";
|
alter table t2 force;
|
set DEBUG_SYNC="now SIGNAL dml";
|
connection default;
|
reap;
|
drop table t2, t1;
|
disconnect con1;
|
In above test case, ALTER TABLE t2 FORCE waits for MDL to open the table.
Another test case:
--source include/have_innodb.inc
|
create table t1(f1 int not null, primary key(f1))engine=innodb;
|
create table t2(f1 int not null primary key,
|
f2 int not null, foreign key(f2) references t1(f1))engine=innodb;
|
set DEBUG_SYNC='ib_after_row_insert_step SIGNAL alter WAIT_FOR dml';
|
--send insert into t2 values(1, 2)
|
connect(con1,localhost,root,,,);
|
set DEBUG_SYNC="now WAIT_FOR alter";
|
alter table t1 force;
|
set DEBUG_SYNC="now SIGNAL dml";
|
connection default;
|
reap;
|
drop table t2, t1;
|
disconnect con1;
|
In this case, alter waits at commit_inplace_alter_table(). That too for innodb table lock.
#6 0x00005555565df591 in lock_wait_suspend_thread (thr=0x7fffb8030768)
|
at /home/thiru/mariarepo/10.5/10.5-work/storage/innobase/lock/lock0wait.cc:351
|
#7 0x00005555566bb378 in row_mysql_handle_errors (new_err=0x7ffff01c3844,
|
trx=0x7ffff0cc3268, thr=0x7fffb8030768, savept=0x0)
|
at /home/thiru/mariarepo/10.5/10.5-work/storage/innobase/row/row0mysql.cc:740
|
#8 0x00005555565cd2f7 in lock_table_for_trx (table=0x7fffc41317e8,
|
trx=0x7ffff0cc3268, mode=LOCK_X)
|
at /home/thiru/mariarepo/10.5/10.5-work/storage/innobase/lock/lock0lock.cc:4070
|
#9 0x00005555566ad12d in row_merge_lock_table (trx=0x7ffff0cc3268,
|
table=0x7fffc41317e8, mode=LOCK_X)
|
at /home/thiru/mariarepo/10.5/10.5-work/storage/innobase/row/row0merge.cc:3698
|
#10 0x0000555556568d42 in ha_innobase::commit_inplace_alter_table (this=
|
0x7fffb8020c08, altered_table=0x7ffff01c4fb0,
|
ha_alter_info=0x7ffff01c4f20, commit=true)
|
at /home/thiru/mariarepo/10.5/10.5-work/storage/innobase/handler/handler0alter.cc:10699
|
#11 0x000055555611bf0a in handler::ha_commit_inplace_alter_table (
|
this=0x7fffb8020c08, altered_table=0x7ffff01c4fb0,
|
ha_alter_info=0x7ffff01c4f20, commit=true
|
First case is acquiring MDL on child table (un-necessary locking)
Second case should do MDL on parent table.
{"report":{"fcp":1098.5999999046326,"ttfb":310.89999997615814,"pageVisibility":"visible","entityId":80762,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":32,"apdex":0.5,"journeyId":"445cf9ca-9927-4483-9cfe-9f13bda8b3cf","navigationType":0,"readyForUser":1405.5999999046326,"redirectCount":0,"resourceLoadedEnd":1418.5999999046326,"resourceLoadedStart":318.09999990463257,"resourceTiming":[{"duration":92.60000002384186,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":318.09999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":318.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":410.6999999284744,"responseStart":0,"secureConnectionStart":0},{"duration":92.60000002384186,"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":318.39999997615814,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":318.39999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":411,"responseStart":0,"secureConnectionStart":0},{"duration":144.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":318.59999990463257,"connectEnd":318.59999990463257,"connectStart":318.59999990463257,"domainLookupEnd":318.59999990463257,"domainLookupStart":318.59999990463257,"fetchStart":318.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":318.59999990463257,"responseEnd":463.09999990463257,"responseStart":463.09999990463257,"secureConnectionStart":318.59999990463257},{"duration":345.5,"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":318.6999999284744,"connectEnd":318.6999999284744,"connectStart":318.6999999284744,"domainLookupEnd":318.6999999284744,"domainLookupStart":318.6999999284744,"fetchStart":318.6999999284744,"redirectEnd":0,"redirectStart":0,"requestStart":318.6999999284744,"responseEnd":664.1999999284744,"responseStart":664.0999999046326,"secureConnectionStart":318.6999999284744},{"duration":374.60000002384186,"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":318.89999997615814,"connectEnd":318.89999997615814,"connectStart":318.89999997615814,"domainLookupEnd":318.89999997615814,"domainLookupStart":318.89999997615814,"fetchStart":318.89999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":318.89999997615814,"responseEnd":693.5,"responseStart":693.5,"secureConnectionStart":318.89999997615814},{"duration":377.3000000715256,"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":319.09999990463257,"connectEnd":319.09999990463257,"connectStart":319.09999990463257,"domainLookupEnd":319.09999990463257,"domainLookupStart":319.09999990463257,"fetchStart":319.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":319.09999990463257,"responseEnd":696.3999999761581,"responseStart":696.3999999761581,"secureConnectionStart":319.09999990463257},{"duration":377.6999999284744,"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":319.39999997615814,"connectEnd":319.39999997615814,"connectStart":319.39999997615814,"domainLookupEnd":319.39999997615814,"domainLookupStart":319.39999997615814,"fetchStart":319.39999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":319.39999997615814,"responseEnd":697.0999999046326,"responseStart":697.0999999046326,"secureConnectionStart":319.39999997615814},{"duration":378.59999990463257,"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":319.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":319.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":698.0999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":378.5,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":319.59999990463257,"connectEnd":319.59999990463257,"connectStart":319.59999990463257,"domainLookupEnd":319.59999990463257,"domainLookupStart":319.59999990463257,"fetchStart":319.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":319.59999990463257,"responseEnd":698.0999999046326,"responseStart":698,"secureConnectionStart":319.59999990463257},{"duration":378.89999997615814,"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":319.7999999523163,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":319.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":698.6999999284744,"responseStart":0,"secureConnectionStart":0},{"duration":385.6999999284744,"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":319.89999997615814,"connectEnd":319.89999997615814,"connectStart":319.89999997615814,"domainLookupEnd":319.89999997615814,"domainLookupStart":319.89999997615814,"fetchStart":319.89999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":319.89999997615814,"responseEnd":705.5999999046326,"responseStart":705.5999999046326,"secureConnectionStart":319.89999997615814},{"duration":852.0999999046326,"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":328,"connectEnd":328,"connectStart":328,"domainLookupEnd":328,"domainLookupStart":328,"fetchStart":328,"redirectEnd":0,"redirectStart":0,"requestStart":328,"responseEnd":1180.0999999046326,"responseStart":1180.0999999046326,"secureConnectionStart":328},{"duration":852.0999999046326,"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":328.5,"connectEnd":328.5,"connectStart":328.5,"domainLookupEnd":328.5,"domainLookupStart":328.5,"fetchStart":328.5,"redirectEnd":0,"redirectStart":0,"requestStart":328.5,"responseEnd":1180.5999999046326,"responseStart":1180.5999999046326,"secureConnectionStart":328.5},{"duration":504.89999997615814,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":629.2999999523163,"connectEnd":629.2999999523163,"connectStart":629.2999999523163,"domainLookupEnd":629.2999999523163,"domainLookupStart":629.2999999523163,"fetchStart":629.2999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":629.2999999523163,"responseEnd":1134.1999999284744,"responseStart":1134.1999999284744,"secureConnectionStart":629.2999999523163},{"duration":289.2999999523163,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":1173.8999999761581,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1173.8999999761581,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1463.1999999284744,"responseStart":0,"secureConnectionStart":0},{"duration":202.79999995231628,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2cib/820016/12ta74/be4b45e9cec53099498fa61c8b7acba4/_/download/contextbatch/css/jira.project.sidebar,-_super,-project.issue.navigator,-jira.general,-jira.browse.project,-jira.view.issue,-jira.global,-atl.general,-com.atlassian.jira.projects.sidebar.init/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":1215.7999999523163,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1215.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1418.5999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":192.60000002384186,"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":1216.7999999523163,"connectEnd":1216.7999999523163,"connectStart":1216.7999999523163,"domainLookupEnd":1216.7999999523163,"domainLookupStart":1216.7999999523163,"fetchStart":1216.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":1216.7999999523163,"responseEnd":1409.3999999761581,"responseStart":1409.3999999761581,"secureConnectionStart":1216.7999999523163},{"duration":198.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/097ae97cb8fbec7d6ea4bbb1f26955b9-CDN/lu2cib/820016/12ta74/be4b45e9cec53099498fa61c8b7acba4/_/download/contextbatch/js/jira.project.sidebar,-_super,-project.issue.navigator,-jira.general,-jira.browse.project,-jira.view.issue,-jira.global,-atl.general,-com.atlassian.jira.projects.sidebar.init/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":1218.5999999046326,"connectEnd":1218.5999999046326,"connectStart":1218.5999999046326,"domainLookupEnd":1218.5999999046326,"domainLookupStart":1218.5999999046326,"fetchStart":1218.5999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":1218.5999999046326,"responseEnd":1417.5,"responseStart":1417.5,"secureConnectionStart":1218.5999999046326}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":100,"responseStart":311,"responseEnd":326,"domLoading":316,"domInteractive":1548,"domContentLoadedEventStart":1548,"domContentLoadedEventEnd":1660,"domComplete":2368,"loadEventStart":2368,"loadEventEnd":2368,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1496.5},{"name":"bigPipe.sidebar-id.end","time":1497.7999999523163},{"name":"bigPipe.activity-panel-pipe-id.start","time":1498},{"name":"bigPipe.activity-panel-pipe-id.end","time":1501},{"name":"activityTabFullyLoaded","time":1698.8999999761581}],"measures":[],"correlationId":"e14af4b5550d0e","effectiveType":"4g","downlink":9.1,"rtt":0,"serverDuration":139,"dbReadsTimeInMs":34,"dbConnsTimeInMs":45,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
In my prototype, I forgot to remove some more code, but strangely no tests were failing due to that:
diff --git a/storage/innobase/row/row0ins.cc b/storage/innobase/row/row0ins.cc
--- a/storage/innobase/row/row0ins.cc
+++ b/storage/innobase/row/row0ins.cc
@@ -1395,16 +1395,6 @@ row_ins_foreign_check_on_constraint(
err = row_update_cascade_for_mysql(thr, cascade,
foreign->foreign_table);
- /* Release the data dictionary latch for a while, so that we do not
- starve other threads from doing CREATE TABLE etc. if we have a huge
- cascaded operation running. */
-
- row_mysql_unfreeze_data_dictionary(thr_get_trx(thr));
-
- DEBUG_SYNC_C("innodb_dml_cascade_dict_unfreeze");
-
- row_mysql_freeze_data_dictionary(thr_get_trx(thr));
-
mtr_start(mtr);
If the tables are sufficiently protected by MDL, there is no need to hold the data dictionary latch to prevent concurrent DDL operations. At the start of this function, my fix already removed an assertion that we must be holding the latch.
There is no public test (not even in MySQL) that would exercise the DEBUG_SYNC point.