Skip to content

Online schema changes on large tables

Accepted.

2026-10-19

The platform’s busiest tables grow with traffic: events and their read projection, dispatch jobs and their read table, dispatch-job attempts, audit logs, login attempts and outbox tables. Most are range-partitioned by month. A plain CREATE INDEX, ALTER TABLE ... SET NOT NULL or a one-statement UPDATE on such a table can hold a strong lock for the whole operation, and in PostgreSQL a request waiting for a strong lock blocks every request queued behind it. A migration that takes minutes on a production-sized table is an outage.

Migrations are applied by the server at start-up (see ADR 0006), so a risky statement runs during a deploy, on every instance that starts. Development databases are nearly empty, which means a migration that is instant in development proves nothing about production.

The owner’s rule, given when a set of index changes on the dispatch tables was being planned, was that a change that can hurt on a big table must be done the online way.

Treat any table that grows with traffic as large, and apply these rules to every migration that touches one. Reference and configuration tables (roles, applications, subscriptions, connections) are small and take plain migrations.

Change Online technique
Add an index CREATE INDEX CONCURRENTLY, in a migration marked -- +goose NO TRANSACTION, one statement per file. A failed build leaves an invalid index: drop it concurrently and rerun
Add an index to a partitioned table CREATE INDEX ... ON ONLY the parent, CONCURRENTLY on each partition, then attach. Ship it as an ops script run before the deploy; the migration then uses IF NOT EXISTS and does nothing
Drop an index DROP INDEX CONCURRENTLY (per partition on partitioned tables)
Add a column Nullable or constant default is instant. A volatile default: add nullable, backfill in batches, then set the default
Add NOT NULL Add a CHECK (col IS NOT NULL) NOT VALID, VALIDATE CONSTRAINT, then SET NOT NULL, then drop the check
Add a foreign key or CHECK Add NOT VALID, then VALIDATE CONSTRAINT
Backfill Batch by key range, commit each batch, throttle, make it idempotent and resumable. Prefer a throttled ops script over a migration
Change a column type (a table rewrite) New column, batched backfill, switch the code, drop the old one; or an external tool such as pg_repack. Needs a plan and an owner decision
Rename or drop a column the running code uses Expand and contract across two releases

Rules that go with the table:

  1. Set lock_timeout (for example SET LOCAL lock_timeout = '5s') so a migration that cannot get its lock fails fast instead of queueing and blocking others.
  2. The migration’s header comment states the lock it takes, whether it rewrites, and the expected duration at production size. If an ops script is needed, it names it.
  3. Test on production-shaped data. An empty embedded database proves nothing about build time.
  4. Ops scripts are rerunnable and run with psql outside a transaction, before the deploy.
  5. Check the table’s real size first. Where production traffic is small, the online script may be unnecessary; say which, rather than assuming.

Good

  • Index and constraint changes no longer risk blocking the write path during a deploy.
  • Reviewers have a checklist, and the cost of a change is written next to it.
  • Failure is safe: a migration that cannot get its lock errors out and can be rerun at a quiet time.

Bad

  • More ceremony. A one-line index becomes two steps (an ops script, then a migration that is a no-op where the script ran), and CONCURRENTLY migrations are one statement per file.
  • Online forms are slower and use more total work than the plain ones.
  • Goose runs each migration in a transaction by default, so authors must remember the no-transaction annotation, and it keeps no checksums to catch a changed file.
  • Ops scripts are outside the migration ledger; operators must run them before the deploy.
  • The first change made under the rule (an index swap on the dispatch read tables) was run on a production database with almost no dispatch traffic, where it was not needed. That produced the “check the size first” rule.
  • An external online-change tool for everything (pg_repack, pgroll, pg-osc). PostgreSQL has native equivalents for most operations, so external tools are kept for table rewrites only.
  • A maintenance window. Possible, but the platform is meant to take rolling deploys.
  • Plain statements and trust the empty-database test. What the rule exists to prevent.