Can MySQL 8.0 Atomic DDL undo schema changes during a transaction rollback?
0 reputation · 23 Mar 2022, 16:27 UTC
0 reputation · 23 Mar 2022, 16:27 UTC
MySQL 8.0 introduced atomic DDL for InnoDB to ensure that data dictionary changes are applied consistently. However, DDL statements typically trigger implicit commits, which prevents the use of a standard ROLLBACK to revert a schema change if a subsequent operation in the same session fails.
While atomic DDL ensures the server does not leave the data dictionary in a corrupted state during a crash, it is unclear how this interacts with user-initiated transaction boundaries when mixing DDL and DML. Specifically, when using ALGORITHM=INSTANT for adding columns, the metadata is updated immediately.
ROLLBACK from undoing an ALTER TABLE operation in MySQL 8.0?No. In MySQL 8.0 the "atomic DDL" feature guarantees that a DDL statement is either fully applied or fully rolled back in the event of a crash, but it does not make the statement transactional in the sense that a user‑initiated ROLLBACK can undo it.
ALTER TABLE … DROP COLUMN is the only way to revert it.Take a backup (full dump or filesystem snapshot) before performing a schema change that you might want to undo.
Generate a “reverse” DDL script in advance (e.g., ALTER TABLE t DROP COLUMN c) and run it manually if the subsequent operation fails.
Use an online‑schema‑change tool (gh‑ost, pt-online-schema-change, or MySQL’s own ALTER TABLE … ALGORITHM=INPLACE when appropriate) that keeps the original table until the cut‑over is safe.
If you need true transactional schema changes, consider using a database that supports transactional DDL (e.g., PostgreSQL) or a migration framework that records the state and can roll back via separate scripts.
Are you certain that the tables involved use the InnoDB engine? Non‑transactional engines (e.g., MyISAM) do not benefit from atomic DDL at all, and the behavior described above applies only to InnoDB in MySQL 8.0.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.