advancedTop 30 Scenario-Based Questions

You need to add a NOT NULL column to a 500-million-row production table without downtime. How?

PostgreSQL 11+: ALTER TABLE t ADD COLUMN status VARCHAR(20) DEFAULT 'ACTIVE'. In PostgreSQL 11+, this writes the DEFAULT to catalog metadata only — no row rewrite, O(1). Then: ALTER TABLE t ALTER COLUMN status SET NOT NULL (fast if all rows have the default). For older versions or MySQL: 1) Add column as nullable with default. 2) Backfill in batches (UPDATE ... LIMIT 10000). 3) Add NOT NULL after

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.

You need to add a NOT NULL column to a 500-million-row production table without downtime. How?

Next Step

Continue to A query is taking 30 seconds. EXPLAIN ANALYZE shows a Seq Scan on a 100M-row table. How do you fix it?← Back to all SQL questions