: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(oauth): add btree index on oauthaccesstoken.token (#67073)
Daniel Escribano85f78ae85b2e
diff --git a/posthog/migrations/1247_oauthaccesstoken_token_idx.py b/posthog/migrations/1247_oauthaccesstoken_token_idx.pynew file mode 100644index 000000000000..98f615f7e57a--- /dev/null+++ b/posthog/migrations/1247_oauthaccesstoken_token_idx.py@@ -0,0 +1,25 @@+from django.db import migrations, models++from posthog.migration_helpers import SafeAddIndexConcurrently+++class Migration(migrations.Migration):+ # CONCURRENTLY so building the B-tree on oauthaccesstoken doesn't take an ACCESS+ # EXCLUSIVE lock on the table. Concurrent builds can't run in a transaction, so the+ # migration is non-atomic. SafeAddIndexConcurrently (vs Django's AddIndexConcurrently)+ # disables lock_timeout/statement_timeout, skips an already-valid index, and rebuilds+ # an invalid leftover from an interrupted build — so a transient cancellation during+ # deploy doesn't wedge bin/migrate retries. It tracks Django model state itself, so no+ # SeparateDatabaseAndState wrapper is needed.+ atomic = False++ dependencies = [+ ("posthog", "1246_alter_organizationdomain_id_jag_allowed_clients_and_more"),+ ]++ operations = [+ SafeAddIndexConcurrently(+ model_name="oauthaccesstoken",+ index=models.Index(fields=["token"], name="oauthaccesstoken_token_idx"),+ ),+ ]diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txtindex 643ce2e13a87..a1b3a1b5c29a 100644--- a/posthog/migrations/max_migration.txt+++ b/posthog/migrations/max_migration.txt@@ -1 +1 @@-1246_alter_organizationdomain_id_jag_allowed_clients_and_more+1247_oauthaccesstoken_token_idxdiff --git a/posthog/models/oauth.py b/posthog/models/oauth.pyindex 00dd43b1c597..85f46238ed8c 100644--- a/posthog/models/oauth.py+++ b/posthog/models/oauth.py@@ -416,6 +416,10 @@ class OAuthAccessToken(AbstractAccessToken):opclasses=["gin_trgm_ops"],condition=Q(application__isnull=False),),+ # B-tree on the plaintext `token` so equality lookups by token value resolve+ # via an index scan instead of a sequential scan. These lookups account for a+ # large share of the server's CPU time; the index removes that hot-path scan.+ models.Index(fields=["token"], name="oauthaccesstoken_token_idx"),]id: models.UUIDField = models.UUIDField(primary_key=True, default=UUIDT, editable=False)