Bug #124 Table corruption is not discovered by CHECK TABLE until FLUSH TABLES
Submitted: 5 Mar 2003 9:45 Modified: 5 Mar 2003 11:01
Reporter: Peter Zaitsev (Basic Quality Contributor) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:3.23,4.0 OS:Linux (Linux)
Assigned to: CPU Architecture:Any

[5 Mar 2003 9:45] Peter Zaitsev
Description:
Table corruption is not discovered by CHECK TABLE until FLUSH table is called.
Moreover SELECT from this table succeeds while fails after FLUSH, this is surprising as we do not expect MYISAM to cache rows:

How to repeat:
create table crash(i int,c char(50)) checksum=1;
insert into crash values(1,"fff");
insert into crash values(2,"fff");
insert into crash values(3,"fff");
insert into crash values(4,"fff");
insert into crash values(5,"fff");

In other shell: remove first 2 chars from .MYD file 

Now try checking it with MySQL:

mysql> check table crash;
+------------+-------+----------+----------+
| Table      | Op    | Msg_type | Msg_text |
+------------+-------+----------+----------+
| test.crash | check | status   | OK       |
+------------+-------+----------+----------+
1 row in set (0.27 sec)

mysql> select * from crash;
+------+------+
| i    | c    |
+------+------+
|    1 | fff  |
|    2 | fff  |
|    3 | fff  |
|    4 | fff  |
|    5 | fff  |
+------+------+
5 rows in set (0.00 sec)

mysql> flush table crash;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from crash;
ERROR 1030: Got error 127 from table handler
mysql> check table crash;
+------------+-------+----------+-------------------------------------------------+
| Table      | Op    | Msg_type | Msg_text                                        |
+------------+-------+----------+-------------------------------------------------+
| test.crash | check | error    | Size of datafile is: 278         Should be: 280 |
| test.crash | check | error    | Corrupt                                         |
+------------+-------+----------+-------------------------------------------------+
2 rows in set (0.00 sec)
[5 Mar 2003 11:01] Sergei Golubchik
The reason for this is that until FLUSH TABLES MYD file is open. When  it is altered in the editor, editor removes old file and creates a new one with the same name. But old file is not lost as it's open. So MySQL sees old MYD file.