Skip to content

fix(deploy): stop running migrations on every boot, lower pg pool defaults - #3491

Merged
bokelley merged 3 commits into
mainfrom
bokelley/fly-pg-conn-exhaust
Apr 29, 2026
Merged

bokelley merged 3 commits into
mainfrom
bokelley/fly-pg-conn-exhaust

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

Fixes the adcp-docs Fly deployment that was failing with PG 53300 — remaining connection slots are reserved for roles with the SUPERUSER attribute. The site stayed live on the old (v2201) machines because the new (v2202) ones crashed on boot before becoming healthy.

Two contributing problems:

  1. HTTPServer.start ran migrations on every machine boot (server/src/http.ts:8538), even though fly.toml's release_command already runs migrations once before each deploy. With dozens of machines booting in parallel during a rolling deploy, each one grabbed a pool client just to read schema_migrations and crashed when slots ran out. Removed the call. Migrations now run only via the release command in prod, and via RUN_MIGRATIONS=true in index.ts for local/docker (docker-compose.yml already sets this).

  2. Default pg pool was sized for a tiny fleet (max=20, min=5). With many machines, min alone idles 5×N connections and bursts to 20×N — easy to dwarf typical Postgres connection ceilings. Lowered defaults to max=8, min=0. Override per-environment with DATABASE_POOL_MAX / DATABASE_POOL_MIN if needed.

The immediate prod incident was unblocked separately by scaling machine count down (was 30 web + 29 worker — way above min_machines_running = 2). This PR is the durable fix so it doesn't recur next deploy.

Test plan

  • npx vitest run tests/unit/migrate.test.ts — 14 passing (no regression in migration logic itself)
  • npx tsc --noEmit — typecheck clean
  • Pre-commit hook (test:unit + test:test-dynamic-imports + typecheck) passed
  • Watch v2203 deploy: rolling deploy of v2202+1 should complete cleanly without machines crash-looping on PG conn slots
  • Confirm release_command (node dist/db/migrate.js) still runs migrations on next deploy and is the only path that does

🤖 Generated with Claude Code

bokelley and others added 3 commits April 28, 2026 20:50
…ault pg pool

Fly rolling deploys were exhausting Postgres connection slots
(53300, "remaining connection slots are reserved for roles with the
SUPERUSER attribute"). Two contributors:

1. HTTPServer.start called runMigrations() unconditionally on every
   boot, even though fly.toml's release_command runs migrations once
   before each deploy. With dozens of machines booting in parallel, each
   one grabbed a pool client just to read schema_migrations and crashed
   when slots ran out. Removed; migrations now run only via the release
   command in prod and RUN_MIGRATIONS=true for local/docker.
2. Default pg pool was max=20, min=5 per machine. With many machines
   that idles 5×N connections and bursts to 20×N, dwarfing typical
   Postgres connection ceilings. Lowered defaults to max=8, min=0.
   Override with DATABASE_POOL_MAX / DATABASE_POOL_MIN.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Defense in depth on top of the previous commit. Even though migrations
should now only run from one place (release_command in prod,
RUN_MIGRATIONS=true in local/docker), wrap the whole run in a
session-scoped pg_advisory_lock so any concurrent caller — a stray app
boot, two devs hitting it locally, future re-introduction of an
in-process call — blocks until the holder finishes instead of racing on
schema_migrations or partially applying the same migration twice.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
- Don't let pg_advisory_unlock failures shadow real migration errors —
  log and continue. Session-scoped locks are released by pg on
  disconnect anyway, so a failed unlock is not load-bearing.
- Pick a lock key (0x6D696772 / 'migr') that fits in int32 to avoid
  overload-resolution ambiguity between pg_advisory_lock(int) and
  pg_advisory_lock(bigint). Old value (4242424242) exceeded int32 max.
- Tests assert the exact lock key value instead of expect.any(Array) —
  a typo'd key would have silently produced zero mutual exclusion in
  prod and still passed tests.
- Added ordering assertion (lock precedes unlock) and a test for the
  unlock-shadowing case.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit 9cb0205 into main Apr 29, 2026
15 of 16 checks passed
@bokelley
bokelley deleted the bokelley/fly-pg-conn-exhaust branch April 29, 2026 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant