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): treat ClickHouse memory limit exceeded as non-retryable (#63734)

Tom Owers916f94324df4
diff --git a/posthog/temporal/data_imports/sources/clickhouse/source.py b/posthog/temporal/data_imports/sources/clickhouse/source.py
index b1ef42045295..b5bb22646e16 100644
--- a/posthog/temporal/data_imports/sources/clickhouse/source.py
+++ b/posthog/temporal/data_imports/sources/clickhouse/source.py
@@ -167,6 +167,14 @@ class ClickHouseSource(SimpleSource[ClickHouseSourceConfig], SSHTunnelMixin, Val
# answers queries with 404, so retrying can't recover. We match only
# 404, not transient gateway codes (502/503/504), which stay retryable.
"returned response code 404": "We reached your ClickHouse host but it returned a 404, so it isn't serving the ClickHouse HTTP interface on that host/port. Please check the host, port, and HTTPS setting (and any tunnel or proxy in front of it).",
+ # MEMORY_LIMIT_EXCEEDED — the source ClickHouse server (per-query
+ # `max_memory_usage` budget or a server-wide OvercommitTracker kill)
+ # ran out of memory running our extraction query. We already stream
+ # in bounded Arrow blocks, so there's nothing to change our side:
+ # the customer's database can't satisfy the query as configured.
+ # Retrying just re-loads an already memory-pressured server, so stop
+ # and tell them to act.
+ "Code: 241": "Your ClickHouse server ran out of memory while we were reading a table. This usually means the database is too small for the table being synced. Try scaling up your ClickHouse service (or raising its memory limit), or sync a smaller table or use an incremental sync, then resume.",
# Raised from the shared `evolve_pyarrow_schema` in `pipelines/pipeline/utils.py`
# when an integer column's source type was widened (e.g. `Int32` → `Int64`) after
# the destination table was created with the narrower type. Delta Lake can't widen
diff --git a/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py b/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py
index cb69ce8e2443..75c310794987 100644
--- a/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py
+++ b/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py
@@ -523,6 +523,14 @@ class TestClickHouseSourceNonRetryableErrors:
"Connection refused",
"certificate verify failed",
"HTTPDriver for https://example.ngrok-free.dev:443 returned response code 404",
+ # MEMORY_LIMIT_EXCEEDED (code 241) — server-wide OvercommitTracker kill
+ "HTTPDriver for https://host:8443 received ClickHouse error code 241\n Code: 241. "
+ "DB::Exception: (total) memory limit exceeded: would use 108.01 GiB, maximum: 108.00 GiB. "
+ "OvercommitTracker decision: Query was selected to stop by OvercommitTracker "
+ "(while reading column properties). (MEMORY_LIMIT_EXCEEDED)",
+ # MEMORY_LIMIT_EXCEEDED (code 241) — per-query `max_memory_usage` budget
+ "Code: 241. DB::Exception: Query memory limit exceeded: would use 3.73 GiB "
+ "(attempt to allocate chunk of 4.60 MiB), maximum: 3.73 GiB. (MEMORY_LIMIT_EXCEEDED)",
],
)
def test_permanent_errors_are_non_retryable(self, source, error_msg):