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): recognize statement-timeout QueryTimeoutException at the activity layer (#65151)

Tom Owers71f7104cf495
diff --git a/posthog/temporal/data_imports/sources/postgres/source.py b/posthog/temporal/data_imports/sources/postgres/source.py
index bf6fd1ba66f9..f2aed09b91aa 100644
--- a/posthog/temporal/data_imports/sources/postgres/source.py
+++ b/posthog/temporal/data_imports/sources/postgres/source.py
@@ -315,6 +315,13 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
"does not exist": None,
"timestamp too small": None,
"QueryTimeoutException": None,
+ # Activity-layer twin of the `QueryTimeoutException` key above. That key only matches once
+ # Temporal wraps the failure (the stringified ApplicationError carries the class name); the
+ # activity-level check sees the raw `str(e)`, which is the bare message with no class name.
+ # Without a message key the timeout goes unrecognised there and the activity burns its full
+ # retry budget re-running the same futile statement-timeout query before the workflow gives
+ # up. Match the index-guidance fragment every postgres statement-timeout message shares.
+ "has an appropriate index": None,
"TemporaryFileSizeExceedsLimitException": None,
"Name or service not known": None,
# Sibling getaddrinfo failure to "Name or service not known" (EAI_NONAME): EAI_NODATA
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index 820621d61487..83d5d2736667 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -725,6 +725,40 @@ class TestPostgresSourceNonRetryableErrors:
is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
assert not is_non_retryable, f"Transient error should remain retryable: {error_msg}"
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Raw activity-level message (what `_handle_import_error` sees via str(e)) — no class name.
+ # The standard incremental-read path (postgres.py) and the partitioned-table window path
+ # word the guidance slightly differently but share the "appropriate index" fragment.
+ "10 min timeout statement reached. Please ensure your incremental field (updated_at) has an appropriate index created",
+ "window 2024-01-01..2024-02-01 hit statement_timeout after 5 retries. Please ensure updated_at has an appropriate index.",
+ # Temporal-wrapped message (what the workflow-level check sees) — carries the class name.
+ "QueryTimeoutException: 10 min timeout statement reached. Please ensure your incremental field (updated_at) has an appropriate index created",
+ ],
+ )
+ def test_statement_timeout_query_timeout_is_non_retryable(self, source, error_msg):
+ 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"Statement-timeout error should be non-retryable: {error_msg}"
+
+ def test_statement_timeout_raw_message_matches_index_fragment_not_class_name(self, source):
+ # The raw activity-level message doesn't carry the class name, so the "QueryTimeoutException"
+ # key can't catch it there — confirm the dedicated message fragment is what recognises it.
+ error_msg = str(
+ _statement_timeout_as_non_retryable(
+ psycopg.errors.QueryCanceled("canceling statement due to statement timeout"),
+ should_use_incremental_field=True,
+ incremental_field="updated_at",
+ )
+ )
+ non_retryable = source.get_non_retryable_errors()
+ matching_keys = [pattern for pattern in non_retryable if pattern in error_msg]
+ # The class-name key can't catch the raw message (str(e) carries no class name); the
+ # dedicated message fragment is what recognises it at the activity layer.
+ assert "QueryTimeoutException" not in matching_keys
+ assert "has an appropriate index" in matching_keys
+
class TestPostgresSourceSetupRecoveryConflictRetry:
@staticmethod