: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): stop retrying corrupted row data as a transient error (#103297)
Tom Owersd8d41a2a2d85
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.pyindex 6b93cd5645ed..421e7687873f 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@@ -332,6 +332,17 @@ _CONNECTION_LIMIT_EXHAUSTED_MESSAGE = ("schedule.")+# What a customer reads when their database reports a damaged page rather than a damaged row+# length. The driver text is raw Postgres internals (a TOAST chunk number, a block number), so it+# reads like a PostHog defect and names no next action.+_SOURCE_PAGE_CORRUPTION_ERROR = (+ "PostHog couldn't read one of the tables you're syncing because your database reported "+ "damaged data on disk. PostHog only reads from your source, so this has to be repaired on "+ "your database. Check your database server logs, run a consistency check on the table (for "+ "example pg_amcheck), reindex it if an index is damaged, or restore the affected data from a "+ "backup. Then re-enable the sync."+)+_RECOVERY_CONFLICT_EXHAUSTED_MESSAGE = ("Your read replica kept canceling PostHog's reads because it had to apply changes from the ""primary that removed rows the sync was still reading, and the conflict outlasted every retry. "@@ -1023,6 +1034,30 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa"memory pressure on your database (for example lower work_mem, reduce concurrent ""connections, or increase the instance's memory), then re-enable the sync."),+ # PostgreSQL's allocator rejects a request it can't service, raised via a bare `elog`+ # that carries no specific SQLSTATE and so surfaces as the internal-error class (XX000,+ # psycopg's `InternalError_`): "invalid memory alloc request size <n>". Observed while+ # streaming rows through a server-side cursor (see `get_rows`) with the requested size+ # wrapped to just under UINT64_MAX — the signature of a corrupted length field in the+ # row's own stored data (for example a damaged TOAST pointer), not anything in our query.+ # The corruption lives in the source row, so retrying re-reads into the same wall every+ # time. The volatile request size is excluded from the match.+ "invalid memory alloc request size": (+ "PostgreSQL refused to allocate memory while reading a row from one of your tables "+ '("invalid memory alloc request size"). This usually means that row\'s stored data is '+ "corrupted on the source database (for example a damaged TOAST value), rather than a "+ "problem with the sync. Check this table for data corruption (for example with "+ "pg_amcheck), then repair or remove the affected rows and re-enable the sync."+ ),+ # The same damage reported through the wordings that name the page instead of the+ # allocation: a TOAST row whose out-of-line chunks are gone, an index page that reads+ # back as zeroes, and a heap or index page the server could not read at all. We only+ # ever run `SELECT ... FROM <relation>`, so each one is damage on the customer's side,+ # fixed to the affected page, and every retry re-reads that page into the same error.+ # The volatile chunk and block numbers and relation names are excluded from the match.+ "missing chunk number": _SOURCE_PAGE_CORRUPTION_ERROR,+ "unexpected zero page": _SOURCE_PAGE_CORRUPTION_ERROR,+ "could not read block": _SOURCE_PAGE_CORRUPTION_ERROR,# Raised when a Postgres numeric value cannot be represented in any Delta-compatible# decimal type — the pipeline falls back through the best-fit decimal and# `decimal256(76, 32)` before giving up. Only triggers when source data genuinelydiff --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 6d0b0db050e6..48beabfb949a 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@@ -873,6 +873,51 @@ class TestPostgresSourceNonRetryableErrors:assert friendly, "Server out-of-memory error should surface an actionable message"assert "ran out of memory" in friendly[0]+ @pytest.mark.parametrize(+ "error_msg",+ [+ # Raw psycopg message (what the activity-level check sees via str(e)). The request size+ # is volatile; the "invalid memory alloc request size" text is stable.+ "invalid memory alloc request size 18446744073709551613",+ # Temporal-wrapped message (what the workflow-level check sees) — carries the class name.+ "InternalError_: invalid memory alloc request size 18446744073709551613",+ ],+ )+ def test_invalid_memory_alloc_request_size_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"Invalid memory alloc request size error should be non-retryable: {error_msg}"++ def test_invalid_memory_alloc_request_size_returns_friendly_message(self, source):+ non_retryable = source.get_non_retryable_errors()+ error_msg = "invalid memory alloc request size 18446744073709551613"+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]+ assert friendly, "Invalid memory alloc request size error should surface an actionable message"+ assert "corrupted" in friendly[0]++ @pytest.mark.parametrize(+ "error_msg",+ [+ # Raw psycopg messages (what the activity-level check sees via str(e)). The chunk and+ # block numbers and the relation names are volatile.+ "missing chunk number 0 for toast value 90210 in pg_toast_16384",+ 'index "orders_pkey" contains unexpected zero page at block 42',+ 'could not read block 7 in file "base/16384/16385": read only 0 of 8192 bytes',+ # Temporal-wrapped message (what the workflow-level check sees) — carries the class name.+ "InternalError_: missing chunk number 0 for toast value 90210 in pg_toast_16384",+ ],+ )+ def test_damaged_source_page_is_non_retryable(self, source, error_msg):+ # The sibling wordings of the allocation-size failure above name the page rather than the+ # row length, so none of them match that key and each would otherwise retry to exhaustion.+ non_retryable = source.get_non_retryable_errors()+ assert error_message_matches(error_msg, non_retryable.keys()), (+ f"Damaged source page should be non-retryable: {error_msg}"+ )+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]+ assert friendly, "Damaged source page should surface an actionable message"+ assert "damaged data on disk" in friendly[0]+@pytest.mark.parametrize("error_msg",[