Skip to content

MySQL online DDL — algorithm and lock strategies that actually run without an outage

How MySQL online DDL really works — ALGORITHM=INSTANT, INPLACE, COPY, the lock matrix, and when to reach for pt-osc or gh-ost instead.

Every operator who has run MySQL in production has been bitten by an ALTER TABLE that promised to be fast and instead held a metadata lock for hours, blocking every connection. Modern MySQL has three DDL algorithms (INSTANT, INPLACE, COPY) and a lock matrix that determines whether the alter is online, blocking writes, or blocking everything. This article is the operator-grade reference: which alter goes which way, what to check before running it, and when to abandon native DDL in favour of pt-online-schema-change or gh-ost.

How to verify

Before running an alter, check what algorithm MySQL would pick and what lock it would take. Use ALTER TABLE ... ALGORITHM=... LOCK=... to force a specific choice; if MySQL cannot honor it, the statement errors immediately instead of locking unexpectedly.

mysql -e "SHOW PROCESSLIST;" | grep -i 'altering\|copy\|waiting for table'
mysql -e "SHOW ENGINE INNODB STATUS\G" | grep -A 5 'LATEST DETECTED DEADLOCK'
mysql -e "SHOW VARIABLES LIKE 'innodb_online_alter_log_max_size';"
mysql -e "SELECT * FROM performance_schema.events_statements_history_long WHERE sql_text LIKE 'ALTER%' LIMIT 10\G"

The way to fail fast is to declare what you expect. ALTER TABLE t ADD COLUMN c INT, ALGORITHM=INSTANT, LOCK=NONE; either runs as INSTANT with no lock or refuses; never silently downgrades to COPY.

What’s happening

MySQL chooses an alter algorithm based on what the change is. The three choices, from fastest to slowest:

  • ALGORITHM=INSTANT (MySQL 8.0+) — metadata-only change. Adding a column at the end of a table, dropping a column (8.0.29+), renaming a column. Microseconds, no rebuild.
  • ALGORITHM=INPLACE — modifies the table in place by writing a new index or restructuring rows without making a full copy. Adding most indexes, changing column nullability, modifying column type within compatible classes. The row image changes; the operation is bounded by the size of the modification, not the size of the table.
  • ALGORITHM=COPY — rebuilds the entire table into a new file, then renames. Required for changes like adding a primary key, converting between InnoDB row formats, or any change MySQL cannot handle in place.

The lock choices:

  • LOCK=NONE — no lock; DML continues during the alter. Available with most INSTANT and many INPLACE operations.
  • LOCK=SHARED — reads allowed, writes blocked. Used when the alter can run alongside reads but DML would corrupt the new structure.
  • LOCK=EXCLUSIVE — nothing else allowed. Always used by COPY (the COPY operation does its own ongoing-DML log replay but holds an exclusive lock at the cutover).

For long-running alters, MySQL writes concurrent DML changes into an innodb_online_alter_log while the operation runs, then replays them at the end under a short metadata lock. If the log grows past innodb_online_alter_log_max_size (default 128 MiB), the alter aborts. Sized correctly for the table and the concurrent write rate, the cutover is sub-second.

When native DDL is not enough — the alter requires ALGORITHM=COPY and the table is too large, or the cutover lock is too long — the answer is pt-online-schema-change or gh-ost. Both work by creating a shadow table, copying data in chunks, and swapping at the end. pt-osc uses triggers to keep the shadow in sync; gh-ost reads the binlog. gh-ost is the modern choice on busy tables because it does not add trigger overhead.

The decision

  1. Identify what the alter is. Adding a nullable column at the end? ALGORITHM=INSTANT. Adding a secondary index? ALGORITHM=INPLACE, LOCK=NONE. Changing a column type incompatibly? ALGORITHM=COPY. Adding or dropping the primary key? ALGORITHM=COPY.

  2. Force the algorithm explicitly. This is the single most important habit.

    ALTER TABLE orders
      ADD COLUMN email VARCHAR(255) NULL,
      ALGORITHM=INSTANT, LOCK=NONE;

    If MySQL would have to fall back to COPY, the statement errors with ERROR 1846: ALGORITHM=INSTANT is not supported. Reason: .... You decide whether to accept the downgrade or use a different tool.

  3. For INPLACE alters that may take time, size the online alter log. A multi-hour ADD INDEX on a busy table can fill the default 128 MiB and abort. Either raise it for the session, or run during a quieter window.

    SET SESSION innodb_online_alter_log_max_size = 1073741824; -- 1 GiB
    ALTER TABLE orders ADD INDEX idx_status_created (status, created_at), ALGORITHM=INPLACE, LOCK=NONE;
  4. For COPY alters on a large table, use gh-ost instead of native DDL.

    gh-ost \
      --host=10.0.0.11 --user=osc --password=OSC_PASS \
      --database=app --table=orders \
      --alter="ADD COLUMN region VARCHAR(50) NOT NULL DEFAULT ''" \
      --chunk-size=2000 --max-load='Threads_running=25' \
      --critical-load='Threads_running=200' \
      --allow-on-master --execute

    gh-ost connects to the primary (or to a replica, which is the safer default), reads the binlog, throttles based on replica lag, and offers a --pause interactive control if metrics misbehave. The cutover at the end is a single rename.

  5. Verify after the alter. The table structure changed; statistics may not have. ANALYZE TABLE updates them; otherwise the optimizer may choose poor plans for a few minutes.

    ANALYZE TABLE orders;
    SHOW INDEXES FROM orders;

Common pitfalls

  • Not forcing the algorithm — MySQL silently falls back to COPY if INSTANT is not applicable, and you discover at minute thirty that the table is being rebuilt and writes are blocked. Always declare ALGORITHM= explicitly.
  • INSTANT after a series of INSTANT alters — every INSTANT ALTER leaves a small metadata trail; after enough of them on the same table, MySQL refuses further INSTANT operations and requires an OPTIMIZE TABLE (which is COPY) to reset.
  • pt-osc on a table with existing triggers — the older toolkit refuses; the newer one rewrites the triggers and can hit edge cases. gh-ost is the safer choice for triggered tables.
  • gh-ost --allow-on-master — runs against the primary, putting binlog reads where they cost the most. Default to gh-ost against a replica unless you specifically need primary-side execution.
  • No throttling on long alters — gh-ost and pt-osc both accept --max-load and --chunk-time; without them, the alter saturates the disk during peak. Always set throttling thresholds.

Online DDL is what turns an ALTER TABLE from a four-hour outage into a managed background operation. In the engagements we run, every schema change goes through review with the planned algorithm declared, large tables get gh-ost instead of native DDL, and the post-alter stats refresh is in the runbook so the optimizer is not flying blind. For the broader pattern of operating MySQL with this care, see /en/services/managed-operations/.