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(propdefs): stop rewriting unchanged property definition rows (#77902)

Eli Reismand409c9e29350
diff --git a/rust/property-defs-rs/src/batch_ingestion.rs b/rust/property-defs-rs/src/batch_ingestion.rs
index 582744288336..4b48eb954c4b 100644
--- a/rust/property-defs-rs/src/batch_ingestion.rs
+++ b/rust/property-defs-rs/src/batch_ingestion.rs
@@ -619,6 +619,15 @@ async fn write_property_definitions_batch(
// must lead with the DISTINCT ON keys; the trailing keys make the surviving row deterministic
// and pick the most useful one to write - a non-null property_type (then is_numerical),
// mirroring the DO UPDATE that only fills a NULL property_type.
+ //
+ // The DO UPDATE guard checks both sides of the conflict on purpose. Testing only the
+ // stored row means an already-known untyped property still fires the UPDATE, writing
+ // property_type NULL over NULL and is_numerical false over false: a new tuple version
+ // carrying no new information, plus index maintenance, on by far the most common path
+ // through this statement. Requiring EXCLUDED.property_type to be non-null keeps only
+ // the writes that actually resolve a type. is_numerical needs no separate check because
+ // it is derived from property_type (see `into_updates` in types.rs), so an incoming NULL
+ // type always carries is_numerical false and can never be the new information.
let result = sqlx::query(r#"
INSERT INTO posthog_propertydefinition (id, name, type, group_type_index, is_numerical, team_id, project_id, property_type)
SELECT DISTINCT ON (COALESCE(project_id, team_id::bigint), name, type, COALESCE(group_type_index, -1))
@@ -642,7 +651,8 @@ async fn write_property_definitions_batch(
DO UPDATE SET
property_type=EXCLUDED.property_type,
is_numerical=EXCLUDED.is_numerical
- WHERE posthog_propertydefinition.property_type IS NULL"#,
+ WHERE posthog_propertydefinition.property_type IS NULL
+ AND EXCLUDED.property_type IS NOT NULL"#,
)
.bind(&batch.ids)
.bind(&batch.names)
diff --git a/rust/property-defs-rs/tests/batch_ingestion.rs b/rust/property-defs-rs/tests/batch_ingestion.rs
index 4b2ca4e00a15..094b3a417a66 100644
--- a/rust/property-defs-rs/tests/batch_ingestion.rs
+++ b/rust/property-defs-rs/tests/batch_ingestion.rs
@@ -498,6 +498,101 @@ async fn test_property_definitions_keeps_rows_that_differ_on_conflict_key(db: Pg
assert_eq!(count, Some(2));
}
+#[sqlx::test(migrations = "./tests/test_migrations")]
+async fn test_property_definitions_rewrite_the_row_only_when_a_type_is_resolved(db: PgPool) {
+ // `xmin` is the id of the transaction that wrote the live tuple, so an unchanged xmin proves
+ // no new row version was written. Both halves of the DO UPDATE guard are load-bearing here:
+ // without the EXCLUDED-side check, "stays_untyped" rewrites the row on every repeat sighting
+ // of an already-known untyped property, which is the dominant path through this statement in
+ // production; without the stored-side check, "ignores_a_retype" overwrites a resolved type.
+ let config = Config::init_with_defaults().unwrap();
+ let is_numeric = |t: &Option<PropertyValueType>| matches!(t, Some(PropertyValueType::Numeric));
+
+ // (name, stored type, incoming type, expected final type, expect the tuple to be rewritten)
+ let cases = [
+ ("stays_untyped", None, None, None, false),
+ (
+ "gains_a_type",
+ None,
+ Some(PropertyValueType::Numeric),
+ Some("Numeric"),
+ true,
+ ),
+ (
+ "keeps_its_type",
+ Some(PropertyValueType::Numeric),
+ None,
+ Some("Numeric"),
+ false,
+ ),
+ (
+ "ignores_a_retype",
+ Some(PropertyValueType::Numeric),
+ Some(PropertyValueType::String),
+ Some("Numeric"),
+ false,
+ ),
+ ];
+
+ for (name, stored, incoming, expected_type, expect_rewrite) in cases {
+ let cache = setup_cache(&config);
+
+ process_batch(
+ &config,
+ cache.clone(),
+ &db,
+ vec![prop(
+ name,
+ stored.clone(),
+ is_numeric(&stored),
+ PropertyParentType::Event,
+ )],
+ &test_lifecycle_handle(),
+ )
+ .await;
+
+ let xmin_before: String =
+ sqlx::query_scalar("SELECT xmin::text FROM posthog_propertydefinition WHERE name = $1")
+ .bind(name)
+ .fetch_one(&db)
+ .await
+ .unwrap();
+
+ process_batch(
+ &config,
+ cache,
+ &db,
+ vec![prop(
+ name,
+ incoming.clone(),
+ is_numeric(&incoming),
+ PropertyParentType::Event,
+ )],
+ &test_lifecycle_handle(),
+ )
+ .await;
+
+ let (xmin_after, property_type): (String, Option<String>) = sqlx::query_as(
+ "SELECT xmin::text, property_type FROM posthog_propertydefinition WHERE name = $1",
+ )
+ .bind(name)
+ .fetch_one(&db)
+ .await
+ .unwrap();
+
+ assert_eq!(
+ property_type.as_deref(),
+ expected_type,
+ "{name}: unexpected final property_type"
+ );
+ assert_eq!(
+ xmin_after != xmin_before,
+ expect_rewrite,
+ "{name}: expected rewritten={expect_rewrite}, xmin {xmin_before} -> {xmin_after}"
+ );
+ }
+}
+
#[sqlx::test(migrations = "./tests/test_migrations")]
async fn test_event_definitions_dedupe_within_batch(db: PgPool) {
// An event name repeated within one batch shares the ON CONFLICT key; before the dedup this raised