Skip to content

listActiveBackgroundTaskCountsByThreadIds throws 'too many SQL variables' at ≥16,377 visible threads, failing the thread-list route #1205

Closed

Description

@SawyerHood

Summary

listActiveBackgroundTaskCountsByThreadIds (packages/db/src/data/events.ts) binds the caller's thread-id list twice — once in each CTE. SQLite's variable limit is 32,766. At 16,377 or more thread ids the query throws SqliteError: too many SQL variables (measured boundary: 16,376 works, 16,377 throws; the double-bound list plus the fixed parameters crosses the limit).

toThreadListEntryResponses calls this function with every listed thread id. So on an instance with ≥16,377 visible threads, GET /api/threads (and bb thread list) does not get slow — it fails outright. The projects-with-threads bootstrap fails the same way once the active thread count crosses the boundary.

Found while measuring #1131 at 26K visible threads; the thread-list route could not run at all until I capped the id list.

Reproduction

Verified against main (d807f13):

import {
  createConnection,
  migrate,
  listActiveBackgroundTaskCountsByThreadIds,
} from "@bb/db";

const db = createConnection(":memory:");
migrate(db);
const ids = (n: number) => Array.from({ length: n }, (_, i) => `thr_${i}`);

listActiveBackgroundTaskCountsByThreadIds(db, { threadIds: ids(16_376) }); // ok
listActiveBackgroundTaskCountsByThreadIds(db, { threadIds: ids(16_377) });
// SqliteError: too many SQL variables

No thread rows need to exist; the prepare fails before execution.

Also: the same query has the pre-fix #1131 access pattern

Cold-cache cost measured on a 1.45GB bb.db (30K threads / 595K events, page cache evicted with posix_fadvise(DONTNEED), reads counted via /proc/self/io):

thread ids cold read syscalls cold time @1000 IOPS
5,000 ~4,150 ~4s
15,000 ~6,888 ~7s

The probes run over the full-size events_thread_type_item_kind_sequence_idx, so cold cost scales with the events index, not with the number of background-task events. This is the same class of problem #1204 fixed for listLatestGoalEventRowsByThreadIds.

Suggested fix

  1. Chunk the thread-id list (or use a temp table / JSON parameter) so the route survives any thread count.
  2. Give it the Fix the #1131 cold stall at its source: index the latest-goal lookup, drop the thread-list GROUP BY #1204 treatment: a partial index over item_kind = 'backgroundTask' events with literal predicates (a partial index never matches a bound parameter) and an INDEXED BY pin, since the stats-less planner will not pick a small partial index on its own.

Refs #1131, #1204.

🤖 Generated with Claude Code

Activity

  1. self-assigned this
    on Aug 8, 2026
  2. added a commit that references this issue on Aug 8, 2026
  3. added a commit that references this issue on Aug 8, 2026
    f0f7849
  4. added 2 commits that reference this issue on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions