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(pganalyze): retry read timeouts and connection errors (#107896)

Tom Owersef3cffeed0fa
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/pganalyze/pganalyze.py b/products/warehouse_sources/backend/temporal/data_imports/sources/pganalyze/pganalyze.py
index 040d10c6914b..02c3a2289fe5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/pganalyze/pganalyze.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/pganalyze/pganalyze.py
@@ -2,6 +2,7 @@ from collections.abc import Iterator
from datetime import UTC, datetime
from typing import Any
+import requests
from structlog.types import FilteringBoundLogger
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
@@ -55,7 +56,7 @@ def _post_graphql(
variables: dict[str, Any],
) -> dict[str, Any]:
@retry(
- retry=retry_if_exception_type(PgAnalyzeRetryableError),
+ retry=retry_if_exception_type((PgAnalyzeRetryableError, requests.ReadTimeout, requests.ConnectionError)),
stop=stop_after_attempt(PGANALYZE_MAX_RETRY_ATTEMPTS),
wait=wait_exponential_jitter(initial=2, max=30),
reraise=True,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/pganalyze/tests/test_pganalyze.py b/products/warehouse_sources/backend/temporal/data_imports/sources/pganalyze/tests/test_pganalyze.py
index 703b6ae34305..f19652b96f5c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/pganalyze/tests/test_pganalyze.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/pganalyze/tests/test_pganalyze.py
@@ -3,6 +3,8 @@ import ipaddress
import pytest
from unittest import mock
+import requests
+
from products.warehouse_sources.backend.temporal.data_imports.sources.pganalyze.pganalyze import (
PgAnalyzeRetryableError,
_post_graphql,
@@ -64,6 +66,30 @@ class TestPostGraphql:
with pytest.raises(Exception, match="Unexpected pganalyze response format"):
_post_graphql(session, "https://app.pganalyze.com/graphql", "query {}", {})
+ def test_retries_on_read_timeout(self) -> None:
+ session = mock.MagicMock()
+ session.post.side_effect = [
+ requests.ReadTimeout("Read timed out. (read timeout=60)"),
+ _mock_response(json_data={"data": {"getServers": []}}),
+ ]
+
+ result = _post_graphql(session, "https://app.pganalyze.com/graphql", "query {}", {})
+
+ assert result == {"getServers": []}
+ assert session.post.call_count == 2
+
+ def test_retries_on_connection_error(self) -> None:
+ session = mock.MagicMock()
+ session.post.side_effect = [
+ requests.ConnectionError("Connection aborted"),
+ _mock_response(json_data={"data": {"getServers": []}}),
+ ]
+
+ result = _post_graphql(session, "https://app.pganalyze.com/graphql", "query {}", {})
+
+ assert result == {"getServers": []}
+ assert session.post.call_count == 2
+
class TestValidateCredentials:
def test_returns_true_on_valid_credentials(self):