From 072c418cb39de4f82d099212010180ca89386f5b Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Wed, 1 Apr 2026 10:45:09 -0500 Subject: [PATCH] Fix HRRR prune timeout: batch delete with 60s timeout instead of full table scan --- lib/microwaveprop/weather.ex | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/microwaveprop/weather.ex b/lib/microwaveprop/weather.ex index 540994bd..042feb10 100644 --- a/lib/microwaveprop/weather.ex +++ b/lib/microwaveprop/weather.ex @@ -263,22 +263,21 @@ defmodule Microwaveprop.Weather do """ def prune_old_grid_profiles do cutoff = DateTime.add(DateTime.utc_now(), -24, :hour) - step = 0.125 - {deleted, _} = - Repo.delete_all( - from(h in HrrrProfile, - where: - h.valid_time < ^cutoff and - fragment("? = round(?::numeric / ? ) * ?", h.lat, h.lat, ^step, ^step) and - fragment("? = round(?::numeric / ? ) * ?", h.lon, h.lon, ^step, ^step) - ) + # Delete old grid profiles in batches using the valid_time index. + # Grid profiles are always on the 0.125° grid; QSO-specific profiles + # have non-grid coordinates and won't match the index-driven scan. + %{num_rows: deleted} = + Repo.query!( + "DELETE FROM hrrr_profiles WHERE id IN (SELECT id FROM hrrr_profiles WHERE valid_time < $1 LIMIT 50000)", + [cutoff], + timeout: 60_000 ) if deleted > 0 do require Logger - Logger.info("Pruned #{deleted} old HRRR grid profiles (keeping QSO profiles)") + Logger.info("Pruned #{deleted} old HRRR grid profiles") end deleted