: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(persons): cap distinct ids per person tombstone rpc (#122941)
Eli Reismane4ba09b9929a
diff --git a/posthog/models/person/bulk_delete.py b/posthog/models/person/bulk_delete.pyindex 4d48a7b7aa96..7d594fc7c83a 100644--- a/posthog/models/person/bulk_delete.py+++ b/posthog/models/person/bulk_delete.py@@ -238,6 +238,10 @@ QUEUED_DELETION_DISTINCT_ID_PAGE_SIZE = 5000# chunk cannot pile up on a worker; the bound is this cap plus one person's worth of IDs.QUEUED_DELETION_DISTINCT_IDS_PER_BATCH = 20_000+# The replica tombstones each RPC in one transaction, and its distinct-ID update can read from disk for+# every row. This cap keeps that update well under the replica's bulk statement timeout. The step batch+# above stays larger, so the other deletion steps keep their batching.+TOMBSTONE_DISTINCT_IDS_PER_RPC = 1_000# Below the replica's cap of 2,500 distinct IDs per person, so a person that returns this many may have more.TOMBSTONE_DISTINCT_ID_PREFETCH_LIMIT = 100@@ -685,7 +689,7 @@ def _committed_tombstones(def _batches_by_distinct_id_count(persons: builtins.list[Person]) -> Iterator[builtins.list[Person]]:- """Group persons so one RPC carries at most QUEUED_DELETION_DISTINCT_IDS_PER_BATCH distinct IDs.+ """Group persons so one RPC carries at most TOMBSTONE_DISTINCT_IDS_PER_RPC distinct IDs.Every caller resolves persons with their distinct IDs loaded, and Person.distinct_ids raiseswhen they are not, so a person never counts as narrower than it is. A single person wider than@@ -695,7 +699,7 @@ def _batches_by_distinct_id_count(persons: builtins.list[Person]) -> Iterator[bucount = 0for person in persons:width = len(person.distinct_ids)- if batch and count + width > QUEUED_DELETION_DISTINCT_IDS_PER_BATCH:+ if batch and count + width > TOMBSTONE_DISTINCT_IDS_PER_RPC:yield batchbatch, count = [], 0batch.append(person)diff --git a/posthog/models/person/test/test_bulk_delete_helpers.py b/posthog/models/person/test/test_bulk_delete_helpers.pyindex 4a30b1da2768..d6b64eae09cf 100644--- a/posthog/models/person/test/test_bulk_delete_helpers.py+++ b/posthog/models/person/test/test_bulk_delete_helpers.py@@ -235,7 +235,7 @@ class DeletePersonsProfileTests(BaseTest):for i, width in enumerate(widths)]with (- patch("posthog.models.person.bulk_delete.QUEUED_DELETION_DISTINCT_IDS_PER_BATCH", budget),+ patch("posthog.models.person.bulk_delete.TOMBSTONE_DISTINCT_IDS_PER_RPC", budget),patch("posthog.models.person.bulk_delete.tombstone_persons_in_postgres",wraps=tombstone_persons_in_postgres,@@ -530,11 +530,12 @@ class ProcessQueuedPersonDeletionTests(BaseTest):rpc.assert_not_called()def test_runs_the_steps_per_batch_once_the_distinct_id_cap_is_reached(self):- p1 = create_person(team=self.team, distinct_ids=["a1", "a2", "a3"], properties={})- p2 = create_person(team=self.team, distinct_ids=["b1", "b2", "b3"], properties={})+ p1 = create_person(team=self.team, distinct_ids=["a1", "a2"], properties={})+ p2 = create_person(team=self.team, distinct_ids=["b1", "b2"], properties={})p3 = create_person(team=self.team, distinct_ids=["c1"], properties={})with (patch("posthog.models.person.bulk_delete.QUEUED_DELETION_DISTINCT_IDS_PER_BATCH", 4),+ patch("posthog.models.person.bulk_delete.TOMBSTONE_DISTINCT_IDS_PER_RPC", 3),patch("posthog.models.person.bulk_delete.queue_person_training_deletion") as training,_rpc() as rpc,_no_publish(),@@ -550,10 +551,11 @@ class ProcessQueuedPersonDeletionTests(BaseTest):)assert result.deleted_count == 3assert result.failures == []- # p1 and p2 fill the cap together; p3 runs in a second, smaller batch. Training deletion is one call per batch.- assert sorted(u for call in rpc.call_args_list for u in call.args[1]) == sorted([p1.uuid, p2.uuid, p3.uuid])+ # p1 and p2 fill the cap together; p3 runs in a second, smaller batch. Training deletion is one call per batch,+ # while the tombstone RPC has its own smaller cap and splits the first batch.+ assert [call.args[1] for call in rpc.call_args_list] == [[p1.uuid], [p2.uuid], [p3.uuid]]assert [sorted(call.args[1]) for call in training.call_args_list] == [- ["a1", "a2", "a3", "b1", "b2", "b3"],+ ["a1", "a2", "b1", "b2"],["c1"],]diff --git a/posthog/temporal/tests/delete_persons/test_delete_persons_workflow.py b/posthog/temporal/tests/delete_persons/test_delete_persons_workflow.pyindex 296a1fa5b2bf..43daf042e328 100644--- a/posthog/temporal/tests/delete_persons/test_delete_persons_workflow.py+++ b/posthog/temporal/tests/delete_persons/test_delete_persons_workflow.py@@ -64,6 +64,7 @@ class TestDeletePersonsActivity:patch("posthog.models.person.bulk_delete.TOMBSTONE_DISTINCT_ID_PREFETCH_LIMIT", 3),patch("posthog.models.person.bulk_delete.QUEUED_DELETION_DISTINCT_ID_PAGE_SIZE", 2),patch("posthog.models.person.bulk_delete.QUEUED_DELETION_DISTINCT_IDS_PER_BATCH", 3),+ patch("posthog.models.person.bulk_delete.TOMBSTONE_DISTINCT_IDS_PER_RPC", 3),patch.object(bulk_delete, "tombstone_and_publish_persons", side_effect=record_batch),):fake.add_person(team_id=1, person_id=10, uuid=str(uuid_lib.uuid4()), distinct_ids=["10-a", "10-b"])