Description:
The problem arises when an "exchange partition" statement crashes unexpectedly and the server is subsequently restarted. In such cases, the partition table involved in the DDL statement may not be found.
Root Cause:
During the InnoDB undo recovery process, the DDL transaction undo log is treated as a DML transaction undo. Consequently, the transaction is rolled back in the background after the DDL log recovery.
How to repeat:
1. Ensure that the “innodb_print_ddl_logs" variable is enabled and execute the ALTER TABLE... EXCHANGE PARTITION... WITH TABLE... statement. When the DDL logs start appearing in the error log but haven't finished printing, immediately kill the server.
2. Restart the server and then use the SHOW CREATE TABLE statement to inspect the tables recovered from replaying the DDL log. The result will report "Tablespace is missing for table xxx".
Suggested fix:
The state "undo->dict_operation" of the exchange partition is false, which leads to abnormal DDL log recovery. The problem can be analyzed in the following steps according to the execution process of exchange partition:
1. "open_tables(thd, &table_list, &table_counter, 0,...)” will allocate an empty InnoDB trx for thd.
2. "dd::sdi::drop_all_for_table" drops the SDI information for two tables. The InnoDB trx will first be assigned an update undo ptr in “trx_undo_assign_undo".
2.1. both the "(trx->mysql_thd && !trx->ddl_operation && thd_is_dd_update_stmt(trx->mysql_thd))” and "trx->ddl_operation” in “trx_undo_assign_undo" are false;
2.2. As a result,, the “trx_undo_mark_as_dict_operation" is not executed. Consequently, the state of the ddl undo header TRX_UNDO_DICT_TRANS is false.
3. During the rename process, the DDL log "DDL log delete" is executed as part of the current transaction and records the undo log in the current transaction undo log.
4. If a crash occurs during the rename tablespace process, the DDL log will be rolled back after a restart. These undo logs are not parsed in "trx_resurrect_modified_tables(false)" and rolled back in "trx_rollback_or_clean_recovered(FALSE)".
5. Then the transaction undo recovery is after the DDL log recovery at restart. As a result, the ddl log is empty and ddl log recovery is failure.