: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(tests): default the materialized-columns metadata cache on in TEST (#69439)
Paul D'Ambra4a4eb9374aaf
diff --git a/ee/clickhouse/materialized_columns/columns.py b/ee/clickhouse/materialized_columns/columns.pyindex 89e1884c1757..e55c3da94b81 100644--- a/ee/clickhouse/materialized_columns/columns.py+++ b/ee/clickhouse/materialized_columns/columns.py@@ -97,7 +97,10 @@ class MaterializedColumn:@staticmethoddef _get_all(table: TablesWithMaterializedColumns) -> list[tuple[str, str, str, bool, list[str]]]:- refresh_cache = random.random() < 0.002 # we run around 50 of those queries per minute+ # In TEST mode never do the probabilistic refresh: test-mode cache is process-local and+ # every schema mutation invalidates the key explicitly, so a random bypass would cause+ # non-deterministic extra system.columns queries that break assertNumQueries / snapshot tests.+ refresh_cache = not TEST and random.random() < 0.002 # we run around 50 of those queries per minuteif table in MATERIALIZATION_VALID_TABLES and not refresh_cache and MATERIALIZED_COLUMNS_USE_CACHE:cache_key = get_materialized_columns_cache_key(table)diff --git a/ee/clickhouse/materialized_columns/test/test_columns.py b/ee/clickhouse/materialized_columns/test/test_columns.pyindex 91b6919eb6dc..15e2735a22de 100644--- a/ee/clickhouse/materialized_columns/test/test_columns.py+++ b/ee/clickhouse/materialized_columns/test/test_columns.py@@ -21,8 +21,10 @@ from posthog.models.property import PropertyName, TableColumnfrom posthog.settings import CLICKHOUSE_DATABASEfrom ee.clickhouse.materialized_columns.columns import (+ MATERIALIZATION_VALID_TABLES,MaterializedColumn,MaterializedColumnDetails,+ _clear_materialized_columns_cache,backfill_materialized_columns,drop_column,get_bloom_filter_index_name,@@ -91,6 +93,9 @@ class TestMaterializedColumns(ClickhouseTestMixin, BaseTest):super().tearDown()def recreate_database(self):+ # Dropping the database removes materialized columns behind the metadata cache's back.+ for table in MATERIALIZATION_VALID_TABLES:+ _clear_materialized_columns_cache(table)sync_execute(f"DROP DATABASE {CLICKHOUSE_DATABASE} SYNC")sync_execute(f"CREATE DATABASE {CLICKHOUSE_DATABASE}")create_clickhouse_tables()diff --git a/posthog/clickhouse/materialized_columns.py b/posthog/clickhouse/materialized_columns.pyindex 673c690a6a2a..e48d2e5055c0 100644--- a/posthog/clickhouse/materialized_columns.py+++ b/posthog/clickhouse/materialized_columns.py@@ -6,7 +6,7 @@ from posthog.settings import EE_AVAILABLEColumnName = strTablesWithMaterializedColumns = TableWithProperties-MATERIALIZATION_VALID_TABLES = {"events", "person", "groups"}+MATERIALIZATION_VALID_TABLES: frozenset[TablesWithMaterializedColumns] = frozenset({"events", "person", "groups"})DMAT_STRING_COLUMN_NAME_PREFIX = "dmat_string_"# Naming prefixes for physical materialized columns; mat_/pmat_ are minted bydiff --git a/posthog/settings/data_stores.py b/posthog/settings/data_stores.pyindex 4885702107dc..ffdfc284ef44 100644--- a/posthog/settings/data_stores.py+++ b/posthog/settings/data_stores.py@@ -667,7 +667,10 @@ if TEST:# Cache timeout for materialized columns metadata (in seconds)MATERIALIZED_COLUMNS_CACHE_TIMEOUT: int = get_from_env("MATERIALIZED_COLUMNS_CACHE_TIMEOUT", 900, type_cast=int)-MATERIALIZED_COLUMNS_USE_CACHE: bool = get_from_env("MATERIALIZED_COLUMNS_USE_CACHE", False, type_cast=str_to_bool)+# Default on in TEST: the schema-introspection query behind materialized-column lookups otherwise runs+# hundreds of times per suite, and the test cache backend is process-local (LocMem) with all column+# mutations invalidating the key (see ee/clickhouse/materialized_columns/columns.py).+MATERIALIZED_COLUMNS_USE_CACHE: bool = get_from_env("MATERIALIZED_COLUMNS_USE_CACHE", TEST, type_cast=str_to_bool)# Limiting event_list API, saving ClickHouse, 0 - disabled, 1 - migration period, 2 - enabled.PATCH_EVENT_LIST_MAX_OFFSET: int = get_from_env("PATCH_EVENT_LIST_MAX_OFFSET", 0, type_cast=int)diff --git a/posthog/test/base.py b/posthog/test/base.pyindex f827542887a0..46cc01c1f2c0 100644--- a/posthog/test/base.py+++ b/posthog/test/base.py@@ -993,6 +993,8 @@ def stripResponse(response, remove=("action", "label", "persons_urls", "filter")def cleanup_materialized_columns():try:from ee.clickhouse.materialized_columns.columns import (+ MATERIALIZATION_VALID_TABLES,+ _clear_materialized_columns_cache,get_bloom_filter_index_name,get_bloom_filter_lower_index_name,get_materialized_columns,@@ -1004,6 +1006,11 @@ def cleanup_materialized_columns():# EE not available? Skipreturn+ # A prior test may have mutated schema with raw sync_execute, bypassing materialize()/+ # drop_column() (which self-invalidate) — refresh before deciding what to drop below.+ for _table in MATERIALIZATION_VALID_TABLES:+ _clear_materialized_columns_cache(_table)+def optionally_drop(table, filter=None):columns_to_drop = [column for column in get_materialized_columns(table).values() if filter is None or filter(column.name)@@ -1042,6 +1049,10 @@ def cleanup_materialized_columns():optionally_drop("events", lambda name: name not in default_column_names)optionally_drop("person")optionally_drop("groups")+ # Raw DROP COLUMN above bypasses drop_column(), which normally self-invalidates the cache.+ # Clear it explicitly so subsequent lookups in the same process reflect the new schema.+ for _table in MATERIALIZATION_VALID_TABLES:+ _clear_materialized_columns_cache(_table)def get_index_from_explain(@@ -1672,6 +1683,18 @@ def run_clickhouse_statement_in_parallel(statements: list[str]):def reset_clickhouse_database() -> None:+ # Dropping tables below removes their materialized columns behind the metadata cache's back,+ # so drop the cached entries with them (mutations via materialize()/drop_column() self-invalidate).+ try:+ from ee.clickhouse.materialized_columns.columns import ( # noqa: PLC0415 — keeps the ee dep optional, like the other ee imports in this module+ MATERIALIZATION_VALID_TABLES,+ _clear_materialized_columns_cache,+ )++ for _mat_table in MATERIALIZATION_VALID_TABLES:+ _clear_materialized_columns_cache(_mat_table)+ except ModuleNotFoundError:+ passrun_clickhouse_statement_in_parallel([DROP_RAW_SESSION_MATERIALIZED_VIEW_SQL(),