You need to add an index and a column to a 500-million-row table that's written to constantly. How do you avoid locking it?

AdvancedScenarioData and databases
Answer

Situation read → Some DDL takes locks that block writes for the whole operation. On a big hot table that's an outage. Use the online variants.

What I'd do →

  1. PostgreSQL index: build it concurrently (no write lock; can't run inside a transaction).
CREATE INDEX CONCURRENTLY idx_orders_customer ON orders (customer_id);

If it fails it leaves an INVALID index; drop it and retry. 2. PostgreSQL column: adding a nullable column, or one with a constant default, is metadata-only in PG 11+. Set lock_timeout so the brief lock never queues behind a long query and blocks everyone.

SET lock_timeout = '3s';
ALTER TABLE orders ADD COLUMN channel text;
  1. MySQL: use ALGORITHM=INSTANT where supported, otherwise gh-ost or pt-online-schema-change, which copy into a shadow table and swap.
  2. Run it off-peak, watch replication lag, and throttle if replicas fall behind.

Why → lock_timeout matters because a waiting ALTER blocks every query behind it, even if the ALTER itself is fast.

Interview tip → Explain the lock queue problem; it's the reason "instant" DDL still causes outages.

All Real-World Interview questions Roadmap: Interview readiness

More on Data and databases