外键约束异常现象

如下测例中,没有违反引用约束的插入失败。

  1. create database `a-b`;
  2. use `a-b`;
  3. SET FOREIGN_KEY_CHECKS=0;
  4. create table t1(c1 int primary key, c2 int) engine=innodb;
  5. create table t2(c1 int primary key, c2 int) engine=innodb;
  6. alter table t2 add foreign key(c2) references `a-b`.t1(c1);
  7. SET FOREIGN_KEY_CHECKS=1;
  8. insert into t1 values(1,1);
  9. select * from t1;
  10. c1 c2
  11. 1 1
  12. select * from t2;
  13. c1 c2
  14. insert into t2 values(1,1);
  15. ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`a-b`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`c2`) REFERENCES `a-b`.`t1` (`c1`))
  16. insert into t2 values(1,1); //预期应该成功实际失败了。子表插入任何数据都会报违反引用约束。

异常分析

首先我们会检查表结构是否正常

  1. show create table t2;
  2. Table Create Table
  3. t2 CREATE TABLE `t2` (
  4. `c1` int(11) NOT NULL,
  5. `c2` int(11) DEFAULT NULL,
  6. PRIMARY KEY (`c1`),
  7. KEY `c2` (`c2`),
  8. CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`c2`) REFERENCES `a-b`.`t1` (`c1`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=latin1

查看 innodb_sys_foreign 表

  1. select * from information_schema.innodb_sys_foreign where id='a@002db/t2_ibfk_1';
  2. +-------------------+------------+----------+--------+------+
  3. | ID | FOR_NAME | REF_NAME | N_COLS | TYPE |
  4. +-------------------+------------+----------+--------+------+
  5. | a@002db/t2_ibfk_1 | a@002db/t2 | a-b/t1 | 1 | 0 |
  6. +-------------------+------------+----------+--------+------+
  7. select * from information_schema.innodb_sys_tables where name='a@002db/t1';
  8. +----------+------------+------+--------+-------+-------------+------------+---------------+
  9. | TABLE_ID | NAME | FLAG | N_COLS | SPACE | FILE_FORMAT | ROW_FORMAT | ZIP_PAGE_SIZE |
  10. +----------+------------+------+--------+-------+-------------+------------+---------------+
  11. | 530 | a@002db/t1 | 1 | 5 | 525 | Antelope | Compact | 0 |
  12. +----------+------------+------+--------+-------+-------------+------------+---------------+

表结构正常,表面上看外键在系统表中元数据库信息正常。仔细比较发现 innodb_sys_foreign 的REF_NAME字段”a-b/t1”实际应为”a@002db/t2”。

MySQL内部表名和库名存储格式

MySQL 内部用 my_charset_filename 字符集来表名和库名。

以下数组定义了 my_charset_filename 字符集需要转换的字符。数组下标为 ascii 值,1代表不需要转换。可以看到字母数字和下划线等不需要转换,同时字符’-‘是需要转换的, 转换函数参见my_wc_mb_filename

  1. static char filename_safe_char[128]=
  2. {
  3. 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ................ */
  4. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ................ */
  5. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* !"#$%&'()*+,-./ */
  6. 1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, /* 0123456789:;<=>? */
  7. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* @ABCDEFGHIJKLMNO */
  8. 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1, /* PQRSTUVWXYZ[\]^_ */
  9. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* `abcdefghijklmno */
  10. 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, /* pqrstuvwxyz{|}~. */
  11. };

异常分析

由上节可知,字符’-‘作为库名或表名是需要转换的。innodb_sys_foreign 中 FOR_NAME 值是转换过的,只有 REF_NAME 未转换,而系统表 innodb_sys_tables 中存储的表名是转换后的。dict_get_referenced_table 根据未转换的表名 a-b/t1 去系统表 SYS_TABLES 查找会查找不到记录。于是会导致

  1. foreign->referenced_table==NULL

因此对子表的任何插入都会返回错误 DB_NO_REFERENCED_ROW,如下代码

  1. row_ins_check_foreign_constraint:
  2. if (check_ref) {
  3. check_table = foreign->referenced_table;
  4. check_index = foreign->referenced_index;
  5. } else {
  6. check_table = foreign->foreign_table;
  7. check_index = foreign->foreign_index;
  8. }
  9. if (check_table == NULL
  10. || check_table->ibd_file_missing
  11. || check_index == NULL) {
  12. if (!srv_read_only_mode && check_ref) {
  13. ……
  14. err = DB_NO_REFERENCED_ROW;
  15. }
  16. goto exit_func;

经过进一步调试分析发现,函数innobase_get_foreign_key_info中主表的库名和表名都没有经过转换,而是直接使用系统字符集。

回过头再看看bug的触发条件:

  1. 表名或库名包含特殊字符;
  2. 此表作为引用约束的主表;
  3. 增加引用约束是设置了SET FOREIGN_KEY_CHECKS=0;

这里强调下第3条, 如果上面的测例中去掉了SET FOREIGN_KEY_CHECKS=0,那么结果 REF_NAME会正常转换

  1. SET FOREIGN_KEY_CHECKS=1;
  2. create table t1(c1 int primary key, c2 int) engine=innodb;
  3. create table t2(c1 int primary key, c2 int) engine=innodb;
  4. alter table t2 add foreign key(c2) references `a-b`.t1(c1);
  5. select * from information_schema.innodb_sys_foreign where id='a@002db/t2_ibfk_1';
  6. +-------------------+------------+------------+--------+------+
  7. | ID | FOR_NAME | REF_NAME | N_COLS | TYPE |
  8. +-------------------+------------+------------+--------+------+
  9. | a@002db/t2_ibfk_1 | a@002db/t2 | a@002db/t1 | 1 | 0 |
  10. +-------------------+------------+------------+--------+------+

online DDL 与 foreign key

MySQL 5.6 online DDL 是支持建索引的。而对于建外键索引同样也是支持的,条件是SET FOREIGN_KEY_CHECKS=0。

  1. ha_innobase::check_if_supported_inplace_alter
  2. if ((ha_alter_info->handler_flags
  3. & Alter_inplace_info::ADD_FOREIGN_KEY)
  4. && prebuilt->trx->check_foreigns) {
  5. ha_alter_info->unsupported_reason = innobase_get_err_msg(
  6. ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_FK_CHECK);
  7. DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
  8. }

SET FOREIGN_KEY_CHECKS=0时,prebuilt->trx->check_foreigns为false。

我们再来看出问题的函数innobase_get_foreign_key_info,只有online DDL的代码路径才会调用此函数:

  1. #0 innobase_get_foreign_key_info
  2. #1 ha_innobase::prepare_inplace_alter_table
  3. #2 handler::ha_prepare_inplace_alter_table
  4. #3 mysql_inplace_alter_table
  5. #4 mysql_alter_table
  6. ......

而非online DDL的路径如下,函数 dict_scan_id 会对表名和库名进行转换:

  1. #0 dict_scan_id
  2. #1 dict_scan_table_name
  3. #2 dict_create_foreign_constraints_low
  4. #3 dict_create_foreign_constraints
  5. #4 row_table_add_foreign_constraints
  6. #5 ha_innobase::create
  7. #6 handler::ha_create
  8. #7 ha_create_table
  9. #8 mysql_alter_table
  10. ......

修复

bug系统中虽然没有相关的bug信息,但从MySQL 5.6.26中我们看到官方Bug#21094069已经进行了修复,在innobase_get_foreign_key_info中对库名和表名进行转换。

参考commit:1fae0d42c352908fed03e29db2b391a0d2969269