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(personhog): count a NULL version as 0 in the version floor RPCs (#112092)

Eli Reisman541cf561c49b
diff --git a/rust/personhog-replica/.sqlx/query-5ce9e7a1203e968cc63cebb39a4a0f866d2e7be45c938469b032c6d4a40d8343.json b/rust/personhog-replica/.sqlx/query-a04c9a81fa175d108970b6bec079c72144ea3d0f66b0b304c02059355d895917.json
similarity index 68%
rename from rust/personhog-replica/.sqlx/query-5ce9e7a1203e968cc63cebb39a4a0f866d2e7be45c938469b032c6d4a40d8343.json
rename to rust/personhog-replica/.sqlx/query-a04c9a81fa175d108970b6bec079c72144ea3d0f66b0b304c02059355d895917.json
index 1294ecf3fd75..98b516e4bfc1 100644
--- a/rust/personhog-replica/.sqlx/query-5ce9e7a1203e968cc63cebb39a4a0f866d2e7be45c938469b032c6d4a40d8343.json
+++ b/rust/personhog-replica/.sqlx/query-a04c9a81fa175d108970b6bec079c72144ea3d0f66b0b304c02059355d895917.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n WITH target AS (\n SELECT person_id FROM posthog_persondistinctid\n WHERE team_id = $1 AND distinct_id = $2\n ),\n updated AS (\n UPDATE posthog_persondistinctid\n SET version = $3\n WHERE team_id = $1 AND distinct_id = $2 AND version < $3\n RETURNING person_id\n )\n SELECT p.id, p.uuid, p.team_id::bigint as \"team_id!\", p.properties::text as \"properties?\",\n p.properties_last_updated_at::text as \"properties_last_updated_at?\",\n p.properties_last_operation::text as \"properties_last_operation?\",\n p.created_at, p.version, p.is_identified,\n CASE WHEN p.is_user_id IS NULL THEN NULL ELSE (p.is_user_id != 0) END as is_user_id,\n p.last_seen_at\n FROM posthog_person p\n INNER JOIN target t ON p.id = t.person_id AND p.team_id = $1\n ",
+ "query": "\n WITH target AS (\n SELECT person_id FROM posthog_persondistinctid\n WHERE team_id = $1 AND distinct_id = $2\n ),\n updated AS (\n UPDATE posthog_persondistinctid\n SET version = $3\n WHERE team_id = $1 AND distinct_id = $2 AND COALESCE(version, 0) < $3\n RETURNING person_id\n )\n SELECT p.id, p.uuid, p.team_id::bigint as \"team_id!\", p.properties::text as \"properties?\",\n p.properties_last_updated_at::text as \"properties_last_updated_at?\",\n p.properties_last_operation::text as \"properties_last_operation?\",\n p.created_at, p.version, p.is_identified,\n CASE WHEN p.is_user_id IS NULL THEN NULL ELSE (p.is_user_id != 0) END as is_user_id,\n p.last_seen_at\n FROM posthog_person p\n INNER JOIN target t ON p.id = t.person_id AND p.team_id = $1\n ",
"describe": {
"columns": [
{
@@ -80,5 +80,5 @@
true
]
},
- "hash": "5ce9e7a1203e968cc63cebb39a4a0f866d2e7be45c938469b032c6d4a40d8343"
+ "hash": "a04c9a81fa175d108970b6bec079c72144ea3d0f66b0b304c02059355d895917"
}
diff --git a/rust/personhog-replica/.sqlx/query-c5eb307c2173cebf044fbd0b99461a1da494054b8cf47c2841f56843b75a5aa1.json b/rust/personhog-replica/.sqlx/query-f0f8d50926a4b094c736c97c7bc1d0ed6ca4cafc131112a7ea61fc5ebbffb078.json
similarity index 58%
rename from rust/personhog-replica/.sqlx/query-c5eb307c2173cebf044fbd0b99461a1da494054b8cf47c2841f56843b75a5aa1.json
rename to rust/personhog-replica/.sqlx/query-f0f8d50926a4b094c736c97c7bc1d0ed6ca4cafc131112a7ea61fc5ebbffb078.json
index 8c6b8c073023..378d8ad32786 100644
--- a/rust/personhog-replica/.sqlx/query-c5eb307c2173cebf044fbd0b99461a1da494054b8cf47c2841f56843b75a5aa1.json
+++ b/rust/personhog-replica/.sqlx/query-f0f8d50926a4b094c736c97c7bc1d0ed6ca4cafc131112a7ea61fc5ebbffb078.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE posthog_person\n SET version = $3\n WHERE team_id = $1 AND id = $2 AND version < $3\n ",
+ "query": "\n UPDATE posthog_person\n SET version = $3\n WHERE team_id = $1 AND id = $2 AND COALESCE(version, 0) < $3\n ",
"describe": {
"columns": [],
"parameters": {
@@ -12,5 +12,5 @@
},
"nullable": []
},
- "hash": "c5eb307c2173cebf044fbd0b99461a1da494054b8cf47c2841f56843b75a5aa1"
+ "hash": "f0f8d50926a4b094c736c97c7bc1d0ed6ca4cafc131112a7ea61fc5ebbffb078"
}
diff --git a/rust/personhog-replica/src/storage/postgres/person.rs b/rust/personhog-replica/src/storage/postgres/person.rs
index 00eebfbe23b1..592a21069841 100644
--- a/rust/personhog-replica/src/storage/postgres/person.rs
+++ b/rust/personhog-replica/src/storage/postgres/person.rs
@@ -1269,11 +1269,8 @@ impl PersonLookup for PostgresStorage {
let mut conn = PostgresStorage::acquire_timed(&self.primary_pool, "primary").await?;
- // Resolve the distinct_id's person and guardedly bump its version in one
- // round-trip. The `target` CTE returns the person whenever the distinct_id
- // exists, while the `UPDATE` only fires when the stored version is below
- // min_version — so an already-higher version is left intact but the person is
- // still returned. No matching distinct_id yields no person.
+ // The person is returned whenever the distinct_id exists, even if its version is already
+ // at or above min_version. A NULL version counts as 0, as in every other personhog write.
let row = sqlx::query_as!(
Person,
r#"
@@ -1284,7 +1281,7 @@ impl PersonLookup for PostgresStorage {
updated AS (
UPDATE posthog_persondistinctid
SET version = $3
- WHERE team_id = $1 AND distinct_id = $2 AND version < $3
+ WHERE team_id = $1 AND distinct_id = $2 AND COALESCE(version, 0) < $3
RETURNING person_id
)
SELECT p.id, p.uuid, p.team_id::bigint as "team_id!", p.properties::text as "properties?",
@@ -1327,12 +1324,12 @@ impl PersonLookup for PostgresStorage {
let mut conn = PostgresStorage::acquire_timed(&self.primary_pool, "primary").await?;
- // Guarded bump: never lowers an existing version.
+ // Never lowers a version. A NULL version counts as 0, as in every other personhog write.
let result = sqlx::query!(
r#"
UPDATE posthog_person
SET version = $3
- WHERE team_id = $1 AND id = $2 AND version < $3
+ WHERE team_id = $1 AND id = $2 AND COALESCE(version, 0) < $3
"#,
team_id as i32,
person_id,
diff --git a/rust/personhog-replica/tests/storage_tests.rs b/rust/personhog-replica/tests/storage_tests.rs
index 8be7284d2bff..d2ecb27331c9 100644
--- a/rust/personhog-replica/tests/storage_tests.rs
+++ b/rust/personhog-replica/tests/storage_tests.rs
@@ -3339,10 +3339,57 @@ async fn test_split_person_each_did_gets_unique_person() {
// Undelete repair: reset version tests
// ============================================================
+async fn seed_person(
+ ctx: &TestContext,
+ distinct_id: &str,
+ version: Option<i64>,
+ is_deleted: bool,
+) -> common::TestPerson {
+ let person = ctx
+ .insert_person(distinct_id, Some(serde_json::json!({"seeded": true})))
+ .await
+ .unwrap();
+ sqlx::query(
+ "UPDATE posthog_person SET version = $3, is_deleted = $4 WHERE team_id = $1 AND id = $2",
+ )
+ .bind(ctx.team_id)
+ .bind(person.id)
+ .bind(version)
+ .bind(is_deleted)
+ .execute(&ctx.pool)
+ .await
+ .unwrap();
+ person
+}
+
+async fn set_distinct_id_state(
+ ctx: &TestContext,
+ distinct_id: &str,
+ version: Option<i64>,
+ is_deleted: bool,
+) {
+ sqlx::query(
+ "UPDATE posthog_persondistinctid SET version = $3, is_deleted = $4 WHERE team_id = $1 AND distinct_id = $2",
+ )
+ .bind(ctx.team_id)
+ .bind(distinct_id)
+ .bind(version)
+ .bind(is_deleted)
+ .execute(&ctx.pool)
+ .await
+ .unwrap();
+}
+
+#[rstest]
+#[case::from_zero(Some(0))]
+#[case::from_null(None)]
#[tokio::test]
-async fn test_set_person_distinct_id_version_floor_updates_and_returns_person() {
+async fn test_set_person_distinct_id_version_floor_updates_and_returns_person(
+ #[case] initial_version: Option<i64>,
+) {
let ctx = TestContext::new().await;
let person = ctx.insert_person("repair_did", None).await.unwrap();
+ set_distinct_id_state(&ctx, "repair_did", initial_version, false).await;
let returned = ctx
.storage
@@ -3417,12 +3464,14 @@ async fn test_set_person_distinct_id_version_floor_does_not_lower() {
ctx.cleanup().await.ok();
}
+#[rstest]
+#[case::from_zero(Some(0))]
+#[case::from_null(None)]
#[tokio::test]
-async fn test_set_person_version_floor_guarded_bump() {
+async fn test_set_person_version_floor_guarded_bump(#[case] initial_version: Option<i64>) {
let ctx = TestContext::new().await;
- let person = ctx.insert_person("rv_did", None).await.unwrap();
+ let person = seed_person(&ctx, "rv_did", initial_version, false).await;
- // Bump above the current version (0).
let updated = ctx
.storage
.set_person_version_floor(ctx.team_id, person.id, 50)