Do it in stages. Add the column as nullable with no default so the change is metadata only, deploy code that writes it on every path while still tolerating nulls, backfill in small batches with pauses so locks and replication stay healthy, verify no nulls remain, then add the not null constraint (validated separately where supported) and remove the fallback code. Never one blocking ALTER on a table that size.
Why interviewers ask this
This is the expand and contract migration, and it separates people who have shipped against big tables from people who have not. Interviewers want the multi deploy sequence, awareness that a naive ALTER takes a lock and rewrites the table, batching to protect replicas, and the discipline to verify before enforcing. It also probes whether you account for rolling deploys where old and new code run at once.
How to structure your answer
- Reject the single blocking migration explicitly.
- Lay out the expand, backfill, contract sequence.
- Explain batching and why it protects replicas.
- Account for old and new code running side by side.
Example answer
The thing I will not do is one ALTER that adds a not null column with a default, because on an older engine that rewrites the whole table under an exclusive lock and the service is down for as long as that takes. So I expand first: add it nullable, which on modern Postgres is a metadata change and effectively instant. Then I deploy code that writes the new column on every insert and update while still tolerating nulls on read, because during a rolling deploy old and new pods are both live and I cannot assume otherwise. Then the backfill, in batches of maybe ten thousand rows keyed by primary key with a short sleep between them, watching replication lag as it runs and slowing down if lag climbs. Once the null count is zero I add the constraint, and in Postgres I would add it as not valid then validate separately to take a weaker lock. The last deploy removes the fallback. Every step is individually reversible.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- How would you roll back once the backfill has started?
- What if the backfill needs application logic per row?
- How would this differ on MySQL?
Related software engineer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions