From 59b92b2205415403580eda013dd7fd2915ee2b17 Mon Sep 17 00:00:00 2001 From: Edward Karavakis Date: Mon, 23 Nov 2026 11:46:14 +0100 Subject: [PATCH] Stop MetricsDB.update() from re-inserting rows it just updated executemanySQL only returns one aggregate rowcount for the whole batch UPDATE, not per-row results, so when any row in a cycle is new, the fallback loop re-attempts an INSERT for every row in the batch - not just the ones that weren't actually updated. Rows that just succeeded the UPDATE hit the unique constraint on (computingSite, gshare, metric) and get logged as a full ORA-00001 error every single cycle this happens, which in production is most cycles given the site/gshare set changes over time. Makes the insert a guarded INSERT ... SELECT ... WHERE NOT EXISTS, matching the pattern DBProxy.backfill_pilot_attributes already uses for the same kind of race - re-inserting an already-present row becomes a harmless no-op instead of a constraint violation. Also adds the same Postgres JSON-cast guard that function needs, even though this daemon isn't enabled on any Postgres deployment today - the schema name itself already gets rewritten transparently for any backend by WrappedCursor's change_schema(), so this would silently break the moment someone enables it there without the guard. --- .../daemons/scripts/metric_collector.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pandaserver/daemons/scripts/metric_collector.py b/pandaserver/daemons/scripts/metric_collector.py index e90974227..7a6ef2f02 100644 --- a/pandaserver/daemons/scripts/metric_collector.py +++ b/pandaserver/daemons/scripts/metric_collector.py @@ -177,7 +177,23 @@ def update(self, metric: str, key_type: str, entity_dict: dict[Any, Any]) -> Non """timestamp = :timestamp """ """WHERE computingSite=:site AND gshare=:gshare AND metric=:metric """ ) - sql_insert = """INSERT INTO ATLAS_PANDA.Metrics """ """VALUES ( """ """:site, :gshare, :metric, :patch_value_json, :timestamp """ """) """ + # guarded against already-updated rows: the fallback below re-attempts every row in the + # batch, not just the ones update() missed (executemanySQL only returns one aggregate + # rowcount, not per-row results), so a plain INSERT would hit the unique constraint + # (computingSite, gshare, metric) on every row that was, in fact, just updated. + # On Postgres a text bind isn't implicitly cast to the json column's type in + # INSERT ... SELECT the way it is in INSERT ... VALUES - see DBProxy.backfill_pilot_attributes + # for the same gotcha. Dead branch under Oracle today since this daemon isn't enabled + # on any Postgres deployment yet. + value_json_bind = "CAST(:patch_value_json AS JSON)" if panda_config.backend == "postgres" else ":patch_value_json" + sql_insert = ( + f"""INSERT INTO ATLAS_PANDA.Metrics (computingSite, gshare, metric, value_json, timestamp) """ + f"""SELECT :site, :gshare, :metric, {value_json_bind}, :timestamp FROM dual """ + """WHERE NOT EXISTS ( """ + """ SELECT 1 FROM ATLAS_PANDA.Metrics """ + """ WHERE computingSite=:site AND gshare=:gshare AND metric=:metric """ + """) """ + ) # now now_time = naive_utcnow() # var map template