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(cohorts): retry cohort recalc on transient readonly ClickHouse error (#67627)

posthog[bot]087a02256ac4
diff --git a/posthog/errors.py b/posthog/errors.py
index 769a69d51a63..804ee05bca4c 100644
--- a/posthog/errors.py
+++ b/posthog/errors.py
@@ -135,6 +135,9 @@ def wrap_clickhouse_query_error(err: Exception) -> Exception:
return CHQueryErrorS3Error(f"S3 error occurred. ({err.message})", code=err.code)
elif name == "INCORRECT_DATA" and "Not a Parquet file" in err.message and "(in file/uri" in err.message:
return _wrap_storage_file_changed_error(err)
+ elif name == "TABLE_IS_READ_ONLY":
+ # Transient: a replica dropped its ZooKeeper/Keeper session and went read-only; it self-heals.
+ return CHQueryErrorTableIsReadOnly(err.message, code=err.code, code_name="table_is_read_only")
# user query errors - pass through original message with proper code_name
elif name == "ILLEGAL_TYPE_OF_ARGUMENT":
@@ -228,6 +231,10 @@ class CHQueryErrorS3FileChangedDuringRead(ExposedCHQueryError):
pass
+class CHQueryErrorTableIsReadOnly(InternalCHQueryError):
+ pass
+
+
# User query errors - these are errors caused by user input/queries
class CHQueryErrorIllegalTypeOfArgument(ExposedCHQueryError):
pass
@@ -1005,4 +1012,5 @@ CH_TRANSIENT_ERRORS = (
CHQueryErrorCannotScheduleTask,
CHQueryErrorS3Error,
CHQueryErrorS3FileChangedDuringRead,
+ CHQueryErrorTableIsReadOnly,
)
diff --git a/posthog/hogql/test/test_log_query_settings.py b/posthog/hogql/test/test_log_query_settings.py
index deea635f6af1..3dead9e29a44 100644
--- a/posthog/hogql/test/test_log_query_settings.py
+++ b/posthog/hogql/test/test_log_query_settings.py
@@ -10,6 +10,8 @@ from posthog.hogql.errors import QueryError
from posthog.hogql.query import HogQLQueryExecutor
from posthog.errors import (
+ CH_TRANSIENT_ERRORS,
+ CHQueryErrorTableIsReadOnly,
CHQueryErrorTooManyBytes,
ExposedCHQueryError,
QueryErrorCategory,
@@ -126,6 +128,15 @@ class TestTooManyBytesError(ClickhouseTestMixin, APIBaseTest):
wrapped = wrap_clickhouse_query_error(server_error)
assert getattr(wrapped, "code_name", None) == "too_many_bytes"
+ def test_wrap_clickhouse_query_error_read_only_is_stable_and_transient(self):
+ # Code 242 (TABLE_IS_READ_ONLY) is a self-healing replica error; it must map to the
+ # importable class that lives in CH_TRANSIENT_ERRORS so tasks can retry it, rather than
+ # falling back to a dynamically generated class that no autoretry tuple references.
+ server_error = ServerException("DB::Exception: Table is in readonly mode.", code=242)
+ wrapped = wrap_clickhouse_query_error(server_error)
+ assert isinstance(wrapped, CHQueryErrorTableIsReadOnly)
+ assert isinstance(wrapped, CH_TRANSIENT_ERRORS)
+
class TestArgumentCountErrorsAreUserFacing(TestCase):
"""Wrong-function-arg-count errors are user query mistakes, not internal bugs, so they must
diff --git a/posthog/tasks/calculate_cohort.py b/posthog/tasks/calculate_cohort.py
index 0afc6d01df86..cbb81c55db43 100644
--- a/posthog/tasks/calculate_cohort.py
+++ b/posthog/tasks/calculate_cohort.py
@@ -19,6 +19,7 @@ from posthog.errors import (
CHQueryErrorCannotScheduleTask,
CHQueryErrorS3Error,
CHQueryErrorS3FileChangedDuringRead,
+ CHQueryErrorTableIsReadOnly,
CHQueryErrorTooManySimultaneousQueries,
)
from posthog.exceptions import ClickHouseAtCapacity
@@ -437,6 +438,7 @@ def _enqueue_single_cohort_calculation(cohort: Cohort, initiating_user: Optional
ClickHouseAtCapacity,
CHQueryErrorS3Error,
CHQueryErrorS3FileChangedDuringRead,
+ CHQueryErrorTableIsReadOnly,
),
retry_backoff=60,
retry_backoff_max=1800,