[MDEV-9469] 'Incorrect key file' on ALTER TABLE Created: 2016-01-26  Updated: 2017-10-10  Resolved: 2016-02-15

Status: Closed
Project: MariaDB Server
Component/s: Data Definition - Alter Table, Storage Engine - InnoDB, Storage Engine - XtraDB
Affects Version/s: 10.0.18, 10.0.23, 10.1.10, 10.0, 10.1
Fix Version/s: 10.0.24

Type: Bug Priority: Major
Reporter: Thomas Mischke Assignee: Jan Lindström (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Environment:

Linux, Windows


Issue Links:
Duplicate
is duplicated by MDEV-9548 Alter table (renaming and adding inde... Closed
Problem/Incident
causes MDEV-13668 InnoDB unnecessarily rebuilds table w... Closed
Relates
relates to MDEV-13640 ALTER TABLE CHANGE and ADD INDEX on a... Closed
relates to MDEV-13838 Wrong result after altering a partiti... Closed
Sprint: 10.0.24

 Description   

Somewhere between MariaDB 10.0.12 and MariaDB 10.0.21 the behavior of ALTER TABLE changed. We have one special ALTER TABLE statement that worked until version 10.0.12 (and maybe later) but stopped to work at version 10.0.21 (maybe earlier). We tried version 10.1.10 as well, which still produces the error.

How to reproduce:

First create a table like this:

CREATE TABLE `w_findispmon05u` (
`atpkey` INT(11) NOT NULL DEFAULT '0',
`atzo05` INT(11) NULL DEFAULT NULL,
`pos` BIGINT(21) NULL DEFAULT NULL,
`f5BnvB` INT(9) NULL DEFAULT NULL,
`f5atbvb` INT(11) NULL DEFAULT NULL,
`f5atbwmg` INT(11) NULL DEFAULT NULL,
`f5pBneu` BIGINT(12) NULL DEFAULT NULL,
`atbwdt` INT(11) NULL DEFAULT NULL,
`atbwzt` INT(11) NULL DEFAULT NULL,
`atbart` VARCHAR(10) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

Next do an ALTER table like this:

ALTER TABLE `w_findispmon05u`
CHANGE COLUMN `atpkey` `f5atpkey` INT(11) NOT NULL DEFAULT '0' FIRST,
CHANGE COLUMN `atzo05` `f5atzo05` INT(11) NULL DEFAULT NULL AFTER `f5atpkey`,
CHANGE COLUMN `atbwdt` `f5atbwdt` INT(11) NULL DEFAULT NULL AFTER `f5pBneu`,
CHANGE COLUMN `atbwzt` `f5atbwzt` INT(11) NULL DEFAULT NULL AFTER `f5atbwdt`,
CHANGE COLUMN `atbart` `f5atbart` VARCHAR(10) NULL DEFAULT NULL AFTER `f5atbwzt`,
ADD INDEX `atpkey` (`f5atpkey`),
ADD INDEX `inatkey` (`f5atzo05`, `pos`),
ADD INDEX `pos` (`pos`, `f5atzo05`);

The message is 'Incorrect key file for table 'w_findispmon05u'; try to repair it'.

If the ALTER table is split into two statements as follows, the error disappears.

ALTER TABLE `w_findispmon05u`
CHANGE COLUMN `atpkey` `f5atpkey` INT(11) NOT NULL DEFAULT '0' FIRST,
CHANGE COLUMN `atzo05` `f5atzo05` INT(11) NULL DEFAULT NULL AFTER `f5atpkey`,
CHANGE COLUMN `atbwdt` `f5atbwdt` INT(11) NULL DEFAULT NULL AFTER `f5pBneu`,
CHANGE COLUMN `atbwzt` `f5atbwzt` INT(11) NULL DEFAULT NULL AFTER `f5atbwdt`,
CHANGE COLUMN `atbart` `f5atbart` VARCHAR(10) NULL DEFAULT NULL AFTER `f5atbwzt`;

ALTER TABLE `w_findispmon05u`
ADD INDEX `atpkey` (`f5atpkey`),
ADD INDEX `inatkey` (`f5atzo05`, `pos`),
ADD INDEX `pos` (`pos`, `f5atzo05`);

We see this behavior with different operating systems (Windows 7 64Bit, various Linux variants), so we think that this does not matter.

To test it with the version 10.1.10, we just used the MSI installer for Windows 64 Bit with all default settings. The error can be reproduced this way.



 Comments   
Comment by Elena Stepanova [ 2016-01-26 ]

Thanks for the report and the test case.

The problem appeared in 10.0 tree (10.0.18 release) with the following revision:

commit b53bcd438f171fcbc3a4ad5051434e1b87c844fe
Author: Jan Lindström <jan.lindstrom@mariadb.com>
Date:   Mon Mar 30 18:53:10 2015 +0300
 
    MDEV-7367: Updating a virtual column corrupts table which crashes server
    
    Analysis: MySQL table definition contains also virtual columns. Similarly,
    index fielnr references MySQL table fields. However, InnoDB table definition
    does not contain virtual columns. Therefore, when matching MySQL key fieldnr
    we need to use actual column name to find out referenced InnoDB dictionary
    column name.
    
    Fix: Add new function to match MySQL index key columns to InnoDB dictionary.

Comment by Jeremy Cole [ 2016-02-12 ]

Note that MDEV-9548, marked as a duplicate, has a much simpler test case.

Comment by Jan Lindström (Inactive) [ 2016-02-15 ]

commit c0b6c27dbede85952b3e6cd04b6393267e1aa656
Author: Jan Lindström <jan.lindstrom@mariadb.com>
Date: Mon Feb 15 14:43:42 2016 +0200

MDEV-9548: Alter table (renaming and adding index) fails with "Incorrect key file for table"
MDEV-9469: 'Incorrect key file' on ALTER TABLE

InnoDB needs to rebuild table if column name is changed and
added index (or foreign key) is created based on this new
name in same alter table.

Generated at Thu Feb 08 07:34:54 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.