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 task list around the filters it always applies (#93008)

posthog[bot]3eb07c0ad165
diff --git a/products/tasks/backend/migrations/0113_task_team_live_list_indexes.py b/products/tasks/backend/migrations/0113_task_team_live_list_indexes.py
new file mode 100644
index 000000000000..91f4b5edc1da
--- /dev/null
+++ b/products/tasks/backend/migrations/0113_task_team_live_list_indexes.py
@@ -0,0 +1,36 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently, SafeRemoveIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ # CONCURRENTLY so building and dropping the indexes does not take an ACCESS EXCLUSIVE
+ # lock on posthog_task. Concurrent index work cannot run in a transaction, so the
+ # migration is non-atomic.
+ atomic = False
+
+ dependencies = [
+ ("tasks", "0112_taskrun_terminal_updated_idx"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="task",
+ index=models.Index(
+ fields=["team", "internal", "archived", "-created_at", "-id"],
+ condition=models.Q(deleted=False),
+ name="posthog_task_team_live_crt_idx",
+ ),
+ ),
+ SafeAddIndexConcurrently(
+ model_name="task",
+ index=models.Index(
+ fields=["team", "internal", "archived", "-last_activity_at", "-id"],
+ condition=models.Q(deleted=False),
+ name="posthog_task_team_live_act_idx",
+ ),
+ ),
+ # Keep the broad ordering indexes for callers that intentionally leave
+ # `internal` or `archived` unconstrained.
+ SafeRemoveIndexConcurrently(model_name="task", name="posthog_task_archived_idx"),
+ ]
diff --git a/products/tasks/backend/migrations/max_migration.txt b/products/tasks/backend/migrations/max_migration.txt
index 3dfe9727367c..aef20cb90776 100644
--- a/products/tasks/backend/migrations/max_migration.txt
+++ b/products/tasks/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0112_taskrun_terminal_updated_idx
+0113_task_team_live_list_indexes
diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.py
index 0c202783786d..a959e1b13982 100644
--- a/products/tasks/backend/models.py
+++ b/products/tasks/backend/models.py
@@ -477,10 +477,23 @@ class Task(DeletedMetaFields, models.Model):
managed = True
indexes = [
models.Index(fields=["signal_report"], name="posthog_task_signal_report_idx"),
- models.Index(fields=["archived"], name="posthog_task_archived_idx"),
+ # The default task list pins `team`, `deleted`, `internal` and `archived` before it
+ # sorts, so the partial indexes below avoid scanning deleted rows and put both boolean
+ # filters ahead of the sort keys. The broad indexes remain for callers that leave
+ # either boolean unconstrained while still needing the results in timestamp order.
+ models.Index(
+ fields=["team", "internal", "archived", "-created_at", "-id"],
+ condition=models.Q(deleted=False),
+ name="posthog_task_team_live_crt_idx",
+ ),
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"),
models.Index(fields=["channel", "-created_at"], name="posthog_task_channel_feed_idx"),
+ models.Index(
+ fields=["team", "internal", "archived", "-last_activity_at", "-id"],
+ condition=models.Q(deleted=False),
+ name="posthog_task_team_live_act_idx",
+ ),
models.Index(fields=["team", "-last_activity_at", "-id"], name="posthog_task_team_activity_idx"),
models.Index(fields=["channel", "-last_activity_at"], name="posthog_task_chan_activity_idx"),
models.Index(fields=["loop"], name="posthog_task_loop_idx"),