A new column is one of the most common changes in a database. It can unlock new features, store additional state, or power better analytics. But adding it without slowing down your application takes precision.
In SQL, the ALTER TABLE command creates a new column. A basic example:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP;
This works, but production systems are rarely this simple. Large tables require careful planning. Adding a column can lock the table, block writes, and cause downtime. Strategies like online schema changes or background migrations reduce risk.
For PostgreSQL, ALTER TABLE with ADD COLUMN is fast if you’re adding a nullable column without a default. Adding a default value will rewrite the table in older versions, so it’s safer to add the column first, then update values in batches.
In MySQL, tools like gh-ost or pt-online-schema-change avoid locking by creating a shadow table and synchronizing changes before swapping them in. This is essential for high-traffic production environments.