Interview Databases & Caching

Why are some ALTER TABLE operations dangerous on a large table, and how do you mitigate?

Databases & Caching · Intermediate level

Answer

Large ALTER TABLE operations are dangerous because they can lock tables, rewrite data, generate huge WAL/binlogs, increase replica lag, and exhaust storage. I mitigate with concurrent operations, batches, NOT VALID constraints, and rehearsals.

Technical explanation

Old and new app versions may run at the same time, so schema must support both during rollout.

Large table rewrites, blocking locks, foreign-key validation, and index builds can cause outages if done naively.

Always rehearse on production-sized data and set lock_timeout and statement_timeout.

Hands-on example

Expand-contract example:

ALTER TABLE users ADD COLUMN full_name text;

Deploy app that dual-writes name and full_name and reads COALESCE(full_name, name).

Backfill in batches.

After all old app versions are gone, add constraints and later drop the old column.

Foreign key safer pattern:

ALTER TABLE orders ADD CONSTRAINT orders_customer_fk FOREIGN KEY (customer_id) REFERENCES customers(id) NOT VALID;

ALTER TABLE orders VALIDATE CONSTRAINT orders_customer_fk;

Preparing for an interview?

Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.

More Databases & Caching interview questions

← All Databases & Caching questions