{"record":{"id":"f58fa403a27c5e3b","repo":"crowdsecurity/crowdsec","slug":"pagination-size-d-offset-d-w-w","errorCode":null,"errorMessage":"pagination size: %d, offset: %d: %w: %w","messagePattern":"pagination size: (.+?), offset: (.+?): %w: %w","errorType":"exception","errorClass":"QueryFail","httpStatus":null,"severity":"error","filePath":"pkg/database/alerts.go","lineNumber":859,"sourceCode":"\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}\n\n\t\tlog.Debugf(\"QueryAlertWithFilter: pagination size %d, offset %d, got %d results\", paginationSize, offset, len(result))\n\t\tlog.Debugf(\"diff is %d, limit is %d\", limit-len(ret), limit)\n\n\t\tif diff := limit - len(ret); diff < paginationSize {\n\t\t\tif len(result) < diff {\n\t\t\t\tret = append(ret, result...)\n\t\t\t\tc.Log.Debugf(\"Pagination done, %d < %d\", len(result), diff)\n\n\t\t\t\tbreak\n\t\t\t}\n","sourceCodeStart":841,"sourceCodeEnd":877,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/alerts.go#L841-L877","documentation":"The paginated fetch (Limit/Offset All query) failed while walking alerts page by page. The message includes pagination size and offset plus both the underlying error and the QueryFail sentinel, double-wrapped with %w.","triggerScenarios":"alerts.Limit(paginationSize).Offset(offset).All(ctx) fails at some page during the loop — DB disconnect mid-pagination, context cancellation, or driver error.","commonSituations":"Long pagination loops over large alert tables outliving DB connection lifetime; context deadline exceeded while iterating; SQLite locking from a concurrent writer.","solutions":["Read the first wrapped error in the chain for the true cause (errors.Unwrap twice)","Retry with a smaller paginationSize or explicit limit","Increase the context timeout for large exports (cscli alerts list)","Check DB stability across the whole pagination loop"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"_, err := client.FindAlerts(ctx, filter)\nif err != nil {\n    var qf interface{ Unwrap() error }\n    // double-wrapped: inspect both wrapped errors\n    return fmt.Errorf(\"alert listing failed: %w\", err)\n}","preventionTips":["Use a context timeout that covers the full pagination loop","Prefer explicit limits over unbounded listings","Keep DB connections alive across long exports"],"tags":["database","pagination","query"],"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-14T05:17:10.506Z"}