{"record":{"id":"cf67c5d2ef4a412a","repo":"bytebase/bytebase","slug":"failed-to-create-task-run-log","errorCode":null,"errorMessage":"failed to create task run log","messagePattern":"failed to create task run log","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/store/task_run_log.go","lineNumber":55,"sourceCode":"\t\t\tproject,\n\t\t\ttask_run_id,\n\t\t\tcreated_at,\n\t\t\tpayload\n\t\t) VALUES (\n\t\t\t?,\n\t\t\t?,\n\t\t\t?,\n\t\t\t?\n\t\t)\n\t`, projectID, taskRunUID, t, p)\n\n\tsql, args, err := q.ToSQL()\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"failed to build sql\")\n\t}\n\n\tif _, err := s.GetDB().ExecContext(ctx, sql, args...); err != nil {\n\t\treturn errors.Wrapf(err, \"failed to create task run log\")\n\t}\n\treturn nil\n}\n\nfunc (s *Store) ListTaskRunLogs(ctx context.Context, projectID string, taskRunUID int64) ([]*TaskRunLog, error) {\n\t// created_at can tie across entries (no per-row sequence exists); ctid\n\t// breaks ties in insertion order, which is stable because the table is\n\t// append-only and rows are never updated.\n\tq := qb.Q().Space(`\n\t\tSELECT\n\t\t\tcreated_at,\n\t\t\tpayload\n\t\tFROM task_run_log\n\t\tWHERE task_run_log.project = ? AND task_run_log.task_run_id = ?\n\t\tORDER BY created_at, ctid\n\t`, projectID, taskRunUID)\n\n\tsql, args, err := q.ToSQL()","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/store/task_run_log.go#L37-L73","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the wrapped underlying error (use errors.Cause or %v of the wrapped error) to identify the database's specific complaint","Verify the metadata database is reachable and PG_URL is correct (psql -U bbdev bbdev)","Confirm migrations are applied: check task_run_log exists in backend/migrator/migration/LATEST.sql and matches the live schema","If it is an FK violation, ensure the referenced task_run row exists and is not deleted before logging","If it is a timeout/cancel, retry the log write or increase the context deadline"],"exampleFix":"// before\nif _, err := s.GetDB().ExecContext(ctx, sql, args...); err != nil {\n\treturn errors.Wrapf(err, \"failed to create task run log\")\n}\n// after\nif _, err := s.GetDB().ExecContext(ctx, sql, args...); err != nil {\n\treturn 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\n}","handlingStrategy":"try-catch","validationCode":"// Pre-checks before inserting:\nif projectID == \"\" || taskRunUID <= 0 || e == nil {\n\treturn errors.New(\"invalid task run log arguments\")\n}\n// Ensure the referenced task run exists if FK enforcement matters:\n// SELECT 1 FROM task_run WHERE project = ? AND id = ?","typeGuard":null,"tryCatchPattern":"if err := s.CreateTaskRunLog(ctx, projectID, taskRunUID, t, replicaID, e); err != nil {\n\tslog.Error(\"create task run log failed\", log.BBError(err))\n\t// Distinguish transient (connection/timeout) from permanent (constraint) via pgconn.PgError code\n\tvar pgErr *pgconn.PgError\n\tif errors.As(err, &pgErr) && pgErr.Code == \"23503\" {\n\t\t// FK violation: parent task_run missing, do not retry\n\t}\n}","preventionTips":["Keep the metadata Postgres healthy and monitor PG_URL connectivity","Apply migrations before deploying so task_run_log schema matches LATEST.sql","Never delete task_run rows that still have logs, or delete logs in the same transaction (respect task_run_log -> task_run lock order)","Pass contexts with sane deadlines and log the full wrapped error chain"],"tags":["go","database","postgres","insert","connection"],"backgroundTag":"database-write-failed","analyzedSha":"1870550677fe08f0d2a78c07acd27541464eb945","analyzedAt":"2026-09-06T21:16:13.665Z","contentChangedAt":"2026-09-06T21:16:13.665Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}