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.

feat(metrics): add a minmax index on metric_series2.last_seen (#99289)

Frank Hamand5903224bd0a5
diff --git a/.github/workflows/ci-nodejs.yml b/.github/workflows/ci-nodejs.yml
index 6be7948bd83a..387043bf2e36 100644
--- a/.github/workflows/ci-nodejs.yml
+++ b/.github/workflows/ci-nodejs.yml
@@ -302,7 +302,10 @@ jobs:
run: pnpm --filter=@posthog/nodejs format:check
- name: Lint with ESLint
- run: pnpm --filter=@posthog/nodejs lint
+ # Type-aware linting holds the whole TS program in memory and sits right at
+ # Node's default heap ceiling, so it aborts at random. Same headroom the
+ # Jest steps in this workflow already give themselves.
+ run: NODE_OPTIONS='--max_old_space_size=4096' pnpm --filter=@posthog/nodejs lint
# Guards the cdp/ingestion separation: fails on new lane->lane or shared->lane
# imports. Presence-checked so branches without the guard yet are unaffected.
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index 7ebfee4e8564..8daa1d004735 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -1252,6 +1252,11 @@ SQL
type = "bloom_filter(0.01)"
granularity = 1
}
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index 952dfc6db32b..25cb1a07e2f6 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -1816,6 +1816,11 @@ SQL
type = "bloom_filter(0.01)"
granularity = 1
}
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index 66397a09fad2..cbf0ef199360 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -6685,6 +6685,11 @@ SQL
type = "bloom_filter(0.01)"
granularity = 1
}
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index 17c7a7b3a1b8..03289bd84e76 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -1402,6 +1402,11 @@ SQL
type = "bloom_filter(0.01)"
granularity = 1
}
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index 636e11aa724f..fb5bce738952 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -1402,6 +1402,11 @@ SQL
type = "bloom_filter(0.01)"
granularity = 1
}
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/roles/logs/metrics/tables.hcl b/posthog/clickhouse/hcl/roles/logs/metrics/tables.hcl
index bfeba8337c38..79de6ad784a9 100644
--- a/posthog/clickhouse/hcl/roles/logs/metrics/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/metrics/tables.hcl
@@ -853,6 +853,11 @@ SQL
type = "bloom_filter(0.01)"
granularity = 1
}
+ index "idx_last_seen_minmax" {
+ expr = "last_seen"
+ type = "minmax"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.metric_series2"
replica_name = "{replica}-{shard}"
diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index 419806342e62..60a4633f5418 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -316,7 +316,8 @@ CREATE TABLE posthog.metric_series2 (
INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_series_distributed (
team_id Int32,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index 5d9f25132f85..4e2bce35bbcd 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -429,7 +429,8 @@ CREATE TABLE posthog.metric_series2 (
INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_series_distributed (
team_id Int32,
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 06475cf58b60..767d35628ec8 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -1320,7 +1320,8 @@ CREATE TABLE posthog.metric_series2 (
INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_series_distributed (
team_id Int32,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index dae7279b0561..076245d9572f 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -361,7 +361,8 @@ CREATE TABLE posthog.metric_series2 (
INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_series_distributed (
team_id Int32,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index 41ebfd664b51..705e8aafcf16 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -361,7 +361,8 @@ CREATE TABLE posthog.metric_series2 (
INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.metric_series2', '{replica}-{shard}', last_seen) ORDER BY (team_id, metric_name, series_fingerprint) TTL original_expiry_timestamp SETTINGS index_granularity = 8192;
CREATE TABLE posthog.metric_series_distributed (
team_id Int32,
diff --git a/posthog/clickhouse/metrics/metrics2.py b/posthog/clickhouse/metrics/metrics2.py
index 26c4dc0daa0e..3bd1361605d6 100644
--- a/posthog/clickhouse/metrics/metrics2.py
+++ b/posthog/clickhouse/metrics/metrics2.py
@@ -192,7 +192,8 @@ CREATE TABLE IF NOT EXISTS {_db()}.{METRIC_SERIES2_TABLE_NAME}
INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
)
ENGINE = {ReplacingMergeTree(METRIC_SERIES2_TABLE_NAME, replication_scheme=ReplicationScheme.REPLICATED, ver="last_seen")}
ORDER BY (team_id, metric_name, series_fingerprint)
@@ -345,6 +346,17 @@ def METRICS2_DROP_UUID_COLUMN_SQL() -> str:
return f"ALTER TABLE {_db()}.{METRICS2_TABLE_NAME} DROP COLUMN IF EXISTS uuid"
+def METRIC_SERIES2_ADD_LAST_SEEN_INDEX_SQL() -> str:
+ return (
+ f"ALTER TABLE {_db()}.{METRIC_SERIES2_TABLE_NAME} "
+ "ADD INDEX IF NOT EXISTS idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1"
+ )
+
+
+def METRIC_SERIES2_MATERIALIZE_LAST_SEEN_INDEX_SQL() -> str:
+ return f"ALTER TABLE {_db()}.{METRIC_SERIES2_TABLE_NAME} MATERIALIZE INDEX IF EXISTS idx_last_seen_minmax"
+
+
def METRICS_DISTRIBUTED_DROP_UUID_COLUMN_SQL() -> str:
return f"ALTER TABLE {_db()}.{METRICS_DISTRIBUTED_TABLE_NAME} DROP COLUMN IF EXISTS uuid"
diff --git a/posthog/clickhouse/migrations/0320_metric_series2_last_seen_index.py b/posthog/clickhouse/migrations/0320_metric_series2_last_seen_index.py
new file mode 100644
index 000000000000..79071c3fa2af
--- /dev/null
+++ b/posthog/clickhouse/migrations/0320_metric_series2_last_seen_index.py
@@ -0,0 +1,29 @@
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.clickhouse.metrics.metrics2 import (
+ METRIC_SERIES2_ADD_LAST_SEEN_INDEX_SQL,
+ METRIC_SERIES2_MATERIALIZE_LAST_SEEN_INDEX_SQL,
+)
+
+# The overview and the names picker filter `metric_series2` on `last_seen`, which is not in
+# the sort key, so every load scans the team's whole series table. Rows land in parts by
+# insert time, so a part's `last_seen` range tracks its age and a minmax index lets a
+# one-day window skip the old parts that hold most of the rows.
+#
+# MATERIALIZE is what makes that true for the parts that already exist; without it only
+# parts written after the ADD carry the index, and those are the small recent ones a
+# window would read anyway.
+operations = [
+ run_sql_with_exceptions(
+ METRIC_SERIES2_ADD_LAST_SEEN_INDEX_SQL(),
+ node_roles=[NodeRole.LOGS],
+ sharded=False,
+ is_alter_on_replicated_table=True,
+ ),
+ run_sql_with_exceptions(
+ METRIC_SERIES2_MATERIALIZE_LAST_SEEN_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 db775bba6390..638956861648 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0319_add_data_deletion_request_id_to_adhoc_events_deletion
+0320_metric_series2_last_seen_index
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 73c4aad812d4..5b22bd65c142 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -4209,7 +4209,8 @@
INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
)
ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_series2', '{replica}-{shard}', last_seen)
ORDER BY (team_id, metric_name, series_fingerprint)
@@ -11364,7 +11365,8 @@
INDEX idx_service_set service_name TYPE set(1000) GRANULARITY 1,
INDEX idx_resource_fingerprint resource_fingerprint TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attr_keys mapKeys(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1
+ INDEX idx_attr_values mapValues(attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
+ INDEX idx_last_seen_minmax last_seen TYPE minmax GRANULARITY 1
)
ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.metric_series2', '{replica}-{shard}', last_seen)
ORDER BY (team_id, metric_name, series_fingerprint)