: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(experiments): implement deduplication backfill as a management command. (#59762)
Rodrigo Iloro4086ca496ce1
diff --git a/products/experiments/backend/management/__init__.py b/products/experiments/backend/management/__init__.pynew file mode 100644index 000000000000..e69de29bb2d1diff --git a/products/experiments/backend/management/commands/__init__.py b/products/experiments/backend/management/commands/__init__.pynew file mode 100644index 000000000000..e69de29bb2d1diff --git a/products/experiments/backend/management/commands/dedupe_experiment_metric_uuids.py b/products/experiments/backend/management/commands/dedupe_experiment_metric_uuids.pynew file mode 100644index 000000000000..1817ea4159ee--- /dev/null+++ b/products/experiments/backend/management/commands/dedupe_experiment_metric_uuids.py@@ -0,0 +1,247 @@+"""Dedupe metric UUIDs across an experiment's metrics / metrics_secondary / saved metrics.++Some experiments accumulated duplicate metric UUIDs (likely via MCP/LLM update+flows that copied a metric without regenerating its UUID). The service layer+now rewrites duplicates on every write, but pre-existing rows in the DB still+carry the corruption. This command performs a one-shot backfill that mirrors+what ``_assign_uuids_to_metrics`` does on the live path: the first occurrence+of a duplicated UUID keeps it (so any attached ``ExperimentMetricResult`` rows+or saved-metric links stay valid), and later occurrences get fresh UUIDs that+are appended to the corresponding ordering array.++Run with ``--dry-run`` first to confirm the affected count before writing.+"""++from copy import deepcopy+from uuid import uuid4++from django.core.management.base import BaseCommand+from django.db import connection, transaction++import structlog++from products.experiments.backend.models.experiment import Experiment, ExperimentToSavedMetric++logger = structlog.get_logger(__name__)+++# Returns experiment ids that have at least one duplicated metric uuid across+# the union of inline `metrics`, inline `metrics_secondary`, and attached+# saved-metric query uuids.+_AFFECTED_IDS_SQL = """+WITH metrics_unnested AS (+ SELECT e.id AS experiment_id, elem->>'uuid' AS metric_uuid+ FROM posthog_experiment e+ CROSS JOIN LATERAL jsonb_array_elements(COALESCE(e.metrics, '[]'::jsonb)) AS elem+ WHERE e.deleted IS NOT TRUE AND elem->>'uuid' IS NOT NULL++ UNION ALL++ SELECT e.id, elem->>'uuid'+ FROM posthog_experiment e+ CROSS JOIN LATERAL jsonb_array_elements(COALESCE(e.metrics_secondary, '[]'::jsonb)) AS elem+ WHERE e.deleted IS NOT TRUE AND elem->>'uuid' IS NOT NULL++ UNION ALL++ SELECT e.id, sm.query->>'uuid'+ FROM posthog_experiment e+ JOIN posthog_experimenttosavedmetric link ON link.experiment_id = e.id+ JOIN posthog_experimentsavedmetric sm ON sm.id = link.saved_metric_id+ WHERE e.deleted IS NOT TRUE AND sm.query->>'uuid' IS NOT NULL+)+SELECT experiment_id+FROM metrics_unnested+GROUP BY experiment_id, metric_uuid+HAVING COUNT(*) > 1+"""+++def _dedupe_metrics(metrics: list[dict] | None, seen: set[str]) -> tuple[list[dict] | None, bool]:+ """Walk ``metrics`` in order. First occurrence of a uuid keeps it; later+ occurrences (and missing uuids) get fresh ones. Returns the prepared list+ plus a `changed` flag.++ Preserves ``None`` (mirrors ``_assign_uuids_to_metrics`` on the live path) so+ the backfill never normalizes a genuinely-null column into an empty list.+ """+ if not metrics:+ return metrics, False+ prepared = deepcopy(metrics)+ changed = False+ for metric in prepared:+ original = metric.get("uuid")+ if not original or original in seen:+ new_uuid = str(uuid4())+ metric["uuid"] = new_uuid+ seen.add(new_uuid)+ changed = True+ else:+ seen.add(original)+ return prepared, changed+++def _reconcile_ordering(+ ordering: list[str] | None,+ original_uuids: set[str],+ new_uuids: set[str],+ protected_uuids: set[str],+) -> list[str] | None:+ """Reconcile an ordering array with a dedup that rewrote inline metric uuids.++ A duplicated uuid's first occurrence keeps its value (so its ordering entry+ stays valid); each later occurrence is regenerated into a brand-new uuid that+ has to be appended. When the regenerated value displaced the *only* inline+ use of the old uuid, the old uuid is now an orphan in the ordering and is+ dropped — e.g. a metric duplicated across the primary and secondary lists+ leaves the secondary's pre-dedup uuid pointing at nothing.++ ``protected_uuids`` are never removed: a saved-metric uuid can sit in the+ ordering while also matching an inline metric's original uuid (the inline+ copy is the one that gets regenerated), and that saved-metric reference must+ survive. Removal is therefore scoped to orphaned *inline* uuids only.+ """+ added = new_uuids - original_uuids+ removed = (original_uuids - new_uuids) - protected_uuids+ if not added and not removed:+ return ordering+ current = [uuid for uuid in (ordering or []) if uuid not in removed]+ for uuid in added:+ if uuid not in current:+ current.append(uuid)+ return current+++class Command(BaseCommand):+ help = "Dedupe experiment metric UUIDs across metrics / metrics_secondary / saved metrics."++ def add_arguments(self, parser):+ parser.add_argument(+ "--dry-run",+ action="store_true",+ default=False,+ help="Report what would change without writing.",+ )++ def handle(self, *args, **options):+ dry_run: bool = options["dry_run"]++ with connection.cursor() as cursor:+ cursor.execute(_AFFECTED_IDS_SQL)+ affected_ids = sorted({row[0] for row in cursor.fetchall()})++ if not affected_ids:+ self.stdout.write("No experiments to dedupe.")+ return++ self.stdout.write(f"{'[DRY RUN] ' if dry_run else ''}Found {len(affected_ids)} experiments needing dedup.")++ updated = 0+ failed = 0+ skipped = 0++ # The affected set is small (a few hundred rows), so a single pass with no+ # chunking keeps the command simple.+ saved_uuids_by_experiment: dict[int, set[str]] = {}+ for link in (+ ExperimentToSavedMetric.objects.filter(experiment_id__in=affected_ids)+ .select_related("saved_metric")+ .only("experiment_id", "saved_metric__query")+ ):+ sm = link.saved_metric+ if sm and sm.query:+ uuid = sm.query.get("uuid")+ if uuid:+ saved_uuids_by_experiment.setdefault(link.experiment_id, set()).add(uuid)++ qs = Experiment.objects.filter(id__in=affected_ids).only(+ "id",+ "metrics",+ "metrics_secondary",+ "primary_metrics_ordered_uuids",+ "secondary_metrics_ordered_uuids",+ )+ for experiment in qs.iterator():+ try:+ # Saved-metric uuids are fixed points: they seed dedup's+ # uniqueness space and are never removed from the orderings.+ saved_metric_uuids: set[str] = set(saved_uuids_by_experiment.get(experiment.id, set()))+ seen: set[str] = set(saved_metric_uuids)++ original_primary_uuids: set[str] = {uuid for m in (experiment.metrics or []) if (uuid := m.get("uuid"))}+ original_secondary_uuids: set[str] = {+ uuid for m in (experiment.metrics_secondary or []) if (uuid := m.get("uuid"))+ }++ new_primary, primary_changed = _dedupe_metrics(experiment.metrics, seen)+ new_secondary, secondary_changed = _dedupe_metrics(experiment.metrics_secondary, seen)++ if not (primary_changed or secondary_changed):+ # Reached either because rows changed between the SELECT+ # and now, or because the only duplication is across two+ # saved metrics — which this command can't fix (it only+ # rewrites inline metrics, treating saved-metric uuids as+ # fixed points). Skip and warn so re-runs aren't expected+ # to drive the affected count to zero in that case.+ skipped += 1+ logger.warning(+ "experiment_metric_uuid_dedupe_skipped",+ experiment_id=experiment.id,+ reason="no_inline_change",+ )+ continue++ new_primary_uuids: set[str] = {uuid for m in (new_primary or []) if (uuid := m.get("uuid"))}+ new_secondary_uuids: set[str] = {uuid for m in (new_secondary or []) if (uuid := m.get("uuid"))}++ new_primary_ordering = _reconcile_ordering(+ experiment.primary_metrics_ordered_uuids,+ original_primary_uuids,+ new_primary_uuids,+ saved_metric_uuids,+ )+ new_secondary_ordering = _reconcile_ordering(+ experiment.secondary_metrics_ordered_uuids,+ original_secondary_uuids,+ new_secondary_uuids,+ saved_metric_uuids,+ )++ logger.info(+ "experiment_metric_uuid_dedupe_planned",+ experiment_id=experiment.id,+ primary_changed=primary_changed,+ secondary_changed=secondary_changed,+ dry_run=dry_run,+ )++ if dry_run:+ updated += 1+ continue++ experiment.metrics = new_primary+ experiment.metrics_secondary = new_secondary+ experiment.primary_metrics_ordered_uuids = new_primary_ordering+ experiment.secondary_metrics_ordered_uuids = new_secondary_ordering+ with transaction.atomic():+ experiment.save(+ update_fields=[+ "metrics",+ "metrics_secondary",+ "primary_metrics_ordered_uuids",+ "secondary_metrics_ordered_uuids",+ ]+ )+ updated += 1+ except Exception as e:+ failed += 1+ logger.error(+ "experiment_metric_uuid_dedupe_failed",+ experiment_id=experiment.id,+ error=str(e),+ exc_info=True,+ )++ verb = "Would update" if dry_run else "Updated"+ suffix = f" {skipped} skipped (saved-metric-only duplicates, not fixable here)." if skipped else ""+ self.stdout.write(f"{verb} {updated} experiments. {failed} failed.{suffix}")diff --git a/products/experiments/backend/test/test_dedupe_experiment_metric_uuids.py b/products/experiments/backend/test/test_dedupe_experiment_metric_uuids.pynew file mode 100644index 000000000000..c26718d7ffe1--- /dev/null+++ b/products/experiments/backend/test/test_dedupe_experiment_metric_uuids.py@@ -0,0 +1,241 @@+from io import StringIO++from posthog.test.base import APIBaseTest++from django.core.management import call_command+from django.utils import timezone++from parameterized import parameterized++from products.experiments.backend.models.experiment import Experiment, ExperimentSavedMetric, ExperimentToSavedMetric+from products.feature_flags.backend.models.feature_flag import FeatureFlag+++class TestDedupeExperimentMetricUuids(APIBaseTest):+ def _create_flag(self, key: str) -> FeatureFlag:+ return FeatureFlag.objects.create(+ team=self.team,+ created_by=self.user,+ key=key,+ name=f"Flag for {key}",+ filters={+ "groups": [{"properties": [], "rollout_percentage": 100}],+ "multivariate": {+ "variants": [+ {"key": "control", "name": "Control", "rollout_percentage": 50},+ {"key": "test", "name": "Test", "rollout_percentage": 50},+ ]+ },+ },+ )++ def _create_experiment(self, key: str, **fields) -> Experiment:+ flag = self._create_flag(key)+ return Experiment.objects.create(+ team=self.team,+ created_by=self.user,+ feature_flag=flag,+ name=f"Experiment {key}",+ start_date=timezone.now(),+ **fields,+ )++ def _metric(self, uuid: str, event: str = "purchase") -> dict:+ return {+ "kind": "ExperimentMetric",+ "metric_type": "mean",+ "source": {"kind": "EventsNode", "event": event, "math": "total"},+ "uuid": uuid,+ }++ def _run(self, *args) -> str:+ out = StringIO()+ call_command("dedupe_experiment_metric_uuids", *args, stdout=out)+ return out.getvalue()++ def _uuids(self, metrics: list[dict] | None) -> list[str]:+ return [m["uuid"] for m in (metrics or [])]++ def test_no_experiments_to_dedupe_reports_nothing(self):+ self._create_experiment(+ "clean",+ metrics=[self._metric("aaaaaaaa-0000-0000-0000-000000000001")],+ primary_metrics_ordered_uuids=["aaaaaaaa-0000-0000-0000-000000000001"],+ )++ output = self._run()++ assert "No experiments to dedupe." in output++ @parameterized.expand(+ [+ ("primary", "metrics", "primary_metrics_ordered_uuids"),+ ("secondary", "metrics_secondary", "secondary_metrics_ordered_uuids"),+ ]+ )+ def test_dedupes_duplicate_uuid_within_a_single_metric_list(self, _name, metrics_field, ordering_field):+ dup = "aaaaaaaa-0000-0000-0000-000000000001"+ experiment = self._create_experiment(+ f"inline-dup-{metrics_field}",+ **{+ metrics_field: [self._metric(dup, "first"), self._metric(dup, "second")],+ ordering_field: [dup],+ },+ )++ self._run()+ experiment.refresh_from_db()++ metrics = getattr(experiment, metrics_field)+ ordering = getattr(experiment, ordering_field)+ uuids = self._uuids(metrics)+ # First occurrence keeps the original uuid; the second is regenerated.+ assert uuids[0] == dup+ assert uuids[1] != dup+ assert len(set(uuids)) == 2+ # The regenerated uuid is appended to the ordering; the incumbent stays.+ assert ordering == [dup, uuids[1]]+ # The order of the metrics themselves and their events is preserved.+ assert metrics[0]["source"]["event"] == "first"+ assert metrics[1]["source"]["event"] == "second"++ def test_dedupes_duplicate_uuid_across_primary_and_secondary(self):+ dup = "bbbbbbbb-0000-0000-0000-000000000001"+ experiment = self._create_experiment(+ "cross-list-dup",+ metrics=[self._metric(dup, "primary")],+ metrics_secondary=[self._metric(dup, "secondary")],+ primary_metrics_ordered_uuids=[dup],+ secondary_metrics_ordered_uuids=[dup],+ )++ self._run()+ experiment.refresh_from_db()++ assert experiment.metrics is not None+ assert experiment.metrics_secondary is not None+ primary_uuid = experiment.metrics[0]["uuid"]+ secondary_uuid = experiment.metrics_secondary[0]["uuid"]+ # Primary keeps the incumbent; the secondary collision is regenerated.+ assert primary_uuid == dup+ assert secondary_uuid != dup+ assert experiment.primary_metrics_ordered_uuids == [dup]+ # The secondary ordering's pre-dedup `dup` is now an orphan (it points at+ # no surviving secondary metric) so it is dropped and the new uuid replaces it.+ assert experiment.secondary_metrics_ordered_uuids == [secondary_uuid]++ def test_inline_metric_colliding_with_saved_metric_uuid_is_regenerated(self):+ shared = "cccccccc-0000-0000-0000-000000000001"+ saved_metric = ExperimentSavedMetric.objects.create(+ team=self.team,+ created_by=self.user,+ name="Saved",+ query={"kind": "ExperimentMetric", "metric_type": "mean", "uuid": shared},+ )+ experiment = self._create_experiment(+ "saved-collision",+ metrics=[self._metric(shared, "inline")],+ primary_metrics_ordered_uuids=[shared],+ )+ ExperimentToSavedMetric.objects.create(+ experiment=experiment,+ saved_metric=saved_metric,+ metadata={"type": "primary"},+ )++ self._run()+ experiment.refresh_from_db()++ assert experiment.metrics is not None+ inline_uuid = experiment.metrics[0]["uuid"]+ # The saved-metric uuid is treated as a fixed point; the inline copy moves.+ assert inline_uuid != shared+ # The saved-metric uuid stays in the ordering, the regenerated inline uuid is appended.+ assert experiment.primary_metrics_ordered_uuids == [shared, inline_uuid]++ def test_dry_run_reports_but_does_not_write(self):+ dup = "dddddddd-0000-0000-0000-000000000001"+ experiment = self._create_experiment(+ "dry-run",+ metrics=[self._metric(dup, "first"), self._metric(dup, "second")],+ primary_metrics_ordered_uuids=[dup],+ )++ output = self._run("--dry-run")+ experiment.refresh_from_db()++ assert "[DRY RUN]" in output+ assert "Would update 1 experiments" in output+ # Nothing was written: the duplicate uuid is still present.+ assert self._uuids(experiment.metrics) == [dup, dup]+ assert experiment.primary_metrics_ordered_uuids == [dup]++ def test_preserves_null_secondary_metrics(self):+ dup = "eeeeeeee-0000-0000-0000-000000000001"+ experiment = self._create_experiment(+ "null-secondary",+ metrics=[self._metric(dup, "first"), self._metric(dup, "second")],+ metrics_secondary=None,+ primary_metrics_ordered_uuids=[dup],+ secondary_metrics_ordered_uuids=None,+ )++ self._run()+ experiment.refresh_from_db()++ # Primary was deduped, but the null secondary column is left untouched+ # rather than normalized to an empty list.+ assert len(set(self._uuids(experiment.metrics))) == 2+ assert experiment.metrics_secondary is None+ assert experiment.secondary_metrics_ordered_uuids is None++ def test_saved_metric_only_duplication_is_skipped(self):+ shared = "ffffffff-0000-0000-0000-000000000001"+ saved_primary = ExperimentSavedMetric.objects.create(+ team=self.team,+ created_by=self.user,+ name="Saved primary",+ query={"kind": "ExperimentMetric", "metric_type": "mean", "uuid": shared},+ )+ saved_secondary = ExperimentSavedMetric.objects.create(+ team=self.team,+ created_by=self.user,+ name="Saved secondary",+ query={"kind": "ExperimentMetric", "metric_type": "mean", "uuid": shared},+ )+ experiment = self._create_experiment(+ "saved-only-dup",+ metrics=[],+ primary_metrics_ordered_uuids=[shared],+ )+ ExperimentToSavedMetric.objects.create(+ experiment=experiment, saved_metric=saved_primary, metadata={"type": "primary"}+ )+ ExperimentToSavedMetric.objects.create(+ experiment=experiment, saved_metric=saved_secondary, metadata={"type": "secondary"}+ )++ output = self._run()+ experiment.refresh_from_db()++ # The command can only rewrite inline metrics, so a duplicate that exists+ # solely across two saved metrics is surfaced as skipped, not silently+ # left looking unprocessed.+ assert "1 skipped" in output+ assert experiment.metrics == []++ def test_is_restart_safe_after_a_full_run(self):+ dup = "33333333-0000-0000-0000-000000000001"+ experiment = self._create_experiment(+ "restart-safe",+ metrics=[self._metric(dup, "a"), self._metric(dup, "b")],+ primary_metrics_ordered_uuids=[dup],+ )++ self._run()+ first_pass_uuids = self._uuids(Experiment.objects.get(id=experiment.id).metrics)++ # A second run finds nothing left to fix and is a no-op.+ output = self._run()+ assert "No experiments to dedupe." in output+ assert self._uuids(Experiment.objects.get(id=experiment.id).metrics) == first_pass_uuids