From fae95a4933899edfd560735a3977a947e5551410 Mon Sep 17 00:00:00 2001 From: Kristofer Pettersson Date: Thu, 16 Jul 2009 01:23:57 +0200 Subject: Bug#45781 infinite hang/crash in "opening tables" after handler tries to open merge table The MERGE table storage engine does not support the HA_CAN_SQL_HANDLE feature and any attempt to open the merge table will fail with ER_ILLEGAL_HA. After an error occurred the tables that was opened must be closed again or they will be left in an inconsistent state. However, the assumption made in the code for closing and register handler tables was that only one table will be opened, and this is not true for MERGE tables which will cause multiple tables to open. The next time a SELECT operation was issued on the merge table it caused the system to freeze. This patch fixes this issue by making sure that all tables which are opened also are closed in the event of an error. mysql-test/r/merge.result: Added test case for bug 45781 mysql-test/t/merge.test: Added test case for bug 45781 sql/sql_handler.cc: * mysql_ha_open() was never ment to open more than one table. If we encounter more tables, we should close all tables related to the current substatement and raise an exception. --- mysql-test/t/merge.test | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'mysql-test/t/merge.test') diff --git a/mysql-test/t/merge.test b/mysql-test/t/merge.test index 8760876b7ee..0d90468fc8d 100644 --- a/mysql-test/t/merge.test +++ b/mysql-test/t/merge.test @@ -1555,4 +1555,28 @@ SELECT * FROM m1; DROP TABLE m1; DROP TABLE t1; +--echo # +--echo # Bug45781 infinite hang/crash in "opening tables" after handler tries to +--echo # open merge table +--echo # + +--disable_warnings +DROP TABLE IF EXISTS m1,t1; +--enable_warnings + +CREATE TABLE t1(a int)engine=myisam; +CREATE TABLE t2(a int)engine=myisam; +CREATE TABLE t3(a int)engine=myisam; +CREATE TABLE t4(a int)engine=myisam; +CREATE TABLE t5(a int)engine=myisam; +CREATE TABLE t6(a int)engine=myisam; +CREATE TABLE t7(a int)engine=myisam; +CREATE TABLE m1(a int)engine=merge union=(t1,t2,t3,t4,t5,t6,t7); +SELECT 1 FROM m1; +--error ER_ILLEGAL_HA +HANDLER m1 OPEN; +DROP TABLE m1,t1,t2,t3,t4,t5,t6,t7; +--error ER_NO_SUCH_TABLE +SELECT 1 FROM m1; # Should not hang! + --echo End of 5.1 tests -- cgit v1.2.1 From 0d61bd9dcee3046076d98b8c49fbcf2f6ee5b9d9 Mon Sep 17 00:00:00 2001 From: V Narayanan Date: Thu, 30 Jul 2009 16:04:41 +0530 Subject: Bug#45800 crash when replacing into a merge table and there is a duplicate A REPLACE in the MERGE engine is actually a REPLACE into one (FIRST or LAST) of the underlying MyISAM tables. So in effect the server works on the meta data of the MERGE table, while the real insert happens in the MyISAM table. The MERGE table has no index, while MyISAM has a unique index. When a REPLACE into a MERGE table ( and the REPLACE conflicts with a duplicate in a child table) is done, we try to access the duplicate key information for the MERGE table. This information actually does not exist, hence this results in a crash. The problem can be resolved by modifying the MERGE engine to provide us the duplicate key information directly, instead of just returning the MyISAM index number as the error key. Then the SQL layer (or "the server") does not try to access the key_info of the MERGE table, which does not exist. The current patch modifies the MERGE engine to provide the position for a record where a unique key violation occurs. include/myisammrg.h: Bug#45800 crash when replacing into a merge table and there is a duplicate Add a member to the st_mymerge_info structure that will store the duplicate key offset in the MERGE table. This offset will be the sum of the record offset of the MyISAM table within the MERGE table and the offset of the record within the MyISAM table. mysql-test/r/merge.result: Bug#45800 crash when replacing into a merge table and there is a duplicate Result file for the test case. mysql-test/t/merge.test: Bug#45800 crash when replacing into a merge table and there is a duplicate Added test case for both REPLACE and INSERT...ON DUPLICATE UPDATE. storage/myisammrg/ha_myisammrg.cc: Bug#45800 crash when replacing into a merge table and there is a duplicate The info method now will process the HA_STATUS_ERRKEY flag and will return the index and the offset of the duplicate key. storage/myisammrg/ha_myisammrg.h: Bug#45800 crash when replacing into a merge table and there is a duplicate Set the HA_DUPLICATE_POS flag to indicate that the duplicate key information is now available in the MERGE storage engine. storage/myisammrg/myrg_info.c: Bug#45800 crash when replacing into a merge table and there is a duplicate We modify the myrg_status function to return the position of the duplicate key. The duplicate key position in the MERGE table will be the MyISAM file_offset and the offset within the MyISAM table of the start position of the records. --- mysql-test/t/merge.test | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'mysql-test/t/merge.test') diff --git a/mysql-test/t/merge.test b/mysql-test/t/merge.test index 0d90468fc8d..39c805d2b5b 100644 --- a/mysql-test/t/merge.test +++ b/mysql-test/t/merge.test @@ -1515,6 +1515,49 @@ insert into m1 (col1) values (1); drop table m1, t1; +--echo # +--echo # Bug#45800 crash when replacing into a merge table and there is a duplicate +--echo # + +--echo # Replace duplicate value in child table when merge table doesn't have key +CREATE TABLE t1 (c1 INT PRIMARY KEY) ENGINE=MyISAM; +CREATE TABLE m1 (c1 INT NOT NULL) ENGINE=MRG_MyISAM INSERT_METHOD=LAST UNION=(t1); +INSERT INTO m1 VALUES (666); +SELECT * FROM m1; +--echo # insert the duplicate value into the merge table +REPLACE INTO m1 VALUES (666); +SELECT * FROM m1; +DROP TABLE m1, t1; + +--echo # Insert... on duplicate key update (with duplicate values in the table) +CREATE TABLE t1 (c1 INT PRIMARY KEY) ENGINE=MyISAM; +CREATE TABLE m1 (c1 INT NOT NULL) ENGINE=MRG_MyISAM INSERT_METHOD=LAST UNION=(t1); +INSERT INTO m1 VALUES (666); +SELECT * FROM m1; +--echo # insert the duplicate value into the merge table +INSERT INTO m1 VALUES (666) ON DUPLICATE KEY UPDATE c1=c1+1; +SELECT * FROM m1; +DROP TABLE m1, t1; + +--echo # Insert duplicate value on MERGE table, where, MERGE has a key but MyISAM has more keys +CREATE TABLE t1 (c1 INT, c2 INT, UNIQUE (c1), UNIQUE (c2)); +CREATE TABLE m1 (c1 INT, c2 INT, UNIQUE (c1)) ENGINE=MRG_MyISAM INSERT_METHOD=LAST UNION=(t1); +INSERT INTO m1 VALUES (1,2); +--echo # insert the duplicate value into the merge table +--error ER_DUP_ENTRY +INSERT INTO m1 VALUES (3,2); +DROP TABLE m1,t1; + +--echo # Try to define MERGE and MyISAM with keys on different columns +CREATE TABLE t1 (c1 INT, c2 INT, UNIQUE (c1)); +CREATE TABLE m1 (c1 INT, c2 INT, UNIQUE (c2)) ENGINE=MRG_MyISAM INSERT_METHOD=LAST UNION=(t1); +--echo # Try accessing the merge table for inserts (error occurs) +--error ER_WRONG_MRG_TABLE +INSERT INTO m1 VALUES (1,2); +--error ER_WRONG_MRG_TABLE +INSERT INTO m1 VALUES (1,4); +DROP TABLE m1,t1; + # #Bug #44040 MySQL allows creating a MERGE table upon VIEWs but crashes #when using it -- cgit v1.2.1