: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
chore(analytics-platform): apply review nits to lazy computation docs (#62878)
Robbie2d759ecde505
diff --git a/products/analytics_platform/backend/lazy_computation/CONSISTENCY.md b/products/analytics_platform/backend/lazy_computation/CONSISTENCY.mdindex 17089e89cc66..7d54da8808b4 100644--- a/products/analytics_platform/backend/lazy_computation/CONSISTENCY.md+++ b/products/analytics_platform/backend/lazy_computation/CONSISTENCY.md@@ -31,7 +31,7 @@ Previously named `insert_distributed_sync`.- Dev's `docker/clickhouse/users.xml` sets `insert_distributed_sync=1` on the `default` profile, so dev and CI always behave synchronously — which also makes the async failure mode invisible in every test environment.- Production profiles are not the dev config. At least one production cluster explicitly sets `distributed_foreground_insert=0` (async) on the `default` profile used for writes, while carrying the legacy `insert_distributed_sync=1` only on the `readonly` profile — where it is inert, since readonly users cannot INSERT.-- An earlier revision of this document concluded the setting was "already enabled globally", citing the dev config and the `posthog/dags/sessions.py` override comment. Both observe intent, not production behavior, and the conclusion was wrong: with async forwarding, INSERTs returned after merely enqueueing to the initiator's distribution queue, jobs were marked READY immediately, and rows became visible to readers only over the following minutes — producing badly undercounted funnel results right after every window recompute.+- The dev config and the `posthog/dags/sessions.py` override comment both observe intent, not deployed behavior — do not read them as proof the setting is global. With async forwarding, an INSERT returns after merely enqueueing to the initiator's distribution queue, so a job marked READY immediately exposes rows that only become visible to readers over the following minutes — producing badly undercounted funnel results right after every window recompute.- To verify actual behavior, check `Settings['insert_distributed_sync']` on real INSERT rows in `system.query_log`, or per-host profile contents in `system.settings_profile_elements`. Unit tests on `_get_insert_settings` pin our side; only production telemetry pins the environment's.Large synchronous distributed inserts buffer per-shard splits on the initiator — the reason `posthog/dags/sessions.py` sets `0` for its bulk INSERTs. Lazy computation inserts are bounded by daily windows; if a very large recompute ever hits memory limits, chunk the windows instead of reverting to async.@@ -255,7 +255,7 @@ Sentry [rejected `select_sequential_consistency`](https://blog.sentry.io/how-to-**Pros**: zero ZK overhead on reads, no per-table serialization, per-query settings only**Cons**: not formally guaranteed — if the preferred replica goes down between INSERT and SELECT, the fallback replica may be stale (see quorum hardening below). Concentrates lazy computation read/write load on one replica per shard — with 3 replicas, other queries using `random` load balancing distribute evenly across all 3 (including replica 1), so replica 1 gets a disproportionate share of total load. Acceptable when lazy computation is a small fraction of total query volume, but worth monitoring as it grows.-**INSERT latency**: the synchronous forward to the target shard (previously assumed free via a global server setting)+**INSERT latency**: the synchronous forward to the target shard**SELECT latency**: none extra (may be slightly faster with shard pruning)**Optional hardening: add quorum writes.** Adding `insert_quorum='auto'` ensures the INSERT is acknowledged by a majority of replicas before returning. This covers the failover edge case: if replica 1 goes down between INSERT and SELECT, `in_order` falls back to replica 2, which has the data thanks to quorum. Importantly, since we're using `load_balancing='in_order'` instead of `select_sequential_consistency`, we do NOT need `insert_quorum_parallel=0` — parallel quorum (the default) works fine, so there's no per-table lock and no throughput limit. The cost is +20-100ms INSERT latency for the quorum wait.diff --git a/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py b/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.pyindex 7108cb431c8b..3079c1feb884 100644--- a/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py+++ b/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py@@ -141,9 +141,10 @@ def _get_insert_settings(team_id: int) -> dict:"insert_quorum": PREAGGREGATION_INSERT_QUORUM,# The executor marks a job READY as soon as the INSERT returns, so rows must be on the# shards by then — not sitting in the initiator's async distribution queue, where they- # become visible to readers only minutes later. Production profiles default this to 0- # (async); never rely on server config for this guarantee. Uses the legacy name of- # `distributed_foreground_insert` (renamed in ClickHouse 23.x) for version compatibility.+ # become visible to readers only minutes later. We set this per-insert rather than+ # relying on the cluster's global default, which is not guaranteed to be synchronous.+ # Uses the legacy name of `distributed_foreground_insert` (renamed in ClickHouse 23.x)+ # for version compatibility."insert_distributed_sync": 1,**HogQLQuerySettings(load_balancing="in_order").model_dump(exclude_none=True),}diff --git a/products/web_analytics/backend/hogql_queries/web_overview_lazy_precompute.py b/products/web_analytics/backend/hogql_queries/web_overview_lazy_precompute.pyindex ac3a4a38d9e8..f9d58f29f7e4 100644--- a/products/web_analytics/backend/hogql_queries/web_overview_lazy_precompute.py+++ b/products/web_analytics/backend/hogql_queries/web_overview_lazy_precompute.py@@ -149,8 +149,8 @@ _READ_SETTINGS = {# Approach E from `products/analytics_platform/backend/lazy_computation/CONSISTENCY.md`:# both INSERT (via `_get_insert_settings`) and SELECT use `in_order` so they# deterministically prefer the same replica. Combined with the synchronous distributed- # insert (`insert_distributed_sync=1`, also set in `_get_insert_settings` — production- # profiles default to async), the SELECT sees data the INSERT just wrote.+ # insert (`insert_distributed_sync=1`, also set per-insert in `_get_insert_settings` so we+ # don't depend on the cluster's global default), the SELECT sees data the INSERT just wrote.## `select_sequential_consistency=1` was tried here and is documented broken in# CONSISTENCY.md when combined with `insert_quorum_parallel=1` (the default).