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(clickhouse): drop obsolete trace span projections (#106611)

Frank Hamand11bcf3b62f06
diff --git a/posthog/clickhouse/hcl/golden/dev/logs.hcl b/posthog/clickhouse/hcl/golden/dev/logs.hcl
index 081a607dbe9b..570cfa1eac54 100644
--- a/posthog/clickhouse/hcl/golden/dev/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/logs.hcl
@@ -3101,13 +3101,6 @@ SQL
type = "bloom_filter(0.05)"
granularity = 99999
}
- projection "projection_index_span_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY span_id
-SQL
-
- }
projection "projection_index_team_span_id" {
query = <<SQL
SELECT team_id, _part_offset
@@ -3136,13 +3129,6 @@ GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
SQL
- }
- projection "projection_index_trace_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY trace_id
-SQL
-
}
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
index 8e2169f15448..54d0a89a84a7 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/logs.hcl
@@ -3600,13 +3600,6 @@ SQL
type = "bloom_filter(0.05)"
granularity = 99999
}
- projection "projection_index_span_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY span_id
-SQL
-
- }
projection "projection_index_team_span_id" {
query = <<SQL
SELECT team_id, _part_offset
@@ -3635,13 +3628,6 @@ GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
SQL
- }
- projection "projection_index_trace_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY trace_id
-SQL
-
}
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans"
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index f5b28274fbb6..476ef7ae5dc2 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -15336,13 +15336,6 @@ SQL
type = "bloom_filter(0.05)"
granularity = 99999
}
- projection "projection_index_span_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY span_id
-SQL
-
- }
projection "projection_index_team_span_id" {
query = <<SQL
SELECT team_id, _part_offset
@@ -15371,13 +15364,6 @@ GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
SQL
- }
- projection "projection_index_trace_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY trace_id
-SQL
-
}
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans"
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
index f4b0f97d90f1..7169b17f746d 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/logs.hcl
@@ -3187,13 +3187,6 @@ SQL
type = "bloom_filter(0.05)"
granularity = 99999
}
- projection "projection_index_span_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY span_id
-SQL
-
- }
projection "projection_index_team_span_id" {
query = <<SQL
SELECT team_id, _part_offset
@@ -3222,16 +3215,6 @@ GROUP BY
SQL
}
- projection "projection_index_trace_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY trace_id
-SQL
-
- settings = {
- index_granularity = "512"
- }
- }
projection "projection_aggregate_counts2" {
query = <<SQL
SELECT
diff --git a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
index 4e039b3c3da5..d7437c3d6d63 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/logs.hcl
@@ -3187,13 +3187,6 @@ SQL
type = "bloom_filter(0.05)"
granularity = 99999
}
- projection "projection_index_span_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY span_id
-SQL
-
- }
projection "projection_index_team_span_id" {
query = <<SQL
SELECT team_id, _part_offset
@@ -3222,16 +3215,6 @@ GROUP BY
SQL
}
- projection "projection_index_trace_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY trace_id
-SQL
-
- settings = {
- index_granularity = "512"
- }
- }
projection "projection_aggregate_counts2" {
query = <<SQL
SELECT
diff --git a/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl b/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl
index 2f2844adc8e6..d96327533010 100644
--- a/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/dev/patches.hcl
@@ -55,13 +55,6 @@ GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
SQL
- }
- projection "projection_index_trace_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY trace_id
-SQL
-
}
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans"
diff --git a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
index c879e6beafba..f1705c83903b 100644
--- a/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/local/tables.hcl
@@ -581,13 +581,6 @@ GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span
SQL
- }
- projection "projection_index_trace_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY trace_id
-SQL
-
}
engine "replicated_merge_tree" {
zoo_path = "/clickhouse/tables/noshard/posthog.trace_spans"
diff --git a/posthog/clickhouse/hcl/roles/logs/prod/patches.hcl b/posthog/clickhouse/hcl/roles/logs/prod/patches.hcl
index a0b867294815..b201a557e024 100644
--- a/posthog/clickhouse/hcl/roles/logs/prod/patches.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/prod/patches.hcl
@@ -54,16 +54,6 @@ GROUP BY
SQL
}
- projection "projection_index_trace_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY trace_id
-SQL
-
- settings = {
- index_granularity = "512"
- }
- }
projection "projection_aggregate_counts2" {
query = <<SQL
SELECT
diff --git a/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl b/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl
index 8e642c43637e..b4113acaf003 100644
--- a/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/logs/traces/tables.hcl
@@ -341,13 +341,6 @@ database "posthog" {
type = "bloom_filter(0.05)"
granularity = 99999
}
- projection "projection_index_span_id" {
- query = <<SQL
-SELECT _part_offset
-ORDER BY span_id
-SQL
-
- }
projection "projection_index_team_span_id" {
query = <<SQL
SELECT team_id, _part_offset
diff --git a/posthog/clickhouse/hcl/sql/dev/logs.sql b/posthog/clickhouse/hcl/sql/dev/logs.sql
index fef421be8669..c0f610a77f8f 100644
--- a/posthog/clickhouse/hcl/sql/dev/logs.sql
+++ b/posthog/clickhouse/hcl/sql/dev/logs.sql
@@ -803,8 +803,6 @@ CREATE TABLE posthog.trace_spans (
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
INDEX idx_trace_bloom_part_v2 trace_id TYPE bloom_filter(0.05) GRANULARITY 99999,
INDEX idx_span_id_bloom_part_v2 span_id TYPE bloom_filter(0.05) GRANULARITY 99999,
- PROJECTION projection_index_span_id (SELECT _part_offset
-ORDER BY span_id),
PROJECTION projection_index_team_span_id (SELECT team_id, _part_offset
ORDER BY span_id),
PROJECTION projection_index_team_trace_id (SELECT team_id, _part_offset
@@ -818,9 +816,7 @@ ORDER BY trace_id),
is_root_span,
count() AS event_count
GROUP BY
- team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span),
- PROJECTION projection_index_trace_id (SELECT _part_offset
-ORDER BY trace_id)
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_spans', '{replica}-{shard}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS allow_part_offset_column_in_projections = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_serialization_version = 'with_buckets', storage_policy = 's3_tiered', ttl_only_drop_parts = 1;
CREATE TABLE posthog.trace_spans_distributed (
time_bucket DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
diff --git a/posthog/clickhouse/hcl/sql/local-multi/logs.sql b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
index cb27908ff2d8..f67ad0972fb4 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/logs.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/logs.sql
@@ -895,8 +895,6 @@ CREATE TABLE posthog.trace_spans (
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
INDEX idx_trace_bloom_part_v2 trace_id TYPE bloom_filter(0.05) GRANULARITY 99999,
INDEX idx_span_id_bloom_part_v2 span_id TYPE bloom_filter(0.05) GRANULARITY 99999,
- PROJECTION projection_index_span_id (SELECT _part_offset
-ORDER BY span_id),
PROJECTION projection_index_team_span_id (SELECT team_id, _part_offset
ORDER BY span_id),
PROJECTION projection_index_team_trace_id (SELECT team_id, _part_offset
@@ -910,9 +908,7 @@ ORDER BY trace_id),
is_root_span,
count() AS event_count
GROUP BY
- team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span),
- PROJECTION projection_index_trace_id (SELECT _part_offset
-ORDER BY trace_id)
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_spans', '{replica}-{shard}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS allow_part_offset_column_in_projections = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_serialization_version = 'with_buckets', ttl_only_drop_parts = 1;
CREATE TABLE posthog.trace_spans_distributed (
time_bucket DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 3f91451a396b..4f266c6d1844 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -3286,8 +3286,6 @@ CREATE TABLE posthog.trace_spans (
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
INDEX idx_trace_bloom_part_v2 trace_id TYPE bloom_filter(0.05) GRANULARITY 99999,
INDEX idx_span_id_bloom_part_v2 span_id TYPE bloom_filter(0.05) GRANULARITY 99999,
- PROJECTION projection_index_span_id (SELECT _part_offset
-ORDER BY span_id),
PROJECTION projection_index_team_span_id (SELECT team_id, _part_offset
ORDER BY span_id),
PROJECTION projection_index_team_trace_id (SELECT team_id, _part_offset
@@ -3301,9 +3299,7 @@ ORDER BY trace_id),
is_root_span,
count() AS event_count
GROUP BY
- team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span),
- PROJECTION projection_index_trace_id (SELECT _part_offset
-ORDER BY trace_id)
+ team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint, is_root_span)
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/noshard/posthog.trace_spans', '{replica}-{shard}') ORDER BY (team_id, time_bucket, service_name, resource_fingerprint, status_code, name, timestamp) PARTITION BY toDate(original_expiry_timestamp) TTL original_expiry_timestamp SETTINGS allow_part_offset_column_in_projections = 1, index_granularity = 8192, index_granularity_bytes = 104857600, map_serialization_version = 'with_buckets', ttl_only_drop_parts = 1;
CREATE TABLE posthog.trace_spans_distributed (
time_bucket DateTime MATERIALIZED toStartOfInterval(timestamp, toIntervalHour(4)),
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
index e7b7a65839ba..4fd02748533c 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/logs.sql
@@ -827,8 +827,6 @@ CREATE TABLE posthog.trace_spans (
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
INDEX idx_trace_bloom_part_v2 trace_id TYPE bloom_filter(0.05) GRANULARITY 99999,
INDEX idx_span_id_bloom_part_v2 span_id TYPE bloom_filter(0.05) GRANULARITY 99999,
- PROJECTION projection_index_span_id (SELECT _part_offset
-ORDER BY span_id),
PROJECTION projection_index_team_span_id (SELECT team_id, _part_offset
ORDER BY span_id),
PROJECTION projection_index_team_trace_id (SELECT team_id, _part_offset
@@ -842,8 +840,6 @@ ORDER BY trace_id),
count() AS event_count
GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint),
- PROJECTION projection_index_trace_id (SELECT _part_offset
-ORDER BY trace_id) WITH SETTINGS (index_granularity = 512),
PROJECTION projection_aggregate_counts2 (SELECT
team_id,
time_bucket,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/logs.sql b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
index fe61a8cbc88e..224a3a37128d 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/logs.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/logs.sql
@@ -827,8 +827,6 @@ CREATE TABLE posthog.trace_spans (
INDEX idx_attributes_str_values mapValues(attributes_map_str) TYPE bloom_filter(0.001) GRANULARITY 16,
INDEX idx_trace_bloom_part_v2 trace_id TYPE bloom_filter(0.05) GRANULARITY 99999,
INDEX idx_span_id_bloom_part_v2 span_id TYPE bloom_filter(0.05) GRANULARITY 99999,
- PROJECTION projection_index_span_id (SELECT _part_offset
-ORDER BY span_id),
PROJECTION projection_index_team_span_id (SELECT team_id, _part_offset
ORDER BY span_id),
PROJECTION projection_index_team_trace_id (SELECT team_id, _part_offset
@@ -842,8 +840,6 @@ ORDER BY trace_id),
count() AS event_count
GROUP BY
team_id, time_bucket, toStartOfMinute(timestamp), service_name, resource_fingerprint),
- PROJECTION projection_index_trace_id (SELECT _part_offset
-ORDER BY trace_id) WITH SETTINGS (index_granularity = 512),
PROJECTION projection_aggregate_counts2 (SELECT
team_id,
time_bucket,
diff --git a/posthog/clickhouse/migrations/0341_drop_trace_spans_old_projections.py b/posthog/clickhouse/migrations/0341_drop_trace_spans_old_projections.py
new file mode 100644
index 000000000000..0012f53d327f
--- /dev/null
+++ b/posthog/clickhouse/migrations/0341_drop_trace_spans_old_projections.py
@@ -0,0 +1,20 @@
+"""AUTO-GENERATED from the declarative HCL by posthog/clickhouse/hcl/codegen/gen_migration.py.
+Placement (node_roles) is derived from the node composition manifest; review before committing.
+"""
+
+from django.conf import settings
+
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+
+DB = settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE
+
+operations = [
+ run_sql_with_exceptions(
+ f"ALTER TABLE {DB}.trace_spans DROP PROJECTION IF EXISTS projection_index_span_id, "
+ "DROP PROJECTION IF EXISTS projection_index_trace_id SETTINGS alter_sync=0",
+ 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 deeab9508b11..f70975c24442 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0340_drop_metrics_avro1_kafka_ingest
+0341_drop_trace_spans_old_projections
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index f949a6b26cbb..57ef9f340643 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -9758,18 +9758,6 @@
is_root_span
),
- PROJECTION projection_index_span_id
- (
- SELECT _part_offset
- ORDER BY span_id
- ),
-
- PROJECTION projection_index_trace_id
- (
- SELECT _part_offset
- ORDER BY trace_id
- ),
-
PROJECTION projection_index_team_span_id
(
SELECT team_id, _part_offset
@@ -15411,18 +15399,6 @@
is_root_span
),
- PROJECTION projection_index_span_id
- (
- SELECT _part_offset
- ORDER BY span_id
- ),
-
- PROJECTION projection_index_trace_id
- (
- SELECT _part_offset
- ORDER BY trace_id
- ),
-
PROJECTION projection_index_team_span_id
(
SELECT team_id, _part_offset
diff --git a/posthog/clickhouse/traces/spans.py b/posthog/clickhouse/traces/spans.py
index 8483eba94d75..27a2912d1611 100644
--- a/posthog/clickhouse/traces/spans.py
+++ b/posthog/clickhouse/traces/spans.py
@@ -95,18 +95,6 @@ CREATE TABLE IF NOT EXISTS {settings.CLICKHOUSE_LOGS_CLUSTER_DATABASE}.{TABLE_NA
is_root_span
),
- PROJECTION projection_index_span_id
- (
- SELECT _part_offset
- ORDER BY span_id
- ),
-
- PROJECTION projection_index_trace_id
- (
- SELECT _part_offset
- ORDER BY trace_id
- ),
-
PROJECTION projection_index_team_span_id
(
SELECT team_id, _part_offset