diff --git a/db/llm_usage_ts_index_test.go b/db/llm_usage_ts_index_test.go new file mode 100644 index 0000000..04f9a40 --- /dev/null +++ b/db/llm_usage_ts_index_test.go @@ -0,0 +1,38 @@ +package db + +import ( + "database/sql" + "errors" + "strings" + "testing" +) + +// Migration 0004 leaves a valid, ready btree index on llm_usage(ts) for the +// time-window queries (UsageDaily, JudgeUsageStats). +func TestLLMUsageTsIndex(t *testing.T) { + d, err := Open(testDSN(t)) + if err != nil { + t.Skipf("postgres unavailable (%v) — skipping", err) + } + t.Cleanup(func() { d.Close() }) + + var valid, ready bool + var def string + err = d.QueryRow(` +SELECT i.indisvalid, i.indisready, pg_get_indexdef(i.indexrelid) +FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid +JOIN pg_namespace n ON n.oid = c.relnamespace +WHERE n.nspname='public' AND c.relname='idx_llm_usage_ts'`).Scan(&valid, &ready, &def) + if errors.Is(err, sql.ErrNoRows) { + t.Fatal("index public.idx_llm_usage_ts does not exist after Open") + } + if err != nil { + t.Fatal(err) + } + if !valid || !ready { + t.Fatalf("idx_llm_usage_ts indisvalid=%v indisready=%v, want both true (%s)", valid, ready, def) + } + if !strings.Contains(def, "USING btree (ts)") { + t.Fatalf("idx_llm_usage_ts definition %q does not index ts with a btree", def) + } +} diff --git a/db/migrate_test.go b/db/migrate_test.go index b392c52..f582fd8 100644 --- a/db/migrate_test.go +++ b/db/migrate_test.go @@ -136,9 +136,9 @@ func assertEmbeddedApplied(t *testing.T, d *DB) []appliedMigration { want := []struct { version int name string - }{{1, llmRecordsMigration}, {2, llmUsageMigration}, {3, "0003_task_scope_icp_index.sql"}} + }{{1, llmRecordsMigration}, {2, llmUsageMigration}, {3, "0003_task_scope_icp_index.sql"}, {4, "0004_llm_usage_ts_index.sql"}} if len(applied) != len(want) { - t.Fatalf("schema_migrations = %+v, want versions 1 to 3", applied) + t.Fatalf("schema_migrations = %+v, want versions 1 to 4", applied) } for i, w := range want { content, err := fs.ReadFile(migrationsFS(), w.name) diff --git a/db/migrations/0004_llm_usage_ts_index.sql b/db/migrations/0004_llm_usage_ts_index.sql new file mode 100644 index 0000000..f9c66d0 --- /dev/null +++ b/db/migrations/0004_llm_usage_ts_index.sql @@ -0,0 +1,15 @@ +-- UsageDaily and JudgeUsageStats (db/llm_usage.go) filter llm_usage on +-- ts >= now() - interval; the dashboard polls UsageDaily every 15 seconds. +-- llm_usage is an append-only ledger that gains a row per LLM call and is never +-- purged, so without an index on ts those queries scan the whole table. +-- +-- This is a plain (non-concurrent) CREATE INDEX: migrations run in a +-- transaction, where CONCURRENTLY is not allowed. It is safe here because +-- db.Open applies migrations at startup, before the HTTP server listens, so the +-- SHARE lock it takes blocks no agent call or request in flight. +-- +-- For an exceptionally large ledger, build the same index online before the +-- upgrade, outside a transaction: +-- CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_llm_usage_ts ON llm_usage(ts); +-- This migration then finds it and does nothing. +CREATE INDEX IF NOT EXISTS idx_llm_usage_ts ON llm_usage(ts);