Adding a new column is one of the most common operations in database work. Done right, it’s safe, fast, and predictable. Done wrong, it can lock tables, stall deploys, and corrupt data. This guide covers the essential patterns for adding a column without risking downtime.
When to Add a New Column
- Capture a new attribute for existing entities.
- Support new features that need persistent storage.
- Prepare for a migration where old columns will be deprecated.
Best Practices
- Plan the schema change — Know the exact data type, constraints, and defaults before touching production.
- Use non-blocking operations — For PostgreSQL,
ALTER TABLE ... ADD COLUMNis fast for empty columns without defaults. For MySQL, run online DDL (ALGORITHM=INPLACEorINSTANTwhere possible). - Avoid default values on creation — In many databases, adding a default value can rewrite the entire table. Add the column as nullable first, backfill in small batches, then add constraints.
- Backfill with care — Break updates into transactions that fit within lock and replication limits.
- Deploy in stages — Add the new column, deploy code that reads/writes to it, backfill values, then enforce constraints.
Example: PostgreSQL Safe Pattern
ALTER TABLE users ADD COLUMN last_login_at timestamptz;
-- Backfill in batches:
UPDATE users SET last_login_at = NOW() WHERE last_login_at IS NULL LIMIT 1000;
After backfill: