: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(replay-vision): index the session-export asset lookup (#94072)
José Sequeirac30e6a03b403
diff --git a/products/exports/backend/migrations/0011_exportedasset_exportedasset_system_session.py b/products/exports/backend/migrations/0011_exportedasset_exportedasset_system_session.pynew file mode 100644index 000000000000..d89a3293ca83--- /dev/null+++ b/products/exports/backend/migrations/0011_exportedasset_exportedasset_system_session.py@@ -0,0 +1,24 @@+import django.db.models.fields.json+from django.db import migrations, models++from posthog.migration_helpers import SafeAddIndexConcurrently+++class Migration(migrations.Migration):+ atomic = False++ dependencies = [+ ("exports", "0010_alter_subscription_target_type"),+ ]++ operations = [+ SafeAddIndexConcurrently(+ model_name="exportedasset",+ index=models.Index(+ models.F("team_id"),+ django.db.models.fields.json.KeyTransform("session_recording_id", "export_context"),+ condition=models.Q(("is_system", True)),+ name="exportedasset_system_session",+ ),+ ),+ ]diff --git a/products/exports/backend/migrations/max_migration.txt b/products/exports/backend/migrations/max_migration.txtindex ee9cd8ecaf75..1961b8cce020 100644--- a/products/exports/backend/migrations/max_migration.txt+++ b/products/exports/backend/migrations/max_migration.txt@@ -1 +1 @@-0010_alter_subscription_target_type+0011_exportedasset_exportedasset_system_sessiondiff --git a/products/exports/backend/models/exported_asset.py b/products/exports/backend/models/exported_asset.pyindex 8629f02f1a00..02ff4dd379f5 100644--- a/products/exports/backend/models/exported_asset.py+++ b/products/exports/backend/models/exported_asset.py@@ -8,6 +8,7 @@ from urllib.parse import urlparsefrom django.conf import settingsfrom django.db import modelsfrom django.db.models import Q+from django.db.models.fields.json import KeyTransformfrom django.http import HttpResponse, HttpResponseRedirectfrom django.utils.text import slugifyfrom django.utils.timezone import now@@ -139,6 +140,16 @@ 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.+ models.Index(+ models.F("team_id"),+ KeyTransform("session_recording_id", "export_context"),+ name="exportedasset_system_session",+ condition=Q(is_system=True),+ ),+ ]def save(self, *args, **kwargs):if not self.expires_after:diff --git a/products/replay_vision/backend/temporal/activities/ensure_session_asset.py b/products/replay_vision/backend/temporal/activities/ensure_session_asset.pyindex 0823520f48a7..0476921d0f03 100644--- a/products/replay_vision/backend/temporal/activities/ensure_session_asset.py+++ b/products/replay_vision/backend/temporal/activities/ensure_session_asset.py@@ -21,7 +21,7 @@ _ASSET_EXPIRES_AFTER_DAYS = 90@track_activity()async def ensure_session_asset_activity(inputs: EnsureSessionAssetInputs) -> EnsureSessionAssetOutput:"""Get-or-create the `is_system=True` MP4 ExportedAsset for `(team, session)`; concurrent runs may produce orphaned duplicates that the asset expiry cleans up."""- existing = (+ existing_id = (await ExportedAsset.objects.filter(team_id=inputs.team_id,export_format=_EXPORT_FORMAT,@@ -33,10 +33,11 @@ async def ensure_session_asset_activity(inputs: EnsureSessionAssetInputs) -> Ensis_system=True,).order_by("id")+ .values_list("id", flat=True).afirst())- if existing is not None:- return EnsureSessionAssetOutput(asset_id=existing.id)+ if existing_id is not None:+ return EnsureSessionAssetOutput(asset_id=existing_id)created_at = now()asset = await ExportedAsset.objects.acreate(