: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(batch-exports): make missing Postgres connection inputs non-retryable (#74259)
Ross90a74bf37b58
diff --git a/products/batch_exports/backend/temporal/destinations/postgres_batch_export.py b/products/batch_exports/backend/temporal/destinations/postgres_batch_export.pyindex 3788a9f4adca..8dc0f9648bb4 100644--- a/products/batch_exports/backend/temporal/destinations/postgres_batch_export.py+++ b/products/batch_exports/backend/temporal/destinations/postgres_batch_export.py@@ -130,6 +130,9 @@ NON_RETRYABLE_ERROR_TYPES = (# Raised when a PostgreSQL stored procedure or function fails.# We don't (at the moment) create or run any ourselves, so it must be something set up by the user."SqlRoutineException",+ # The inputs are missing required connection details (e.g. credentials or host).+ # This usually means the backing integration is misconfigured or absent, so retrying won't help.+ "PostgreSQLMissingRequiredInputsError",)@@ -156,6 +159,16 @@ class PostgreSQLTransactionError(Exception):super().__init__(f"A transaction failed to complete after {max_attempts} attempts: {err_msg}")+class PostgreSQLMissingRequiredInputsError(Exception):+ """Raised when the export is missing required connection inputs (credentials or host/port).++ This usually means the backing integration is misconfigured or absent, so retrying won't recover it.+ """++ def __init__(self, err_msg: str):+ super().__init__(f"The export is missing required connection inputs: {err_msg}")++class _PostgreSQLClientInputsProtocol(typing.Protocol):def credentials(self) -> Credentials: ...@@ -933,7 +946,12 @@ async def insert_into_postgres_activity_from_stage(inputs: PostgresInsertInputs))[:63]client_inputs = await _get_postgresql_integration(inputs) or inputs- pg_client = PostgreSQLClient.from_inputs(client_inputs, database=inputs.database)+ try:+ pg_client = PostgreSQLClient.from_inputs(client_inputs, database=inputs.database)+ except ValueError as err:+ # Missing credentials/host usually means a misconfigured or absent integration, which retrying+ # can't recover. Surface it as a non-retryable error so the export fails fast instead of breaching SLA.+ raise PostgreSQLMissingRequiredInputsError(str(err)) from errasync with pg_client.connect() as pg_client:table_exists = Falsediff --git a/products/batch_exports/backend/tests/temporal/destinations/postgres/test_postgres_client.py b/products/batch_exports/backend/tests/temporal/destinations/postgres/test_postgres_client.pyindex 0217959f4a50..8cdfaf323ab6 100644--- a/products/batch_exports/backend/tests/temporal/destinations/postgres/test_postgres_client.py+++ b/products/batch_exports/backend/tests/temporal/destinations/postgres/test_postgres_client.py@@ -7,7 +7,10 @@ from psycopg.errors import SerializationFailurefrom posthog.models.integration import MISSING_CERT_PATH, PostgreSQLIntegrationfrom products.batch_exports.backend.temporal.destinations.postgres_batch_export import (+ NON_RETRYABLE_ERROR_TYPES,+ PostgresInsertInputs,PostgreSQLClient,+ PostgreSQLMissingRequiredInputsError,PostgreSQLTransactionError,remove_invalid_json,run_in_retryable_transaction,@@ -38,6 +41,30 @@ def test_remove_invalid_json(input_data, expected_data):assert remove_invalid_json(input_data) == expected_data[email protected]("missing_field", ["user", "password", "host", "port"])+def test_from_inputs_raises_value_error_when_connection_inputs_missing(missing_field):+ # The activity relies on `from_inputs` raising ValueError to convert missing inputs into a+ # non-retryable error, so it fails fast rather than retrying to an SLA breach.+ inputs = PostgresInsertInputs(+ team_id=1,+ data_interval_start=None,+ data_interval_end="2023-04-25T14:30:00+00:00",+ database="posthog",+ table_name="events",+ host="localhost",+ port=5432,+ user="posthog",+ password="posthog",+ )+ setattr(inputs, missing_field, None)+ with pytest.raises(ValueError):+ PostgreSQLClient.from_inputs(inputs, database=inputs.database)+++def test_missing_required_inputs_error_is_non_retryable():+ assert PostgreSQLMissingRequiredInputsError.__name__ in NON_RETRYABLE_ERROR_TYPES++async def test_run_in_retryable_transaction_raises_non_retryable_error_after_max_retries(postgres_config, setup_postgres_test_db):