I wrote about the Postgres side of this in September, in One long SELECT, one ALTER TABLE, and every query behind them. This one adds MySQL 8.4 and measures what a lock timeout with retries costs the other clients.

Adding a nullable column is one of the cheapest schema changes there is. In Postgres 18 it only touches the catalog, and in MySQL 8.4 you can ask for ALGORITHM=INSTANT. On an idle table both took a few milliseconds in my tests. What I wanted to know is what happens to everyone else when the table isn't idle, specifically when some other session has an open transaction that read from it and hasn't committed yet.

The setup

Postgres 18.6 and MySQL 8.4.13 in two throwaway Docker containers on a Mac laptop. Each had one table, items, with 200,000 rows. Three clients ran for 32 seconds:

  • a writer doing single-row INSERTs in autocommit, about 80 a second
  • a reader doing primary-key SELECTs, at the same rate
  • a session that opened a transaction at 3 s, ran SELECT count(*) FROM items, then sat idle until it committed at 23 s

At 6 s a fourth session ran the migration:

-- Postgres ALTER TABLE items ADD COLUMN note text; -- MySQL ALTER TABLE items ADD COLUMN note text, ALGORITHM=INSTANT; 

Every query's start time and duration was logged. I ran each case three times per engine. The durations below varied by 30 ms at most between runs, so I'm quoting run one.

What the clients saw

Case Engine ALTER took Longest INSERT Longest SELECT INSERTs started between 6 s and 23 s
no open transaction Postgres 1 ms 0.01 s 0.01 s 1,376
no open transaction MySQL 4 ms 0.01 s 0.01 s 1,347
open transaction Postgres 17.0 s 17.0 s 17.0 s 1
open transaction MySQL 17.0 s 17.0 s 17.0 s 1

With the open transaction, the ALTER waited the full 17 seconds until the other session committed. That part I expected. What matters is that every INSERT and every SELECT that arrived after the ALTER waited behind it, for those same 17 seconds. One insert started in that window, and it was the one that hung. The plain primary-key read, which has nothing to do with the new column, hung too.

Neither engine returned an error to the clients. They just stopped.

The reason is the same in both. The ALTER needs an exclusive lock on the table, which in MySQL is the metadata lock. It can't get one while the idle transaction still holds its shared lock from the count(*). And once an exclusive request is waiting, new requests for the shared lock queue behind it instead of slipping past. So one idle session plus one cheap migration takes the whole table offline, and the cost of the migration itself has nothing to do with it.

What it looks like while it's happening

I took a snapshot of each engine's session list a moment into the stall. In Postgres, from pg_stat_activity with pg_blocking_pids():

pid state wait_event query blocked by 148 active relation INSERT INTO items (v, payload) VALUES … {151} 149 active relation SELECT payload FROM items WHERE id = … {151} 150 idle in transaction ClientRead SELECT count(*) FROM items {} 151 active relation ALTER TABLE items ADD COLUMN note text {150} 

The INSERT and the SELECT both name the ALTER as the thing blocking them, not the idle transaction. If you only follow the first hop, the migration looks like the culprit. The session actually holding things up is listed as idle, and its query column shows the last thing it ran, which can be minutes old in a real app.

MySQL's processlist is less helpful:

id command state info 51 Query Waiting for table metadata lock ALTER TABLE items ADD COLUMN note text, ALGORITHM=INSTANT 52 Query Waiting for table metadata lock SELECT payload FROM items WHERE id = 126667 53 Sleep NULL 54 Query Waiting for table metadata lock INSERT INTO items (v, payload) VALUES (1, 'x') 

The session holding the lock shows up as Sleep with no query at all. Nothing on that line says it's the one with the open transaction. You need performance_schema.metadata_locks or sys.schema_table_lock_waits to connect it.

A lock timeout on the migration

The usual advice is to give the migration a short lock timeout and retry. I set lock_timeout = '2s' in Postgres and lock_wait_timeout = 2 in MySQL on the migration's session, then retried every 3 seconds until it worked.

Engine Attempts Error on each failed attempt Longest INSERT Stalls over 0.5 s INSERTs started between 6 s and 23 s
Postgres 4 canceling statement due to lock timeout 2.0 s 4 734
MySQL 4 1205 Lock wait timeout exceeded; try restarting transaction 2.0 s 4 711

It works in the sense that no request waited longer than 2 seconds. But each attempt froze the table for its full 2 seconds before giving up, and there were four of them, at 6, 11, 16 and 21 seconds. The clients spent 8 of those 17 seconds stuck, and the writer got through about half as many inserts as it did with no migration running. The fourth attempt went through when the idle transaction committed at 23 s.

What I got wrong

I treated the lock timeout as the fix. It turns one 17 second outage into four 2 second ones. Whether that's better depends on what your clients' own timeouts are, and with a 3 second retry the table was blocked almost half the time until the real problem went away. A shorter timeout, or a longer gap between retries, would trade those two against each other, but I only tried one setting.

My clients had no query timeouts of their own, so each stall shows up as one slow call. In an app with a 5 second request timeout, the no-timeout case would have been a pile of errors, not one slow insert.

And I'd have read the Postgres snapshot wrong if I'd stopped at the first column. The obvious reading is "the ALTER is blocking everything". It is, but only because something else is blocking it.

Caveats

One table, one laptop, Docker, two clients plus the idle session. I didn't test ADD COLUMN with a volatile default, index builds, CREATE INDEX CONCURRENTLY, or what happens when the open transaction belongs to a connection pooler. In every run the idle transaction committed on its own at 23 s. In real life it might be a forgotten psql session that never does.

What I'd take from it

  • Before a migration, look for sessions that are idle in transaction (Postgres) or have open transactions in information_schema.innodb_trx (MySQL). The cheapest ALTER is only cheap if nothing is holding the table.
  • Set a lock timeout on the migration anyway, but count on it costing one full timeout of blocked traffic per attempt.
  • Postgres has idle_in_transaction_session_timeout for the other side of the problem. It ends the session that's holding things up instead of the migration.