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): retry primary-key discovery through BigQuery's job-metadata race (#79020)

Tom Owersb2d63598e316
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 fad4a66058ae..06d40cabc783 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
@@ -675,10 +675,10 @@ def _get_primary_keys_for_table(table: bigquery.Table, client: bigquery.Client)
"""
job_config = QueryJobConfig()
- job = client.query(query, job_config=job_config, project=table.project, retry=BIGQUERY_QUERY_CREATE_RETRY)
+ rows = _query_result_with_job_retry(client, query, job_config=job_config, project=table.project)
primary_keys = []
- for row in job.result(job_retry=BIGQUERY_QUERY_JOB_RETRY):
+ for row in rows:
field_name = row["column_name"].removeprefix(f"{table.table_id}.")
if field_name not in existing_fields:
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 75f98b0db391..57a39498f5f2 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
@@ -1579,6 +1579,28 @@ def test_bigquery_get_primary_keys_for_table_passes_job_retry():
assert client.query.call_args.kwargs["retry"] is BIGQUERY_QUERY_CREATE_RETRY
[email protected]("products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.bigquery.time.sleep")
+def test_bigquery_get_primary_keys_for_table_retries_transient_job_not_found(mock_sleep):
+ """The primary-key probe hits BigQuery's job-metadata race the same as the other queries in this
+ file; it must retry with a fresh job instead of crashing the whole sync."""
+ table = mock.MagicMock()
+ table.schema = [SimpleNamespace(name="id")]
+ table.dataset_id = "dataset"
+ table.table_id = "table"
+ table.project = "project"
+
+ client = mock.MagicMock()
+ ok_job = mock.MagicMock()
+ ok_job.result.return_value = iter([{"column_name": "id"}])
+ client.query.side_effect = [NotFound("404 Not found: Job prj:US.abc"), ok_job]
+
+ primary_keys = _get_primary_keys_for_table(table, client)
+
+ assert primary_keys == ["id"]
+ assert client.query.call_count == 2
+ mock_sleep.assert_called_once()
+
+
def test_run_destination_query_passes_job_retry():
"""The copy-into-temp-table query — where the production sync crashed on a transient per-second
rate quota — must run under the extended job retry so it waits the rate limit out in place