All articles
30 November 2026
8 min read

Schema changes without downtime: expand and contract

By Tim Rutte, Cloud & Software ArchitectTopicLegacy & Modernization, Backend & Platforms

Two parallel pieces of brushed metal track joined by a switch with a bright blue switch blade, on a light grey surface.

A schema change that takes half a second can stop your application for minutes.

That sounds like a contradiction. It is not, once you know what the database is waiting for.

Most outages during schema changes do not happen because a table is big. They happen because one statement waits for a lock and everything else queues up behind it. Or because code and schema are changed in the same deploy, even though they never switch at the same moment.

There is a well-known answer to both: expand and contract. Expand first, then move over, then tear down. Here is what that looks like in practice, for MySQL and for PostgreSQL.

Why a fast change can still stop everything

Almost every ALTER TABLE needs the table to itself for a brief moment. In MySQL that is the metadata lock, in PostgreSQL the level is called ACCESS EXCLUSIVE.

The moment itself is short. The problem is the wait before it.

If a long transaction is running on the table, a report for example, or a forgotten open window in a database client, the ALTER TABLE waits until it finishes. And while it waits, every new query on that table queues up behind it. Even plain reads.

From the outside this looks like an outage. Connections pile up, the application stops answering. And the actual change has not even started.

One line helps, and I put it in front of every schema change:

-- MySQL
SET SESSION lock_wait_timeout = 5;

-- PostgreSQL
SET lock_timeout = '5s';

If the statement cannot get the lock within five seconds, it gives up. Then you try again. A failed attempt costs nothing. A waiting one costs the application.

Schema and code never switch at the same moment

The second cause of outages has nothing to do with locks.

A deploy does not swap the code in one go. For seconds or minutes the old and the new version run side by side. And if something goes wrong, the old one is back after the rollback.

That gives you the rule the whole approach hangs on: every version of the code has to work with the schema before the change and with the schema after it.

Renaming a column and adapting the code in the same deploy does not meet that. The old code looks for the old column, the new code for the new one, and one of them does not exist right now.

So one change becomes a series of small ones, each harmless on its own.

Renaming a column in six steps

The example: in the customers table, zip should become postcode.

  1. Expand. The new column is added, the old one stays. The running code does not notice.
  2. Write to both. From now on the code writes to both columns and keeps reading from the old one.
  3. Backfill. The existing rows get their value in the new column, in small batches.
  4. Switch reads. The code reads from the new column and keeps writing to both.
  5. Stop writing the old one. The code no longer knows the old column.
  6. Contract. The old column is dropped.

Six steps for a rename looks like overkill. The objection is obvious.

But every one of these steps can be undone on its own, except the last. And between two steps you can wait as long as you like.

The backfill is the step people underestimate most. A single UPDATE over the whole table holds locks on millions of rows and lets the replicas fall behind. Batches along the primary key are better:

UPDATE customers
SET postcode = zip
WHERE id BETWEEN 100001 AND 105000
  AND postcode IS NULL;

Five thousand rows, a short pause, the next batch. Keep an eye on replica lag while you do it. What else tears during backfill and reconciliation is covered in Dual write and backfill: where migrations really tear.

A note for anyone using Doctrine or another migration tool: the automatic diff does not know what you are planning. Depending on tool and version, it turns a rename into a DROP and an ADD, and then the data is gone. Or it generates a RENAME that breaks the old code at once. Read the generated migration before it runs.

Which statement locks what

Not every change needs the whole route. A new column or a new index is only the first step, and in current versions that is usually cheap. Usually.

MySQL 8.0 and later

Adding a column has not required copying the table since 8.0.12. Since 8.0.29 that holds at any position, and for dropping a column too. I still spell out the algorithm:

ALTER TABLE customers
  ADD COLUMN postcode VARCHAR(10) NULL,
  ALGORITHM=INSTANT;

ALTER TABLE customers
  ADD INDEX idx_postcode (postcode),
  ALGORITHM=INPLACE, LOCK=NONE;

Not because MySQL would otherwise get it wrong. But because the statement then fails with an error when the requested route is not possible, instead of quietly copying the whole table.

Changing a column type copies the table in most cases and blocks all writes for as long as that takes. For that there are tools like gh-ost or pt-online-schema-change, which build a shadow table and swap it in at the end. Or you go the route through a new column, as above.

And one more thing: an index that is built without a lock on the primary is built again afterwards on a replica using classic replication. The replica lags for that long. If you send reads there, they return old data in the meantime.

PostgreSQL

Adding a column with a constant default has been a pure metadata change since version 11. Three places still hurt:

-- Index without a write lock, not inside a transaction
CREATE INDEX CONCURRENTLY idx_customers_postcode
  ON customers (postcode);

-- Foreign key in two steps
ALTER TABLE orders
  ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id)
  REFERENCES customers (id) NOT VALID;

ALTER TABLE orders VALIDATE CONSTRAINT fk_customer;

A plain CREATE INDEX blocks writes until the index is built. The CONCURRENTLY variant does not. In return it can fail, and then it leaves an invalid index behind that you have to drop and create again.

A foreign key checks all existing rows when it is created and holds locks on both tables while it does. With NOT VALID it applies to new rows at once. The check of the old rows runs afterwards with a weaker lock.

And SET NOT NULL reads the whole table while it is exclusively locked. Since version 12 you can avoid that: first add a CHECK constraint on IS NOT NULL with NOT VALID, validate it, then run SET NOT NULL. PostgreSQL recognizes the validated constraint and skips the scan.

When the old part goes

Contracting is the only step with no way back. A dropped column only comes back from the backup.

That is why it is also the step that stays undone longest. Many systems carry columns nobody has read for years, because nobody dares to remove them.

I check three things first.

First: does anything still write to it? After step 5 no new value should arrive. A look at the most recent rows after a few days shows whether that is true.

Second: does anything still read it? Searching your own code is quick. The reports, exports and analyses that go straight to the database, past the application, are not. In MySQL the statement statistics in the Performance Schema help, in PostgreSQL pg_stat_statements. More on that in The real legacy is your database schema, in the section about the users you do not know.

Third: is there a copy? Before the DROP I put key and value of the old column into an archive table. That costs one statement and turns an irreversible step into one you can restore from if you have to.

Only then the DROP, again with the lock timeout.

How long to leave between step 5 and step 6, there is no fixed number. At least as long as the rarest regular job needs to come around once. For a month-end close that means longer than a month.

When a maintenance window is the better choice

All of this is effort. Several deploys instead of one, plus a backfill run and a list of open contract steps that someone has to keep.

It does not always pay off.

If your system has no users at night and twenty minutes of downtime cost nobody anything, an announced maintenance window is more honest and cheaper. One change, one deploy, done.

The lock timeout still belongs in front of it. So does the backup.

Expand and contract pays off where there is no window any more: customers in several time zones, interfaces with other people's systems attached, tables whose rebuild takes hours.

What stays

The database rarely stops because of the change itself. It stops because something waits for a lock, or because code and schema do not match.

A timeout helps against the waiting. Never changing two things in the same step helps against the mismatch.

Expanding is cheap. For contracting you have to set yourself a date.