Skip to content

Commit e5c2285

Browse files
author
guilhem@mysql.com
committed
Fix for BUG#4551 "Temporary InnoDB tables not replicated properly with CREATE TABLE .. SELECT"
The problem was that (for any storage engine), the created temporary table was not removed if CREATE SELECT failed (because of a constraint violation for example). This was not consistent with the manual and with CREATE SELECT (no TEMPORARY).
1 parent 37bf41a commit e5c2285

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

mysql-test/r/create_select_tmp.result

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
drop table if exists t1, t2;
2+
CREATE TABLE t1 ( a int );
3+
INSERT INTO t1 VALUES (1),(2),(1);
4+
CREATE TABLE t2 ( PRIMARY KEY (a) ) TYPE=INNODB SELECT a FROM t1;
5+
Duplicate entry '1' for key 1
6+
select * from t2;
7+
Table 'test.t2' doesn't exist
8+
CREATE TEMPORARY TABLE t2 ( PRIMARY KEY (a) ) TYPE=INNODB SELECT a FROM t1;
9+
Duplicate entry '1' for key 1
10+
select * from t2;
11+
Table 'test.t2' doesn't exist
12+
CREATE TABLE t2 ( PRIMARY KEY (a) ) TYPE=MYISAM SELECT a FROM t1;
13+
Duplicate entry '1' for key 1
14+
select * from t2;
15+
Table 'test.t2' doesn't exist
16+
CREATE TEMPORARY TABLE t2 ( PRIMARY KEY (a) ) TYPE=MYISAM SELECT a FROM t1;
17+
Duplicate entry '1' for key 1
18+
select * from t2;
19+
Table 'test.t2' doesn't exist

mysql-test/t/create_select_tmp.test

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Testcase for BUG#4551
2+
# The bug was that when the table was TEMPORARY, it was not deleted if
3+
# the CREATE SELECT failed (the code intended too, but it actually
4+
# didn't). And as the CREATE TEMPORARY TABLE was not written to the
5+
# binlog if it was a transactional table, it resulted in an
6+
# inconsistency between binlog and the internal list of temp tables.
7+
8+
-- source include/have_innodb.inc
9+
drop table if exists t1, t2;
10+
CREATE TABLE t1 ( a int );
11+
INSERT INTO t1 VALUES (1),(2),(1);
12+
--error 1062;
13+
CREATE TABLE t2 ( PRIMARY KEY (a) ) TYPE=INNODB SELECT a FROM t1;
14+
--error 1146;
15+
select * from t2;
16+
--error 1062;
17+
CREATE TEMPORARY TABLE t2 ( PRIMARY KEY (a) ) TYPE=INNODB SELECT a FROM t1;
18+
--error 1146;
19+
select * from t2;
20+
--error 1062;
21+
CREATE TABLE t2 ( PRIMARY KEY (a) ) TYPE=MYISAM SELECT a FROM t1;
22+
--error 1146;
23+
select * from t2;
24+
--error 1062;
25+
CREATE TEMPORARY TABLE t2 ( PRIMARY KEY (a) ) TYPE=MYISAM SELECT a FROM t1;
26+
--error 1146;
27+
select * from t2;

sql/sql_insert.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -1545,9 +1545,13 @@ void select_create::abort()
15451545
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
15461546
enum db_type table_type=table->db_type;
15471547
if (!table->tmp_table)
1548+
{
15481549
hash_delete(&open_cache,(byte*) table);
1549-
if (!create_info->table_existed)
1550-
quick_rm_table(table_type,db,name);
1550+
if (!create_info->table_existed)
1551+
quick_rm_table(table_type, db, name);
1552+
}
1553+
else if (!create_info->table_existed)
1554+
close_temporary_table(thd, db, name);
15511555
table=0;
15521556
}
15531557
VOID(pthread_mutex_unlock(&LOCK_open));

0 commit comments

Comments
 (0)