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
feat(channels): parallelize queries in attachment references (#3654)
teo64aa1dc6638e
diff --git a/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs b/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rsindex c625947de7..11523abb56 100644--- a/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs+++ b/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs@@ -1260,9 +1260,10 @@ impl ChannelRepo for PgChannelsRepo {entity_id: &str,user_id: &str,) -> Result<Vec<AttachmentEntityReference>, Self::Err> {- let attachment_references = sqlx::query_as!(- AttachmentChannelReference,- r#"+ let attachment_references_fut = async {+ sqlx::query_as!(+ AttachmentChannelReference,+ r#"SELECTa.channel_id AS "channel_id: uuid::Uuid",c.name AS "channel_name?", -- Option<String>@@ -1283,17 +1284,19 @@ impl ChannelRepo for PgChannelsRepo {AND m.deleted_at IS NULLORDER BY a.created_at DESC"#,- entity_type,- entity_id,- user_id,- )- .fetch_all(&self.pool)- .await- .context("failed to get attachment references")?;+ entity_type,+ entity_id,+ user_id,+ )+ .fetch_all(&self.pool)+ .await+ .context("failed to get attachment references")+ };- let mention_references = sqlx::query_as!(- AttachmentChannelReference,- r#"+ let mention_references_fut = async {+ sqlx::query_as!(+ AttachmentChannelReference,+ r#"SELECTm.channel_id AS "channel_id: uuid::Uuid",c.name AS "channel_name?", -- Option<String>@@ -1314,16 +1317,18 @@ impl ChannelRepo for PgChannelsRepo {AND m.deleted_at IS NULLORDER BY em.created_at DESC"#,- entity_type,- entity_id,- user_id,- )- .fetch_all(&self.pool)- .await- .context("failed to get mention references")?;+ entity_type,+ entity_id,+ user_id,+ )+ .fetch_all(&self.pool)+ .await+ .context("failed to get mention references")+ };- let generic_references = sqlx::query!(- r#"+ let generic_references_fut = async {+ sqlx::query!(+ r#"SELECTem.source_entity_type,em.source_entity_id,@@ -1337,22 +1342,31 @@ impl ChannelRepo for PgChannelsRepo {AND em.source_entity_type != 'message'ORDER BY em.created_at DESC"#,- entity_type,- entity_id,- )- .fetch_all(&self.pool)- .await- .context("failed to get generic entity references")?- .into_iter()- .map(|row| AttachmentGenericReference {- source_entity_type: row.source_entity_type,- source_entity_id: row.source_entity_id,- entity_type: row.entity_type,- entity_id: row.entity_id,- user_id: row.user_id,- created_at: row.created_at,- })- .collect::<Vec<_>>();+ entity_type,+ entity_id,+ )+ .fetch_all(&self.pool)+ .await+ .context("failed to get generic entity references")+ };++ let (attachment_references, mention_references, generic_rows) = tokio::try_join!(+ attachment_references_fut,+ mention_references_fut,+ generic_references_fut,+ )?;++ let generic_references = generic_rows+ .into_iter()+ .map(|row| AttachmentGenericReference {+ source_entity_type: row.source_entity_type,+ source_entity_id: row.source_entity_id,+ entity_type: row.entity_type,+ entity_id: row.entity_id,+ user_id: row.user_id,+ created_at: row.created_at,+ })+ .collect::<Vec<_>>();let mut references: Vec<AttachmentEntityReference> = attachment_references.into_iter()