All ideasMore in Work

Migrate my 180-million-row production table with zero downtime

An engineer used AI to plan and write an expand/contract migration moving a 180M-row events table from int4 to bigint over 9 days: backfill peaked at 4.1 seconds of replica lag and the final swap locked for 1.4 seconds, with zero downtime. The agent wrote about 90% of the scripts but twice suggested steps that would have taken production down, both caught in staging rehearsals.

This only partly worked. Read what happened before you try it.

How it works

  1. Map every foreign key, index, view, trigger and app query touching events.id before proposing any fix
  2. Add a bigint column kept in sync by a trigger
  3. Backfill in 20,000-row batches that pause when replica lag exceeds 5 seconds
  4. Build the unique index concurrently
  5. Prove NOT NULL via a validated check constraint
  6. Rehearse the swap six times on staging
  7. Swap in 1.4 seconds and repeat for three foreign-key tables

Why it’s worth knowing

The agent migrated 180 million rows live — and its 'simpler' shortcut would have locked the whole product for minutes.

Where it’s from

Summarized from a public post on dev.to. See the original ↗

Upgraded prompt

A ready-to-use prompt for this idea, built from research on what makes AI agents do the job right: who the AI should be, the exact steps, and a check to make sure it was done the way you wanted.

1Who the AI should be
2The prompt, with the steps
3The check when it’s done
Upgraded prompts are coming soon.

Try next

Every idea is a real post from someone who tried it, summarized in our words, with a link to the original. App names are swapped for plain words.