Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
perf(email): walk each inbox link in index order (#7607)
teo3c99ae46bd30
diff --git a/crates/email/src/outbound/email_pg_repo/dynamic/filters.rs b/crates/email/src/outbound/email_pg_repo/dynamic/filters.rsindex c0de3d2c4d..5712fbc0e5 100644--- a/crates/email/src/outbound/email_pg_repo/dynamic/filters.rs+++ b/crates/email/src/outbound/email_pg_repo/dynamic/filters.rs@@ -1066,6 +1066,8 @@ pub(super) fn get_sort_timestamp_field(view: &PreviewView) -> &'static str {PreviewView::StandardLabel(PreviewViewStandardLabel::Inbox) => {"GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at)"}+ // Matches idx_email_threads_non_spam_link_ts_id, so a per-link+ // equality scan can stop at LIMIT._ => "COALESCE(t.latest_non_spam_message_ts, t.updated_at)",}}diff --git a/crates/email/src/outbound/email_pg_repo/dynamic/query.rs b/crates/email/src/outbound/email_pg_repo/dynamic/query.rsindex 9c56253b93..aee182af0c 100644--- a/crates/email/src/outbound/email_pg_repo/dynamic/query.rs+++ b/crates/email/src/outbound/email_pg_repo/dynamic/query.rs@@ -124,13 +124,14 @@ fn push_thread_candidate_select() {let defer_uh = !uses_view_history(email_filter, ¶ms.sort_method_str);- // Multi-inbox owned scans fan out one ordered, LIMITed subscan per link:- // `link_id = ANY(...)` index scans can't return ordered output (pre-PG17),- // forcing a fetch-and-sort of every candidate thread across all inboxes.- // Per-link scans walk the index in order and stop at LIMIT.+ // Owned scans fan out one ordered, LIMITed subscan per link.+ // `link_id = ANY(...)` cannot return index order, even for a single id,+ // so the planner fetches every matching thread and sorts. A per-link+ // equality walk stops at LIMIT. Team scope stays on ANY: a per-link+ // LIMIT would starve the cross-mailbox dedupe.let per_link_fanout = matches!(source, ThreadCandidateSource::Owned)&& params.team_id.is_none()- && params.link_ids.len() > 1;+ && !params.link_ids.is_empty();if per_link_fanout {builder.push(diff --git a/crates/email/src/outbound/email_pg_repo/dynamic/tests.rs b/crates/email/src/outbound/email_pg_repo/dynamic/tests.rsindex 0222f4f836..b7e4ea0257 100644--- a/crates/email/src/outbound/email_pg_repo/dynamic/tests.rs+++ b/crates/email/src/outbound/email_pg_repo/dynamic/tests.rs@@ -603,13 +603,20 @@ fn test_build_query_multi_link_fans_out_per_link() {}#[test]-fn test_build_query_single_link_keeps_any_scan() {+fn test_build_query_single_link_fans_out_per_link() {let view = PreviewView::StandardLabel(PreviewViewStandardLabel::Sent);let expr = Expr::Literal(EmailLiteral::CalendarOnly(false));let sql = super::query::debug_build_query_sql(&view, &expr);- assert!(sql.contains("t.link_id = ANY("));- assert!(!sql.contains("FROM unnest("));+ // One id inside ANY still cannot walk the sort index in order.+ assert!(+ sql.contains("FROM unnest(") && sql.contains("t.link_id = links.link_id"),+ "single-link owned scan must fan out: {sql}"+ );+ assert!(+ !sql.contains("t.link_id = ANY("),+ "single-link owned scan must not use = ANY: {sql}"+ );}#[test]@@ -738,13 +745,15 @@ fn test_build_query_non_team_has_no_dedupe_machinery() {// Cursor stays inside the candidate select on the per-mailbox path// (contrast with the team path, which moves it past the dedupe wrapper).// The default UpdatedAt sort defers the uh join, so the cursor is a plain- // (ts, id) comparison rather than the viewed-history CASE.+ // (ts, id) comparison rather than the viewed-history CASE. Owned scans+ // also open a per-link LATERAL before that cursor; the message join is+ // the later one, after the candidate LIMIT.let cursor_pos = sql.find(", t.id) < (").expect("per-mailbox cursor comparison missing");- let lateral_pos = sql.find("CROSS JOIN LATERAL").expect("lateral missing");+ let message_lateral = sql.find("-- Step 2:").expect("message lateral missing");assert!(- cursor_pos < lateral_pos,+ cursor_pos < message_lateral,"cursor must sit inside the candidate select: {sql}");}diff --git a/crates/macro_db_client/migrations/20261006151520_email_threads_non_spam_link_sort.sql b/crates/macro_db_client/migrations/20261006151520_email_threads_non_spam_link_sort.sqlnew file mode 100644index 0000000000..15e87586bb--- /dev/null+++ b/crates/macro_db_client/migrations/20261006151520_email_threads_non_spam_link_sort.sql@@ -0,0 +1,15 @@+-- no-transaction+-- All-mail, drafts, starred, important, other, and user-label thread pages+-- sort by COALESCE(latest_non_spam_message_ts, updated_at) DESC, id DESC.+-- The older idx_email_threads_non_spam_ts_id orders the raw timestamp and+-- skips rows where it is null, so it cannot serve that expression. A+-- per-link equality scan (link_id = $link) stops at LIMIT on this index.+-- Must stay a single statement: sqlx sends no-transaction migrations as one+-- simple-query batch, and a multi-statement batch gets an implicit+-- transaction, which CONCURRENTLY forbids. If an interrupted deploy leaves an+-- INVALID index behind, IF NOT EXISTS will not rebuild it -- drop it by hand+-- and re-run. Check with:+-- SELECT indexrelid::regclass FROM pg_index WHERE NOT indisvalid;+-- Rollback: DROP INDEX CONCURRENTLY IF EXISTS idx_email_threads_non_spam_link_ts_id;+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_email_threads_non_spam_link_ts_id+ ON email_threads (link_id, (COALESCE(latest_non_spam_message_ts, updated_at)) DESC, id DESC);