Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

nicer handling of insert distinct id failure #527

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/utils/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,15 @@ export class DB {
distinctId: string
): Promise<ProducerRecord | void> {
const insertResult = await client.query(
'INSERT INTO posthog_persondistinctid (distinct_id, person_id, team_id) VALUES ($1, $2, $3) RETURNING *',
'INSERT INTO posthog_persondistinctid (distinct_id, person_id, team_id) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING RETURNING *',
[distinctId, person.id, person.team_id]
)

// some other thread already added this ID
if (insertResult.rows.length === 0) {
return
}

const personDistinctIdCreated = insertResult.rows[0] as PersonDistinctId
if (this.kafkaProducer) {
return {
Expand Down
9 changes: 6 additions & 3 deletions src/worker/ingestion/process-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ export class EventsProcessor {
try {
await this.db.addDistinctId(oldPerson, distinctId)
// Catch race case when somebody already added this distinct_id between .get and .addDistinctId
} catch {
} catch (error) {
Sentry.captureException(error)
// integrity error
if (retryIfFailed) {
// run everything again to merge the users if needed
Expand All @@ -305,7 +306,8 @@ export class EventsProcessor {
try {
await this.db.addDistinctId(newPerson, previousDistinctId)
// Catch race case when somebody already added this distinct_id between .get and .addDistinctId
} catch {
} catch (error) {
Sentry.captureException(error)
// integrity error
if (retryIfFailed) {
// run everything again to merge the users if needed
Expand All @@ -321,7 +323,8 @@ export class EventsProcessor {
distinctId,
previousDistinctId,
])
} catch {
} catch (error) {
Sentry.captureException(error)
// Catch race condition where in between getting and creating,
// another request already created this person
if (retryIfFailed) {
Expand Down