Add primary key and indexes to Postgres Queue provider
Feature Request
Is your feature request related to a problem? Please describe.
The pg_loco_queue table is created without a primary key or indexes, which causes every dequeue, completion, or failure to perform a seq scan. This results in very poor performance on large tables.
Describe the solution you'd like
Add a primary key on id as well as a partial index on (status, priority DESC, run_at, id) for the pg_loco_queue to allow for index scans of the queue operations. We may also consider adding a primary key to the sqlite driver as well, as it is also missing one.
Ran some local container benchmarks with Claude to reproduce:
| Indexes | Dequeue SELECT (auto / forced generic) |
UPDATE … WHERE id = $2 |
|---|---|---|
| none (upstream DDL) | Seq Scan 144 ms / Seq Scan 167 ms | Seq Scan, 110 ms, removes 1,499,999 rows |
+ PK on id |
Index Scan + Sort 0.8 ms / Seq Scan 153 ms | Index Scan, 0.011 ms |
PK + (status, priority DESC, run_at, id) |
Index Scan, no Sort, 0.010 ms / same | Index Scan, 0.010 ms |
Describe alternatives you've considered
Clearing out the queue constantly - puts additional load on the database and seems like a mitigation rather than a solution as it would still be slow without an index.
Source: loco-rs/loco