Wrap startup migrations in a Postgres advisory lock #16

Open
opened 2026-07-26 17:06:36 +10:00 by benvin · 0 comments
Owner

Problem. Migrations are embedded and applied automatically on startup.
The moment a second API replica exists (or a restart races a slow migration),
two processes run the migration path concurrently — partial DDL, duplicate
inserts, or a wedged schema, on the database that is the source of truth for
the entire firewall estate.

Proposal. pg_advisory_lock around the migrate step (one line with most
migration libraries); replicas that don't win the lock wait and proceed once
the schema is current. Cheap now, painful to retrofit after an incident.

**Problem.** Migrations are embedded and applied automatically on startup. The moment a second API replica exists (or a restart races a slow migration), two processes run the migration path concurrently — partial DDL, duplicate inserts, or a wedged schema, on the database that is the source of truth for the entire firewall estate. **Proposal.** `pg_advisory_lock` around the migrate step (one line with most migration libraries); replicas that don't win the lock wait and proceed once the schema is current. Cheap now, painful to retrofit after an incident.
Sign in to join this conversation.