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(contacts): make the contacts list an index-only read (#7556)
teo9a2941b5eece
diff --git a/.sqlx/query-aab8538835af1c079c4864c1a576b3a51049dd104e2306c4596f1ff99ec329c3.json b/.sqlx/query-0e13c6e3a10deeb2df83d245e2cdecfb8675fff6f0787a98e080b10923b32a2e.jsonsimilarity index 59%rename from .sqlx/query-aab8538835af1c079c4864c1a576b3a51049dd104e2306c4596f1ff99ec329c3.jsonrename to .sqlx/query-0e13c6e3a10deeb2df83d245e2cdecfb8675fff6f0787a98e080b10923b32a2e.jsonindex a77bb0ab76..9b1f35e89f 100644--- a/.sqlx/query-aab8538835af1c079c4864c1a576b3a51049dd104e2306c4596f1ff99ec329c3.json+++ b/.sqlx/query-0e13c6e3a10deeb2df83d245e2cdecfb8675fff6f0787a98e080b10923b32a2e.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT user1 AS contact FROM contacts_connections WHERE user2 = $1\n UNION\n SELECT user2 AS contact FROM contacts_connections WHERE user1 = $1\n ",+ "query": "\n SELECT user1 AS contact FROM contacts_connections WHERE user2 = $1\n UNION ALL\n SELECT user2 AS contact FROM contacts_connections WHERE user1 = $1\n ","describe": {"columns": [{@@ -18,5 +18,5 @@null]},- "hash": "aab8538835af1c079c4864c1a576b3a51049dd104e2306c4596f1ff99ec329c3"+ "hash": "0e13c6e3a10deeb2df83d245e2cdecfb8675fff6f0787a98e080b10923b32a2e"}diff --git a/crates/contacts/src/outbound/repository.rs b/crates/contacts/src/outbound/repository.rsindex 332301fe56..93efc4eb64 100644--- a/crates/contacts/src/outbound/repository.rs+++ b/crates/contacts/src/outbound/repository.rs@@ -28,10 +28,12 @@ impl ContactsRepository for DbContactsRepository {&self,user_id: MacroUserIdStr<'_>,) -> Result<Vec<MacroUserIdStr<'static>>, Report> {+ // Each pair is stored once with user1 < user2, so the branches are+ // disjoint and neither repeats a contact.let rows = sqlx::query!("SELECT user1 AS contact FROM contacts_connections WHERE user2 = $1- UNION+ UNION ALLSELECT user2 AS contact FROM contacts_connections WHERE user1 = $1",user_id.as_ref()diff --git a/crates/contacts/src/outbound/repository/fixtures/both_sides.sql b/crates/contacts/src/outbound/repository/fixtures/both_sides.sqlnew file mode 100644index 0000000000..790c91961d--- /dev/null+++ b/crates/contacts/src/outbound/repository/fixtures/both_sides.sql@@ -0,0 +1,6 @@+INSERT INTO contacts_connections(user1, user2) VALUES+ ('macro|[email protected]', 'macro|[email protected]'),+ ('macro|[email protected]', 'macro|[email protected]'),+ ('macro|[email protected]', 'macro|[email protected]'),+ ('macro|[email protected]', 'macro|[email protected]'),+ ('macro|[email protected]', 'macro|[email protected]');diff --git a/crates/contacts/src/outbound/repository/test.rs b/crates/contacts/src/outbound/repository/test.rsindex f8bd4dd5a1..32162a0998 100644--- a/crates/contacts/src/outbound/repository/test.rs+++ b/crates/contacts/src/outbound/repository/test.rs@@ -77,6 +77,32 @@ async fn test_get_contacts(pool: PgPool) -> sqlx::Result<()> {Ok(())}+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "fixtures", scripts("both_sides"))+)]+async fn test_get_contacts_reads_both_columns_once(pool: PgPool) -> sqlx::Result<()> {+ let repo = DbContactsRepository::new(pool);+ let mut contacts: Vec<String> = repo+ .get_contacts(mid("macro|[email protected]"))+ .await+ .unwrap()+ .iter()+ .map(|c| c.as_ref().to_owned())+ .collect();+ contacts.sort();+ assert_eq!(+ contacts,+ [+ "macro|[email protected]",+ "macro|[email protected]",+ "macro|[email protected]",+ "macro|[email protected]",+ ]+ );+ Ok(())+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn test_create_connections_batch(pool: PgPool) -> sqlx::Result<()> {let connections: Vec<(MacroUserIdStr<'static>, MacroUserIdStr<'static>)> = (0..8)diff --git a/crates/macro_db_client/migrations/20261101055556_contacts_connections_user2_user1_index.sql b/crates/macro_db_client/migrations/20261101055556_contacts_connections_user2_user1_index.sqlnew file mode 100644index 0000000000..086eca882b--- /dev/null+++ b/crates/macro_db_client/migrations/20261101055556_contacts_connections_user2_user1_index.sql@@ -0,0 +1,12 @@+-- no-transaction+-- The contacts list selects user1 by user2. Keying user1 too lets that branch+-- be answered from the index without visiting the table.+--+-- 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. No IF NOT EXISTS: if an+-- interrupted build leaves an INVALID index, the next run fails on it instead+-- of recording success, so the indexes this one replaces are never dropped+-- without it; drop it with DROP INDEX CONCURRENTLY and re-run.+CREATE INDEX CONCURRENTLY idx_contacts_connections_user2_user1+ ON contacts_connections (user2, user1);diff --git a/crates/macro_db_client/migrations/20261101055558_drop_contacts_connections_user2_index.sql b/crates/macro_db_client/migrations/20261101055558_drop_contacts_connections_user2_index.sqlnew file mode 100644index 0000000000..058ac36034--- /dev/null+++ b/crates/macro_db_client/migrations/20261101055558_drop_contacts_connections_user2_index.sql@@ -0,0 +1,5 @@+-- no-transaction+-- Superseded by idx_contacts_connections_user2_user1, whose leading column is+-- user2. Single statement on purpose: see+-- 20261101055556_contacts_connections_user2_user1_index.sql.+DROP INDEX CONCURRENTLY IF EXISTS idx_contacts_connections_user2;diff --git a/crates/macro_db_client/migrations/20261101055559_drop_contacts_connections_user1_index.sql b/crates/macro_db_client/migrations/20261101055559_drop_contacts_connections_user1_index.sqlnew file mode 100644index 0000000000..9a8e7d9208--- /dev/null+++ b/crates/macro_db_client/migrations/20261101055559_drop_contacts_connections_user1_index.sql@@ -0,0 +1,5 @@+-- no-transaction+-- The unique (user1, user2) index already serves lookups by user1. Single+-- statement on purpose: see+-- 20261101055556_contacts_connections_user2_user1_index.sql.+DROP INDEX CONCURRENTLY IF EXISTS idx_contacts_connections_user1;diff --git a/crates/macro_db_client/migrations/20261101055560_tune_contacts_connections_autovacuum.sql b/crates/macro_db_client/migrations/20261101055560_tune_contacts_connections_autovacuum.sqlnew file mode 100644index 0000000000..da06896518--- /dev/null+++ b/crates/macro_db_client/migrations/20261101055560_tune_contacts_connections_autovacuum.sql@@ -0,0 +1,18 @@+-- Contact upserts only change updated_at, so they are HOT updates, and HOT+-- pruning subtracts the dead tuples it reclaims from the count that triggers+-- autovacuum. The default 10% threshold is never reached, so the table is+-- vacuumed only when its transaction age forces it, days apart. Pages changed+-- since the last vacuum are not all-visible, and the contacts list's+-- index-only scans read the table for them. Vacuum after a fixed number of+-- changes instead.+--+-- Setting autovacuum options takes SHARE UPDATE EXCLUSIVE, which doesn't block+-- reads or writes but waits behind a running vacuum; give up rather than wait.+SET LOCAL lock_timeout = '5s';++ALTER TABLE public.contacts_connections SET (+ autovacuum_vacuum_scale_factor = 0,+ autovacuum_vacuum_threshold = 1000,+ autovacuum_vacuum_insert_scale_factor = 0,+ autovacuum_vacuum_insert_threshold = 1000+);