skip to content

In MySQL 8.4, what do ALTER TABLE's ALGORITHM=INSTANT, INPLACE and COPY mean, and why pin ALGORITHM and LOCK explicitly?

level: middleimportance: must knowfreq 60%

answer

  1. metadata, rebuild or copy
  2. the server silently falls back
  3. fail fast instead of blocking
  4. VARCHAR length bytes at 255
  5. 64 instant row versions

basics

~20 s

INSTANT changes only data-dictionary metadata, INPLACE works inside InnoDB and usually allows concurrent writes, and COPY rebuilds the table row by row while blocking writes. Pinning ALGORITHM and LOCK makes an ALTER fail immediately rather than silently fall back to a blocking copy.

solid answer

~50 s

MySQL runs `ALTER TABLE` with one of three algorithms. `INSTANT` only edits metadata in the data dictionary, so it takes the same time on any table size; in 8.4 it is the default for adding, dropping and renaming columns and for changing defaults. `INPLACE` avoids a row-by-row copy but may still rebuild the table inside InnoDB, logging concurrent DML and applying it at the end; adding a secondary index is the classic case. `COPY` builds a new table and copies every row, and concurrent DML is not permitted; changing a column's data type, such as `INT` to `BIGINT`, only works this way. If you omit `ALGORITHM`, MySQL picks `INSTANT`, else `INPLACE`, else `COPY` without asking. Writing `ALGORITHM=INSTANT` or `ALGORITHM=INPLACE, LOCK=NONE` turns a surprise multi-hour write outage into an immediate error such as 1846, ALGORITHM=INPLACE is not supported.

code

sql · 15 lines
sql
-- orders.status is VARCHAR(20) NOT NULL DEFAULT 'pending' in utf8mb4 (80 bytes, 1 length byte)

ALTER TABLE orders
  ADD COLUMN gift_message VARCHAR(200) NULL,
  ALGORITHM=INSTANT;

ALTER TABLE orders
  MODIFY status VARCHAR(63) NOT NULL DEFAULT 'pending',
  ALGORITHM=INPLACE, LOCK=NONE;   -- 252 bytes: still 1 length byte

ALTER TABLE orders
  MODIFY status VARCHAR(64) NOT NULL DEFAULT 'pending',
  ALGORITHM=INPLACE, LOCK=NONE;   -- 256 bytes: needs 2 length bytes
-- ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported.
-- Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY.

go deeper

for a junior

Recall the three algorithm names and that COPY blocks writes while INSTANT only touches metadata.

for a middle

Explain the fallback order, the LOCK levels, the VARCHAR length-byte rule and why a type change needs COPY, using a concrete ALTER on a large table.

for a senior

Show that you pin ALGORITHM and LOCK in every migration, track TOTAL_ROW_VERSIONS, size the online log and plan for replica lag from long in-place rebuilds.

for a principal

Turn the algorithm rules into a review gate: which changes may run natively in business hours and which must go through an online schema-change tool.

