{"record":{"id":"6cf2aec163d2be27","repo":"gastownhall/beads","slug":"search-count-s-w","errorCode":null,"errorMessage":"search count %s: %w","messagePattern":"search count (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/domain/db/issue_search_counts.go","lineNumber":183,"sourceCode":"\tif len(whereClauses) > 0 {\n\t\twhereSQL = \"WHERE \" + strings.Join(whereClauses, \" AND \")\n\t}\n\torderBy := orderBySQL(filter.SortBy, filter.SortDesc, \"i\")\n\treturn r.runSearchQuery(ctx, tables, whereSQL, orderBy, searchWindowForFilter(filter).sql, args, includeWispReverseDeps, hydrationFor(filter))\n}\n\n//nolint:gosec // G201: SQL fragments are built from hardcoded table names and parameterized filters.\nfunc (r *issueSQLRepositoryImpl) runSearchQuery(ctx context.Context, tables filterTables, whereSQL, orderBySQL, limitSQL string, args []any, includeWispReverseDeps bool, hyd sqlbuild.CountsHydration) ([]*types.IssueWithCounts, error) {\n\tsearchSQL, _ := sqlbuild.SearchCountsSQL(tables, nil, whereSQL, orderBySQL, limitSQL, includeWispReverseDeps, hyd)\n\treturn r.scanCountsQuery(ctx, tables, searchSQL, args, hyd)\n}\n\n// scanCountsQuery runs a prebuilt counts mega-query and hydrates each row,\n// deduping by issue ID (mirrors issueops.scanCountsRowsInTx).\nfunc (r *issueSQLRepositoryImpl) scanCountsQuery(ctx context.Context, tables filterTables, query string, args []any, hyd sqlbuild.CountsHydration) ([]*types.IssueWithCounts, error) {\n\trows, err := r.runner.QueryContext(ctx, query, args...)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"search count %s: %w\", tables.Main, err)\n\t}\n\tdefer func() { _ = rows.Close() }()\n\n\tvar out []*types.IssueWithCounts\n\tseen := make(map[string]bool)\n\tfor rows.Next() {\n\t\tiwc, scanErr := scanReadyWorkRowWithCounts(rows, hyd)\n\t\tif scanErr != nil {\n\t\t\treturn nil, scanErr\n\t\t}\n\t\tif iwc == nil || iwc.Issue == nil {\n\t\t\tcontinue\n\t\t}\n\t\tif seen[iwc.Issue.ID] {\n\t\t\tcontinue\n\t\t}\n\t\tseen[iwc.Issue.ID] = true\n\t\tout = append(out, iwc)","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/domain/db/issue_search_counts.go#L165-L201","documentation":"Generic wrapper in scanCountsQuery for QueryContext failures of a counts mega-query; %s is the main table name of the filterTables set (issues, wisps, dependencies...). Called from both fetchCountsByIDs and runSearchQuery, it marks which table family's counts query failed at the engine.","triggerScenarios":"QueryContext fails on any counts mega-query — unknown table/column, engine syntax error, too many bound parameters, connection loss, or context deadline exceeded.","commonSituations":"Stale schema missing dependencies/wisp tables (outside the tolerated optional paths), very large ID chunks stressing parameter limits, remote Dolt connection drops, or timeouts on wide aggregate queries.","solutions":["Check the wrapped driver error message for the concrete cause","Run bd migrations / bd doctor to ensure all counts-related tables exist","Reduce hydration breadth (SkipLabels/SkipCounts/Lite) or page size","Increase context timeout; retry transient connection errors"],"exampleFix":"// before\nctx, cancel := context.WithTimeout(ctx, 2*time.Second)\n// after\nctx, cancel := context.WithTimeout(ctx, 30*time.Second)","handlingStrategy":"retry","validationCode":"for _, t := range []string{\"issues\", \"dependencies\", \"labels\", \"wisp_dependencies\"} {\n    if err := tableExists(ctx, db, t); err != nil {\n        return fmt.Errorf(\"missing table %s: %w\", t, err)\n    }\n}","typeGuard":null,"tryCatchPattern":"items, err := store.Search(ctx, q, filter)\nif err != nil {\n    var derr *driverError\n    if errors.As(err, &derr) && derr.Transient() {\n        items, err = store.Search(ctx, q, filter)\n    }\n}","preventionTips":["Run schema checks (bd doctor) before heavy counts queries","Cap ID batch sizes to stay under parameter limits","Use SkipLabels/SkipCounts/Lite for constrained environments","Apply context deadlines with retry on deadline-exceeded"],"tags":["sql","database","counts-query"],"backgroundTag":"sql-query-execution-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}