: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(sessions): rebuild sessions v3 tables on self-hosted upgrade (#127889)
Paul D'Ambrae20ef9cd42c5
diff --git a/posthog/clickhouse/migrations/0364_sessions_v3_more_ad_ids.py b/posthog/clickhouse/migrations/0364_sessions_v3_more_ad_ids.pyindex 893fe51b2995..264b3bba0627 100644--- a/posthog/clickhouse/migrations/0364_sessions_v3_more_ad_ids.py+++ b/posthog/clickhouse/migrations/0364_sessions_v3_more_ad_ids.py@@ -1,16 +1,18 @@from posthog.clickhouse.client.connection import NodeRolefrom posthog.clickhouse.client.migration_tools import run_sql_with_exceptions-from posthog.models.raw_sessions.migrations_v3 import (- DROP_FLAG_KEY_VALUES_BLOOM_FILTER,- DROP_FLAG_KEYS,- DROP_FLAG_KEYS_BLOOM_FILTER,-)from posthog.models.raw_sessions.sessions_v3 import (- DISTRIBUTED_RAW_SESSIONS_TABLE_V3,+ DISTRIBUTED_RAW_SESSIONS_TABLE_SQL_V3,+ DROP_RAW_SESSION_DISTRIBUTED_TABLE_SQL_V3,+ DROP_RAW_SESSION_MATERIALIZED_VIEW_RECORDINGS_SQL_V3,+ DROP_RAW_SESSION_MATERIALIZED_VIEW_SQL_V3,+ DROP_RAW_SESSION_SHARDED_TABLE_SQL_V3,+ DROP_RAW_SESSION_VIEW_SQL_V3,+ DROP_RAW_SESSION_WRITABLE_TABLE_SQL_V3,+ RAW_SESSIONS_CREATE_OR_REPLACE_VIEW_SQL_V3,RAW_SESSIONS_TABLE_MV_RECORDINGS_SQL_V3,RAW_SESSIONS_TABLE_MV_SQL_V3,- SHARDED_RAW_SESSIONS_TABLE_V3,- WRITABLE_RAW_SESSIONS_TABLE_V3,+ SHARDED_RAW_SESSIONS_TABLE_SQL_V3,+ WRITABLE_RAW_SESSIONS_TABLE_SQL_V3,)from posthog.run_mode import run_mode@@ -22,56 +24,26 @@ from posthog.run_mode import run_mode# dropping the ingestion MV stops v3 ingestion. As of 2026-10-28, prod-us is done; dev and EU are# still to do. The declared cloud schema is the HCL in posthog/clickhouse/hcl/.#-# Everywhere else (fresh installs, CI, the test database) this migration is required: the earlier-# v3 migrations render the current table and view definitions, but 0178 dropped the MVs, and this-# is the only migration that creates them again. Both statements are CREATE ... IF NOT EXISTS, so-# an existing MV is kept. An install that created v3 before the shape change keeps the old shape.-# To use sessionTableVersion v3 there, drop raw_sessions_v3_mv, raw_sessions_v3_recordings_mv,-# raw_sessions_v3_v, raw_sessions_v3, writable_raw_sessions_v3 and sharded_raw_sessions_v3, then-# create them again from the definitions in posthog/models/raw_sessions/sessions_v3.py.-#-# The drops come first. 0180 and 0325 replay on a fresh cluster and put flag_keys and the two-# bloom filter indexes back on top of today's base SQL, which no longer declares them, so without-# these statements a fresh install ends up with a table the declared HCL does not describe.-DROP_SUPERSEDED_FLAG_COLUMNS = [- run_sql_with_exceptions(- DROP_FLAG_KEYS_BLOOM_FILTER.format(table_name=SHARDED_RAW_SESSIONS_TABLE_V3()),- node_roles=[NodeRole.DATA],- sharded=True,- is_alter_on_replicated_table=True,- ),- run_sql_with_exceptions(- DROP_FLAG_KEY_VALUES_BLOOM_FILTER.format(table_name=SHARDED_RAW_SESSIONS_TABLE_V3()),- node_roles=[NodeRole.DATA],- sharded=True,- is_alter_on_replicated_table=True,- ),- run_sql_with_exceptions(- DROP_FLAG_KEYS.format(table_name=SHARDED_RAW_SESSIONS_TABLE_V3()),- node_roles=[NodeRole.DATA],- sharded=True,- is_alter_on_replicated_table=True,- ),- run_sql_with_exceptions(- DROP_FLAG_KEYS.format(table_name=WRITABLE_RAW_SESSIONS_TABLE_V3()),- node_roles=[NodeRole.DATA],- sharded=False,- is_alter_on_replicated_table=False,- ),- run_sql_with_exceptions(- DROP_FLAG_KEYS.format(table_name=DISTRIBUTED_RAW_SESSIONS_TABLE_V3()),- node_roles=[NodeRole.DATA],- sharded=False,- is_alter_on_replicated_table=False,- ),+# Everywhere else (fresh installs, CI, the test database, self-hosted upgrades) this migration+# drops the v3 tables and creates them again from the current definitions. 0178 dropped the MVs and+# this is the only migration that creates them again. An MV cannot be created on top of a table+# with the old shape (uniq into uniqExact fails), so an upgraded install must get new tables. Off+# cloud, nothing has written to v3 since 0178 and the default session table is v2, so the dropped+# rows are stale. A rebuild also removes flag_keys and the bloom filter indexes that 0180 and 0325+# put back on top of today's base SQL, so the result matches the declared HCL.+REBUILD_RAW_SESSIONS_V3 = [+ run_sql_with_exceptions(DROP_RAW_SESSION_MATERIALIZED_VIEW_SQL_V3(), node_roles=[NodeRole.DATA]),+ run_sql_with_exceptions(DROP_RAW_SESSION_MATERIALIZED_VIEW_RECORDINGS_SQL_V3(), node_roles=[NodeRole.DATA]),+ run_sql_with_exceptions(DROP_RAW_SESSION_VIEW_SQL_V3(), node_roles=[NodeRole.DATA]),+ run_sql_with_exceptions(DROP_RAW_SESSION_DISTRIBUTED_TABLE_SQL_V3(), node_roles=[NodeRole.DATA]),+ run_sql_with_exceptions(DROP_RAW_SESSION_WRITABLE_TABLE_SQL_V3(), node_roles=[NodeRole.DATA]),+ run_sql_with_exceptions(DROP_RAW_SESSION_SHARDED_TABLE_SQL_V3(), node_roles=[NodeRole.DATA]),+ run_sql_with_exceptions(SHARDED_RAW_SESSIONS_TABLE_SQL_V3(), node_roles=[NodeRole.DATA]),+ run_sql_with_exceptions(WRITABLE_RAW_SESSIONS_TABLE_SQL_V3(), node_roles=[NodeRole.DATA]),+ run_sql_with_exceptions(DISTRIBUTED_RAW_SESSIONS_TABLE_SQL_V3(), node_roles=[NodeRole.DATA]),+ run_sql_with_exceptions(RAW_SESSIONS_CREATE_OR_REPLACE_VIEW_SQL_V3(), node_roles=[NodeRole.DATA]),+ run_sql_with_exceptions(RAW_SESSIONS_TABLE_MV_SQL_V3(), node_roles=[NodeRole.DATA]),+ run_sql_with_exceptions(RAW_SESSIONS_TABLE_MV_RECORDINGS_SQL_V3(), node_roles=[NodeRole.DATA]),]-operations = (- []- if run_mode().is_deployed_cloud- else [- *DROP_SUPERSEDED_FLAG_COLUMNS,- run_sql_with_exceptions(RAW_SESSIONS_TABLE_MV_SQL_V3(), node_roles=[NodeRole.DATA]),- run_sql_with_exceptions(RAW_SESSIONS_TABLE_MV_RECORDINGS_SQL_V3(), node_roles=[NodeRole.DATA]),- ]-)+operations = [] if run_mode().is_deployed_cloud else REBUILD_RAW_SESSIONS_V3diff --git a/posthog/models/raw_sessions/migrations_v3.py b/posthog/models/raw_sessions/migrations_v3.pyindex 5191c1109e5b..f72ff22c9a66 100644--- a/posthog/models/raw_sessions/migrations_v3.py+++ b/posthog/models/raw_sessions/migrations_v3.py@@ -93,21 +93,3 @@ ADD_FLAG_KEY_VALUES_BLOOM_FILTER = """ALTER TABLE {table_name}ADD INDEX IF NOT EXISTS flag_key_values_bloom_filter flag_key_values TYPE bloom_filter() GRANULARITY 1"""---DROP_FLAG_KEYS = """-ALTER TABLE {table_name}-DROP COLUMN IF EXISTS flag_keys-"""---DROP_FLAG_KEYS_BLOOM_FILTER = """-ALTER TABLE {table_name}-DROP INDEX IF EXISTS flag_keys_bloom_filter-"""---DROP_FLAG_KEY_VALUES_BLOOM_FILTER = """-ALTER TABLE {table_name}-DROP INDEX IF EXISTS flag_key_values_bloom_filter-"""