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(clickhouse): stop reporting duplicate-PK probe budget exhaustion as errors (#63742)

Tom Owersc165c98a4aa8
diff --git a/posthog/temporal/data_imports/sources/clickhouse/clickhouse.py b/posthog/temporal/data_imports/sources/clickhouse/clickhouse.py
index d04893f8d165..a745513a3b98 100644
--- a/posthog/temporal/data_imports/sources/clickhouse/clickhouse.py
+++ b/posthog/temporal/data_imports/sources/clickhouse/clickhouse.py
@@ -728,6 +728,16 @@ _DUPLICATE_PK_CHECK_SETTINGS: dict[str, Any] = {
"max_memory_usage": 1_000_000_000,
}
+# ClickHouse error names raised when the bounded probe exhausts one of its own
+# budgets (`max_memory_usage` → MEMORY_LIMIT_EXCEEDED, `max_execution_time` →
+# TIMEOUT_EXCEEDED). `optimize_aggregation_in_order` keeps the GROUP BY
+# streaming, but on large/slow (e.g. S3-backed) source tables the scan can
+# still hit these caps before `read_overflow_mode='break'` truncates on rows.
+# When that happens the probe behaves exactly as designed — fall back to
+# "assume duplicates" — so it's an expected outcome, not an error worth
+# reporting to error tracking.
+_DUPLICATE_PK_PROBE_BUDGET_ERRORS: tuple[str, ...] = ("MEMORY_LIMIT_EXCEEDED", "TIMEOUT_EXCEEDED")
+
def _has_duplicate_primary_keys(
client: ClickHouseClient,
@@ -764,14 +774,18 @@ def _has_duplicate_primary_keys(
result = client.query(query, settings=_DUPLICATE_PK_CHECK_SETTINGS)
return len(result.result_rows) > 0
except ClickHouseError as e:
- # Any unexpected server error is treated as "assume duplicates" —
- # safer to force append mode than to merge against a key we couldn't
- # verify. (We don't hit max_rows_to_read here because
- # read_overflow_mode='break' turns that into a silent truncation.)
+ # Any server error is treated as "assume duplicates" — safer to force
+ # append mode than to merge against a key we couldn't verify. (We don't
+ # hit max_rows_to_read here because read_overflow_mode='break' turns
+ # that into a silent truncation.)
logger.warning(
f"_has_duplicate_primary_keys: assuming duplicates exist (probe failed for {database}.{table_name}): {e}"
)
- capture_exception(e)
+ # Exhausting the probe's own memory/time budget is the designed fallback
+ # on large source tables, not an exceptional condition — don't flood
+ # error tracking with it. Capture only genuinely unexpected errors.
+ if not any(name in str(e) for name in _DUPLICATE_PK_PROBE_BUDGET_ERRORS):
+ capture_exception(e)
return True
diff --git a/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py b/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py
index d0bb39702bd2..bc9d6dce6794 100644
--- a/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py
+++ b/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py
@@ -681,11 +681,48 @@ class TestHasDuplicatePrimaryKeys:
def test_fails_safe_to_true_on_clickhouse_error(self):
client = MagicMock()
- client.query.side_effect = ClickHouseError("Code: 241. Memory limit exceeded")
+ client.query.side_effect = ClickHouseError("Code: 62. DB::Exception: Syntax error")
# On error we must assume duplicates exist so the incremental merge is
# blocked. Returning False here would silently corrupt the Delta table.
assert _has_duplicate_primary_keys(client, "db", "t", ["id"], self._logger()) is True
+ def test_captures_unexpected_clickhouse_error(self):
+ from posthog.temporal.data_imports.sources.clickhouse import clickhouse as ch_module
+
+ client = MagicMock()
+ client.query.side_effect = ClickHouseError("Code: 62. DB::Exception: Syntax error")
+
+ with patch.object(ch_module, "capture_exception") as mock_capture:
+ assert _has_duplicate_primary_keys(client, "db", "t", ["id"], self._logger()) is True
+
+ mock_capture.assert_called_once()
+
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # max_memory_usage cap (code 241)
+ "HTTPDriver received ClickHouse error code 241\n Code: 241. DB::Exception: Query memory limit "
+ "exceeded: would use 958.14 MiB, maximum: 953.67 MiB: While executing AggregatingInOrderTransform. "
+ "(MEMORY_LIMIT_EXCEEDED)\n",
+ # max_execution_time cap (code 159)
+ "HTTPDriver received ClickHouse error code 159\n Code: 159. DB::Exception: Timeout exceeded: "
+ "elapsed 53343.4 ms, maximum: 30000 ms. (TIMEOUT_EXCEEDED)\n",
+ ],
+ )
+ def test_probe_budget_exhaustion_not_captured(self, error_msg):
+ from posthog.temporal.data_imports.sources.clickhouse import clickhouse as ch_module
+
+ client = MagicMock()
+ client.query.side_effect = ClickHouseError(error_msg)
+
+ with patch.object(ch_module, "capture_exception") as mock_capture:
+ # Still assumes duplicates (safe append mode), but the probe hitting
+ # its own memory/time budget is the designed fallback — not error
+ # tracking noise.
+ assert _has_duplicate_primary_keys(client, "db", "t", ["id"], self._logger()) is True
+
+ mock_capture.assert_not_called()
+
def test_passes_bounded_settings(self):
client = MagicMock()
result = MagicMock()