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.

chore(metrics): add timestamp minmax index to metrics2 and bucketed maps to metric_series3 (#101537)

Frank Hamand5fc5bef0b5d0
diff --git a/posthog/clickhouse/metrics/metrics2.py b/posthog/clickhouse/metrics/metrics2.py
index 3bd1361605d6..c6cd85cdc800 100644
--- a/posthog/clickhouse/metrics/metrics2.py
+++ b/posthog/clickhouse/metrics/metrics2.py
@@ -138,6 +138,7 @@ CREATE TABLE IF NOT EXISTS {_db()}.{METRICS2_TABLE_NAME}
INDEX idx_trace_id_bf trace_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
PROJECTION projection_series_activity
(
SELECT
@@ -346,6 +347,13 @@ def METRICS2_DROP_UUID_COLUMN_SQL() -> str:
return f"ALTER TABLE {_db()}.{METRICS2_TABLE_NAME} DROP COLUMN IF EXISTS uuid"
+def METRICS2_ADD_TIMESTAMP_INDEX_SQL() -> str:
+ return (
+ f"ALTER TABLE {_db()}.{METRICS2_TABLE_NAME} "
+ "ADD INDEX IF NOT EXISTS idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1"
+ )
+
+
def METRIC_SERIES2_ADD_LAST_SEEN_INDEX_SQL() -> str:
return (
f"ALTER TABLE {_db()}.{METRIC_SERIES2_TABLE_NAME} "
diff --git a/posthog/clickhouse/migrations/0324_metrics2_timestamp_index.py b/posthog/clickhouse/migrations/0324_metrics2_timestamp_index.py
new file mode 100644
index 000000000000..f63045faf8cc
--- /dev/null
+++ b/posthog/clickhouse/migrations/0324_metrics2_timestamp_index.py
@@ -0,0 +1,15 @@
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.clickhouse.metrics.metrics2 import METRICS2_ADD_TIMESTAMP_INDEX_SQL
+
+# `time_bucket` in the `metrics2` sort key only narrows a time filter to the hour, so a
+# minmax index on `timestamp` lets a query skip the granules inside that hour it does
+# not need. Only parts written after this migration carry it.
+operations = [
+ run_sql_with_exceptions(
+ METRICS2_ADD_TIMESTAMP_INDEX_SQL(),
+ node_roles=[NodeRole.LOGS],
+ sharded=False,
+ is_alter_on_replicated_table=True,
+ ),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 5a95c70ee64f..fb6a87824408 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0323_metrics_metadata3_dual_write
+0324_metrics2_timestamp_index
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 78f622318208..23af3a111092 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -4413,6 +4413,7 @@
INDEX idx_trace_id_bf trace_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
PROJECTION projection_series_activity
(
SELECT
@@ -11701,6 +11702,7 @@
INDEX idx_trace_id_bf trace_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_observed_minmax observed_timestamp TYPE minmax GRANULARITY 1,
+ INDEX idx_timestamp_minmax timestamp TYPE minmax GRANULARITY 1,
PROJECTION projection_series_activity
(
SELECT