Adding a new column in a production database is simple in syntax but heavy in consequence. Whether you use PostgreSQL, MySQL, or a columnar store, the operation changes your data shape. It affects queries, indexes, and downstream services. Done carelessly, it locks tables and blocks writes. Done well, it unlocks features without downtime.
In SQL, the basic form is straightforward:
ALTER TABLE users ADD COLUMN last_active_at TIMESTAMP;
For PostgreSQL, this is instant if the column can be null. Adding a non-null column with a default rewrites the table. On large datasets, that means minutes or hours of blocking time. The safer approach is two steps:
ALTER TABLE users ADD COLUMN last_active_at TIMESTAMP;
UPDATE users SET last_active_at = NOW() WHERE last_active_at IS NULL;
ALTER TABLE users ALTER COLUMN last_active_at SET NOT NULL;
When you add a new column, think about: