Skip to content

fix: update trigger for runs status and update schema for consistency - #4426

Merged
grutt merged 2 commits into
mainfrom
belanger/runs-status-update-triggers
Jul 14, 2026
Merged

fix: update trigger for runs status and update schema for consistency#4426
grutt merged 2 commits into
mainfrom
belanger/runs-status-update-triggers

Conversation

@abelanger5

Copy link
Copy Markdown
Contributor

Description

Updates the trigger on v1_runs_status so that it updates the corresponding v1_statuses_olap table using an INSERT INTO...ON CONFLICT instead of an UPDATE.

Note the following p99 reduction in latency (milliseconds) on UpdateTaskStatusesFromMQ after this fix was applied:

image

This particular database runs TimescaleDB and v1_statuses_olap is a hypertable. When running query plans simulating this trigger, we saw that every chunk of the hypertable was being index scanned when calling the UPDATE. This seems internal to Timescale's partitioning implementation; we don't see the same thing on updates of our partitioned tables.

This was ultimately the cause of extremely heavy LWLock:BufferContent (called lwlock:buffer_content in some systems) due to the index scan touching every partition across 30 days of retention.

Type of change

  • Refactor (non-breaking changes to code which doesn't change any behaviour)

What's Changed

  • Updates the v1_runs_olap_status_update_function to use INSERT INTO...ON CONFLICT instead of UPDATE

Checklist

Changes have been:

  • Tested (unit, integration, or manually with steps specified)
  • Linted and formatted

🤖 AI Disclosure
  • I acknowledge that an LLM was used in the creation of this Pull Request, in accordance with Hatchet's AI_POLICY.md.
  • Details: Claude Fable for implementation

@vercel

vercel Bot commented Jul 14, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
hatchet-docs Ready Ready Preview, Comment Jul 14, 2026 10:09pm

Request Review

@github-actions github-actions Bot added the engine Related to the core Hatchet engine label Jul 14, 2026
Comment thread sql/schema/v1-olap.sql
parent_task_external_id UUID,

PRIMARY KEY (inserted_at, id, readable_status, kind)
PRIMARY KEY (inserted_at, id)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by: cleanup

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the v1_runs_olap_status_update_function trigger path by switching from an UPDATE ... FROM to an INSERT ... ON CONFLICT DO UPDATE, aiming to avoid TimescaleDB hypertable-wide scans and reduce contention/latency. It also updates the OLAP schema file to stay consistent with the current table keying.

Changes:

  • Rewrites v1_runs_olap_status_update_function to use an upsert into v1_statuses_olap (instead of UPDATE ... FROM).
  • Adds a new migration to apply the trigger-function rewrite to existing databases.
  • Updates sql/schema/v1-olap.sql to use the (inserted_at, id) primary key for v1_runs_olap for schema consistency.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
sql/schema/v1-olap.sql Updates v1_runs_olap primary key for consistency and rewrites the status-update trigger function to an upsert.
cmd/hatchet-migrate/migrate/migrations/20260714120000_v1_0_126.sql Migration to apply the upsert-based trigger function change (with a down migration restoring the prior update behavior).

Comment thread sql/schema/v1-olap.sql Outdated
Comment on lines +868 to +876
SELECT
external_id,
inserted_at,
tenant_id,
workflow_id,
kind,
readable_status
FROM new_rows
ON CONFLICT (external_id, inserted_at) DO UPDATE
Comment on lines +20 to +28
SELECT
external_id,
inserted_at,
tenant_id,
workflow_id,
kind,
readable_status
FROM new_rows
ON CONFLICT (external_id, inserted_at) DO UPDATE
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Optional test failure: The load-deadlock job failed on this PR (optimistic-scheduling=true). This check is non-mandatory and does not block merging, but may be worth investigating. View logs

@grutt
grutt merged commit 2ea5926 into main Jul 14, 2026
52 of 53 checks passed
@grutt
grutt deleted the belanger/runs-status-update-triggers branch July 14, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engine Related to the core Hatchet engine

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants