-
Notifications
You must be signed in to change notification settings - Fork 1
fix: close concurrent-insert duplicate-URL race on poi_news #444
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| -- Migration 069: close the concurrent-insert duplicate-URL race on poi_news. | ||
| -- | ||
| -- saveNewsItems dedups with a SELECT-then-INSERT, which has no DB-level guard: | ||
| -- two concurrent collection branches can both read "not present" and both | ||
| -- insert the same URL (observed repeatedly on the historicbridges POI, e.g. | ||
| -- ids 1523/1524 and 2622/2623 inserted ~24ms apart). A unique index on the | ||
| -- normalized source_url turns that race into a clean no-op when paired with | ||
| -- ON CONFLICT DO NOTHING in the insert path. | ||
| -- | ||
| -- Events are intentionally excluded: distinct events legitimately share a | ||
| -- generic source URL (a venue homepage or calendar page), so source_url is not | ||
| -- unique for poi_events. | ||
|
|
||
| -- Safety net: collapse any pre-existing normalized-URL duplicates before adding | ||
| -- the constraint, keeping the best row per URL (published > pending > rejected, | ||
| -- then lowest id). Idempotent -- a no-op once the table holds one row per URL. | ||
| DELETE FROM poi_news a | ||
| USING poi_news b | ||
| WHERE a.source_url IS NOT NULL | ||
| AND b.source_url IS NOT NULL | ||
| AND LOWER(REGEXP_REPLACE(a.source_url, '/+$', '')) = LOWER(REGEXP_REPLACE(b.source_url, '/+$', '')) | ||
| AND (CASE a.moderation_status WHEN 'published' THEN 0 WHEN 'pending' THEN 1 ELSE 2 END, a.id) | ||
| > (CASE b.moderation_status WHEN 'published' THEN 0 WHEN 'pending' THEN 1 ELSE 2 END, b.id); | ||
|
|
||
| CREATE UNIQUE INDEX IF NOT EXISTS poi_news_source_url_norm_uniq | ||
| ON poi_news (LOWER(REGEXP_REPLACE(source_url, '/+$', ''))) | ||
| WHERE source_url IS NOT NULL; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a self-join with$O(N^2)$ comparison of all rows matching the join condition.
USINGto delete duplicates can be highly inefficient on large tables because it requires anA more performant and standard PostgreSQL approach is to use a subquery with
ROW_NUMBER(). This scans the table once, partitions and sorts the rows, and deletes all but the first row in each partition.