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 resourcesExceeded query failures as non-retryable (#67914)

Tom Owersc432bdad5e28
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
index 1ae1fe17bbdb..4432706d70aa 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
@@ -534,6 +534,12 @@ def _get_primary_keys_for_table(table: bigquery.Table, client: bigquery.Client)
return primary_keys
+# Stable wording BigQuery puts in a `resourcesExceeded` query failure's message. Shared between
+# `_is_bigquery_resource_exceeded` and `BigQuerySource.get_non_retryable_errors` so the two sites
+# stay in lockstep if BigQuery ever adjusts the phrasing.
+BIGQUERY_RESOURCES_EXCEEDED_ERROR = "Resources exceeded during query execution"
+
+
def _is_bigquery_resource_exceeded(error: BadRequest) -> bool:
"""True for BigQuery's `resourcesExceeded` query failures.
@@ -544,7 +550,7 @@ def _is_bigquery_resource_exceeded(error: BadRequest) -> bool:
crash.
"""
reasons = {err.get("reason") for err in (getattr(error, "errors", None) or [])}
- return "resourcesExceeded" in reasons or "Resources exceeded during query execution" in str(error)
+ return "resourcesExceeded" in reasons or BIGQUERY_RESOURCES_EXCEEDED_ERROR in str(error)
def _has_duplicate_primary_keys(table: bigquery.Table, client: bigquery.Client, primary_keys: list[str] | None) -> bool:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py
index c645044a646b..15807e52765f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py
@@ -14,6 +14,7 @@ from posthog.schema import (
from products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.bigquery import (
BIGQUERY_DATASET_NOT_FOUND_ERROR,
BIGQUERY_INVALID_IDENTIFIER_ERROR,
+ BIGQUERY_RESOURCES_EXCEEDED_ERROR,
BIGQUERY_TOKEN_RESPONSE_ERROR,
BigQueryImplementation,
build_destination_table_prefix,
@@ -194,6 +195,17 @@ class BigQuerySource(SQLSource[BigQuerySourceConfig]):
# 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.",
+ # BigQuery aborts a query job with "Resources exceeded during query execution" (reason
+ # `resourcesExceeded`) when the query can't run within a worker's memory — heavy sorts or
+ # analytic OVER() clauses over a large table or view. We copy incremental / view /
+ # row-filtered reads into a temp table before reading them (`_run_destination_query_with_job_retry`
+ # in `bigquery.py`), and that copy carries the offending shape, so it fails here. It's a
+ # deterministic property of the customer's data volume and query shape — retrying the same
+ # query always fails identically (Google's own default job-retry doesn't retry this reason),
+ # so it just spams error tracking. The user must reduce the data synced or simplify the
+ # source view. Matched on BigQuery's stable wording, not the volatile peak-usage percentage
+ # or job id.
+ BIGQUERY_RESOURCES_EXCEEDED_ERROR: "BigQuery couldn't run a query for this source because it exceeded the memory allowed for a single query. This is usually caused by heavy sorts or analytic (window) functions over a large table or view. Retrying won't help — please reduce how much data you're syncing (for example add row filters or an incremental field), or simplify the source view, then re-enable the source.",
}
def validate_credentials(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
index 01031c38f938..8e7706292843 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
@@ -1312,3 +1312,23 @@ 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)
+
+
+def test_bigquery_resources_exceeded_is_non_retryable():
+ """A `resourcesExceeded` query failure exceeds a worker's memory deterministically (heavy sorts /
+ analytic OVER() clauses over a large table or view), so retrying the identical temp-table copy in
+ `_run_destination_query_with_job_retry` always fails — it must be recognised as non-retryable
+ rather than retried on every attempt."""
+ error_msg = str(
+ BadRequest(
+ "GET https://bigquery.googleapis.com/bigquery/v2/projects/<redacted>/queries/<redacted>"
+ "?maxResults=0&location=us-central1&prettyPrint=false: Resources exceeded during query "
+ "execution: The query could not be executed in the allotted memory. Peak usage: 122% of limit."
+ )
+ )
+
+ non_retryable_errors = BigQuerySource().get_non_retryable_errors()
+ matching = [key for key in non_retryable_errors if key in error_msg]
+
+ assert matching, "resourcesExceeded query failure should be recognised as non-retryable"
+ assert all(non_retryable_errors[key] is not None for key in matching)