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(migrations): repair invalid hogfunction batch_export_id index (#92376)

posthog[bot]25445f70fb92
diff --git a/posthog/management/migration_analysis/operations.py b/posthog/management/migration_analysis/operations.py
index 35b71259392f..486049f7eace 100644
--- a/posthog/management/migration_analysis/operations.py
+++ b/posthog/management/migration_analysis/operations.py
@@ -484,7 +484,7 @@ class RunSQLAnalyzer(OperationAnalyzer):
score=1,
reason="CREATE INDEX CONCURRENTLY is safe (non-blocking)",
details={"sql": sql},
- guidance="Also prefix with `SET lock_timeout = 0;` so the deploy lock_timeout can't cancel the build and leave an invalid index that defeats IF NOT EXISTS on retry.",
+ guidance="Prefer `SafeAddIndexConcurrently` (or the raw-SQL `CreateIndexConcurrently`) from posthog.migration_helpers. IF NOT EXISTS matches by name, not validity, so this raw form skips past an `indisvalid = false` leftover from an interrupted build and never rebuilds it; the helpers detect and rebuild it. If you keep the raw form, also prefix with `SET lock_timeout = 0; SET statement_timeout = 0;` so neither the deploy lock_timeout nor a configured statement_timeout can cancel the build in the first place.",
)
return OperationRisk(
type=self.operation_type,
diff --git a/products/cdp/backend/migrations/0005_repair_hogfunction_batch_export_id_index.py b/products/cdp/backend/migrations/0005_repair_hogfunction_batch_export_id_index.py
new file mode 100644
index 000000000000..7e63e5582b67
--- /dev/null
+++ b/products/cdp/backend/migrations/0005_repair_hogfunction_batch_export_id_index.py
@@ -0,0 +1,26 @@
+from django.db import migrations
+
+from posthog.migration_helpers import CreateIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ # CONCURRENTLY can't run inside a transaction.
+ atomic = False
+
+ dependencies = [
+ ("cdp", "0004_backfill_internal_event_filter_sources"),
+ ]
+
+ operations = [
+ # Repair the implicit ForeignKey index on posthog_hogfunction.batch_export_id.
+ # Migration posthog/0949 built it with a raw `CREATE INDEX CONCURRENTLY IF NOT EXISTS`,
+ # which matches by name, not validity, so an interrupted build left it
+ # indisvalid = false and every retry skipped past it. CreateIndexConcurrently
+ # drops and rebuilds an invalid leftover; a valid index is left untouched.
+ # No SeparateDatabaseAndState state op: the FK's implicit index is already in state.
+ CreateIndexConcurrently(
+ index_name="posthog_hogfunction_batch_export_id_d64c3403",
+ table_name="posthog_hogfunction",
+ columns="(batch_export_id)",
+ ),
+ ]
diff --git a/products/cdp/backend/migrations/max_migration.txt b/products/cdp/backend/migrations/max_migration.txt
index 9faf2e3cf857..a51388e79621 100644
--- a/products/cdp/backend/migrations/max_migration.txt
+++ b/products/cdp/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0004_backfill_internal_event_filter_sources
+0005_repair_hogfunction_batch_export_id_index