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(persons): skip distinct id reset when no live person owns it (#109642)

Eli Reisman45bc203e1176
diff --git a/posthog/models/person/deletion.py b/posthog/models/person/deletion.py
index 085b714fa524..d742d9d23e05 100644
--- a/posthog/models/person/deletion.py
+++ b/posthog/models/person/deletion.py
@@ -13,6 +13,7 @@ from posthog.models.person.util import (
PersonTombstonePublication,
create_person,
create_person_distinct_id,
+ get_person_by_distinct_id,
get_person_tombstones,
get_persons_by_uuids,
)
@@ -81,9 +82,30 @@ def _updated_distinct_ids(team_id: int, distinct_id_versions: list[tuple[str, in
# since they no longer belong to deleted persons
# it's safer to throw and exit if anything went wrong
+ # The floor RPC also matches deleted rows, and publishing one as live leaves a ghost the sweep never removes.
+ # This lookup reads the replica, so the primary is rechecked after the floor RPC.
+ live_person = get_person_by_distinct_id(team_id, distinct_id, distinct_id_limit=0)
+ if live_person is None:
+ logger.info("Skipping distinct id reset: no live person", team_id=team_id, distinct_id=distinct_id)
+ continue
+
# The write goes through personhog (an external RPC that can't join a
# Postgres transaction), so there is no surrounding atomic block.
person = _update_distinct_id_in_postgres(distinct_id, version, team_id)
+ if person is not None and person.uuid != live_person.uuid:
+ logger.warning(
+ "Skipping distinct id reset: distinct id moved to another person",
+ team_id=team_id,
+ distinct_id=distinct_id,
+ )
+ continue
+ if person is not None and get_person_tombstones(team_id, [person.uuid]):
+ logger.warning(
+ "Skipping distinct id reset: person is deleted in Postgres",
+ team_id=team_id,
+ distinct_id=distinct_id,
+ )
+ continue
# Update ClickHouse via Kafka message
if person:
diff --git a/posthog/models/person/test/test_reset_distinct_ids_personhog.py b/posthog/models/person/test/test_reset_distinct_ids_personhog.py
index 2dc588f0d507..0b36e2b1aa22 100644
--- a/posthog/models/person/test/test_reset_distinct_ids_personhog.py
+++ b/posthog/models/person/test/test_reset_distinct_ids_personhog.py
@@ -7,9 +7,18 @@ Covers the two personhog RPC writes in the repair flow:
from uuid import uuid4
+from unittest import mock
+
from django.test import SimpleTestCase
-from posthog.models.person.deletion import _set_person_version_floor, _update_distinct_id_in_postgres
+from parameterized import parameterized
+
+from posthog.models.person import Person, deletion
+from posthog.models.person.deletion import (
+ _set_person_version_floor,
+ _update_distinct_id_in_postgres,
+ _updated_distinct_ids,
+)
from posthog.personhog_client.fake_client import fake_personhog_client
@@ -53,3 +62,36 @@ class TestSetPersonVersionFloorRPC(SimpleTestCase):
assert calls[0].request.team_id == 1
assert calls[0].request.person_id == 42
assert calls[0].request.min_version == 500
+
+
+class TestUpdatedDistinctIdsSkipsDeleted(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("no_live_person", "none", False),
+ ("distinct_id_moved_to_another_person", "other", False),
+ ("replica_lags_a_delete_on_the_primary", "same", True),
+ ]
+ )
+ def test_leaves_the_distinct_id_unpublished(self, _name: str, replica_owner: str, deleted: bool) -> None:
+ owner_uuid = uuid4()
+ live_person = {
+ "none": None,
+ "other": Person(uuid=uuid4(), team_id=1),
+ "same": Person(uuid=owner_uuid, team_id=1),
+ }[replica_owner]
+ with (
+ fake_personhog_client() as fake,
+ mock.patch.object(deletion, "get_person_by_distinct_id", return_value=live_person),
+ mock.patch.object(deletion, "create_person_distinct_id") as publish_distinct_id,
+ mock.patch.object(deletion, "create_person") as publish_person,
+ ):
+ fake.add_person(
+ team_id=1, person_id=42, uuid=str(owner_uuid), version=3, distinct_ids=["did-1"], is_deleted=deleted
+ )
+
+ _updated_distinct_ids(1, [("did-1", 105)])
+
+ fake.assert_not_called("set_person_version_floor")
+
+ publish_distinct_id.assert_not_called()
+ publish_person.assert_not_called()