Skip to content

Commit

Permalink
non-jank idempotent query to populate volume_resource_usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jmpesp committed Nov 7, 2024
1 parent ff590d6 commit 2b511b1
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions schema/crdb/crucible-ref-count-records/up08.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@
snapshot records to populate the volume resource usage records */
INSERT INTO volume_resource_usage (
SELECT
(
OVERLAY(
OVERLAY(
MD5(volume.id::TEXT || dataset_id::TEXT || region_id::TEXT || snapshot_id::TEXT || snapshot_addr || volume_references::TEXT)
PLACING '4' from 13
)
PLACING '8' from 17
)::uuid
) as usage_id,
gen_random_uuid() AS usage_id,
volume.id AS volume_id,
'region_snapshot' AS usage_type,
NULL AS region_id,
Expand All @@ -22,10 +14,18 @@ INSERT INTO volume_resource_usage (
JOIN
region_snapshot
ON
volume.data like '%' || region_snapshot.snapshot_addr || '%'
volume.data like ('%' || region_snapshot.snapshot_addr || '%')
LEFT JOIN
volume_resource_usage
ON
volume_resource_usage.volume_id = volume.id AND
volume_resource_usage.usage_type = 'region_snapshot' AND
volume_resource_usage.region_id IS NULL AND
volume_resource_usage.region_snapshot_region_id = region_snapshot.region_id AND
volume_resource_usage.region_snapshot_dataset_id = region_snapshot.dataset_id AND
volume_resource_usage.region_snapshot_snapshot_id = region_snapshot.snapshot_id
WHERE
volume.time_deleted is NULL AND
region_snapshot.deleting = false
)
ON CONFLICT (usage_id) DO NOTHING
;
region_snapshot.deleting = false AND
volume_resource_usage.usage_id IS NULL
);

0 comments on commit 2b511b1

Please sign in to comment.