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-imports): stop reporting transient app-db blips from the reconcile sweep (#86787)

Tom Owersa9ffc0bca2e1
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/consumer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/consumer.py
index d85e51febdf7..95efaa70aa1c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/consumer.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/consumer.py
@@ -21,6 +21,7 @@ import structlog
from asgiref.sync import sync_to_async
from posthog.exceptions_capture import capture_exception
+from posthog.temporal.common.db_errors import is_transient_db_error
from products.warehouse_sources.backend.temporal.data_imports.metrics import (
LOCK_TAKEOVER_LATEST_ERROR,
@@ -240,8 +241,16 @@ class DeltaBatchConsumerAdapter:
)
except Exception as e:
# Leave the job for the reconcile sweep rather than crashing the consumer.
- logger.exception("fail_run_job_status_update_failed", job_id=batch.job_id, run_uuid=batch.run_uuid)
- capture_exception(e)
+ if is_transient_db_error(e):
+ logger.warning(
+ "fail_run_job_status_update_app_db_not_ready",
+ job_id=batch.job_id,
+ run_uuid=batch.run_uuid,
+ error=str(e),
+ )
+ else:
+ logger.exception("fail_run_job_status_update_failed", job_id=batch.job_id, run_uuid=batch.run_uuid)
+ capture_exception(e)
workflow_run_id = batch.metadata.get("workflow_run_id")
if workflow_run_id:
@@ -383,8 +392,16 @@ class DeltaBatchConsumerAdapter:
error=ref.reason or "run failed (reconciled from queue)",
)
except Exception as e:
- logger.exception("reconcile_job_status_update_failed", job_id=ref.job_id, run_uuid=ref.run_uuid)
- capture_exception(e)
+ if is_transient_db_error(e):
+ logger.warning(
+ "reconcile_job_status_update_app_db_not_ready",
+ job_id=ref.job_id,
+ run_uuid=ref.run_uuid,
+ error=str(e),
+ )
+ else:
+ logger.exception("reconcile_job_status_update_failed", job_id=ref.job_id, run_uuid=ref.run_uuid)
+ capture_exception(e)
reconciled = False
if reconciled:
@@ -475,8 +492,16 @@ class DeltaBatchConsumerAdapter:
error=STRANDED_RUN_ERROR,
)
except Exception as e:
- logger.exception("stranded_run_job_status_update_failed", job_id=ref.job_id, run_uuid=ref.run_uuid)
- capture_exception(e)
+ if is_transient_db_error(e):
+ logger.warning(
+ "stranded_run_job_status_update_app_db_not_ready",
+ job_id=ref.job_id,
+ run_uuid=ref.run_uuid,
+ error=str(e),
+ )
+ else:
+ logger.exception("stranded_run_job_status_update_failed", job_id=ref.job_id, run_uuid=ref.run_uuid)
+ capture_exception(e)
reconciled = False
# Count only fully terminalized runs: on a failed job write the failed-run
@@ -558,8 +583,13 @@ class DeltaBatchConsumerAdapter:
job_dead = await self._is_job_dead(batch)
except Exception as e:
# Fail open: an app-DB hiccup must never wedge the loader.
- logger.exception("job_status_check_failed", batch_id=batch.id, job_id=batch.job_id)
- capture_exception(e)
+ if is_transient_db_error(e):
+ logger.warning(
+ "job_status_check_app_db_not_ready", batch_id=batch.id, job_id=batch.job_id, error=str(e)
+ )
+ else:
+ logger.exception("job_status_check_failed", batch_id=batch.id, job_id=batch.job_id)
+ capture_exception(e)
return True
if not job_dead:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py
index 42579bd5c15e..c1ba60aa4f3e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py
@@ -5,6 +5,8 @@ from typing import Any
import pytest
from unittest.mock import AsyncMock, patch
+from django.db import OperationalError as DjangoOperationalError
+
import psycopg
import structlog
@@ -21,6 +23,9 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline
_is_server_not_ready_error,
)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.load.health import HealthState
+from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue import (
+ consumer as consumer_module,
+)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer import (
BatchConsumer,
ConsumerConfig,
@@ -32,6 +37,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline
FRESHNESS_WINDOW_SECONDS,
FailedRunRef,
PendingBatch,
+ StrandedRunRef,
)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.metrics import (
CLAIMABLE_BATCHES,
@@ -1370,6 +1376,31 @@ class TestFailRun:
mock_status.assert_called_once()
+ @pytest.mark.asyncio
+ async def test_does_not_report_app_db_not_ready_error(self):
+ # Same self-healing app-DB refusal as TestReconcileFailedRuns, but hit while
+ # fail_run marks the job Failed directly rather than via the reconcile sweep.
+ consumer = _make_consumer()
+ batch = _make_batch()
+
+ with (
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.fail_run",
+ new_callable=AsyncMock,
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer._update_job_status_to_failed",
+ side_effect=DjangoOperationalError(
+ "server login has been failing, cached error: the database system is in "
+ "recovery mode (server_login_retry)"
+ ),
+ ),
+ patch(f"{consumer_module.__name__}.capture_exception") as mock_capture,
+ ):
+ await consumer._fail_run(batch, reason="boom", conn=consumer._poll_conn)
+
+ mock_capture.assert_not_called()
+
class TestUpdateJobStatusToFailed:
def test_swallows_does_not_exist_when_job_deleted_mid_sync(self):
@@ -1483,6 +1514,35 @@ class TestShouldProcessBatch:
assert result is True
mock_queue_fail.assert_not_called()
+ @pytest.mark.asyncio
+ async def test_status_check_does_not_report_app_db_not_ready_error(self):
+ # Same fail-open behavior as above, but for the self-healing app-DB refusal this
+ # was reported against: it must still fail open without paging error tracking,
+ # since the app DB accepts connections again within seconds.
+ consumer = _make_consumer()
+ conn = consumer._poll_conn
+ assert conn is not None
+ batch = _make_batch()
+
+ with (
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer._get_job_status_and_error",
+ side_effect=DjangoOperationalError(
+ "server login has been failing, cached error: the database system is in "
+ "recovery mode (server_login_retry)"
+ ),
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.fail_run",
+ new_callable=AsyncMock,
+ ),
+ patch(f"{consumer_module.__name__}.capture_exception") as mock_capture,
+ ):
+ result = await consumer._adapter.should_process_batch(conn, batch=batch)
+
+ assert result is True
+ mock_capture.assert_not_called()
+
class TestDeadJobSkip:
@pytest.mark.asyncio
@@ -1811,6 +1871,108 @@ class TestReconcileFailedRuns:
assert mock_mark.call_count == 2 # error on the first ref does not abort the sweep
+ @pytest.mark.asyncio
+ async def test_does_not_report_app_db_not_ready_error(self):
+ # Reproduces the reported issue: the app DB (read via the Django ORM, not the queue
+ # DB) briefly refuses connections during a failover while the reconcile sweep is
+ # marking a job Failed. This self-heals within seconds and the sweep already retries
+ # every interval, so it must not be sent to error tracking.
+ consumer = _make_consumer()
+ ref = _make_failed_run_ref()
+
+ with (
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.get_oldest_unclaimed_batch_age_seconds",
+ new_callable=AsyncMock,
+ return_value=0.0,
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.get_claimable_batch_count",
+ new_callable=AsyncMock,
+ return_value=0,
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.get_failed_runs",
+ new_callable=AsyncMock,
+ return_value=[ref],
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.get_stale_stranded_runs",
+ new_callable=AsyncMock,
+ return_value=[],
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.mark_job_failed_if_not_terminal",
+ side_effect=DjangoOperationalError(
+ "server login has been failing, cached error: the database system is in "
+ "recovery mode (server_login_retry)"
+ ),
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.release_v3_pipeline_lock",
+ ),
+ patch(f"{consumer_module.__name__}.capture_exception") as mock_capture,
+ ):
+ await consumer._reconcile_failed_runs()
+
+ mock_capture.assert_not_called()
+
+ @pytest.mark.asyncio
+ async def test_stranded_run_sweep_does_not_report_app_db_not_ready_error(self):
+ # Same self-healing app-DB refusal, but hit by the stranded-run sweep the reconcile
+ # sweep runs after the main failed-runs pass, on the same mark_job_failed_if_not_terminal seam.
+ consumer = _make_consumer()
+ ref = StrandedRunRef(
+ run_uuid="run-1",
+ job_id="job-1",
+ team_id=1,
+ schema_id="schema-1",
+ workflow_run_id="wf-1",
+ non_terminal_batches=2,
+ )
+
+ with (
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.get_oldest_unclaimed_batch_age_seconds",
+ new_callable=AsyncMock,
+ return_value=0.0,
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.get_claimable_batch_count",
+ new_callable=AsyncMock,
+ return_value=0,
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.get_failed_runs",
+ new_callable=AsyncMock,
+ return_value=[],
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.get_stale_stranded_runs",
+ new_callable=AsyncMock,
+ return_value=[ref],
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.fail_run",
+ new_callable=AsyncMock,
+ return_value=0,
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.mark_job_failed_if_not_terminal",
+ side_effect=DjangoOperationalError(
+ "server login has been failing, cached error: the database system is in "
+ "recovery mode (server_login_retry)"
+ ),
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.release_v3_pipeline_lock",
+ ),
+ patch(f"{consumer_module.__name__}.capture_exception") as mock_capture,
+ ):
+ await consumer._reconcile_failed_runs()
+
+ mock_capture.assert_not_called()
+
class TestConnectionRecovery:
@pytest.mark.parametrize(