fix: trend_windows now upserts instead of accumulating (7.5GB→4MB), add competitive signal retention cleanup

This commit is contained in:
Celes Renata
2026-04-16 14:32:24 +00:00
parent 58a8726306
commit 6bab199159
3 changed files with 64 additions and 0 deletions
@@ -0,0 +1,23 @@
-- Fix trend_windows to upsert instead of accumulating rows.
-- Add unique constraint so ON CONFLICT works, then deduplicate existing data.
-- Step 1: Keep only the most recent row per (entity_type, entity_id, window)
DELETE FROM trend_windows
WHERE id NOT IN (
SELECT DISTINCT ON (entity_type, entity_id, "window") id
FROM trend_windows
ORDER BY entity_type, entity_id, "window", generated_at DESC
);
-- Step 2: Add unique constraint for upsert
CREATE UNIQUE INDEX IF NOT EXISTS idx_trend_windows_entity_window
ON trend_windows (entity_type, entity_id, "window");
-- Step 3: Clean up old competitive signal records (keep last 30 days)
DELETE FROM competitive_signal_records
WHERE computed_at < NOW() - INTERVAL '30 days';
-- Step 4: Add a partial index to speed up the NOT EXISTS check in the
-- aggregation propagation query
CREATE INDEX IF NOT EXISTS idx_competitive_signals_source_doc_ticker
ON competitive_signal_records (source_document_id, source_ticker);