This blog post explains the cause of “ERROR 1412 (HY000): Table definition has changed, please retry transaction” with the specific Isolation level settings.

Background

As per the MySQL documentation, this error should occur for “operations that make a temporary copy of the original table and delete the original table when the temporary copy is built.”

Consistent read does not work over ALTER TABLE operations that make a temporary copy of the original table and delete the original table when the temporary copy is built. When you reissue a consistent read within a transaction, rows in the new table are not visible because those rows did not exist when the transaction’s snapshot was taken. In this case, the transaction returns an error: ER_TABLE_DEF_CHANGED, “Table definition has changed, please retry transaction”.

Here is the test example verifying the above statement when transaction_isolation | REPEATABLE-READ and temp table are created.

Session 1:

 

Session 2: Changing the column data type will create a temp table.

## While this is running, checked shows processlist, which shows the temp table,

Session 1:

 Another undocumented case

However, we can also see this ER_TABLE_DEF_CHANGED error for ALTER operations, which do not create a temporary copy of the table. This conflicts with what is mentioned in the MySQL documentation for the ER_TABLE_DEF_CHANGED occurrence.

Here is the example case:

Adding a column with ALGORITHM=INSTANT will NOT make a temporary copy, so the expectation is that it should not throw an ER_TABLE_DEF_CHANGED error.

# Create two tables with some data using sysbench.

 

Session1:

 

Session 2:


Session 1:

Summary

The error occurs only when transaction_isolation is REPEATABLE-READ, as it reads the snapshot established by the first such read in that transaction. You will not see this issue with the READ COMMITTED isolation level, as each consistent read within a transaction sets and reads its own fresh snapshot.

Here is the documentation bug: https://bugs.mysql.com/bug.php?id=116132


Percona offers enterprise-grade scalability and performance without traditional enterprise drawbacks. We deliver secure, tested, open source software complete with advanced features like backup, monitoring, and encryption only otherwise found in MySQL Enterprise Edition.

 

Why Customers Choose Percona for MySQL

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments