-
Notifications
You must be signed in to change notification settings - Fork 23
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
base: main
Are you sure you want to change the base?
Conversation
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 | ||
) |
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.
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?
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.
Thank you for the hint. Now it is much better!
https://www.postgresql.org/docs/current/sql-select.html#SQL-DISTINCT
9be287a
to
343b1a5
Compare
343b1a5
to
96084a0
Compare
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 newerrefreshed_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.