fix(engine): scheduled workflow idempotency insert conflict - #4454
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
abelanger5
approved these changes
Jul 17, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a concurrency bug in scheduled workflow execution by making idempotency key claiming safe under concurrent callers, and aligning the database schema/queries to avoid insert conflicts.
Changes:
- Update idempotency key schema to use
(tenant_id, key)as the primary key and add an index to support expiry cleanup. - Remove
SKIP LOCKEDfrom the idempotency claim query to prevent concurrent “missing row” paths that can lead to duplicate inserts. - Ensure scheduled workflow runs use a unique run external ID as the claimant, and add a regression test to verify only one MQ publish occurs under concurrency.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| sql/schema/v1-core.sql | Adjusts v1_idempotency_key primary key and adds an expiry index in the canonical schema. |
| pkg/repository/sqlcv1/idempotency-keys.sql.go | Regenerates SQLC output to remove SKIP LOCKED from the locking query. |
| pkg/repository/sqlcv1/idempotency-keys.sql | Updates the source SQL used by SQLC to remove SKIP LOCKED. |
| pkg/repository/idempotency.go | Adds a constructor used by the new ticker test to build an idempotency repo from a pool. |
| internal/services/ticker/schedule_workflow_v1.go | Uses a per-run UUID as the claimant/external ID to prevent duplicate runs across repeated calls. |
| internal/services/ticker/schedule_workflow_v1_test.go | Adds a concurrency regression test asserting only one message is published. |
| cmd/hatchet-migrate/migrate/migrations/20260717150345_v1_0_131.sql | Migration to shift the PK to (tenant_id, key) and add the expiry index. |
Files not reviewed (1)
- pkg/repository/sqlcv1/idempotency-keys.sql.go: Generated file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Seeing some of these
2026-07-17T12:30:53.699Z ERR could not run scheduled workflow error="could not claim idempotency key for scheduled workflow: ERROR: duplicate key value violates unique constraint \"v1_idempotency_key_pkey\" (SQLSTATE 23505)" service=tickererrors on cloudPretty sure the issue here is that we're attempting two concurrent inserts. I think the root of the problem was using
SKIP LOCKEDto see which rows to lock, since if a row was locked we'd end up trying to do another insert, which would then fail. Removed that. Then I also updated the schema to change the PK so we don't have overlapping indexes, which I think should also helpThen there was one other issue too, which was that we could spawn duplicate runs if this method was called multiple times since the
claimed_by_external_idwas set upstream (we were usingopts.Id) and instead, we should use a unique id so the run's external id is the claimant, not the external id of the schedule.Also added a regression test which asserts we only pub to the MQ once
Type of change
Checklist
Changes have been:
🤖 AI Disclosure
No AI use