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.

perf(tasks): add an index on pr url for task run output (#60909)

Joshua Snydera326236e90d0
diff --git a/products/tasks/backend/migrations/0036_taskrun_output_pr_url_idx.py b/products/tasks/backend/migrations/0036_taskrun_output_pr_url_idx.py
new file mode 100644
index 000000000000..307ee466f087
--- /dev/null
+++ b/products/tasks/backend/migrations/0036_taskrun_output_pr_url_idx.py
@@ -0,0 +1,22 @@
+import django.db.models.fields.json
+from django.contrib.postgres.operations import AddIndexConcurrently
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("tasks", "0035_task_origin_product_signals_scout"),
+ ]
+
+ operations = [
+ AddIndexConcurrently(
+ model_name="taskrun",
+ index=models.Index(
+ django.db.models.fields.json.KeyTransform("pr_url", "output"),
+ name="task_run_output_pr_url_idx",
+ condition=models.Q(output__pr_url__isnull=False),
+ ),
+ ),
+ ]
diff --git a/products/tasks/backend/migrations/max_migration.txt b/products/tasks/backend/migrations/max_migration.txt
index a82311795ed6..969c9d1a58c4 100644
--- a/products/tasks/backend/migrations/max_migration.txt
+++ b/products/tasks/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0035_task_origin_product_signals_scout
+0036_taskrun_output_pr_url_idx
diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.py
index 9c9991feea9e..9d32c2cb1f0e 100644
--- a/products/tasks/backend/models.py
+++ b/products/tasks/backend/models.py
@@ -20,6 +20,7 @@ from django.conf import settings
from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError
from django.db import IntegrityError, models, transaction
+from django.db.models.fields.json import KeyTransform
from django.utils import timezone as django_timezone
import structlog
@@ -593,6 +594,18 @@ class TaskRun(models.Model):
class Meta:
db_table = "posthog_task_run"
ordering = ["-created_at"]
+ indexes = [
+ # 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
+ # runs without a PR URL (queued/in-progress/failed) while still serving
+ # the query.
+ models.Index(
+ KeyTransform("pr_url", "output"),
+ name="task_run_output_pr_url_idx",
+ condition=models.Q(output__pr_url__isnull=False),
+ ),
+ ]
def __str__(self):
return f"Run for {self.task.title} - {self.get_status_display()}"