How do you backfill 90 days into a live table safely?
Question
You fixed a discount bug and must backfill 90 daily partitions (2026-09-04 to 2026-12-02) in a live daily_orders warehouse table. Each day holds 2 million rows (180 million rows total) and takes 2 minutes to rebuild.
Your warehouse has 16 query slots. Live BI dashboards and hourly ingestion use 10 slots, leaving 6 spare slots.
- Why is launching all 90 days at once unsafe, and how do you throttle concurrency and estimate finish time?
- How do you load each day without double-counting or exposing half-written partitions to live readers?
- What reconciliation checks do you run before promoting each partition?
Show hints (3)
- "BUDGET QUERY SLOTS"
Keep live traffic's 10 slots untouched and leave headroom from the 6 spare slots. Try capping backfill at 3 concurrent day jobs and compute total runtime.
- "STAGE THEN SWAP"
Write each day into an isolated staging table or partition first. Replace the live day atomically in one transaction or partition exchange.
- "RECONCILE BEFORE PUBLISH"
Compare staging row count,
SUM(cents), and duplicate key counts against source expectations before swapping intolive.
Show checked explanation
Cap backfill concurrency at 3 day jobs (or use a dedicated compute pool) so live BI keeps 10 slots plus 3 burst slots. Ninety days finish in 60 minutes (30 waves * 2 min). Stage each day in isolation, reconcile row count and total cents, then swap that partition in one transaction so reruns stay idempotent.
import sqlite3 def backfill_day(conn, dt, rows, want_n, want_sum): if len(rows) != want_n or sum(c for _, c in rows) != want_sum: raise ValueError("reconciliation mismatch") with conn: conn.execute("DELETE FROM stg") conn.executemany("INSERT INTO stg VALUES (?, ?)", rows) conn.execute("DELETE FROM live WHERE dt = ?", (dt,)) conn.execute("INSERT INTO live SELECT ?, id, cents FROM stg", (dt,)) db = sqlite3.connect(":memory:") db.execute("CREATE TABLE live (dt TEXT, id INT PRIMARY KEY, cents INT)") db.execute("CREATE TEMP TABLE stg (id INT, cents INT)") db.execute("INSERT INTO live VALUES ('2026-12-01', 1, 500)") for _ in range(2): backfill_day(db, "2026-12-01", [(1, 450), (2, 300)], 2, 750) print(db.execute("SELECT dt, COUNT(*), SUM(cents) FROM live").fetchone())
('2026-12-01', 2, 750)Author-checked on CPython 3.12.10 and SQLite 3.49.1: ran the staged atomic backfill twice for idempotency, checked slot and runtime math, and tested rollback on reconciliation mismatch and duplicate keys. Checked by an editor other than the author.
Walkthrough
Backfilling 180 million rows into a live table fails in three classic ways: resource starvation, double-loaded data, and half-written days visible to dashboards.
1. Throttle concurrency to protect live SLAs. Firing 90 jobs into 6 spare slots oversubscribes the queue by 15x, starving hourly ingestion and dashboards. Better: route backfill to a separate compute pool or cap concurrency at 3 day jobs (leaving 3 spare slots for live bursts). At 2 minutes per day, 90 / 3 = 30 waves finish in 60 minutes (1 hour). Run 1 pilot day first and inspect output before launching the remaining 89 days.
2. Stage and swap one partition at a time. Never append directly into live, and never run DELETE and INSERT in separate commits. Write each day into a staging table or partition, then replace that single dt partition atomically (DELETE + INSERT in one transaction, PostgreSQL ATTACH/DETACH PARTITION, or Iceberg partition overwrite). If a worker crashes or retries, backfill_day either rolls back cleanly or overwrites the same slice with identical rows.
3. Reconcile before swapping. Check each staged day before promoting it:
| Check | What it compares | Catches |
|---|---|---|
| Row count | COUNT(*) vs expected source count | Truncated extracts, missing files |
| Metric total | SUM(cents) vs source total (or expected delta) | Double joins, formula regressions |
| Key uniqueness | COUNT(DISTINCT id) = COUNT(*) | Duplicate loads before primary-key swap |
| Partition isolation | MIN(dt) = MAX(dt) = target_day | Time-zone spillover into neighbor days |
Also check downstream incremental models: pause scheduled triggers or pass explicit partition dates so backfilled September rows do not trigger alerts or inflate today's incremental totals.
Common mistakes
- Launching all 90 days in parallel on the shared production pool, saturating all 16 slots and breaking live BI and hourly ingestion SLAs.
- Using
APPENDinstead of isolated partition replace, which turns every retried day into doubled rows and inflated revenue. - Skipping a 1-day pilot run and reconciliation check. A typo in the new discount formula then corrupts all 90 partitions before anyone notices.
- Forgetting downstream consumers that read by
updated_ator CDC logs, causing 90 days of old orders to fire live alerts or exports.
Follow-up questions
- "Should you backfill newest days first or oldest days first?" If partitions do not depend on prior days, run newest days first so recent dashboards recover quickly. If each day depends on yesterday's running balance or cumulative state, you must backfill oldest to newest in strict order.
- "Day 47 of 90 fails due to a transient source timeout. How do you resume?" Track each date partition's status (
pending,staged,published,failed) in a control table or orchestrator DAG. Retry only failed or pending dates; because every partition load is idempotent, re-running day 47 never duplicates data. - "What if one single partition swap needs zero reader lock wait on a huge PostgreSQL table?" Build and index a detached staging table for that day first. Validate its
CHECKconstraint soATTACH PARTITIONskips a full table scan, then swap partitions in a short metadata transaction with alock_timeout.
What a strong answer covers
Budgets query slots at 3 concurrent days (60 minutes total, 3 slots burst headroom) and pilots 1 day first. Stages and reconciles row count plus SUM(cents) before an atomic per-day swap, and accounts for downstream consumers.
Open in interactive practice deck →