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.

perf(error-tracking): stop losing fingerprint upserts taking a row lock (#98950)

posthog[bot]6884964fabf2
diff --git a/rust/cymbal/.sqlx/query-862b0d12dbe1248c97ffc0fb9db3c1d06c0eb2abe100337ccd57ca85ea88688c.json b/rust/cymbal/.sqlx/query-8e537cd2e8a8aacd0b14ce10911d84a50ffccd4753d64ce6ed8ed5b23880b44e.json
similarity index 76%
rename from rust/cymbal/.sqlx/query-862b0d12dbe1248c97ffc0fb9db3c1d06c0eb2abe100337ccd57ca85ea88688c.json
rename to rust/cymbal/.sqlx/query-8e537cd2e8a8aacd0b14ce10911d84a50ffccd4753d64ce6ed8ed5b23880b44e.json
index 8567495f79ea..14101300cbe3 100644
--- a/rust/cymbal/.sqlx/query-862b0d12dbe1248c97ffc0fb9db3c1d06c0eb2abe100337ccd57ca85ea88688c.json
+++ b/rust/cymbal/.sqlx/query-8e537cd2e8a8aacd0b14ce10911d84a50ffccd4753d64ce6ed8ed5b23880b44e.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n INSERT INTO posthog_errortrackingissuefingerprintv2 (id, team_id, issue_id, fingerprint, version, first_seen, created_at)\n VALUES ($1, $2, $3, $4, 0, $5, NOW())\n ON CONFLICT (team_id, fingerprint) DO UPDATE SET team_id = EXCLUDED.team_id -- a no-op update to force a returned row\n RETURNING id, team_id, issue_id, fingerprint, version\n ",
+ "query": "\n INSERT INTO posthog_errortrackingissuefingerprintv2 (id, team_id, issue_id, fingerprint, version, first_seen, created_at)\n VALUES ($1, $2, $3, $4, 0, $5, NOW())\n ON CONFLICT (team_id, fingerprint) DO NOTHING\n RETURNING id, team_id, issue_id, fingerprint, version\n ",
"describe": {
"columns": [
{
@@ -46,5 +46,5 @@
false
]
},
- "hash": "862b0d12dbe1248c97ffc0fb9db3c1d06c0eb2abe100337ccd57ca85ea88688c"
+ "hash": "8e537cd2e8a8aacd0b14ce10911d84a50ffccd4753d64ce6ed8ed5b23880b44e"
}
diff --git a/rust/cymbal/src/modes/processing/issue_resolution.rs b/rust/cymbal/src/modes/processing/issue_resolution.rs
index d1b399573f48..366956345741 100644
--- a/rust/cymbal/src/modes/processing/issue_resolution.rs
+++ b/rust/cymbal/src/modes/processing/issue_resolution.rs
@@ -448,24 +448,22 @@ impl IssueFingerprintOverride {
Ok(res)
}
- pub async fn create_or_load<'c, E>(
- executor: E,
+ pub async fn create_or_load(
+ conn: &mut sqlx::PgConnection,
team_id: i32,
fingerprint: &str,
issue: &Issue,
first_seen: DateTime<Utc>,
- ) -> Result<Self, UnhandledError>
- where
- E: sqlx::Executor<'c, Database = sqlx::Postgres>,
- {
+ ) -> Result<Self, UnhandledError> {
// We do an "ON CONFLICT DO NOTHING" here because callers can compare the returned issue id
- // to the passed Issue, to see if the issue was actually inserted or not.
- let res = sqlx::query_as!(
+ // to the passed Issue, to see if the issue was actually inserted or not. DO NOTHING takes
+ // no row lock and writes no dead tuple on a losing racer, unlike a self-assigning DO UPDATE.
+ let inserted = sqlx::query_as!(
IssueFingerprintOverride,
r#"
INSERT INTO posthog_errortrackingissuefingerprintv2 (id, team_id, issue_id, fingerprint, version, first_seen, created_at)
VALUES ($1, $2, $3, $4, 0, $5, NOW())
- ON CONFLICT (team_id, fingerprint) DO UPDATE SET team_id = EXCLUDED.team_id -- a no-op update to force a returned row
+ ON CONFLICT (team_id, fingerprint) DO NOTHING
RETURNING id, team_id, issue_id, fingerprint, version
"#,
Uuid::new_v4(),
@@ -473,9 +471,20 @@ impl IssueFingerprintOverride {
issue.id,
fingerprint,
first_seen
- ).fetch_one(executor).await.expect("Got at least one row back");
+ ).fetch_optional(&mut *conn).await?;
- Ok(res)
+ if let Some(inserted) = inserted {
+ return Ok(inserted);
+ }
+
+ // Someone else won the insert, so DO NOTHING returned no row. Read the existing one back.
+ Self::load(&mut *conn, team_id, fingerprint)
+ .await?
+ .ok_or_else(|| {
+ UnhandledError::Other(
+ "fingerprint override conflict but existing row not found".to_string(),
+ )
+ })
}
}
diff --git a/rust/cymbal/src/modes/processing/stages/linking/issue.rs b/rust/cymbal/src/modes/processing/stages/linking/issue.rs
index e25ae2720033..701ca6b8cafb 100644
--- a/rust/cymbal/src/modes/processing/stages/linking/issue.rs
+++ b/rust/cymbal/src/modes/processing/stages/linking/issue.rs
@@ -304,7 +304,7 @@ async fn resolve_issue(
// Insert the fingerprint override
let issue_override = IssueFingerprintOverride::create_or_load(
- &mut *txn,
+ &mut txn,
team_id,
&fingerprint,
&issue,
@@ -375,6 +375,11 @@ async fn resolve_issue(
.await?;
let processed_properties = event_properties.processed_properties(&issue);
+ // Produce before the commit on purpose. A failure here rolls the transaction back, so the
+ // retry re-runs this whole slow path and produces again. Committing first would leave a
+ // new issue whose fingerprint state never reached ClickHouse: the retry takes the fast
+ // path, finds the issue already active, and `maybe_reopen` returns false, so nothing
+ // re-sends it. Moving this after the commit needs a durable outbox first.
send_fingerprint_issue_state(
context,
&issue,