{"record":{"id":"f396d2f1c4bdbdab","repo":"bytebase/bytebase","slug":"failed-to-begin-transaction-f396d2","errorCode":null,"errorMessage":"failed to begin transaction","messagePattern":"failed to begin transaction","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/store/sheet.go","lineNumber":280,"sourceCode":"\t\treturn nil, errors.New(\"project is required to create sheets\")\n\t}\n\tif len(creates) == 0 {\n\t\treturn creates, nil\n\t}\n\n\tvar statements []string\n\tvar sha256s [][]byte\n\tfor _, c := range creates {\n\t\tstatements = append(statements, c.Statement)\n\t\th := sha256.Sum256([]byte(c.Statement))\n\t\tc.Sha256 = hex.EncodeToString(h[:])\n\t\tsha256s = append(sha256s, h[:])\n\t\tc.Size = int64(len(c.Statement))\n\t}\n\n\ttx, err := s.GetDB().BeginTx(ctx, nil)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"failed to begin transaction\")\n\t}\n\tdefer tx.Rollback()\n\n\tif err := requireActiveProject(ctx, tx, projectID); err != nil {\n\t\treturn nil, err\n\t}\n\n\tq := qb.Q().Space(`\n\t\tINSERT INTO sheet_blob (\n\t\t\tsha256,\n\t\t\tcontent\n\t\t) SELECT\n\t\t \tunnest(CAST(? AS BYTEA[])),\n\t\t\tunnest(CAST(? AS TEXT[]))\n\t\tON CONFLICT DO NOTHING\n\t`, sha256s, statements)\n\tquery, args, err := q.ToSQL()\n\tif err != nil {","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/store/sheet.go#L262-L298","documentation":"CreateSheets in backend/store/sheet.go opens a metadata-database transaction with BeginTx before inserting sheet blobs and refs. If the database connection cannot start a transaction (connection dropped, pool exhausted, DB down), the underlying driver error is wrapped as \"failed to begin transaction\". Nothing was written yet — the operation failed before any SQL ran.","triggerScenarios":"Calling CreateSheet/BatchCreateSheets/CreateRelease while the metadata Postgres is unreachable, the connection pool is exhausted, the connection was closed by the server, or during a database restart/failover.","commonSituations":"Metadata DB restarted or failed over; network blip between Bytebase and Postgres; connection idle timeout killing pooled connections; too many concurrent transactions exhausting the pool or max_connections.","solutions":["Check the wrapped driver error (errors.Unwrap / %v of the full error) to see the root cause — connection refused, pool timeout, etc.","Verify the metadata database is running and reachable: `psql -U bbdev bbdev` or check PG_URL / external DB config.","Retry the operation; transient connection failures often resolve after failover completes.","If pool exhaustion, reduce concurrency or raise the connection pool size / Postgres max_connections."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Pre-flight connectivity check before batch operations\nrows, err := db.QueryContext(ctx, \"SELECT 1\")\nif err != nil {\n\treturn fmt.Errorf(\"metadata database unreachable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"var sheets *[]*Sheet\nerr := retry.Do(func() error {\n\ts, err := store.CreateSheets(ctx, project, input)\n\tif err != nil && strings.Contains(err.Error(), \"failed to begin transaction\") {\n\t\treturn retry.RetryableError(err) // transient DB issue\n\t}\n\tif err != nil { return retry.UnrecoverableError(err) }\n\tsheets = &s\n\treturn nil\n}, retry.Attempts(3), retry.Delay(time.Second))","preventionTips":["Monitor metadata database availability and failover status","Set sane pool sizes and connection lifetimes to avoid stale connections","Retry idempotent write paths like sheet creation on transient begin failures","Alert on connection-refused/pool-timeout errors from the metadata DB"],"tags":["database","transaction","postgres","connectivity"],"backgroundTag":"connection-refused","analyzedSha":"1870550677fe08f0d2a78c07acd27541464eb945","analyzedAt":"2026-09-06T21:16:13.665Z","contentChangedAt":"2026-09-06T21:16:13.665Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}