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): correct misleading timeout message for fallback primary-key probe (#66654)

Tom Owersacd4c813bcf1
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 9213cee76379..9d3de1de9a00 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
@@ -427,6 +427,24 @@ def _statement_timeout_as_non_retryable(
)
+def _pk_uniqueness_probe_timeout_error() -> QueryTimeoutException:
+ """Build the timeout error for the fallback `id` primary-key uniqueness probe.
+
+ When a table has no declared primary key we fall back to assuming `id` is unique and verify
+ it with a full-table `GROUP BY id HAVING COUNT(*) > 1`, which can exhaust the statement_timeout
+ on large tables. The generic table-setup timeout message points at the incremental field, but
+ indexing that field doesn't help this probe — the fix is a primary key / index on `id`. Keeps
+ the "has an appropriate index" fragment so it stays non-retryable at the activity layer too
+ (see source.py).
+ """
+ return QueryTimeoutException(
+ 'Timed out verifying that the "id" column is unique. This table has no primary key, so '
+ 'PostHog assumed "id" was unique to sync incrementally but could not confirm it within the '
+ 'timeout. Add a primary key, or ensure the "id" column has an appropriate index created, so '
+ "PostHog can sync this table incrementally."
+ )
+
+
@dataclasses.dataclass(frozen=True)
class PostgresDiscoveredSchema:
source_catalog: str | None
@@ -2790,9 +2808,14 @@ def postgres_source(
has_duplicate_primary_keys = False
if used_id_pk_fallback:
logger.debug("Checking duplicate primary keys...")
- has_duplicate_primary_keys = _has_duplicate_primary_keys(
- cursor, schema, table_name, primary_keys, logger
- )
+ try:
+ has_duplicate_primary_keys = _has_duplicate_primary_keys(
+ cursor, schema, table_name, primary_keys, logger
+ )
+ except psycopg.errors.QueryCanceled as e:
+ # Surface a message about the assumed `id` primary key rather than
+ # falling through to the generic incremental-field timeout below.
+ raise _pk_uniqueness_probe_timeout_error() from e
except psycopg.errors.QueryCanceled:
if should_use_incremental_field:
raise QueryTimeoutException(
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 2e3cd1dd34ac..f4c29313c329 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
@@ -95,6 +95,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.p
_is_unsupported_function_error,
_next_recovery_conflict_chunk_size,
_normalize_function_names,
+ _pk_uniqueness_probe_timeout_error,
_raise_if_setup_connection_broken,
_recovery_conflict_abort_error,
_rls_active_from_conn,
@@ -789,6 +790,18 @@ class TestPostgresSourceNonRetryableErrors:
assert "QueryTimeoutException" not in matching_keys
assert "has an appropriate index" in matching_keys
+ def test_pk_uniqueness_probe_timeout_is_non_retryable_and_points_at_primary_key(self, source):
+ # A statement_timeout in the fallback `id` uniqueness probe used to surface the generic
+ # "index your incremental field" message, which won't fix this full-table GROUP BY. The
+ # message must point at the assumed primary key while staying non-retryable at the raw
+ # activity-level layer (where str(e) carries no class name).
+ error_msg = str(_pk_uniqueness_probe_timeout_error())
+ assert "primary key" in error_msg
+ assert "incremental field" not in error_msg
+ non_retryable = source.get_non_retryable_errors()
+ matching_keys = [pattern for pattern in non_retryable if pattern in error_msg]
+ assert "has an appropriate index" in matching_keys
+
def test_ssh_handshake_eof_is_non_retryable(self, source):
# `_tunnel_with_handshake_translation` turns paramiko's bare, empty-message handshake
# EOFError into this stable message; without the entry it would match no rule and retry forever.