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): fail open on read-only-transaction errors during billing checks (#88316)

Tom Owers58301859dce6
diff --git a/products/warehouse_sources/backend/temporal/data_imports/row_tracking.py b/products/warehouse_sources/backend/temporal/data_imports/row_tracking.py
index c77e9811f6dd..5b8d80576e8a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/row_tracking.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/row_tracking.py
@@ -6,7 +6,7 @@ from typing import TYPE_CHECKING
from django.conf import settings
from django.db.models import F, Q, Sum
-from django.db.utils import OperationalError
+from django.db.utils import InternalError, OperationalError
import requests
from dateutil import parser
@@ -246,9 +246,10 @@ async def will_hit_billing_limit(team_id: int, source: "ExternalDataSource", log
await logger.awarning(f"BillingLimits: Redis error while checking billing limits, failing open: {e}")
return False
- except OperationalError as e:
- # Same rationale as above: a dropped Postgres connection while fetching billing
- # data is a transient infra blip, and the check already fails open.
+ except (OperationalError, InternalError) as e:
+ # Same rationale as above: a dropped Postgres connection, or a read-only
+ # transaction hitting a replica/failover blip, while fetching billing data is
+ # a transient infra issue, and the check already fails open.
await logger.awarning(f"BillingLimits: Database error while checking billing limits, failing open: {e}")
return False
diff --git a/products/warehouse_sources/backend/temporal/data_imports/tests/test_row_tracking.py b/products/warehouse_sources/backend/temporal/data_imports/tests/test_row_tracking.py
index 616400df4adc..a68beac75b98 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/tests/test_row_tracking.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/test_row_tracking.py
@@ -9,11 +9,12 @@ from freezegun import freeze_time
from posthog.test.base import BaseTest
from unittest import mock
-from django.db.utils import OperationalError
+from django.db.utils import InternalError, OperationalError
from django.test import override_settings
import requests
from asgiref.sync import sync_to_async
+from parameterized import parameterized
from redis import exceptions as redis_exceptions
from structlog.types import FilteringBoundLogger
@@ -244,11 +245,27 @@ class TestRowTracking(BaseTest):
mock_capture_exception.assert_not_called()
+ @parameterized.expand(
+ [
+ (
+ "operational_error",
+ OperationalError(
+ 'connection failed: connection to server at "127.0.0.1", port 5432 failed: '
+ "server closed the connection unexpectedly"
+ ),
+ ),
+ (
+ "internal_error",
+ InternalError("cannot execute UPDATE in a read-only transaction"),
+ ),
+ ]
+ )
@pytest.mark.asyncio
- async def test_row_tracking_fails_open_on_operational_error_without_capturing_exception(self):
- # A dropped Postgres connection while fetching billing data is a transient infra
- # blip, not a bug, and must fail open like any other billing-check error without
- # being reported to error tracking.
+ async def test_row_tracking_fails_open_on_database_error_without_capturing_exception(self, _name, exception):
+ # A dropped Postgres connection, or hitting a read-only replica/failover blip,
+ # while fetching billing data is a transient infra issue, not a bug, and must
+ # fail open like any other billing-check error without being reported to error
+ # tracking.
source = await self._create_source()
with (
@@ -257,10 +274,7 @@ class TestRowTracking(BaseTest):
"products.warehouse_sources.backend.temporal.data_imports.row_tracking.capture_exception"
) as mock_capture_exception,
):
- mock_get_billing.side_effect = OperationalError(
- 'connection failed: connection to server at "127.0.0.1", port 5432 failed: '
- "server closed the connection unexpectedly"
- )
+ mock_get_billing.side_effect = exception
assert await self._run(source, 10) is False