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.

feat(clickhouse): trigger the deletion sweep after the GDPR deletes run (#82729)

Eli Reisman8ccdacc60184
diff --git a/posthog/dags/clickhouse_cleanup.py b/posthog/dags/clickhouse_cleanup.py
index b89cbbd15cfe..fa8fdbabda8e 100644
--- a/posthog/dags/clickhouse_cleanup.py
+++ b/posthog/dags/clickhouse_cleanup.py
@@ -46,6 +46,7 @@ from posthog.dags.common.staged_dictionary import (
create_on_every_cluster,
load_and_verify_on_every_cluster,
)
+from posthog.dags.deletes import deletes_job
from posthog.dataclasses import frozen
from posthog.models.async_deletion.delete_cohorts import sweep_cohort_deletions
from posthog.models.person.sql import PERSON_DISTINCT_ID2_TABLE, PERSONS_TABLE
@@ -1354,6 +1355,9 @@ def drop_assets_on_failure(context: dagster.HookContext) -> None:
# Matched by a run-queue limit of 1 in charts (argocd/dagster/deployment_settings), so a
# second sweep run queues instead of running concurrently. The janitor depends on this.
"clickhouse_deletion_sweep_concurrency": "v1",
+ # Nothing else bounds total runtime: the per-wait timeouts can each fire without the run
+ # ending. Safe to lose a killed run, since every op is idempotent.
+ "dagster/max_runtime": 43200,
},
)
def clickhouse_deletion_sweep_job():
@@ -1371,3 +1375,83 @@ def clickhouse_deletion_sweep_job():
# Each op takes the previous op's output, which is what keeps the sweeps in sequence.
drop_snapshot_assets(delete_persons(run))
+
+
+# What the sensor launches with. Every field is pinned so a changed default cannot move production,
+# and every value is a ceiling, which is what lets one config serve both regions.
+SCHEDULED_RUN_CONFIG = {
+ "ops": {
+ "clear_removed_cohort_data": {
+ "config": {
+ "dry_run": False,
+ "cleanup": True,
+ "cohort_sweep": True,
+ # Not DEFAULT_MAX_COHORTS: at 2,000 this op is two thirds of the run and still
+ # needs ~24 US runs to drain. Draining is an attended campaign, not weekly work.
+ "max_cohorts": 100,
+ "team_batches": DEFAULT_TEAM_BATCHES,
+ # Never 0: unbounded takes the whole backlog in one run. 30M outpaces both
+ # regions' weekly arrivals, so the backlog converges.
+ "max_persons": 30_000_000,
+ "shards": 16,
+ "max_execution_time": 1800,
+ # The orphaned distinct id populate, not the persons one, sets this. 64 GiB failed it.
+ "max_memory_usage": 128 * 1024**3,
+ "dictionary_load_timeout": 1800,
+ "mutation_stall_timeout": 1800,
+ "mutation_capacity_timeout": 3600,
+ # The only bound on a slow but healthy mutation. Worst measured: 5,844 s.
+ "mutation_wait_deadline": 21600,
+ "min_team_id": 0,
+ "max_team_id": 0,
+ }
+ }
+ }
+}
+
+
[email protected]_status_sensor(
+ run_status=dagster.DagsterRunStatus.SUCCESS,
+ monitored_jobs=[deletes_job],
+ request_job=clickhouse_deletion_sweep_job,
+ # Enabled on registration. A sensor that never fires raises no alert, so shipping it stopped
+ # would end weekly hard-deletion silently.
+ default_status=dagster.DefaultSensorStatus.RUNNING,
+ minimum_interval_seconds=60,
+)
+def run_cleanup_sweep_after_deletes(
+ context: dagster.RunStatusSensorContext,
+) -> dagster.RunRequest | dagster.SkipReason:
+ """Chain the sweep behind the GDPR deletes run instead of giving it a cron of its own.
+
+ Both jobs mutate person and person_distinct_id2 across the cluster, and deletes_job starts
+ after the Saturday-night squash and can run for hours, so any fixed cron for the sweep
+ overlaps it in the worst weeks. Chaining on success serializes the weekend into
+ squash -> deletes_job -> sweep. A week where the upstream chain fails skips the sweep, which
+ is safe: the worklist derives from live tombstones, so the next run picks everything up.
+ """
+ active = context.instance.get_run_records(
+ dagster.RunsFilter(
+ job_name=clickhouse_deletion_sweep_job.name,
+ statuses=[
+ dagster.DagsterRunStatus.QUEUED,
+ dagster.DagsterRunStatus.NOT_STARTED,
+ dagster.DagsterRunStatus.STARTING,
+ dagster.DagsterRunStatus.STARTED,
+ # A canceling run still counts: its last mutation keeps applying server-side.
+ # wait_for_mutation_capacity holds stragglers off the tables, and the next
+ # run's janitor reaps a canceled run's dictionaries.
+ dagster.DagsterRunStatus.CANCELING,
+ ],
+ ),
+ limit=1,
+ )
+ if active:
+ # deletes_job can also be launched by hand, so back-to-back successes are possible; a
+ # second sweep mutating the same tables concurrently is the one thing this must prevent.
+ return dagster.SkipReason("a deletion sweep run is already active")
+
+ # The run_key makes each deletes_job success launch at most one sweep. The sensor is the only
+ # launch that deletes: dry_run defaults to true, so an ad-hoc run from the Dagster UI reports
+ # what it would remove rather than removing it.
+ return dagster.RunRequest(run_key=context.dagster_run.run_id, run_config=SCHEDULED_RUN_CONFIG)
diff --git a/posthog/dags/locations/clickhouse.py b/posthog/dags/locations/clickhouse.py
index d25de83685dc..a8f0614f0d6c 100644
--- a/posthog/dags/locations/clickhouse.py
+++ b/posthog/dags/locations/clickhouse.py
@@ -67,6 +67,7 @@ defs = dagster.Definitions(
data_deletion_requests.auto_approve_deletion_requests_schedule,
],
sensors=[
+ clickhouse_cleanup.run_cleanup_sweep_after_deletes,
deletes.run_deletes_after_squash,
data_deletion_requests.data_deletion_request_pickup_sensor,
data_deletion_requests.verify_queued_deletion_requests,
diff --git a/posthog/dags/tests/test_clickhouse_cleanup.py b/posthog/dags/tests/test_clickhouse_cleanup.py
index 70d740e0439f..4aa9df95bd74 100644
--- a/posthog/dags/tests/test_clickhouse_cleanup.py
+++ b/posthog/dags/tests/test_clickhouse_cleanup.py
@@ -24,6 +24,7 @@ from posthog.clickhouse.cluster import ClickhouseCluster
from posthog.dags import clickhouse_cleanup
from posthog.dags.clickhouse_cleanup import (
PG_CLEANUP_QUEUE_TABLE,
+ SCHEDULED_RUN_CONFIG,
MutationProgress,
MutationStalled,
MutationStatus,
@@ -1064,3 +1065,63 @@ def test_a_mutation_that_fails_every_attempt_fails_the_run_and_gets_killed(
return count
assert cluster.any_host(unfinished_mutations).result() == 0
+
+
+def _deletes_success_context(instance: dagster.DagsterInstance) -> dagster.RunStatusSensorContext:
+ deletes_run = dagster.DagsterRun(job_name="deletes_job", run_id="11111111-1111-1111-1111-111111111111")
+ return dagster.build_run_status_sensor_context(
+ sensor_name="run_cleanup_sweep_after_deletes",
+ dagster_event=dagster.DagsterEvent(
+ event_type_value=dagster.DagsterEventType.RUN_SUCCESS.value, job_name="deletes_job"
+ ),
+ dagster_instance=instance,
+ dagster_run=deletes_run,
+ )
+
+
+def test_the_sweep_sensor_launches_a_real_run_after_deletes():
+ # Weekly hard-deletion has no Celery fallback once the cutover lands. A sensor that ships
+ # stopped, or loses the dry_run override, silently ends or no-ops the sweep.
+ sensor = clickhouse_cleanup.run_cleanup_sweep_after_deletes
+ assert sensor.default_status == dagster.DefaultSensorStatus.RUNNING
+
+ instance = dagster.DagsterInstance.ephemeral()
+ request = sensor(_deletes_success_context(instance))
+ assert isinstance(request, dagster.RunRequest)
+ assert request.run_config == SCHEDULED_RUN_CONFIG
+ # One sweep per deletes_job success: re-evaluating the same event must not launch another.
+ assert request.run_key == "11111111-1111-1111-1111-111111111111"
+
+
+def test_the_job_carries_the_operational_tags():
+ # The charts run-queue limit matches the concurrency tag, and the janitor's unconditional reap
+ # depends on it. max_runtime is the only bound on total runtime.
+ tags = clickhouse_deletion_sweep_job.tags
+ assert tags["clickhouse_deletion_sweep_concurrency"] == "v1"
+ assert int(tags["dagster/max_runtime"]) == 43200
+
+
+def test_the_scheduled_config_pins_every_setting_the_sweep_reads():
+ # A field added to CleanupConfig without a scheduled value would run production on whatever
+ # the code default happens to be, which is exactly what pinning this config prevents.
+ pinned = set(SCHEDULED_RUN_CONFIG["ops"]["clear_removed_cohort_data"]["config"])
+ declared = set(clickhouse_cleanup.CleanupConfig.model_fields)
+ assert declared == pinned
+
+
+ "status",
+ [
+ dagster.DagsterRunStatus.STARTED,
+ # A canceling run's last mutation keeps applying server-side, so it still counts as active.
+ dagster.DagsterRunStatus.CANCELING,
+ ],
+)
+def test_the_sweep_sensor_skips_while_a_sweep_is_already_active(status: dagster.DagsterRunStatus):
+ # Two concurrent sweeps would mutate person and person_distinct_id2 at the same time, which
+ # is the contention this sensor exists to prevent.
+ instance = dagster.DagsterInstance.ephemeral()
+ instance.create_run_for_job(job_def=clickhouse_deletion_sweep_job, status=status)
+
+ result = clickhouse_cleanup.run_cleanup_sweep_after_deletes(_deletes_success_context(instance))
+ assert isinstance(result, dagster.SkipReason)