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
Improve upload claim query performance (#1406)
Evan Hutnik2f7c65718b26
diff --git a/rust/cloud-storage/email_db_client/src/attachments/provider/upload.rs b/rust/cloud-storage/email_db_client/src/attachments/provider/upload.rsindex 0746f1eb69..6b52fd331c 100644--- a/rust/cloud-storage/email_db_client/src/attachments/provider/upload.rs+++ b/rust/cloud-storage/email_db_client/src/attachments/provider/upload.rs@@ -236,6 +236,7 @@ pub async fn fetch_job_attachments_for_backfill(#[tracing::instrument(skip(db), err)]pub async fn new_email_document_atts(db: &Pool<Postgres>,+ link_id: Uuid,message_provider_id: &str,) -> anyhow::Result<Vec<AttachmentUploadMetadata>> {// query for conditions 1-4: claim attachments atomically and return metadata@@ -249,7 +250,8 @@ pub async fn new_email_document_atts(FROM email_attachments aJOIN email_messages m ON a.message_id = m.idLEFT JOIN document_email de ON de.email_attachment_id = a.id- WHERE m.provider_id = $1+ WHERE m.link_id = $2+ AND m.provider_id = $1AND a.filename IS NOT NULL{}AND de.email_attachment_id IS NULL@@ -300,6 +302,7 @@ pub async fn new_email_document_atts(let rows = sqlx::query(&query1).bind(message_provider_id)+ .bind(link_id).fetch_all(db).await?;@@ -322,7 +325,8 @@ pub async fn new_email_document_atts(FROM email_messages mJOIN email_threads t ON m.thread_id = t.idJOIN email_links l ON t.link_id = l.id- WHERE m.provider_id = $1+ WHERE m.link_id = $2+ AND m.provider_id = $1),previously_contacted_emails AS (SELECT DISTINCT ec.email_address@@ -355,7 +359,8 @@ pub async fn new_email_document_atts(FROM email_attachments aJOIN email_messages m ON a.message_id = m.idLEFT JOIN document_email de ON de.email_attachment_id = a.id- WHERE m.provider_id = $1+ WHERE m.link_id = $2+ AND m.provider_id = $1AND de.email_attachment_id IS NULLAND a.upload_claimed_at IS NULLAND a.filename IS NOT NULL@@ -390,6 +395,7 @@ pub async fn new_email_document_atts(let rows = sqlx::query(&query2).bind(message_provider_id)+ .bind(link_id).fetch_all(db).await?;@@ -409,6 +415,7 @@ pub async fn new_email_document_atts(#[tracing::instrument(skip(db), err)]pub async fn new_email_media_atts(db: &Pool<Postgres>,+ link_id: Uuid,message_provider_id: &str,) -> anyhow::Result<Vec<AttachmentUploadMetadata>> {let query = format!(@@ -421,7 +428,8 @@ pub async fn new_email_media_atts(FROM email_attachments aJOIN email_messages m ON a.message_id = m.idLEFT JOIN email_attachments_sfs eas ON eas.attachment_id = a.id- WHERE m.provider_id = $1+ WHERE m.link_id = $2+ AND m.provider_id = $1AND {}AND eas.attachment_id IS NULLAND a.upload_claimed_at IS NULL@@ -450,6 +458,7 @@ pub async fn new_email_media_atts(let rows = sqlx::query(&query).bind(message_provider_id)+ .bind(link_id).fetch_all(db).await?;diff --git a/rust/cloud-storage/email_db_client/src/attachments/provider/upload/test.rs b/rust/cloud-storage/email_db_client/src/attachments/provider/upload/test.rsindex 8e4cfc3e6a..733697c352 100644--- a/rust/cloud-storage/email_db_client/src/attachments/provider/upload/test.rs+++ b/rust/cloud-storage/email_db_client/src/attachments/provider/upload/test.rs@@ -153,7 +153,12 @@ async fn insertable_attachments_condition_1_user_sent_message(pool: Pool<Postgre// Test condition 1: user sent the messagelet message_provider_id = "target-msg-101";- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000001a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 1 attachment (sent_message_doc.pdf)assert_eq!(res.len(), 1);@@ -176,7 +181,12 @@ async fn insertable_attachments_condition_2_important_label(pool: Pool<Postgres>// Test condition 2: message has IMPORTANT labellet message_provider_id = "target-msg-201";- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000001a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 1 attachment (important_doc.pdf)assert_eq!(res.len(), 1);@@ -199,7 +209,12 @@ async fn insertable_attachments_condition_3_same_domain(pool: Pool<Postgres>) ->// Test condition 3: message from same domain as userlet message_provider_id = "target-msg-301";- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000001a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 1 attachment (same_domain_doc.pdf)assert_eq!(res.len(), 1);@@ -222,7 +237,12 @@ async fn insertable_attachments_condition_4_whitelisted_domain(pool: Pool<Postgr// Test condition 4: message from whitelisted domainlet message_provider_id = "target-msg-801";- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000001a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 1 attachment (whitelisted_domain_doc.pdf)assert_eq!(res.len(), 1);@@ -250,7 +270,12 @@ async fn insertable_attachments_condition_5_previously_contacted(// Test condition 4: user has previously contacted a thread participantlet message_provider_id = "target-msg-401";- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000001a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 1 attachment (previously_contacted_doc.pdf)// This should be found by the second query (condition 4)@@ -277,7 +302,12 @@ async fn insertable_attachments_no_conditions_met(pool: Pool<Postgres>) -> Resul// Test control case: no conditions metlet message_provider_id = "target-msg-601";- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000001a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 0 attachmentsassert_eq!(res.len(), 0);@@ -297,7 +327,12 @@ async fn insertable_attachments_excludes_already_uploaded(pool: Pool<Postgres>)// Test document_email exclusionlet message_provider_id = "target-msg-701";- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000001a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 0 attachments (attachment already exists in document_email)assert_eq!(res.len(), 0);@@ -317,7 +352,12 @@ async fn insertable_attachments_filters_mime_types(pool: Pool<Postgres>) -> Resu// Test that filtered mime types are excludedlet message_provider_id = "target-msg-101";- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000001a").unwrap(),+ message_provider_id,+ )+ .await?;// Should only return PDF, not image or zipassert_eq!(res.len(), 1);@@ -350,7 +390,12 @@ async fn insertable_attachments_thread_exists_logic(pool: Pool<Postgres>) -> Res// target-msg-202 is in Thread 2, which contains target-msg-201 with IMPORTANT labellet message_provider_id = "other-msg-202";- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000001a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 0 because other-msg-202 has no attachments// But the EXISTS clause should still evaluate to true due to target-msg-201 having IMPORTANT@@ -376,7 +421,12 @@ async fn insertable_attachments_returns_first_query_when_available(// Use a message that meets condition 1 (is_sent = true)let message_provider_id = "target-msg-101";- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000001a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return result from first queryassert_eq!(res.len(), 1);@@ -496,7 +546,12 @@ async fn thread_media_for_backfill_returns_empty_for_thread_without_media()]async fn new_email_media_includes_non_inline_attachments(pool: Pool<Postgres>) -> Result<()> {let message_provider_id = "new-media-msg-301";- let res = new_email_media_atts(&pool, message_provider_id).await?;+ let res = new_email_media_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000003a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 2 attachments: new_photo.jpg and new_video.mp4assert_eq!(res.len(), 2);@@ -522,7 +577,12 @@ async fn new_email_media_includes_non_inline_attachments(pool: Pool<Postgres>) -)]async fn new_email_media_excludes_already_uploaded_to_sfs(pool: Pool<Postgres>) -> Result<()> {let message_provider_id = "new-media-msg-302";- let res = new_email_media_atts(&pool, message_provider_id).await?;+ let res = new_email_media_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000003a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 0 attachments (attachment already exists in email_attachments_sfs)assert_eq!(res.len(), 0);@@ -536,7 +596,12 @@ async fn new_email_media_excludes_already_uploaded_to_sfs(pool: Pool<Postgres>))]async fn new_email_media_filters_inline_images(pool: Pool<Postgres>) -> Result<()> {let message_provider_id = "new-media-msg-303";- let res = new_email_media_atts(&pool, message_provider_id).await?;+ let res = new_email_media_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000003a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 2 attachment: attachment_image.png// Should include inline_image.png (has content_id)@@ -555,7 +620,12 @@ async fn new_email_media_filters_inline_images(pool: Pool<Postgres>) -> Result<()]async fn new_email_media_returns_empty_for_nonexistent_message(pool: Pool<Postgres>) -> Result<()> {let message_provider_id = "nonexistent-msg-id";- let res = new_email_media_atts(&pool, message_provider_id).await?;+ let res = new_email_media_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000003a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 0 attachmentsassert_eq!(res.len(), 0);@@ -577,7 +647,12 @@ async fn new_email_document_atts_claims_attachments(pool: Pool<Postgres>) -> Reslet message_provider_id = "claim-test-doc-msg";// First call should return attachments and claim them- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-000000000c01").unwrap(),+ message_provider_id,+ )+ .await?;assert_eq!(res.len(), 2);// Verify the attachments were claimed (upload_claimed_at is now set)@@ -595,7 +670,12 @@ async fn new_email_document_atts_claims_attachments(pool: Pool<Postgres>) -> Res}// Second call should return empty (attachments already claimed)- let res2 = new_email_document_atts(&pool, message_provider_id).await?;+ let res2 = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-000000000c01").unwrap(),+ message_provider_id,+ )+ .await?;assert_eq!(res2.len(),0,@@ -615,7 +695,12 @@ async fn new_email_media_atts_claims_attachments(pool: Pool<Postgres>) -> Resultlet message_provider_id = "claim-test-media-msg";// First call should return attachments and claim them- let res = new_email_media_atts(&pool, message_provider_id).await?;+ let res = new_email_media_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-000000000c01").unwrap(),+ message_provider_id,+ )+ .await?;assert_eq!(res.len(), 2);// Verify the attachments were claimed (upload_claimed_at is now set)@@ -633,7 +718,12 @@ async fn new_email_media_atts_claims_attachments(pool: Pool<Postgres>) -> Result}// Second call should return empty (attachments already claimed)- let res2 = new_email_media_atts(&pool, message_provider_id).await?;+ let res2 = new_email_media_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-000000000c01").unwrap(),+ message_provider_id,+ )+ .await?;assert_eq!(res2.len(),0,@@ -655,7 +745,12 @@ async fn new_email_document_atts_excludes_preclaimed_attachments(// This message has an attachment that was pre-claimed (upload_claimed_at already set)let message_provider_id = "claim-test-preclaimed-msg";- let res = new_email_document_atts(&pool, message_provider_id).await?;+ let res = new_email_document_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-00000000001a").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 0 attachments since the attachment is already claimedassert_eq!(@@ -677,7 +772,12 @@ async fn new_email_media_atts_excludes_preclaimed_attachments(pool: Pool<Postgre// This message has an attachment that was pre-claimed (upload_claimed_at already set)let message_provider_id = "claim-test-preclaimed-media-msg";- let res = new_email_media_atts(&pool, message_provider_id).await?;+ let res = new_email_media_atts(+ &pool,+ Uuid::parse_str("00000000-0000-0000-0000-000000000c01").unwrap(),+ message_provider_id,+ )+ .await?;// Should return 0 attachments since the attachment is already claimedassert_eq!(diff --git a/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs b/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rsindex 7eca63cc73..5cff4de854 100644--- a/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs+++ b/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs@@ -229,10 +229,12 @@ async fn handle_attachment_upload(let (document_atts, media_atts) = tokio::try_join!(email_db_client::attachments::provider::upload::new_email_document_atts(&ctx.db,+ link.id,&payload.provider_message_id,),email_db_client::attachments::provider::upload::new_email_media_atts(&ctx.db,+ link.id,&payload.provider_message_id,))diff --git a/rust/cloud-storage/macro_db_client/migrations/20260209101421_email_sent_at_index.sql b/rust/cloud-storage/macro_db_client/migrations/20260209101421_email_sent_at_index.sqlnew file mode 100644index 0000000000..ea824bec53--- /dev/null+++ b/rust/cloud-storage/macro_db_client/migrations/20260209101421_email_sent_at_index.sql@@ -0,0 +1,3 @@+CREATE INDEX IF NOT EXISTS idx_email_messages_link_id_sent+ ON email_messages (link_id)+ WHERE is_sent = true;\ No newline at end of file