r/SQL • u/Harshita_Netla • Sep 16 '26
Discussion How do you handle schema migrations on a live production database without downtime?
For example, if you need to add or change a column, index, or constraint, how do you make the change without interrupting users?
Do you follow a specific migration strategy or deployment process
27
Upvotes
35
u/Winsaucerer docs.spawn.dev Sep 16 '26
One thing I do is make sure that I never use
SELECT *in any queries, so that adding a column does not cause a new column to suddenly appear in queries and break code/assumptions. Basically, I try to write migrations in such a way that I can apply them first before updating applications, without them breaking any existing deployed software. Then update the software after.But for my main project(s), changes like adding columns, indexes, or constraints generally don't cause downtime. Sometimes I test changes against a replica of prod to see what impact on performance there might be.
As for migrations themselves, shameless plug for my own migration tool I've been dogfooding for quite a few months now, spawn (for postgres).
19
u/Blecki Sep 16 '26
99.99...9% of the time you just apply the change. The table locks for a few seconds maybe. Not a big deal.
If you actually happen to be someone like Meta or Google, you have many thousand redundant machines and a software layer that hides all this complexity from the end user, and you have an intern update each one and the abstraction layer figures it out.
If you're an ordinary company you have an outage.
But if you really, really, really can't have an outage - you got some options. Adding a column is basically instant but might break client code, so you have a testing environment right? To synch changes without downtime you have to ship a version that doesn't break when the unexpected new column is present -> then add it -> then ship the version that uses it.
Adding an index could take longer. Strategy depends on your data consistency needs. A non clustered index can be added without locking reads. Stalling updates for a bit might be just fine. Or you might have to duplicate tables, add the index, resynch the data, switch the tables out.
Adding a constraint shouldn't be a big deal unless the current data violates it. Then you fix the data first, no downtime needed to add the constraints. But also I avoid enforcing them in sql.
5
u/Old-Biscotti3842 Sep 16 '26
Nobody's mentioned the two-step, which is the bit that makes most of this safe. Postgres only, sorry, SQL Server people have a different set of problems here.
Adding a FK or a CHECK normally scans the whole table while holding a lock that blocks writes. Add it NOT VALID instead and there's no scan, new rows are enforced straight away, and then VALIDATE CONSTRAINT as a separate statement does the scan under SHARE UPDATE EXCLUSIVE, so reads and writes carry on. NOT NULL on a big table works the same way: CHECK (col IS NOT NULL) NOT VALID, validate that, then SET NOT NULL, which won't rescan because the constraint already proved it. 12 and up for that last part. On 18 you can mark the NOT NULL itself NOT VALID and skip the CHECK entirely.
What gets people is the migration tool undoing all of it. Rails, Django and most of the others wrap each migration in a transaction, and NOT VALID plus VALIDATE inside one transaction is just the blocking version with extra typing, the lock is held till commit either way. Same reason CREATE INDEX CONCURRENTLY blows up in there, as u/elevarq said. One flag each, disable_ddl_transaction! in Rails and atomic = False in Django, then keep the two steps in separate migrations.
u/Hour-Measurement-835's point about the queue is the other half of this, low lock_timeout and retry. Though not everything needs the treatment: adding a column with a default stopped rewriting the table back in 11, so that one really is instant.
4
u/phunkmunkie Sep 16 '26
Depends on the size of the table and how busy the table is. For big tables that aren’t writing like crazy, you could create a new table with all the things you need, and then pull a switcheroo (migrate most data safely, then lock the table move the rest, and rename the tables). Depending on table usage you could rename (seed any identity properly) and move the data later. The interruption can be sub second… no timeouts or anything.
But it depends on how the table is used.
3
u/elevarq Sep 16 '26
Depends on the type of change. Always check the type of lock a change needs, see if you could use a different lock in case it would be too aggressive, etc.
And “concurrently” is your best friend in PostgreSQL, but you have to prepare your changes for this: It can’t be used inside a transaction like (nearly) all other changes.
2
u/Thiondar ORACLE Sep 16 '26
If downtime is a real issue, then hopefully you’re using one of the major databases.
With Oracle, you use
dbms_redefinition. Practically everything can be done online there.DB2 is said to come close with
sysproc.admin_move_table; SQL Server isn't quite as straightforward, though a fair amount can apparently be done online.1
u/antiexe Sep 16 '26
This depends a lot on what the change is and what DB server is being used.
Some servers support some DDL operations as instant changes, for example, adding a new column that is null, probably instant most engines.
There are also tools that do "online schema changes" such as percona toolkit online schema change, pt-osc and GitHub's online schema migration, gh-ost. These tools basically make a copy of the existing table, alter it, then copy the data in batches over to it while keeping up with new data and changes as it runs.
1
u/gogod1x Sep 21 '26
u/Old-Biscotti3842 already has the two-step DDL covered. The half that took us down was the existing rows: the DDL is metadata-only, the data is not.
The order I keep coming back to:
Add the column nullable, no default:
alter table orders add column region text;
Metadata-only from 11 on, so there is nothing to wait on.
Backfill in separately committed batches rather than one UPDATE over the whole table:
update orders set region = 'EU' where id in (select id from orders where region is null limit 5000);
Loop until it returns 0, short pause between batches. Each batch is its own transaction, autovacuum keeps up, and a restart resumes where it stopped. One UPDATE over 40M rows is a single long transaction: it holds a snapshot for its whole life and leaves you a table full of dead tuples when it finishes.
The rule of thumb that has held up: every step that takes an exclusive lock stays metadata-only; anything that scans or rewrites rows belongs in a step that does not block writes.
3
u/[deleted] Sep 16 '26
[removed] — view removed comment