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): skip materialized view inner tables in source discovery (#63760)

Tom Owers1d67ac1a2651
diff --git a/posthog/temporal/data_imports/sources/clickhouse/clickhouse.py b/posthog/temporal/data_imports/sources/clickhouse/clickhouse.py
index 2778ac450132..d04893f8d165 100644
--- a/posthog/temporal/data_imports/sources/clickhouse/clickhouse.py
+++ b/posthog/temporal/data_imports/sources/clickhouse/clickhouse.py
@@ -228,12 +228,28 @@ def get_schemas(
schema_list: dict[str, list[tuple[str, str, bool]]] = collections.defaultdict(list)
for row in result.result_rows:
table_name, column_name, raw_type = row[0], row[1], row[2]
+ if _is_inner_table(table_name):
+ continue
_, nullable = _strip_type_modifiers(raw_type)
schema_list[table_name].append((column_name, raw_type, nullable))
return schema_list
+def _is_inner_table(table_name: str) -> bool:
+ """Whether a table is a materialized view's hidden inner table.
+
+ ClickHouse backs a materialized view created without an explicit `TO`
+ target with an auto-generated inner table — `.inner.<mv_name>` on older
+ Ordinary databases, `.inner_id.<uuid>` on Atomic databases. These are
+ implementation details, not user data: their `.inner_id.<uuid>` names
+ change whenever the view is recreated, so a sync pointed at one breaks
+ the moment the view is rebuilt. Discover the materialized view by its own
+ name instead, never its inner table.
+ """
+ return table_name.startswith(".inner.") or table_name.startswith(".inner_id.")
+
+
# Match `TO db.table` or `TO table` clause in MV CREATE statement.
# ClickHouse always emits the target in `system.tables.create_table_query`
# for MVs created with explicit `TO` target.
diff --git a/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py b/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py
index e276dfbe5eb1..d0bb39702bd2 100644
--- a/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py
+++ b/posthog/temporal/data_imports/sources/clickhouse/test_clickhouse.py
@@ -596,6 +596,30 @@ class TestGetSchemas:
assert events_cols["id"] == ("UInt64", False)
assert events_cols["name"] == ("Nullable(String)", True)
+ def test_excludes_materialized_view_inner_tables(self):
+ from posthog.temporal.data_imports.sources.clickhouse import clickhouse as ch_module
+
+ rows = [
+ ("events", "id", "UInt64"),
+ (".inner_id.8c612ff0-b72c-4b20-8ea5-405ed002c2f6", "id", "UInt64"),
+ (".inner_id.8c612ff0-b72c-4b20-8ea5-405ed002c2f6", "count", "UInt64"),
+ (".inner.my_legacy_mv", "value", "String"),
+ ]
+ mock_client = self._make_mock_client(rows)
+
+ with patch.object(ch_module, "_get_client", return_value=mock_client):
+ schemas = ch_module.get_schemas(
+ host="localhost",
+ port=8443,
+ database="default",
+ user="default",
+ password="",
+ secure=True,
+ verify=True,
+ )
+
+ assert set(schemas.keys()) == {"events"}
+
class TestSourceClassValidateCredentials:
"""High-level checks on validate_credentials error mapping."""