## Three ways to execute an ALTER TABLE When InnoDB alters a table, MySQL chooses one of three algorithms: - **`INSTANT`**: only the table's metadata in the data dictionary changes. Table data is untouched, so the operation is effectively instantaneous on a table of any size. An exclusive metadata lock may be taken briefly, and concurrent DML is permitted. - **`INPLACE`**: InnoDB performs the change without the server copying rows through a new table. It may still **rebuild the table in place**, and while it rebuilds or builds an index it records concurrent inserts, updates and deletes in a temporary online log that it applies at the end. Concurrent DML is typically supported. - **`COPY`**: MySQL creates a new table with the new definition, copies the data row by row and swaps the tables. **Concurrent DML is not permitted** for the duration. If the statement has no `ALGORITHM` clause, MySQL uses `INSTANT` when the clauses support it, otherwise `INPLACE`, otherwise `COPY`. That fallback is silent: the same statement can be instant for one change and a long blocking copy for another. ## Which common changes land where | Change | `INSTANT` | `INPLACE` | Concurrent DML | |---|---|---|---| | Add a column (any position) | yes, the 8.4 default | yes, but rebuilds | yes | | Drop a column | yes | yes, rebuilds | yes | | Rename a column, same type | yes | yes | yes | | Set or drop a column default | yes | yes | yes | | Add a secondary index | no | yes, no rebuild | yes | | Extend a `VARCHAR` within the same length-byte count | no | yes, metadata only | yes | | Extend a `VARCHAR` across 255 bytes | no | no, `COPY` only | no | | Change a column's data type | no | no, `COPY` only | no | Two rows deserve explanation: - **`VARCHAR` widening.** A `VARCHAR` stores a 1-byte length prefix when its values need at most 255 bytes, and 2 bytes beyond that. An in-place extension must keep the same number of length bytes. In `utf8mb4`, at four bytes per character, `VARCHAR(20)` to `VARCHAR(63)` (252 bytes) stays in place, while `VARCHAR(20)` to `VARCHAR(64)` (256 bytes) requires `COPY`. Shrinking a `VARCHAR` always requires `COPY`. - **Data-type changes.** Widening an `INT` key to `BIGINT` is a type change, so it is `COPY` only and blocks writes for as long as the copy takes. ## INSTANT's limits in 8.4 - Each instant add or drop creates a new **row version**; `INFORMATION_SCHEMA.INNODB_TABLES.TOTAL_ROW_VERSIONS` counts them. MySQL 8.4 allows **64** (later releases raised it), after which instant adds and drops fail with error 4092 until a rebuilding `ALTER TABLE` or `OPTIMIZE TABLE` resets the count. - Instant column changes are not available for tables with `ROW_FORMAT=COMPRESSED`, tables with a `FULLTEXT` index or temporary tables, and a statement cannot combine an instant change with clauses that do not support `INSTANT`. - An instant add that would push the maximum possible row size over the limit is rejected with error 4092 and a suggestion to use `INPLACE` or `COPY`. ## The LOCK clause and why to pin both `LOCK` states the least concurrency you will accept: - `LOCK=NONE`: concurrent reads and writes allowed. - `LOCK=SHARED`: reads allowed, writes blocked. - `LOCK=EXCLUSIVE`: reads and writes blocked. - `LOCK=DEFAULT`, or no clause: as much concurrency as the operation allows. If a requested `ALGORITHM` or `LOCK` level cannot be honoured, the statement **fails up front with an error** instead of degrading. That is the point of pinning them in every migration on a large table: 1. Write the intended level, for example `ALGORITHM=INSTANT` for a new nullable column. 2. If MySQL replies with error 1846 or a "not supported" message, you learned in milliseconds that the change needs a copy. 3. Plan that change with an online schema-change tool or a maintenance window, rather than discovering it as an outage. ## Costs that remain even when the ALTER is online - **Metadata locks.** Every algorithm can need an exclusive metadata lock, at least briefly, so a long-open transaction on the table can make even an `INSTANT` change wait, and queries queue behind it. - **The online log.** An `INPLACE` rebuild buffers concurrent DML in a temporary log capped by `innodb_online_alter_log_max_size` (128 MiB by default); if the log fills, the `ALTER TABLE` fails and uncommitted concurrent DML operations are rolled back. - **No throttle and replica lag.** An online DDL operation cannot be paused or throttled, and a replica only starts applying it after it has finished on the source, so a long in-place rebuild becomes replication lag.

  • In MySQL 8.4, how can you tell whether a table is close to the limit on instant column changes?
    Query INFORMATION_SCHEMA.INNODB_TABLES and read TOTAL_ROW_VERSIONS for the table. Each instant add or drop increments it, and at 64 MySQL 8.4 rejects further instant adds and drops with error 4092. A table-rebuilding ALTER TABLE or OPTIMIZE TABLE resets it to zero, which you can schedule before the limit becomes a blocker.
  • Why does MySQL refuse ALGORITHM=INPLACE when shrinking a VARCHAR(100) column to VARCHAR(50)?
    Decreasing a VARCHAR's size is not supported in place, because existing values may not fit and every row has to be checked and rewritten. MySQL requires ALGORITHM=COPY, which blocks concurrent DML, so on a large table you would use an online schema-change tool instead.
  • What happens to a MySQL INPLACE table rebuild when the busy table receives more concurrent writes than the online log can hold?
    InnoDB records concurrent DML in a temporary online log bounded by innodb_online_alter_log_max_size, 128 MiB by default. If the log exceeds that size, the ALTER TABLE fails and uncommitted concurrent DML is rolled back. Raising the limit lets more DML accumulate, at the cost of a longer locked apply phase at the end.

  • Omitting ALGORITHM is safe because MySQL always picks a non-blocking method
  • Changing INT to BIGINT can run with ALGORITHM=INPLACE, LOCK=NONE
  • An INSTANT ALTER needs no metadata lock at all
  • Any VARCHAR widening is a metadata-only change
  • ALGORITHM=INPLACE always means the table is not rebuilt

Assumes MySQL 8.4: INSTANT is the default algorithm for column add and drop, columns can be added instantly at any position, and the instant row-version limit is 64.