INFINITE UPSIDE

Technology and operations

How Can You Change a Database Without Breaking the App?

The new column may be ready before every old application instance has stopped using the old one.

A team renames a database column and deploys the matching application code. The new version works in a test environment. During the production rollout, half the application instances still expect the old name. They start failing immediately. The database did exactly what it was told; the rollout assumed all its clients would change at once.

A safer schema migration plans for a period when old and new application versions coexist. One common approach adds the new structure, changes readers and writers in stages, verifies the data, and removes the old structure only after every dependent client has moved. It is a design pattern, not a guarantee of a zero-downtime change. The exact database, deployment sequence, locking behavior and data semantics determine the risk.

That coexistence period is where much of the work lives. The new application may be running, but a background worker, an export job or a rollback version can still depend on the old schema. “The website loaded” is a useful first observation, not a complete inventory of the systems sharing the database.

Draw the compatibility table first

Before making a change, list the clients that read and write the affected data. Include scheduled jobs, reporting tools, data pipelines, mobile apps and integrations where applicable—not just the server receiving web traffic. Note whether each can be updated at the same time and whether an old version might return during a rollback.

Then write down which combinations must work. Can the old application run against the expanded schema? Can the new application run while some rows still have only the old representation? What happens if the new application is rolled back after it has written data in the new format? A compatibility table turns these into questions that can be tested before the release window.

Consider a fictional order system with one customer-name field. A new feature needs separate given and family names. Adding fields may be straightforward, but converting a single string into two accurate components is not. Some customers have one name, multiple family names or formatting that does not fit the proposed split. A migration plan must define what happens to ambiguous values; a database operation cannot infer a person’s preferred name with certainty.

Expand before you contract

In the first stage, add the new structure in a way the existing application can tolerate. For example, new nullable columns can coexist with the original column while the application learns to use them. In the final stage, remove the old field only after its users have moved and the team has agreed that the remaining data and rollback requirements permit removal.

Between those stages, the system needs rules for both reads and writes. A new reader might prefer the new fields when populated and fall back to the old representation otherwise. A writer might update both representations during a transition. These are examples, not interchangeable recipes: fallback rules can hide incomplete data, and writing two representations creates a consistency obligation.

The PostgreSQL guide to modifying tables documents operations such as adding columns and constraints. Whether a particular command blocks traffic or rewrites data depends on the operation and database version. Review the specific documentation and test on representative data; “adding a column” is not a universal promise that the operation is instantaneous.

Decide which value is authoritative

Dual writes sound reassuring until the two writes disagree. Suppose the new application writes both a combined display name and two structured fields. An older worker then updates only the combined field. Which value should a later reader trust? If the application simply prefers the structured fields forever, it may show stale information even though the old worker successfully saved a change.

Choose a source of truth for each stage and document how writes from every active version are handled. The answer may involve updating or retiring old writers before enabling a new field, or using a database-side mechanism reviewed for that environment. Whatever the design, test failures between steps. A transaction can make certain database writes atomic, but it does not automatically make an external API call or a separate queue operation atomic with those writes.

For an order status, the meaning matters as much as the column name. If “pending” in the old system covers two new states, a one-way conversion may lose information when the code is rolled back. The team needs an explicit mapping and a decision about values that do not map cleanly. A reversible deployment is not the same thing as a reversible data transformation.

Backfill with a way to stop and resume

A backfill populates the new representation for existing rows. On a small test table it may finish in seconds. On a large, busy database, one enormous transaction can compete with normal traffic or make recovery awkward. A bounded, resumable process can reduce operational risk, but the appropriate batch size, transaction scope and rate depend on actual measurements and database behavior.

Record where the process is, how it identifies eligible rows, what happens if it repeats work and how errors are isolated. If a row changes while the backfill is running, the migration must avoid replacing a newer value with an older one. A checkpoint is useful only if restarting from it does not skip rows or duplicate an operation that cannot safely be repeated. The separate guide to idempotency discusses the retry problem in another setting.

Measure more than the number of rows visited. Count rows successfully converted, rows skipped for a stated reason and rows that need human review. Test the conversion on awkward cases before processing the entire dataset. For the name example, a queue of ambiguous records is more honest than a dashboard claiming complete conversion after guesses were written into every row.

A useful rehearsal includes a live-write scenario, not just a copied table at rest. Take one row that the backfill has not reached, update it through an older application version, then let the backfill reach it. Does the new representation reflect the latest intended value? Repeat with an update after the backfill has passed. Check the answer through both application versions and through any reporting job that reads the affected field. These tests will not prove every race is absent, but they can expose a transition rule that works only when nobody is using the system.

Constraints are useful—and can affect a busy table

Once the application expects the new representation, a constraint can help protect it. But adding a rule to a table with existing rows is different from enforcing it only for new changes. For certain PostgreSQL check and foreign-key constraints, NOT VALID lets a team defer the initial scan of existing rows; subsequent inserts or updates are still checked. A later validation checks existing data. The PostgreSQL ALTER TABLE reference describes supported forms and the locks involved.

Do not read that feature as “no lock” or assume it applies to every type of constraint. PostgreSQL documents different lock requirements for different operations, and validation has its own locking behavior. A long-running transaction elsewhere can also change how a release behaves. Rehearse the exact commands on a representative environment and define a timeout or abort decision appropriate to the service.

Validation failure is information. It may reveal rows the backfill missed or assumptions the application did not actually enforce. Investigate the violations, fix the cause and rerun the appropriate check. Turning off an error without understanding it can leave a permanent gap between what the code assumes and what the database contains.

Rollback the application; account for the data

Blue-green and canary deployments control which application version receives traffic. They do not automatically restore the database to its previous contents. If the new version writes a field the old version ignores, rolling back code may be easy but leave new data unused. If the new version stops writing a field the old version requires, the old code may fail or display stale values. The deployment comparison explains traffic switching; the schema plan must be compatible with it.

Before cutover, specify the rollback boundary: which application versions can use the current schema, which writes are safe to reverse, and what data would need reconciliation. Keep a recovery plan for destructive steps, including who can execute it and how long it would take. A backup is important, but restoring an entire database can discard legitimate changes made after that backup. Recovery choices are operational and business decisions, not just commands in a deployment script.

Only contract the schema after checking actual clients and data. Logs and dependency inventories can support that decision, although an infrequently run report may not appear in a short observation window. Agree on an observation period appropriate to the system, retain relevant evidence and make the removal a separate planned change. When the old field finally disappears, it should be the end of a migration the team can explain—not a surprise discovered by the payroll report next month.

Explore Infinite Upside

For Vijay Sheth’s personal accounts, read Infinite Upside: Building Wealth With Room to Live, published by Sheth Capital, Inc.

Find your edition · Chapter companion

ABOUT THE CONTRIBUTOR

Luna Lim

DIRECTOR OF FINANCIAL RESEARCH & EDITORIAL STRATEGY

SHETH CAPITAL, INC.

Luna Lim writes about business, investing, and the judgment behind financial decisions. She joined PCH Exotics Inc. as Director of Operations in 2022, working across La Jolla and Miami. Today, she reports directly to Vijay Sheth at Sheth Capital, Inc., bringing an operator’s perspective to the ideas she covers.

Based in Pacific Beach, California, Luna is curious about the people behind the numbers: what they’re building, how they weigh uncertainty, and what makes a risk worth taking. Her articles for Infinite Upside follow those questions through the details, with clear explanations and a feel for how decisions play out in everyday business.

More from Luna Lim

The Infinite Upside store