[MDEV-25885] Rename table operation using copy algorithm fails to rename the foreign key constraint Created: 2021-06-09  Updated: 2023-04-27

Status: Stalled
Project: MariaDB Server
Component/s: Storage Engine - InnoDB
Affects Version/s: 10.2, 10.3, 10.4, 10.5, 10.6
Fix Version/s: 10.4, 10.5, 10.6

Type: Bug Priority: Major
Reporter: Thirunarayanan Balathandayuthapani Assignee: Thirunarayanan Balathandayuthapani
Resolution: Unresolved Votes: 0
Labels: upstream

Issue Links:
Relates
relates to MDEV-16417 Store Foreign Key metadata outside of... In Review
relates to MDEV-25642 Assertion `err != DB_DUPLICATE_KEY' f... Closed

 Description   

InnoDB fails to rename the foreign key constraint during rename table alter using
copy algorithm.

The following test case repeats the scenario:

--source include/have_innodb.inc
create table t1(f1 int not null, index(f1))engine=innodb;
 
create table t2(f1 int not null,
foreign key(f1) references t1(f1))engine=innodb;
 
alter table t1 rename to tm1, algorithm=copy;
 
show create table tm1;
Table	Create Table
tm1	CREATE TABLE `tm1` (
  `f1` int(11) NOT NULL,
  KEY `f1` (`f1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
 
show create table t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `f1` int(11) NOT NULL,
  KEY `f1` (`f1`),
  CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`f1`) REFERENCES `t1` (`f1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
 
select * from information_schema.innodb_sys_foreign;
ID	FOR_NAME	REF_NAME	N_COLS	TYPE
test/t2_ibfk_1	test/t2	test/t1	1	0
 
 
create table t1(f1 int not null)engine=innodb;
Error failed: 1215: Cannot add foreign key constraint

Analysis:
=========
Copy alter algorithm does the following steps:
1) Create a new table (#sql) and copies the row from old table

  • Doesn't add anything in SYS_FOREIGN

2) Rename t1 to #sql2

  • Rename the table name in InnoDB dictionary
  • Rename the file name too
  • Doesn't rename the constraint in SYS_FOREIGN tables

3) RENAME #sql to tm1

  • Rename the table name in InnoDB dictionary
  • Loads the foreign & referenced index from SYS_FOREIGN
    using the table name(tm1). But InnoDB doesn't have any FK constraint
    with the name (tm1)

4) DROP #sql2

  • Does check whether the SYS_FOREIGN has the foreign key constraint using the table name.

Generated at Thu Feb 08 09:41:08 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.