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): reject xmin as a plain incremental/append field (#75971)

Tom Owers5c63803e2cea
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
index bddb75621bdd..98763dd21712 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
@@ -110,6 +110,14 @@ XMIN_PROJECTED_COLUMN = "_ph_xmin"
# xmin replication relies on `pg_snapshot_xmin` / `xid8`, both PG13+.
XMIN_MIN_SERVER_VERSION = 130000
+# Raised when `xmin` (type `IncrementalFieldType.XID`) is configured as a plain incremental/append
+# field instead of going through the dedicated xmin replication sync type. Matched verbatim in
+# `PostgresSource.get_non_retryable_errors`.
+XMIN_AS_INCREMENTAL_FIELD_ERROR = (
+ "'xmin' incremental field type is only valid for the xmin replication sync type, not for "
+ "incremental or append syncs"
+)
+
# In-process retries for a recovery conflict before the abort (non-retryable, see source.py). The
# read path counts these only once the chunk has shrunk to the floor.
_MAX_SETUP_RECOVERY_CONFLICT_RETRIES = 10
@@ -1937,6 +1945,14 @@ def _build_query(
if incremental_field is None or incremental_field_type is None:
raise ValueError("incremental_field and incremental_field_type can't be None")
+ # `xmin` is a synthetic system column with no ordering operators against a plain integer
+ # (see `_xmin_predicate`) — it only works through the dedicated xmin replication sync type,
+ # which never reaches this generic path (it always sets `xmin_bounds` and returns above).
+ # Picking `xmin` as a plain incremental/append field builds `"xmin" >= 0`, which Postgres
+ # rejects with `UndefinedFunction`.
+ if incremental_field_type == IncrementalFieldType.XID:
+ raise ValueError(XMIN_AS_INCREMENTAL_FIELD_ERROR)
+
if db_incremental_field_last_value is None:
db_incremental_field_last_value = incremental_type_to_initial_value(incremental_field_type)
@@ -2061,6 +2077,9 @@ def _build_count_query(
if incremental_field is None or incremental_field_type is None:
raise ValueError("incremental_field and incremental_field_type can't be None")
+ if incremental_field_type == IncrementalFieldType.XID:
+ raise ValueError(XMIN_AS_INCREMENTAL_FIELD_ERROR)
+
if db_incremental_field_last_value is None:
db_incremental_field_last_value = incremental_type_to_initial_value(incremental_field_type)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
index 83f78f657ae5..d4149fb8f06e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
@@ -44,6 +44,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.c
)
from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.postgres import (
_SSH_HANDSHAKE_EOF_ERROR,
+ XMIN_AS_INCREMENTAL_FIELD_ERROR,
PostgresImplementation,
SSLRequiredError,
_rls_active_from_conn,
@@ -404,6 +405,15 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
"PostHog can't build its sync query. Choose an incremental field for the table in its "
"sync settings, or switch it to full table replication, then re-enable the sync."
),
+ # `xmin` was picked as a plain incremental/append field instead of through the dedicated
+ # xmin replication sync type — `_build_query`/`_build_count_query` raise this before
+ # emitting SQL Postgres would reject (`xid >= integer` has no operator). The stored config
+ # is fixed until the customer changes it, so every retry re-hits the same wall.
+ XMIN_AS_INCREMENTAL_FIELD_ERROR: (
+ "This table's incremental field is set to Postgres's internal 'xmin' system column, "
+ "which only works with the dedicated xmin replication sync type. Switch this schema to "
+ "xmin replication, or choose a different incremental field, then re-enable the sync."
+ ),
"failed: timeout expired": None,
# NOTE: "SSL connection has been closed unexpectedly" is intentionally NOT listed here.
# It denotes an established SSL connection being dropped on connect or mid-stream (idle
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
index 4b5246f38bbb..14abe094a019 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
@@ -830,6 +830,32 @@ class TestPostgresSourceNonRetryableErrors:
assert friendly, "Missing incremental field error should surface an actionable message"
assert "incremental field" in friendly[0]
+ @pytest.mark.parametrize(
+ "builder",
+ [
+ # schema, table_name, should_use_incremental_field, table_type, incremental_field,
+ # incremental_field_type, db_incremental_field_last_value
+ lambda: _build_query("public", "my_table", True, None, "xmin", IncrementalFieldType.XID, None),
+ # _build_count_query has no `table_type` parameter
+ lambda: _build_count_query("public", "my_table", True, "xmin", IncrementalFieldType.XID, None),
+ ],
+ )
+ def test_xmin_as_incremental_field_is_non_retryable(self, source, builder):
+ # `xmin` is only valid through the dedicated xmin replication sync type. Picking it as a
+ # plain incremental/append field used to build `"xmin" >= 0`, which Postgres rejects with
+ # `UndefinedFunction: operator does not exist: xid >= integer`. Drive the real raise sites so
+ # a message change that breaks the classifier key is caught.
+ with pytest.raises(ValueError) as exc_info:
+ builder()
+ error_msg = str(exc_info.value)
+ non_retryable = source.get_non_retryable_errors()
+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
+ assert is_non_retryable, f"xmin-as-incremental-field error should be non-retryable: {error_msg}"
+
+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]
+ assert friendly, "xmin-as-incremental-field error should surface an actionable message"
+ assert "xmin replication" in friendly[0]
+
def test_exhausted_recovery_conflict_retries_are_non_retryable(self, source):
error_msg = str(_recovery_conflict_abort_error(10))
non_retryable = source.get_non_retryable_errors()