: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(marketing-analytics): stabilize session cache identity (#105597)
Javier Bahamondesa030dfc0290f
diff --git a/docs/internal/marketing-attribution-session-precompute.md b/docs/internal/marketing-attribution-session-precompute.mdindex 42c6af8a3aa8..2e89b6e1c03f 100644--- a/docs/internal/marketing-attribution-session-precompute.md+++ b/docs/internal/marketing-attribution-session-precompute.md@@ -16,6 +16,16 @@ Materialized CTEs share the pageview identity scan between reach and credit, andQueries that override the session table version or v2 join mode fall back to live attribution when they differ from the writer. AUTO and v2 share the same session semantics. Custom channel rules can use cached dimensions when they match the writer's project rules, including rules that depend on the full entry URL. The writer classifies the channel before storing it and includes the rules in the job hash, so rule changes require fresh jobs. Queries with different rules, or disabled project-timezone conversion, use the live path. Identity and execution-only modifiers do not invalidate cached dimensions.+The shared job hash excludes `cookielessTrafficIsRegular` because the writer does not classify traffic types.+Different evaluations of that rollout flag in background workers and query workers do not require new session jobs.++For the cache-key transition, an old job with an unset `cookielessTrafficIsRegular` keeps the same key because serialization already omitted null values.+An old job with an explicit `true` or `false` has a different key and needs replacement before cached reads resume.+There is no compatibility lookup for those old keys.+Use the updated writer to prepare valid jobs for the full attribution read window before enabling cached reads.+Keep cached reads disabled until a read-only job lookup confirms complete, fresh coverage.+Missing coverage falls back to live calculation, so changing the key alone does not guarantee faster queries.+Live and cached pageview scans include the full final second of the selected date range, matching conversion filters. This also applies to explicit fractional date bounds and pageview conversion goals.Attribution filters compare the event timestamp directly with the date bounds, using microsecond precision for the end of the range.This avoids copying timestamp casts into session filters and preserves the shared raw-session timestamp definition.diff --git a/products/marketing_analytics/backend/hogql_queries/marketing_sessions_precompute.py b/products/marketing_analytics/backend/hogql_queries/marketing_sessions_precompute.pyindex 81b22e959bc2..355941096736 100644--- a/products/marketing_analytics/backend/hogql_queries/marketing_sessions_precompute.py+++ b/products/marketing_analytics/backend/hogql_queries/marketing_sessions_precompute.py@@ -132,7 +132,10 @@ def ensure_marketing_sessions_precomputed(),table=LazyComputationTable.WEB_SESSIONS_DIMENSIONAL_PREAGGREGATED,modifiers=modifiers,- cache_key_context={"modifiers": modifiers.model_dump_json(exclude_none=True)},+ # Traffic-type classification is absent from this query; its rollout may differ across workers.+ cache_key_context={+ "modifiers": modifiers.model_dump_json(exclude_none=True, exclude={"cookielessTrafficIsRegular"})+ },placeholders=base_placeholders(),query_type="marketing_sessions_dimensional_insert",)diff --git a/products/marketing_analytics/backend/hogql_queries/test_marketing_sessions_precompute.py b/products/marketing_analytics/backend/hogql_queries/test_marketing_sessions_precompute.pyindex 61dad69deac1..c013bf190580 100644--- a/products/marketing_analytics/backend/hogql_queries/test_marketing_sessions_precompute.py+++ b/products/marketing_analytics/backend/hogql_queries/test_marketing_sessions_precompute.py@@ -4,12 +4,14 @@ from datetime import UTC, datetime, timedeltaimport time_machinefrom posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_events+from unittest.mock import patchfrom parameterized import parameterizedfrom posthog.schema import HogQLQueryModifiersfrom posthog.hogql import ast+from posthog.hogql.modifiers import create_default_modifiers_for_teamfrom posthog.hogql.query import execute_hogql_queryfrom posthog.clickhouse.client import sync_execute@@ -17,6 +19,10 @@ from posthog.schema_enums import SessionTableVersionfrom posthog.test.persons import create_personfrom posthog.uuidt import uuid7+from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import (+ LazyComputationTable,+ ensure_precomputed,+)from products.marketing_analytics.backend.hogql_queries.marketing_sessions_precompute import (SESSIONS_INSERT_TEMPLATE,base_placeholders,@@ -26,6 +32,67 @@ from products.marketing_analytics.backend.hogql_queries.marketing_sessions_preco@time_machine.travel("2026-09-10T12:00:00Z", tick=False)class TestMarketingSessionsPrecompute(ClickhouseTestMixin, APIBaseTest):+ @parameterized.expand(+ [+ (version, legacy_value)+ for version in (SessionTableVersion.V2, SessionTableVersion.V3)+ for legacy_value in (None, True, False)+ ]+ )+ def test_cookieless_rollout_reuses_jobs_after_cache_key_transition(+ self, version: SessionTableVersion, legacy_value: bool | None+ ) -> None:+ self.team.modifiers = {"sessionTableVersion": version}+ start = datetime(2026, 9, 1, tzinfo=UTC)+ end = start + timedelta(days=1)+ with patch(+ "products.web_analytics.backend.hogql_queries.cookieless_flag.resolve_cookieless_traffic_is_regular_modifier"+ ) as resolve:+ resolve.return_value = legacy_value+ legacy_modifiers = create_default_modifiers_for_team(self.team)+ legacy = ensure_precomputed(+ team=self.team,+ insert_query=SESSIONS_INSERT_TEMPLATE,+ time_range_start=start,+ time_range_end=end,+ ttl_seconds=90 * 24 * 60 * 60,+ table=LazyComputationTable.WEB_SESSIONS_DIMENSIONAL_PREAGGREGATED,+ modifiers=legacy_modifiers,+ cache_key_context={"modifiers": legacy_modifiers.model_dump_json(exclude_none=True)},+ placeholders=base_placeholders(),+ )+ assert legacy.ready, legacy.errors+ assert legacy.job_ids+ written = ensure_marketing_sessions_precomputed(self.team, start, end, run_inserts=False)+ if legacy_value is None:+ assert set(written.job_ids) == set(legacy.job_ids)+ else:+ assert not written.ready+ assert not written.job_ids+ written = ensure_marketing_sessions_precomputed(self.team, start, end)+ assert set(written.job_ids).isdisjoint(legacy.job_ids)+ assert written.ready, written.errors+ assert written.job_ids+ original_sql = None+ for enabled in (None, True, False):+ resolve.return_value = enabled+ response = execute_hogql_query(+ SESSIONS_INSERT_TEMPLATE,+ self.team,+ placeholders={+ **base_placeholders(),+ "time_window_min": ast.Constant(value=start),+ "time_window_max": ast.Constant(value=end),+ },+ )+ assert response.clickhouse+ if original_sql is None:+ original_sql = response.clickhouse+ assert response.clickhouse == original_sql+ cached = ensure_marketing_sessions_precomputed(self.team, start, end, run_inserts=False)+ assert cached.ready, cached.errors+ assert set(cached.job_ids) == set(written.job_ids)+@parameterized.expand([("UTC",), ("America/Santiago",), ("Asia/Kolkata",), ("Asia/Kathmandu",)])def test_session_start_windows_use_utc_boundaries(self, timezone: str) -> None:self.team.timezone = timezone