Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(tasks): index webhook task run lookups (#87779)

Sven Lange3f79e1fe142a
diff --git a/products/tasks/backend/migrations/0105_taskrun_webhook_lookup_indexes.py b/products/tasks/backend/migrations/0105_taskrun_webhook_lookup_indexes.py
new file mode 100644
index 000000000000..c9fd54c0ee5b
--- /dev/null
+++ b/products/tasks/backend/migrations/0105_taskrun_webhook_lookup_indexes.py
@@ -0,0 +1,39 @@
+from django.contrib.postgres.indexes import GinIndex, OpClass
+from django.db import migrations, models
+from django.db.models.fields.json import KeyTransform
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("posthog", "1315_githubinstallrequest_account"),
+ ("tasks", "0104_desktop_beta_terms_acceptance"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="taskrun",
+ index=GinIndex(
+ OpClass(KeyTransform("verified_pr_urls", "state"), name="jsonb_path_ops"),
+ name="task_run_verified_pr_urls_idx",
+ ),
+ ),
+ SafeAddIndexConcurrently(
+ model_name="taskrun",
+ index=GinIndex(
+ OpClass(KeyTransform("head_branches", "output"), name="jsonb_path_ops"),
+ name="task_run_head_branches_idx",
+ ),
+ ),
+ SafeAddIndexConcurrently(
+ model_name="taskrun",
+ index=models.Index(
+ fields=["branch"],
+ name="task_run_branch_idx",
+ condition=models.Q(branch__isnull=False),
+ ),
+ ),
+ ]
diff --git a/products/tasks/backend/migrations/max_migration.txt b/products/tasks/backend/migrations/max_migration.txt
index ff8c6609af0f..25bea1ad8b2a 100644
--- a/products/tasks/backend/migrations/max_migration.txt
+++ b/products/tasks/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0104_desktop_beta_terms_acceptance
+0105_taskrun_webhook_lookup_indexes
diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.py
index 0b170b236603..28b43672881f 100644
--- a/products/tasks/backend/models.py
+++ b/products/tasks/backend/models.py
@@ -20,7 +20,7 @@ if TYPE_CHECKING:
from django.conf import settings
from django.contrib.postgres.fields import ArrayField
-from django.contrib.postgres.indexes import GinIndex
+from django.contrib.postgres.indexes import GinIndex, OpClass
from django.core.exceptions import ValidationError
from django.db import IntegrityError, connection, models, transaction
from django.db.models.fields.json import KeyTransform
@@ -1987,6 +1987,19 @@ class TaskRun(models.Model):
db_table = "posthog_task_run"
ordering = ["-created_at"]
indexes = [
+ GinIndex(
+ OpClass(KeyTransform("verified_pr_urls", "state"), name="jsonb_path_ops"),
+ name="task_run_verified_pr_urls_idx",
+ ),
+ GinIndex(
+ OpClass(KeyTransform("head_branches", "output"), name="jsonb_path_ops"),
+ name="task_run_head_branches_idx",
+ ),
+ models.Index(
+ fields=["branch"],
+ name="task_run_branch_idx",
+ condition=models.Q(branch__isnull=False),
+ ),
# Partial functional index backing the per-PR-webhook lookup
# `filter(output__pr_url=...)`. The equality lookup implies the key is
# present, so the `IS NOT NULL` condition keeps the index off the many