{"record":{"id":"e61b76454d7620a1","repo":"crowdsecurity/crowdsec","slug":"unable-to-count-nb-alerts-w","errorCode":null,"errorMessage":"unable to count nb alerts: %w","messagePattern":"unable to count nb alerts: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/alerts.go","lineNumber":847,"sourceCode":"\t\t}\n\n\t\t// only if with_decisions is present and set to false, we exclude this\n\t\tif val, ok := filter[\"with_decisions\"]; ok && val[0] == \"false\" {\n\t\t\tc.Log.Debugf(\"skipping decisions\")\n\t\t} else {\n\t\t\talerts = alerts.\n\t\t\t\tWithDecisions()\n\t\t}\n\n\t\talerts = alerts.\n\t\t\tWithEvents().\n\t\t\tWithMetas().\n\t\t\tWithOwner()\n\n\t\tif limit == 0 {\n\t\t\tlimit, err = alerts.Count(ctx)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"unable to count nb alerts: %w\", err)\n\t\t\t}\n\t\t}\n\n\t\tif sort == \"ASC\" {\n\t\t\talerts = alerts.Order(ent.Asc(alert.FieldCreatedAt), ent.Asc(alert.FieldID))\n\t\t} else {\n\t\t\talerts = alerts.Order(ent.Desc(alert.FieldCreatedAt), ent.Desc(alert.FieldID))\n\t\t}\n\n\t\tresult, err := alerts.Limit(paginationSize).Offset(offset).All(ctx)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"pagination size: %d, offset: %d: %w: %w\", paginationSize, offset, err, QueryFail)\n\t\t}\n\n\t\tif len(result) == 0 { // no results, no need to try to paginate further\n\t\t\tc.Log.Debugf(\"Pagination done because no results found at offset %d | len(ret) %d\", offset, len(ret))\n\t\t\tbreak\n\t\t}","sourceCodeStart":829,"sourceCodeEnd":865,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/alerts.go#L829-L865","documentation":"When limit is 0 (unpaginated mode) QueryAlertWithFilter counts matching alerts first; this error wraps a failure of that Count query. The filter was valid but the database could not perform the count.","triggerScenarios":"alerts.Count(ctx) fails — DB unreachable, query timeout on a huge alerts table, or context cancelled.","commonSituations":"Millions of alerts making COUNT(*) slow; SQLite lock contention; DB restarting during a FlushAlerts run.","solutions":["Check the wrapped DB error and database server logs","Retry once connections recover","Use an explicit limit instead of 0 to avoid a full-table count","Consider pruning old alerts (cscli alerts delete --until) to shrink the table"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"alerts, err := client.FindAlerts(ctx, filter)\nif err != nil {\n    if errors.Is(err, context.DeadlineExceeded) {\n        // fall back to a paged query with explicit limit\n    }\n    return err\n}","preventionTips":["Avoid limit=0 on very large alert tables","Pass an explicit limit to skip the full COUNT","Prune alerts regularly (cscli alerts delete --until)"],"tags":["database","count","pagination"],"backgroundTag":"sql-query-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}