MySQL · 捉虫动态 · 删被引用索引导致crash

bug描述

设置 foreign_key_checks=0 删除被引用的索引后,再设置foreign_key_checks=1,对引用表进行DML操作会导致 mysqld crash,以下是重现的测例:

drop table if exists t2;
drop table if exists t1;

create table t1 (a int, b int, key idx1(a)) engine=innodb;
insert into t1 values(1,1);
insert into t1 values(2,2);
create table t2 (a int, b int, foreign key (b) references t1(a)) engine=innodb;
set session foreign_key_checks = 0;
alter table t1 drop key idx1;
set session foreign_key_checks = 1;
insert into t2 values (1,1); //此语句执行时mysqld会crash

## 分析

对于引用约束,在mysql实现中引用表和被引用表都会记录表的引用关系。

以下链表记录该表引用了哪些表,链表中每个元素为 dict_foreign_t

table->foreign_list      

以下链表记录该表被哪些表引用,链表中每个元素为 dict_foreign_t

table->referenced_list

dict_foreign_t 结构如下

dict_foreign_t
{
  foreign_table       // t2
  foreign_index       
  referenced_table    // t1
  referenced_index    // idx1
  ......
}

对于上面的测例,t1为被引用表,t2为引用表。

  • 对于t1
    table->foreign_list 为null
    table->referenced_list 则记录了引用关系

  • 对于t2
    table->foreign_list 记录了引用关系
    table->referenced_list 则为null

对于删除索引操作,如果索引涉及到引用关系,那么对应引用关系中的索引也应该做相应调整。对于测例中的删索引操作 alter table t1 drop key idx1; 应该做如下调整

  • 修改t1表的引用关系
    table->referenced_list中dict_foreign_t->referenced_index 置为null

  • 修改t2表的引用关系
    table->foreign_list中dict_foreign_t->referenced_index 置为null

而此bug修复之前, 并没有修改t2表的引用关系, 从而导致后面对t2表进行DML操作时,如果访问了无效的dict_foreign_t->referenced_index就会导致crash。

修复方法

删除被引用的索引后,应修改引用表的引用关系,即应修改table->referenced_list。
详见官方修复

附加说明

如果删除引用表对应的外键时,mysql如何处理的呢?同删除被引用表的索引一样,都需要调整引用表和被引用表的关系。

实际上,当删除引用表对应的外键时,如果存在和此外键相似(这里的相似是指索引列数和列顺序相同)的索引时,会用相似的索引代替删除的外键,从而保持原有的引用约束关系。

例如,在下面的例子中,原来t2存在有两个索引idx1和idx2,都可以作为外键,函数dict_foreign_find_index选择了idx1作为外键。当删除idx1后,同样通过dict_foreign_find_index选择了idx2做为了外键。

drop table if exists t2;
drop table if exists t1;

create table t1 (a int, b int, key(a)) engine=innodb;
create table t2 (a int, b int, foreign key (b) references t1(a),
                 key idx1(b), key idx2(b)) engine=innodb;
alter table t2 drop key idx1;

同样,此bug修复后,删除被引用表的索引时,如果存在相似的索引会用相似的索引代替。
例如,在下面的例子中,原来t1存在有两个索引idx1和idx2,都可以作为被引用索引,函数dict_foreign_find_index选择了idx1作为被引用索引。当删除idx1后,同样通过dict_foreign_find_index选择了idx2做为了新的被引用索引。

drop table if exists  t2;
drop table if exists  t1;
create table t1 (a int, b int, key idx1(a), key
                 idx2(a)) engine=innodb;
create table t2 (a int, b int,
                 foreign key (b) references t1(a)) engine=innodb;

其实,相似索引的存在是完全没有必要的。如果禁止创建相似的索引,那么引用约束这块的处理也不会这么复杂了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值