Skip to content

Commit

Permalink
MDEV-10083: Orphan ibd file when playing with foreign keys
Browse files Browse the repository at this point in the history
Analysis: row_drop_table_for_mysql did not allow dropping
referenced table even in case when actual creating of the
referenced table was not successfull if foreign_key_checks=1.

Fix: Allow dropping referenced table even if foreign_key_checks=1
if actual table create returned error.
  • Loading branch information
Jan Lindström committed Jun 23, 2016
1 parent a482e76 commit ef92aaf
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 32 deletions.
87 changes: 87 additions & 0 deletions mysql-test/suite/innodb/r/innodb-fkcheck.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
set global innodb_file_per_table = 1;
drop table if exists b;
drop database if exists bug_fk;
create database bug_fk;
use bug_fk;
CREATE TABLE b (
b int unsigned NOT NULL,
d1 datetime NOT NULL,
PRIMARY KEY (b,d1)
) ENGINE=InnoDB;
CREATE TABLE c (
b int unsigned NOT NULL,
d1 datetime NOT NULL,
d2 datetime NOT NULL,
PRIMARY KEY (b,d1),
CONSTRAINT b_fk FOREIGN KEY (b) REFERENCES b (b)
) ENGINE=InnoDB;
show warnings;
Level Code Message
set foreign_key_checks = 0;
DROP TABLE IF EXISTS b;
show create table c;
Table Create Table
c CREATE TABLE `c` (
`b` int(10) unsigned NOT NULL,
`d1` datetime NOT NULL,
`d2` datetime NOT NULL,
PRIMARY KEY (`b`,`d1`),
CONSTRAINT `b_fk` FOREIGN KEY (`b`) REFERENCES `b` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE b (
b bigint unsigned NOT NULL,
d1 date NOT NULL,
PRIMARY KEY (b,d1)
) ENGINE=InnoDB;
ERROR HY000: Can't create table 'bug_fk.b' (errno: 150)
show warnings;
Level Code Message
Error 1005 Can't create table 'bug_fk.b' (errno: 150)
DROP TABLE IF EXISTS d;
Warnings:
Note 1051 Unknown table 'd'
CREATE TABLE d (
b bigint unsigned NOT NULL,
d1 date NOT NULL,
PRIMARY KEY (b,d1),
CONSTRAINT bd_fk FOREIGN KEY (b) REFERENCES b (b)
) ENGINE=InnoDB;
show warnings;
Level Code Message
set foreign_key_checks = 1;
show create table c;
Table Create Table
c CREATE TABLE `c` (
`b` int(10) unsigned NOT NULL,
`d1` datetime NOT NULL,
`d2` datetime NOT NULL,
PRIMARY KEY (`b`,`d1`),
CONSTRAINT `b_fk` FOREIGN KEY (`b`) REFERENCES `b` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
show create table d;
Table Create Table
d CREATE TABLE `d` (
`b` bigint(20) unsigned NOT NULL,
`d1` date NOT NULL,
PRIMARY KEY (`b`,`d1`),
CONSTRAINT `bd_fk` FOREIGN KEY (`b`) REFERENCES `b` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE b (
b bigint unsigned NOT NULL,
d1 date NOT NULL,
PRIMARY KEY (b,d1)
) ENGINE=InnoDB;
ERROR HY000: Can't create table 'bug_fk.b' (errno: 150)
show warnings;
Level Code Message
Error 1005 Can't create table 'bug_fk.b' (errno: 150)
set foreign_key_checks=0;
drop table c;
drop table d;
create table b(id int) engine=innodb;
show warnings;
Level Code Message
b.frm
b.ibd
drop table if exists b;
drop database if exists bug_fk;
115 changes: 115 additions & 0 deletions mysql-test/suite/innodb/t/innodb-fkcheck.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
--source include/have_innodb.inc

#
# MDEV-10083: Orphan ibd file when playing with foreign keys
#
--disable_query_log
SET @start_global_fpt = @@global.innodb_file_per_table;
SET @start_global_fkc = @@global.foreign_key_checks;
--enable_query_log

set global innodb_file_per_table = 1;

--disable_warnings
drop table if exists b;
drop database if exists bug_fk;
--enable_warnings

let $MYSQLD_DATADIR = `select @@datadir`;

create database bug_fk;
use bug_fk;

CREATE TABLE b (
b int unsigned NOT NULL,
d1 datetime NOT NULL,
PRIMARY KEY (b,d1)
) ENGINE=InnoDB;

CREATE TABLE c (
b int unsigned NOT NULL,
d1 datetime NOT NULL,
d2 datetime NOT NULL,
PRIMARY KEY (b,d1),
CONSTRAINT b_fk FOREIGN KEY (b) REFERENCES b (b)
) ENGINE=InnoDB;

show warnings;

set foreign_key_checks = 0;

DROP TABLE IF EXISTS b;

show create table c;

#
# Note that column b has different type in parent table
#
--error 1005
CREATE TABLE b (
b bigint unsigned NOT NULL,
d1 date NOT NULL,
PRIMARY KEY (b,d1)
) ENGINE=InnoDB;

show warnings;

DROP TABLE IF EXISTS d;

CREATE TABLE d (
b bigint unsigned NOT NULL,
d1 date NOT NULL,
PRIMARY KEY (b,d1),
CONSTRAINT bd_fk FOREIGN KEY (b) REFERENCES b (b)
) ENGINE=InnoDB;

show warnings;

set foreign_key_checks = 1;

show create table c;
show create table d;

#
# Table c column b used on foreign key has different type
# compared referenced column b in table b, but this
# create still produced b.ibd file. This is because
# we row_drop_table_for_mysql was called and referenced
# table is not allowed to be dropped even in case
# when actual create is not successfull.
#
--error 1005
CREATE TABLE b (
b bigint unsigned NOT NULL,
d1 date NOT NULL,
PRIMARY KEY (b,d1)
) ENGINE=InnoDB;

show warnings;

--list_files $MYSQLD_DATADIR/bug_fk b*

set foreign_key_checks=0;

drop table c;
drop table d;

--list_files $MYSQLD_DATADIR/bug_fk b*

create table b(id int) engine=innodb;
show warnings;

--list_files $MYSQLD_DATADIR/bug_fk b*

#
# Cleanup
#
--disable_query_log
SET @@global.innodb_file_per_table = @start_global_fpt;
SET @@global.foreign_key_checks = @start_global_fkc;
--enable_query_log

--disable_warnings
drop table if exists b;
drop database if exists bug_fk;
--enable_warnings
8 changes: 4 additions & 4 deletions storage/innobase/dict/dict0crea.c
Original file line number Diff line number Diff line change
Expand Up @@ -1242,14 +1242,14 @@ dict_create_or_check_foreign_constraint_tables(void)
fprintf(stderr,
"InnoDB: dropping incompletely created"
" SYS_FOREIGN table\n");
row_drop_table_for_mysql("SYS_FOREIGN", trx, TRUE);
row_drop_table_for_mysql("SYS_FOREIGN", trx, TRUE, TRUE);
}

if (table2) {
fprintf(stderr,
"InnoDB: dropping incompletely created"
" SYS_FOREIGN_COLS table\n");
row_drop_table_for_mysql("SYS_FOREIGN_COLS", trx, TRUE);
row_drop_table_for_mysql("SYS_FOREIGN_COLS", trx, TRUE, TRUE);
}

fprintf(stderr,
Expand Down Expand Up @@ -1298,8 +1298,8 @@ dict_create_or_check_foreign_constraint_tables(void)
"InnoDB: dropping incompletely created"
" SYS_FOREIGN tables\n");

row_drop_table_for_mysql("SYS_FOREIGN", trx, TRUE);
row_drop_table_for_mysql("SYS_FOREIGN_COLS", trx, TRUE);
row_drop_table_for_mysql("SYS_FOREIGN", trx, TRUE, TRUE);
row_drop_table_for_mysql("SYS_FOREIGN_COLS", trx, TRUE, TRUE);

error = DB_MUST_GET_MORE_FILE_SPACE;
}
Expand Down
3 changes: 2 additions & 1 deletion storage/innobase/handler/ha_innodb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7644,7 +7644,8 @@ ha_innobase::delete_table(

error = row_drop_table_for_mysql(norm_name, trx,
thd_sql_command(thd)
== SQLCOM_DROP_DB);
== SQLCOM_DROP_DB,
FALSE);

/* Flush the log to reduce probability that the .frm files and
the InnoDB data dictionary get out-of-sync if the user runs
Expand Down
5 changes: 4 additions & 1 deletion storage/innobase/include/row0mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,10 @@ row_drop_table_for_mysql(
/*=====================*/
const char* name, /*!< in: table name */
trx_t* trx, /*!< in: transaction handle */
ibool drop_db);/*!< in: TRUE=dropping whole database */
ibool drop_db,/*!< in: TRUE=dropping whole database */
ibool create_failed);/*!<in: TRUE=create table failed
because e.g. foreign key column
type mismatch. */
/*********************************************************************//**
Drop all temporary tables during crash recovery. */
UNIV_INTERN
Expand Down
2 changes: 1 addition & 1 deletion storage/innobase/row/row0merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -2685,7 +2685,7 @@ row_merge_drop_table(
/* There must be no open transactions on the table. */
ut_a(table->n_mysql_handles_opened == 0);

return(row_drop_table_for_mysql(table->name, trx, FALSE));
return(row_drop_table_for_mysql(table->name, trx, FALSE, FALSE));
}

