Relational databases
Live schema changes and backfills
A live schema change moves a database and its callers through a period when different versions must work together. Adding a column is only one step. Existing rows need values, callers still running an older release need their fields, and writes arriving during the copy must leave the new representation correct.
The shop will replace products.price, a decimal price, with price_hundredths, an integer holding the same amount in hundredths. P7’s 20.00 becomes 2000. This changes the representation, not its currency or the price agreed on an order line. Order 103 remains worth 29.00.
We will follow the change through expand, migrate and contract: introduce a compatible form, move data and callers to it, then retire the old form. The difficult part is establishing when each move is safe. A completed backfill process cannot answer that on its own.
Expand, migrate and contract
A direct replacement couples the database deployment to every caller. Rename price immediately and an older query naming it fails. Change its meaning to hundredths under the same name and an older reader can display 2000 as though it meant 2000.00. A rolling deployment, which replaces application instances gradually, makes that overlap normal.
Instead, add the new column while keeping the old one. Initially allow its value to be missing: older inserts know nothing about it, and old rows have not been converted. Then introduce a bridge caller: it reads the old price but writes both forms together. Once those writers are in place, a backfill supplies the new form for existing rows.
For P7, the transition must keep the old readable 20.00 while introducing its equivalent 2000 hundredths. The read field and the fields a caller writes need not change at the same deployment step.
Figure as text
Start with price. Add nullable price_hundredths, deploy bridge writers that write both atomically and drain every old writer. Backfill and reconcile while reads use price. Switch reads while both forms stay maintained. Retire old dependencies and return targets before removing price.
During the backfill, some rows have both forms and others have only the old one. Keeping reads on price makes that an accepted intermediate state. Switching readers is a later decision, after all required rows have correct new values. Removing the old field is later again, after no remaining caller or promised return path needs it.
The conversion itself needs a declared domain. Here a finite, non-negative numeric(8,2) price has at most two decimal places, so multiplying by 100 is exact and its maximum, 99,999,999 hundredths, fits a PostgreSQL integer. Keep that original range while returning to the decimal form remains possible. Validate input before conversion; silently rounding an extra fraction would be a different policy.
Check stored exceptions too. PostgreSQL permits NaN in numeric, and the existing price >= 0 check does not exclude it. Audit for it and resolve any such rows under an agreed business rule before deploying this conversion. Giving an undefined price the value zero would invent a price.
Compatibility during a rolling deployment
A release name is too coarse a description of compatibility. Ask which fields a caller reads, which it writes, and what it assumes about existing values. The bridge caller can coexist with an old-only caller while both read price. It cannot establish that the new field stays current while the old-only caller can still change the source.
| Caller | Reads | Writes | When accepted |
|---|---|---|---|
| Old-only | price | price | Before the old write path closes. |
| Bridge | price | Both, atomically | After expansion; throughout backfill and the return window. |
| New reader | Hundredths | Both, atomically | After convergence; preserves the return window. |
| New-only | Hundredths | Hundredths | After old dependencies retire and old-column requirements are prepared. |
“Both, atomically” means one transaction accepts or rejects the pair. Two independent writes leave a gap, and a crash between them can leave the forms disagreeing. For a price change, a bridge caller can use one statement:
UPDATE products
SET price = 22.00,
price_hundredths = 2200
WHERE product_id = 'P7';Both constants must come from the same validated amount. The insert path must also initialise both fields. A database trigger or another enforced conversion mechanism can implement a different bridge, but its complete write behaviour needs the same proof. The protocol here uses application callers and drains the old-only writers.
Close the old write path
Suppose the backfill has already put 2000 beside P7’s 20.00. One surviving old-only worker can change price to 22.00 and leave 2000 untouched. The new field is non-null and wrong. A backfill that visits only missing fields will never repair it.
Before starting this backfill, establish that every writer follows the bridge rule. Include background workers, imports, scheduled tasks, administrative tools and in-flight transactions, as well as web instances. Stop new old-only work, drain the old work already admitted, and prevent an automatic restart or rollback from bringing that write path back. “Every web instance is on the new release” establishes only part of this boundary.
Until it is closed, new reads remain disabled. Afterwards, new inserts arrive with both forms and price updates preserve their agreement. That write rule is what allows the copy to make lasting progress.
Backfill without overwriting a newer value
A backfill worker reads P7 at 20.00 and calculates 2000. Before it writes, a bridge caller raises the price to 22.00 and commits 2200 beside it. The worker’s calculation is still correct for its earlier read. Storing it now would be wrong.
Compare a backfill that writes its earlier calculation unconditionally with one that checks the source or calculates from the row it actually changes. The difference is whether its accepted value still describes the current row.
Figure as text
A worker reads 20.00 and remembers 2000. A bridge writer commits 22.00 and 2200. An unguarded remembered assignment overwrites 2200 with 2000. Guarding on price 20.00 affects zero rows and requires a reread. Converting the current protected value leaves 2200 correct.
Assigning 2000 repeatedly has the same effect as assigning it once. That makes the assignment repeatable, but says nothing about whether 2000 still represents the current price. A safe backfill must connect the source it transforms to the row it changes.
One way is to calculate from the row inside the protected database write. The following PostgreSQL statement repairs missing or unequal values for one product without using an earlier application copy:
UPDATE products
SET price_hundredths = (price * 100)::integer
WHERE product_id = 'P7'
AND price_hundredths IS DISTINCT FROM (price * 100)::integer
RETURNING product_id, price, price_hundredths;Another way, useful when transformation happens outside the database, is a conditional write. The worker that read 20.00 attempts its 2000 only while the source still equals 20.00:
UPDATE products
SET price_hundredths = 2000
WHERE product_id = 'P7'
AND price = 20.00
AND price_hundredths IS DISTINCT FROM 2000
RETURNING product_id, price, price_hundredths;After the bridge commit, this affects zero rows and leaves 2200 intact. Re-read to distinguish an already-correct row from a changed or deleted one; retry changed work from its current source. Do not mark an unexamined zero-row result as completed. Equality is sufficient for this deterministic price conversion: if the source changes away and back, 2000 is correct again. A transformation whose meaning depends on a particular revision needs a revision guard instead.
In PostgreSQL Read Committed, an update encountering a concurrently updated target can wait, then recheck its condition against the committed replacement. Other isolation levels can reject a conflict and require a transaction retry. These examples specify a committed ordering rather than simulate those waits. Transactions and isolation develops the concurrent rules.
Commit and resume in bounded batches
On a large table, choose a bounded group of keys, transform them safely, commit, and record how to resume. A cursor is a saved key after which the next group begins. Its ordering must be explicit and stable. It records committed coverage of that scan, not the correctness of every value in the table.
Commit changes and their checkpoint together where possible. If the checkpoint is saved separately, save it only after the batch commits: a crash in between may replay committed work, which the current-state transformation can safely recheck. Advancing it before commit can skip work that rolled back. Retain conflicted, rejected or deliberately skipped keys for reconciliation; a cursor must not make them disappear.
Smaller transactions limit each batch’s lock duration and recovery work, while more commits add overhead. They also expose partial migration progress. Here old readers tolerate that because their source stays complete. Updates, upserts and bulk imports develops batch identity and progress records outside this live compatibility protocol.
Rows arriving behind the cursor
Even an exact record of committed visits can miss a late row. Product IDs are text. For this scan, choose a text ordering in which P11 precedes P3. A cursor that has passed P3 will not discover a newly inserted P11 by continuing forwards.
Suppose P11, a tea strainer at 8.00, arrives after P3’s batch commits. A bridge insert supplies both 8.00 and 800; a prohibited old-only insert supplies only 8.00. Neither will be found by simply continuing after P3.
Figure as text
The committed cursor is P3. A late P11 sorts before it in text order, so the remaining scan of P7 and P9 never visits P11. A bridge insert already fills 800; an old-only insert leaves NULL. Whole-population reconciliation finds the missing value after the scan ends.
The bridge insert makes P11 ready at birth: 8.00 and 800 commit together. The backfill does not need to visit every row ever inserted; it needs every required row to reach the accepted relation. The old-only variant leaves a missing value behind the cursor, proving why the write boundary was necessary.
A fresh reconciliation must cover the entire required population, including late rows and retained exceptions. A maximum cursor or a job marked “finished” describes the worker’s progress. Neither establishes what now exists outside its visited set.
Prove that the migration has converged
Convergence means the data has reached the intended relation and ongoing writes keep it there. For this migration every product must have a new value, and that value must equal its source price multiplied by 100. Test those claims separately:
SELECT
count(*) FILTER (
WHERE price_hundredths IS NULL
) AS missing,
count(*) FILTER (
WHERE price_hundredths IS DISTINCT FROM (price * 100)::integer
) AS different
FROM products;The accepted result is missing = 0 and different = 0. The second count includes missing values: IS DISTINCT FROM treats a null and a non-null result as different. An ordinary <> comparison would not count that missing case.
The stale pair 22.00/2000 has zero missing values and one difference. An old-only P11 arrival has one of each. Equal row counts would miss the bad value, and checking only non-nullness would miss the stale 2000. Inspect the failing keys and their sources, repair with the safe transformation, and rerun the checks.
The query sees one database snapshot. It establishes the relation at that observation; the bridge rule makes the result useful beyond it. Once every row agrees, any permitted insert creates an agreeing row, any price update changes both forms together, and deletion removes the row. The guarded backfill cannot introduce a stale pair. If a writer can violate that rule, repeated zero counts cannot establish lasting convergence.
For a larger migration, track work remaining and exceptions as well as worker progress. A migration that keeps retrying a hot key or omits an unsupported value has not converged. Define its required population, transformation and acceptance query before running it, so “complete” has a meaning independent of the job’s exit status.
Switch readers and preserve a return path
Enable new reads after the semantic checks pass and every database object those reads require is usable. Verify that the caller understands the new unit: 2200 hundredths must produce the same displayed 22.00. Compare equivalent application results as well as stored fields. Retain the bridge write rule while old readers remain an accepted return target.
This return window permits switching reads back to price. It does not permit restoring an old-only writer. That would reopen the race even if both fields were correct at the moment of rollback. Keep a known bridge release as the return target, with its schema requirements and write behaviour recorded.
Pause is a different decision. Before acceptance, a backfill can stop between committed batches while bridge callers continue serving old reads. Preserve its checkpoint and exceptions. After new reads begin, pausing optional migration work does not remove the obligation to keep both forms current. In either state, confirm what stopped: the launcher, a running batch, a database statement and an engine-managed schema job are separate things.
Run the change within the available headroom
Application compatibility does not establish that the physical operation fits. Adding or validating objects can wait for locks; copying rows consumes processor time, storage work, log capacity and space. The old and new forms may coexist alongside temporary work and retained versions. Budget the peak, including foreground writes and cleanup, rather than only the final size.
Choose batch and wait limits from the service’s available capacity. Observe useful request completions, errors and latency alongside migration rate, waits, free space and log or replica backlog. Define when to reduce or stop new batches. A higher backfill rate that makes purchases fail is not useful progress. After pausing, confirm the current operation’s terminal state and whether its transaction still holds resources.
Diagnosing a database incident connects those symptoms to a bounded intervention. The native mechanisms differ: PostgreSQL schema changes under load covers lock modes, rewrites and index validity; CockroachDB schema jobs under load covers job state and cleanup. Cancelling a CockroachDB statement can leave its schema job running, so a missing waiting client is especially poor evidence that the work stopped.
Contract the old form
Contracting removes the compatibility you deliberately added. First retire every old read and dependency: application instances, workers, reports, views, exports and supported rollback releases. Confirm the new callers can insert and update under the schema that will exist during each remaining step.
The old price column is still NOT NULL. Stopping its writes while it remains required would break new inserts. Either keep supplying it until the drop, or first change that requirement in a compatible schema step. Retire any cross-form checks or conversion triggers that would also prevent new-only writes. Preserve the new form’s non-negative, required-value and range rules as appropriate to the accepted contract.
The return window ends when price stops being maintained, even if the column has not yet been dropped. A later return needs its data reconciled and its callers made compatible again. After removal, it needs a new expansion and migration. Deploying an older binary cannot reconstruct the missing representation.
Constraints, indexes and replacement tables
The same distinction between future behaviour and existing state applies beyond columns. Enforcing a constraint on future writes does not prove that existing rows satisfy it. Check and repair that population, then accept the engine’s completed validation before relying on the stronger rule. Whether a particular engine separates those steps is part of its native procedure.
For an index replacement, build the candidate and establish that it is valid and usable before enabling a caller that depends on it. Check representative results and query work. Retain the old index until the supported workload and dependencies no longer need it, including any uniqueness or reference role. An index name in the catalogue does not establish readiness: PostgreSQL can leave an invalid index after a failed concurrent build.
A replacement table adds a larger copy boundary. Copying the initial rows leaves inserts, updates and deletes made during the copy to reconcile. The change-capture or dual-write protocol must order those changes against the copy, so that an older copied value cannot replace a newer one.
Switch routing only at a catch-up boundary where the data is equivalent and the constraints and references are complete. If you promise a way back, keep the old table current too. Table partitioning and data lifecycle supplies a reason to consider such a move; the live copy still needs this compatibility and acceptance protocol.
The SQL examples were checked in PGlite 0.5.8, a WebAssembly build of PostgreSQL 18.3. The concurrent schedules are illustrative. They establish no measured server concurrency, migration duration or production headroom.