diff --git a/internal/storage/migrations.go b/internal/storage/migrations.go index 33ff453..144cb53 100644 --- a/internal/storage/migrations.go +++ b/internal/storage/migrations.go @@ -172,6 +172,22 @@ var _migrations = []migrations.Migration{ return nil }, }, + { + // Migration 8 dropped the (trigger_id, event_id) primary key in favour + // of (id), leaving trigger_id unindexed. ListTriggersOccurrences filters + // WHERE trigger_id = ? and orders by date, so without this index every + // page is a sequential scan over an ever-growing table. Built + // CONCURRENTLY to avoid blocking writes during deploy. + Up: func(ctx context.Context, tx bun.IDB) error { + if _, err := tx.ExecContext(ctx, ` + create index concurrently if not exists triggers_occurrences_trigger_id_date_idx + on triggers_occurrences (trigger_id, date); + `); err != nil { + return err + } + return nil + }, + }, } func Migrate(ctx context.Context, db *bun.DB) error {