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): stop flooding error tracking with handled partition-sizing failures (#63759)

Tom Owers09e191b2a062
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.py
index f6ba5253a7e7..13a62325a6fe 100644
--- a/posthog/temporal/data_imports/sources/postgres/postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py
@@ -1496,11 +1496,19 @@ def _get_partition_settings(
except psycopg.errors.QueryCanceled:
raise
except Exception as e:
- # A read replica can cancel this best-effort sizing query with a recovery conflict; it's
- # transient (the row-streaming reader retries it in-process) and expected on replicas, so
- # degrade quietly instead of flooding error tracking with a handled condition. Genuinely
- # unexpected failures are still captured.
- if not _is_recovery_conflict_error(e):
+ # Partition sizing is best-effort: on failure we return None and the sync proceeds
+ # unpartitioned. Two handled conditions must not flood error tracking:
+ # - A read replica can cancel this sizing query with a recovery conflict
+ # (`SerializationFailure` "conflict with recovery"); it's transient and expected on
+ # replicas, and the row-streaming reader retries it in-process.
+ # - An earlier best-effort query in this same transaction (chunk sizing, row count,
+ # partition detection) can hit that transient condition and leave the transaction in
+ # `INERROR`, so this query then fails with `InFailedSqlTransaction` purely as a
+ # downstream symptom.
+ # Both resurface (and are classified through the normal retryable/non-retryable path) via
+ # the real extraction query, so degrade quietly. Genuinely unexpected failures are still
+ # captured.
+ if not _is_recovery_conflict_error(e) and not isinstance(e, psycopg.errors.InFailedSqlTransaction):
capture_exception(e)
logger.debug(f"_get_partition_settings: returning None due to error: {e}")
return None
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index dd98f41065f3..727242a1f8d4 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -1955,6 +1955,31 @@ class TestGetPartitionSettings:
assert result is None
capture_mock.assert_called_once()
+ @pytest.mark.parametrize(
+ "exc",
+ [
+ # Direct read-replica recovery conflict on the sizing query.
+ psycopg.errors.SerializationFailure("canceling statement due to conflict with recovery"),
+ # Downstream symptom: an earlier best-effort query in this transaction hit the transient
+ # condition above and left it in INERROR, so the sizing query fails with this instead.
+ psycopg.errors.InFailedSqlTransaction(
+ "current transaction is aborted, commands ignored until end of transaction block"
+ ),
+ ],
+ )
+ def test_handled_transient_errors_return_none_without_capturing(self, exc):
+ # Both are handled, transient conditions that resurface (and are classified) via the real
+ # extraction query, so partition sizing must degrade to None without flooding error tracking.
+ logger = structlog.get_logger()
+ cursor = mock.MagicMock()
+ cursor.execute.side_effect = exc
+
+ with patch("posthog.temporal.data_imports.sources.postgres.postgres.capture_exception") as capture_mock:
+ result = _get_partition_settings(cast(Any, cursor), "public", "t", logger, is_partitioned=False)
+
+ assert result is None
+ capture_mock.assert_not_called()
+
class TestIsRecoveryConflictError:
@pytest.mark.parametrize(