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(data-warehouse): stop table registration failing when row count refresh times out (#78444)

Tom Owers8cfd09613582
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.py
index e6b49e5575b3..b07b6f2b9a76 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.py
@@ -16,6 +16,7 @@ import dlt.common.libs.pyarrow
import dlt.extract.incremental
import dlt.extract.incremental.transform
from clickhouse_driver.errors import ServerException
+from structlog.types import FilteringBoundLogger
from posthog.exceptions_capture import capture_exception
from posthog.sync import database_sync_to_async_pool
@@ -133,6 +134,17 @@ async def set_initial_sync_complete(schema_id: str, team_id: int) -> None:
await database_sync_to_async_pool(mark_initial_sync_complete)(schema_id=schema_id, team_id=team_id)
+def _refresh_cumulative_row_count(table: DataWarehouseTable, logger: FilteringBoundLogger, context: str) -> None:
+ # Counting the full S3 dataset can exceed both the chdb and ClickHouse-cluster timeouts on a
+ # large table (get_count() then raises). That's only a display stat, not the synced data itself
+ # (already written successfully by this point) — keep the previous row_count rather than let it
+ # fail the whole table registration.
+ try:
+ table.row_count = table.get_count()
+ except Exception:
+ logger.warning(f"Could not refresh cumulative row count for {context}, keeping previous value", exc_info=True)
+
+
async def validate_schema_and_update_table(
run_id: str,
team_id: int,
@@ -209,7 +221,7 @@ async def validate_schema_and_update_table(
table.url_pattern = new_url_pattern
table.queryable_folder = queryable_folder
if external_data_schema.table_row_count_is_cumulative:
- table.row_count = table.get_count()
+ _refresh_cumulative_row_count(table, logger, f"{_schema_name} ({_schema_id})")
else:
table.row_count = row_count
# get_count() above can retry against a degraded ClickHouse cluster for minutes, long
@@ -366,7 +378,7 @@ async def register_cdc_companion_table(
table.format = table_format
table.url_pattern = new_url_pattern
table.queryable_folder = queryable_folder
- table.row_count = table.get_count()
+ _refresh_cumulative_row_count(table, logger, companion_table_name)
# Scope to the fields changed here so this out-of-transaction save doesn't rewrite
# `columns` with its pre-merge value before the column save below.
# get_count() above can retry against a degraded ClickHouse cluster for minutes, long
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/test_pipeline_sync.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/test_pipeline_sync.py
index 6abbf1ce2746..8af0a0d88a8d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/test_pipeline_sync.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/test_pipeline_sync.py
@@ -18,6 +18,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.helpers
resolve_table_and_folder_names,
)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_sync import (
+ _refresh_cumulative_row_count,
merge_columns,
update_last_synced_at,
)
@@ -87,6 +88,31 @@ class TestResolveTableAndFolderNames:
assert build_table_name(source, names.table_storage_name) == expected
+class TestRefreshCumulativeRowCount:
+ def test_updates_row_count_on_success(self) -> None:
+ table = MagicMock(row_count=1)
+ table.get_count.return_value = 42
+
+ _refresh_cumulative_row_count(table, MagicMock(), "orders (schema-1)")
+
+ assert table.row_count == 42
+
+ def test_keeps_previous_row_count_when_get_count_fails(self) -> None:
+ # get_count() raises when both the chdb and ClickHouse-cluster reads of the S3 dataset
+ # time out on a large table. That's a display stat, not the synced data — it must not
+ # propagate and fail the whole table registration for an otherwise-successful sync.
+ table = MagicMock(row_count=7)
+ table.get_count.side_effect = Exception(
+ "Reading the files from your storage bucket took too long and the query was cancelled."
+ )
+ logger = MagicMock()
+
+ _refresh_cumulative_row_count(table, logger, "orders (schema-1)")
+
+ assert table.row_count == 7
+ logger.warning.assert_called_once()
+
+
def _register_companion_sync(
run_id: str,
team_id: int,