: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(managed-warehouse): right-size registration workers (#84697)
Eric Duongce6f7059bb9e
diff --git a/products/managed_warehouse/backend/storage.py b/products/managed_warehouse/backend/storage.pyindex d668efac6b57..55167ba50f8a 100644--- a/products/managed_warehouse/backend/storage.py+++ b/products/managed_warehouse/backend/storage.py@@ -776,12 +776,20 @@ def create_staging_read_secret(conn: psycopg.Connection, catalog_bucket: str) ->)-def connect_to_duckgres(server: DuckgresServer, *, application_name: str = "posthog") -> psycopg.Connection:+def connect_to_duckgres(+ server: DuckgresServer,+ *,+ application_name: str = "posthog",+ options: str | None = None,+) -> psycopg.Connection:"""Open a psycopg connection to a duckgres server.``application_name`` is a caller-identifying slug echoed into duckgres'sanalytics events, so callers should pass one instead of relying on the``"posthog"`` default.++ ``options`` is forwarded as the libpq startup options for caller-specific+ Duckgres settings."""return psycopg.connect(host=server.host,@@ -791,6 +799,7 @@ def connect_to_duckgres(server: DuckgresServer, *, application_name: str = "postpassword=server.password,autocommit=True,application_name=application_name,+ options=options,)diff --git a/products/managed_warehouse/backend/temporal/ducklake_register_data_imports_workflow.py b/products/managed_warehouse/backend/temporal/ducklake_register_data_imports_workflow.pyindex faf07a4b3ebf..e8a9ff7fc7f4 100644--- a/products/managed_warehouse/backend/temporal/ducklake_register_data_imports_workflow.py+++ b/products/managed_warehouse/backend/temporal/ducklake_register_data_imports_workflow.py@@ -76,6 +76,7 @@ _DUCKGRES_CANCEL_MARGIN = dt.timedelta(minutes=1)_DUCKGRES_CANCEL_MAX_ATTEMPTS = 10_DUCKGRES_CANCEL_RETRY_SECONDS = 0.5_DUCKGRES_CANCEL_TIMEOUT_SECONDS = 5.0+_DUCKGRES_REGISTER_WORKER_OPTIONS = "-c duckgres.worker_cpu=4 -c duckgres.worker_memory=16Gi"# Duckgres cancel fires one minute before this deadline. One attempt: a# StartToClose timeout has an unknown catalog outcome, so a retry could race# the original CALL.@@ -547,7 +548,7 @@ def _should_publish_prepared_generation(inputs: DuckLakeRegisterDataImportsActivdef _connect_to_duckgres_for_team(team_id: int) -> Iterator[psycopg.Connection]:if is_dev_mode():conninfo = make_duckgres_conninfo(team_id, application_name="ducklake-register")- with psycopg.connect(conninfo, autocommit=True) as conn:+ with psycopg.connect(conninfo, autocommit=True, options=_DUCKGRES_REGISTER_WORKER_OPTIONS) as conn:yield connreturn@@ -555,7 +556,11 @@ def _connect_to_duckgres_for_team(team_id: int) -> Iterator[psycopg.Connection]:server = get_duckgres_server_for_organization(organization_id)if server is None:raise ApplicationError(f"No DuckgresServer configured for team {team_id}", non_retryable=True)- with connect_to_duckgres(server, application_name="ducklake-register") as conn:+ with connect_to_duckgres(+ server,+ application_name="ducklake-register",+ options=_DUCKGRES_REGISTER_WORKER_OPTIONS,+ ) as conn:yield conndiff --git a/products/managed_warehouse/backend/tests/temporal/test_ducklake_register_data_imports_workflow.py b/products/managed_warehouse/backend/tests/temporal/test_ducklake_register_data_imports_workflow.pyindex 6bf626f65498..a68e28fdcb9a 100644--- a/products/managed_warehouse/backend/tests/temporal/test_ducklake_register_data_imports_workflow.py+++ b/products/managed_warehouse/backend/tests/temporal/test_ducklake_register_data_imports_workflow.py@@ -239,7 +239,11 @@ def test_copy_activity_uses_s3_copy_and_local_duckgres_postgres_connection(monkeapplied = copy_and_register_ducklake_data_imports_activity(inputs)assert applied is True- connect.assert_called_once_with("postgresql://duckgres", autocommit=True)+ connect.assert_called_once_with(+ "postgresql://duckgres",+ autocommit=True,+ options="-c duckgres.worker_cpu=4 -c duckgres.worker_memory=16Gi",+ )assert s3.copy_calls == [([@@ -314,6 +318,40 @@ def test_copy_activity_uses_s3_copy_and_local_duckgres_postgres_connection(monkeworkload_metrics.bytes.record.assert_called_once_with(300.0)+def test_production_register_connection_requests_right_sized_duckgres_worker(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ server = MagicMock()+ server.host = "duckgres.example.com"+ server.port = 5432+ server.database = "ducklake"+ server.username = "posthog"+ server.password = "example-password"+ monkeypatch.setattr(registration_module, "is_dev_mode", lambda: False)+ monkeypatch.setattr(registration_module, "_get_org_id_for_team", lambda _team_id: "org-id")+ monkeypatch.setattr(registration_module, "get_duckgres_server_for_organization", lambda _org_id: server)++ conn = MagicMock()+ connect = MagicMock()+ connect.return_value.__enter__.return_value = conn+ connect.return_value.__exit__.return_value = False+ monkeypatch.setattr(registration_module.psycopg, "connect", connect)++ with registration_module._connect_to_duckgres_for_team(1) as connected:+ assert connected is conn++ connect.assert_called_once_with(+ host="duckgres.example.com",+ port=5432,+ dbname="ducklake",+ user="posthog",+ password="example-password",+ autocommit=True,+ application_name="ducklake-register",+ options="-c duckgres.worker_cpu=4 -c duckgres.worker_memory=16Gi",+ )++def test_duckgres_cancel_watchdog_cancels_the_active_query(monkeypatch):conn = MagicMock()cancel_timer = MagicMock()