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.

fix(usage): place billing usage storage on aux (#92381)

Rory Shankse1054b62fc11
diff --git a/posthog/clickhouse/hcl/golden/dev/auxiliary.hcl b/posthog/clickhouse/hcl/golden/dev/auxiliary.hcl
index a9ebf92f4d5c..ab32f650c604 100644
--- a/posthog/clickhouse/hcl/golden/dev/auxiliary.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/auxiliary.hcl
@@ -1694,6 +1694,58 @@ database "posthog" {
}
}
+ table "sharded_billing_usage_records" {
+ order_by = ["team_id", "toDate(timestamp)", "producer_id", "usage_key", "record_id"]
+ partition_by = "toYYYYMM(timestamp)"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "schema_version" {
+ type = "UInt8"
+ }
+ column "record_id" {
+ type = "String"
+ }
+ column "producer_id" {
+ type = "LowCardinality(String)"
+ }
+ column "team_id" {
+ type = "Int64"
+ }
+ column "organization_id" {
+ type = "UUID"
+ }
+ column "usage_key" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "quantity" {
+ type = "Int64"
+ }
+ column "timestamp" {
+ type = "DateTime64(6, 'UTC')"
+ }
+ column "inserted_at" {
+ type = "DateTime64(6, 'UTC')"
+ }
+ column "_timestamp" {
+ type = "DateTime"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_partition" {
+ type = "UInt64"
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records"
+ replica_name = "{replica}"
+ version_column = "inserted_at"
+ }
+ }
+
table "sharded_conversion_goal_attributed_preaggregated" {
order_by = ["team_id", "job_id", "person_id", "conversion_timestamp", "touchpoint_timestamp"]
partition_by = "toYYYYMMDD(expires_at)"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hcl b/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hcl
index d9bfc4843ab6..221e9d5953df 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hcl
@@ -1738,6 +1738,58 @@ database "posthog" {
}
}
+ table "sharded_billing_usage_records" {
+ order_by = ["team_id", "toDate(timestamp)", "producer_id", "usage_key", "record_id"]
+ partition_by = "toYYYYMM(timestamp)"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "schema_version" {
+ type = "UInt8"
+ }
+ column "record_id" {
+ type = "String"
+ }
+ column "producer_id" {
+ type = "LowCardinality(String)"
+ }
+ column "team_id" {
+ type = "Int64"
+ }
+ column "organization_id" {
+ type = "UUID"
+ }
+ column "usage_key" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "quantity" {
+ type = "Int64"
+ }
+ column "timestamp" {
+ type = "DateTime64(6, 'UTC')"
+ }
+ column "inserted_at" {
+ type = "DateTime64(6, 'UTC')"
+ }
+ column "_timestamp" {
+ type = "DateTime"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_partition" {
+ type = "UInt64"
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records"
+ replica_name = "{replica}"
+ version_column = "inserted_at"
+ }
+ }
+
table "sharded_conversion_goal_attributed_preaggregated" {
order_by = ["team_id", "job_id", "person_id", "conversion_timestamp", "touchpoint_timestamp"]
partition_by = "toYYYYMMDD(expires_at)"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/data.hcl b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
index e827f020b24c..65029a659c6e 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/data.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
@@ -356,7 +356,7 @@ database "posthog" {
type = "UInt64"
}
engine "distributed" {
- cluster_name = "posthog"
+ cluster_name = "aux"
remote_database = "posthog"
remote_table = "sharded_billing_usage_records"
sharding_key = "cityHash64(team_id)"
@@ -5086,58 +5086,6 @@ database "posthog" {
}
}
- table "sharded_billing_usage_records" {
- order_by = ["team_id", "toDate(timestamp)", "producer_id", "usage_key", "record_id"]
- partition_by = "toYYYYMM(timestamp)"
- settings = {
- index_granularity = "8192"
- }
- column "schema_version" {
- type = "UInt8"
- }
- column "record_id" {
- type = "String"
- }
- column "producer_id" {
- type = "LowCardinality(String)"
- }
- column "team_id" {
- type = "Int64"
- }
- column "organization_id" {
- type = "UUID"
- }
- column "usage_key" {
- type = "LowCardinality(String)"
- }
- column "unit" {
- type = "LowCardinality(String)"
- }
- column "quantity" {
- type = "Int64"
- }
- column "timestamp" {
- type = "DateTime64(6, 'UTC')"
- }
- column "inserted_at" {
- type = "DateTime64(6, 'UTC')"
- }
- column "_timestamp" {
- type = "DateTime"
- }
- column "_offset" {
- type = "UInt64"
- }
- column "_partition" {
- type = "UInt64"
- }
- engine "replicated_replacing_merge_tree" {
- zoo_path = "/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records"
- replica_name = "{replica}"
- version_column = "inserted_at"
- }
- }
-
table "sharded_distinct_id_usage" {
order_by = ["team_id", "minute", "distinct_id"]
partition_by = "toYYYYMMDD(minute)"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/ingestion_small.hcl b/posthog/clickhouse/hcl/golden/local-multi/ingestion_small.hcl
index dc806c1d0810..746335657b6a 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/ingestion_small.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/ingestion_small.hcl
@@ -1092,7 +1092,7 @@ database "posthog" {
type = "UInt64"
}
engine "distributed" {
- cluster_name = "posthog"
+ cluster_name = "aux"
remote_database = "posthog"
remote_table = "sharded_billing_usage_records"
sharding_key = "cityHash64(team_id)"
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index 7f653d6615d0..92da583058c8 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -383,7 +383,7 @@ database "posthog" {
type = "UInt64"
}
engine "distributed" {
- cluster_name = "posthog"
+ cluster_name = "aux"
remote_database = "posthog"
remote_table = "sharded_billing_usage_records"
sharding_key = "cityHash64(team_id)"
@@ -15522,7 +15522,7 @@ SQL
type = "UInt64"
}
engine "distributed" {
- cluster_name = "posthog"
+ cluster_name = "aux"
remote_database = "posthog"
remote_table = "sharded_billing_usage_records"
sharding_key = "cityHash64(team_id)"
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hcl b/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hcl
index 440d190e618a..d8bc9a9564e9 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hcl
@@ -1694,6 +1694,58 @@ database "posthog" {
}
}
+ table "sharded_billing_usage_records" {
+ order_by = ["team_id", "toDate(timestamp)", "producer_id", "usage_key", "record_id"]
+ partition_by = "toYYYYMM(timestamp)"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "schema_version" {
+ type = "UInt8"
+ }
+ column "record_id" {
+ type = "String"
+ }
+ column "producer_id" {
+ type = "LowCardinality(String)"
+ }
+ column "team_id" {
+ type = "Int64"
+ }
+ column "organization_id" {
+ type = "UUID"
+ }
+ column "usage_key" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "quantity" {
+ type = "Int64"
+ }
+ column "timestamp" {
+ type = "DateTime64(6, 'UTC')"
+ }
+ column "inserted_at" {
+ type = "DateTime64(6, 'UTC')"
+ }
+ column "_timestamp" {
+ type = "DateTime"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_partition" {
+ type = "UInt64"
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records"
+ replica_name = "{replica}"
+ version_column = "inserted_at"
+ }
+ }
+
table "sharded_conversion_goal_attributed_preaggregated" {
order_by = ["team_id", "job_id", "person_id", "conversion_timestamp", "touchpoint_timestamp"]
partition_by = "toYYYYMMDD(expires_at)"
diff --git a/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hcl b/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hcl
index 21e68205200d..b196938c48d1 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hcl
@@ -1732,6 +1732,58 @@ database "posthog" {
}
}
+ table "sharded_billing_usage_records" {
+ order_by = ["team_id", "toDate(timestamp)", "producer_id", "usage_key", "record_id"]
+ partition_by = "toYYYYMM(timestamp)"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "schema_version" {
+ type = "UInt8"
+ }
+ column "record_id" {
+ type = "String"
+ }
+ column "producer_id" {
+ type = "LowCardinality(String)"
+ }
+ column "team_id" {
+ type = "Int64"
+ }
+ column "organization_id" {
+ type = "UUID"
+ }
+ column "usage_key" {
+ type = "LowCardinality(String)"
+ }
+ column "unit" {
+ type = "LowCardinality(String)"
+ }
+ column "quantity" {
+ type = "Int64"
+ }
+ column "timestamp" {
+ type = "DateTime64(6, 'UTC')"
+ }
+ column "inserted_at" {
+ type = "DateTime64(6, 'UTC')"
+ }
+ column "_timestamp" {
+ type = "DateTime"
+ }
+ column "_offset" {
+ type = "UInt64"
+ }
+ column "_partition" {
+ type = "UInt64"
+ }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records"
+ replica_name = "{replica}"
+ version_column = "inserted_at"
+ }
+ }
+
table "sharded_conversion_goal_attributed_preaggregated" {
order_by = ["team_id", "job_id", "person_id", "conversion_timestamp", "touchpoint_timestamp"]
partition_by = "toYYYYMMDD(expires_at)"
diff --git a/posthog/clickhouse/hcl/roles/auxiliary/shared/tables.hcl b/posthog/clickhouse/hcl/roles/auxiliary/shared/tables.hcl
index 4b642b4fca2b..91748f9f0820 100644
--- a/posthog/clickhouse/hcl/roles/auxiliary/shared/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/auxiliary/shared/tables.hcl
@@ -428,6 +428,31 @@ database "posthog" {
replica_name = "{replica}"
}
}
+ table "sharded_billing_usage_records" {
+ order_by = ["team_id", "toDate(timestamp)", "producer_id", "usage_key", "record_id"]
+ partition_by = "toYYYYMM(timestamp)"
+ settings = {
+ index_granularity = "8192"
+ }
+ column "schema_version" { type = "UInt8" }
+ column "record_id" { type = "String" }
+ column "producer_id" { type = "LowCardinality(String)" }
+ column "team_id" { type = "Int64" }
+ column "organization_id" { type = "UUID" }
+ column "usage_key" { type = "LowCardinality(String)" }
+ column "unit" { type = "LowCardinality(String)" }
+ column "quantity" { type = "Int64" }
+ column "timestamp" { type = "DateTime64(6, 'UTC')" }
+ column "inserted_at" { type = "DateTime64(6, 'UTC')" }
+ column "_timestamp" { type = "DateTime" }
+ column "_offset" { type = "UInt64" }
+ column "_partition" { type = "UInt64" }
+ engine "replicated_replacing_merge_tree" {
+ zoo_path = "/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records"
+ replica_name = "{replica}"
+ version_column = "inserted_at"
+ }
+ }
table "sharded_web_bot_definition" {
order_by = ["id"]
settings = {
diff --git a/posthog/clickhouse/hcl/roles/data/local/tables.hcl b/posthog/clickhouse/hcl/roles/data/local/tables.hcl
index 9bbd76968120..ca33db3df45e 100644
--- a/posthog/clickhouse/hcl/roles/data/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/data/local/tables.hcl
@@ -7811,32 +7811,6 @@ SQL
comment = "column_materializer::properties::$ai_experiment_id"
}
}
- table "sharded_billing_usage_records" {
- order_by = ["team_id", "toDate(timestamp)", "producer_id", "usage_key", "record_id"]
- partition_by = "toYYYYMM(timestamp)"
- settings = {
- index_granularity = "8192"
- }
- column "schema_version" { type = "UInt8" }
- column "record_id" { type = "String" }
- column "producer_id" { type = "LowCardinality(String)" }
- column "team_id" { type = "Int64" }
- column "organization_id" { type = "UUID" }
- column "usage_key" { type = "LowCardinality(String)" }
- column "unit" { type = "LowCardinality(String)" }
- column "quantity" { type = "Int64" }
- column "timestamp" { type = "DateTime64(6, 'UTC')" }
- column "inserted_at" { type = "DateTime64(6, 'UTC')" }
- column "_timestamp" { type = "DateTime" }
- column "_offset" { type = "UInt64" }
- column "_partition" { type = "UInt64" }
- engine "replicated_replacing_merge_tree" {
- zoo_path = "/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records"
- replica_name = "{replica}"
- version_column = "inserted_at"
- }
- }
-
table "billing_usage_records" {
column "schema_version" { type = "UInt8" }
column "record_id" { type = "String" }
@@ -7852,7 +7826,7 @@ SQL
column "_offset" { type = "UInt64" }
column "_partition" { type = "UInt64" }
engine "distributed" {
- cluster_name = "posthog"
+ cluster_name = "aux"
remote_database = "posthog"
remote_table = "sharded_billing_usage_records"
sharding_key = "cityHash64(team_id)"
diff --git a/posthog/clickhouse/hcl/roles/ingestion_small/local/tables.hcl b/posthog/clickhouse/hcl/roles/ingestion_small/local/tables.hcl
index e8eea5383e5b..2a183cc342ba 100644
--- a/posthog/clickhouse/hcl/roles/ingestion_small/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/ingestion_small/local/tables.hcl
@@ -2120,7 +2120,7 @@ SQL
column "_offset" { type = "UInt64" }
column "_partition" { type = "UInt64" }
engine "distributed" {
- cluster_name = "posthog"
+ cluster_name = "aux"
remote_database = "posthog"
remote_table = "sharded_billing_usage_records"
sharding_key = "cityHash64(team_id)"
diff --git a/posthog/clickhouse/hcl/sql/dev/auxiliary.sql b/posthog/clickhouse/hcl/sql/dev/auxiliary.sql
index 465b8e6c870f..3f39538c830d 100644
--- a/posthog/clickhouse/hcl/sql/dev/auxiliary.sql
+++ b/posthog/clickhouse/hcl/sql/dev/auxiliary.sql
@@ -275,6 +275,21 @@ CREATE TABLE posthog.raw_error_tracking_fingerprint_issue_state (
_partition UInt64,
INDEX kafka_timestamp_minmax_raw_error_tracking_fingerprint_issue_state _timestamp TYPE minmax GRANULARITY 3
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.raw_error_tracking_fingerprint_issue_state', '{replica}-{shard}', version) ORDER BY (team_id, fingerprint) SETTINGS index_granularity = 512;
+CREATE TABLE posthog.sharded_billing_usage_records (
+ schema_version UInt8,
+ record_id String,
+ producer_id LowCardinality(String),
+ team_id Int64,
+ organization_id UUID,
+ usage_key LowCardinality(String),
+ unit LowCardinality(String),
+ quantity Int64,
+ timestamp DateTime64(6, 'UTC'),
+ inserted_at DateTime64(6, 'UTC'),
+ _timestamp DateTime,
+ _offset UInt64,
+ _partition UInt64
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records', '{replica}', inserted_at) ORDER BY (team_id, toDate(timestamp), producer_id, usage_key, record_id) PARTITION BY toYYYYMM(timestamp) SETTINGS index_granularity = 8192;
CREATE TABLE posthog.sharded_conversion_goal_attributed_preaggregated (
team_id Int64,
job_id UUID,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sql b/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sql
index b06ee60ab77d..7450d6bf5c30 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sql
@@ -289,6 +289,21 @@ CREATE TABLE posthog.raw_error_tracking_fingerprint_issue_state (
_partition UInt64,
INDEX kafka_timestamp_minmax_raw_error_tracking_fingerprint_issue_state _timestamp TYPE minmax GRANULARITY 3
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.raw_error_tracking_fingerprint_issue_state', '{replica}-{shard}', version) ORDER BY (team_id, fingerprint) SETTINGS index_granularity = 512;
+CREATE TABLE posthog.sharded_billing_usage_records (
+ schema_version UInt8,
+ record_id String,
+ producer_id LowCardinality(String),
+ team_id Int64,
+ organization_id UUID,
+ usage_key LowCardinality(String),
+ unit LowCardinality(String),
+ quantity Int64,
+ timestamp DateTime64(6, 'UTC'),
+ inserted_at DateTime64(6, 'UTC'),
+ _timestamp DateTime,
+ _offset UInt64,
+ _partition UInt64
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records', '{replica}', inserted_at) ORDER BY (team_id, toDate(timestamp), producer_id, usage_key, record_id) PARTITION BY toYYYYMM(timestamp) SETTINGS index_granularity = 8192;
CREATE TABLE posthog.sharded_conversion_goal_attributed_preaggregated (
team_id Int64,
job_id UUID,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/data.sql b/posthog/clickhouse/hcl/sql/local-multi/data.sql
index 8cd2b6e709b5..b5a284eac772 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/data.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/data.sql
@@ -70,6 +70,21 @@ CREATE TABLE posthog.ai_events (
_offset UInt64,
_partition UInt64
) ENGINE = Distributed('ai_events', 'posthog', 'sharded_ai_events', cityHash64(concat(toString(team_id), '-', trace_id, '-', toString(toDate(timestamp)))));
+CREATE TABLE posthog.billing_usage_records (
+ schema_version UInt8,
+ record_id String,
+ producer_id LowCardinality(String),
+ team_id Int64,
+ organization_id UUID,
+ usage_key LowCardinality(String),
+ unit LowCardinality(String),
+ quantity Int64,
+ timestamp DateTime64(6, 'UTC'),
+ inserted_at DateTime64(6, 'UTC'),
+ _timestamp DateTime,
+ _offset UInt64,
+ _partition UInt64
+) ENGINE = Distributed('aux', 'posthog', 'sharded_billing_usage_records', cityHash64(team_id));
CREATE TABLE posthog.channel_definition (
domain String,
kind String,
@@ -924,21 +939,6 @@ CREATE TABLE posthog.sharded_app_metrics2 (
_offset UInt64,
_partition UInt64
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/{shard}/posthog.sharded_app_metrics2', '{replica}') ORDER BY (team_id, app_source, app_source_id, instance_id, toStartOfHour(timestamp), metric_kind, metric_name) PARTITION BY toYYYYMM(timestamp) TTL toDate(timestamp) + toIntervalDay(90) SETTINGS index_granularity = 8192;
-CREATE TABLE posthog.sharded_billing_usage_records (
- schema_version UInt8,
- record_id String,
- producer_id LowCardinality(String),
- team_id Int64,
- organization_id UUID,
- usage_key LowCardinality(String),
- unit LowCardinality(String),
- quantity Int64,
- timestamp DateTime64(6, 'UTC'),
- inserted_at DateTime64(6, 'UTC'),
- _timestamp DateTime,
- _offset UInt64,
- _partition UInt64
-) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records', '{replica}', inserted_at) ORDER BY (team_id, toDate(timestamp), producer_id, usage_key, record_id) PARTITION BY toYYYYMM(timestamp) SETTINGS index_granularity = 8192;
CREATE TABLE posthog.sharded_distinct_id_usage (
team_id Int64,
distinct_id String,
@@ -2933,21 +2933,6 @@ CREATE TABLE posthog.app_metrics2 (
_offset UInt64,
_partition UInt64
) ENGINE = Distributed('posthog', 'posthog', 'sharded_app_metrics2', rand());
-CREATE TABLE posthog.billing_usage_records (
- schema_version UInt8,
- record_id String,
- producer_id LowCardinality(String),
- team_id Int64,
- organization_id UUID,
- usage_key LowCardinality(String),
- unit LowCardinality(String),
- quantity Int64,
- timestamp DateTime64(6, 'UTC'),
- inserted_at DateTime64(6, 'UTC'),
- _timestamp DateTime,
- _offset UInt64,
- _partition UInt64
-) ENGINE = Distributed('posthog', 'posthog', 'sharded_billing_usage_records', cityHash64(team_id));
CREATE TABLE posthog.distinct_id_usage (
team_id Int64,
distinct_id String,
diff --git a/posthog/clickhouse/hcl/sql/local-multi/ingestion_small.sql b/posthog/clickhouse/hcl/sql/local-multi/ingestion_small.sql
index 727f0cd660c8..abd189b57f39 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/ingestion_small.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/ingestion_small.sql
@@ -319,7 +319,7 @@ CREATE TABLE posthog.writable_billing_usage_records (
_timestamp DateTime,
_offset UInt64,
_partition UInt64
-) ENGINE = Distributed('posthog', 'posthog', 'sharded_billing_usage_records', cityHash64(team_id));
+) ENGINE = Distributed('aux', 'posthog', 'sharded_billing_usage_records', cityHash64(team_id));
CREATE TABLE posthog.writable_duplicate_events (
team_id Int64,
distinct_id String,
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 33a1d23428fd..0d20be3fae67 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -3362,7 +3362,7 @@ CREATE TABLE posthog.writable_billing_usage_records (
_timestamp DateTime,
_offset UInt64,
_partition UInt64
-) ENGINE = Distributed('posthog', 'posthog', 'sharded_billing_usage_records', cityHash64(team_id));
+) ENGINE = Distributed('aux', 'posthog', 'sharded_billing_usage_records', cityHash64(team_id));
CREATE TABLE posthog.writable_cohort_membership (
team_id Int64,
cohort_id Int64,
@@ -5743,7 +5743,7 @@ CREATE TABLE posthog.billing_usage_records (
_timestamp DateTime,
_offset UInt64,
_partition UInt64
-) ENGINE = Distributed('posthog', 'posthog', 'sharded_billing_usage_records', cityHash64(team_id));
+) ENGINE = Distributed('aux', 'posthog', 'sharded_billing_usage_records', cityHash64(team_id));
CREATE TABLE posthog.conversion_goal_attributed_preaggregated (
team_id Int64,
job_id UUID,
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sql b/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sql
index b8a095ef8ec5..0517a4cc7823 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sql
@@ -275,6 +275,21 @@ CREATE TABLE posthog.raw_error_tracking_fingerprint_issue_state (
_partition UInt64,
INDEX kafka_timestamp_minmax_raw_error_tracking_fingerprint_issue_state _timestamp TYPE minmax GRANULARITY 3
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.raw_error_tracking_fingerprint_issue_state', '{replica}-{shard}', version) ORDER BY (team_id, fingerprint) SETTINGS index_granularity = 512;
+CREATE TABLE posthog.sharded_billing_usage_records (
+ schema_version UInt8,
+ record_id String,
+ producer_id LowCardinality(String),
+ team_id Int64,
+ organization_id UUID,
+ usage_key LowCardinality(String),
+ unit LowCardinality(String),
+ quantity Int64,
+ timestamp DateTime64(6, 'UTC'),
+ inserted_at DateTime64(6, 'UTC'),
+ _timestamp DateTime,
+ _offset UInt64,
+ _partition UInt64
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records', '{replica}', inserted_at) ORDER BY (team_id, toDate(timestamp), producer_id, usage_key, record_id) PARTITION BY toYYYYMM(timestamp) SETTINGS index_granularity = 8192;
CREATE TABLE posthog.sharded_conversion_goal_attributed_preaggregated (
team_id Int64,
job_id UUID,
diff --git a/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sql b/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sql
index 8454c8756b88..bfca76533082 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sql
@@ -275,6 +275,21 @@ CREATE TABLE posthog.raw_error_tracking_fingerprint_issue_state (
_partition UInt64,
INDEX kafka_timestamp_minmax_raw_error_tracking_fingerprint_issue_state _timestamp TYPE minmax GRANULARITY 3
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/noshard/posthog.raw_error_tracking_fingerprint_issue_state', '{replica}-{shard}', version) ORDER BY (team_id, fingerprint) SETTINGS index_granularity = 512;
+CREATE TABLE posthog.sharded_billing_usage_records (
+ schema_version UInt8,
+ record_id String,
+ producer_id LowCardinality(String),
+ team_id Int64,
+ organization_id UUID,
+ usage_key LowCardinality(String),
+ unit LowCardinality(String),
+ quantity Int64,
+ timestamp DateTime64(6, 'UTC'),
+ inserted_at DateTime64(6, 'UTC'),
+ _timestamp DateTime,
+ _offset UInt64,
+ _partition UInt64
+) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.sharded_billing_usage_records', '{replica}', inserted_at) ORDER BY (team_id, toDate(timestamp), producer_id, usage_key, record_id) PARTITION BY toYYYYMM(timestamp) SETTINGS index_granularity = 8192;
CREATE TABLE posthog.sharded_conversion_goal_attributed_preaggregated (
team_id Int64,
job_id UUID,
diff --git a/posthog/clickhouse/migrations/0303_billing_usage_records.py b/posthog/clickhouse/migrations/0303_billing_usage_records.py
index 7dbbc5b88d12..6d2e2dd4eada 100644
--- a/posthog/clickhouse/migrations/0303_billing_usage_records.py
+++ b/posthog/clickhouse/migrations/0303_billing_usage_records.py
@@ -9,7 +9,7 @@ from posthog.models.usage_ingestion.billing_usage_records import (
)
operations = [
- run_sql_with_exceptions(BILLING_USAGE_RECORDS_DATA_TABLE_SQL(), node_roles=[NodeRole.DATA]),
+ run_sql_with_exceptions(BILLING_USAGE_RECORDS_DATA_TABLE_SQL(), node_roles=[NodeRole.AUX]),
run_sql_with_exceptions(DISTRIBUTED_BILLING_USAGE_RECORDS_TABLE_SQL(), node_roles=[NodeRole.DATA]),
run_sql_with_exceptions(WRITABLE_BILLING_USAGE_RECORDS_TABLE_SQL(), node_roles=[NodeRole.INGESTION_SMALL]),
run_sql_with_exceptions(KAFKA_BILLING_USAGE_RECORDS_TABLE_SQL(), node_roles=[NodeRole.INGESTION_SMALL]),
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index b3659c878d1d..48436c80fc4d 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -1166,7 +1166,7 @@
, _partition UInt64
)
- ENGINE = Distributed('posthog', 'posthog_test', 'sharded_billing_usage_records', cityHash64(team_id))
+ ENGINE = Distributed('aux', 'posthog_test', 'sharded_billing_usage_records', cityHash64(team_id))
'''
# ---
@@ -9108,7 +9108,7 @@
, _partition UInt64
)
- ENGINE = Distributed('posthog', 'posthog_test', 'sharded_billing_usage_records', cityHash64(team_id))
+ ENGINE = Distributed('aux', 'posthog_test', 'sharded_billing_usage_records', cityHash64(team_id))
'''
# ---
diff --git a/posthog/models/usage_ingestion/billing_usage_records.py b/posthog/models/usage_ingestion/billing_usage_records.py
index a35555f83edf..a1dc8310b5bd 100644
--- a/posthog/models/usage_ingestion/billing_usage_records.py
+++ b/posthog/models/usage_ingestion/billing_usage_records.py
@@ -64,7 +64,13 @@ CREATE TABLE IF NOT EXISTS {BILLING_USAGE_RECORDS_TABLE}
{BASE_BILLING_USAGE_RECORDS_COLUMNS}
{KAFKA_COLUMNS_WITH_PARTITION}
)
-ENGINE = {Distributed(data_table=SHARDED_BILLING_USAGE_RECORDS_TABLE, sharding_key="cityHash64(team_id)")}
+ENGINE = {
+ Distributed(
+ data_table=SHARDED_BILLING_USAGE_RECORDS_TABLE,
+ sharding_key="cityHash64(team_id)",
+ cluster=settings.CLICKHOUSE_AUX_CLUSTER,
+ )
+ }
"""
@@ -75,7 +81,13 @@ CREATE TABLE IF NOT EXISTS {WRITABLE_BILLING_USAGE_RECORDS_TABLE}
{BASE_BILLING_USAGE_RECORDS_COLUMNS}
{KAFKA_COLUMNS_WITH_PARTITION}
)
-ENGINE = {Distributed(data_table=SHARDED_BILLING_USAGE_RECORDS_TABLE, sharding_key="cityHash64(team_id)")}
+ENGINE = {
+ Distributed(
+ data_table=SHARDED_BILLING_USAGE_RECORDS_TABLE,
+ sharding_key="cityHash64(team_id)",
+ cluster=settings.CLICKHOUSE_AUX_CLUSTER,
+ )
+ }
"""
diff --git a/rust/usage-ingestion/README.md b/rust/usage-ingestion/README.md
index 3016253b31e9..50ab22c8e813 100644
--- a/rust/usage-ingestion/README.md
+++ b/rust/usage-ingestion/README.md
@@ -69,7 +69,8 @@ still rejects an intentionally cross-slot transaction.
| Kafka cluster | `warpstream-shared` |
| Topic | `clickhouse_billing_usage_records`, 8 partitions, 7-day retention |
| ClickHouse Kafka table and MV | `NodeRole.INGESTION_SMALL` |
-| ClickHouse storage and read tables | `NodeRole.DATA` |
+| ClickHouse storage table | `NodeRole.AUX` |
+| ClickHouse read table | `NodeRole.DATA` |
| Reachability | in-cluster only; no external proxy and no request authentication |
| Owning team | `team-ingestion` |