: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(clickhouse): drop events_json from the person_id squash targets (#105148)
Paweł Szczurcd0c5f1f286c
diff --git a/docs/internal/clickhouse-deletion-coverage.md b/docs/internal/clickhouse-deletion-coverage.mdindex c715018562a4..039b74135095 100644--- a/docs/internal/clickhouse-deletion-coverage.md+++ b/docs/internal/clickhouse-deletion-coverage.md@@ -83,15 +83,17 @@ ClickHouse has no S3 dictionary source, but that source runs its query locally,- Retention belongs to the bucket lifecycle policy, set through `DICTIONARY_STAGING_S3_*`. Nothing deletes the objects.The same staging carries the person-overrides squash, which is not a deletion but has the identical problem.-`squash_person_overrides` rewrites `person_id` on every table in `SQUASH_TARGETS` — `sharded_events`, `sharded_events_json` and `sharded_flag_evaluations` — through a mutation that joins a snapshot dictionary, then deletes the overrides it just applied.+`squash_person_overrides` rewrites `person_id` on every table in `SQUASH_TARGETS` — `sharded_events` and `sharded_flag_evaluations` — through a mutation that joins a snapshot dictionary, then deletes the overrides it just applied.Skipping one of those tables is worse than under-deleting: the overrides that record the correct `person_id` are gone in the next op, so the divergence is permanent.+`sharded_events_json` is deliberately not in the list, so the squash never dispatches to the events cluster.+A row a merge stranded there keeps the absorbed `person_id` until its partition ages out, and nothing later can recover the mapping.`SQUASH_TARGETS` is deliberately its own list rather than `PERSONAL_DATA_TARGETS`, so registering a table for deletion does not silently enroll it in the squash as well.`posthog/dags/common/staged_dictionary.py` holds the piece both jobs share.## Covered tables- `sharded_events` — all sweeps.-- `sharded_events_json` — person, team, queued-uuid and event removal, but `deletes_job` skips it by default today. Property rewriting is unsupported: temporary properties and quarantine diagnostics retain additional copies that the legacy property-removal machinery does not rewrite. Optional: only present after the native-JSON migration. See the known gap below.+- `sharded_events_json` — person, team, queued-uuid and event removal, but `deletes_job` skips it by default today. Not a squash target. Property rewriting is unsupported: temporary properties and quarantine diagnostics retain additional copies that the legacy property-removal machinery does not rewrite. Optional: only present after the native-JSON migration. See the known gap below.- `sharded_flag_evaluations` — person, team, queued-uuid and event removal. Not property removal (below). Optional.- `sharded_posthog_document_embeddings_<model>` — event and team deletion, through `delete_event_documents`. An embedded document is keyed by the id of the thing it describes (`document_id`), and an Event deletion's key is that same id, so the pending dictionary is joined on `(team_id, Event, document_id)`. Every per-model table listed by the error tracking facade's `document_embedding_tables` is swept and counted.@@ -205,8 +207,9 @@ Keeping the fork downstream of person resolution is the contract, tracked on #81Write-time parity is not sufficient on its own, because a later merge moves the person the sweep looks for.After person A merges into B, a deletion of B is queued under B's uuid, so any row still carrying A matches nothing and survives with its event `properties` and its stale `person_id` until the TTL drops the part.-`sharded_flag_evaluations` is a squash target as well as a deletion target for that reason: it is in `SQUASH_TARGETS` in `posthog/dags/person_overrides.py`, and `person_id` is not in its sort key, so it takes the same `ALTER UPDATE` the events tables take.+`sharded_flag_evaluations` is a squash target as well as a deletion target for that reason: it is in `SQUASH_TARGETS` in `posthog/dags/person_overrides.py`, and `person_id` is not in its sort key, so it takes the same `ALTER UPDATE` `sharded_events` takes.A table missing from `SQUASH_TARGETS` strands its rows permanently, because the squash deletes the overrides that recorded the mapping right after applying them.+That is the accepted cost for `sharded_events_json`, which is excluded on purpose.Rows a merge stranded before `sharded_flag_evaluations` joined the list age out with their partition.## Related, and deliberately unchangeddiff --git a/posthog/dags/person_overrides.py b/posthog/dags/person_overrides.pyindex e95713ac594f..cee78530860b 100644--- a/posthog/dags/person_overrides.py+++ b/posthog/dags/person_overrides.py@@ -17,7 +17,7 @@ from posthog.dags.common.staged_dictionary import (load_and_verify_on_every_cluster,)from posthog.dataclasses import frozen-from posthog.models.deletion_targets import EVENTS_TARGETS, FLAG_EVALUATIONS, resolve_placements, sweep_clusters+from posthog.models.deletion_targets import EVENTS, FLAG_EVALUATIONS, resolve_placements, sweep_clustersfrom posthog.models.person.sql import PERSON_DISTINCT_ID_OVERRIDES_TABLE# Every table the squash rewrites person_id on. sharded_flag_evaluations is not an events table,@@ -25,9 +25,13 @@ from posthog.models.person.sql import PERSON_DISTINCT_ID_OVERRIDES_TABLE# uuid against that column. A row a merge left on the absorbed person therefore matches nothing and# survives until its partition ages out, so the squash has to move it too.#+# sharded_events_json is deliberately absent, so the squash never reaches the events cluster. Its+# rows keep the person_id a merge left on them, because this job deletes the overrides that record+# the correct one; see COVERAGE_DOC.+## Deliberately not PERSONAL_DATA_TARGETS: registering a table for deletion should not silently make# it a squash target as well.-SQUASH_TARGETS = (*EVENTS_TARGETS, FLAG_EVALUATIONS)+SQUASH_TARGETS = (EVENTS, FLAG_EVALUATIONS)def _squash_clusters(cluster: ClickhouseCluster) -> list[ClickhouseCluster]:diff --git a/posthog/dags/tests/test_person_overrides.py b/posthog/dags/tests/test_person_overrides.pyindex 8ab83867b7fa..7566b5948a12 100644--- a/posthog/dags/tests/test_person_overrides.py+++ b/posthog/dags/tests/test_person_overrides.py@@ -27,8 +27,8 @@ from posthog.dags.person_overrides import ()from posthog.dags.tests.conftest import insert_flag_evaluationsfrom posthog.models.async_deletion import AsyncDeletion, DeletionType-from posthog.models.deletion_targets import EVENTS, EVENTS_JSON, FLAG_EVALUATIONS, TargetPlacement-from posthog.models.event.sql import EVENTS_DATA_TABLE, EVENTS_JSON_DATA_TABLE+from posthog.models.deletion_targets import EVENTS, FLAG_EVALUATIONS, TargetPlacement+from posthog.models.event.sql import EVENTS_DATA_TABLEfrom posthog.models.flag_evaluations.sql import FLAG_EVALUATIONS_DATA_TABLE@@ -270,22 +270,20 @@ def test_a_staged_snapshot_dictionary_holds_the_same_rows_as_the_snapshot_table(@pytest.mark.django_dbdef test_run_person_id_update_mutations_rewrites_each_target_on_its_own_cluster(cluster: ClickhouseCluster):- # sharded_events_json and sharded_flag_evaluations may each sit on a cluster whose shards only- # its own handle enumerates. Running one of those rewrites over the job's handle would skip its- # rows, and the overrides that record the correct person_id are deleted in the very next op, so- # the divergence would be permanent. The assertion is keyed by table because a weaker one --- # that some mutation reached the sibling -- still passes when a target is dropped entirely.+ # A squash target may sit on a cluster whose shards only its own handle enumerates. Running its+ # rewrite over the job's handle would skip those rows, and the overrides that record the correct+ # person_id are deleted in the very next op, so the divergence would be permanent. The assertion+ # is keyed by table because a weaker one -- that some mutation reached the sibling -- still+ # passes when a target is dropped entirely.dictionary = _create_snapshot_with(cluster, [(1, "a", UUID(int=7), 3)])sibling = cluster.sibling(django_settings.CLICKHOUSE_SINGLE_SHARD_CLUSTER)placements = [TargetPlacement(target=EVENTS, cluster=cluster),- TargetPlacement(target=EVENTS_JSON, cluster=sibling),TargetPlacement(target=FLAG_EVALUATIONS, cluster=sibling),]calls = Mock()enqueued = {EVENTS_DATA_TABLE(): {0: Mock()},- EVENTS_JSON_DATA_TABLE: {0: Mock()},FLAG_EVALUATIONS_DATA_TABLE: {0: Mock()},}@@ -303,12 +301,12 @@ def test_run_person_id_update_mutations_rewrites_each_target_on_its_own_cluster(calls.attach_mock(wait_for_mutations, "wait")run_person_id_update_mutations(cluster, dictionary)- # This assertion names the targets literally instead of reusing SQUASH_TARGETS or EVENTS_TARGETS.- # Either constant would still match after someone drops a target from its definition.- resolve_placements.assert_called_once_with(cluster, (EVENTS, EVENTS_JSON, FLAG_EVALUATIONS))+ # This assertion names the targets literally instead of reusing SQUASH_TARGETS. That constant+ # would still match after someone adds sharded_events_json back to it, which would send the+ # squash to the events cluster.+ resolve_placements.assert_called_once_with(cluster, (EVENTS, FLAG_EVALUATIONS))assert {enqueue.args[0].table: enqueue.args[1] for enqueue in enqueue_on_shards.call_args_list} == {EVENTS_DATA_TABLE(): cluster,- EVENTS_JSON_DATA_TABLE: sibling,FLAG_EVALUATIONS_DATA_TABLE: sibling,}# Each wait has to receive the mutations its own enqueue returned. A wait handed an empty set@@ -316,12 +314,11 @@ def test_run_person_id_update_mutations_rewrites_each_target_on_its_own_cluster(wait_for_mutations.assert_has_calls([call(cluster, enqueued[EVENTS_DATA_TABLE()]),- call(sibling, enqueued[EVENTS_JSON_DATA_TABLE]),call(sibling, enqueued[FLAG_EVALUATIONS_DATA_TABLE]),],any_order=True,)# Waiting on each mutation as it is enqueued would cost the sum of their completion times# rather than the longest, which is the whole reason the op enqueues in one pass.- assert [name for name, *_ in calls.mock_calls] == ["enqueue"] * 3 + ["wait"] * 3+ assert [name for name, *_ in calls.mock_calls] == ["enqueue"] * 2 + ["wait"] * 2cluster.any_host(dictionary.source.drop).result()