Adding a new column is one of the most common changes in database development. It seems simple, but it touches performance, schema design, and data integrity. Whether working with PostgreSQL, MySQL, or a modern distributed store, the details matter.
First, define the column with precision. Specify the data type, default value, and constraints. Use ALTER TABLE for relational databases, ensuring the migration script is idempotent. For MySQL:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
For PostgreSQL, remember that adding a column with a default can lock the table on large datasets. In high-traffic production systems, add the column without a default, backfill in batches, then set the default. This avoids downtime.
In environments with strict uptime, use tools that perform online schema changes. Percona’s pt-online-schema-change or native PostgreSQL background workers can handle live migrations. Always measure the impact on replication and failover.