A new column in a database may seem small, but it changes the shape of your data forever. It impacts reads, writes, indexes, and downstream consumers. Whether the database is PostgreSQL, MySQL, or a modern cloud-native store, the steps are similar, but the trade-offs differ based on engine, scale, and workload.
When adding a new column in SQL, key considerations include:
- Schema change strategy: For production systems, avoid blocking writes. Use non-locking operations where possible.
- Default values and NULLs: Setting a default can rewrite the whole table and lock it for minutes or hours at scale. Sometimes it’s better to add the column as nullable, backfill asynchronously, then apply constraints.
- Indexes: Adding an index on a new column can be as heavy as the column creation itself. Consider deferred indexing.
- Code deployment sequencing: Deploy schema changes first in a way that is compatible with both old and new application code—schema migration should be backward-compatible until app traffic is fully switched.
In PostgreSQL, you can add a new column with:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
If you need a default without locking large tables: