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(conversations): index widget token lookups (#88369)

Sven Langeea64372e06f4
diff --git a/posthog/migrations/1319_team_posthog_team_widget_token_idx.py b/posthog/migrations/1319_team_posthog_team_widget_token_idx.py
new file mode 100644
index 000000000000..e5c38274d8b6
--- /dev/null
+++ b/posthog/migrations/1319_team_posthog_team_widget_token_idx.py
@@ -0,0 +1,27 @@
+# Generated by Django 5.2.17 on 2026-08-25 11:25
+
+import django.db.models.fields.json
+from django.contrib.postgres.indexes import HashIndex
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("dashboards", "0015_dashboard_customization"),
+ ("posthog", "1318_disable_oauth_scope_trgm_fastupdate"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="team",
+ index=HashIndex(
+ django.db.models.fields.json.KeyTransform("widget_public_token", "conversations_settings"),
+ condition=models.Q(("conversations_enabled", True)),
+ name="posthog_team_widget_token_idx",
+ ),
+ ),
+ ]
diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt
index fb64ff314f02..e6ed0e9ad5ce 100644
--- a/posthog/migrations/max_migration.txt
+++ b/posthog/migrations/max_migration.txt
@@ -1 +1 @@
-1318_disable_oauth_scope_trgm_fastupdate
+1319_team_posthog_team_widget_token_idx
diff --git a/posthog/models/team/team.py b/posthog/models/team/team.py
index b6d37c39914d..68b91c85fd22 100644
--- a/posthog/models/team/team.py
+++ b/posthog/models/team/team.py
@@ -6,10 +6,12 @@ from zoneinfo import ZoneInfo
from django.conf import settings
from django.contrib.postgres.fields import ArrayField
+from django.contrib.postgres.indexes import HashIndex
from django.core.cache import cache
from django.core.validators import MaxValueValidator, MinLengthValidator, MinValueValidator
from django.db import connection, models, transaction
from django.db.models import QuerySet
+from django.db.models.fields.json import KeyTransform
from django.db.models.signals import post_delete, post_save
import pytz
@@ -274,6 +276,13 @@ class Team(UUIDTClassicModel):
# falls back to a bare `Manager()` for related access and re-loads the fat deprecated
# taxonomy columns on every such fetch — on the hot path that's every authenticated request.
base_manager_name = "objects"
+ indexes = [
+ HashIndex(
+ KeyTransform("widget_public_token", "conversations_settings"),
+ condition=models.Q(conversations_enabled=True),
+ name="posthog_team_widget_token_idx",
+ ),
+ ]
constraints = [
models.CheckConstraint(
name="project_id_is_not_null",