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): treat a blank stored incremental watermark as no watermark (#83989)

Tom Owers0cc16ea68f62
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/partitioned_tables.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/partitioned_tables.py
index c133f2102223..f461e4b6d792 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/partitioned_tables.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/partitioned_tables.py
@@ -392,7 +392,7 @@ def iterate_date_windows(
]
cursor_lo: Any = db_incremental_field_last_value
- if cursor_lo is None:
+ if cursor_lo is None or cursor_lo == "":
cursor_lo = incremental_type_to_initial_value(incremental_field_type)
cursor_lo = _ensure_aware(cursor_lo, incremental_field_type)
@@ -564,7 +564,10 @@ def build_partition_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 db_incremental_field_last_value is None:
+ # A stored watermark of "" must not become a literal `''` — Postgres rejects casting it
+ # against a numeric/date/etc. column with "invalid input syntax" (see postgres.py's
+ # `_build_query` for the same guard on the non-partitioned path).
+ if db_incremental_field_last_value is None or db_incremental_field_last_value == "":
db_incremental_field_last_value = incremental_type_to_initial_value(incremental_field_type)
operator = sql.SQL(incremental_type_to_operator(incremental_field_type))
@@ -603,7 +606,7 @@ def iterate_partitions(
# If range-partitioned on the incremental field, skip children whose upper
# bound is at or below cursor to avoid reading already-synced data.
- skippable_upper: Any = db_incremental_field_last_value
+ skippable_upper: Any = db_incremental_field_last_value if db_incremental_field_last_value != "" else None
if incremental_field_type is not None and skippable_upper is not None:
skippable_upper = _ensure_aware(skippable_upper, incremental_field_type)
partition_bounds: dict[str, tuple[Any, Any] | None] = {}
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 b1a98a5ca06d..30675454e4ec 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
@@ -1992,7 +1992,10 @@ def _build_query(
if incremental_field_type == IncrementalFieldType.XID:
raise ValueError(XMIN_AS_INCREMENTAL_FIELD_ERROR)
- if db_incremental_field_last_value is None:
+ # A stored watermark of "" (e.g. a stale/corrupted sync_type_config value) must not become a
+ # literal `''` in the WHERE clause — Postgres rejects casting it against a numeric/date/etc.
+ # column with "invalid input syntax", so treat it the same as no watermark at all.
+ if db_incremental_field_last_value is None or db_incremental_field_last_value == "":
db_incremental_field_last_value = incremental_type_to_initial_value(incremental_field_type)
# Use the type-aware operator (`>=` for Date) only for single-shot scans. Windowed
@@ -2119,7 +2122,7 @@ def _build_count_query(
if incremental_field_type == IncrementalFieldType.XID:
raise ValueError(XMIN_AS_INCREMENTAL_FIELD_ERROR)
- if db_incremental_field_last_value is None:
+ if db_incremental_field_last_value is None or db_incremental_field_last_value == "":
db_incremental_field_last_value = incremental_type_to_initial_value(incremental_field_type)
operator = sql.SQL(incremental_type_to_operator(incremental_field_type))
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 a3cf7bdfeed3..4ff4c826fcf0 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
@@ -4605,6 +4605,19 @@ class TestBuildQuery:
with pytest.raises(ValueError):
_build_query("public", "events", True, "table", "id", None, None)
+ @pytest.mark.parametrize(
+ "field_type",
+ [IncrementalFieldType.Numeric, IncrementalFieldType.Integer],
+ )
+ def test_empty_string_last_value_falls_back_to_initial_value(self, field_type):
+ # Regression: a stored watermark of "" used to become a literal `''` in the WHERE
+ # clause, which Postgres rejects for a numeric/integer column with "invalid input
+ # syntax for type numeric" instead of falling back like a missing (None) watermark.
+ query = _build_query("public", "events", True, "table", "cursor", field_type, "")
+ rendered = self._render(query)
+ assert "''" not in rendered
+ assert '"cursor" > 0' in rendered
+
def test_sampling_table(self):
query = _build_query("public", "users", False, "table", None, None, None, add_sampling=True)
rendered = self._render(query)
@@ -5083,6 +5096,19 @@ class TestBuildPartitionQuery:
rendered = self._render(query)
assert f'"cursor" {expected_operator} ' in rendered
+ def test_empty_string_last_value_falls_back_to_initial_value(self):
+ query = build_partition_query(
+ "public",
+ "events_2026_01",
+ should_use_incremental_field=True,
+ incremental_field="cursor",
+ incremental_field_type=IncrementalFieldType.Numeric,
+ db_incremental_field_last_value="",
+ )
+ rendered = self._render(query)
+ assert "''" not in rendered
+ assert '"cursor" > 0' in rendered
+
def test_row_filter_full_refresh(self):
query = build_partition_query(
"public",
@@ -5135,6 +5161,12 @@ class TestBuildCountQuery:
assert "ORDER BY" not in rendered
assert "FROM (" not in rendered
+ def test_empty_string_last_value_falls_back_to_initial_value(self):
+ query = _build_count_query("public", "events", True, "cursor", IncrementalFieldType.Numeric, "")
+ rendered = self._render(query)
+ assert "''" not in rendered
+ assert '"cursor" > 0' in rendered
+
class TestIsPartitionedTable:
@pytest.mark.parametrize(