bytebase/bytebase · error

failed to begin transaction

Error message

failed to begin transaction

What it means

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.

Solutions

  1. Check the wrapped driver error (errors.Unwrap / %v of the full error) to see the root cause — connection refused, pool timeout, etc.
  2. Verify the metadata database is running and reachable: `psql -U bbdev bbdev` or check PG_URL / external DB config.
  3. Retry the operation; transient connection failures often resolve after failover completes.
  4. If pool exhaustion, reduce concurrency or raise the connection pool size / Postgres max_connections.
Defensive patterns

Strategy: retry

Validate before calling

// Pre-flight connectivity check before batch operations
rows, err := db.QueryContext(ctx, "SELECT 1")
if err != nil {
	return fmt.Errorf("metadata database unreachable: %w", err)
}

Try / catch

var sheets *[]*Sheet
err := retry.Do(func() error {
	s, err := store.CreateSheets(ctx, project, input)
	if err != nil && strings.Contains(err.Error(), "failed to begin transaction") {
		return retry.RetryableError(err) // transient DB issue
	}
	if err != nil { return retry.UnrecoverableError(err) }
	sheets = &s
	return nil
}, retry.Attempts(3), retry.Delay(time.Second))

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of bytebase/bytebase@1870550677 (2026-09-06). Data as JSON: /api/errors/f396d2f1c4bdbdab. Report an issue: GitHub.

Appendix: source

Thrown at backend/store/sheet.go:280

		return nil, errors.New("project is required to create sheets")
	}
	if len(creates) == 0 {
		return creates, nil
	}

	var statements []string
	var sha256s [][]byte
	for _, c := range creates {
		statements = append(statements, c.Statement)
		h := sha256.Sum256([]byte(c.Statement))
		c.Sha256 = hex.EncodeToString(h[:])
		sha256s = append(sha256s, h[:])
		c.Size = int64(len(c.Statement))
	}

	tx, err := s.GetDB().BeginTx(ctx, nil)
	if err != nil {
		return nil, errors.Wrap(err, "failed to begin transaction")
	}
	defer tx.Rollback()

	if err := requireActiveProject(ctx, tx, projectID); err != nil {
		return nil, err
	}

	q := qb.Q().Space(`
		INSERT INTO sheet_blob (
			sha256,
			content
		) SELECT
		 	unnest(CAST(? AS BYTEA[])),
			unnest(CAST(? AS TEXT[]))
		ON CONFLICT DO NOTHING
	`, sha256s, statements)
	query, args, err := q.ToSQL()
	if err != nil {

View on GitHub (pinned to 1870550677)