perf(drivers): MongoDB + PostgreSQL optimizations (3/3 of #991)#994
perf(drivers): MongoDB + PostgreSQL optimizations (3/3 of #991)#994SamTV12345 wants to merge 2 commits into
Conversation
MongoDB (databases/mongodb_db.ts): - Remove the per-operation schedulePing() (clearInterval + setInterval on every get/set/findKeys/remove/doBulk). The mongodb driver already maintains its own server heartbeat; the redundant ping cost two timer mutations per DB operation. - Fix findKeys regex: previously stripped '*' instead of converting it to '.*' and had no '^...\$' anchor, producing unanchored substring matches that bypassed the _id index and could return false positives. Now escapes regex metacharacters and converts wildcards correctly. - Switch doBulk from initializeOrderedBulkOp() to initializeUnorderedBulkOp() — our bulk ops are keyed differently per op so ordering is unnecessary and unordered parallelizes server-side. PostgreSQL (databases/postgres_db.ts): - doBulk uses a single multi-row UPSERT (INSERT ... VALUES (\$1,\$2),(\$3,\$4),... ON CONFLICT DO UPDATE) on the native upsert path, collapsing N round-trips into one. The function-based fallback path is preserved for old PG versions. - Named prepared statements on the three hottest single-row queries (get, set, remove) so PG parses and plans them once per connection. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The mongo + postgres perf commit introduced three `any` casts that
qodo-style review flagged. Replace them with concrete types:
- mongodb_db.ts: type the findKeys selector as `Filter<UeberDoc>` where
`UeberDoc = { _id: string; value: string }` narrows mongo's default
ObjectId-typed `_id` to our string-keyed schema. The collection is
cast to `Collection<UeberDoc>` at the find() call so the rest of the
driver's typing is unchanged.
- postgres_db.ts: type doBulk's `tasks` array via an `AsyncTaskCb`
alias `(err?: Error | null) => void` matching async.parallel's
contract. Wrap each pg.query in a lambda that bridges pg's
`(err: Error, result)` signature to the async-shaped callback, so
the previous `tasks as any` cast goes away.
No behavior change; ts-check + lint stay clean; 79 tests pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
Review Summary by QodoMongoDB and PostgreSQL driver performance optimizations
WalkthroughsDescription• Remove redundant per-operation MongoDB ping scheduling (clearInterval/setInterval pairs) • Fix MongoDB findKeys regex to properly escape metacharacters and anchor matches • Switch MongoDB bulk operations from ordered to unordered for server-side parallelization • Collapse PostgreSQL bulk upserts into single multi-row INSERT with ON CONFLICT • Add named prepared statements for PostgreSQL hottest queries (get, set, remove) • Tighten TypeScript types with UeberDoc document shape and Filter narrowing Diagramflowchart LR
A["MongoDB Operations"] -->|Remove schedulePing| B["Eliminate Timer Overhead"]
A -->|Fix findKeys Regex| C["Correct Pattern Matching"]
A -->|Unordered Bulk Ops| D["Enable Server Parallelization"]
E["PostgreSQL Operations"] -->|Multi-row Upsert| F["Reduce Round-trips"]
E -->|Named Prepared Statements| G["Cache Query Plans"]
H["Type Improvements"] -->|UeberDoc + Filter| I["Remove any Casts"]
File Changes1. databases/mongodb_db.ts
|
Code Review by Qodo
1. doBulk may never resolve
|
perf(drivers): MongoDB + PostgreSQL driver optimizations
Part 3 of 3 from splitting #991. Touches
databases/mongodb_db.tsanddatabases/postgres_db.tsonly.MongoDB (
mongodb_db.ts)schedulePing(). Everyget/set/findKeys/remove/doBulkdid aclearInterval+setIntervalpair. The mongodb driver already maintains its own server heartbeat, so this was two timer mutations per op for nothing. Theintervalfield andclearPing/schedulePinghelpers are removed.findKeysregex. Previously stripped*instead of converting it to.*, and had no^…$anchor — producing unanchored substring matches that bypassed the_idindex and could return false positives. Now escapes regex metacharacters and converts wildcards correctly.doBulkusesinitializeUnorderedBulkOp()instead of ordered — our bulk ops are keyed per-op so ordering is unnecessary, and unordered parallelizes server-side.UeberDocdocument shape +Filter<UeberDoc>/Collection<UeberDoc>narrowing replace theanycasts the changes above would otherwise have introduced.PostgreSQL (
postgres_db.ts)doBulkcollapses N round-trips into one on the native-upsert path via a single multi-rowINSERT … VALUES ($1,$2),($3,$4),… ON CONFLICT DO UPDATE. The function-based fallback (old PG / CockroachDB without native upsert) is preserved for single-row and multi-row cases.get,set,remove) so PG parses + plans them once per connection.Verification
tsc --noEmitpasses.vitest run test/mock test/memory→ 173 passing. (Live mongo/postgres suites require running databases — CI exercises those.)Merge order
These two files are also reformatted by the formatting PR, so merge the formatting PR first, then I'll rebase this one on top (it'll resolve to a clean logic-only diff). Independent of the cache PR.
🤖 Generated with Claude Code