Sub topic 17
Planning Safe, Reversible Schema Changes
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, can create tables, and understand primary and foreign keys. This chapter focuses on changing a live schema without losing data or unexpectedly blocking application traffic.
A schema change is safe when you understand its effect on existing rows, dependent objects, running queries, and application code. A reversible change has a tested way to restore the previous state. Reversibility is not always possible after dropping data, so plan the recovery path before running destructive DDL.
Use an expand-and-contract plan
For changes that affect application code, use separate stages:
- Expand the schema by adding a compatible column, table, or constraint.
- Deploy application code that can work with both the old and new schema.
- Copy or backfill existing data in small batches.
- Verify the new data and switch reads and writes to the new structure.
- Contract the schema by removing the old column or object later.
For example, adding a nullable column usually allows an older application to continue working. Adding a required column without a default may fail because existing rows have no value.
BEGIN;
ALTER TABLE customers
ADD COLUMN full_name VARCHAR(200);
UPDATE customers
SET full_name = name
WHERE full_name IS NULL;
COMMIT;This example is suitable only when the update finishes within an acceptable time and the database supports transactional DDL. On a large table, run the backfill separately in batches instead of holding one long transaction.
Check dependencies and locking
Before changing or dropping an object, inspect:
- Foreign keys that reference the table.
- Views, triggers, procedures, and indexes that use the object.
- Application queries and reports that read the affected column.
- Long-running transactions that may block or be blocked by the DDL.
- The amount of data that must be scanned, copied, or rewritten.
Some type changes require a table rewrite or an exclusive lock. During that period, reads or writes may wait. Test the migration with production-like data, measure its duration, and schedule it when traffic is low if the database cannot perform the operation online.
The following decision path summarizes a cautious migration:
Make the migration observable
Record the migration version, start and end time, affected row count, and verification results. Add checks such as:
SELECT COUNT(*) AS missing_full_names
FROM customers
WHERE full_name IS NULL;Do not treat a successful DDL response as proof that the migration is correct. Check constraints, representative queries, application errors, and performance after the change.
| Change | Main risk | Safer approach |
|---|---|---|
| Add nullable column | Inconsistent new data | Backfill, validate, then enforce rules |
| Change type | Conversion failure or table rewrite | Test conversions and inspect locking |
| Drop column | Permanent data loss | Back up, stop readers, remove in a later release |
| Add foreign key | Existing invalid rows | Find and repair violations first |
Exercise
Why might dropping a column and then adding it again fail to provide a true rollback?
Show answer
The new column has the same definition but not the values that were deleted. A backup or preserved copy is needed to restore the data.