Adding a new column sounds simple, but in production systems it’s rarely trivial. The wrong migration can lock tables, block writes, or stall an entire deployment. The right approach maintains uptime, preserves data integrity, and works across environments without surprises.
In relational databases like PostgreSQL, MySQL, and MariaDB, the ALTER TABLE statement is the standard way to add a new column. But schema migrations run directly on large tables can cause performance hits. For big datasets, consider adding the column as nullable first:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP NULL;
This executes quickly and avoids rewriting existing rows. Once it is live, backfill the column in small batches to prevent load spikes. After the backfill is complete, set NOT NULL constraints or add default values as needed:
ALTER TABLE users ALTER COLUMN last_login SET NOT NULL;
In distributed systems, coordinate deployments so that application code can handle both pre- and post-migration states. Use feature flags to toggle behavior when the new column is ready. Always test the migration against realistic data in staging before applying it to production.