Adding a new column to a database is common, but it can be costly if handled poorly. The wrong approach can lock tables, impact query performance, or break downstream applications. The right approach is fast, safe, and minimizes downtime.
Start with clarity: know exactly why the new column exists. Identify its type, constraints, and default values. Avoid defaults that require rewriting all existing rows if the dataset is large. Use NULL defaults when possible, then backfill in controlled batches.
On relational databases like PostgreSQL and MySQL, schema changes are not always instant. Adding a non-nullable column with a default can trigger a full table rewrite. This can block writes and degrade reads. For large tables, consider online schema changes or tools like gh-ost and pt-online-schema-change.
If the new column supports indexes, create them after backfilling data. Building indexes on empty columns wastes resources. Indexing after backfill ensures operational efficiency and avoids unnecessary locking.