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(bigquery): treat CDC max_staleness read failure as non-retryable (#64600)

Tom Owers05ebb63aeec5
diff --git a/posthog/temporal/data_imports/sources/bigquery/source.py b/posthog/temporal/data_imports/sources/bigquery/source.py
index 308c76eb6aac..4a3615140f38 100644
--- a/posthog/temporal/data_imports/sources/bigquery/source.py
+++ b/posthog/temporal/data_imports/sources/bigquery/source.py
@@ -116,6 +116,17 @@ class BigQuerySource(SQLSource[BigQuerySourceConfig]):
# from transient rate-limit quota errors ("Quota exceeded: ..." / reason
# `rateLimitExceeded`) that must stay retryable.
"Custom quota exceeded": "Your BigQuery project hit a custom usage quota set by your administrator (for example QueryUsagePerDay). Raise the custom cost-control quota in Google Cloud, or reduce how much data you're syncing, then re-enable the source.",
+ # Raised from the Storage Read API's `create_read_session` (see `get_rows` in
+ # `bigquery.py`) when the source table uses change data capture with a `max_staleness`
+ # window and has pending upserts older than that window. The Storage Read API can't
+ # apply CDC changes on the fly — only GoogleSQL queries and BigQuery's background apply
+ # jobs do — so it rejects the read as a google.api_core InvalidArgument whose str() is
+ # "400 request failed: The table has un-applied upsert data that is not fresh enough to
+ # meet table's max_staleness.". Retrying within the sync's window can't recover it: the
+ # pending changes are applied on BigQuery's own schedule, not ours, so it just hammers
+ # the Read API and spams error tracking until a later sync finds the table caught up.
+ # Matched on the stable freshness phrasing rather than the volatile table id.
+ "un-applied upsert data that is not fresh enough": "BigQuery couldn't read this table through the Storage Read API because it uses change data capture and has pending upserts that haven't been applied within the table's max_staleness window. This usually clears once BigQuery applies the pending changes, so a later sync should recover. If it persists, lower the table's max_staleness or run a query against the table in BigQuery to apply the changes.",
}
def validate_credentials(
diff --git a/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py b/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py
index f6bcc5ac736d..1ad04d499729 100644
--- a/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py
+++ b/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py
@@ -5,7 +5,7 @@ from freezegun import freeze_time
from unittest import mock
from dateutil import parser
-from google.api_core.exceptions import BadRequest, Forbidden, NotFound, PermissionDenied
+from google.api_core.exceptions import BadRequest, Forbidden, InvalidArgument, NotFound, PermissionDenied
from posthog.temporal.data_imports.pipelines.pipeline.typings import SourceInputs
from posthog.temporal.data_imports.sources.bigquery import bigquery as bq_module
@@ -822,3 +822,37 @@ def test_bigquery_billing_not_enabled_is_non_retryable():
assert billing_key in non_retryable_errors, "expected billing key to be non-retryable"
# Mirror the substring match used by `update_external_data_job_model`.
assert billing_key in internal_error
+
+
+def test_bigquery_cdc_staleness_is_non_retryable():
+ """A CDC table whose pending upserts are staler than its max_staleness can't be read via the
+ Storage Read API (it never applies CDC changes), so the read fails as an InvalidArgument.
+ Retrying within the sync's window can't recover it — the apply happens on BigQuery's schedule —
+ so it must be recognised as non-retryable instead of hammering the Read API every attempt."""
+ error_msg = str(
+ InvalidArgument(
+ "request failed: The table has un-applied upsert data that is not fresh enough to meet "
+ "table's max_staleness."
+ )
+ )
+
+ non_retryable_errors = BigQuerySource().get_non_retryable_errors()
+ matching = [key for key in non_retryable_errors if key in error_msg]
+
+ assert matching, "CDC max_staleness read failure should be recognised as non-retryable"
+ assert all(non_retryable_errors[key] is not None for key in matching)
+
+
+ "other_error",
+ [
+ # A genuine config error about max_staleness must not be swallowed by the freshness key.
+ "400 Invalid value for max_staleness: must be a valid INTERVAL",
+ # Transient server errors must stay retryable.
+ "503 Service unavailable, please retry",
+ ],
+)
+def test_bigquery_cdc_staleness_key_does_not_match_unrelated_errors(other_error):
+ non_retryable_errors = BigQuerySource().get_non_retryable_errors()
+ assert "un-applied upsert data that is not fresh enough" not in other_error
+ assert not any(key in other_error for key in non_retryable_errors)