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(activity-logs): index activity log by team, user and created_at (#121854)

posthog[bot]caf08238cda7
diff --git a/posthog/migrations/1409_activitylog_team_user_created_at_index.py b/posthog/migrations/1409_activitylog_team_user_created_at_index.py
new file mode 100644
index 000000000000..8cc24e52e82b
--- /dev/null
+++ b/posthog/migrations/1409_activitylog_team_user_created_at_index.py
@@ -0,0 +1,21 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("posthog", "1408_file_system_team_name_trgm_index"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="activitylog",
+ index=models.Index(
+ fields=["team_id", "user", "-created_at"],
+ name="idx_alog_team_user_created_at",
+ ),
+ ),
+ ]
diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt
index 3c1cba8ffd2b..7bfce60a81a4 100644
--- a/posthog/migrations/max_migration.txt
+++ b/posthog/migrations/max_migration.txt
@@ -1 +1 @@
-1408_file_system_team_name_trgm_index
+1409_activitylog_team_user_created_at_index
diff --git a/posthog/models/activity_logging/activity_log.py b/posthog/models/activity_logging/activity_log.py
index 0c4d1bf5e298..e62cfa2d3e4c 100644
--- a/posthog/models/activity_logging/activity_log.py
+++ b/posthog/models/activity_logging/activity_log.py
@@ -256,6 +256,12 @@ class ActivityLog(UUIDTModel):
fields=["team_id", "-created_at"],
name="idx_alog_team_created_at",
),
+ # One user's rows in a time window, for the activity summary. Not partial, because the
+ # query keeps rows where `is_system` and `was_impersonated` are NULL.
+ models.Index(
+ fields=["team_id", "user", "-created_at"],
+ name="idx_alog_team_user_created_at",
+ ),
]
team_id = models.PositiveIntegerField(null=True)