: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): restore parallel DuckLake registration (#82785)
Eric Duonga9703cf7bf68
diff --git a/products/managed_warehouse/backend/README.md b/products/managed_warehouse/backend/README.mdindex d31e684ae7b8..a734c5fcad15 100644--- a/products/managed_warehouse/backend/README.md+++ b/products/managed_warehouse/backend/README.md@@ -86,7 +86,7 @@ Every copy is written to a deterministic schema inside DuckLake. Each workflow nDuckgres stores a table-naming version on the organization. Organizations that existed when versioning was introduced keep the batch sink's snake-case format, such as `tik_tok_ads_ad_report`. New organizations use the copy workflow format, such as `tiktokads_ad_report`. Copy, registration, the batch sink, and query binding derive the same physical name from that organization-level policy. Do not change the policy after an organization has written data unless the underlying tables are migrated at the same time.-Each completed import creates a timestamped prepared Parquet snapshot in the data warehouse bucket. The registration workflow copies those objects directly into the DuckLake bucket, preserving Hive partition directories, registers the destination objects with `ducklake_add_data_files`, verifies the shadow table's row count, and only then swaps it into the stable table name through the Duckgres PostgreSQL connection. Each file registration is autocommitted so one statement cannot accumulate metadata for the entire generation. Publication uses one short transaction that renames the current table to an attempt-owned backup and the verified shadow to the stable name. A mismatch never enters the publication transaction, so the previous table remains live. The backup is dropped after publication. Each prepared generation gets its own object prefix and child workflow ID.+Each completed import creates a timestamped prepared Parquet snapshot in the data warehouse bucket. The registration workflow copies those objects directly into the DuckLake bucket, preserving Hive partition directories, and registers the generation through one recursive Parquet glob. This gives DuckDB one parallel metadata scan and one generation-sized DuckLake commit. The workflow verifies the shadow table's row count, then swaps it into the stable table name through the Duckgres PostgreSQL connection. Publication uses one short transaction that renames the current table to an attempt-owned backup and the verified shadow to the stable name. A mismatch never enters the publication transaction, so the previous table remains live. The backup is dropped after publication. Each prepared generation gets its own object prefix and child workflow ID.The registered objects are permanent DuckLake data files, not staging files. Old generations remain reachable through DuckLake snapshots until snapshot expiration and old-file cleanup make them eligible for object deletion. Choose the bucket lifecycle policy with that retention behavior in mind.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 d2b959eeb395..54b334bf7596 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@@ -643,20 +643,18 @@ def _register_prepared_parquet_files(psql.SQL(", ").join(psql.Identifier(column) for column in partition_columns),))- # DuckLake flushes file and column stats at statement commit, so one call per file bounds each batch.- for landing_path in landing_paths:- _raise_if_duckgres_cancel_requested(cancel_requested)- conn.execute(- psql.SQL(- "CALL ducklake_add_data_files({}, {}, {}, schema => {}, "- "allow_missing => true, hive_partitioning => true)"- ).format(- psql.Literal("ducklake"),- psql.Literal(registration_names.shadow_name),- psql.Literal(landing_path),- psql.Literal(schema_name),- )+ _raise_if_duckgres_cancel_requested(cancel_requested)+ conn.execute(+ psql.SQL(+ "CALL ducklake_add_data_files({}, {}, {}, schema => {}, "+ "allow_missing => true, hive_partitioning => true)"+ ).format(+ psql.Literal("ducklake"),+ psql.Literal(registration_names.shadow_name),+ parquet_glob,+ psql.Literal(schema_name),)+ )with _stage_timer(stage="verify", team_id=inputs.team_id, schema_id=inputs.metadata.source_schema_id):_raise_if_duckgres_cancel_requested(cancel_requested)diff --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 d08ff465bc75..2c81316f2238 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@@ -263,7 +263,8 @@ def test_copy_activity_uses_s3_copy_and_local_duckgres_postgres_connection(monkeprevious_cleanup_index = next(index for index, query in enumerate(executed) if "DROP TABLE IF EXISTS" in query and "__ph_previous_" in query)- assert len(registration_indexes) == 2+ assert len(registration_indexes) == 1+ registration_query = executed[registration_indexes[0]]first_path = ("s3://ducklake/posthog_data_imports_team_1/postgres_customers/_imports/schema/job/""1234567890_abcdef12/_ph_partition_key=2026-07/a.parquet"@@ -272,18 +273,14 @@ def test_copy_activity_uses_s3_copy_and_local_duckgres_postgres_connection(monke"s3://ducklake/posthog_data_imports_team_1/postgres_customers/_imports/schema/job/""1234567890_abcdef12/_ph_partition_key=2026-08/b.parquet")- assert first_path in executed[registration_indexes[0]]- assert second_path not in executed[registration_indexes[0]]- assert second_path in executed[registration_indexes[1]]- assert first_path not in executed[registration_indexes[1]]- assert sum(first_path in query for query in executed) == 1- assert sum(second_path in query for query in executed) == 1parquet_glob = ("s3://ducklake/posthog_data_imports_team_1/postgres_customers/_imports/schema/job/""1234567890_abcdef12/**/*.[pP][aA][rR][qQ][uU][eE][tT]")- assert sum(parquet_glob in query for query in executed) == 2- assert all(parquet_glob not in executed[index] for index in registration_indexes)+ assert parquet_glob in registration_query+ assert sum(parquet_glob in query for query in executed) == 3+ assert not any(first_path in query for query in executed)+ assert not any(second_path in query for query in executed)assert len(verification_indexes) == 2assert len(rename_indexes) == 2assert max(registration_indexes) < min(verification_indexes)@@ -414,7 +411,7 @@ def test_duckgres_cancel_watchdog_rejects_an_exhausted_activity_budget(monkeypattimer_factory.assert_not_called()-def test_registration_stops_between_files_after_query_cancellation(monkeypatch):+def test_registration_stops_after_glob_query_cancellation(monkeypatch):monkeypatch.setattr(registration_module, "setup_duckgres_session", MagicMock())conn = MagicMock()cancel_requested = registration_module.threading.Event()@@ -431,16 +428,20 @@ def test_registration_stops_between_files_after_query_cancellation(monkeypatch):prepared_queryable_folder=_activity_inputs().metadata.prepared_queryable_folder,)+ landing_paths = [f"{landing_uri}/{name}.parquet" for name in ("first", "second")]+with pytest.raises(TimeoutError, match="activity deadline"):registration_module._register_prepared_parquet_files(_activity_inputs(),conn,- [f"{landing_uri}/first.parquet", f"{landing_uri}/second.parquet"],+ landing_paths,cancel_requested=cancel_requested,)executed = [str(call.args[0]) for call in conn.execute.call_args_list]- assert sum("ducklake_add_data_files" in query for query in executed) == 1+ registration_queries = [query for query in executed if "ducklake_add_data_files" in query]+ assert len(registration_queries) == 1+ assert f"{landing_uri}/{registration_module._PARQUET_FILE_GLOB}" in registration_queries[0]assert not any("SELECT count(*)" in query for query in executed)assert sum("DROP TABLE" in query and "__ph_register_" in query for query in executed) == 1assert not any("RENAME TO" in query for query in executed)