: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(postgres): stop wrapping app-DB blips in an unrecognized exception type (#78564)
Tom Owersf49aea9bc8d5
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/exceptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/exceptions.pyindex 812f27c8b00a..86a96d357b55 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/exceptions.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/exceptions.py@@ -18,18 +18,6 @@ class XminUnsupportedError(Exception):"""-class PostHogDatabaseConnectionError(Exception):- """Raised when loading sync metadata from PostHog's own database fails to connect.-- This is a PostHog-side infrastructure blip (e.g. a transient DNS failure resolving our- database host), not a problem with the customer's Postgres. Its message intentionally- avoids the connection-error substrings in `PostgresSource.get_non_retryable_errors`: a- failure reaching our database stringifies the same as a customer misconfiguration (e.g.- "Name or service not known"), and must stay retryable rather than be misclassified as- non-retryable and stop a healthy sync.- """--class ForeignServerUnreachableError(Exception):"""Raised when a setup query touched a postgres_fdw foreign table whose foreign server failed.@@ -38,7 +26,6 @@ class ForeignServerUnreachableError(Exception):e.g. "... failed: Connection refused". That English wording collides with the connect-timesubstrings in `PostgresSource.get_non_retryable_errors` (which target the *direct* connection tothe customer's database), so a foreign server that's briefly down (failover, restart) would be- misclassified as a permanent misconfiguration and stop a healthy sync. The server-side sibling of- `PostHogDatabaseConnectionError`: its message intentionally avoids those substrings so a transient- foreign-server outage stays retryable.+ misclassified as a permanent misconfiguration and stop a healthy sync. Its message intentionally+ avoids those substrings so a transient foreign-server outage stays retryable."""diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.pyindex 23c59b50b133..1c580f09bcce 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py@@ -1,8 +1,6 @@import loggingfrom typing import TYPE_CHECKING, Optional, cast-from django.db import OperationalError as DjangoOperationalError-import structlogfrom psycopg import OperationalErrorfrom psycopg.errors import SqlclientUnableToEstablishSqlconnection@@ -1173,20 +1171,16 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDafrom products.warehouse_sources.backend.temporal.data_imports.sources.postgres.exceptions import (CDCHandledExternally,ForeignServerUnreachableError,- PostHogDatabaseConnectionError,)ssh_tunnel = self.make_ssh_tunnel_func(config, inputs.team_id)- # This reads sync metadata from PostHog's own database, not the customer's Postgres. A- # transient failure reaching our database here (e.g. a DNS blip resolving our host) raises- # the same "Name or service not known" wording a customer host misconfig would, which- # `get_non_retryable_errors` would misclassify as non-retryable and permanently stop a- # healthy sync. Re-raise as a retryable error whose message doesn't collide with those.- try:- schema = ExternalDataSchema.objects.select_related("source").get(id=inputs.schema_id)- except DjangoOperationalError as e:- raise PostHogDatabaseConnectionError("Failed to load sync metadata from PostHog's database") from e+ # This reads sync metadata from PostHog's own database, not the customer's Postgres — a+ # transient failure reaching it (e.g. a DNS blip resolving our host) is a plain Django+ # `OperationalError`, which `_handle_import_error` already classifies as a self-recovering+ # app-DB blip and keeps out of error tracking. Let it propagate as-is: wrapping it would only+ # hide that type from the classifier and turn a benign retry into reported error-tracking noise.+ schema = ExternalDataSchema.objects.select_related("source").get(id=inputs.schema_id)schema_metadata = schema.schema_metadata or {}source_schema = (schema_metadata.get("source_schema") if isinstance(schema_metadata.get("source_schema"), str) else Nonediff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.pyindex 3cb3c63b1c09..a55cf81da9cd 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py@@ -36,7 +36,6 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.sql)from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.exceptions import (ForeignServerUnreachableError,- PostHogDatabaseConnectionError,XminUnsupportedError,)from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.partitioned_tables import (@@ -275,14 +274,17 @@ class TestPostgresImplementationWiring:class TestPostgresSourceMetadataConnectionErrors:- def test_posthog_database_connection_failure_stays_retryable(self):+ def test_posthog_database_connection_failure_propagates_unwrapped(self):# `source_for_pipeline` first reads sync metadata from PostHog's own database. A transient- # connection failure there (e.g. a DNS blip resolving our host) surfaces the same- # "Name or service not known" wording a customer host misconfig would, so it must be- # re-raised as PostHogDatabaseConnectionError to avoid being misclassified as non-retryable.+ # connection failure there (e.g. a DNS blip resolving our host) must propagate as the same+ # Django `OperationalError` it was raised as. `_handle_import_error` already classifies that+ # type, regardless of message, as a self-recovering app-DB blip and keeps it out of error+ # tracking — wrapping it in a source-specific exception type would hide it from that check+ # and report it as a new bug on every occurrence instead.source = PostgresSource()config = MagicMock()inputs = MagicMock()+ original_error = DjangoOperationalError("[Errno -2] Name or service not known")with (patch.object(PostgresSource, "make_ssh_tunnel_func", return_value=None),@@ -290,16 +292,11 @@ class TestPostgresSourceMetadataConnectionErrors:"products.warehouse_sources.backend.models.external_data_schema.ExternalDataSchema") as mock_schema_model,):- mock_schema_model.objects.select_related.return_value.get.side_effect = DjangoOperationalError(- "[Errno -2] Name or service not known"- )- with pytest.raises(PostHogDatabaseConnectionError) as exc_info:+ mock_schema_model.objects.select_related.return_value.get.side_effect = original_error+ with pytest.raises(DjangoOperationalError) as exc_info:source.source_for_pipeline(config, inputs)- non_retryable = source.get_non_retryable_errors()- error_msg = str(exc_info.value)- is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())- assert not is_non_retryable, f"A PostHog-side DB connection failure must stay retryable: {error_msg}"+ assert exc_info.value is original_errorclass TestPostgresSourceForeignServerConnectionError: