: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): say what to do when a full-table read times out (#100065)
Tom Owers4f3242d191d4
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.pyindex 52f9c7c253b3..ca5ee0d5a359 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@@ -698,8 +698,8 @@ def _statement_timeout_as_non_retryable(so retrying is futile. On incremental syncs, map it to the same non-retryableQueryTimeoutException the server-cursor and windowed read paths already raise,with an actionable message. Returns None when the error is not a statement- timeout, or the sync is non-incremental (the caller should re-raise the original- error so a full re-sync can reorder rows safely).+ timeout, or the sync is non-incremental — a full-table read restarts from scratch,+ so the caller keeps it retryable and words it with `_full_table_timeout_error`."""if not isinstance(error, psycopg.errors.QueryCanceled) or not should_use_incremental_field:return None@@ -709,6 +709,21 @@ def _statement_timeout_as_non_retryable()+def _full_table_timeout_error() -> Exception:+ """Build the timeout error for a full-table read cancelled by the statement_timeout.++ `_statement_timeout_as_non_retryable` covers incremental reads only, so a full-table read used+ to propagate psycopg's raw "canceling statement due to statement timeout" — driver text that+ names neither the table nor anything the customer can change. This stays a plain retryable+ Exception, matching no key in `get_non_retryable_errors`: a full-table read restarts from+ scratch, so unlike an incremental read it can still finish on a later attempt.+ """+ return Exception(+ "Reading this table hit your database's statement timeout before it finished. Switch the "+ "table to incremental replication in its sync settings so each run reads less."+ )++def _raised_while_closing_generator(error: BaseException) -> bool:"""True when `error` surfaced while the row generator was being closed.@@ -3987,7 +4002,7 @@ def postgres_source("max_standby_streaming_delay or enable hot_standby_feedback on the replica, ""or sync from the primary database instead.") from e- raise+ raise _full_table_timeout_error() from eexcept _CONNECTION_DROPPED_ERROR_TYPES as e:if _is_recovery_conflict_error(e):# A recovery conflict raised by the (re)connect itself surfaces as a plain@@ -4262,7 +4277,7 @@ def postgres_source()if timeout_error is not None:raise timeout_error from e- raise+ raise _full_table_timeout_error() from eexcept psycopg.errors.LockNotAvailable as e:# The server-cursor DECLARE waited past the source's lock_timeout for a lock# another transaction holds (a concurrent DDL / VACUUM FULL takes ACCESSdiff --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.pyindex beebaf9a2819..2041dd3d5164 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@@ -105,6 +105,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.p_connect_to_postgres,_connect_with_dropped_retry,_fetch_rows_for,+ _full_table_timeout_error,_get_estimated_row_count_for_partitioned_table,_get_partition_settings,_get_partition_settings_for_partitioned_table,@@ -1529,6 +1530,13 @@ class TestPostgresSourceNonRetryableErrors:assert "QueryTimeoutException" not in matching_keysassert "has an appropriate index" in matching_keys+ def test_full_table_timeout_message_stays_retryable(self, source):+ # A full-table read restarts from scratch, so it keeps retrying. Wording it like the+ # incremental message would collide with a non-retryable key and disable the sync.+ error_msg = str(_full_table_timeout_error())+ non_retryable = source.get_non_retryable_errors()+ assert not any(pattern in error_msg for pattern in non_retryable)+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@@ -3163,8 +3171,8 @@ class TestStatementTimeoutAsNonRetryable:[# Incremental syncs map the timeout to a non-retryable QueryTimeoutException.(True, "updated_at", "updated_at"),- # Full-table syncs must re-raise the raw QueryCanceled so a fresh re-sync can- # reorder rows; we only short-circuit incremental reads.+ # Full-table syncs stay retryable so a fresh re-sync can reorder rows; we only+ # short-circuit incremental reads.(False, None, None),],)@@ -3204,8 +3212,9 @@ class TestServerCursorStatementTimeout:"""The main server-cursor streaming path in `get_rows` must not leak a raw,retryable QueryCanceled when a FETCH hits the statement_timeout — it must mapto a non-retryable QueryTimeoutException for incremental syncs (mirroring the- offset-chunking and windowed paths), and re-raise the raw error for full-table- syncs so a fresh re-sync can reorder rows safely.+ offset-chunking and windowed paths). A full-table read stays retryable so a fresh+ re-sync can reorder rows safely, but must still carry a message the customer can act+ on rather than psycopg's raw text."""class _Cursor:@@ -3306,9 +3315,9 @@ class TestServerCursorStatementTimeout:[# Incremental syncs map the FETCH timeout to a non-retryable QueryTimeoutException.(True, QueryTimeoutException, "updated_at"),- # Full-table syncs have no stable ORDER BY, so we re-raise the raw QueryCanceled- # to let a fresh re-sync reorder rows rather than giving up.- (False, psycopg.errors.QueryCanceled, None),+ # Full-table syncs have no stable ORDER BY, so they stay retryable to let a fresh+ # re-sync reorder rows rather than giving up — with a message that names the fix.+ (False, Exception, "incremental replication"),],)def test_statement_timeout_handling(self, should_use_incremental_field, expected_exception, expected_substr):