Skip to content

db/perf: add a warm-connection duplicate prepare() guard to catch uncached fixed-shape Drizzle queries automatically #8445

Description

@Tiberriver256

Summary

We've been fixing uncached hot-path Drizzle queries one file at a time (already merged: #8362 / #8369, #8363 / #8367, #8364 / #8368, #8365 / #8366; newly filed: #8437, #8438, #8439), because neither of the repo's existing database perf tests catches repeated SQL compilation automatically:

Adding a low-level duplicate-prepare() guard (either on warm requests in request-path-statements.test.ts or alongside query-plan-test-guard.ts with an allowlist like query-plan-allowlist.ts) would flush out and prevent this entire class of regressions automatically instead of playing whack-a-mole.

What a full-suite sweep catches right now (272957819d)

Instrumenting Database.prototype.prepare across all 518 test files (74 in @bb/db + 444 in @bb/server, excluding tests and migrate.ts) to count identical (connection, sql) strings prepared more than once on the same SQLite connection surfaced:

  • 1,141,421 duplicate db.$client.prepare() calls of identical SQL strings
  • 684 unique (callsite, sql) pairs across 58 production files

Top 10 files by duplicate prepare() calls (89% of total)

File Duplicate prepare()s Unique SQLs Examples of what the guard catches
packages/db/src/data/events.ts 573,435 71 insertStoredEventRow (332,329 — #8437), timeline window queries (#8439)
packages/db/src/data/thread-pruning.ts 187,024 35 prepareThreadPruningCursor, getNextThreadPruningPolicy, drainPendingThreadPruningWork
packages/db/src/data/app-settings.ts 116,479 5 writeValue (96,639), getDisabledProviderIds (13,963)
packages/db/src/data/threads.ts 56,827 48 upsertThreadSearchSegment (20,376), deleteThreadSearchSegmentById, getThreadStartupContext, isThreadCopyingForkHistory (#8438)
packages/db/src/data/thread-pruning-work.ts 49,614 2 markThreadPruningWork (49,181 — #8437)
packages/db/src/data/resolved-item-pruning.ts 19,268 19 Pruning cursor / batch statements
packages/db/src/data/plugin-marketplaces.ts 16,724 6 getPluginMarketplace (9,156), getPluginMarketplaceIcon
packages/db/src/data/environment-variables.ts 14,483 6 listEnvironmentVariables (14,428)
packages/db/src/data/thread-run-state.ts 10,437 10 Remaining write/update queries
packages/db/src/data/environments.ts 10,385 30 getPreparingEnvironment, environmentHasLiveThreads

Proposed Guardrail Options

  1. Warm-request compilation guard in apps/server/test/perf/request-path-statements.test.ts (highest signal, zero cold-path noise):

    • Run each of the 34 real request paths once to warm per-connection statement caches, then record db.$client.prepare() (and SQLiteSyncDialect.prototype.sqlToQuery) on a second warm execution.
    • Fail if any fixed-shape SQL string is re-prepared on the warm pass (with an explicit allowlist for genuinely dynamic IN (...) batch queries).
    • Catches regressions on every hot HTTP/WS/daemon/sweep path without nagging cold admin or migration queries.
  2. Duplicate-SQL detector in packages/db/src/query-plan-test-guard.ts (broadest coverage):

    • In CI audit mode, track (connection, sql) pairs on db.$client.prepare() and report any production call site under packages/db/src/data/ that prepares the exact same SQL string more than once on the same connection, backed by an explicit allowlist mirroring query-plan-allowlist.ts.

Related Issues

AGENT GENERATED

Activity

  1. added theissue type on Dec 1, 2026
  2. added
    threads
    on Dec 1, 2026
  3. bb-slop-cop commented on Dec 1, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    Applied and verified the missing classifications:

    • Type: Task
    • Priority: Low
    • Effort: Medium
    • Labels: threads, perf

    This requests broader automated database-performance regression coverage, rather than a separate runtime bug fix. Existing tests cover selected cached lookups and request statement scaling; extending detection requires test instrumentation and exception handling. No existing classification values were present to preserve.

    No reproduction report or pull request: this is a non-Bug task, so the rule skips those steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    perfthreads

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions