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): report tombstoned-delete rows on every RPC exit (#103102)

Eli Reisman4ec9f952ed0a
diff --git a/posthog/dags/person_pg_cleanup_drain.py b/posthog/dags/person_pg_cleanup_drain.py
index 2864a8ce2277..74f6f6d6c398 100644
--- a/posthog/dags/person_pg_cleanup_drain.py
+++ b/posthog/dags/person_pg_cleanup_drain.py
@@ -826,6 +826,16 @@ def _drain_gauges(totals: DrainTotals, completed_at: float) -> list[PublishedGau
PublishedGauge(
name=f"{prefix}persons_deleted", help_text="Persons the run hard-deleted", value=totals.persons_deleted
),
+ PublishedGauge(
+ name=f"{prefix}persons_skipped_live",
+ help_text="Queued persons that are live in Postgres, so the drain left them alone",
+ value=totals.persons_skipped_live,
+ ),
+ PublishedGauge(
+ name=f"{prefix}persons_not_found",
+ help_text="Queued persons with no Postgres row, so the queue row was already stale",
+ value=totals.persons_not_found,
+ ),
PublishedGauge(
name=f"{prefix}persons_blocked",
help_text="Tombstoned persons personhog reported as still owning a live distinct id",
diff --git a/posthog/dags/tests/test_person_pg_cleanup_drain.py b/posthog/dags/tests/test_person_pg_cleanup_drain.py
index 50cd6925a762..8cf37c6e585a 100644
--- a/posthog/dags/tests/test_person_pg_cleanup_drain.py
+++ b/posthog/dags/tests/test_person_pg_cleanup_drain.py
@@ -772,6 +772,8 @@ def test_publishes_every_measurement_the_run_took():
totals = DrainTotals(
rows_read=5,
persons_deleted=3,
+ persons_skipped_live=2,
+ persons_not_found=7,
persons_blocked=1,
rows_deleted=40,
rows_stamped_blocked=1,
@@ -793,6 +795,8 @@ def test_publishes_every_measurement_the_run_took():
"queue_rows_estimate_at_start",
"rows_read",
"persons_deleted",
+ "persons_skipped_live",
+ "persons_not_found",
"persons_blocked",
"rows_deleted",
"rows_stamped_blocked",
@@ -806,6 +810,8 @@ def test_publishes_every_measurement_the_run_took():
"queue_rows_estimate_at_start": 1000,
"rows_read": 5,
"persons_deleted": 3,
+ "persons_skipped_live": 2,
+ "persons_not_found": 7,
"persons_blocked": 1,
"rows_deleted": 40,
"rows_stamped_blocked": 1,
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 80956cc61de7..1fbec690b9e3 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -8822,7 +8822,9 @@ dependencies = [
"envconfig",
"futures",
"lifecycle",
+ "metrics",
"metrics-exporter-prometheus",
+ "metrics-util",
"personhog-common",
"personhog-proto",
"rand 0.8.5",
diff --git a/rust/personhog-replica/Cargo.toml b/rust/personhog-replica/Cargo.toml
index bdb8d3723601..1cf7e668997f 100644
--- a/rust/personhog-replica/Cargo.toml
+++ b/rust/personhog-replica/Cargo.toml
@@ -29,6 +29,8 @@ tracing-subscriber = { workspace = true }
uuid = { workspace = true }
[dev-dependencies]
+metrics = { workspace = true }
+metrics-util = { workspace = true }
rstest = "0.26"
tokio = { workspace = true, features = ["test-util"] }
tokio-stream = "0.1"
diff --git a/rust/personhog-replica/src/storage/postgres/person.rs b/rust/personhog-replica/src/storage/postgres/person.rs
index aab08f8c9d68..ec8800738965 100644
--- a/rust/personhog-replica/src/storage/postgres/person.rs
+++ b/rust/personhog-replica/src/storage/postgres/person.rs
@@ -622,6 +622,7 @@ impl PersonLookup for PostgresStorage {
};
if candidates.is_empty() {
tx.commit().await?;
+ record_tombstoned_delete_rows(&outcome, &client, &method);
return Ok(outcome);
}
@@ -747,36 +748,7 @@ impl PersonLookup for PostgresStorage {
tx.commit().await?;
- for (operation, value) in [
- ("delete_tombstoned_persons_deleted", outcome.deleted),
- (
- "delete_tombstoned_persons_skipped_live",
- outcome.skipped_live,
- ),
- (
- "delete_tombstoned_persons_blocked",
- outcome.blocked_uuids.len() as i64,
- ),
- (
- "delete_tombstoned_persons_pending",
- outcome.pending_uuids.len() as i64,
- ),
- (
- "delete_tombstoned_persons_rows_deleted",
- outcome.rows_deleted,
- ),
- ] {
- common_metrics::histogram(
- DB_ROWS_RETURNED,
- &[
- ("operation".to_string(), operation.to_string()),
- ("pool".to_string(), "bulk_primary".to_string()),
- ("client".to_string(), client.to_string()),
- ("method".to_string(), method.to_string()),
- ],
- value as f64,
- );
- }
+ record_tombstoned_delete_rows(&outcome, &client, &method);
Ok(outcome)
}
@@ -1365,6 +1337,41 @@ impl DependentRowCounts {
}
}
+/// Called by every exit that runs the resolve query. That query decides the counts, so the early
+/// return for a call with no tombstoned candidate would otherwise hide live persons here.
+fn record_tombstoned_delete_rows(outcome: &TombstonedDeleteOutcome, client: &str, method: &str) {
+ for (operation, value) in [
+ ("delete_tombstoned_persons_deleted", outcome.deleted),
+ (
+ "delete_tombstoned_persons_skipped_live",
+ outcome.skipped_live,
+ ),
+ (
+ "delete_tombstoned_persons_blocked",
+ outcome.blocked_uuids.len() as i64,
+ ),
+ (
+ "delete_tombstoned_persons_pending",
+ outcome.pending_uuids.len() as i64,
+ ),
+ (
+ "delete_tombstoned_persons_rows_deleted",
+ outcome.rows_deleted,
+ ),
+ ] {
+ common_metrics::histogram(
+ DB_ROWS_RETURNED,
+ &[
+ ("operation".to_string(), operation.to_string()),
+ ("pool".to_string(), "bulk_primary".to_string()),
+ ("client".to_string(), client.to_string()),
+ ("method".to_string(), method.to_string()),
+ ],
+ value as f64,
+ );
+ }
+}
+
async fn probe_dependent_rows<'e, E>(
executor: E,
team_id: i64,
diff --git a/rust/personhog-replica/tests/storage_tests.rs b/rust/personhog-replica/tests/storage_tests.rs
index 75ce97a474b7..6d630f8a338c 100644
--- a/rust/personhog-replica/tests/storage_tests.rs
+++ b/rust/personhog-replica/tests/storage_tests.rs
@@ -1,6 +1,7 @@
mod common;
use common::TestContext;
+use metrics_util::debugging::{DebugValue, DebuggingRecorder, Snapshotter};
use personhog_replica::storage::postgres::ConsistencyLevel;
use personhog_replica::storage::{GroupKey, TombstonedDeleteOutcome};
use rand::Rng;
@@ -3343,6 +3344,55 @@ async fn test_delete_tombstoned_persons_nothing_to_do(#[case] uuids: Vec<Uuid>)
ctx.cleanup().await.ok();
}
+fn recorded_rows(snapshotter: &Snapshotter, operation: &str) -> f64 {
+ snapshotter
+ .snapshot()
+ .into_vec()
+ .iter()
+ .filter(|(key, _, _, _)| {
+ key.key().name() == "personhog_replica_db_rows_returned"
+ && key
+ .key()
+ .labels()
+ .any(|label| label.key() == "operation" && label.value() == operation)
+ })
+ .filter_map(|(_, _, _, value)| match value {
+ DebugValue::Histogram(samples) => {
+ Some(samples.iter().map(|s| s.into_inner()).sum::<f64>())
+ }
+ _ => None,
+ })
+ .sum()
+}
+
+#[tokio::test]
+async fn test_delete_tombstoned_persons_records_skipped_live_when_nothing_is_tombstoned() {
+ let recorder = DebuggingRecorder::new();
+ let snapshotter = recorder.snapshotter();
+ let _guard = metrics::set_default_local_recorder(&recorder);
+
+ let ctx = TestContext::new().await;
+ let live = ctx
+ .insert_person("tomb_metrics_live", None)
+ .await
+ .expect("Failed to insert person");
+
+ let outcome = ctx
+ .storage
+ .delete_tombstoned_persons(ctx.team_id, &[live.uuid], TEST_MAX_ROWS)
+ .await
+ .expect("Failed to delete tombstoned persons");
+
+ assert_eq!(outcome.skipped_live, 1);
+ assert_eq!(outcome.deleted, 0);
+ assert_eq!(
+ recorded_rows(&snapshotter, "delete_tombstoned_persons_skipped_live"),
+ 1.0,
+ "a call that finds only live persons must still report them"
+ );
+ ctx.cleanup().await.ok();
+}
+
#[tokio::test]
async fn test_delete_tombstoned_persons_cross_team_isolation() {
let ctx = TestContext::new().await;