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(web-analytics): alias action_id in goals precompute insert (#62242)

Lucas Ricoy7ccba3df2fb7
diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.py
index f06c3591147e..cacf35d13519 100644
--- a/posthog/clickhouse/schema.py
+++ b/posthog/clickhouse/schema.py
@@ -57,6 +57,10 @@ from posthog.clickhouse.preaggregation.web_bounces_dimensional_preaggregated_sql
DISTRIBUTED_WEB_BOUNCES_DIMENSIONAL_PREAGGREGATED_TABLE_SQL,
SHARDED_WEB_BOUNCES_DIMENSIONAL_PREAGGREGATED_TABLE_SQL,
)
+from posthog.clickhouse.preaggregation.web_goals_preaggregated_sql import (
+ DISTRIBUTED_WEB_GOALS_PREAGGREGATED_TABLE_SQL,
+ SHARDED_WEB_GOALS_PREAGGREGATED_TABLE_SQL,
+)
from posthog.clickhouse.preaggregation.web_overview_preaggregated_sql import (
DISTRIBUTED_WEB_OVERVIEW_PREAGGREGATED_TABLE_SQL,
SHARDED_WEB_OVERVIEW_PREAGGREGATED_TABLE_SQL,
@@ -328,6 +332,7 @@ CREATE_MERGETREE_TABLE_QUERIES = (
SHARDED_EXPERIMENT_METRIC_EVENTS_TABLE_SQL,
SHARDED_CONVERSION_GOAL_ATTRIBUTED_TABLE_SQL,
SHARDED_MARKETING_TOUCHPOINTS_TABLE_SQL,
+ SHARDED_WEB_GOALS_PREAGGREGATED_TABLE_SQL,
SHARDED_WEB_OVERVIEW_PREAGGREGATED_TABLE_SQL,
SHARDED_WEB_STATS_PREAGGREGATED_TABLE_SQL,
SHARDED_WEB_VITALS_PATHS_PREAGGREGATED_TABLE_SQL,
@@ -391,6 +396,7 @@ CREATE_DISTRIBUTED_TABLE_QUERIES = (
DISTRIBUTED_EXPERIMENT_METRIC_EVENTS_TABLE_SQL,
DISTRIBUTED_CONVERSION_GOAL_ATTRIBUTED_TABLE_SQL,
DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE_SQL,
+ DISTRIBUTED_WEB_GOALS_PREAGGREGATED_TABLE_SQL,
DISTRIBUTED_WEB_OVERVIEW_PREAGGREGATED_TABLE_SQL,
DISTRIBUTED_WEB_STATS_PREAGGREGATED_TABLE_SQL,
DISTRIBUTED_WEB_VITALS_PATHS_PREAGGREGATED_TABLE_SQL,
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 988efed78289..a4add77c2f57 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -6351,6 +6351,41 @@
'''
# ---
+# name: test_create_table_query[sharded_web_goals_preaggregated]
+ '''
+
+ CREATE TABLE IF NOT EXISTS sharded_web_goals_preaggregated
+ (
+ team_id Int64,
+ job_id UUID,
+
+ -- Hourly UTC bucket. Reads filter by [window_start_utc, window_end_utc).
+ time_window_start DateTime64(6, 'UTC'),
+
+ -- Django Action.id of the action this row aggregates. The action expression
+ -- itself is in the INSERT AST (cache-key shaped), not stored here.
+ action_id Int64,
+
+ -- Total match count (sum across sessions in the hour) and unique converting
+ -- persons (HLL-estimated). The runner reads top-5 actions only; this table
+ -- stores exactly that set per the precompute scope.
+ count_state AggregateFunction(sum, Int64),
+ unique_persons_state AggregateFunction(uniq, UUID),
+
+ -- ReplacingMergeTree version column: latest INSERT wins on duplicate ORDER BY keys.
+ computed_at DateTime64(6, 'UTC') DEFAULT now(),
+
+ -- Sub-day precision so the framework can attach TTLs like 15 min for "today".
+ expires_at DateTime64(6, 'UTC') DEFAULT now() + INTERVAL 7 DAY
+ ) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_{shard}/posthog.web_goals_preaggregated', '{replica}', computed_at)
+
+ PARTITION BY toYYYYMMDD(expires_at)
+ ORDER BY (team_id, job_id, action_id, time_window_start)
+ TTL toDateTime(expires_at)
+ SETTINGS index_granularity=8192, ttl_only_drop_parts = 1
+
+ '''
+# ---
# name: test_create_table_query[sharded_web_overview_preaggregated]
'''
@@ -6649,6 +6684,36 @@
'''
# ---
+# name: test_create_table_query[web_goals_preaggregated]
+ '''
+
+ CREATE TABLE IF NOT EXISTS web_goals_preaggregated
+ (
+ team_id Int64,
+ job_id UUID,
+
+ -- Hourly UTC bucket. Reads filter by [window_start_utc, window_end_utc).
+ time_window_start DateTime64(6, 'UTC'),
+
+ -- Django Action.id of the action this row aggregates. The action expression
+ -- itself is in the INSERT AST (cache-key shaped), not stored here.
+ action_id Int64,
+
+ -- Total match count (sum across sessions in the hour) and unique converting
+ -- persons (HLL-estimated). The runner reads top-5 actions only; this table
+ -- stores exactly that set per the precompute scope.
+ count_state AggregateFunction(sum, Int64),
+ unique_persons_state AggregateFunction(uniq, UUID),
+
+ -- ReplacingMergeTree version column: latest INSERT wins on duplicate ORDER BY keys.
+ computed_at DateTime64(6, 'UTC') DEFAULT now(),
+
+ -- Sub-day precision so the framework can attach TTLs like 15 min for "today".
+ expires_at DateTime64(6, 'UTC') DEFAULT now() + INTERVAL 7 DAY
+ ) ENGINE = Distributed('aux', 'posthog_test', 'sharded_web_goals_preaggregated', sipHash64(job_id))
+
+ '''
+# ---
# name: test_create_table_query[web_overview_preaggregated]
'''
@@ -10142,6 +10207,41 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[sharded_web_goals_preaggregated]
+ '''
+
+ CREATE TABLE IF NOT EXISTS sharded_web_goals_preaggregated
+ (
+ team_id Int64,
+ job_id UUID,
+
+ -- Hourly UTC bucket. Reads filter by [window_start_utc, window_end_utc).
+ time_window_start DateTime64(6, 'UTC'),
+
+ -- Django Action.id of the action this row aggregates. The action expression
+ -- itself is in the INSERT AST (cache-key shaped), not stored here.
+ action_id Int64,
+
+ -- Total match count (sum across sessions in the hour) and unique converting
+ -- persons (HLL-estimated). The runner reads top-5 actions only; this table
+ -- stores exactly that set per the precompute scope.
+ count_state AggregateFunction(sum, Int64),
+ unique_persons_state AggregateFunction(uniq, UUID),
+
+ -- ReplacingMergeTree version column: latest INSERT wins on duplicate ORDER BY keys.
+ computed_at DateTime64(6, 'UTC') DEFAULT now(),
+
+ -- Sub-day precision so the framework can attach TTLs like 15 min for "today".
+ expires_at DateTime64(6, 'UTC') DEFAULT now() + INTERVAL 7 DAY
+ ) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_{shard}/posthog.web_goals_preaggregated', '{replica}', computed_at)
+
+ PARTITION BY toYYYYMMDD(expires_at)
+ ORDER BY (team_id, job_id, action_id, time_window_start)
+ TTL toDateTime(expires_at)
+ SETTINGS index_granularity=8192, ttl_only_drop_parts = 1
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[sharded_web_overview_preaggregated]
'''
diff --git a/products/web_analytics/backend/hogql_queries/test/test_web_goals_lazy_precompute.py b/products/web_analytics/backend/hogql_queries/test/test_web_goals_lazy_precompute.py
index 14bc430bd2ce..888e517288be 100644
--- a/products/web_analytics/backend/hogql_queries/test/test_web_goals_lazy_precompute.py
+++ b/products/web_analytics/backend/hogql_queries/test/test_web_goals_lazy_precompute.py
@@ -204,6 +204,13 @@ class TestWebGoalsLazyPrecompute(ClickhouseTestMixin, APIBaseTest):
jobs = list(PreaggregationJob.objects.filter(team_id=self.team.pk))
assert len(jobs) > 0, "expected at least one precompute job to be created"
+ # A failed INSERT still leaves a job row (status FAILED), so existence
+ # alone doesn't prove the insert ran — assert the jobs reached READY.
+ # This catches insert-build errors (e.g. an unaliased SELECT column)
+ # without depending on the skipped read-after-write round trip.
+ assert all(j.status == PreaggregationJob.Status.READY for j in jobs), (
+ f"expected all precompute jobs READY, got {[(str(j.id), j.status, j.error) for j in jobs]}"
+ )
@unittest.skip(
"Mirrors the CI-only flake in test_web_stats_paths_lazy_precompute.py — "
diff --git a/products/web_analytics/backend/hogql_queries/web_goals_lazy_precompute.py b/products/web_analytics/backend/hogql_queries/web_goals_lazy_precompute.py
index 2ddbeec19dec..e7000bc8c9d4 100644
--- a/products/web_analytics/backend/hogql_queries/web_goals_lazy_precompute.py
+++ b/products/web_analytics/backend/hogql_queries/web_goals_lazy_precompute.py
@@ -237,7 +237,7 @@ def _build_insert_query(actions: Sequence) -> str:
return f"""
SELECT
toStartOfHour(start_timestamp) AS time_window_start,
- action_id,
+ action_id AS action_id,
sumState(assumeNotNull(toInt(action_count))) AS count_state,
uniqStateIf(
session_person_id,