: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): fall back to default chunk size on estimation timeout (#63797)
Tom Owersc0946456e354
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.pyindex 225b7440d7ab..50df1c39db13 100644--- a/posthog/temporal/data_imports/sources/postgres/postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py@@ -1299,9 +1299,16 @@ def _get_table_chunk_size(cursor: psycopg.Cursor, inner_query: sql.Composed, log)return chunk_size- except psycopg.errors.QueryCanceled:- raiseexcept Exception as e:+ # Best-effort: any failure (including a statement_timeout / QueryCanceled) falls back to+ # DEFAULT_CHUNK_SIZE. The estimation query wraps the sample in `octet_length(t::text)`,+ # which serializes every column to text and so evaluates generated columns and check/domain+ # validator functions — making it strictly more expensive than the real chunked `SELECT *`+ # extraction. A timeout here therefore says nothing about whether extraction will succeed,+ # and the savepoint above keeps the connection usable. The streaming read loop has its own+ # dedicated QueryCanceled handling (`_statement_timeout_as_non_retryable`), so re-raising the+ # cancellation here would only bypass that path and leak a raw, retryable QueryCanceled that+ # Temporal re-attempts forever on tables this query can never complete on.logger.debug(f"_get_table_chunk_size: Error: {e}. Using DEFAULT_CHUNK_SIZE={DEFAULT_CHUNK_SIZE}", exc_info=e)return DEFAULT_CHUNK_SIZEdiff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.pyindex e05ef64e751c..2717b4f45f51 100644--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py@@ -1361,6 +1361,26 @@ class TestGetTableChunkSize:cursor.execute("SELECT 1")assert cursor.fetchone()[0] == 1+ @pytest.mark.django_db+ def test_statement_timeout_falls_back_without_poisoning_transaction(self):+ # The estimation query wraps the sample in octet_length(t::text), which can exceed the+ # source's statement_timeout on tables whose rows trigger slow validator functions. A+ # cancelled probe must degrade to DEFAULT_CHUNK_SIZE, not crash the whole import — the+ # streaming read loop has its own dedicated QueryCanceled handling.+ logger = structlog.get_logger()++ with django_connection.cursor() as dj_cursor:+ # Tight timeout + a deliberately slow probe reproduces a real QueryCanceled.+ dj_cursor.execute("SET LOCAL statement_timeout = '100ms'")+ inner_query = sql.SQL("SELECT pg_sleep(3) AS c").format()++ chunk_size = _get_table_chunk_size(cast(Any, dj_cursor), inner_query, logger)+ assert chunk_size == DEFAULT_CHUNK_SIZE++ # Savepoint rollback leaves the connection usable for the rest of setup.+ dj_cursor.execute("SELECT 1")+ assert dj_cursor.fetchone()[0] == 1+class TestGetRowsToSync:# Mirrors a misconfigured incremental field: COUNT(*) over a column that doesn't exist.