/*********************************************************************//**
Expand Down
23 changes: 15 additions & 8 deletions storage/innobase/row/row0mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,7 @@ row_create_table_for_mysql(

if (dict_table_get_low(table->name, DICT_ERR_IGNORE_NONE)) {

row_drop_table_for_mysql(table->name, trx, FALSE);
row_drop_table_for_mysql(table->name, trx, FALSE, TRUE);
trx_commit_for_mysql(trx);
} else {
dict_mem_table_free(table);
Expand Down Expand Up @@ -2117,7 +2117,7 @@ row_create_index_for_mysql(

trx_general_rollback_for_mysql(trx, NULL);

row_drop_table_for_mysql(table_name, trx, FALSE);
row_drop_table_for_mysql(table_name, trx, FALSE, TRUE);

trx_commit_for_mysql(trx);

Expand Down Expand Up @@ -2187,7 +2187,7 @@ row_table_add_foreign_constraints(

trx_general_rollback_for_mysql(trx, NULL);

row_drop_table_for_mysql(name, trx, FALSE);
row_drop_table_for_mysql(name, trx, FALSE, TRUE);

trx_commit_for_mysql(trx);

Expand Down Expand Up @@ -2228,7 +2228,7 @@ row_drop_table_for_mysql_in_background(

/* Try to drop the table in InnoDB */

