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(redshift): isolate schema-discovery probes with autocommit (#63765)

Tom Owers33a1b7098a46
diff --git a/posthog/temporal/data_imports/sources/redshift/redshift.py b/posthog/temporal/data_imports/sources/redshift/redshift.py
index 35616c2bb75f..f42319843c4e 100644
--- a/posthog/temporal/data_imports/sources/redshift/redshift.py
+++ b/posthog/temporal/data_imports/sources/redshift/redshift.py
@@ -952,6 +952,11 @@ class RedshiftImplementation(SQLSourceImplementation[RedshiftSourceConfig, psyco
enabled_columns = inputs.enabled_columns
with self.connect(config) as connection:
+ # Autocommit so each best-effort discovery probe runs in its own transaction. A probe
+ # that fails — a permission error, an EXPLAIN the cluster rejects, a cancelled COUNT(*) —
+ # otherwise leaves the shared transaction aborted (INERROR), and every probe after it
+ # raises `InFailedSqlTransaction` until a rollback. Mirrors the postgres source.
+ connection.autocommit = True
with connection.cursor() as cursor:
logger.debug("Getting table types...")
full_table = self.get_table_metadata(cursor, schema, table_name, logger)
diff --git a/posthog/temporal/data_imports/sources/redshift/tests/test_redshift.py b/posthog/temporal/data_imports/sources/redshift/tests/test_redshift.py
index 39edcaa0ffef..971257f2564a 100644
--- a/posthog/temporal/data_imports/sources/redshift/tests/test_redshift.py
+++ b/posthog/temporal/data_imports/sources/redshift/tests/test_redshift.py
@@ -694,20 +694,26 @@ def build_pipeline_mocks(mocker):
# methods, so a single cursor mock can serve both connections —
# only the streaming connection requires `conn.adapters` to be set.
state = {"first_conn": True}
+ created_conns: list = []
def connect_side_effect(*args, **kwargs):
conn = MagicMock()
conn.__enter__.return_value = conn
conn.cursor.return_value = streaming_cursor
+ # psycopg requires autocommit be set before a transaction starts; default the mock to
+ # False so a test can assert build_pipeline flips it on the metadata connection.
+ conn.autocommit = False
if not state["first_conn"]:
conn.adapters = MagicMock()
state["first_conn"] = False
+ created_conns.append(conn)
return conn
mock_connect = mocker.patch(
"posthog.temporal.data_imports.sources.redshift.redshift.psycopg.connect",
side_effect=connect_side_effect,
)
+ mock_connect.created_conns = created_conns
return mock_connect, streaming_cursor
@@ -721,6 +727,17 @@ class TestBuildPipeline:
# psycopg.connect was called at least once for the metadata pass
assert mock_connect.called
+ def test_metadata_connection_uses_autocommit(self, build_pipeline_mocks):
+ # Regression: discovery probes share one connection. Without autocommit a single failing
+ # best-effort probe leaves the transaction aborted (INERROR) and every probe after it —
+ # `has_duplicate_primary_keys` was the reported one — raises `InFailedSqlTransaction`.
+ mock_connect, _ = build_pipeline_mocks
+ impl = RedshiftImplementation()
+ impl.build_pipeline(_make_config(), _make_inputs())
+
+ metadata_conn = mock_connect.created_conns[0]
+ assert metadata_conn.autocommit is True
+
def test_streaming_drains_without_error(self, build_pipeline_mocks):
_, streaming_cursor = build_pipeline_mocks
impl = RedshiftImplementation()