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(postgres): degrade quietly when best-effort schema metadata connection drops (#63774)

Tom Owers88f2d5b83b17
diff --git a/posthog/temporal/data_imports/sources/postgres/source.py b/posthog/temporal/data_imports/sources/postgres/source.py
index 53b5b158831f..3ff0ab1d0d92 100644
--- a/posthog/temporal/data_imports/sources/postgres/source.py
+++ b/posthog/temporal/data_imports/sources/postgres/source.py
@@ -404,8 +404,14 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
# Row-level security check powers the advisory warning in the table picker.
rls_active_by_table = _rls_active_from_conn(conn, config.schema, names)
except Exception as e:
- # Connection-level failure: neither lookup is usable.
- capture_exception(e)
+ # Connection-level failure for the best-effort PK/index/RLS metadata lookup. The
+ # schema listing already succeeded above (`db_schemas`), so degrade quietly — log a
+ # warning and drop the optional metadata, consistent with the foreign-key and index
+ # lookups in this function. Don't `capture_exception` here: this connection is opened
+ # separately from schema discovery and is prone to transient drops (e.g. an SSH-tunnel
+ # hiccup raising "server closed the connection unexpectedly"), which would otherwise
+ # flood error tracking despite the listing still succeeding.
+ structlog.get_logger().warning("Failed to fetch PK/index/RLS metadata for Postgres schemas", exc_info=e)
pk_columns_by_table = {}
indexed_columns_by_table = None
tables_with_pks = set()
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index 2717b4f45f51..468a08f5b859 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -869,6 +869,54 @@ class TestPostgresSourceGetSchemasDegradesGracefully:
assert schemas[0].name == "public.users"
assert schemas[0].foreign_keys == []
+ def test_metadata_connection_failure_degrades_quietly_without_capturing(self, source):
+ # The PK/index/RLS metadata connection is opened separately from schema discovery and is
+ # prone to transient drops (commonly an SSH-tunnel hiccup raising "server closed the
+ # connection unexpectedly"). Schema discovery already succeeded, so this must degrade quietly:
+ # surface the schema listing and DON'T flood error tracking with a captured exception.
+ discovered = {
+ "public.users": PostgresDiscoveredSchema(
+ source_catalog=None,
+ source_schema="public",
+ source_table_name="users",
+ columns=[("id", "integer", False)],
+ )
+ }
+
+ tunnel_cm = mock.MagicMock()
+ tunnel_cm.__enter__.return_value = ("localhost", 5432)
+ tunnel_cm.__exit__.return_value = None
+
+ connection_dropped = psycopg.OperationalError(
+ 'connection failed: connection to server at "127.0.0.1", port 37761 failed: '
+ "server closed the connection unexpectedly\n\tThis probably means the server terminated "
+ "abnormally\n\tbefore or while processing the request."
+ )
+
+ with (
+ mock.patch.object(source, "with_ssh_tunnel", return_value=tunnel_cm),
+ mock.patch(
+ "posthog.temporal.data_imports.sources.postgres.source.get_postgres_schemas",
+ return_value=discovered,
+ ),
+ mock.patch(
+ "posthog.temporal.data_imports.sources.postgres.source.get_postgres_foreign_keys",
+ return_value={},
+ ),
+ mock.patch(
+ "posthog.temporal.data_imports.sources.postgres.source.pg_connection",
+ side_effect=connection_dropped,
+ ),
+ mock.patch("posthog.temporal.data_imports.sources.postgres.source.capture_exception") as mock_capture,
+ ):
+ schemas = source.get_schemas(self._config(), team_id=1)
+
+ assert len(schemas) == 1
+ assert schemas[0].name == "public.users"
+ # Best-effort metadata is dropped, but the listing survives and nothing is captured.
+ assert schemas[0].supports_cdc is False
+ mock_capture.assert_not_called()
+
class TestGetSslmode:
@pytest.mark.parametrize(