: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
chore(personhog): fixes some delete path regression (#57735)
Nick Best735159b17316
diff --git a/posthog/models/person/bulk_delete.py b/posthog/models/person/bulk_delete.pyindex 53992e9768e1..325904862643 100644--- a/posthog/models/person/bulk_delete.py+++ b/posthog/models/person/bulk_delete.py@@ -15,7 +15,12 @@ from temporalio import commonfrom posthog.models.activity_logging.activity_log import Detail, log_activityfrom posthog.models.async_deletion import AsyncDeletion, DeletionTypefrom posthog.models.person import Person, PersonDistinctId-from posthog.models.person.util import delete_person, delete_persons_from_postgres+from posthog.models.person.util import (+ _fetch_persons_by_distinct_ids_via_personhog,+ _fetch_persons_by_uuids_via_personhog,+ delete_person,+ delete_persons_from_postgres,+)from posthog.models.user import Userfrom posthog.temporal.common.client import sync_connectfrom posthog.temporal.session_replay.delete_recordings.types import DeletionConfig, RecordingsWithPersonInput@@ -44,13 +49,25 @@ def resolve_persons_for_deletion() -> builtins.list[Person]:"""Materialize Persons matching either uuids or distinct_ids.- The ``persondistinctid_set`` prefetch must pass a ``team_id``-filtered queryset:- ``posthog_persondistinctid`` is partitioned by ``team_id``, and a bare- ``Prefetch("persondistinctid_set", to_attr=...)`` produces a query without- ``WHERE team_id = X`` that scans every partition across every team. Pattern- copied from ``PersonViewSet.safely_get_queryset`` in ``posthog/api/person.py``.+ Goes straight to personhog, falling back to ORM only on error or if+ the client is not configured."""+ from posthog.personhog_client.client import get_personhog_client+ if not uuids and not distinct_ids:+ return []++ client = get_personhog_client()+ if client is not None:+ try:+ if uuids:+ return _fetch_persons_by_uuids_via_personhog(team_id, uuids)+ else:+ return _fetch_persons_by_distinct_ids_via_personhog(team_id, cast(builtins.list[str], distinct_ids))+ except Exception:+ logger.warning("resolve_persons_for_deletion_personhog_failure", team_id=team_id, exc_info=True)++ # ORM fallbackpersons_queryset = (Person.objects.filter(team_id=team_id) # nosemgrep: no-direct-persons-db-orm.only(*_PERSON_DELETION_COLUMNS)@@ -76,8 +93,6 @@ def resolve_persons_for_deletion("person_id", flat=True)persons_queryset = persons_queryset.filter(id__in=person_ids)- else:- return []return list(persons_queryset)diff --git a/posthog/models/person/util.py b/posthog/models/person/util.pyindex cadfeb463313..9673b4d0fbfd 100644--- a/posthog/models/person/util.py+++ b/posthog/models/person/util.py@@ -665,6 +665,30 @@ def _delete_person(def _get_distinct_ids_with_version(person: Person) -> dict[str, int]:+ from posthog.personhog_client.client import get_personhog_client++ client = get_personhog_client()+ if client is not None:+ try:+ resp = client.get_distinct_ids_for_person(+ GetDistinctIdsForPersonRequest(team_id=person.team_id, person_id=person.pk)+ )+ PERSONHOG_ROUTING_TOTAL.labels(+ operation="get_distinct_ids_with_version", source="personhog", client_name=get_client_name()+ ).inc()+ return {d.distinct_id: int(d.version or 0) for d in resp.distinct_ids}+ except Exception:+ PERSONHOG_ROUTING_ERRORS_TOTAL.labels(+ operation="get_distinct_ids_with_version",+ source="personhog",+ error_type="grpc_error",+ client_name=get_client_name(),+ ).inc()+ logger.warning("personhog_get_distinct_ids_with_version_failure", team_id=person.team_id, exc_info=True)++ PERSONHOG_ROUTING_TOTAL.labels(+ operation="get_distinct_ids_with_version", source="django_orm", client_name=get_client_name()+ ).inc()return {distinct_id: int(version or 0)for distinct_id, version in PersonDistinctId.objects.db_manager(READ_DB_FOR_PERSONS)diff --git a/posthog/personhog_client/converters.py b/posthog/personhog_client/converters.pyindex d8aded722fc4..bed5dc9c59f7 100644--- a/posthog/personhog_client/converters.py+++ b/posthog/personhog_client/converters.py@@ -71,6 +71,7 @@ def proto_person_to_model(is_identified=person.is_identified,created_at=datetime.fromtimestamp(person.created_at / 1000, tz=UTC) if person.created_at else datetime.now(UTC),last_seen_at=datetime.fromtimestamp(person.last_seen_at / 1000, tz=UTC) if person.last_seen_at else None,+ version=person.version if person.version is not None else 0,)if distinct_ids is not None:obj._distinct_ids = distinct_idsdiff --git a/rust/personhog-replica/.sqlx/query-3b8e37f1ea3b73ca1edc33c8b7ce828f5117978a92cc68a46f1eea476b008c2b.json b/rust/personhog-replica/.sqlx/query-3b8e37f1ea3b73ca1edc33c8b7ce828f5117978a92cc68a46f1eea476b008c2b.jsonnew file mode 100644index 000000000000..c35eae826ee5--- /dev/null+++ b/rust/personhog-replica/.sqlx/query-3b8e37f1ea3b73ca1edc33c8b7ce828f5117978a92cc68a46f1eea476b008c2b.json@@ -0,0 +1,35 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT person_id, distinct_id, version\n FROM posthog_persondistinctid\n WHERE team_id = $1 AND person_id = ANY($2)\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "person_id",+ "type_info": "Int8"+ },+ {+ "ordinal": 1,+ "name": "distinct_id",+ "type_info": "Varchar"+ },+ {+ "ordinal": 2,+ "name": "version",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "Int4",+ "Int8Array"+ ]+ },+ "nullable": [+ false,+ false,+ true+ ]+ },+ "hash": "3b8e37f1ea3b73ca1edc33c8b7ce828f5117978a92cc68a46f1eea476b008c2b"+}diff --git a/rust/personhog-replica/.sqlx/query-b4db7d33974f686be5b779858feebc05a3d076bc09739d9b5302b3f0e7bf3015.json b/rust/personhog-replica/.sqlx/query-b4db7d33974f686be5b779858feebc05a3d076bc09739d9b5302b3f0e7bf3015.jsondeleted file mode 100644index 35bd6713180a..000000000000--- a/rust/personhog-replica/.sqlx/query-b4db7d33974f686be5b779858feebc05a3d076bc09739d9b5302b3f0e7bf3015.json+++ /dev/null@@ -1,30 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT l.person_id, l.distinct_id\n FROM UNNEST($2::bigint[]) AS pid(id)\n CROSS JOIN LATERAL (\n SELECT person_id, distinct_id\n FROM posthog_persondistinctid\n WHERE team_id = $1 AND person_id = pid.id\n LIMIT $3\n ) l\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "person_id",- "type_info": "Int8"- },- {- "ordinal": 1,- "name": "distinct_id",- "type_info": "Varchar"- }- ],- "parameters": {- "Left": [- "Int4",- "Int8Array",- "Int8"- ]- },- "nullable": [- false,- false- ]- },- "hash": "b4db7d33974f686be5b779858feebc05a3d076bc09739d9b5302b3f0e7bf3015"-}diff --git a/rust/personhog-replica/.sqlx/query-ebb1251c44eb2dafd8ac339d9c1e9d59e6cbfddbc59472002726dc8b3dad154b.json b/rust/personhog-replica/.sqlx/query-ebb1251c44eb2dafd8ac339d9c1e9d59e6cbfddbc59472002726dc8b3dad154b.jsonnew file mode 100644index 000000000000..07f76775db62--- /dev/null+++ b/rust/personhog-replica/.sqlx/query-ebb1251c44eb2dafd8ac339d9c1e9d59e6cbfddbc59472002726dc8b3dad154b.json@@ -0,0 +1,36 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT l.person_id, l.distinct_id, l.version\n FROM UNNEST($2::bigint[]) AS pid(id)\n CROSS JOIN LATERAL (\n SELECT person_id, distinct_id, version\n FROM posthog_persondistinctid\n WHERE team_id = $1 AND person_id = pid.id\n LIMIT $3\n ) l\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "person_id",+ "type_info": "Int8"+ },+ {+ "ordinal": 1,+ "name": "distinct_id",+ "type_info": "Varchar"+ },+ {+ "ordinal": 2,+ "name": "version",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "Int4",+ "Int8Array",+ "Int8"+ ]+ },+ "nullable": [+ false,+ false,+ true+ ]+ },+ "hash": "ebb1251c44eb2dafd8ac339d9c1e9d59e6cbfddbc59472002726dc8b3dad154b"+}diff --git a/rust/personhog-replica/.sqlx/query-fd95b734341b8695b1d72e7f8cac128492ea1e3d21312c5d52a1cb73105c1710.json b/rust/personhog-replica/.sqlx/query-fd95b734341b8695b1d72e7f8cac128492ea1e3d21312c5d52a1cb73105c1710.jsondeleted file mode 100644index 9d73fd6c1cb7..000000000000--- a/rust/personhog-replica/.sqlx/query-fd95b734341b8695b1d72e7f8cac128492ea1e3d21312c5d52a1cb73105c1710.json+++ /dev/null@@ -1,29 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT person_id, distinct_id\n FROM posthog_persondistinctid\n WHERE team_id = $1 AND person_id = ANY($2)\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "person_id",- "type_info": "Int8"- },- {- "ordinal": 1,- "name": "distinct_id",- "type_info": "Varchar"- }- ],- "parameters": {- "Left": [- "Int4",- "Int8Array"- ]- },- "nullable": [- false,- false- ]- },- "hash": "fd95b734341b8695b1d72e7f8cac128492ea1e3d21312c5d52a1cb73105c1710"-}diff --git a/rust/personhog-replica/src/service/mod.rs b/rust/personhog-replica/src/service/mod.rsindex 1c8b9bc2df2f..a663ff8ea72d 100644--- a/rust/personhog-replica/src/service/mod.rs+++ b/rust/personhog-replica/src/service/mod.rs@@ -292,7 +292,7 @@ impl PersonHogReplica for PersonHogReplicaService {.or_default().push(DistinctIdWithVersion {distinct_id: mapping.distinct_id,- version: None, // This endpoint doesn't return version per distinct_id+ version: mapping.version,});}diff --git a/rust/personhog-replica/src/storage/postgres/distinct_id.rs b/rust/personhog-replica/src/storage/postgres/distinct_id.rsindex a92ddc12ef85..653630811c18 100644--- a/rust/personhog-replica/src/storage/postgres/distinct_id.rs+++ b/rust/personhog-replica/src/storage/postgres/distinct_id.rs@@ -110,10 +110,10 @@ impl DistinctIdLookup for PostgresStorage {sqlx::query_as!(DistinctIdMapping,r#"- SELECT l.person_id, l.distinct_id+ SELECT l.person_id, l.distinct_id, l.versionFROM UNNEST($2::bigint[]) AS pid(id)CROSS JOIN LATERAL (- SELECT person_id, distinct_id+ SELECT person_id, distinct_id, versionFROM posthog_persondistinctidWHERE team_id = $1 AND person_id = pid.idLIMIT $3@@ -130,7 +130,7 @@ impl DistinctIdLookup for PostgresStorage {sqlx::query_as!(DistinctIdMapping,r#"- SELECT person_id, distinct_id+ SELECT person_id, distinct_id, versionFROM posthog_persondistinctidWHERE team_id = $1 AND person_id = ANY($2)"#,diff --git a/rust/personhog-replica/src/storage/types/person.rs b/rust/personhog-replica/src/storage/types/person.rsindex cd4f42c80ea1..c6b9e14b4278 100644--- a/rust/personhog-replica/src/storage/types/person.rs+++ b/rust/personhog-replica/src/storage/types/person.rs@@ -19,6 +19,7 @@ pub struct Person {pub struct DistinctIdMapping {pub person_id: i64,pub distinct_id: String,+ pub version: Option<i64>,}#[derive(Debug, Clone)]