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

How MySQL Online ALTER TABLE Affects Live Traffic

When you run ALTER TABLE in a production MySQL database, MySQL changes the table's definition while your applications keep sending queries. MySQL can perform some changes while reads and writes continue, yet those online changes can still make application requests wait. MySQL uses a metadata lock to protect a table's definition. If another session already holds that lock, MySQL waits to run the ALTER TABLE, and new requests to the same table can queue behind it. For other changes, MySQL copies or rebuilds the table data, which uses server resources or blocks writes.

You should choose how to run a schema change based on the exact ALTER TABLE statement, the table itself, and your MySQL version. On MySQL 8.4 with InnoDB, check the algorithm, which controls how MySQL applies the change, and the lock level, which controls whether queries and data changes can continue. Checking both lets you decide whether to run native DDL, use a migration tool, or split the work across application code and the database.

How metadata locks turn a brief wait into a traffic queue

Any transaction that touches a table holds a metadata lock until that transaction ends. When you run ALTER TABLE, MySQL needs metadata locks to modify the table definition and commit the new one. Even when MySQL allows reads and writes during the main part of the work, an open transaction can force MySQL to wait for the lock it needs to finish.

That wait can stall queries that had nothing to do with the original blocker. MySQL documents a queueing pattern where an open transaction holds a lock, MySQL requests an exclusive metadata lock for the ALTER TABLE and waits, and subsequent requests to the table line up behind it. If the lock never clears in time, MySQL times out the ALTER TABLE. A single long-running transaction can turn a brief final lock phase into a visible outage for application traffic, as described in the MySQL concurrency documentation.

Before you run DDL, check SHOW FULL PROCESSLIST for sessions showing Waiting for table metadata lock. You can also query the Performance Schema metadata_locks table to see granted and pending lock requests and track down the sessions holding them. For broader context on how MySQL handles locks, see how MySQL locking and concurrency control works.

What the ALTER algorithm allows

You use the ALGORITHM clause to tell MySQL how to change the table, and the LOCK clause to tell it whether queries and writes can continue while it works. These two clauses control different things. MySQL can avoid copying data to a new table and still rebuild the table in place, and a permissive lock level will not prevent metadata-lock waits.

Algorithm What MySQL does Traffic effect
INSTANT Changes table metadata without rewriting table data. Allows concurrent reads and writes, though MySQL still acquires metadata locks.
INPLACE Avoids copying table data to a separate table, though some in-place changes still rebuild the table. Allows concurrent writes for some statements; resource use and lock phases depend on the exact change.
COPY Creates a copy of the table and copies rows into it. Blocks concurrent writes. Reads are generally allowed while MySQL prepares and populates the copy, with documented exceptions.

The MySQL ALTER TABLE documentation covers these algorithms and what MySQL does when one is unsupported. In MySQL 8.4, if you leave out ALGORITHM, MySQL uses INSTANT when the storage engine and statement support it. If they do not, MySQL tries INPLACE, and falls back to COPY if INPLACE is unsupported. If you specify an algorithm explicitly and MySQL cannot use it, MySQL returns an error instead of falling back.

The LOCK clause sets the level of access MySQL must allow. LOCK=NONE permits concurrent queries and data changes when the statement supports them, and makes MySQL return an error if it cannot. LOCK=SHARED allows reads while blocking writes. LOCK=EXCLUSIVE blocks both reads and writes. If you omit the clause, MySQL picks the most permissive lock level the statement supports. Adding LOCK=NONE stops MySQL from running a change that blocks writes, though MySQL can still pause on metadata-lock waits.

Which changes are lower risk?

The MySQL 8.4 InnoDB operation matrix is a good starting point for sorting changes by risk. Because the exact statement and table restrictions still apply, check the operation-specific support matrix before you rely on a general rule.

