Model performance: batch persistToDb into single multi-row INSERT
2026-05-07
What We Built
persistToDb iterated all in-memory buckets and issued one await client.query(INSERT ... ON CONFLICT) per bucket inside a loop. 100 buckets = 100 sequential round-trips on a held pg client, blocking that connection for the entire 5-minute flush window. Fix: build a multi-row VALUES parameterized INSERT with chunking at 4000 rows/statement.
Why It Matters
Held connection during flush was reducing effective DB pool size for request traffic. Single-statement insert means connection is released ~100x faster.
How It Works
Regression test inserts 50 buckets and asserts pool.insertCallCount === 1 (vs 50 pre-fix). ON CONFLICT semantics preserved verbatim.
Lockstep Checklist
- [x] No API route changes (middleware/internal — lockstep N/A)
- [x] No SDK changes
- [x] No MCP tool changes
- [x] Regression test included (test-first invariant per /quality-fleet protocol)
- [x] Linked to /quality-fleet R1 dashboard at
.quality/dashboard.md
Provenance
Auto-found by /quality-fleet R1 (2026-05-07) scanner round, fixed in fix-agent batch under "go for all of it" autonomy grant. PR #209 merged to main as commit 70d2e16ac. Finding(s) tracked at .quality/findings.jsonl (entries: "e4c7a2d98b56"). Production-deployed via ECS task-def revision 732 series.