From 7ffa8f039b75ca913c95f4397722c71e7533d0e5 Mon Sep 17 00:00:00 2001 From: Lars Date: Sat, 28 Mar 2026 13:22:05 +0100 Subject: [PATCH] fix: PostgreSQL date subtraction in historical value query **Error:** function pg_catalog.extract(unknown, integer) does not exist HINT: No function matches the given name and argument types. **Problem:** In PostgreSQL, date - date returns INTEGER (days), not INTERVAL. EXTRACT(EPOCH FROM integer) fails because EPOCH expects timestamp/interval. **Solution:** Changed from: ORDER BY ABS(EXTRACT(EPOCH FROM (date - '2026-01-01'))) To: ORDER BY ABS(date - '2026-01-01'::date) This directly uses the day difference (integer) for sorting, which is exactly what we need to find the closest date. Co-Authored-By: Claude Opus 4.6 --- backend/routers/goals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/routers/goals.py b/backend/routers/goals.py index 8ab0877..ed4df73 100644 --- a/backend/routers/goals.py +++ b/backend/routers/goals.py @@ -713,7 +713,7 @@ def _get_historical_value_for_goal_type(conn, profile_id: str, goal_type: str, t FROM {source_table} WHERE profile_id = %s AND {date_col} BETWEEN %s AND %s - ORDER BY ABS(EXTRACT(EPOCH FROM ({date_col} - %s::date))) + ORDER BY ABS({date_col} - %s::date) LIMIT 1 """, ( profile_id,