bytebase/bytebase · error

failed to create task run log

Error message

failed to create task run log

What it means

This error wraps a failure from database ExecContext when executing the INSERT that persists a task run log in CreateTaskRunLog (backend/store/task_run_log.go:55). The SQL itself built fine; the database rejected or could not complete the write. Typical causes are connection failures, constraint violations (e.g. foreign key to task_run), schema drift, or a canceled/timed-out context. The underlying pq/pgx error is preserved by the errors.Wrapf wrapper.

Source

Thrown at backend/store/task_run_log.go:55

			project,
			task_run_id,
			created_at,
			payload
		) VALUES (
			?,
			?,
			?,
			?
		)
	`, projectID, taskRunUID, t, p)

	sql, args, err := q.ToSQL()
	if err != nil {
		return errors.Wrapf(err, "failed to build sql")
	}

	if _, err := s.GetDB().ExecContext(ctx, sql, args...); err != nil {
		return errors.Wrapf(err, "failed to create task run log")
	}
	return nil
}

func (s *Store) ListTaskRunLogs(ctx context.Context, projectID string, taskRunUID int64) ([]*TaskRunLog, error) {
	// created_at can tie across entries (no per-row sequence exists); ctid
	// breaks ties in insertion order, which is stable because the table is
	// append-only and rows are never updated.
	q := qb.Q().Space(`
		SELECT
			created_at,
			payload
		FROM task_run_log
		WHERE task_run_log.project = ? AND task_run_log.task_run_id = ?
		ORDER BY created_at, ctid
	`, projectID, taskRunUID)

	sql, args, err := q.ToSQL()

View on GitHub (pinned to 1870550677)

Solutions

  1. Read the wrapped underlying error (use errors.Cause or %v of the wrapped error) to identify the database's specific complaint
  2. Verify the metadata database is reachable and PG_URL is correct (psql -U bbdev bbdev)
  3. Confirm migrations are applied: check task_run_log exists in backend/migrator/migration/LATEST.sql and matches the live schema
  4. If it is an FK violation, ensure the referenced task_run row exists and is not deleted before logging
  5. If it is a timeout/cancel, retry the log write or increase the context deadline

Example fix

// before
if _, err := s.GetDB().ExecContext(ctx, sql, args...); err != nil {
	return errors.Wrapf(err, "failed to create task run log")
}
// after
if _, err := s.GetDB().ExecContext(ctx, sql, args...); err != nil {
	return errors.Wrapf(err, "failed to create task run log: sql=%s args=%v", sql, args) // log statement for diagnosis; consider a bounded retry for transient errors
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-checks before inserting:
if projectID == "" || taskRunUID <= 0 || e == nil {
	return errors.New("invalid task run log arguments")
}
// Ensure the referenced task run exists if FK enforcement matters:
// SELECT 1 FROM task_run WHERE project = ? AND id = ?

Try / catch

if err := s.CreateTaskRunLog(ctx, projectID, taskRunUID, t, replicaID, e); err != nil {
	slog.Error("create task run log failed", log.BBError(err))
	// Distinguish transient (connection/timeout) from permanent (constraint) via pgconn.PgError code
	var pgErr *pgconn.PgError
	if errors.As(err, &pgErr) && pgErr.Code == "23503" {
		// FK violation: parent task_run missing, do not retry
	}
}

Prevention

When it happens

Trigger: Calling CreateTaskRunLog (directly or via CreateTaskRunLogS) when the metadata Postgres is unreachable, the task_run_id has no matching task_run row (FK violation), the task_run_log table is missing or has a drifted schema, or ctx is canceled before the write completes.

Common situations: Metadata database down or restarted mid-run; wrong PG_URL; migration not applied so task_run_log columns differ; inserting a log for a task run that was deleted concurrently; connection pool exhaustion under load; context deadline exceeded during long rollouts.

Related errors


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