Commit ef92aaf9 authored by Jan Lindström's avatar Jan Lindström

MDEV-10083: Orphan ibd file when playing with foreign keys

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.
parent a482e76e
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;
--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
......@@ -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,
......@@ -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;
}
......
......@@ -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
......
......@@ -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
......
......@@ -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));
}
/*********************************************************************//**
......
......@@ -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);
......@@ -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);
......@@ -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);
......@@ -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
......@@ -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;
......@@ -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;
......@@ -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);
}
......@@ -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) {
......
......@@ -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);
......
......@@ -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,
......@@ -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;
}
......
......@@ -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
......@@ -8858,6 +8859,7 @@ innobase_drop_database(
trx_free_for_mysql(trx);
return; /* ignore */
}
row_drop_database_for_mysql(namebuf, trx);
my_free(namebuf);
......
......@@ -470,7 +470,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
......
......@@ -2747,7 +2747,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));
}
/*********************************************************************//**
......
......@@ -2013,7 +2013,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);
......@@ -2143,7 +2143,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);
......@@ -2278,7 +2278,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);
......@@ -2319,7 +2319,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
......@@ -3216,7 +3216,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;
......@@ -3331,7 +3334,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;
......@@ -3718,7 +3725,7 @@ row_mysql_drop_temp_tables(void)
table = dict_table_get_low(table_name, DICT_ERR_IGNORE_ALL);
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);
}
......@@ -3848,7 +3855,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) {
......
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment