Skip to content

: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.

fix(temporal): add missing SAVEPOINT in _get_table_chunk_size (#63913)

mendral-app[bot]12757fd8d9a1
diff --git a/posthog/temporal/data_imports/sources/postgres/postgres.py b/posthog/temporal/data_imports/sources/postgres/postgres.py
index 98f9e345a5a3..0c10e7ed9ea9 100644
--- a/posthog/temporal/data_imports/sources/postgres/postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/postgres.py
@@ -1295,7 +1295,15 @@ def _has_duplicate_primary_keys(
def _get_table_chunk_size(cursor: psycopg.Cursor, inner_query: sql.Composed, logger: FilteringBoundLogger) -> int:
+ # Under autocommit each statement is its own transaction — a failure can't poison
+ # subsequent commands, so no SAVEPOINT is needed. When called inside a shared
+ # transaction (e.g. tests or future callers), wrap in a SAVEPOINT so that a
+ # QueryCanceled doesn't abort the surrounding transaction.
+ use_savepoint = not cursor.connection.autocommit
try:
+ if use_savepoint:
+ cursor.execute("SAVEPOINT _chunk_size_probe")
+
query = sql.SQL("""
SELECT percentile_cont(0.95) within group (order by subquery.row_size) FROM (
SELECT octet_length(t::text) as row_size FROM ({}) as t
@@ -1308,6 +1316,9 @@ def _get_table_chunk_size(cursor: psycopg.Cursor, inner_query: sql.Composed, log
cursor.execute(query)
row = cursor.fetchone()
+ if use_savepoint:
+ cursor.execute("RELEASE SAVEPOINT _chunk_size_probe")
+
if row is None:
logger.debug(f"_get_table_chunk_size: No results returned. Using DEFAULT_CHUNK_SIZE={DEFAULT_CHUNK_SIZE}")
return DEFAULT_CHUNK_SIZE
@@ -1325,10 +1336,15 @@ def _get_table_chunk_size(cursor: psycopg.Cursor, inner_query: sql.Composed, log
# 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.
+ # and the savepoint (when present) 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.
+ if use_savepoint:
+ try:
+ cursor.execute("ROLLBACK TO SAVEPOINT _chunk_size_probe")
+ except Exception:
+ pass
logger.debug(f"_get_table_chunk_size: Error: {e}. Using DEFAULT_CHUNK_SIZE={DEFAULT_CHUNK_SIZE}", exc_info=e)
return DEFAULT_CHUNK_SIZE