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.

fix(batch-exports): Cap insert block size writing to internal stage (#64074)

Tomás Farías Santana733865ebb203
diff --git a/posthog/settings/batch_exports.py b/posthog/settings/batch_exports.py
index c02893d423ee..a656e22c8c5d 100644
--- a/posthog/settings/batch_exports.py
+++ b/posthog/settings/batch_exports.py
@@ -77,6 +77,10 @@ OVERRIDE_TIMESTAMP_TEAM_IDS: dict[int, int] = dict(
)
CLICKHOUSE_OFFLINE_5MIN_CLUSTER_HOST: str | None = os.getenv("CLICKHOUSE_OFFLINE_5MIN_CLUSTER_HOST", None)
+# Used in internal stage to cap Arrow record batch size
+BATCH_EXPORTS_CLICKHOUSE_MAX_INSERT_BLOCK_SIZE_BYTES: int = get_from_env(
+ "BATCH_EXPORTS_CLICKHOUSE_MAX_INSERT_BLOCK_SIZE_BYTES", 64 * 1024 * 1024, type_cast=int
+)
BATCH_EXPORT_OBJECT_STORAGE_ENDPOINT: str = os.getenv(
"BATCH_EXPORT_OBJECT_STORAGE_ENDPOINT", "http://objectstorage:19000"
diff --git a/products/batch_exports/backend/temporal/pipeline/internal_stage.py b/products/batch_exports/backend/temporal/pipeline/internal_stage.py
index bdefb0c58bf1..0280ee765a18 100644
--- a/products/batch_exports/backend/temporal/pipeline/internal_stage.py
+++ b/products/batch_exports/backend/temporal/pipeline/internal_stage.py
@@ -567,7 +567,19 @@ async def _write_batch_export_record_batches_to_internal_stage(
await wait_for_delta_past_data_interval_end(end_at, delta)
done_ranges: list[tuple[dt.datetime, dt.datetime]] = []
- async with get_client(team_id=team_id, clickhouse_url=clickhouse_url) as client:
+ async with get_client(
+ team_id=team_id,
+ clickhouse_url=clickhouse_url,
+ # TODO: Strict limits are available in ClickHouse 26.4
+ # We should uncomment all of these here to let max_insert_block_size_bytes dictate the size
+ # once clickhouse is upgraded.
+ # use_strict_insert_block_limits=1,
+ # max_insert_block_size_rows=0,
+ # max_insert_block_size_bytes=settings.BATCH_EXPORTS_CLICKHOUSE_MAX_INSERT_BLOCK_SIZE_BYTES,
+ min_insert_block_size_bytes=settings.BATCH_EXPORTS_CLICKHOUSE_MAX_INSERT_BLOCK_SIZE_BYTES,
+ # Disable all of these so only the bytes limits counts.
+ min_insert_block_size_rows=0,
+ ) as client:
if not await client.is_alive():
raise ConnectionError("Cannot establish connection to ClickHouse")