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.

feat(compression): Configure compression method based on team_id (#117900)

Tomás Farías Santana553aff35c196
diff --git a/posthog/settings/batch_exports.py b/posthog/settings/batch_exports.py
index 5a48cf329107..ed9ad4c9ea9f 100644
--- a/posthog/settings/batch_exports.py
+++ b/posthog/settings/batch_exports.py
@@ -121,6 +121,9 @@ BATCH_EXPORT_OBJECT_STORAGE_ENDPOINT: str = os.getenv(
)
BATCH_EXPORT_OBJECT_STORAGE_REGION: str = os.getenv("BATCH_EXPORT_OBJECT_STORAGE_REGION", "us-east-1")
BATCH_EXPORT_INTERNAL_STAGING_BUCKET: str = os.getenv("BATCH_EXPORT_INTERNAL_STAGING_BUCKET", "posthog")
+BATCH_EXPORT_ARROW_COMPRESSION_METHOD: str = get_from_env("BATCH_EXPORT_ARROW_COMPRESSION_METHOD", "zstd")
+BATCH_EXPORT_ARROW_COMPRESSION_TEAM_IDS: list[str] = get_list(os.getenv("BATCH_EXPORT_ARROW_COMPRESSION_TEAM_IDS", ""))
+
# The number of partitions controls how many files ClickHouse writes to concurrently. Used as the fallback
# when we have no size estimate to drive a dynamic partition count (e.g. the first ever run of an export).
BATCH_EXPORT_CLICKHOUSE_S3_PARTITIONS: int = get_from_env("BATCH_EXPORT_CLICKHOUSE_S3_PARTITIONS", 10, type_cast=int)
diff --git a/products/batch_exports/backend/temporal/pipeline/internal_stage.py b/products/batch_exports/backend/temporal/pipeline/internal_stage.py
index 0ea4526064a5..fb93ddfa9266 100644
--- a/products/batch_exports/backend/temporal/pipeline/internal_stage.py
+++ b/products/batch_exports/backend/temporal/pipeline/internal_stage.py
@@ -802,6 +802,9 @@ async def _write_batch_export_record_batches_to_internal_stage(
query = query_or_model
query_settings = {}
+ if str(team_id) in settings.BATCH_EXPORT_ARROW_COMPRESSION_TEAM_IDS:
+ query_settings["output_format_arrow_compression_method"] = settings.BATCH_EXPORT_ARROW_COMPRESSION_METHOD
+
base_s3_staging_folder = get_base_s3_staging_folder(
batch_export_id=batch_export_id,
data_interval_start=data_interval_start,
diff --git a/products/batch_exports/backend/tests/temporal/pipeline/test_internal_stage.py b/products/batch_exports/backend/tests/temporal/pipeline/test_internal_stage.py
index 277ba909f352..8661f13db59a 100644
--- a/products/batch_exports/backend/tests/temporal/pipeline/test_internal_stage.py
+++ b/products/batch_exports/backend/tests/temporal/pipeline/test_internal_stage.py
@@ -1606,6 +1606,7 @@ class TestHogQLModel:
BATCH_EXPORT_HOGQL_MAX_EXECUTION_TIME=900,
BATCH_EXPORT_HOGQL_MAX_MEMORY_USAGE=30_000_000_000,
BATCH_EXPORT_HOGQL_MAX_BYTES_TO_READ=200_000_000_000,
+ BATCH_EXPORT_ARROW_COMPRESSION_METHOD="zstd",
)
async def test_applies_settings_and_log_comment(
self,
@@ -1627,17 +1628,18 @@ class TestHogQLModel:
"""
batch_export_id = str(uuid.uuid4())
- await _run_activity(
- activity_environment=activity_environment,
- object_storage_client=object_storage_client,
- team_id=ateam.pk,
- data_interval_start=data_interval_start,
- data_interval_end=data_interval_end,
- model=BatchExportModel(
- name="hogql", schema=None, hogql_query="SELECT event AS event FROM events", user_id=auser.pk
- ),
- batch_export_id=batch_export_id,
- )
+ with override_settings(BATCH_EXPORT_ARROW_COMPRESSION_TEAM_IDS=[str(ateam.pk)]):
+ await _run_activity(
+ activity_environment=activity_environment,
+ object_storage_client=object_storage_client,
+ team_id=ateam.pk,
+ data_interval_start=data_interval_start,
+ data_interval_end=data_interval_end,
+ model=BatchExportModel(
+ name="hogql", schema=None, hogql_query="SELECT event AS event FROM events", user_id=auser.pk
+ ),
+ batch_export_id=batch_export_id,
+ )
# `Settings` in `system.query_log` records only settings changed from their default, so the
# overflow modes are absent here (`throw` is already ClickHouse's default, and we pin it only
@@ -1652,6 +1654,7 @@ class TestHogQLModel:
"max_bytes_before_external_sort",
"max_bytes_ratio_before_external_sort",
"max_bytes_to_read",
+ "output_format_arrow_compression_method",
],
)
- assert actual_settings == [["900", "30000000000", "15000000000", "0", "200000000000"]]
+ assert actual_settings == [["900", "30000000000", "15000000000", "0", "200000000000", "zstd"]]
diff --git a/products/batch_exports/backend/tests/test_internal_stage_storage_config.py b/products/batch_exports/backend/tests/test_internal_stage_storage_config.py
index 973776ef240e..6a68fbd17e3a 100644
--- a/products/batch_exports/backend/tests/test_internal_stage_storage_config.py
+++ b/products/batch_exports/backend/tests/test_internal_stage_storage_config.py
@@ -1,13 +1,18 @@
+import datetime as dt
+
import pytest
+from unittest.mock import AsyncMock, patch
from django.test.utils import override_settings
from posthog.credentials import AWSKeyPair
+from posthog.temporal.common.clickhouse import ClickHouseClient
from products.batch_exports.backend.temporal.errors import MissingRequiredInputsError
from products.batch_exports.backend.temporal.pipeline.internal_stage import (
_get_s3_credentials,
_get_s3_endpoint_url,
+ _write_batch_export_record_batches_to_internal_stage,
get_base_s3_staging_folder,
get_s3_staging_folder,
)
@@ -20,6 +25,51 @@ OBJECT_STORAGE_SECRET_ACCESS_KEY = "object-storage-secret"
EXPECTED_STAGE_FOLDER = "batch-exports/batch-export/2026-01-01-2026-01-02/attempt_1"
+ "team_ids,compression,expected_settings",
+ [
+ ([], "zstd", {}),
+ (["2"], "zstd", {}),
+ (["2", "1"], "zstd", {"output_format_arrow_compression_method": "zstd"}),
+ (["1"], "lz4_frame", {"output_format_arrow_compression_method": "lz4_frame"}),
+ (["1"], "none", {"output_format_arrow_compression_method": "none"}),
+ ],
+)
+async def test_staging_arrow_compression_is_scoped_to_allowed_teams(
+ team_ids: list[str], compression: str, expected_settings: dict[str, str]
+) -> None:
+ interval_end = dt.datetime.now(dt.UTC) - dt.timedelta(hours=1)
+ interval_start = interval_end - dt.timedelta(hours=1)
+ client = AsyncMock(spec=ClickHouseClient)
+ client.is_alive.return_value = True
+ client.execute_query_with_summary.return_value = {"written_rows": "1"}
+
+ with (
+ override_settings(
+ BATCH_EXPORT_ARROW_COMPRESSION_METHOD=compression,
+ BATCH_EXPORT_ARROW_COMPRESSION_TEAM_IDS=team_ids,
+ ),
+ patch("products.batch_exports.backend.temporal.pipeline.internal_stage.get_client") as get_client,
+ patch("products.batch_exports.backend.temporal.pipeline.internal_stage.get_s3_client") as get_s3_client,
+ ):
+ get_client.return_value.__aenter__.return_value = client
+ get_s3_client.return_value.__aenter__.return_value.list_objects_v2.return_value = {}
+ written_rows = await _write_batch_export_record_batches_to_internal_stage(
+ query_or_model="SELECT 1",
+ full_range=(interval_start, interval_end),
+ query_parameters={},
+ team_id=1,
+ batch_export_id="00000000-0000-4000-8000-000000000001",
+ data_interval_start=interval_start.isoformat(),
+ data_interval_end=interval_end.isoformat(),
+ s3_staging_folder_url="s3://bucket/folder",
+ )
+
+ assert written_rows == 1
+ assert client.execute_query_with_summary.call_args.kwargs["settings"] == expected_settings
+
+
def _get_test_s3_staging_folder_url() -> str:
return get_s3_staging_folder(
batch_export_id="batch-export",