: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): degrade gracefully when a source lacks pg_is_in_recovery (#76056)
Tom Owersac62e17ff00f
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.pyindex c1b8fa991d88..349e133bc534 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py@@ -2587,7 +2587,18 @@ class PostgreSQLColumn(Column):def _is_read_replica(cursor: psycopg.Cursor) -> bool:- cursor.execute("SELECT pg_is_in_recovery()")+ try:+ cursor.execute("SELECT pg_is_in_recovery()")+ except Exception as e:+ # Postgres-wire-compatible engines (e.g. DuckDB-backed proxies) accept the connection but+ # don't implement `pg_is_in_recovery` — a Postgres-only replication concept. Such an engine+ # is never a physical hot-standby, so degrade to "not a replica" like the other best-effort+ # probes on this connection (mirrors `_is_unsupported_function_error` callers). The setup+ # connection runs autocommit, so this failed statement can't poison later probes on it.+ if not _is_unsupported_function_error(e, "pg_is_in_recovery"):+ raise+ return False+row = cursor.fetchone()if row is None:return Falsediff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.pyindex 739bd6a17f18..b23e7ff5fc3b 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py@@ -5746,6 +5746,27 @@ class TestIsReadReplica:result = _is_read_replica(cast(Any, dj_cursor))assert result is False+ def test_unsupported_function_error_returns_false(self):+ # A DuckDB/Flight-SQL-backed Postgres-wire engine accepts the connection but doesn't+ # implement `pg_is_in_recovery` (a Postgres-only replication concept), surfacing a generic+ # InternalError instead of UndefinedFunction. Such an engine is never a physical hot-standby,+ # so this must degrade to False rather than crashing table setup.+ cursor = MagicMock()+ cursor.execute.side_effect = psycopg.errors.InternalError(+ "Catalog Error: Scalar Function with name pg_is_in_recovery does not exist!"+ )+ assert _is_read_replica(cast(Any, cursor)) is False++ def test_reraises_other_errors(self):+ # A failure unrelated to the missing-function shape (e.g. a connection drop) must propagate+ # so the caller's retry/reconnect handling still runs, instead of being swallowed here.+ cursor = MagicMock()+ cursor.execute.side_effect = psycopg.errors.InternalError_(+ "(EDBHANDLEREXITED) DbHandler exited. Check logs for more information"+ )+ with pytest.raises(psycopg.errors.InternalError_):+ _is_read_replica(cast(Any, cursor))+class _RecordingCursor:"""Wraps a real cursor, recording executed SQL as text while delegating everything else."""