: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): treat backward slot advance as a benign no-op (#68542)
Tom Owersb284a3993b0c
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/stream_reader.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/stream_reader.pyindex 347ba60dfffe..92a0634d4fda 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/stream_reader.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/stream_reader.py@@ -44,6 +44,17 @@ _SLOT_ADVANCE_MAX_ATTEMPTS = 3_SLOT_READ_MAX_ATTEMPTS = 4+def _is_slot_already_ahead_error(exc: psycopg.errors.ObjectNotInPrerequisiteState) -> bool:+ """Whether Postgres refused a slot advance because the slot is already at or past the target+ LSN ("cannot advance replication slot to X, minimum is Y" with Y > X).++ Distinct from slot invalidation (``is_slot_invalidation_error``): the slot is healthy and+ simply ahead of the requested position, so the advance is a benign no-op rather than a failure.+ """+ message = str(exc).lower()+ return "cannot advance replication slot to" in message and "minimum is" in message++@dataclass(frozen=True, slots=True)class PgCDCConnectionParams:host: str@@ -225,6 +236,14 @@ class PgCDCStreamReader:)try:self._advance_slot(advance_conn, query)+ except psycopg.errors.ObjectNotInPrerequisiteState as e:+ if not _is_slot_already_ahead_error(e):+ raise+ # The slot's confirmed position already sits at or beyond `position` — a retried or+ # overlapping run advanced it further — so the WAL up to here is already released and+ # there is nothing to do. Postgres refuses a backward advance; treat it as a no-op.+ logger.info("slot_already_ahead", slot_name=self._params.slot_name, position=position)+ returnfinally:advance_conn.close()diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_stream_reader.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_stream_reader.pyindex ab0f3dcd5f79..f6b33ad4ef62 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_stream_reader.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/cdc/tests/test_stream_reader.py@@ -3,6 +3,7 @@ from unittest import mockfrom unittest.mock import patchimport psycopg+import psycopg.errorsfrom products.warehouse_sources.backend.temporal.data_imports.sources.postgres.cdc.stream_reader import (_SLOT_READ_MAX_ATTEMPTS,@@ -337,3 +338,45 @@ class TestPgCDCStreamReaderConfirmPosition:assert cur.execute.call_count == 3conn.commit.assert_not_called()conn.close.assert_called_once()++ def test_confirm_position_tolerates_slot_already_ahead(self, params):+ # A retried / overlapping run advanced the slot further, so Postgres refuses the backward+ # advance. The WAL is already released past this LSN, so it must be a no-op, not a failure.+ conn = mock.MagicMock()+ conn.cursor.return_value.__enter__.return_value.execute.side_effect = (+ psycopg.errors.ObjectNotInPrerequisiteState(+ "cannot advance replication slot to B4/C7327D08, minimum is B4/CB22FB98"+ )+ )+ connect = mock.MagicMock(return_value=conn)++ reader = PgCDCStreamReader(params)+ with patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.postgres.cdc.stream_reader._connect_to_postgres",+ connect,+ ):+ reader.confirm_position("B4/C7327D08")++ conn.commit.assert_not_called()+ conn.close.assert_called_once()++ def test_confirm_position_reraises_other_prerequisite_errors(self, params):+ # Slot invalidation is also ObjectNotInPrerequisiteState but must keep propagating so the+ # slot gets recreated — only the "already ahead" case is swallowed.+ conn = mock.MagicMock()+ conn.cursor.return_value.__enter__.return_value.execute.side_effect = (+ psycopg.errors.ObjectNotInPrerequisiteState(+ "cannot advance replication slot that has not previously reserved WAL"+ )+ )+ connect = mock.MagicMock(return_value=conn)++ reader = PgCDCStreamReader(params)+ with patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.postgres.cdc.stream_reader._connect_to_postgres",+ connect,+ ):+ with pytest.raises(psycopg.errors.ObjectNotInPrerequisiteState):+ reader.confirm_position("B4/C7327D08")++ conn.close.assert_called_once()