: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(data-warehouse): isolate postgres metadata probes in savepoints (#62150)
Daniel RC3dd3a57db6a5
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.pyindex 71c2d3e9ec2d..43c2050ba829 100644--- a/posthog/temporal/data_imports/sources/postgres/postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py@@ -997,9 +997,12 @@ def _explain_query(cursor: psycopg.Cursor, query: sql.Composed, logger: Filterinlogger.debug(f"Running EXPLAIN on {query.as_string()}")try:- query_with_explain = sql.SQL("EXPLAIN {}").format(query)- cursor.execute(query_with_explain)- rows = cursor.fetchall()+ # Debug-only and best-effort: a query may use syntax the source DB rejects (e.g.+ # TABLESAMPLE on CockroachDB). Savepoint so a failure doesn't poison the transaction.+ with cursor.connection.transaction(savepoint_name="explain_query"):+ query_with_explain = sql.SQL("EXPLAIN {}").format(query)+ cursor.execute(query_with_explain)+ rows = cursor.fetchall()explain_result: str = ""# Build up a single string of the EXPLAIN outputfor row in rows:@@ -1007,7 +1010,6 @@ def _explain_query(cursor: psycopg.Cursor, query: sql.Composed, logger: Filterinexplain_result += f"\n{col}"logger.debug(f"EXPLAIN result: {explain_result}")except Exception as e:- capture_exception(e)logger.debug(f"EXPLAIN raised an exception: {e}")@@ -1146,10 +1148,13 @@ def _get_table_chunk_size(cursor: psycopg.Cursor, inner_query: sql.Composed, log) as subquery""").format(inner_query)- _explain_query(cursor, query, logger)- logger.debug(f"Running query: {query.as_string()}")- cursor.execute(query)- row = cursor.fetchone()+ # Best-effort: the sampled query can fail (e.g. TABLESAMPLE on CockroachDB). Savepoint+ # so a failure falls back to DEFAULT_CHUNK_SIZE without poisoning the transaction.+ with cursor.connection.transaction(savepoint_name="table_chunk_size"):+ _explain_query(cursor, query, logger)+ logger.debug(f"Running query: {query.as_string()}")+ cursor.execute(query)+ row = cursor.fetchone()if row is None:logger.debug(f"_get_table_chunk_size: No results returned. Using DEFAULT_CHUNK_SIZE={DEFAULT_CHUNK_SIZE}")diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.pyindex d93aaf2fd269..9b55dd24d6d2 100644--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py@@ -14,6 +14,7 @@ import structlogfrom psycopg import sqlimport posthog.temporal.data_imports.sources.postgres.partitioned_tables as partitioned_tables_pkg+from posthog.temporal.data_imports.pipelines.pipeline.consts import DEFAULT_CHUNK_SIZEfrom posthog.temporal.data_imports.pipelines.pipeline.utils import (DEFAULT_NUMERIC_SCALE,MAX_NUMERIC_SCALE,@@ -49,6 +50,7 @@ from posthog.temporal.data_imports.sources.postgres.postgres import (_get_primary_keys,_get_sslmode,_get_table,+ _get_table_chunk_size,_has_duplicate_primary_keys,_is_connection_dropped_error,_is_partitioned_table,@@ -1079,6 +1081,21 @@ class TestIsPartitionedTable:assert _is_partitioned_table(cast(Any, dj_cursor), "public", table_name) is expected+class TestGetTableChunkSize:+ @pytest.mark.django_db+ def test_failing_probe_falls_back_without_poisoning_transaction(self):+ logger = structlog.get_logger()++ with django_connection.cursor() as dj_cursor:+ inner_query = sql.SQL("SELECT * FROM does_not_exist_chunk_probe").format()++ chunk_size = _get_table_chunk_size(cast(Any, dj_cursor), inner_query, logger)+ assert chunk_size == DEFAULT_CHUNK_SIZE++ dj_cursor.execute("SELECT 1")+ assert dj_cursor.fetchone()[0] == 1++class TestPartitionedTableChunkSizing:"""Incremental reads use per-child partition queries; no parent FETCH chunk cap."""