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.

chore(data-warehouse): add CDC sweeper observability and raise cleanup timeout (#65587)

Daniel RCa2de4f64a7e0
diff --git a/posthog/temporal/data_imports/cdc/activities.py b/posthog/temporal/data_imports/cdc/activities.py
index 96032d06e22e..f674deae085e 100644
--- a/posthog/temporal/data_imports/cdc/activities.py
+++ b/posthog/temporal/data_imports/cdc/activities.py
@@ -1061,6 +1061,9 @@ def cleanup_orphan_slots_activity() -> None:
sources = ExternalDataSource.objects.filter(source_type__in=cdc_supported_source_types()).iterator(chunk_size=100)
sources_checked = 0
+ sources_errored = 0
+ slots_dropped = 0
+ sweep_started = dt.datetime.now(tz=dt.UTC)
# A single source's management connection (10s connect_timeout × several ops) can stall the
# loop, so heartbeat from a background thread rather than once per iteration — otherwise a
# stalled source would starve heartbeats and Temporal would kill the whole sweep.
@@ -1075,6 +1078,7 @@ def cleanup_orphan_slots_activity() -> None:
cdc_config = adapter.parse_cdc_config(source)
except Exception:
log.exception("failed_to_parse_cdc_config", source_id=str(source.id))
+ sources_errored += 1
continue
# Restore the original filter semantics on decrypted values: skip sources that
@@ -1106,17 +1110,21 @@ def cleanup_orphan_slots_activity() -> None:
try:
with adapter.management_connection(source, connect_timeout=10) as conn:
adapter.drop_resources(conn, cdc_config.slot_name, cdc_config.publication_name)
+ slots_dropped += 1
except Exception:
source_log.exception("failed_to_cleanup_deleted_source_slot")
+ sources_errored += 1
continue
# 2. Active sources — check WAL lag
+ source_started = dt.datetime.now(tz=dt.UTC)
try:
with adapter.management_connection(source, connect_timeout=10) as conn:
lag_bytes = adapter.get_lag_bytes(conn, cdc_config.slot_name)
retention_cap_mb = adapter.get_retention_cap_mb(conn)
except Exception:
source_log.exception("failed_to_check_slot_lag")
+ sources_errored += 1
continue
if lag_bytes is None:
@@ -1143,13 +1151,19 @@ def cleanup_orphan_slots_activity() -> None:
with adapter.management_connection(source, connect_timeout=10) as conn:
adapter.drop_resources(conn, cdc_config.slot_name, cdc_config.publication_name)
+ slots_dropped += 1
source.status = ExternalDataSource.Status.ERROR
source.save(update_fields=["status", "updated_at"])
except Exception:
source_log.exception("failed_to_auto_drop_slot")
+ sources_errored += 1
elif cdc_config.management_mode == "self_managed":
- source.status = ExternalDataSource.Status.ERROR
- source.save(update_fields=["status", "updated_at"])
+ try:
+ source.status = ExternalDataSource.Status.ERROR
+ source.save(update_fields=["status", "updated_at"])
+ except Exception:
+ source_log.exception("failed_to_update_source_status")
+ sources_errored += 1
elif lag_mb >= cdc_config.lag_warning_threshold_mb:
source_log.warning(
@@ -1158,4 +1172,16 @@ def cleanup_orphan_slots_activity() -> None:
threshold_mb=cdc_config.lag_warning_threshold_mb,
)
- log.info("cleanup_orphan_slots_completed", sources_checked=sources_checked)
+ source_log.info(
+ "slot_lag_checked",
+ lag_mb=round(lag_mb, 1),
+ duration_ms=round((dt.datetime.now(tz=dt.UTC) - source_started).total_seconds() * 1000),
+ )
+
+ log.info(
+ "cleanup_orphan_slots_completed",
+ sources_checked=sources_checked,
+ sources_errored=sources_errored,
+ slots_dropped=slots_dropped,
+ duration_s=round((dt.datetime.now(tz=dt.UTC) - sweep_started).total_seconds(), 1),
+ )
diff --git a/posthog/temporal/data_imports/cdc/workflows.py b/posthog/temporal/data_imports/cdc/workflows.py
index 1f8671602a25..e6ecf956a7d0 100644
--- a/posthog/temporal/data_imports/cdc/workflows.py
+++ b/posthog/temporal/data_imports/cdc/workflows.py
@@ -73,7 +73,7 @@ class CDCSlotCleanupWorkflow(PostHogWorkflow):
await workflow.execute_activity(
cleanup_orphan_slots_activity,
- start_to_close_timeout=dt.timedelta(minutes=15),
+ start_to_close_timeout=dt.timedelta(minutes=30),
heartbeat_timeout=dt.timedelta(minutes=2),
retry_policy=RetryPolicy(
initial_interval=dt.timedelta(seconds=30),