Example change MySQL 8.4 InnoDB behavior Production implication
Add a qualifying regular column with ALGORITHM=INSTANT Changes metadata without rewriting table data. Often a good candidate for native ALTER TABLE, once you confirm the table and statement meet instant-operation restrictions.
Change a column's default The operation matrix lists instant support. Usually much lighter than a table rewrite, though metadata locks and application compatibility still matter.
Add a secondary index Uses INPLACE, skips a table rebuild, and allows concurrent writes. Writes can continue, though MySQL still uses server resources and time to build the index.
Change a column's data type Rebuilds the table and blocks concurrent writes in the listed case. Plan for write blocking, use a migration tool, or schedule a maintenance window.
Add a stored generated column Rebuilds the table and blocks concurrent writes in the listed case. Requires careful planning on a busy table.
Add a primary key Rebuilds the table; allows concurrent writes under specific conditions. "In place" does not mean cheap. Verify the conditions and plan for rebuild costs.

When MySQL rebuilds a table in place, it consumes CPU, I/O, and disk space that your application queries also need. For some online changes, MySQL records concurrent writes in a temporary log and applies them at the end. If that log grows past innodb_online_alter_log_max_size, MySQL aborts the ALTER TABLE with DB_ONLINE_LOG_TOO_BIG. Raising that limit gives MySQL more room to buffer writes, though MySQL may then hold the table locked for longer while it applies the logged changes. MySQL details these limits in its online DDL space requirements.

When MySQL copies a table, it needs disk space for the temporary table and blocks writes until it finishes. Neither method has a universal safe table size or a predictable runtime. Before you run the change in production, weigh the table size, write rate, and free disk space against how much replica lag and application latency your service can handle.

When to use native DDL, a migration tool, or a phased rollout

Run native ALTER TABLE when MySQL supports the algorithm and lock level you need and your server can absorb the extra load. If you cannot afford blocked writes, add constraints such as ALGORITHM=INSTANT or ALGORITHM=INPLACE, LOCK=NONE where supported so MySQL returns an error instead of falling back. Test the exact statement on a representative table definition. A small test table confirms which algorithm and lock level MySQL supports, though it will not tell you how long the change will take on a large production table.

When native DDL would block writes for too long or trigger a heavy rebuild, you can use a migration tool to copy the data in the background. Percona's pt-online-schema-change creates an altered shadow table, copies rows in chunks, uses triggers to forward live writes from the original table to the copy, and then swaps the tables. It comes with constraints: its triggers conflict if the original table already has triggers, it generally needs a primary key or unique index, and foreign keys complicate the final swap.

GitHub's gh-ost lets you throttle or pause the copy and delay cutover, the final table swap. Its documentation notes that cutover can still briefly block pending connections. Migration tools shift the heavy work out of the critical path, though they still use server resources and carry cutover risks. Check how each tool handles your table's triggers, keys, foreign keys, replication setup, and acceptable pause time. You can read more about gh-ost in this gh-ost migration overview.

When old and new versions of your application need to run side by side during a deployment, split the work into a phased rollout using an expand-and-contract sequence:

  1. Expand the schema by adding a compatible element, using a qualifying instant or online ALTER TABLE when possible.
  2. Deploy application code that reads and writes both the old and new formats.
  3. Backfill existing rows in controlled batches while you watch server load and replica lag.
  4. Switch reads and writes to the new format after you validate the data.
  5. Contract the schema by removing the old column or constraint once older code no longer depends on it.

Splitting the change this way means you do not have to coordinate a schema change and a code deploy at the exact same moment. You still need to check the algorithm, metadata locks, and traffic impact for the final removal.

What to check before running the ALTER

Start by confirming your production MySQL version, storage engine, and full table definition, including indexes, triggers, and foreign keys. Look up the exact statement in that version's support matrix to see whether MySQL rebuilds the table, allows concurrent writes, or falls back to copying the table.

Right before you run the change, check for long-running transactions and pending metadata locks. Estimate the temporary disk space, I/O, and CPU MySQL will need, and decide how much replica lag and application latency you can tolerate. Test the statement on a representative table first. While the change runs, watch the ALTER TABLE progress, application latency, server load, and replication lag. If you use a migration tool, test both the data copy and the cutover ahead of time, and verify your backups before touching production. For more on what can go wrong, see why database migrations fail 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 *