severity: errorpack: dbv1.0 core
db/per-request-client
Database client constructed per request
npx @pitcheye/check . --pack dbwhyItBreaks
Every request opens a new TCP connection that is never explicitly closed. Against a pooler (PgBouncer/Supabase/Neon) the slot stays checked out until it times out, so the app serves a few hundred requests and then hangs forever on connection acquisition — the classic "works locally, dies on deploy at ~200 rps" failure.
✖ Bad — triggers errorfixtures/db-per-request-client/bad
export async function GET() {
const sql = postgres(process.env.DATABASE_URL!);
}✓ Good — passes checkfixtures/db-per-request-client/good
const sql = postgres(process.env.DATABASE_URL!); // module scope, one pool
let db;
export function getDb() {
if (!db) db = drizzle(sql);
return db;
}suggestedFix
Hoist one module-level client and hand it out through a single accessor (`getDb()`), guarded with `if (!db) db = createDb()` so the module is evaluated once per process.
explain_rule({"db/per-request-client"})Agent & MCP JSON output contract
{
"ruleId": "db/per-request-client",
"pack": "db",
"severity": "error",
"file": "apps/web/src/app/api/health/route.ts",
"line": 6,
"message": "postgres()/createDb()/new Pool()/neon() constructed inside a request-path function",
"whyItBreaks": "Every request opens a new TCP connection that is never explicitly closed. Against a pooler (PgBouncer/Supabase/Neon) the slot stays checked out until it times out, so the app serves a few hundred requests and then hangs forever on connection acquisition — the classic \"works locally, dies on deploy at ~200 rps\" failure.",
"suggestedFix": "Hoist one module-level client and hand it out through a single accessor (`getDb()`), guarded with `if (!db) db = createDb()` so the module is evaluated once per process.",
"docsUrl": "https://pitcheye.dev/rules/db-per-request-client"
}Markdown: curl -H "Accept: text/markdown" https://pitcheye.dev/rules/db-per-request-client
raw markdown
# db/per-request-client
- **severity:** error
- **pack:** db
- **docs:** https://pitcheye.dev/rules/db-per-request-client
## whyItBreaks
Every request opens a new TCP connection that is never explicitly closed. Against a pooler (PgBouncer/Supabase/Neon) the slot stays checked out until it times out, so the app serves a few hundred requests and then hangs forever on connection acquisition — the classic "works locally, dies on deploy at ~200 rps" failure.
## bad
```ts
export async function GET() {
const sql = postgres(process.env.DATABASE_URL!);
}
```
## good
```ts
const sql = postgres(process.env.DATABASE_URL!); // module scope, one pool
let db;
export function getDb() {
if (!db) db = drizzle(sql);
return db;
}
```
## suggestedFix
Hoist one module-level client and hand it out through a single accessor (`getDb()`), guarded with `if (!db) db = createDb()` so the module is evaluated once per process.
## commands
```bash
npx @pitcheye/check . --pack db
```
```json
{"tool":"explain_rule","arguments":{"ruleId":"db/per-request-client"}}
```