: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(data-warehouse): make delta S3 commit safety explicit, gate unsafe rename behind a setting (#69909)
estefaniaed7d3586dcf1
diff --git a/posthog/settings/data_warehouse.py b/posthog/settings/data_warehouse.pyindex 293a6735f880..5cbdfc22ee76 100644--- a/posthog/settings/data_warehouse.py+++ b/posthog/settings/data_warehouse.py@@ -19,6 +19,12 @@ USE_LOCAL_SETUP = TEST or (DEBUG and len(os.getenv("OBJECT_STORAGE_ENDPOINT", "hPYARROW_DEBUG_LOGGING = get_from_env("PYARROW_DEBUG_LOGGING", False, type_cast=str_to_bool)+# Rollback-only escape hatch: restores the legacy delta-rs unsafe-rename S3 backend,+# which has no commit-conflict detection. Default (false) keeps conditional-put commits.+DATA_WAREHOUSE_DELTA_S3_ALLOW_UNSAFE_RENAME = get_from_env(+ "DATA_WAREHOUSE_DELTA_S3_ALLOW_UNSAFE_RENAME", False, type_cast=str_to_bool+)+# At-rest (compressed) byte budget per Delta partition. The auto-repartition controller rewrites a# table into a finer scheme once its largest partition exceeds this. delta-rs merges decompress the# whole target partition into an Arrow working set — roughly ~20x the at-rest size, and far more fordiff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/delta_table_helper.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/delta_table_helper.pyindex 555df882487a..659bd3af8595 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/delta_table_helper.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/delta_table_helper.py@@ -178,19 +178,23 @@ class DeltaTableHelper:settings.OBJECT_STORAGE_ENDPOINT,)- return {+ options = {"aws_access_key_id": settings.DATAWAREHOUSE_LOCAL_ACCESS_KEY,"aws_secret_access_key": settings.DATAWAREHOUSE_LOCAL_ACCESS_SECRET,"endpoint_url": settings.OBJECT_STORAGE_ENDPOINT,"region_name": settings.DATAWAREHOUSE_LOCAL_BUCKET_REGION,"AWS_DEFAULT_REGION": settings.DATAWAREHOUSE_LOCAL_BUCKET_REGION,"AWS_ALLOW_HTTP": "true",- "AWS_S3_ALLOW_UNSAFE_RENAME": "true",}-- return {- "AWS_S3_ALLOW_UNSAFE_RENAME": "true",- }+ else:+ options = {}++ # Conditional puts make a clashing concurrent commit fail loudly instead of+ # clobbering _delta_log; set explicitly so a library default change can't undo it.+ options["conditional_put"] = "etag"+ if settings.DATA_WAREHOUSE_DELTA_S3_ALLOW_UNSAFE_RENAME:+ options["AWS_S3_ALLOW_UNSAFE_RENAME"] = "true"+ return optionsasync def _get_delta_table_uri(self) -> str:normalized_resource_name = NamingConvention.normalize_identifier(self._resource_name)diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_delta_table_helper.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_delta_table_helper.pyindex b49cb0286b71..1fc691e0fdce 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_delta_table_helper.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_delta_table_helper.py@@ -7,6 +7,8 @@ from typing import Any, castimport pytestfrom unittest.mock import AsyncMock, MagicMock, patch+from django.test import override_settings+import pyarrow as paimport deltalakeimport pyarrow.compute as pc@@ -132,6 +134,36 @@ _COMMIT_LAYOUT_CASES: list[tuple[str, list[dict], dict, bool]] = []+class TestStorageOptionsCommitSafety:+ # Re-adding AWS_S3_ALLOW_UNSAFE_RENAME unconditionally would silently restore+ # the legacy rename backend, which has no commit-conflict detection.+ @parameterized.expand(+ [+ ("production_default_safe", False, False),+ ("production_rollback_escape_hatch", False, True),+ ("local_default_safe", True, False),+ ]+ )+ def test_conditional_put_on_unsafe_rename_gated(+ self, _case: str, use_local_setup: bool, allow_unsafe: bool+ ) -> None:+ helper = DeltaTableHelper(resource_name="t", job=MagicMock(), logger=_make_logger())++ with (+ override_settings(+ USE_LOCAL_SETUP=use_local_setup,+ DATA_WAREHOUSE_DELTA_S3_ALLOW_UNSAFE_RENAME=allow_unsafe,+ ),+ patch(+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline.delta_table_helper.ensure_bucket_exists"+ ),+ ):+ options = helper.get_storage_options()++ assert options["conditional_put"] == "etag"+ assert ("AWS_S3_ALLOW_UNSAFE_RENAME" in options) is allow_unsafe++class TestHasCommitWithMetadata:@pytest.mark.asyncioasync def test_returns_false_when_no_delta_table(self, helper: DeltaTableHelper):