: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(clickhouse): catch connection errors in direct SQL queries (#83490)
Tom Owersb69b3a634f0d
diff --git a/posthog/hogql/direct_sql/clickhouse_adapter.py b/posthog/hogql/direct_sql/clickhouse_adapter.pyindex cb06a03a3fd1..4d56381f3b4a 100644--- a/posthog/hogql/direct_sql/clickhouse_adapter.py+++ b/posthog/hogql/direct_sql/clickhouse_adapter.py@@ -279,6 +279,8 @@ class ClickHouseAdapter:def execute(self, request: DirectQueryRequest) -> DirectQueryResult:from clickhouse_connect.driver.exceptions import ClickHouseError+ from products.warehouse_sources.backend.facade.source_management import ClickHouseConnectionError+source = request.sourceclickhouse_source, config = self.validate_source_config(source, request.team)settings = request.settings@@ -311,7 +313,13 @@ class ClickHouseAdapter:fetch_result = _fetch_capped_clickhouse_rows(client, request.sql, request.values or None, statement_timeout_seconds)- except (ClickHouseError, OSError, BaseSSHTunnelForwarderError, ExposedHogQLError) as error:+ except (+ ClickHouseError,+ ClickHouseConnectionError,+ OSError,+ BaseSSHTunnelForwarderError,+ ExposedHogQLError,+ ) as error:span.set_attribute("error_type", error.__class__.__name__)if request.debug:return DirectQueryResult(diff --git a/posthog/hogql/direct_sql/test/test_clickhouse_adapter.py b/posthog/hogql/direct_sql/test/test_clickhouse_adapter.pyindex 7ea7a4b904b7..254e3c316096 100644--- a/posthog/hogql/direct_sql/test/test_clickhouse_adapter.py+++ b/posthog/hogql/direct_sql/test/test_clickhouse_adapter.py@@ -24,6 +24,8 @@ from posthog.hogql.parser import parse_selectfrom posthog.hogql.printer.utils import prepare_and_print_astfrom posthog.hogql.timings import HogQLTimings+from products.warehouse_sources.backend.facade.source_management import ClickHouseConnectionError+class TestDirectClickHouseTable(SimpleTestCase):def _table(self, database: str) -> DirectClickHouseTable:@@ -119,6 +121,34 @@ class TestClickHouseAdapterExecute(BaseTest):self.assertEqual(str(error.exception), "Could not establish session to SSH gateway")+ def test_execute_raises_exposed_error_when_client_connection_fails(self):+ # `_get_client` wraps connect-time failures (e.g. a read timeout waking a cold ClickHouse+ # Cloud service) in `ClickHouseConnectionError`, a plain Exception rather than a+ # `ClickHouseError`. Without it in this except clause, the error escapes as unhandled+ # instead of a clean `ExposedHogQLError` and gets captured as error-tracking noise.+ source = MagicMock()+ source.id = "src"+ clickhouse_source = MagicMock()+ clickhouse_source.direct_query_client.side_effect = ClickHouseConnectionError("Read timed out")++ adapter = ClickHouseAdapter()+ request = DirectQueryRequest(+ source=source,+ team=self.team,+ sql="SELECT 1",+ values=None,+ settings=HogQLGlobalSettings(),+ timings=HogQLTimings(),+ query_type="HogQLQuery",+ debug=False,+ )++ with patch.object(adapter, "validate_source_config", return_value=(clickhouse_source, MagicMock())):+ with self.assertRaises(ExposedHogQLError) as error:+ adapter.execute(request)++ self.assertEqual(str(error.exception), "Read timed out")+class TestClickHouseReadOnlyGuard(SimpleTestCase):@parameterized.expand(diff --git a/products/warehouse_sources/backend/facade/source_management.py b/products/warehouse_sources/backend/facade/source_management.pyindex 93b97a0de3e0..b116c407203b 100644--- a/products/warehouse_sources/backend/facade/source_management.py+++ b/products/warehouse_sources/backend/facade/source_management.py@@ -23,6 +23,7 @@ _LAZY = {"CDCRepairInProgress": "cdc.repair","repair_cdc_source": "cdc.repair","purge_buffer_prefix": "cdc.buffer",+ "ClickHouseConnectionError": "sources.clickhouse.clickhouse","ClickHouseSource": "sources.clickhouse.source","AnySource": "sources.common.base","ExternalWebhookInfo": "sources.common.base",