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(usage): add the billing_usage_records clickhouse pipeline (#86960)

Ben White6e4a199d466b
diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml
index 66a0ded4ade5..b71e7b96ca81 100644
--- a/docker-compose.dev.yml
+++ b/docker-compose.dev.yml
@@ -593,7 +593,8 @@ services:
clickhouse_app_metrics2 \
log_entries \
clickhouse_property_values \
- clickhouse_tophog"
+ clickhouse_tophog \
+ clickhouse_billing_usage_records"
for topic in $$TOPICS; do
if rpk topic create "$$topic" --brokers kafka:9092 -p 1 -r 1 2>&1; then
echo "Topic $$topic created"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/data.hcl b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
index 9ae7977ec6ba..a0c2a02e34a4 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/data.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
@@ -315,6 +315,54 @@ database "posthog" {
}
}
+ table "billing_usage_records" {
+ 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 "distributed" {
+ cluster_name = "posthog"
+ remote_database = "posthog"
+ remote_table = "sharded_billing_usage_records"
+ sharding_key = "cityHash64(team_id)"
+ }
+ }
+
table "channel_definition" {
order_by = ["domain", "kind"]
settings = {
@@ -4914,6 +4962,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_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 c4df7bbad469..b8ee96497215 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/ingestion_small.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/ingestion_small.hcl
@@ -51,6 +51,48 @@ database "posthog" {
}
}
+ table "kafka_billing_usage_records" {
+ settings = {
+ date_time_input_format = "best_effort"
+ }
+ 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')"
+ }
+ engine "kafka" {
+ broker_list = "warpstream_ingestion"
+ topic_list = "kafka_topic_list = 'clickhouse_billing_usage_records'"
+ group_name = "kafka_group_name = 'clickhouse_billing_usage_records'"
+ format = "kafka_format = 'JSONEachRow'"
+ }
+ }
+
table "kafka_duplicate_events" {
column "team_id" {
type = "Int64"
@@ -1009,6 +1051,54 @@ database "posthog" {
}
}
+ table "writable_billing_usage_records" {
+ 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 "distributed" {
+ cluster_name = "posthog"
+ remote_database = "posthog"
+ remote_table = "sharded_billing_usage_records"
+ sharding_key = "cityHash64(team_id)"
+ }
+ }
+
table "writable_duplicate_events" {
column "team_id" {
type = "Int64"
@@ -1744,6 +1834,67 @@ SQL
}
}
+ materialized_view "billing_usage_records_mv" {
+ to_table = "posthog.writable_billing_usage_records"
+ query = <<SQL
+SELECT
+ schema_version,
+ record_id,
+ producer_id,
+ team_id,
+ organization_id,
+ usage_key,
+ unit,
+ quantity,
+ timestamp,
+ inserted_at,
+ _timestamp,
+ _offset,
+ _partition
+FROM posthog.kafka_billing_usage_records
+SQL
+
+ 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"
+ }
+ }
+
materialized_view "duplicate_events_mv" {
to_table = "posthog.writable_duplicate_events"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index 337b05128426..812e7b8f5fa4 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -342,6 +342,54 @@ database "posthog" {
}
}
+ table "billing_usage_records" {
+ 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 "distributed" {
+ cluster_name = "posthog"
+ remote_database = "posthog"
+ remote_table = "sharded_billing_usage_records"
+ sharding_key = "cityHash64(team_id)"
+ }
+ }
+
table "channel_definition" {
order_by = ["domain", "kind"]
settings = {
@@ -2465,6 +2513,48 @@ database "posthog" {
}
}
+ table "kafka_billing_usage_records" {
+ settings = {
+ date_time_input_format = "best_effort"
+ }
+ 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')"
+ }
+ engine "kafka" {
+ broker_list = "warpstream_ingestion"
+ topic_list = "kafka_topic_list = 'clickhouse_billing_usage_records'"
+ group_name = "kafka_group_name = 'clickhouse_billing_usage_records'"
+ format = "kafka_format = 'JSONEachRow'"
+ }
+ }
+
table "kafka_cohort_membership" {
column "team_id" {
type = "Int64"
@@ -8702,6 +8792,58 @@ SQL
}
}
+ 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)"
@@ -14729,6 +14871,54 @@ SQL
}
}
+ table "writable_billing_usage_records" {
+ 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 "distributed" {
+ cluster_name = "posthog"
+ remote_database = "posthog"
+ remote_table = "sharded_billing_usage_records"
+ sharding_key = "cityHash64(team_id)"
+ }
+ }
+
table "writable_cohort_membership" {
column "team_id" {
type = "Int64"
@@ -17511,6 +17701,67 @@ SQL
}
}
+ materialized_view "billing_usage_records_mv" {
+ to_table = "posthog.writable_billing_usage_records"
+ query = <<SQL
+SELECT
+ schema_version,
+ record_id,
+ producer_id,
+ team_id,
+ organization_id,
+ usage_key,
+ unit,
+ quantity,
+ timestamp,
+ inserted_at,
+ _timestamp,
+ _offset,
+ _partition
+FROM posthog.kafka_billing_usage_records
+SQL
+
+ 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"
+ }
+ }
+
materialized_view "cohort_membership_mv" {
to_table = "posthog.writable_cohort_membership"
query = <<SQL
diff --git a/posthog/clickhouse/hcl/roles/data/local/tables.hcl b/posthog/clickhouse/hcl/roles/data/local/tables.hcl
index b942186ec3e4..6e94e54a464d 100644
--- a/posthog/clickhouse/hcl/roles/data/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/data/local/tables.hcl
@@ -7865,4 +7865,51 @@ 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" }
+ 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 "distributed" {
+ cluster_name = "posthog"
+ 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 ffd7ff26ca57..0367a207eb68 100644
--- a/posthog/clickhouse/hcl/roles/ingestion_small/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/ingestion_small/local/tables.hcl
@@ -2104,4 +2104,81 @@ SQL
type = "AggregateFunction(sum, UInt64)"
}
}
+
+ table "writable_billing_usage_records" {
+ 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 "distributed" {
+ cluster_name = "posthog"
+ remote_database = "posthog"
+ remote_table = "sharded_billing_usage_records"
+ sharding_key = "cityHash64(team_id)"
+ }
+ }
+
+ table "kafka_billing_usage_records" {
+ settings = { date_time_input_format = "best_effort" }
+ 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')" }
+ engine "kafka" {
+ broker_list = "warpstream_ingestion"
+ topic_list = "kafka_topic_list = 'clickhouse_billing_usage_records'"
+ group_name = "kafka_group_name = 'clickhouse_billing_usage_records'"
+ format = "kafka_format = 'JSONEachRow'"
+ }
+ }
+
+ materialized_view "billing_usage_records_mv" {
+ to_table = "posthog.writable_billing_usage_records"
+ query = <<SQL
+SELECT
+ schema_version,
+ record_id,
+ producer_id,
+ team_id,
+ organization_id,
+ usage_key,
+ unit,
+ quantity,
+ timestamp,
+ inserted_at,
+ _timestamp,
+ _offset,
+ _partition
+FROM posthog.kafka_billing_usage_records
+SQL
+
+ 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" }
+ }
}
diff --git a/posthog/clickhouse/hcl/sql/local-multi/data.sql b/posthog/clickhouse/hcl/sql/local-multi/data.sql
index 5d121f46f4fa..78031efaa7be 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/data.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/data.sql
@@ -896,6 +896,21 @@ 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,
@@ -2890,6 +2905,21 @@ 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 a040739b82aa..b43f89083495 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/ingestion_small.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/ingestion_small.sql
@@ -14,6 +14,18 @@ CREATE TABLE posthog.kafka_app_metrics (
error_type String,
error_details String CODEC(ZSTD(3))
) ENGINE = Kafka() SETTINGS kafka_broker_list = 'msk_cluster', kafka_format = 'kafka_format = \'JSONEachRow\'', kafka_group_name = 'kafka_group_name = \'group1\'', kafka_topic_list = 'kafka_topic_list = \'clickhouse_app_metrics\'';
+CREATE TABLE posthog.kafka_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')
+) ENGINE = Kafka() SETTINGS date_time_input_format = 'best_effort', kafka_broker_list = 'warpstream_ingestion', kafka_format = 'kafka_format = \'JSONEachRow\'', kafka_group_name = 'kafka_group_name = \'clickhouse_billing_usage_records\'', kafka_topic_list = 'kafka_topic_list = \'clickhouse_billing_usage_records\'';
CREATE TABLE posthog.kafka_duplicate_events (
team_id Int64,
distinct_id String,
@@ -293,6 +305,21 @@ CREATE TABLE posthog.writable_app_metrics (
_offset UInt64,
_partition UInt64
) ENGINE = Distributed('posthog', 'posthog', 'sharded_app_metrics', rand());
+CREATE TABLE posthog.writable_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.writable_duplicate_events (
team_id Int64,
distinct_id String,
@@ -518,6 +545,21 @@ CREATE MATERIALIZED VIEW posthog.app_metrics_mv TO posthog.writable_app_metrics
_offset,
_partition
FROM posthog.kafka_app_metrics;
+CREATE MATERIALIZED VIEW posthog.billing_usage_records_mv TO posthog.writable_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) AS SELECT
+ schema_version,
+ record_id,
+ producer_id,
+ team_id,
+ organization_id,
+ usage_key,
+ unit,
+ quantity,
+ timestamp,
+ inserted_at,
+ _timestamp,
+ _offset,
+ _partition
+FROM posthog.kafka_billing_usage_records;
CREATE MATERIALIZED VIEW posthog.duplicate_events_mv TO posthog.writable_duplicate_events (team_id Int64, distinct_id String, event String, source_uuid UUID, duplicate_uuid UUID, similarity_score Float64, dedup_type LowCardinality(String), is_confirmed UInt8, reason Nullable(String), version String, different_property_count UInt32, properties_similarity Float64, source_message String, duplicate_message String, distinct_fields Array(Tuple(field_name String, original_value String, new_value String)), inserted_at DateTime64(3, 'UTC'), _timestamp Nullable(DateTime), _offset UInt64, _partition UInt64) AS SELECT
team_id,
distinct_id,
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index feb5f0bf061b..0657258e9e17 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -277,6 +277,18 @@ CREATE TABLE posthog.kafka_app_metrics2_ws (
metric_name String,
count Int64
) ENGINE = Kafka() SETTINGS kafka_broker_list = 'warpstream_ingestion', kafka_format = 'kafka_format = \'JSONEachRow\'', kafka_group_name = 'kafka_group_name = \'clickhouse_app_metrics2_ws\'', kafka_topic_list = 'kafka_topic_list = \'clickhouse_app_metrics2\'';
+CREATE TABLE posthog.kafka_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')
+) ENGINE = Kafka() SETTINGS date_time_input_format = 'best_effort', kafka_broker_list = 'warpstream_ingestion', kafka_format = 'kafka_format = \'JSONEachRow\'', kafka_group_name = 'kafka_group_name = \'clickhouse_billing_usage_records\'', kafka_topic_list = 'kafka_topic_list = \'clickhouse_billing_usage_records\'';
CREATE TABLE posthog.kafka_cohort_membership (
team_id Int64,
cohort_id Int64,
@@ -1496,6 +1508,21 @@ 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_conversion_goal_attributed_preaggregated (
team_id Int64,
job_id UUID,
@@ -3178,6 +3205,21 @@ CREATE TABLE posthog.writable_app_metrics2 (
_offset UInt64,
_partition UInt64
) ENGINE = Distributed('posthog', 'posthog', 'sharded_app_metrics2', rand());
+CREATE TABLE posthog.writable_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.writable_cohort_membership (
team_id Int64,
cohort_id Int64,
@@ -3928,6 +3970,21 @@ CREATE MATERIALIZED VIEW posthog.app_metrics_mv TO posthog.writable_app_metrics
_offset,
_partition
FROM posthog.kafka_app_metrics;
+CREATE MATERIALIZED VIEW posthog.billing_usage_records_mv TO posthog.writable_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) AS SELECT
+ schema_version,
+ record_id,
+ producer_id,
+ team_id,
+ organization_id,
+ usage_key,
+ unit,
+ quantity,
+ timestamp,
+ inserted_at,
+ _timestamp,
+ _offset,
+ _partition
+FROM posthog.kafka_billing_usage_records;
CREATE MATERIALIZED VIEW posthog.cohort_membership_mv TO posthog.writable_cohort_membership (team_id Int64, cohort_id Int64, person_id UUID, status String, last_updated DateTime64(6)) AS SELECT
team_id,
cohort_id,
@@ -5423,6 +5480,21 @@ 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.conversion_goal_attributed_preaggregated (
team_id Int64,
job_id UUID,
diff --git a/posthog/clickhouse/kafka_engine.py b/posthog/clickhouse/kafka_engine.py
index 5b446e229a73..9687c0d5ac54 100644
--- a/posthog/clickhouse/kafka_engine.py
+++ b/posthog/clickhouse/kafka_engine.py
@@ -27,6 +27,7 @@ CONSUMER_GROUP_PRECALCULATED_PERSON_PROPERTIES = (
)
CONSUMER_GROUP_DISTINCT_ID_USAGE = "clickhouse_distinct_id_usage"
CONSUMER_GROUP_TOPHOG = "clickhouse_tophog"
+CONSUMER_GROUP_BILLING_USAGE_RECORDS = "clickhouse_billing_usage_records"
CONSUMER_GROUP_AI_EVENTS = "clickhouse_ai_events" if _US else "group1"
CONSUMER_GROUP_PROPERTY_VALUES = "clickhouse_property_values"
CONSUMER_GROUP_FLAG_EVALUATIONS = "clickhouse_flag_evaluations"
diff --git a/posthog/clickhouse/migrations/0303_billing_usage_records.py b/posthog/clickhouse/migrations/0303_billing_usage_records.py
new file mode 100644
index 000000000000..7dbbc5b88d12
--- /dev/null
+++ b/posthog/clickhouse/migrations/0303_billing_usage_records.py
@@ -0,0 +1,17 @@
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.models.usage_ingestion.billing_usage_records import (
+ BILLING_USAGE_RECORDS_DATA_TABLE_SQL,
+ BILLING_USAGE_RECORDS_MV_SQL,
+ DISTRIBUTED_BILLING_USAGE_RECORDS_TABLE_SQL,
+ KAFKA_BILLING_USAGE_RECORDS_TABLE_SQL,
+ WRITABLE_BILLING_USAGE_RECORDS_TABLE_SQL,
+)
+
+operations = [
+ run_sql_with_exceptions(BILLING_USAGE_RECORDS_DATA_TABLE_SQL(), node_roles=[NodeRole.DATA]),
+ 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]),
+ run_sql_with_exceptions(BILLING_USAGE_RECORDS_MV_SQL(), node_roles=[NodeRole.INGESTION_SMALL]),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 4d7f45feb63e..c0055ff4c3fe 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0302_logs34_to_volume_buckets_mv
+0303_billing_usage_records
diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.py
index df700d71e407..335b39129c12 100644
--- a/posthog/clickhouse/schema.py
+++ b/posthog/clickhouse/schema.py
@@ -311,6 +311,13 @@ from posthog.models.tophog.sql import (
TOPHOG_MV_SQL,
WRITABLE_TOPHOG_TABLE_SQL,
)
+from posthog.models.usage_ingestion.billing_usage_records import (
+ BILLING_USAGE_RECORDS_DATA_TABLE_SQL,
+ BILLING_USAGE_RECORDS_MV_SQL,
+ DISTRIBUTED_BILLING_USAGE_RECORDS_TABLE_SQL,
+ KAFKA_BILLING_USAGE_RECORDS_TABLE_SQL,
+ WRITABLE_BILLING_USAGE_RECORDS_TABLE_SQL,
+)
from posthog.models.web_preaggregated.sql import WEB_BOUNCES_SQL, WEB_STATS_SQL
from posthog.session_recordings.sql.session_replay_event_sql import (
DISTRIBUTED_SESSION_REPLAY_EVENTS_TABLE_SQL,
@@ -433,6 +440,7 @@ CREATE_MERGETREE_TABLE_QUERIES = (
METRIC_SAMPLES_TABLE_SQL,
METRIC_ATTRIBUTES_TABLE_SQL,
TOPHOG_DATA_TABLE_SQL,
+ BILLING_USAGE_RECORDS_DATA_TABLE_SQL,
AI_EVENTS_DATA_TABLE_SQL,
PROPERTY_VALUES_TABLE_SQL,
DMAT_SLOT_ASSIGNMENTS_TABLE_SQL,
@@ -519,6 +527,8 @@ CREATE_DISTRIBUTED_TABLE_QUERIES = (
METRIC_SAMPLES_DISTRIBUTED_TABLE_SQL,
WRITABLE_TOPHOG_TABLE_SQL,
DISTRIBUTED_TOPHOG_TABLE_SQL,
+ DISTRIBUTED_BILLING_USAGE_RECORDS_TABLE_SQL,
+ WRITABLE_BILLING_USAGE_RECORDS_TABLE_SQL,
DISTRIBUTED_AI_EVENTS_TABLE_SQL,
DISTRIBUTED_PROPERTY_VALUES_TABLE_SQL,
)
@@ -553,6 +563,7 @@ CREATE_KAFKA_TABLE_QUERIES = (
KAFKA_COHORT_MEMBERSHIP_TABLE_SQL,
KAFKA_DISTINCT_ID_USAGE_TABLE_SQL,
KAFKA_TOPHOG_TABLE_SQL,
+ KAFKA_BILLING_USAGE_RECORDS_TABLE_SQL,
KAFKA_AI_EVENTS_TABLE_SQL,
KAFKA_PROPERTY_VALUES_TABLE_SQL_FN,
)
@@ -604,6 +615,7 @@ CREATE_MV_TABLE_QUERIES = (
METRIC_ATTRIBUTES_MV,
METRIC_RESOURCE_ATTRIBUTES_MV,
TOPHOG_MV_SQL,
+ BILLING_USAGE_RECORDS_MV_SQL,
AI_EVENTS_MV_SQL,
PROPERTY_VALUES_MV_SQL,
)
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index ca18dca88ef1..8d9e1baabdd8 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -106,6 +106,27 @@
'''
# ---
+# name: test_create_kafka_table_with_different_kafka_host[kafka_billing_usage_records]
+ '''
+
+ CREATE TABLE IF NOT EXISTS kafka_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')
+ )
+ ENGINE = Kafka(warpstream_shared, kafka_topic_list = 'clickhouse_billing_usage_records_test', kafka_group_name = 'clickhouse_billing_usage_records', kafka_format = 'JSONEachRow')
+ SETTINGS date_time_input_format = 'best_effort'
+
+ '''
+# ---
# name: test_create_kafka_table_with_different_kafka_host[kafka_cohort_membership]
'''
@@ -1124,6 +1145,54 @@
'''
# ---
+# name: test_create_table_query[billing_usage_records]
+ '''
+
+ CREATE TABLE IF NOT EXISTS 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_test', 'sharded_billing_usage_records', cityHash64(team_id))
+
+ '''
+# ---
+# name: test_create_table_query[billing_usage_records_mv]
+ '''
+
+ CREATE MATERIALIZED VIEW IF NOT EXISTS billing_usage_records_mv
+ TO writable_billing_usage_records
+ AS SELECT
+ schema_version,
+ record_id,
+ producer_id,
+ team_id,
+ organization_id,
+ usage_key,
+ unit,
+ quantity,
+ timestamp,
+ inserted_at,
+ _timestamp,
+ _offset,
+ _partition
+ FROM kafka_billing_usage_records
+
+ '''
+# ---
# name: test_create_table_query[channel_definition]
'''
@@ -2341,6 +2410,27 @@
'''
# ---
+# name: test_create_table_query[kafka_billing_usage_records]
+ '''
+
+ CREATE TABLE IF NOT EXISTS kafka_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')
+ )
+ ENGINE = Kafka(warpstream_shared, kafka_topic_list = 'clickhouse_billing_usage_records_test', kafka_group_name = 'clickhouse_billing_usage_records', kafka_format = 'JSONEachRow')
+ SETTINGS date_time_input_format = 'best_effort'
+
+ '''
+# ---
# name: test_create_table_query[kafka_cohort_membership]
'''
@@ -6534,6 +6624,33 @@
'''
# ---
+# name: test_create_table_query[sharded_billing_usage_records]
+ '''
+
+ CREATE TABLE IF NOT EXISTS 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/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_{shard}/posthog.sharded_billing_usage_records', '{replica}', inserted_at)
+ PARTITION BY toYYYYMM(timestamp)
+ ORDER BY (team_id, toDate(timestamp), producer_id, usage_key, record_id)
+
+ '''
+# ---
# name: test_create_table_query[sharded_conversion_goal_attributed_preaggregated]
'''
@@ -9033,6 +9150,31 @@
'''
# ---
+# name: test_create_table_query[writable_billing_usage_records]
+ '''
+
+ CREATE TABLE IF NOT EXISTS writable_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_test', 'sharded_billing_usage_records', cityHash64(team_id))
+
+ '''
+# ---
# name: test_create_table_query[writable_cohort_membership]
'''
@@ -11509,6 +11651,33 @@
'''
# ---
+# name: test_create_table_query_replicated_and_storage[sharded_billing_usage_records]
+ '''
+
+ CREATE TABLE IF NOT EXISTS 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/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_{shard}/posthog.sharded_billing_usage_records', '{replica}', inserted_at)
+ PARTITION BY toYYYYMM(timestamp)
+ ORDER BY (team_id, toDate(timestamp), producer_id, usage_key, record_id)
+
+ '''
+# ---
# name: test_create_table_query_replicated_and_storage[sharded_conversion_goal_attributed_preaggregated]
'''
diff --git a/posthog/kafka_client/topics.py b/posthog/kafka_client/topics.py
index 751d5daa9045..98bdbb42f8ab 100644
--- a/posthog/kafka_client/topics.py
+++ b/posthog/kafka_client/topics.py
@@ -69,6 +69,7 @@ KAFKA_WAREHOUSE_SOURCE_WEBHOOKS = f"{KAFKA_PREFIX}data_warehouse_source_webhooks
KAFKA_WAREHOUSE_SOURCE_WEBHOOKS_DLQ = f"{KAFKA_PREFIX}data_warehouse_source_webhooks_dlq{SUFFIX}"
KAFKA_CLICKHOUSE_TOPHOG = f"{KAFKA_PREFIX}clickhouse_tophog{SUFFIX}"
+KAFKA_BILLING_USAGE_RECORDS = f"{KAFKA_PREFIX}clickhouse_billing_usage_records{SUFFIX}"
# Distinct ID usage - pre-processed by WarpStream pipeline from clickhouse_events_json
KAFKA_DISTINCT_ID_USAGE_EVENTS_JSON = f"{KAFKA_PREFIX}distinct_id_usage_events_json{SUFFIX}"
diff --git a/posthog/models/usage_ingestion/__init__.py b/posthog/models/usage_ingestion/__init__.py
new file mode 100644
index 000000000000..49fa4677339f
--- /dev/null
+++ b/posthog/models/usage_ingestion/__init__.py
@@ -0,0 +1 @@
+"""ClickHouse storage for canonical usage records."""
diff --git a/posthog/models/usage_ingestion/billing_usage_records.py b/posthog/models/usage_ingestion/billing_usage_records.py
new file mode 100644
index 000000000000..a35555f83edf
--- /dev/null
+++ b/posthog/models/usage_ingestion/billing_usage_records.py
@@ -0,0 +1,118 @@
+from django.conf import settings
+
+from posthog.clickhouse.kafka_engine import (
+ CONSUMER_GROUP_BILLING_USAGE_RECORDS,
+ KAFKA_COLUMNS_WITH_PARTITION,
+ kafka_engine,
+)
+from posthog.clickhouse.table_engines import Distributed, ReplacingMergeTree, ReplicationScheme
+from posthog.kafka_client.topics import KAFKA_BILLING_USAGE_RECORDS
+
+BILLING_USAGE_RECORDS_TABLE = "billing_usage_records"
+SHARDED_BILLING_USAGE_RECORDS_TABLE = f"sharded_{BILLING_USAGE_RECORDS_TABLE}"
+WRITABLE_BILLING_USAGE_RECORDS_TABLE = f"writable_{BILLING_USAGE_RECORDS_TABLE}"
+KAFKA_BILLING_USAGE_RECORDS_TABLE = f"kafka_{BILLING_USAGE_RECORDS_TABLE}"
+BILLING_USAGE_RECORDS_MV = f"{BILLING_USAGE_RECORDS_TABLE}_mv"
+
+
+# inserted_at is the version column, so the latest send of an identity wins, which is also
+# how a producer corrects a quantity. It is deliberately absent from the HogQL schema:
+# `timestamp` is monotonic per resend, so argMax on it reads what a merge would keep, and
+# exposing two time columns invites filtering on the one that is not in the sorting key.
+def billing_usage_records_data_table_engine() -> ReplacingMergeTree:
+ return ReplacingMergeTree(
+ SHARDED_BILLING_USAGE_RECORDS_TABLE,
+ replication_scheme=ReplicationScheme.SHARDED,
+ ver="inserted_at",
+ )
+
+
+# Every producer stamps `timestamp` from its own clock when it flushes, never from anything a
+# customer sends, which is what lets toDate(timestamp) sit in the sorting key. Sourcing it from
+# event data would hand a customer control over whether their records deduplicate.
+BASE_BILLING_USAGE_RECORDS_COLUMNS = """
+ 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')
+""".strip()
+
+
+def BILLING_USAGE_RECORDS_DATA_TABLE_SQL() -> str:
+ return f"""
+CREATE TABLE IF NOT EXISTS {SHARDED_BILLING_USAGE_RECORDS_TABLE}
+(
+ {BASE_BILLING_USAGE_RECORDS_COLUMNS}
+ {KAFKA_COLUMNS_WITH_PARTITION}
+)
+ENGINE = {billing_usage_records_data_table_engine()}
+PARTITION BY toYYYYMM(timestamp)
+ORDER BY (team_id, toDate(timestamp), producer_id, usage_key, record_id)
+"""
+
+
+def DISTRIBUTED_BILLING_USAGE_RECORDS_TABLE_SQL() -> str:
+ return f"""
+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)")}
+"""
+
+
+def WRITABLE_BILLING_USAGE_RECORDS_TABLE_SQL() -> str:
+ return f"""
+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)")}
+"""
+
+
+def KAFKA_BILLING_USAGE_RECORDS_TABLE_SQL() -> str:
+ return f"""
+CREATE TABLE IF NOT EXISTS {KAFKA_BILLING_USAGE_RECORDS_TABLE}
+(
+ {BASE_BILLING_USAGE_RECORDS_COLUMNS}
+)
+ENGINE = {
+ kafka_engine(
+ topic=KAFKA_BILLING_USAGE_RECORDS,
+ group=CONSUMER_GROUP_BILLING_USAGE_RECORDS,
+ named_collection=settings.CLICKHOUSE_KAFKA_WARPSTREAM_SHARED_NAMED_COLLECTION,
+ )
+ }
+SETTINGS date_time_input_format = 'best_effort'
+"""
+
+
+def BILLING_USAGE_RECORDS_MV_SQL(target_table: str = WRITABLE_BILLING_USAGE_RECORDS_TABLE) -> str:
+ return f"""
+CREATE MATERIALIZED VIEW IF NOT EXISTS {BILLING_USAGE_RECORDS_MV}
+TO {target_table}
+AS SELECT
+ schema_version,
+ record_id,
+ producer_id,
+ team_id,
+ organization_id,
+ usage_key,
+ unit,
+ quantity,
+ timestamp,
+ inserted_at,
+ _timestamp,
+ _offset,
+ _partition
+FROM {KAFKA_BILLING_USAGE_RECORDS_TABLE}
+"""