{"record":{"id":"97262baee492e6e0","repo":"jackc/pgx","slug":"batch-already-closed","errorCode":null,"errorMessage":"batch already closed","messagePattern":"batch already closed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"batch.go","lineNumber":135,"sourceCode":"\ntype batchResults struct {\n\tctx       context.Context\n\tconn      *Conn\n\tmrr       *pgconn.MultiResultReader\n\terr       error\n\tb         *Batch\n\tqqIdx     int\n\tclosed    bool\n\tendTraced bool\n}\n\n// Exec reads the results from the next query in the batch as if the query has been sent with Exec.\nfunc (br *batchResults) Exec() (pgconn.CommandTag, error) {\n\tif br.err != nil {\n\t\treturn pgconn.CommandTag{}, br.err\n\t}\n\tif br.closed {\n\t\treturn pgconn.CommandTag{}, fmt.Errorf(\"batch already closed\")\n\t}\n\n\tquery, arguments, _ := br.nextQueryAndArgs()\n\n\tif !br.mrr.NextResult() {\n\t\terr := br.mrr.Close()\n\t\tif err == nil {\n\t\t\terr = errors.New(\"no more results in batch\")\n\t\t}\n\t\tif br.conn.batchTracer != nil {\n\t\t\tbr.conn.batchTracer.TraceBatchQuery(br.ctx, br.conn, TraceBatchQueryData{\n\t\t\t\tSQL:  query,\n\t\t\t\tArgs: arguments,\n\t\t\t\tErr:  err,\n\t\t\t})\n\t\t}\n\t\treturn pgconn.CommandTag{}, err\n\t}","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/batch.go#L117-L153","documentation":"batchResults guard: Exec/Query was called on a batchResults that has already been closed — its results were consumed (or abandoned) and the underlying MultiResultReader is finished. The stale batchResults handle is the faulty input; each result can only be read once.","triggerScenarios":"Thrown at batch.go:135 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Read each batch result exactly once, before calling Close","Use Exec/Query return values rather than re-querying the same batchResults","Track the batch lifecycle explicitly when conditionally consuming results"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}