[MDEV-19661] Stray .ibd file left behind after killing "CREATE TABLE ... AS SELECT ..." Created: 2019-05-31  Updated: 2023-04-27

Status: Open
Project: MariaDB Server
Component/s: Data Definition - Create Table
Affects Version/s: 10.2.24, 10.3.15, 10.4.5
Fix Version/s: 10.4

Type: Bug Priority: Major
Reporter: Hartmut Holzgraefe Assignee: Oleksandr Byelkin
Resolution: Unresolved Votes: 0
Labels: None


 Description   

Under certain conditions, when killing a CREATE TABLE ... AS SELECT query, the InnoDB .ibd file stays behind even after the killed query has completely been rolled back, only the .frm file is removed, and the table still exists in the InnoDB data dictionary, so that DROP fails with 'Unknown table', but CREATE using the same name says 'Table already exists'

This only seems to happen when two preconditions are met:

  • at least one table join in the table
  • the joined tables are of a certain minimum size

How to reproduce:

create table t1 (id int primary key auto_increment);
insert into t1 select null;
insert into t1 select null from t1;
insert into t1 select null from t1;
insert into t1 select null from t1;
insert into t1 select null from t1;
insert into t1 select null from t1;
insert into t1 select null from t1;
insert into t1 select null from t1;
insert into t1 select null from t1;
insert into t1 select null from t1;
insert into t1 select null from t1;
create table t2 as select x.id as x_id, y.id as y_id from t1 x, t1 y;
CTRL-C 
CTRL-C

After killing the query with CTRL-C or KILL, the t2.frm file is gone,
but the t2.ibd file remains, and even after removing it manually
t2 can't be recreated as it still exists in the InnoDB data directory



 Comments   
Comment by Marko Mäkelä [ 2019-09-30 ]

Unfortunately, CREATE TABLE…SELECT is internally implemented as at least 2 transactions: CREATE TABLE and INSERT…SELECT. InnoDB in its current form cannot execute both DDL and DML in the same transaction; see also MDEV-18572.

Before we have MDEV-17567 Atomic DDL, we could work around this issue on the SQL layer as follows:

  1. Initially, create the table with a #sql- name. This will ensure that InnoDB will drop the table on the next startup, thanks to MDEV-14585.
  2. Copy the rows.
  3. Rename the table from #sql- to the final name.
Generated at Thu Feb 08 08:53:23 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.