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(error-tracking): index symbol set cleanup batch query (#67227)

posthog[bot]af69f39e9bae
diff --git a/products/error_tracking/backend/migrations/0018_symbolset_last_used_created_idx.py b/products/error_tracking/backend/migrations/0018_symbolset_last_used_created_idx.py
new file mode 100644
index 000000000000..81372743313d
--- /dev/null
+++ b/products/error_tracking/backend/migrations/0018_symbolset_last_used_created_idx.py
@@ -0,0 +1,25 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently, SafeRemoveIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ # Concurrent index builds cannot run inside a transaction.
+ atomic = False
+
+ dependencies = [
+ ("error_tracking", "0017_migrate_cohorts_models"),
+ ]
+
+ operations = [
+ # Add the composite first so `last_used` lookups keep index coverage
+ # throughout, then drop the now-redundant single-column index.
+ SafeAddIndexConcurrently(
+ model_name="errortrackingsymbolset",
+ index=models.Index(fields=["last_used", "created_at"], name="et_symset_used_created_idx"),
+ ),
+ SafeRemoveIndexConcurrently(
+ model_name="errortrackingsymbolset",
+ name="posthog_err_last_us_c924f6_idx",
+ ),
+ ]
diff --git a/products/error_tracking/backend/migrations/max_migration.txt b/products/error_tracking/backend/migrations/max_migration.txt
index 3be78220c53c..1db1292b408f 100644
--- a/products/error_tracking/backend/migrations/max_migration.txt
+++ b/products/error_tracking/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0017_migrate_cohorts_models
+0018_symbolset_last_used_created_idx
diff --git a/products/error_tracking/backend/models.py b/products/error_tracking/backend/models.py
index d0ec78fcd2bb..7deeb89d7007 100644
--- a/products/error_tracking/backend/models.py
+++ b/products/error_tracking/backend/models.py
@@ -239,7 +239,10 @@ class ErrorTrackingSymbolSet(UUIDTModel):
class Meta:
indexes = [
models.Index(fields=["team_id", "ref"]),
- models.Index(fields=["last_used"]),
+ # Composite covers the cleanup filter's two OR branches: `last_used < cutoff`
+ # (leading column) and `last_used IS NULL AND created_at < cutoff` (NULL group
+ # then created_at range), so batch cleanup avoids a full PK-ordered scan.
+ models.Index(fields=["last_used", "created_at"], name="et_symset_used_created_idx"),
]
constraints = [