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(exports): index exported assets by team and session for every row (#101930)

Tue Haulundd1923dc43919
diff --git a/products/exports/backend/migrations/0015_exportedasset_session_index.py b/products/exports/backend/migrations/0015_exportedasset_session_index.py
new file mode 100644
index 000000000000..3bd594af69ad
--- /dev/null
+++ b/products/exports/backend/migrations/0015_exportedasset_session_index.py
@@ -0,0 +1,24 @@
+import django.db.models.fields.json
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently, SafeRemoveIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("exports", "0014_validate_subscription_context_team_fk"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="exportedasset",
+ index=models.Index(
+ models.F("team_id"),
+ django.db.models.fields.json.KeyTransform("session_recording_id", "export_context"),
+ name="exportedasset_session",
+ ),
+ ),
+ SafeRemoveIndexConcurrently(model_name="exportedasset", name="exportedasset_system_session"),
+ ]
diff --git a/products/exports/backend/migrations/max_migration.txt b/products/exports/backend/migrations/max_migration.txt
index 9ad17dfac883..db7f4c4f0c1b 100644
--- a/products/exports/backend/migrations/max_migration.txt
+++ b/products/exports/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0014_validate_subscription_context_team_fk
+0015_exportedasset_session_index
diff --git a/products/exports/backend/models/exported_asset.py b/products/exports/backend/models/exported_asset.py
index 2e345569b218..d316b9919f78 100644
--- a/products/exports/backend/models/exported_asset.py
+++ b/products/exports/backend/models/exported_asset.py
@@ -143,13 +143,12 @@ class ExportedAsset(models.Model):
class Meta:
db_table = "posthog_exportedasset"
indexes = [
- # The replay session-export get-or-create probes by (team, session recording id) at
- # high volume; without this expression index it walks the team's whole asset history.
+ # The replay session-export get-or-create and the recording-delete cleanup both probe by
+ # (team, session recording id); without this expression index they walk the team's whole asset history.
models.Index(
models.F("team_id"),
KeyTransform("session_recording_id", "export_context"),
- name="exportedasset_system_session",
- condition=Q(is_system=True),
+ name="exportedasset_session",
),
]