dbConnectionBudget in capacity.ts reserved room for an API deploy overlap (apiReloadOverlap, BIT-31550) but not for a worker deploy overlap. PM2 startOrReload on the fork-mode photoreport-worker starts the new process before the old one finishes its 25 s drain (DRAIN_MS in worker.ts), so both Prisma pools and both broadcasters are open at once.
With defaults:
Worker Prisma pool: 47 (capped by the budget)
Worker broadcaster: 1
During worker overlap: 2 × (47 + 1) = 96
API steady state: 2 × (10 + 3) = 26
Total: 122 > 100 max_connections → PostgreSQL error 53300 "sorry, too many clients already"
This is the same class of bug as BIT-31550 (API reload overlap), just for the worker.
Fix
Add workerReloadOverlap (default 1) to dbConnectionBudget. The formula now divides the remaining budget by (1 + workerReloadOverlap), so the total stays under max_connections even when two workers coexist during a reload.
The worker's Prisma pool drops from 47 to 23 with defaults — still enough for the concurrent jobs the capacity planner sizes, and the headroom and ad-hoc connection margin are preserved.
vitest run --config vitest.unit.config.ts — 280 passed, 3 skipped
New test: total connections during a worker deploy overlap stay ≤ max_connections
Moved test/capacity.test.ts into the unit config (pure test, no database needed)
Paperclip-Issue: BIT-33530
## Root cause
`dbConnectionBudget` in `capacity.ts` reserved room for an API deploy overlap (`apiReloadOverlap`, BIT-31550) but not for a **worker deploy overlap**. PM2 `startOrReload` on the fork-mode `photoreport-worker` starts the new process before the old one finishes its 25 s drain (`DRAIN_MS` in `worker.ts`), so both Prisma pools and both broadcasters are open at once.
With defaults:
- Worker Prisma pool: 47 (capped by the budget)
- Worker broadcaster: 1
- During worker overlap: 2 × (47 + 1) = 96
- API steady state: 2 × (10 + 3) = 26
- **Total: 122 > 100 `max_connections`** → PostgreSQL error 53300 "sorry, too many clients already"
This is the same class of bug as BIT-31550 (API reload overlap), just for the worker.
## Fix
Add `workerReloadOverlap` (default 1) to `dbConnectionBudget`. The formula now divides the remaining budget by `(1 + workerReloadOverlap)`, so the total stays under `max_connections` even when two workers coexist during a reload.
The worker's Prisma pool drops from 47 to 23 with defaults — still enough for the concurrent jobs the capacity planner sizes, and the headroom and ad-hoc connection margin are preserved.
```
// Before: available = max - superuserReserved - apiConnections - 1 - headroom = 47
// After: available = floor((max - superuserReserved - apiConnections - headroom) / (1 + workerOverlap)) - 1 = 23
```
## Verification
- `pnpm --filter @photoreport/api build` — clean
- `pnpm --filter @photoreport/api typecheck` — clean
- `vitest run --config vitest.unit.config.ts` — 280 passed, 3 skipped
- New test: total connections during a worker deploy overlap stay ≤ `max_connections`
- Moved `test/capacity.test.ts` into the unit config (pure test, no database needed)
Paperclip-Issue: BIT-33530
dbConnectionBudget reserved room for an API deploy overlap (apiReloadOverlap,
BIT-31550) but not for a worker deploy overlap. PM2 startOrReload on the
fork-mode photoreport-worker starts the new process before the old one finishes
its 25 s drain, so both Prisma pools and both broadcasters are open at once.
With defaults that is 2 * (47 + 1) + 26 = 122 > 100 max_connections, tipping
Postgres into error 53300 "sorry, too many clients already".
Add workerReloadOverlap (default 1) that divides the remaining budget by the
number of concurrent workers, so the total stays under max_connections even
during a reload. The worker's Prisma pool drops from 47 to 23 with defaults —
enough for the concurrent jobs the capacity planner sizes, and the headroom
and ad-hoc connection margin are preserved.
Paperclip-Issue: BIT-33530
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Root cause
dbConnectionBudgetincapacity.tsreserved room for an API deploy overlap (apiReloadOverlap, BIT-31550) but not for a worker deploy overlap. PM2startOrReloadon the fork-modephotoreport-workerstarts the new process before the old one finishes its 25 s drain (DRAIN_MSinworker.ts), so both Prisma pools and both broadcasters are open at once.With defaults:
max_connections→ PostgreSQL error 53300 "sorry, too many clients already"This is the same class of bug as BIT-31550 (API reload overlap), just for the worker.
Fix
Add
workerReloadOverlap(default 1) todbConnectionBudget. The formula now divides the remaining budget by(1 + workerReloadOverlap), so the total stays undermax_connectionseven when two workers coexist during a reload.The worker's Prisma pool drops from 47 to 23 with defaults — still enough for the concurrent jobs the capacity planner sizes, and the headroom and ad-hoc connection margin are preserved.
Verification
pnpm --filter @photoreport/api build— cleanpnpm --filter @photoreport/api typecheck— cleanvitest run --config vitest.unit.config.ts— 280 passed, 3 skippedmax_connectionstest/capacity.test.tsinto the unit config (pure test, no database needed)Paperclip-Issue: BIT-33530