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): keep app-DB connection blips out of error tracking (#75733)

Tom Owers6cae1096f065
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py
index 69339c0d5b01..12b93b34bbcc 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py
@@ -4,6 +4,7 @@ import datetime as dt
import dataclasses
from typing import Any, NoReturn, Optional
+from django.db import InterfaceError, OperationalError
from django.db.models import Prefetch
from structlog.contextvars import bind_contextvars
@@ -344,7 +345,8 @@ async def _handle_import_error(
the activity; only that marker type does. ``RESTClientRetryableError`` gets the same treatment
by type, since it's already a ``NonReportableError`` subclass and every REST-based source hits
that condition already. A transient object-store hiccup talking to our own data-warehouse
- bucket is re-raised as ``NonReportableError`` the same way.
+ bucket is re-raised as ``NonReportableError`` the same way, as is a Django
+ ``OperationalError``/``InterfaceError`` (a connection-pool blip against our own app DB).
Everything else is logged as an exception and re-raised so Temporal retries it as usual.
"""
@@ -399,6 +401,18 @@ async def _handle_import_error(
await logger.adebug("Transient object-store error - re-raising for Temporal retry")
raise NonReportableError(error_msg) from error
+ # A Django OperationalError/InterfaceError here comes from a lookup against PostHog's own app
+ # DB (e.g. resolving a team or CustomPropertySource for the person-property staging hook) —
+ # every source that talks to a customer's own database (Postgres, MySQL, Redshift) does so over
+ # a raw driver connection, never Django's ORM, so this exception type can only mean a transient
+ # connection-pool blip on our side (e.g. a PgBouncer query_wait_timeout under load), not a
+ # customer data or config problem. Same classification already used for app-DB blips in
+ # delta_table_helper.is_transient_maintenance_error.
+ if isinstance(error, OperationalError | InterfaceError):
+ await logger.awarning(error_msg)
+ await logger.adebug("Transient app-DB error - re-raising for Temporal retry")
+ raise NonReportableError(error_msg) from error
+
# Cross-source non-retryable errors (missing primary key on an incremental table, bad SSH tunnel
# auth, a widened column type) are raised from shared pipeline code, not any one source. The
# finalization activity already consults this shared dict; this in-activity handler decides whether
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py
index 58cf0b0fb5c5..d5cdc0e7765a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py
@@ -7,6 +7,10 @@ import pytest
from posthog.test.base import BaseTest
from unittest import mock
+from django.db import InterfaceError, OperationalError
+
+from parameterized import parameterized
+
from posthog.temporal.common.errors import NonReportableError
from products.warehouse_sources.backend.models.external_data_job import ExternalDataJob
@@ -250,6 +254,39 @@ async def test_rest_client_retryable_error_logged_as_warning_without_source_opt_
logger.aexception.assert_not_awaited()
+ [
+ ("operational_error", OperationalError, "query_wait_timeout"),
+ ("interface_error", InterfaceError, "connection already closed"),
+ ]
+)
+async def test_app_db_connection_error_reraised_as_non_reportable(_name: str, error_cls: type[Exception], message: str):
+ # A Django OperationalError/InterfaceError here can only come from a lookup against PostHog's
+ # own app DB (e.g. resolving a team or CustomPropertySource for the person-property staging
+ # hook) — sources talk to a customer's own database over a raw driver connection, never Django's
+ # ORM. It's a transient connection-pool blip on our side (e.g. a PgBouncer query_wait_timeout
+ # under load), so it must be re-raised as NonReportableError (like RESTClientRetryableError
+ # above) rather than the bare exception, which the activity interceptor would still capture.
+ error = error_cls(message)
+ source = mock.MagicMock(spec=SimpleSource)
+ source.get_non_retryable_errors.return_value = {}
+ source.get_retryable_errors.return_value = set()
+
+ logger = mock.MagicMock()
+ logger.awarning = mock.AsyncMock()
+ logger.aexception = mock.AsyncMock()
+ logger.adebug = mock.AsyncMock()
+
+ with mock.patch.object(module.SourceRegistry, "get_source", return_value=source):
+ with pytest.raises(NonReportableError) as exc_info:
+ await module._handle_import_error(mock.MagicMock(), logger, error)
+
+ assert exc_info.value.__cause__ is error
+ logger.awarning.assert_awaited_once()
+ logger.aexception.assert_not_awaited()
+
+
@pytest.mark.asyncio
async def test_transient_object_store_error_reraised_as_non_reportable():
# A transient S3 credential-provider blip (IMDS/STS) talking to our own data-warehouse bucket,