{"record":{"id":"52ed60a556be88b5","repo":"bytebase/bytebase","slug":"failed-to-create-pending-task-runs","errorCode":null,"errorMessage":"failed to create pending task runs","messagePattern":"failed to create pending task runs","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/store/task_run.go","lineNumber":497,"sourceCode":"\t\t\tcandidates.project,\n\t\t\tcandidates.task_id,\n\t\t\tcandidates.run_at,\n\t\t\tCOALESCE((SELECT MAX(attempt) + 1 FROM task_run WHERE task_run.task_id = candidates.task_id AND task_run.project = candidates.project), 0),\n\t\t\t?,\n\t\t\t?\n\t\tFROM candidates\n\t\tON CONFLICT (project, task_id, attempt) DO NOTHING\n\t`, projects, taskUIDs, runAts, baseID,\n\t\tstorepb.TaskRun_PENDING.String(), storepb.TaskRun_AVAILABLE.String(), storepb.TaskRun_RUNNING.String(), storepb.TaskRun_DONE.String(),\n\t\tcreatorPtr, storepb.TaskRun_PENDING.String(), payloadStr)\n\n\tquery, args, err := q.ToSQL()\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"failed to build sql\")\n\t}\n\n\tif _, err := tx.ExecContext(ctx, query, args...); err != nil {\n\t\treturn errors.Wrapf(err, \"failed to create pending task runs\")\n\t}\n\n\tif err := tx.Commit(); err != nil {\n\t\treturn errors.Wrapf(err, \"failed to commit tx\")\n\t}\n\n\treturn nil\n}\n\nfunc (s *Store) listTaskRunCreationInstances(ctx context.Context, projects []string, taskUIDs []int64) ([]string, error) {\n\tq := qb.Q().Space(`\n\t\tSELECT DISTINCT task.instance\n\t\tFROM (\n\t\t\tSELECT\n\t\t\t\tunnest(CAST(? AS TEXT[])) AS project,\n\t\t\t\tunnest(CAST(? AS BIGINT[])) AS task_id\n\t\t) requested_tasks\n\t\tJOIN task ON task.project = requested_tasks.project AND task.id = requested_tasks.task_id","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/store/task_run.go#L479-L515","documentation":"The batch INSERT of pending task runs failed when executed inside the transaction (tx.ExecContext), and the store wraps the driver error as \"failed to create pending task runs\". The transaction is not committed, so no task_run rows are persisted. The underlying PostgreSQL error (constraint violation, FK failure, connection issue) is attached as the cause.","triggerScenarios":"Executing the batch INSERT at task_run.go:486 with a task_id/project that does not exist in the task table (FK violation); payload JSON rejected by the payload column; database unreachable or statement timeout; unique conflict other than the ON CONFLICT (project, task_id, attempt) case handled by DO NOTHING (e.g. id conflicts).","commonSituations":"Scheduling task runs for an archived instance whose task rows were removed concurrently; metadata DB under connectivity trouble during rollout creation; passing a payload string that violates a JSONB check; running against a schema version older than the code expects (missing columns).","solutions":["Read the wrapped driver error in the log — it names the exact constraint, FK, or connection failure","Confirm all (project, task_id) pairs passed in still exist and are not skipped/archived","Check metadata database health (PG_URL connectivity, migrations up to date)","Retry the operation — the transaction rolled back atomically so state is clean","Add ON CONFLICT coverage or pre-checks if id-base allocation races with concurrent creators"],"exampleFix":"// before: fire-and-forget scheduling\nif err := store.CreatePendingTaskRuns(ctx, projects, uids, runAts); err != nil {\n\treturn err\n}\n// after: pre-validate tasks exist before scheduling\nif err := ensureTasksActive(ctx, store, projects, uids); err != nil {\n\treturn errors.Wrap(err, \"cannot schedule task runs\")\n}\nif err := store.CreatePendingTaskRuns(ctx, projects, uids, runAts); err != nil {\n\treturn errors.Wrap(err, \"cannot schedule task runs\")\n}","handlingStrategy":"try-catch","validationCode":"// pre-check tasks exist and are not skipped\nfor i := range taskUIDs {\n\tif _, err := store.GetTask(ctx, projects[i], taskUIDs[i]); err != nil {\n\t\treturn errors.Wrapf(err, \"task %d/%s does not exist\", taskUIDs[i], projects[i])\n\t}\n}","typeGuard":null,"tryCatchPattern":"if _, err := tx.ExecContext(ctx, query, args...); err != nil {\n\ttx.Rollback()\n\treturn errors.Wrapf(err, \"failed to create pending task runs\")\n}","preventionTips":["Validate all task IDs exist before scheduling","Ensure the metadata schema is current before deploying","Check DB connectivity/health before large batch operations"],"tags":["database","postgres","transaction"],"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"}