: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
fix(insights): rebuild the invalid insightviewed null unique index (#93016)
posthog[bot]415804d8ebef
diff --git a/products/product_analytics/backend/migrations/0007_fix_insightviewed_null_duplicates.py b/products/product_analytics/backend/migrations/0007_fix_insightviewed_null_duplicates.pynew file mode 100644index 000000000000..b7864e524343--- /dev/null+++ b/products/product_analytics/backend/migrations/0007_fix_insightviewed_null_duplicates.py@@ -0,0 +1,30 @@+from django.db import migrations+from django.db.models import Max, Subquery++import structlog++logger = structlog.get_logger(__name__)+++def clean_duplicates(apps, schema_editor):+ InsightViewed = apps.get_model("product_analytics", "InsightViewed")++ null_rows = InsightViewed.objects.filter(team__isnull=True, user__isnull=True)+ # Keep the newest row, the highest id, for each insight.+ keep_ids = null_rows.values("insight_id").annotate(keep_id=Max("id")).values("keep_id")+ deleted_count, _ = null_rows.exclude(id__in=Subquery(keep_ids)).delete()++ logger.info("clean_duplicates_complete", deleted_count=deleted_count)+++class Migration(migrations.Migration):+ dependencies = [+ ("product_analytics", "0006_insightvariable_values_query_connection_id"),+ ]++ operations = [+ # Migration 0946 cleaned these rows once, but the partial unique index that+ # migration 0947 was meant to add never became valid in every region, so+ # duplicates can have built up again.+ migrations.RunPython(clean_duplicates, migrations.RunPython.noop, elidable=True),+ ]diff --git a/products/product_analytics/backend/migrations/0008_repair_insightviewed_null_unique_index.py b/products/product_analytics/backend/migrations/0008_repair_insightviewed_null_unique_index.pynew file mode 100644index 000000000000..4e73fabf2344--- /dev/null+++ b/products/product_analytics/backend/migrations/0008_repair_insightviewed_null_unique_index.py@@ -0,0 +1,26 @@+from django.db import migrations++from posthog.migration_helpers import CreateIndexConcurrently+++class Migration(migrations.Migration):+ atomic = False # Required for CREATE INDEX CONCURRENTLY++ dependencies = [+ ("product_analytics", "0007_fix_insightviewed_null_duplicates"),+ ]++ operations = [+ # Migration 0947 built this index with a raw `CREATE UNIQUE INDEX CONCURRENTLY+ # IF NOT EXISTS`. Postgres matches `IF NOT EXISTS` by name and not by validity, so+ # where a build was cancelled the invalid index stayed and every retry skipped it.+ # `CreateIndexConcurrently` drops the invalid leftover before it rebuilds.+ # The index is not part of model state, so this migration adds no state operation.+ CreateIndexConcurrently(+ index_name="posthog_insightviewed_null_team_user_unique",+ table_name="posthog_insightviewed",+ columns="(insight_id)",+ unique=True,+ where='WHERE "team_id" IS NULL AND "user_id" IS NULL',+ ),+ ]diff --git a/products/product_analytics/backend/migrations/max_migration.txt b/products/product_analytics/backend/migrations/max_migration.txtindex e583ff417d8a..a05acdc6dd8b 100644--- a/products/product_analytics/backend/migrations/max_migration.txt+++ b/products/product_analytics/backend/migrations/max_migration.txt@@ -1 +1 @@-0006_insightvariable_values_query_connection_id+0008_repair_insightviewed_null_unique_index