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(clickhouse): align inserted_at nullability on events_recent proxies (#69326)

Paweł Szczur0fb5f8376d33
diff --git a/posthog/clickhouse/hcl/golden/local-data.hcl b/posthog/clickhouse/hcl/golden/local-data.hcl
index 4823226595d7..a421d9aca14a 100644
--- a/posthog/clickhouse/hcl/golden/local-data.hcl
+++ b/posthog/clickhouse/hcl/golden/local-data.hcl
@@ -566,7 +566,7 @@ database "posthog" {
type = "UInt64"
}
column "inserted_at" {
- type = "Nullable(DateTime64(6, 'UTC'))"
+ type = "DateTime64(6, 'UTC')"
default = "now64()"
}
engine "distributed" {
@@ -1415,7 +1415,7 @@ database "posthog" {
type = "UInt64"
}
column "inserted_at" {
- type = "Nullable(DateTime64(6, 'UTC'))"
+ type = "DateTime64(6, 'UTC')"
default = "now64()"
}
engine "distributed" {
diff --git a/posthog/clickhouse/hcl/roles/data/local/tables.hcl b/posthog/clickhouse/hcl/roles/data/local/tables.hcl
index 029105a168d0..d9610b69aa9f 100644
--- a/posthog/clickhouse/hcl/roles/data/local/tables.hcl
+++ b/posthog/clickhouse/hcl/roles/data/local/tables.hcl
@@ -557,7 +557,7 @@ database "posthog" {
type = "UInt64"
}
column "inserted_at" {
- type = "Nullable(DateTime64(6, 'UTC'))"
+ type = "DateTime64(6, 'UTC')"
default = "now64()"
}
engine "distributed" {
@@ -1396,7 +1396,7 @@ database "posthog" {
type = "UInt64"
}
column "inserted_at" {
- type = "Nullable(DateTime64(6, 'UTC'))"
+ type = "DateTime64(6, 'UTC')"
default = "now64()"
}
engine "distributed" {
diff --git a/posthog/clickhouse/hcl/sql/local-data.sql b/posthog/clickhouse/hcl/sql/local-data.sql
index 9d80926c4773..7b75d9b79a0f 100644
--- a/posthog/clickhouse/hcl/sql/local-data.sql
+++ b/posthog/clickhouse/hcl/sql/local-data.sql
@@ -2719,7 +2719,7 @@ CREATE TABLE posthog.distributed_events_recent (
historical_migration Bool,
_timestamp DateTime,
_offset UInt64,
- inserted_at Nullable(DateTime64(6, 'UTC')) DEFAULT now64()
+ inserted_at DateTime64(6, 'UTC') DEFAULT now64()
) ENGINE = Distributed('posthog_primary_replica', 'posthog', 'sharded_events_recent', sipHash64(distinct_id));
CREATE TABLE posthog.distributed_posthog_document_embeddings (
team_id Int64,
@@ -2854,7 +2854,7 @@ CREATE TABLE posthog.events_recent (
historical_migration Bool,
_timestamp DateTime,
_offset UInt64,
- inserted_at Nullable(DateTime64(6, 'UTC')) DEFAULT now64()
+ inserted_at DateTime64(6, 'UTC') DEFAULT now64()
) ENGINE = Distributed('posthog_primary_replica', 'posthog', 'sharded_events_recent', sipHash64(distinct_id));
CREATE TABLE posthog.experiment_exposures_preaggregated (
team_id Int64,
diff --git a/posthog/clickhouse/migrations/0287_events_recent_inserted_at_not_nullable.py b/posthog/clickhouse/migrations/0287_events_recent_inserted_at_not_nullable.py
new file mode 100644
index 000000000000..f6a6eb4d4d2c
--- /dev/null
+++ b/posthog/clickhouse/migrations/0287_events_recent_inserted_at_not_nullable.py
@@ -0,0 +1,11 @@
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+
+operations = [
+ run_sql_with_exceptions(
+ "ALTER TABLE distributed_events_recent MODIFY COLUMN IF EXISTS inserted_at DateTime64(6, 'UTC') DEFAULT now64()",
+ node_roles=[NodeRole.DATA],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index c72e3ffd790b..48535372c34c 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0286_ingestion_warnings_v2
+0287_events_recent_inserted_at_not_nullable
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 305181def734..aed0eb567d00 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -1184,7 +1184,7 @@
, _timestamp DateTime
, _offset UInt64
- , inserted_at Nullable(DateTime64(6, 'UTC')) DEFAULT NOW64()
+ , inserted_at DateTime64(6, 'UTC') DEFAULT NOW64()
) ENGINE = Distributed('posthog_primary_replica', 'posthog_test', 'sharded_events_recent', sipHash64(distinct_id))
@@ -1588,7 +1588,7 @@
, _timestamp DateTime
, _offset UInt64
- , inserted_at Nullable(DateTime64(6, 'UTC')) DEFAULT NOW64()
+ , inserted_at DateTime64(6, 'UTC') DEFAULT NOW64()
) ENGINE = Distributed('posthog_primary_replica', 'posthog_test', 'sharded_events_recent', sipHash64(distinct_id))
diff --git a/posthog/models/event/sql.py b/posthog/models/event/sql.py
index e3fbed8e3096..1dffc9eb63d7 100644
--- a/posthog/models/event/sql.py
+++ b/posthog/models/event/sql.py
@@ -342,7 +342,7 @@ def EVENTS_RECENT_TABLE_SQL(on_cluster=False):
sharding_key="sipHash64(distinct_id)",
cluster=settings.CLICKHOUSE_PRIMARY_REPLICA_CLUSTER,
),
- extra_fields=KAFKA_COLUMNS + INSERTED_AT_COLUMN,
+ extra_fields=KAFKA_COLUMNS + INSERTED_AT_NOT_NULLABLE_COLUMN,
dynamically_materialized_columns="",
materialized_columns="",
indexes="",
@@ -358,7 +358,7 @@ def DISTRIBUTED_EVENTS_RECENT_TABLE_SQL(on_cluster=False):
sharding_key="sipHash64(distinct_id)",
cluster=settings.CLICKHOUSE_PRIMARY_REPLICA_CLUSTER,
),
- extra_fields=KAFKA_COLUMNS + INSERTED_AT_COLUMN,
+ extra_fields=KAFKA_COLUMNS + INSERTED_AT_NOT_NULLABLE_COLUMN,
dynamically_materialized_columns="",
materialized_columns="",
indexes="",