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(deletes): keep one row per key in the adhoc deletes dictionary (#99554)

Paweł Szczur911473b39440
diff --git a/posthog/dags/deletes.py b/posthog/dags/deletes.py
index 53c207805371..9594f6f98e9a 100644
--- a/posthog/dags/deletes.py
+++ b/posthog/dags/deletes.py
@@ -307,7 +307,15 @@ class AdhocEventDeletesDictionary(Dictionary):
@property
def query(self) -> str:
- return f"SELECT team_id, uuid, created_at FROM {self.source.qualified_name} WHERE (team_id, uuid) not in (SELECT team_id, uuid FROM {self.source.qualified_name} WHERE is_deleted = 1)"
+ # Grouped rather than filtered with NOT IN: the source is a ReplacingMergeTree, so a key
+ # inserted twice reads as two rows until a merge collapses them. The dictionary keeps one
+ # row per key whichever way, but which one it keeps follows the order the source rows
+ # arrive, and that order is not stable when a host parses a staged Parquet in parallel.
+ # max(is_deleted) = 0 is the same exclusion the NOT IN subquery made, on one scan.
+ return (
+ f"SELECT team_id, uuid, max(created_at) AS created_at FROM {self.source.qualified_name} "
+ f"GROUP BY team_id, uuid HAVING max(is_deleted) = 0"
+ )
def staged(self) -> StagedDictionary:
return StagedDictionary(
diff --git a/posthog/dags/tests/test_deletes.py b/posthog/dags/tests/test_deletes.py
index b65d3b4a07c2..4e7277a0ea6a 100644
--- a/posthog/dags/tests/test_deletes.py
+++ b/posthog/dags/tests/test_deletes.py
@@ -1,4 +1,4 @@
-from datetime import datetime, timedelta
+from datetime import UTC, datetime, timedelta
from functools import partial
from typing import cast
from uuid import UUID
@@ -13,6 +13,7 @@ from clickhouse_driver import Client
from dagster import build_op_context
from posthog.clickhouse.cluster import ClickhouseCluster, LightweightDeleteMutationRunner
+from posthog.dags.common.staged_dictionary import create_on_every_cluster
from posthog.dags.deletes import (
_DELETE_PREDICATE,
AdhocEventDeletesDictionary,
@@ -793,6 +794,71 @@ def test_a_rerun_stages_over_the_previous_runs_object(cluster: ClickhouseCluster
cluster.any_host(table.drop).result()
+def test_the_adhoc_dictionary_holds_one_row_per_key_when_the_source_has_duplicates(cluster: ClickhouseCluster):
+ # The source is a ReplacingMergeTree, so a uuid requested twice reads as two rows until a merge
+ # collapses them. A dictionary keeps one row per key either way, but a source query that emits
+ # both leaves the winner to the order the rows arrive, and that order is not stable when a host
+ # parses a staged Parquet in parallel. Two clusters then disagree and the run is blocked.
+ adhoc = AdhocEventDeletesDictionary(source=AdhocEventDeletesTable())
+ team_id = 424244
+ uuid = UUID(int=13)
+ earlier = datetime(2026, 8, 28, 9, 0, 0, tzinfo=UTC)
+ later = datetime(2026, 8, 28, 11, 0, 0, tzinfo=UTC)
+
+ def insert_duplicate_requests(client: Client) -> None:
+ client.execute(
+ "INSERT INTO adhoc_events_deletion (team_id, uuid, created_at) VALUES",
+ [(team_id, uuid, earlier), (team_id, uuid, later)],
+ )
+
+ def read_back(client: Client) -> list:
+ return client.execute(f"SELECT count(), max(created_at) FROM {adhoc.qualified_name} WHERE team_id = {team_id}")
+
+ try:
+ cluster.any_host(insert_duplicate_requests).result()
+ cluster.any_host(partial(adhoc.create, shards=1, max_execution_time=0, max_memory_usage=0)).result()
+ cluster.any_host(adhoc.load).result()
+
+ [[held, created_at]] = cluster.any_host(read_back).result()
+
+ assert held == 1
+ assert created_at == later
+ finally:
+ cluster.any_host(adhoc.drop).result()
+ cluster.any_host(
+ lambda client: client.execute(f"DELETE FROM adhoc_events_deletion WHERE team_id = {team_id}")
+ ).result()
+
+
+def test_creating_on_a_second_cluster_points_it_at_the_staged_object(cluster: ClickhouseCluster):
+ # A cluster that shares no Keeper with the source table never receives it, so its dictionary
+ # has to read the staged object. A call site that hands it the source query instead leaves it
+ # loading from a table it cannot see, and the dictionary fails or comes back empty.
+ table = PendingDeletesTable(timestamp=datetime(2026, 8, 26, 10, 11, 14))
+ dictionary = PendingDeletesDictionary(source=table)
+ sibling = cluster.sibling(django_settings.CLICKHOUSE_SINGLE_SHARD_CLUSTER)
+ context = build_op_context()
+
+ def show_create(client: Client) -> str:
+ [[query]] = client.execute(f"SHOW CREATE DICTIONARY {dictionary.qualified_name}")
+ return query
+
+ try:
+ cluster.any_host(table.create).result()
+ cluster.any_host(partial(_insert_pending_deletes, table)).result()
+
+ create_on_every_cluster(
+ context, [cluster, sibling], dictionary, shards=1, max_execution_time=0, max_memory_usage=0
+ )
+
+ assert dictionary.staged().key in sibling.any_host(show_create).result()
+ finally:
+ cluster.any_host(dictionary.drop).result()
+ cluster.any_host(table.drop).result()
+
+
@pytest.mark.django_db
def test_the_post_sweep_count_reports_rows_the_sweep_left_behind(cluster: ClickhouseCluster):
# The count only earns its place if it can come back non-zero. A predicate or parameter that