Adding a new column is a common operation in database development, but speed and precision matter. A poorly executed change can lock tables, break queries, or slow deployments. The goal is to evolve the schema without downtime and without surprises.
In SQL, the ALTER TABLE command is the standard way to create a new column. For example:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
This statement adds a column named last_login with a default value. On small tables, it runs instantly. On large tables, it can trigger a full table rewrite, so the exact impact depends on your database engine and configuration.
For PostgreSQL, avoid unnecessary writes by making the new column NULL initially, then backfilling data in batches. Example: