Currently Available: Need a skilled Software Developer for your next project?
Categories
Databases MySQL

Make MySQL `ALTER TABLE` Fail on Unsupported Online DDL

When you change a MySQL table definition without an ALGORITHM clause, MySQL picks the execution method for you. If its first choice does not work for that change, MySQL falls back to another method and can end up copying the entire table. You can prevent that fallback by adding an explicit ALGORITHM clause so MySQL rejects the statement when it cannot use the method you want. If your application also needs to read and write the table while the change runs, add a LOCK clause to tell MySQL what level of concurrent access it must allow.

MySQL refers to a table change as online DDL when it lets other sessions read and write the table during the change. The MySQL 8.4 ALTER TABLE documentation covers these clauses and explains when MySQL returns an error. Check the manual for your exact MySQL version, because what MySQL supports depends on the version, the storage engine, the table, and the specific change.

Specify the path and concurrency you require

MySQL accepts ALGORITHM=DEFAULT, INSTANT, INPLACE, or COPY. With INSTANT, MySQL updates only the table metadata and skips rebuilding the table. With INPLACE, MySQL avoids the COPY algorithm, though it can still rebuild the table. With COPY, MySQL copies the table. With DEFAULT, which MySQL also uses when you omit the clause, MySQL tries INSTANT first, then INPLACE, and finally COPY. When you name a specific algorithm, you require MySQL to use that method. If MySQL cannot apply the change that way, it returns an error instead of falling back.

For example, this statement requires an in-place change that permits concurrent reads and writes:

ALTER TABLE example
  ADD COLUMN note VARCHAR(100),
  ALGORITHM=INPLACE,
  LOCK=NONE;

Here, LOCK=NONE tells MySQL that it must allow concurrent reads and writes, or fail with an error. On InnoDB in MySQL 8.4, adding a column with ALGORITHM=INPLACE still makes MySQL rebuild the table, even though it lets other sessions read and write while the rebuild runs. That statement rules out the COPY algorithm, yet MySQL will still rebuild the table. Check the InnoDB operation support table before you rely on any combination of ALGORITHM and LOCK.

Pick the algorithm that matches what you can accept. If you want MySQL to update only table metadata, write ALGORITHM=INSTANT. If you only need to keep MySQL from using COPY, write ALGORITHM=INPLACE, keeping in mind that InnoDB can still rebuild the table. When you request ALGORITHM=INSTANT, use LOCK=DEFAULT, because MySQL does not allow LOCK=NONE with ALGORITHM=INSTANT.

Check version and table-specific limits

MySQL supports different algorithms depending on the change and the server version. MySQL added instant column addition in 8.0.12, letting InnoDB add a column by updating metadata instead of rebuilding the table. Before 8.0.29, MySQL could only place an instant column at the end of the table; starting in 8.0.29, it can place one at any position. Even then, instant addition has restrictions. MySQL cannot use it on compressed tables, tables with a FULLTEXT index for text searches, or temporary tables. Check the support table for your exact version rather than assuming a change qualifies by name.

The current state of an InnoDB table also determines whether MySQL can run an instant change. Each time you add or drop a column instantly, MySQL records a new row version for the table's column layout in INFORMATION_SCHEMA.INNODB_TABLES.TOTAL_ROW_VERSIONS. In MySQL 8.4, the limit is 64. Once a table hits 64 row versions, MySQL refuses any further instant column additions or drops until you rebuild the table, which resets the count to zero. If you run an instant column addition on a partitioned InnoDB table, MySQL also blocks you from using ALTER TABLE ... EXCHANGE PARTITION on that table later. Check these limits against your table before you plan the change.

LOCK=NONE has its own limits, separate from algorithm support. InnoDB rejects LOCK=NONE on tables that have foreign-key constraints using ON ... CASCADE or ON ... SET NULL. Changing a column's data type is another case where online DDL does not apply. MySQL 8.4 InnoDB rebuilds the table for that change and blocks concurrent reads and writes, so adding ALGORITHM=INPLACE, LOCK=NONE causes MySQL to reject the statement rather than run it online.

What fail-fast clauses do not guarantee

When you specify LOCK=NONE, you require MySQL to allow concurrent reads and writes while the change runs, assuming the change supports that access level. MySQL still takes locks. During InnoDB online DDL, MySQL can wait to acquire an exclusive metadata lock, which protects the table definition, and InnoDB also requires that lock during the final phase of the change. If a long-running transaction already holds a metadata lock on the table, MySQL waits until that transaction finishes or the lock wait times out. See the documented online DDL limitations and this explanation of why MySQL DDL waits on metadata locks.

Even when you require a specific algorithm and lock level, MySQL can still consume heavy resources or fail mid-way. While running online DDL, InnoDB can use substantial disk space and system resources, overflow the configured online alter log size, conflict with concurrent writes, or cause replication lag. If a large rebuild fails, InnoDB can also spend significant time and resources rolling it back. The ALGORITHM and LOCK clauses make MySQL reject unsupported execution paths, yet they do not tell you how long the change will take or how much load it will put on production.

Verify the exact change before deployment

Before you run the statement in production, test the exact ALTER TABLE command in a non-production environment that matches your production MySQL version, storage engine, schema, and table settings. Use realistic data and concurrent traffic, then verify whether MySQL completes the change with your required algorithm and lock level or rejects it. A clean test run proves that MySQL accepts the change in that environment, though it does not guarantee the same runtime or lock wait behavior under production traffic.

Before testing an instant column add or drop on InnoDB, check TOTAL_ROW_VERSIONS for the table. For any table change, check the operation support table for your version and weigh disk space, resource usage, possible metadata-lock waits, and replication impact. MySQL documents these limits and failure conditions, yet it provides no single check that both avoids side effects and predicts how a change will behave in production.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *