{"record":{"id":"cc46963b235a126c","repo":"gastownhall/beads","slug":"count-events-w","errorCode":null,"errorMessage":"count events: %w","messagePattern":"count events: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/issueops/delete.go","lineNumber":231,"sourceCode":"\t\treturn nil, fmt.Errorf(\"count dependencies: %w\", err)\n\t}\n\twispDepsCount, err := CountRowsForIssueIDsInTx(ctx, tx, \"wisp_dependencies\", set.WispIDs)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"count wisp dependencies: %w\", err)\n\t}\n\tdepsCount += wispDepsCount\n\n\tif labelsCount, err = CountRowsForIssueIDsInTx(ctx, tx, \"labels\", set.RegularIDs); err != nil {\n\t\treturn nil, fmt.Errorf(\"count labels: %w\", err)\n\t}\n\twispLabelsCount, err := CountRowsForIssueIDsInTx(ctx, tx, \"wisp_labels\", set.WispIDs)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"count wisp labels: %w\", err)\n\t}\n\tlabelsCount += wispLabelsCount\n\n\tif eventsCount, err = CountRowsForIssueIDsInTx(ctx, tx, \"events\", set.RegularIDs); err != nil {\n\t\treturn nil, fmt.Errorf(\"count events: %w\", err)\n\t}\n\twispEventsCount, err := CountRowsForIssueIDsInTx(ctx, tx, \"wisp_events\", set.WispIDs)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"count wisp events: %w\", err)\n\t}\n\teventsCount += wispEventsCount\n\n\tfor i := 0; i < len(set.All); i += deleteBatchSize {\n\t\tend := i + deleteBatchSize\n\t\tif end > len(set.All) {\n\t\t\tend = len(set.All)\n\t\t}\n\t\tbatch := set.All[i:end]\n\t\tbatchInClause, batchArgs := buildSQLInClause(batch)\n\n\t\tfor _, depTable := range []string{\"dependencies\", \"wisp_dependencies\"} {\n\t\t\trows, err := tx.QueryContext(ctx,\n\t\t\t\tfmt.Sprintf(`SELECT issue_id FROM %s WHERE %s`, depTable, depTargetIn(\"\", batchInClause)),","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/issueops/delete.go#L213-L249","documentation":"DeleteResolvedSetInTx aborts when the SELECT COUNT over the \"events\" table for regular issue IDs fails. The error wraps the driver error from CountRowsForIssueIDsInTx; the count populates the events figure of the dry-run delete result.","triggerScenarios":"Calling DeleteIssuesInTx/DeleteInTx when events is missing/unreadable, the transaction connection dropped after the label counts, or ctx was cancelled before this step.","commonSituations":"Very large events tables causing count timeouts; connection reset by Dolt server; lock contention with concurrent writers on events.","solutions":["Unwrap the error for the underlying cause (timeout vs connection vs schema).","Retry the delete; transient lock/timeout failures usually clear.","Raise the context timeout for deletes touching many issues with many events.","Verify the events table exists and is healthy (e.g. via bd doctor)."],"exampleFix":"// before\nres, err := store.DeleteInTx(ctx, tx, ids)\nif err != nil {\n    log.Fatal(err)\n}\n// after\nres, err := store.DeleteInTx(ctx, tx, ids)\nif err != nil {\n    log.Printf(\"delete failed: %v\", err)\n    if isTransient(err) {\n        res, err = store.DeleteInTx(ctx, tx, ids) // retry\n    }\n}","handlingStrategy":"retry","validationCode":"var n int\nerr := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM events LIMIT 1`).Scan(&n)\nif err != nil {\n    return fmt.Errorf(\"events table unreadable before delete: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"res, err := store.DeleteIssuesInTx(ctx, tx, ids)\nif err != nil {\n    if errors.Is(err, context.DeadlineExceeded) {\n        ctx, cancel = context.WithTimeout(context.Background(), longerTimeout)\n        defer cancel()\n        res, err = store.DeleteIssuesInTx(ctx, tx, ids)\n    }\n}","preventionTips":["Size context timeouts for the events-table volume of the issues being deleted.","Monitor Dolt server health for query kills/timeouts.","Split very large deletes into smaller batches."],"tags":["go","storage","transaction","sql","batch-delete"],"backgroundTag":"sql-count-query-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}