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

Handle entity duplicates in Mobile radio tracker #923

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

kurotych
Copy link
Member

@kurotych kurotych commented Dec 19, 2024

During development testing, we encountered the following issue:

ERROR mobile_config::mobile_radio_tracker: error in tracking changes to mobile radios err=error returned from database: ON CONFLICT DO UPDATE command cannot affect row a second time

The root cause of this problem is the existence of duplicate entity keys in the key_to_assets table (on the dev server).

Solution: Handle (entity_key, asset) duplicates by fetching unique entries with the newer refreshed_at.

(Theoretically possible) Case where one entity_key maps to several different assets will still result in the error described above. Since I don't see a graceful recovery from such data corruption.

@kurotych kurotych marked this pull request as ready for review December 19, 2024 12:45
Comment on lines 211 to 222
WITH unique_entity_keys AS (
SELECT
kta.entity_key as entity_key,
kta.asset as asset,
MAX(mhi.refreshed_at) AS refreshed_at
FROM key_to_assets kta
INNER JOIN mobile_hotspot_infos mhi
ON kta.asset = mhi.asset
WHERE kta.entity_key IS NOT NULL
AND mhi.refreshed_at IS NOT NULL
GROUP BY kta.entity_key, kta.asset
)
Copy link
Contributor

Choose a reason for hiding this comment

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

can this query be simplified back into something more like the original with a SELECT DISTINCT ON (entity_key, asset) ... ORDER BY refreshed_at DESC to limit results to the most recent single entry for that given combo of identifiers?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the hint. Now it is much better!

https://www.postgresql.org/docs/current/sql-select.html#SQL-DISTINCT

@kurotych kurotych force-pushed the mobile-tracking-entity-dupl branch from 9be287a to 343b1a5 Compare January 3, 2025 10:45
@kurotych kurotych force-pushed the mobile-tracking-entity-dupl branch from 343b1a5 to 96084a0 Compare January 3, 2025 10:46
@kurotych kurotych requested a review from jeffgrunewald January 3, 2025 10:54
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