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): index the two task columns the SET_NULL cascades filter on (#94749)

posthog[bot]6fa8c3b7dcbd
diff --git a/products/tasks/backend/migrations/0117_task_set_null_cascade_indexes.py b/products/tasks/backend/migrations/0117_task_set_null_cascade_indexes.py
new file mode 100644
index 000000000000..3fd04d04f24f
--- /dev/null
+++ b/products/tasks/backend/migrations/0117_task_set_null_cascade_indexes.py
@@ -0,0 +1,26 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ # posthog_task is large enough that a plain CREATE INDEX would hold an ACCESS EXCLUSIVE
+ # lock for the whole build. CONCURRENTLY cannot run inside a transaction.
+ atomic = False
+
+ dependencies = [
+ ("tasks", "0116_drop_task_session_storage_key_like_index"),
+ ]
+
+ operations = [
+ # Both columns are only filtered on their own by the SET_NULL cascade Django emits
+ # when a User or a UserIntegration row is deleted.
+ SafeAddIndexConcurrently(
+ model_name="task",
+ index=models.Index(fields=["created_by"], name="posthog_task_creator_idx"),
+ ),
+ SafeAddIndexConcurrently(
+ model_name="task",
+ index=models.Index(fields=["github_user_integration"], name="posthog_task_gh_user_int_idx"),
+ ),
+ ]
diff --git a/products/tasks/backend/migrations/max_migration.txt b/products/tasks/backend/migrations/max_migration.txt
index 6d052cdaf8cd..3f19dbad8fb8 100644
--- a/products/tasks/backend/migrations/max_migration.txt
+++ b/products/tasks/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0116_drop_task_session_storage_key_like_index
+0117_task_set_null_cascade_indexes
diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.py
index b2cd1fda596f..944537ba236f 100644
--- a/products/tasks/backend/models.py
+++ b/products/tasks/backend/models.py
@@ -499,6 +499,10 @@ class Task(DeletedMetaFields, models.Model):
),
models.Index(fields=["team", "-created_at", "-id"], name="posthog_task_team_created_idx"),
models.Index(fields=["team", "created_by", "-created_at", "-id"], name="posthog_task_team_creator_idx"),
+ # Single-column, so the SET_NULL cascades can seek them. The composite index
+ # above leads with `team`, so a filter on `created_by` alone cannot use it.
+ models.Index(fields=["created_by"], name="posthog_task_creator_idx"),
+ models.Index(fields=["github_user_integration"], name="posthog_task_gh_user_int_idx"),
models.Index(fields=["channel", "-created_at"], name="posthog_task_channel_feed_idx"),
models.Index(
fields=["team", "internal", "archived", "-last_activity_at", "-id"],