Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you rollback a query in state 'committing alter table to storage engine'

We've got an InnoDB table with 70 million rows, and we have been trying to run an alter table statement to modify and add a couple of columns. The query seems to have altered the table, and is now in the state of 'committing alter table to storage engine'.

START TRANSACTION;
ALTER TABLE table
  MODIFY COLUMN column1 int(11) NOT NULL DEFAULT 0,
  MODIFY COLUMN column2 tinyint(1) NOT NULL DEFAULT 1,
  ADD COLUMN column3 int(11),
  ADD COLUMN column4 int(11) NOT NULL DEFAULT 1,
  ADD COLUMN column5 varchar(255);
COMMIT;

This has been running overnight, and is at 19 hours at the current time. We do not have the performance schema enabled so cannot look at an estimated time of completion. My concern lies as to what the query is doing and whether the query will rollback if killed. I have seen other questions relate to queries that are stuck in copying to tmp tables, or awaiting a table lock. However I cannot find anything about being stuck while the alter table is committing.

Is it safe to kill a query in this state, and if the query is killed, will it rollback successfully?

The server is running MariaDB 10.2

like image 231
Steve Norwood Avatar asked Mar 06 '18 13:03

Steve Norwood


People also ask

Can you roll back ALTER TABLE?

I am sorry to have to inform you, but ALTER TABLE cannot be rolled back. In fact, ALTER TABLE triggers an implicit commit. In your case, the ALTER TABLE will either finish or you will have a temp table left hanging around.

Can we rollback after commit in MySQL?

No, there's no query that will "undo" a committed data-modifying query. If you have a backup of the database, you can restore the backup and use DBA tools (in MySQL's case, it's mysqlbinlog) to "replay" all data-modifying queries from the logs since the backup back to the database, but skip over the problem query.

Why rollback is not working in MySQL?

and make sure that you are not using COMMIT after the Query which you need to rollback. Refer Table Engines and Transaction. And When a DB connection is created, it is in auto-commit mode by default.

How do I rollback a query in MySQL?

Example. Following statement reverts the changes after the last commit. All the changes done past the last commit will be reverted if we rollback a transaction. Since we have inserted the last 4 records after commit, they will be reverted at the time of roll back.


2 Answers

I implemented the ALGORITHM=INPLACE and LOCK=NONE for InnoDB in MySQL 5.6. Depending on the previous table definition, this operation could imply ALGORITHM=INPLACE, or it could fall back to ALGORITHM=COPY. Starting with MariaDB 10.3 (MDEV-11369), ADD COLUMN would be instantaneous; before that, the table would have to be rebuilt. (The syntax ALGORITHM=INPLACE is very misleading.) Starting with MariaDB 10.2.13 and 10.3.5 (MDEV-11415), ALGORITHM=COPY will no longer write undo log records for the individual rows, and the rollback (in case of client disconnect or killed server) should be much faster.

like image 42
Marko Mäkelä Avatar answered Sep 18 '22 22:09

Marko Mäkelä


From the documentation:

Some statements cannot be rolled back. In general, these include data definition language (DDL) statements, such as those that create or drop databases, those that create, drop, or alter tables or stored routines.

You should design your transactions not to include such statements. If you issue a statement early in a transaction that cannot be rolled back, and then another statement later fails, the full effect of the transaction cannot be rolled back in such cases by issuing a ROLLBACK statement.

like image 151
T Doggy Dog Avatar answered Sep 18 '22 22:09

T Doggy Dog