From: Dmitry Lenev Date: November 19 2010 7:26am Subject: bzr commit into mysql-5.5-runtime branch (Dmitry.Lenev:3193) Bug#57985 List-Archive: http://lists.mysql.com/commits/124360 X-Bug: 57985 Message-Id: <20101119072628.8854E1E50D7@mockturtle> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1412051110==" --===============1412051110== MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline #At file:///home/dlenev/src/bzr/mysql-5.5-57985/ based on revid:jon.hauglid@stripped 3193 Dmitry Lenev 2010-11-19 Fix for bug #57985 "ONLINE/FAST ALTER PARTITION can fail and leave the table unusable". Failing ALTER statement on partitioned table could have left this table in an unusable state. This has happened in cases when ALTER was executed using "fast" algorithm, which doesn't involve copying of data between old and new versions of table, and the resulting new table was incompatible with partitioning function in some way. The problem stems from the fact that discrepancies between new table definition and partitioning function are discovered only when the table is opened. In case of "fast" algorithm this has happened too late during ALTER's execution, at the moment when all changes were already done and couldn't have been reverted. In the cases when "slow" algorithm, which copies data, is used such discrepancies are detected at the moment new table definition is opened implicitly when new version of table is created in storage engine. As result ALTER is aborted before any changes to table were done. This fix tries to address this issue by ensuring that "fast" algorithm behaves similarly to "slow" algorithm and checks compatibility between new definition and partitioning function by trying to open new definition after .FRM file for it has been created. Long term we probably should implement some way to check compatibility between partitioning function and new table definition which won't involve opening it, as this should allow much cleaner fix for this problem. @ mysql-test/r/partition_innodb.result Added test for bug #57985 "ONLINE/FAST ALTER PARTITION can fail and leave the table unusable". @ mysql-test/t/partition_innodb.test Added test for bug #57985 "ONLINE/FAST ALTER PARTITION can fail and leave the table unusable". @ sql/sql_table.cc Ensure that in cases when .FRM for partitioned table is created without creating table in storage engine (e.g. during "fast" ALTER TABLE) we still open table definition. This allows to check that definition of created table/.FRM is compatible with its partitioning function. modified: mysql-test/r/partition_innodb.result mysql-test/t/partition_innodb.test sql/sql_table.cc === modified file 'mysql-test/r/partition_innodb.result' --- a/mysql-test/r/partition_innodb.result 2010-09-13 13:56:56 +0000 +++ b/mysql-test/r/partition_innodb.result 2010-11-19 07:26:09 +0000 @@ -489,3 +489,31 @@ Warning 1265 Data truncated for column ' Error 1067 Invalid default value for 'b' SET SESSION sql_mode = @old_mode; DROP TABLE t1; +# +# Bug#57985 "ONLINE/FAST ALTER PARTITION can fail and leave the +# table unusable". +# +DROP TABLE IF EXISTS t1; +CREATE TABLE t1 (a bigint not null, b int not null, PRIMARY KEY (a)) +ENGINE = InnoDB PARTITION BY KEY(a) PARTITIONS 2; +INSERT INTO t1 values (0,1), (1,2); +# The below ALTER should fail. It should leave the +# table in its original, non-corrupted, usable state. +ALTER TABLE t1 ADD UNIQUE KEY (b); +ERROR HY000: A UNIQUE INDEX must include all columns in the table's partitioning function +# The below statements should succeed, as ALTER should +# have left table intact. +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` bigint(20) NOT NULL, + `b` int(11) NOT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +/*!50100 PARTITION BY KEY (a) +PARTITIONS 2 */ +SELECT * FROM t1; +a b +1 2 +0 1 +DROP TABLE t1; === modified file 'mysql-test/t/partition_innodb.test' --- a/mysql-test/t/partition_innodb.test 2010-08-20 17:15:48 +0000 +++ b/mysql-test/t/partition_innodb.test 2010-11-19 07:26:09 +0000 @@ -569,3 +569,24 @@ SET SESSION sql_mode = 'NO_ZERO_DATE'; OPTIMIZE TABLE t1; SET SESSION sql_mode = @old_mode; DROP TABLE t1; + + +--echo # +--echo # Bug#57985 "ONLINE/FAST ALTER PARTITION can fail and leave the +--echo # table unusable". +--echo # +--disable_warnings +DROP TABLE IF EXISTS t1; +--enable_warnings +CREATE TABLE t1 (a bigint not null, b int not null, PRIMARY KEY (a)) + ENGINE = InnoDB PARTITION BY KEY(a) PARTITIONS 2; +INSERT INTO t1 values (0,1), (1,2); +--echo # The below ALTER should fail. It should leave the +--echo # table in its original, non-corrupted, usable state. +--error ER_UNIQUE_KEY_NEED_ALL_FIELDS_IN_PF +ALTER TABLE t1 ADD UNIQUE KEY (b); +--echo # The below statements should succeed, as ALTER should +--echo # have left table intact. +SHOW CREATE TABLE t1; +SELECT * FROM t1; +DROP TABLE t1; === modified file 'sql/sql_table.cc' --- a/sql/sql_table.cc 2010-11-16 10:00:12 +0000 +++ b/sql/sql_table.cc 2010-11-19 07:26:09 +0000 @@ -3815,6 +3815,46 @@ void sp_prepare_create_field(THD *thd, C (void) prepare_blob_field(thd, sql_field); } + +/** + Auxiliary function which allows to check if freshly created .FRM + file for table can be opened. + + @retval FALSE - Success. + @retval TRUE - Failure. +*/ + +static bool check_if_created_table_can_be_opened(THD *thd, + const char *path, + const char *db, + const char *table_name, + HA_CREATE_INFO *create_info, + handler *file) +{ + TABLE table; + TABLE_SHARE share; + bool result; + + /* + It is impossible to open definition of partitioned table without .par file. + */ + if (file->ha_create_handler_files(path, NULL, CHF_CREATE_FLAG, create_info)) + return TRUE; + + init_tmp_table_share(thd, &share, db, 0, table_name, path); + + result= (open_table_def(thd, &share, 0) || + open_table_from_share(thd, &share, "", 0, (uint) READ_ALL, + 0, &table, TRUE)); + if (! result) + (void) closefrm(&table, 0); + + free_table_share(&share); + (void) file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info); + return result; +} + + /* Create a table @@ -4241,6 +4281,29 @@ bool mysql_create_table_no_lock(THD *thd thd->thread_specific_used= TRUE; } +#ifdef WITH_PARTITION_STORAGE_ENGINE + else if (part_info && create_info->frm_only) + { + /* + For partitioned tables we can't find some problems with table + until table is opened. Therefore in order to disallow creation + of corrupted tables we have to try to open table as the part + of its creation process. + In cases when both .FRM and SE part of table are created table + is implicitly open in ha_create_table() call. + In cases when we create .FRM without SE part we have to open + table explicitly. + */ + if (check_if_created_table_can_be_opened(thd, path, db, table_name, + create_info, file)) + { + char frm_name[FN_REFLEN]; + strxmov(frm_name, path, reg_ext, NullS); + (void) mysql_file_delete(key_file_frm, frm_name, MYF(0)); + goto err; + } + } +#endif error= FALSE; err: --===============1412051110== MIME-Version: 1.0 Content-Type: text/bzr-bundle; charset="us-ascii"; name="bzr/dmitry.lenev@stripped" Content-Transfer-Encoding: 7bit Content-Disposition: inline # Bazaar merge directive format 2 (Bazaar 0.90) # revision_id: dmitry.lenev@stripped # target_branch: file:///home/dlenev/src/bzr/mysql-5.5-57985/ # testament_sha1: 77971b19ff4530c683b286694d11e6479b9b641c # timestamp: 2010-11-19 10:26:28 +0300 # base_revision_id: jon.hauglid@stripped\ # 5mdoncvyt2hwmruc # # Begin bundle IyBCYXphYXIgcmV2aXNpb24gYnVuZGxlIHY0CiMKQlpoOTFBWSZTWZQi+YUABcN/gFDwAAJ59/// f+/+6v////5gD22fN2rgN6b68Kdhrok7jLet67NkfbdvuPryO93V1dq6RG+7eeC7fBKJqANUw0Gk YjQ0GmQmmjynqMT1G0ho00aNBp6glCACZIaRoExUz1NHlTNT1P1TQxA9IBoaMEaegaCZIk09QekN AB6hoMgwgAAAANAAkJIUzSNBDJkGgaAANAAMgAAABFIkwmSZNHqnhTyp6n6mwinpqaaPU2k0yAaH lAA0GgkSEJoyaNJtTJpoEaNNU/Uxqj1G9SZPUaNNNPUep5QGjSMIl61EC1bG0Qpqg3LipBG4XYu7 x2Kd/j+j7eO/hw1UNtf2iEBgBWDLh+3yqL/ViKEcQHLSak9ZUucEqzIEkyLFObSfX6Xy66TmtYuo Tc0O2Po/04PkhD1LbPVVdKOgvuqKM2+oz0X8vDc8LgW+xRhN5SR6zh/yXp7/Ou+avaeiQ4q2i9+e NmMAKoMHz2V6s3DYfj/7OgFahG4aUCbfRCIQjRgi3L1PuvYEJ1B86YEj47honi5fVFLz4D3df4Bz QqGQq3WnmqPQV0qCMGYMGL4CPwBFpwd/5sFPdYYwwOdjdEonPYKp7pylCvUum6bVC703cmEmtngf GLives/57j7vnktBp9Az0hMZ4YXFZC4YFqKGFCO3df8i5SDhxIy15bm/SFB88RjlJ9njmwBPjhdB U83RAwQ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