1import * as Effect from "effect/Effect";
2import * as SqlClient from "effect/sql/SqlClient";
3
4/**
5 * Composite index for windowed thread detail reads. Pagination orders turns by
6 * the stable keyset (requested_at, turn_id); the pre-existing
7 * (thread_id, requested_at) index cannot serve the tiebreak order, forcing a
8 * temp B-tree over all of a thread's turns before the page LIMIT applies.
9 * With this index the candidates scan is genuinely bounded by the page size.
10 */
11export default Effect.gen(function* () {
12 const sql = yield* SqlClient.SqlClient;
13 yield* sql`
14 CREATE INDEX IF NOT EXISTS idx_projection_turns_thread_keyset
15 ON projection_turns(thread_id, requested_at, turn_id)
16 `;
17});