Adding a new column is one of the most common database changes. It sounds simple, but a poorly handled schema change can lock tables, slow queries, or bring down production. The right approach depends on your database engine, the size of your data, and the traffic patterns of your system.
In SQL, the syntax is straightforward:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
On a small table, this runs instantly. On a table with hundreds of millions of rows, execution time and locking can become dangerous. Modern migrations require careful planning to avoid downtime.
For PostgreSQL, adding a nullable column with no default usually completes quickly, because it does not rewrite the table. Adding a column with a default value or a NOT NULL constraint forces a full table rewrite. This can block writes for a long time. To avoid it, add the column as nullable, backfill in batches, then apply the constraint.
In MySQL and MariaDB, online DDL options like ALGORITHM=INPLACE can prevent full locks. Always check the engine documentation for supported algorithms. For example: