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(postgres): treat pg_stat_statements not loaded as non-retryable (#119679)

Tom Owersafd6d7c83b6a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
index 0a9d6cdec8ed..5c9e58bcbb0f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
@@ -1406,6 +1406,20 @@ class PostgresSource(
'(PostgreSQL reported "has not been populated"). Run REFRESH MATERIALIZED VIEW on it in '
"your database so it contains data, then re-enable the sync."
),
+ # Also ObjectNotInPrerequisiteState (SQLSTATE 55000), raised while reading a selected
+ # view whose own definition queries the pg_stat_statements extension (e.g. a
+ # slow-queries monitoring view) when that extension's module isn't loaded via the
+ # server's `shared_preload_libraries` setting. Loading it needs a postgresql.conf edit
+ # and a server restart, which only the customer's database admin can do — deterministic
+ # and outside our control, so retrying just re-reads into the same error. The message is
+ # a fixed PostgreSQL string with no volatile parts to exclude.
+ "pg_stat_statements must be loaded via": (
+ "A view you're syncing queries the pg_stat_statements extension, which your "
+ "PostgreSQL server hasn't loaded (\"pg_stat_statements must be loaded via "
+ "'shared_preload_libraries'\"). Add it to shared_preload_libraries in postgresql.conf "
+ "and restart your database server, or remove that view from the sync, then re-enable "
+ "the sync."
+ ),
# Raised by Postgres while reading a view/materialized view whose own definition calls
# `jsonb_each()` (or `jsonb_each_text()`) on a jsonb value that isn't an object for some
# rows (a JSON array, scalar, or `'null'`). We only ever run `SELECT ... FROM <relation>`;
diff --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.py
index 293b4ad70fdf..51157b49cc06 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
@@ -1641,6 +1641,27 @@ class TestPostgresSourceNonRetryableErrors:
assert friendly, "Unpopulated materialized view error should surface an actionable message"
assert "REFRESH MATERIALIZED VIEW" in friendly[0]
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Raw psycopg message (what the activity-level check sees via str(e)).
+ 'pg_stat_statements must be loaded via "shared_preload_libraries"',
+ # Temporal-wrapped message (what the workflow-level check sees) — carries the class name.
+ 'ObjectNotInPrerequisiteState: pg_stat_statements must be loaded via "shared_preload_libraries"',
+ ],
+ )
+ def test_pg_stat_statements_not_loaded_errors_are_non_retryable(self, source, error_msg):
+ non_retryable = source.get_non_retryable_errors()
+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
+ assert is_non_retryable, f"pg_stat_statements not loaded error should be non-retryable: {error_msg}"
+
+ def test_pg_stat_statements_not_loaded_returns_friendly_message(self, source):
+ non_retryable = source.get_non_retryable_errors()
+ error_msg = 'pg_stat_statements must be loaded via "shared_preload_libraries"'
+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]
+ assert friendly, "pg_stat_statements not loaded error should surface an actionable message"
+ assert "shared_preload_libraries" in friendly[0]
+
@pytest.mark.parametrize(
"error_msg",
[