2023-10-08 mysql-代号m-增加外键导致crash-问题分析

摘要:

2023-10-08 mysql-代号m-增加外键导致crash-问题分析

相关bug: https://devops.aliyun.com/projex/project/36ed2d8a9a29e7f8407c6f5498/bug/5e13f3c4165c590665a1693edf

DDL:

sql 复制代码
create table t1(c1 int primary key);

create table t2(c2 int);
sql 复制代码
alter table t2 add foreign key(c2) references t1(c1);

错误日志:

sql 复制代码
    375: ha_create_table: mdb: mdb exec ok, query: alter table t2 add foreign key(c2) references t1(c1);
    344: ha_delete_table: mdb: ERROR: mdb monetdbe_query fail, dbname: db3 query: alter table t2 add foreign key(c2) references t1(c1); err: ParseException:SQLparser:42000!CONSTRAINT FOREIGN KEY: key 't2_c2_fkey' already exists

    344: bool Sql_cmd_alter_table::execute: mdb: ERROR: mdb monetdbe_query fail, dbname: db3 query: alter table t2 add foreign key(c2) references t1(c1); err: ParseException:SQLparser:42000!CONSTRAINT FOREIGN KEY: key 't2_c2_fkey' already exists

核心逻辑:

Mondetdb_Adaptor::execute_query

调用堆栈:

cpp 复制代码
#0  Mondetdb_Adaptor::execute_query (this=0x7f06527d59d0, dbname=0xbfe1580 "db3", sql=0x7f0267c21fe0 "alter table t5 add foreign key(c7) references t4(c6)", table_def=0x0)
    at /root/work/trunk/mysql-server-mysql-8.0.33/sql/monetdb_adaptor/monetdb_adaptor.cc:344
#1  0x0000000003aeddf5 in ha_delete_table (thd=0x7f0598000c90, table_type=0xbe38680, path=0x7f061c6f1ed0 "./db3/#sql2-132ae8-14", db=0x7f0267c23480 "db3", alias=0x7f061c6f2a00 "#sql2-132ae8-14", 
    table_def=0x7f0267b0b528, generate_warning=false) at /root/work/trunk/mysql-server-mysql-8.0.33/sql/handler.cc:2598
#2  0x0000000003781050 in quick_rm_table (thd=0x7f0598000c90, base=0xbe38680, db=0x7f0267c23480 "db3", table_name=0x7f061c6f2a00 "#sql2-132ae8-14", flags=11)
    at /root/work/trunk/mysql-server-mysql-8.0.33/sql/sql_table.cc:3852
#3  0x00000000037ac2d5 in mysql_alter_table (thd=0x7f0598000c90, new_db=0x7f0267c23480 "db3", new_name=0x0, create_info=0x7f061c6f5010, table_list=0x7f0267c22e30, alter_info=0x7f061c6f4ea0)
    at /root/work/trunk/mysql-server-mysql-8.0.33/sql/sql_table.cc:18095
#4  0x0000000003e48d5c in Sql_cmd_alter_table::execute (this=0x7f0267c23728, thd=0x7f0598000c90) at /root/work/trunk/mysql-server-mysql-8.0.33/sql/sql_alter.cc:350
#5  0x00000000036c1843 in mysql_execute_command (thd=0x7f0598000c90, first_level=true) at /root/work/trunk/mysql-server-mysql-8.0.33/sql/sql_parse.cc:4714
#6  0x00000000036c3aff in dispatch_sql_command (thd=0x7f0598000c90, parser_state=0x7f061c6f68b0) at /root/work/trunk/mysql-server-mysql-8.0.33/sql/sql_parse.cc:5363
#7  0x00000000036b9848 in dispatch_command (thd=0x7f0598000c90, com_data=0x7f061c6f79a0, command=COM_QUERY) at /root/work/trunk/mysql-server-mysql-8.0.33/sql/sql_parse.cc:2050
#8  0x00000000036b7790 in do_command (thd=0x7f0598000c90) at /root/work/trunk/mysql-server-mysql-8.0.33/sql/sql_parse.cc:1439
#9  0x000000000393e43e in handle_connection (arg=0xbf89ef0) at /root/work/trunk/mysql-server-mysql-8.0.33/sql/conn_handler/connection_handler_per_thread.cc:302
#10 0x000000000594c187 in pfs_spawn_thread (arg=0xbfffa70) at /root/work/trunk/mysql-server-mysql-8.0.33/storage/perfschema/pfs.cc:3042
#11 0x00007f066bc401ca in start_thread () from /lib64/libpthread.so.0
#12 0x00007f066a1f0e73 in clone () from /lib64/libc.so.6

出错原因:

  1. mysql在执行alter, 会先create一个新表,然后drop掉旧表
  2. 导致调用mdb两次, 但是语句却是同一个

解决方案:

  1. 在调用mdb前, 确保执行的sql时候的sql_command和当前逻辑保持一致
相关推荐
kejijianwen2 小时前
JdbcTemplate常用方法一览AG网页参数绑定与数据寻址实操
服务器·数据库·oracle
编程零零七3 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
高兴就好(石5 小时前
DB-GPT部署和试用
数据库·gpt
这孩子叫逆6 小时前
6. 什么是MySQL的事务?如何在Java中使用Connection接口管理事务?
数据库·mysql
Karoku0666 小时前
【网站架构部署与优化】web服务与http协议
linux·运维·服务器·数据库·http·架构
码农郁郁久居人下6 小时前
Redis的配置与优化
数据库·redis·缓存
MuseLss7 小时前
Mycat搭建分库分表
数据库·mycat
Hsu_kk8 小时前
Redis 主从复制配置教程
数据库·redis·缓存
DieSnowK8 小时前
[Redis][环境配置]详细讲解
数据库·redis·分布式·缓存·环境配置·新手向·详细讲解
程序猿小D8 小时前
第二百三十五节 JPA教程 - JPA Lob列示例
java·数据库·windows·oracle·jdk·jpa