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): skip internal materialized-view columns in schema discovery (#65607)

Tom Owers2a7a83eb27e3
diff --git a/posthog/temporal/data_imports/sources/redshift/redshift.py b/posthog/temporal/data_imports/sources/redshift/redshift.py
index da16f0262aab..cb9d7d8a9c13 100644
--- a/posthog/temporal/data_imports/sources/redshift/redshift.py
+++ b/posthog/temporal/data_imports/sources/redshift/redshift.py
@@ -102,6 +102,13 @@ _REDSHIFT_CONNECT_OPTS: dict[str, Any] = {
# system catalogs plus `pg_automv` (auto-materialized views) and per-session `pg_temp_*` schemas.
SYSTEM_REDSHIFT_SCHEMAS = ["pg_catalog", "information_schema", "pg_internal", "pg_automv"]
+# Redshift stamps internal bookkeeping columns onto materialized views — e.g.
+# `padb_internal_txn_id_col` and `padb_internal_txn_seq_col`. They appear in
+# `information_schema.columns` but `SELECT *` never returns them, so leaving them in the
+# discovered schema makes the Arrow schema disagree with the streamed rows and
+# `pa.Table.from_pydict` raises a `KeyError`. The `padb_internal` prefix is Redshift-reserved.
+REDSHIFT_INTERNAL_COLUMN_LIKE = "padb_internal%"
+
def _display_name(schema_name: str, table_name: str, *, qualify: bool) -> str:
"""Discovery key for a table: dotted `schema.table` in multi-schema mode, bare table otherwise."""
@@ -387,8 +394,8 @@ class RedshiftImplementation(SQLSourceImplementation[RedshiftSourceConfig, psyco
qualify = selected_schema is None
with conn.cursor() as cursor:
- params: dict = {}
- where: list[str] = []
+ params: dict = {"internal_column": REDSHIFT_INTERNAL_COLUMN_LIKE}
+ where: list[str] = ["column_name NOT LIKE %(internal_column)s"]
if selected_schema is not None:
params["schema"] = selected_schema
where.append("table_schema = %(schema)s")
@@ -803,7 +810,12 @@ class RedshiftImplementation(SQLSourceImplementation[RedshiftSourceConfig, psyco
information_schema.columns
WHERE
table_schema = {schema}
- AND table_name = {table}""").format(schema=sql.Literal(schema), table=sql.Literal(table_name))
+ AND table_name = {table}
+ AND column_name NOT LIKE {internal_column}""").format(
+ schema=sql.Literal(schema),
+ table=sql.Literal(table_name),
+ internal_column=sql.Literal(REDSHIFT_INTERNAL_COLUMN_LIKE),
+ )
if logger is not None:
_explain_query(cursor, query, 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 32b7815a2283..02b4a286cff7 100644
--- a/posthog/temporal/data_imports/sources/redshift/tests/test_redshift.py
+++ b/posthog/temporal/data_imports/sources/redshift/tests/test_redshift.py
@@ -315,6 +315,19 @@ class TestGetTableMetadata:
assert table.columns[0].numeric_precision == 10
assert table.columns[0].numeric_scale == 2
+ def test_excludes_redshift_internal_columns_from_arrow_schema(self, impl, cursor):
+ # Materialized views expose `padb_internal_*` bookkeeping columns in
+ # `information_schema.columns`, but `SELECT *` never returns them. Leaving them in the
+ # Arrow schema made `pa.Table.from_pydict` raise `KeyError: 'padb_internal_txn_id_col'`.
+ cursor.execute.return_value = cursor
+ cursor.fetchone.return_value = (False,)
+ cursor.__iter__.return_value = iter([("id", "integer", "NO", None, None)])
+
+ impl.get_table_metadata(cursor, "public", "my_mat_view")
+
+ metadata_query = cursor.execute.call_args.args[0].as_string()
+ assert "column_name NOT LIKE 'padb_internal%'" in metadata_query
+
class TestGetRowsToSync:
def _inner(self):
@@ -552,6 +565,21 @@ class TestGetColumns:
assert impl.get_columns(conn, _make_config(), names=["foo"]) == {}
+ def test_excludes_redshift_internal_columns(self, impl):
+ # Discovery must drop the `padb_internal_*` columns Redshift stamps onto materialized
+ # views — they never come back from `SELECT *`, so surfacing them desyncs the schema.
+ conn = MagicMock()
+ cur = MagicMock()
+ cur.__enter__.return_value = cur
+ cur.fetchall.return_value = []
+ conn.cursor.return_value = cur
+
+ impl.get_columns(conn, _make_config(), names=None)
+
+ executed_sql, executed_params = cur.execute.call_args.args
+ assert "column_name NOT LIKE %(internal_column)s" in executed_sql
+ assert executed_params["internal_column"] == "padb_internal%"
+
def test_blank_schema_qualifies_and_excludes_system_schemas(self, impl):
conn = MagicMock()
cur = MagicMock()