Adding a new column should be fast, safe, and predictable. Whether working in PostgreSQL, MySQL, or a distributed SQL system, the approach is the same: define what the column stores, plan the schema change, and execute it without breaking production. The wrong move can lock writes, trigger downtime, or break dependent services.
Start by choosing the correct column type. Match it to the data shape — integers for counters, text for strings, JSON for semi-structured payloads. Decide on nullability. A NOT NULL column with no default will fail if rows lack values. Consider whether the column needs a default value for seamless deployment.
In PostgreSQL, a statement like:
ALTER TABLE users ADD COLUMN last_login TIMESTAMPTZ DEFAULT now();
adds a new column, sets a default, and ensures new rows have valid data. In large tables, avoid updating every row in a single transaction unless necessary. Use DEFAULT NULL and backfill in batches to prevent locks.
In MySQL, ALTER TABLE often rebuilds the whole table. On massive datasets, this can be a risk. Use online schema change tools like pt-online-schema-change or gh-ost to keep the database responsive.