From 8d84bd2d188abee68b91ccf2f7263927610b1ed7 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Fri, 3 Apr 2026 10:46:17 -0500 Subject: [PATCH] Prune propagation scores per valid_time to avoid massive single DELETE --- lib/microwaveprop/propagation.ex | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/microwaveprop/propagation.ex b/lib/microwaveprop/propagation.ex index 649557a9..5f15573a 100644 --- a/lib/microwaveprop/propagation.ex +++ b/lib/microwaveprop/propagation.ex @@ -151,13 +151,26 @@ defmodule Microwaveprop.Propagation do @doc "Remove scores with valid_times older than 1 hour before the earliest current forecast." def prune_old_scores do - # Keep all forecast hours from the latest run + 1 hour buffer for the previous run cutoff = DateTime.add(DateTime.utc_now(), -2, :hour) - {deleted, _} = Repo.delete_all(from(gs in GridScore, where: gs.valid_time < ^cutoff)) + # Get distinct old valid_times and delete one at a time to avoid massive single DELETEs + old_times = + Repo.all( + from(gs in GridScore, + where: gs.valid_time < ^cutoff, + select: gs.valid_time, + distinct: gs.valid_time + ) + ) - if deleted > 0 do - Logger.info("PropagationScores: pruned #{deleted} old scores (valid_time < #{cutoff})") + total = + Enum.reduce(old_times, 0, fn vt, acc -> + {deleted, _} = Repo.delete_all(from(gs in GridScore, where: gs.valid_time == ^vt)) + acc + deleted + end) + + if total > 0 do + Logger.info("PropagationScores: pruned #{total} old scores across #{length(old_times)} valid_times") end end