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(activity-logs): index org/team activity log list ordering (#72032)

Yasen54d9b5e870cd
diff --git a/posthog/migrations/1260_activitylog_idx_alog_org_created_at_and_more.py b/posthog/migrations/1260_activitylog_idx_alog_org_created_at_and_more.py
new file mode 100644
index 000000000000..1de9afa7cf25
--- /dev/null
+++ b/posthog/migrations/1260_activitylog_idx_alog_org_created_at_and_more.py
@@ -0,0 +1,34 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ # posthog_activitylog is large and write-heavy, so build the B-trees CONCURRENTLY to
+ # avoid an ACCESS EXCLUSIVE lock on the table. Concurrent builds can't run in a
+ # transaction, hence atomic = False. SafeAddIndexConcurrently disables
+ # lock_timeout/statement_timeout, skips an already-valid index, and rebuilds an invalid
+ # leftover from an interrupted build, so a cancelled deploy doesn't wedge bin/migrate
+ # retries. It tracks Django model state itself — no SeparateDatabaseAndState wrapper.
+ atomic = False
+
+ dependencies = [
+ ("posthog", "1259_globalratelimitthresholdconfig"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="activitylog",
+ index=models.Index(
+ fields=["organization_id", "-created_at"],
+ name="idx_alog_org_created_at",
+ ),
+ ),
+ SafeAddIndexConcurrently(
+ model_name="activitylog",
+ index=models.Index(
+ fields=["team_id", "-created_at"],
+ name="idx_alog_team_created_at",
+ ),
+ ),
+ ]
diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt
index f80969f726cf..a471fe6d3ed5 100644
--- a/posthog/migrations/max_migration.txt
+++ b/posthog/migrations/max_migration.txt
@@ -1 +1 @@
-1259_globalratelimitthresholdconfig
+1260_activitylog_idx_alog_org_created_at_and_more
diff --git a/posthog/models/activity_logging/activity_log.py b/posthog/models/activity_logging/activity_log.py
index 48661e132697..463213ffe8db 100644
--- a/posthog/models/activity_logging/activity_log.py
+++ b/posthog/models/activity_logging/activity_log.py
@@ -200,6 +200,19 @@ class ActivityLog(UUIDTModel):
name="idx_alog_team_scp_act_crtd",
condition=models.Q(was_impersonated=False) & models.Q(is_system=False),
),
+ # Advanced activity logs default list ordering. The org- and team-scoped list
+ # endpoints order by -created_at with no scope filter, so the scope-led indexes
+ # above can't serve the sort, and the org indexes above are partial on a detail
+ # predicate the list query never carries. These full indexes let the LIMITed
+ # ordered scan walk created_at directly instead of sorting the whole partition.
+ models.Index(
+ fields=["organization_id", "-created_at"],
+ name="idx_alog_org_created_at",
+ ),
+ models.Index(
+ fields=["team_id", "-created_at"],
+ name="idx_alog_team_created_at",
+ ),
]
team_id = models.PositiveIntegerField(null=True)