Adding a new column is one of the most common changes in database schema design. It can be simple, but in production systems it demands precision. Mistakes here can lock tables, cause downtime, or break existing queries. The right method depends on the database engine, the data type, and the scale of data.
For relational databases like PostgreSQL and MySQL, the ALTER TABLE statement is the standard way to add a new column. A basic example:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP;
This command defines the column and its type. But in high-traffic systems, running this unbuffered can block reads and writes. PostgreSQL manages many changes efficiently, yet large tables still require careful planning. Setting a default value or a NOT NULL constraint will trigger a full table rewrite, which can be dangerous under load.
In MySQL, especially older versions, adding a new column often locks the table. Newer releases with INSTANT or ONLINE options reduce impact, but only for certain column operations. Always confirm whether your version supports these features. If not, use tools like pt-online-schema-change to migrate without downtime.