@@ -160,6 +160,15 @@ begin
160160 ' r_' || p_queue_name
161161 );
162162
163+ -- Partial index for claim candidate ORDER BY (available_at, run_id).
164+ -- Matches the exact ordering used in the claim query for ready runs.
165+ execute format(
166+ ' create index if not exists %I on durable.%I (available_at, run_id) include (task_id)
167+ where state in (' ' pending' ' , ' ' sleeping' ' )' ,
168+ (' r_' || p_queue_name) || ' _ready' ,
169+ ' r_' || p_queue_name
170+ );
171+
163172 execute format(
164173 ' create index if not exists %I on durable.%I (task_id)' ,
165174 (' r_' || p_queue_name) || ' _ti' ,
@@ -172,12 +181,46 @@ begin
172181 ' w_' || p_queue_name
173182 );
174183
184+ -- Speed up cleanup_task_terminal wait deletion by task_id.
185+ execute format(
186+ ' create index if not exists %I on durable.%I (task_id)' ,
187+ (' w_' || p_queue_name) || ' _ti' ,
188+ ' w_' || p_queue_name
189+ );
190+
175191 -- Index for finding children of a parent task (for cascade cancellation)
176192 execute format(
177193 ' create index if not exists %I on durable.%I (parent_task_id) where parent_task_id is not null' ,
178194 (' t_' || p_queue_name) || ' _pti' ,
179195 ' t_' || p_queue_name
180196 );
197+
198+ -- Speed up claim timeout scans.
199+ execute format(
200+ ' create index if not exists %I on durable.%I (claim_expires_at)
201+ where state = ' ' running' ' and claim_expires_at is not null' ,
202+ (' r_' || p_queue_name) || ' _cei' ,
203+ ' r_' || p_queue_name
204+ );
205+
206+ -- Speed up cancellation sweep: only index tasks that have cancellation policies.
207+ execute format(
208+ ' create index if not exists %I on durable.%I (task_id)
209+ where state in (' ' pending' ' , ' ' sleeping' ' , ' ' running' ' )
210+ and cancellation is not null
211+ and (cancellation ? ' ' max_delay' ' or cancellation ? ' ' max_duration' ' )' ,
212+ (' t_' || p_queue_name) || ' _cxlpol' ,
213+ ' t_' || p_queue_name
214+ );
215+
216+ -- Composite index for active task state lookups.
217+ -- Enables Index Only Scans for claim_task join, emit_event, and cancel propagation.
218+ execute format(
219+ ' create index if not exists %I on durable.%I (state, task_id)
220+ where state in (' ' pending' ' , ' ' sleeping' ' , ' ' running' ' , ' ' cancelled' ' )' ,
221+ (' t_' || p_queue_name) || ' _state_tid' ,
222+ ' t_' || p_queue_name
223+ );
181224end;
182225$$;
183226
@@ -346,6 +389,7 @@ begin
346389 -- These are max_delay (delay before starting) and
347390 -- max_duration (duration from created to finished)
348391 -- Use a loop so we can cleanup each cancelled task properly.
392+ -- Only scan tasks that actually have cancellation policies set.
349393 for v_cancelled_task in
350394 execute format(
351395 ' with limits as (
@@ -357,6 +401,8 @@ begin
357401 state
358402 from durable.%I
359403 where state in (' ' pending' ' , ' ' sleeping' ' , ' ' running' ' )
404+ and cancellation is not null
405+ and (cancellation ? ' ' max_delay' ' or cancellation ? ' ' max_duration' ' )
360406 ),
361407 to_cancel as (
362408 select task_id
0 commit comments