Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(plugin-server): reduce createPerson to a single query #18126

Merged
merged 1 commit into from
Oct 26, 2023

Conversation

bretthoerner
Copy link
Contributor

@bretthoerner bretthoerner commented Oct 20, 2023

Problem

createPerson used a txn to insert the person row and then insert (1 or 2) distinct ID rows, each with its own round trip to the DB, while the txn was held open.

Changes

Do all of createPerson in a single DB round-trip.

👉 Stay up-to-date with PostHog coding conventions for a smoother review.

How did you test this code?

Existing tests, this is a drop in for well tested, existing behavior.

@bretthoerner bretthoerner force-pushed the brett/ps-queries2 branch 2 times, most recently from d6ae520 to 840017f Compare October 21, 2023 12:20
@PostHog PostHog deleted a comment from posthog-bot Oct 21, 2023
isIdentified,
uuid,
version,
...distinctIds.slice().reverse(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it looks like PG does inserts and/or assigns row IDs in reverse order of what you provide in the CTEs (meaning the last INSERT gets the earlier ID). This doesn't matter at all for actual code correctness, but we have a lot of tests that depend on ORDER BY id and so I'd rather just prove I am not changing anything at all behavior-wise. That's why the reverse() is done here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment about that

@bretthoerner bretthoerner requested a review from a team October 21, 2023 12:42
const person = {
...personCreated,
created_at: DateTime.fromISO(personCreated.created_at).toUTC(),
version,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit (for follow-up PR): maybe we should change RawPerson ... same as with PluginEvent stuff I think we have too many types for the same thing and it's confusing.

distinctIds
.map(
(_, index) => `, distinct_id_${index} AS (
INSERT INTO posthog_persondistinctid (distinct_id, person_id, team_id, version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry about this getting out of sync with addDistinctIdPooled, for lack of a better solution maybe a comment to keep them in sync like we have done in other places 🤷‍♀️

@bretthoerner bretthoerner force-pushed the brett/ps-queries2 branch 2 times, most recently from 42f8f54 to 011c47f Compare October 25, 2023 21:18
@bretthoerner bretthoerner merged commit 4acca90 into master Oct 26, 2023
68 checks passed
@bretthoerner bretthoerner deleted the brett/ps-queries2 branch October 26, 2023 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants