Skip to main content

evento_sql_migrator/m0005/
mod.rs

1//! Migration adding a leading-cursor index for no-routing-key subscription scans.
2//!
3//! The m0004 index `idx_event_type_routing_cursor` places `routing_key` between
4//! `aggregator_type` and the cursor/sort columns, so it only helps subscriptions
5//! that constrain `routing_key`. A subscription reading with `.all()` has no
6//! `routing_key` predicate, so the planner can only use that index up to
7//! `aggregator_type=?` — the `timestamp` range becomes unreachable, forcing a full
8//! scan of every matching row plus an external sort on every poll.
9//!
10//! This migration adds a second, complementary index whose cursor columns come
11//! immediately after `aggregator_type`, letting the `timestamp > ?` cursor bound
12//! push into the index seek so a caught-up poll returns without a scan or sort.
13
14mod event;
15
16use sqlx_migrator::vec_box;
17
18/// Migration that adds a leading-cursor index for `.all()` subscription scans.
19///
20/// ## Changes
21///
22/// - Creates `idx_event_type_cursor` on
23///   `(aggregator_type, timestamp, timestamp_subsec, version, id)`.
24///
25/// ## Notes
26///
27/// - This is additive: `idx_event_type_routing_cursor` (m0004) is kept, since it
28///   still serves subscriptions that filter by `routing_key`.
29/// - The trailing `id` is included so the index fully covers the keyset tiebreaker
30///   (`id` is the final `ORDER BY` / cursor column), avoiding any residual sort.
31/// - It also lets the `latest_timestamp()` query (`MAX(timestamp)` over the same
32///   filter) read the index tail.
33///
34/// ## Dependencies
35///
36/// This migration depends on [`M0004`](crate::M0004).
37pub struct M0005;
38
39#[cfg(feature = "sqlite")]
40sqlx_migrator::sqlite_migration!(
41    M0005,
42    "main",
43    "m0005",
44    vec_box![crate::M0004],
45    vec_box![event::create_type_cursor_idx::Operation]
46);
47
48#[cfg(feature = "mysql")]
49sqlx_migrator::mysql_migration!(
50    M0005,
51    "main",
52    "m0005",
53    vec_box![crate::M0004],
54    vec_box![event::create_type_cursor_idx::Operation]
55);
56
57#[cfg(feature = "postgres")]
58sqlx_migrator::postgres_migration!(
59    M0005,
60    "main",
61    "m0005",
62    vec_box![crate::M0004],
63    vec_box![event::create_type_cursor_idx::Operation]
64);