From 0449606ac7d0ac1b5718b7e5e8f5fc9622f1e3ea Mon Sep 17 00:00:00 2001 From: jedarden Date: Thu, 25 Jun 2026 01:03:10 -0400 Subject: [PATCH] fix(db): add LIMIT to pair frequency query to prevent OOMKill in acb-index-builder The fetchOpenPredictions function had an unbounded query building a pair frequency map for rivalry detection. With thousands of bots and matches, this could return tens of thousands of rows and cause OOMKill. - Add ORDER BY COUNT(*) DESC to prioritize most common pairings - Add LIMIT 1000 - sufficient to detect rivalries (pairs with >= 3 matches) This fixes the 45-day CrashLoopBackOff with 4700+ restarts. Co-Authored-By: Claude --- cmd/acb-index-builder/db.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/acb-index-builder/db.go b/cmd/acb-index-builder/db.go index e8ac8c7..d377648 100644 --- a/cmd/acb-index-builder/db.go +++ b/cmd/acb-index-builder/db.go @@ -912,6 +912,8 @@ func fetchOpenPredictions(ctx context.Context, db *sql.DB) ([]OpenPredictionMatc } // Build pair frequency map for rivalry detection (count completed h2h matches) + // LIMIT to most common pairings to prevent OOMKill - we only need enough + // to detect rivalries (typically < 100 pairs have >= 3 matches) pairFrequency := make(map[string]int) freqRows, err := db.QueryContext(ctx, ` SELECT mp1.bot_id, mp2.bot_id, COUNT(*) @@ -920,6 +922,8 @@ func fetchOpenPredictions(ctx context.Context, db *sql.DB) ([]OpenPredictionMatc JOIN match_participants mp2 ON m.match_id = mp2.match_id AND mp2.player_slot = 1 WHERE m.status = 'completed' GROUP BY mp1.bot_id, mp2.bot_id + ORDER BY COUNT(*) DESC + LIMIT 1000 `) if err != nil { return nil, fmt.Errorf("query pair frequency: %w", err)