Adding a column to a table with a few thousand rows is trivial. Doing it on a table with hundreds of millions of rows that serves live traffic is a different job, and a careless ALTER TABLE is a classic cause of outages. The goal is to change the structure without blocking reads and writes for longer than a blink.
What MySQL can do online
MySQL 8 supports several algorithms for ALTER TABLE, and you can ask for a specific one so that the statement fails fast instead of silently choosing a blocking method.
-- metadata-only change, nearly instant (MySQL 8.0.12 and later)
ALTER TABLE wallets ADD COLUMN risk_score INT NULL, ALGORITHM=INSTANT;
-- rebuilds in the background while allowing reads and writes
ALTER TABLE wallets ADD INDEX idx_owner (owner_id), ALGORITHM=INPLACE, LOCK=NONE;- INSTANT changes only the data dictionary. Adding a column is the typical case. Before MySQL 8.0.29 an instant added column could only go at the end of the table, and some operations still need other algorithms, so check the documentation for your version.
- INPLACE with LOCK=NONE avoids copying the table to a new file for many operations, such as adding a secondary index, and lets reads and writes continue, although it still uses a lot of I/O and takes time proportional to the table.
- COPY builds a full copy and blocks writes. Changing a column type or character set often falls here.
The metadata lock trap
Even a fast ALTER needs a brief exclusive metadata lock at the start and at the end. If a long-running transaction has touched the table, the ALTER waits for it. While it waits, every new query on that table queues behind the ALTER, so one forgotten open transaction plus one schema change can freeze the table for the whole application. Defend yourself:
- Check for long transactions before you start, for example in
information_schema.innodb_trx. - Set a short
lock_wait_timeoutfor the session running the change, so it gives up quickly instead of causing a pile-up, and retry later. - Run the change in a quiet period and watch it.
Shadow-table tools: gh-ost and pt-online-schema-change
For changes that MySQL cannot do online, or on very large tables where you want more control, use an online schema change tool. Both create a new empty "ghost" table with the desired structure, copy rows over in small chunks, keep it in sync with changes happening on the original, and finally swap the two names with a short atomic rename.
- gh-ost from GitHub reads the binary log to capture changes, so it needs no triggers on the table. It can be paused, throttled and even stopped at any time.
- pt-online-schema-change from Percona uses triggers on the original table to propagate writes.
With either tool, watch replication lag and let the tool throttle itself when replicas fall behind. Rehearse on a copy of production-sized data first, and know how to abort and roll back.
Expand, migrate, contract
Even a perfect online ALTER does not solve the application side. Old and new versions of the code run at the same time during a deploy, so the schema must work for both. Break a risky change into compatible steps.
- Expand: add the new column or table, nullable or with a default. Old code ignores it.
- Migrate: deploy code that writes both the old and the new structure, backfill existing rows in small batches, and switch reads to the new one.
- Contract: once nothing uses the old column, remove it in a later release.
Renaming a column is the standard example: add the new column, write both, backfill, switch reads, then drop the old one. Never rename in place while old code is still running.
Takeaway
Prefer INSTANT and INPLACE where they apply, guard against metadata lock pile-ups with timeouts and a check for long transactions, use gh-ost or pt-online-schema-change for heavy rebuilds, and split application-visible changes into expand, migrate and contract so every deploy step is compatible with the one before.
Comments