Repository navigation
fix(deploy): stop running migrations on every boot, lower pg pool defaults - #3491
Merged
Merged
Conversation
…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]>
3 of 4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the
adcp-docsFly deployment that was failing with PG53300 — 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:
HTTPServer.startran migrations on every machine boot (server/src/http.ts:8538), even thoughfly.toml'srelease_commandalready 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 readschema_migrationsand crashed when slots ran out. Removed the call. Migrations now run only via the release command in prod, and viaRUN_MIGRATIONS=trueinindex.tsfor local/docker (docker-compose.ymlalready sets this).Default pg pool was sized for a tiny fleet (
max=20, min=5). With many machines,minalone idles 5×N connections and bursts to 20×N — easy to dwarf typical Postgres connection ceilings. Lowered defaults tomax=8, min=0. Override per-environment withDATABASE_POOL_MAX/DATABASE_POOL_MINif 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 cleanrelease_command(node dist/db/migrate.js) still runs migrations on next deploy and is the only path that does🤖 Generated with Claude Code