error = row_drop_table_for_mysql(name, trx, FALSE);
error = row_drop_table_for_mysql(name, trx, FALSE, FALSE);

/* Flush the log to reduce probability that the .frm files and
the InnoDB data dictionary get out-of-sync if the user runs
Expand Down Expand Up @@ -3078,7 +3078,10 @@ row_drop_table_for_mysql(
/*=====================*/
const char* name, /*!< in: table name */
trx_t* trx, /*!< in: transaction handle */
ibool drop_db)/*!< in: TRUE=dropping whole database */
ibool drop_db,/*!< in: TRUE=dropping whole database */
ibool create_failed) /*!<in: TRUE=create table failed
because e.g. foreign key column
type mismatch. */
{
dict_foreign_t* foreign;
dict_table_t* table;
Expand Down Expand Up @@ -3193,7 +3196,11 @@ row_drop_table_for_mysql(
foreign = UT_LIST_GET_NEXT(referenced_list, foreign);
}

if (foreign && trx->check_foreigns
/* We should allow dropping a referenced table if creating
that referenced table has failed for some reason. For example
if referenced table is created but it column types that are
referenced do not match. */
if (foreign && trx->check_foreigns && !create_failed
&& !(drop_db && dict_tables_have_same_db(
name, foreign->foreign_table_name_lookup))) {
FILE* ef = dict_foreign_err_file;
Expand Down Expand Up @@ -3578,7 +3585,7 @@ row_mysql_drop_temp_tables(void)
table = dict_load_table(table_name, TRUE, DICT_ERR_IGNORE_NONE);

if (table) {
row_drop_table_for_mysql(table_name, trx, FALSE);
row_drop_table_for_mysql(table_name, trx, FALSE, FALSE);
trx_commit_for_mysql(trx);
}

Expand Down Expand Up @@ -3708,7 +3715,7 @@ row_drop_database_for_mysql(
goto loop;
}

err = row_drop_table_for_mysql(table_name, trx, TRUE);
err = row_drop_table_for_mysql(table_name, trx, TRUE, FALSE);
trx_commit_for_mysql(trx);

if (err != DB_SUCCESS) {
Expand Down
2 changes: 1 addition & 1 deletion storage/innobase/trx/trx0roll.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ trx_rollback_active(
ut_print_name(stderr, trx, TRUE, table->name);
fputs(" in recovery\n", stderr);

err = row_drop_table_for_mysql(table->name, trx, TRUE);
err = row_drop_table_for_mysql(table->name, trx, TRUE, FALSE);
trx_commit_for_mysql(trx);

ut_a(err == (int) DB_SUCCESS);
Expand Down
8 changes: 4 additions & 4 deletions storage/xtradb/dict/dict0crea.c
Original file line number Diff line number Diff line change
Expand Up @@ -1444,14 +1444,14 @@ dict_create_or_check_foreign_constraint_tables(void)
fprintf(stderr,
"InnoDB: dropping incompletely created"
" SYS_FOREIGN table\n");
row_drop_table_for_mysql("SYS_FOREIGN", trx, TRUE);
row_drop_table_for_mysql("SYS_FOREIGN", trx, TRUE, TRUE);
}

if (table2) {
fprintf(stderr,
"InnoDB: dropping incompletely created"
" SYS_FOREIGN_COLS table\n");
row_drop_table_for_mysql("SYS_FOREIGN_COLS", trx, TRUE);
row_drop_table_for_mysql("SYS_FOREIGN_COLS", trx, TRUE, TRUE);
}

fprintf(stderr,
Expand Down Expand Up @@ -1500,8 +1500,8 @@ dict_create_or_check_foreign_constraint_tables(void)
"InnoDB: dropping incompletely created"
" SYS_FOREIGN tables\n");

row_drop_table_for_mysql("SYS_FOREIGN", trx, TRUE);
row_drop_table_for_mysql("SYS_FOREIGN_COLS", trx, TRUE);
row_drop_table_for_mysql("SYS_FOREIGN", trx, TRUE, TRUE);
row_drop_table_for_mysql("SYS_FOREIGN_COLS", trx, TRUE, TRUE);

error = DB_MUST_GET_MORE_FILE_SPACE;
}
Expand Down
4 changes: 3 additions & 1 deletion storage/xtradb/handler/ha_innodb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8773,7 +8773,8 @@ ha_innobase::delete_table(

error = row_drop_table_for_mysql(norm_name, trx,
thd_sql_command(thd)
== SQLCOM_DROP_DB);
== SQLCOM_DROP_DB,
FALSE);

/* Flush the log to reduce probability that the .frm files and
the InnoDB data dictionary get out-of-sync if the user runs
Expand Down Expand Up @@ -8858,6 +8859,7 @@ innobase_drop_database(
trx_free_for_mysql(trx);
return; /* ignore */
}

row_drop_database_for_mysql(namebuf, trx);
my_free(namebuf);

Expand Down
Loading

0 comments on commit ef92aaf

Please sign in to comment.