: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): treat source-side read-only transaction writes as non-retryable (#72354)
Tom Owers9aaf89250238
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 917a64fb31ac..ab27896c29bd 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@@ -644,6 +644,22 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa# against the source data, so retrying re-evaluates the same view and hits the same row."cannot call jsonb_each on a non-object": "A view you're syncing calls jsonb_each() on a JSON value that isn't an object for at least one row, so Postgres can't evaluate the view and we can't read it. Guard the call in your view definition (for example only call jsonb_each() when jsonb_typeof(col) = 'object'), or remove that view from the sync.","cannot call jsonb_each_text on a non-object": "A view you're syncing calls jsonb_each_text() on a JSON value that isn't an object for at least one row, so Postgres can't evaluate the view and we can't read it. Guard the call in your view definition (for example only call jsonb_each_text() when jsonb_typeof(col) = 'object'), or remove that view from the sync.",+ # A selected relation's own definition writes to the database while we read it — a view or+ # trigger that calls a function which runs REFRESH MATERIALIZED VIEW (or INSERT/UPDATE/DELETE).+ # We read inside a read-only transaction and never write to the source, so Postgres rejects+ # the write with SQLSTATE 25006 "cannot execute <stmt> in a read-only transaction". The write+ # lives in the customer's schema and is deterministic, so retrying re-reads into the same+ # wall. Match the stable read-only-transaction phrase and exclude the volatile statement+ # kind and relation name; it appears in both the raw activity-level str(e) and the+ # Temporal-wrapped "ReadOnlySqlTransaction: ..." workflow-level form.+ "in a read-only transaction": (+ "One of the tables or views you selected to sync tries to write to your database while "+ "PostHog is reading it (for example a view or trigger that refreshes a materialized "+ "view), and PostHog reads inside a read-only transaction so the write is rejected "+ '("cannot execute ... in a read-only transaction"). PostHog never writes to your '+ "source. Remove that relation from the sync, or change its definition so reading it "+ "doesn't perform a write, then re-enable the sync."+ ),# A selected relation is a postgres_fdw foreign table and the connecting role has no user# mapping for the foreign server it points at, so every SELECT fails with# "UndefinedObject: user mapping not found for user <user>, server <server>" (SQLSTATEdiff --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 72e3e0cebc7a..4fa573d16b61 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@@ -879,6 +879,30 @@ class TestPostgresSourceNonRetryableErrors:assert friendly, "Unpopulated materialized view error should surface an actionable message"assert "REFRESH MATERIALIZED VIEW" in friendly[0]+ @pytest.mark.parametrize(+ "error_msg",+ [+ # Raw psycopg message (what the activity-level check sees via str(e)) — a view/trigger+ # function refreshing a materialized view during our read-only SELECT.+ 'cannot execute REFRESH MATERIALIZED VIEW in a read-only transaction\nCONTEXT: SQL statement "REFRESH MATERIALIZED VIEW CONCURRENTLY analyticssnapshot"',+ # Temporal-wrapped message (what the workflow-level check sees) — carries the class name.+ "ReadOnlySqlTransaction: cannot execute REFRESH MATERIALIZED VIEW in a read-only transaction",+ # Same class of upstream write via a different statement — the match must generalize.+ "cannot execute INSERT in a read-only transaction",+ ],+ )+ def test_readonly_transaction_write_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"Write in a read-only transaction should be non-retryable: {error_msg}"++ def test_readonly_transaction_write_returns_friendly_message(self, source):+ non_retryable = source.get_non_retryable_errors()+ error_msg = "cannot execute REFRESH MATERIALIZED VIEW in a read-only transaction"+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]+ assert friendly, "Write in a read-only transaction should surface an actionable message"+ assert "read-only transaction" in friendly[0]+@pytest.mark.parametrize("error_msg",[