: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(data-warehouse): stop warning that a quiet table has stopped syncing (#104604)
Tom Owersd47fec9211f1
diff --git a/products/data_warehouse/backend/sync_status.py b/products/data_warehouse/backend/sync_status.pyindex 699976c43358..aa1d39732b58 100644--- a/products/data_warehouse/backend/sync_status.py+++ b/products/data_warehouse/backend/sync_status.py@@ -32,12 +32,42 @@ _BILLING_LIMIT_REASONS: dict[str, str] = {}+def _last_run_at(schema: ExternalDataSchema) -> datetime | None:+ """When a sync last ran to completion, whether or not it moved any rows.++ `last_synced_at` alone answers a narrower question. It is the signals watermark+ (`partition_field > last_synced_at`) as well as a display value, so the v3 pipeline+ deliberately leaves it where it is when a run extracts nothing — advancing it would+ narrow the next run's signal window past rows that arrive late. Such a run stamps only+ `last_full_run_at`, so a schema whose source is simply quiet reads here as having run.++ Both are consulted because neither covers every path on its own: a fast return writes+ `last_synced_at` and not `last_full_run_at`, and a v3 zero-batch run does the reverse.+ """+ stamped: datetime | None = None+ raw = schema.last_full_run_at+ if raw is not None:+ try:+ parsed = datetime.fromisoformat(raw)+ except (TypeError, ValueError):+ parsed = None+ # A naive stamp cannot be compared against `now`, and guessing its zone would+ # invent freshness. Mirrors `_fast_return_eligible`, which rejects one outright.+ if parsed is not None and parsed.tzinfo is not None:+ stamped = parsed++ synced = ensure_utc(schema.last_synced_at) if schema.last_synced_at is not None else None+ candidates = [value for value in (synced, stamped) if value is not None]+ return max(candidates) if candidates else None++def _is_stale(schema: ExternalDataSchema, now: datetime) -> bool:- """Stale once the last sync is older than 2x the cadence; unknown cadence or never-synced is not stale."""+ """Stale once the last sync run is older than 2x the cadence; unknown cadence or never-run is not stale."""interval = schema.sync_frequency_interval- if interval is None or schema.last_synced_at is None:+ last_run = _last_run_at(schema)+ if interval is None or last_run is None:return False- return (now - ensure_utc(schema.last_synced_at)) > interval * STALE_RUNNING_MULTIPLIER+ return (now - last_run) > interval * STALE_RUNNING_MULTIPLIERdef _active_external_data_schemas(warehouse_table: DataWarehouseTable) -> list[ExternalDataSchema]:diff --git a/products/data_warehouse/backend/test/test_sync_status.py b/products/data_warehouse/backend/test/test_sync_status.pyindex 26a92ba6b5f9..3c112d3f612f 100644--- a/products/data_warehouse/backend/test/test_sync_status.py+++ b/products/data_warehouse/backend/test/test_sync_status.py@@ -49,6 +49,7 @@ class TestWarehouseSyncWarnings(BaseTest):sync_frequency_interval: timedelta | None = timedelta(hours=6),latest_error: str | None = None,should_sync: bool = True,+ last_full_run_at: str | None = None,) -> ExternalDataSchema:return ExternalDataSchema.objects.create(name="Charge",@@ -60,6 +61,7 @@ class TestWarehouseSyncWarnings(BaseTest):sync_frequency_interval=sync_frequency_interval,latest_error=latest_error,should_sync=should_sync,+ sync_type_config={"last_full_run_at": last_full_run_at} if last_full_run_at is not None else {},)def test_no_warnings_for_self_managed_table(self) -> None:@@ -122,6 +124,71 @@ class TestWarehouseSyncWarnings(BaseTest):assert "password" not in warnings[0].messageassert "data warehouse source" in warnings[0].message.lower()+ def test_no_warning_when_recent_runs_extracted_nothing(self) -> None:+ # The v3 pipeline leaves `last_synced_at` alone on a run that extracts zero rows, because+ # it doubles as the signals watermark. Such a run stamps `last_full_run_at`, so a schema+ # whose source is simply quiet must not be reported as stale.+ self._make_schema(+ status=ExternalDataSchema.Status.COMPLETED,+ sync_frequency_interval=timedelta(hours=6),+ last_synced_at=self.now - timedelta(hours=15),+ last_full_run_at=(self.now - timedelta(hours=1)).isoformat(),+ )+ assert get_warehouse_sync_warnings(self.table, now=self.now) == []++ def test_warning_when_both_the_last_sync_and_the_last_run_are_old(self) -> None:+ # A schema that has genuinely stopped running still warns: the run stamp is no fresher+ # than the sync, so neither says the source was checked recently.+ self._make_schema(+ status=ExternalDataSchema.Status.COMPLETED,+ sync_frequency_interval=timedelta(hours=6),+ last_synced_at=self.now - timedelta(hours=15),+ last_full_run_at=(self.now - timedelta(hours=14)).isoformat(),+ )+ warnings = get_warehouse_sync_warnings(self.table, now=self.now)+ assert len(warnings) == 1+ assert "more than twice its configured sync interval" in warnings[0].message++ @parameterized.expand(+ [+ ("not_a_timestamp", "whenever"),+ # A naive stamp cannot be compared against an aware `now`, and assuming a zone would+ # invent freshness a schema may not have.+ ("naive_timestamp", "2026-01-01T11:00:00"),+ ]+ )+ def test_an_unusable_run_stamp_falls_back_to_the_last_sync(self, _name: str, stamp: str) -> None:+ self._make_schema(+ status=ExternalDataSchema.Status.COMPLETED,+ sync_frequency_interval=timedelta(hours=6),+ last_synced_at=self.now - timedelta(hours=15),+ last_full_run_at=stamp,+ )+ warnings = get_warehouse_sync_warnings(self.table, now=self.now)+ assert len(warnings) == 1++ def test_a_fast_returned_run_still_reads_as_fresh(self) -> None:+ # A fast return writes `last_synced_at` and deliberately not `last_full_run_at`, so the+ # freshest of the two has to be what staleness measures from.+ self._make_schema(+ status=ExternalDataSchema.Status.COMPLETED,+ sync_frequency_interval=timedelta(hours=6),+ last_synced_at=self.now - timedelta(hours=1),+ last_full_run_at=(self.now - timedelta(hours=20)).isoformat(),+ )+ assert get_warehouse_sync_warnings(self.table, now=self.now) == []++ def test_no_warning_before_a_schema_has_ever_synced(self) -> None:+ # A run stamp without a sync means the table has never had data land in it; the+ # never-synced messages cover that, and a staleness warning would be noise.+ self._make_schema(+ status=ExternalDataSchema.Status.COMPLETED,+ sync_frequency_interval=timedelta(hours=6),+ last_synced_at=None,+ last_full_run_at=(self.now - timedelta(hours=20)).isoformat(),+ )+ assert get_warehouse_sync_warnings(self.table, now=self.now) == []+def test_warning_when_running_but_stale(self) -> None:self._make_schema(status=ExternalDataSchema.Status.RUNNING,