Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

[db-perf] perf(channels): read only thread-bearing notifications on Home unread (#9636)

teoc4d7679c6aa0
diff --git a/crates/channels/AGENTS.md b/crates/channels/AGENTS.md
index b93e3444b7..368aab6d8d 100644
--- a/crates/channels/AGENTS.md
+++ b/crates/channels/AGENTS.md
@@ -94,13 +94,15 @@ DMs, and the shared message store other domains discuss things on.
records, calls) are authorized in `ChannelServiceImpl::get_attachment_references`
through the `CommentParentAccess` port; the repo returns every parent. A
service built without `with_comment_parent_access` withholds them.
-- The channel list's unseen/seen filter matches a set built once from the
- user's notifications (`= ANY(ARRAY(...))`). A per-channel `EXISTS` or plain
- `IN` walks every candidate channel's notification history; that was ~24% of
- the prod read replica's buffer hits.
-- Notification filters read `user_notification`'s copied entity columns, not a
- `notification` join, and compare `c.id` as a uuid so the channel primary key
- stays usable. Web Home sends `(unseen OR seen) AND unseen`;
+- Notification filters read `user_notification`'s copied entity columns, never a
+ `notification` join: through `notification`, a per-channel check walks every
+ user's history on the channel (once ~24% of the prod read replica's buffer
+ hits). The channel list seeks `idx_user_notification_viewer_entity` once per
+ candidate channel; an array of the user's notified channels reads every
+ notification the user has in that state, PR ones included.
+- The thread feed's notified roots skip `channel_message_send` rows, which never
+ name a thread, so `idx_user_notification_channel_threads` holds only rows
+ that can. Web Home sends `(unseen OR seen) AND unseen`;
`drop_implied_notification_conjuncts` folds that to one notification read.
## Related
diff --git a/crates/channels/src/outbound/pg_channels_repo.rs b/crates/channels/src/outbound/pg_channels_repo.rs
index fa80ba3497..0a91c38388 100644
--- a/crates/channels/src/outbound/pg_channels_repo.rs
+++ b/crates/channels/src/outbound/pg_channels_repo.rs
@@ -589,32 +589,17 @@ static CHANNEL_LIST_SELECT: &str = r#"
/// Matches channels where the user has a live notification in `state`.
///
-/// Both forms read only the user's own channel rows through
-/// `idx_user_notification_viewer_entity`, which carries the entity, so neither
-/// joins `notification`. Unseen and seen rows are few per user, so those states
-/// collect the user's notified channels once: `= ANY(ARRAY(...))` keeps that
-/// set an InitPlan, where a plain `IN` is pulled up into a per-channel
-/// semi-join. Done rows accumulate without bound, so done seeks the index once
-/// per candidate channel instead.
+/// Each candidate channel is one seek on `idx_user_notification_viewer_entity`,
+/// whose key is the user, the entity and the state, so the cost is bounded by
+/// the user's channels. Collecting the user's notified channels into an array
+/// first reads every notification the user has in `state`: the planner reaches
+/// them through `(user_id, state)` and discards other entity types on the heap,
+/// which for a user with many pull request notifications is most of the read.
#[cfg(feature = "list")]
fn build_channel_notification_state_clause(state: item_filters::NotificationState) -> String {
let predicate = notification_state_predicate(state);
- match state {
- item_filters::NotificationState::Unseen | item_filters::NotificationState::Seen => {
- format!(
- r#"c.id = ANY(ARRAY(
- SELECT DISTINCT un.event_item_id::uuid
- FROM user_notification un
- WHERE un.user_id = $1
- AND un.deleted_at IS NULL
- AND un.event_item_type = 'channel'
- AND {predicate}
- AND un.event_item_id ~ '{UUID_TEXT_PATTERN}'
- ))"#
- )
- }
- item_filters::NotificationState::Done => format!(
- r#"EXISTS (
+ format!(
+ r#"EXISTS (
SELECT 1
FROM user_notification un
WHERE un.user_id = $1
@@ -623,8 +608,7 @@ fn build_channel_notification_state_clause(state: item_filters::NotificationStat
AND un.event_item_id = (c.id)::text
AND {predicate}
)"#
- ),
- }
+ )
}
#[cfg(feature = "list")]
@@ -931,39 +915,52 @@ fn push_channel_thread_participant_filter_expr(
r#"(EXISTS (
SELECT 1
FROM comms_channel_participants pcp
- WHERE m.parent_entity_type = 'channel' AND m.parent_entity_id = pcp.channel_id::text
+ WHERE pcp.channel_id = m.channel_message_parent_id
+ AND pcp.channel_id::text = m.parent_entity_id
AND pcp.user_id = "#,
);
builder.push_bind(participant.clone());
+ // The root and its replies are checked apart: one `tm.id = m.id OR
+ // tm.thread_id = m.id` scan cannot use either index on its own.
builder.push(
r#"
AND pcp.left_at IS NULL
- ) AND EXISTS (
+ ) AND ((m.deleted_at IS NULL AND "#,
+ );
+ push_channel_message_participation_expr(builder, "m", &participant);
+ builder.push(
+ r#") OR EXISTS (
SELECT 1
FROM comms_messages tm
- WHERE (tm.id = m.id OR tm.thread_id = m.id)
+ WHERE tm.thread_id = m.id
AND tm.deleted_at IS NULL
- AND (
- tm.sender_id = "#,
- );
- builder.push_bind(participant.clone());
- builder.push(
- r#"
- OR EXISTS (
- SELECT 1
- FROM comms_entity_mentions em
- WHERE em.source_entity_type = 'message'
- AND em.source_entity_id = tm.id::text
- AND em.entity_type = 'user'
- AND em.entity_id = "#,
+ AND "#,
);
- builder.push_bind(participant);
- builder.push(
+ push_channel_message_participation_expr(builder, "tm", &participant);
+ builder.push(")))");
+}
+
+/// Whether `participant` sent or is @-mentioned in the message aliased `message`.
+#[cfg(feature = "list")]
+fn push_channel_message_participation_expr(
+ builder: &mut QueryBuilder<'static, Postgres>,
+ message: &'static str,
+ participant: &str,
+) {
+ builder.push(format!("({message}.sender_id = "));
+ builder.push_bind(participant.to_owned());
+ builder.push(format!(
r#"
- )
- )
- ))"#,
- );
+ OR EXISTS (
+ SELECT 1
+ FROM comms_entity_mentions em
+ WHERE em.source_entity_type = 'message'
+ AND em.source_entity_id = {message}.id::text
+ AND em.entity_type = 'user'
+ AND em.entity_id = "#
+ ));
+ builder.push_bind(participant.to_owned());
+ builder.push("))");
}
#[cfg(feature = "list")]
@@ -1027,8 +1024,10 @@ fn channel_thread_expr_mentions_notification(expr: &Expr<ChannelThreadLiteral>)
/// Root ids of channel-message notifications in `state_predicate`.
///
/// `$1` is the requesting user, the first bind `build_channel_thread_notification_query`
-/// pushes. The user's rows are narrowed to channel notifications through
-/// `idx_user_notification_viewer_entity` before any `notification` lookup.
+/// pushes. Message sends never carry a thread and are most of a user's channel
+/// notifications, so skipping them by the copied event type lets
+/// `idx_user_notification_channel_threads` return only rows that can name a
+/// root before any `notification` lookup.
/// Each arm fences the user's rows and the notification lookup with
/// `OFFSET 0`. Without the inner fence Postgres hashes every `channel_message`
/// notification in the table when that type's statistics are too low. AND is
@@ -1045,6 +1044,7 @@ fn channel_message_notification_ids_sql(state_predicate: &str) -> String {
WHERE un.user_id = $1
AND un.deleted_at IS NULL
AND un.event_item_type = 'channel'
+ AND un.notification_event_type <> 'channel_message_send'
AND {state_predicate}
OFFSET 0
) un
@@ -1203,7 +1203,11 @@ fn build_channel_thread_notification_query(
JOIN notified
ON notified.root_id = m.id
AND notified.channel_id = m.parent_entity_id
- JOIN comms_channels c ON c.id::text = m.parent_entity_id
+ -- The generated uuid parent reaches the channel and participant keys;
+ -- the text comparisons keep the exact matching of the stored ids.
+ JOIN comms_channels c
+ ON c.id = m.channel_message_parent_id
+ AND c.id::text = m.parent_entity_id
LEFT JOIN LATERAL (
SELECT MAX(reply.updated_at) AS latest_reply_updated_at
FROM comms_messages reply
@@ -1216,7 +1220,8 @@ fn build_channel_thread_notification_query(
AND EXISTS (
SELECT 1
FROM comms_channel_participants cp
- WHERE cp.channel_id::text = m.parent_entity_id
+ WHERE cp.channel_id = m.channel_message_parent_id
+ AND cp.channel_id::text = m.parent_entity_id
AND cp.user_id = "#
));
// First bind. The `$1` literals inside `ids_sql` are this same user.
diff --git a/crates/channels/src/outbound/pg_channels_repo/tests.rs b/crates/channels/src/outbound/pg_channels_repo/tests.rs
index 254f515e35..2e3500f9a8 100644
--- a/crates/channels/src/outbound/pg_channels_repo/tests.rs
+++ b/crates/channels/src/outbound/pg_channels_repo/tests.rs
@@ -377,16 +377,15 @@ async fn seed_channel_list_notifications(pool: &Pool<Postgres>) -> anyhow::Resul
}
#[test]
-fn channel_list_pending_states_read_the_user_notifications_once() {
+fn channel_list_states_seek_the_viewer_entity_index() {
use item_filters::NotificationState::{Done, Seen, Unseen};
- for state in [Unseen, Seen] {
+ for state in [Unseen, Seen, Done] {
let sql = super::build_channel_notification_state_clause(state);
- assert!(sql.contains("= ANY(ARRAY("), "{sql}");
- assert!(!sql.contains("n.event_item_id = (c.id)"), "{sql}");
+ assert!(sql.contains("un.event_item_id = (c.id)::text"), "{sql}");
+ assert!(!sql.contains("ARRAY("), "{sql}");
+ assert!(!sql.contains("JOIN notification"), "{sql}");
}
- let done = super::build_channel_notification_state_clause(Done);
- assert!(done.contains("n.event_item_id = (c.id)::text"), "{done}");
}
#[sqlx::test(
@@ -3022,6 +3021,16 @@ fn home_unread_thread_feed_reads_notifications_once() {
assert_eq!(sql.matches("FROM user_notification").count(), 1, "{sql}");
assert!(!sql.contains("INTERSECT"), "{sql}");
assert!(!sql.contains("UNION"), "{sql}");
+ // Matches idx_user_notification_channel_threads' predicate.
+ assert!(
+ sql.contains("un.notification_event_type <> 'channel_message_send'"),
+ "{sql}"
+ );
+ assert!(!sql.contains("tm.id = m.id OR"), "{sql}");
+ assert!(
+ sql.contains("ON c.id = m.channel_message_parent_id"),
+ "{sql}"
+ );
}
#[sqlx::test(
diff --git a/crates/macro_db_client/migrations/20261105003357_user_notification_channel_threads_idx.sql b/crates/macro_db_client/migrations/20261105003357_user_notification_channel_threads_idx.sql
new file mode 100644
index 0000000000..ef59236f91
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261105003357_user_notification_channel_threads_idx.sql
@@ -0,0 +1,13 @@
+-- no-transaction
+-- One viewer's channel notifications that can name a thread, by state. The
+-- channel thread feed collects its notified roots from these rows; message
+-- sends, the bulk of channel notifications, never carry a thread.
+-- Must stay a single statement: sqlx sends no-transaction migrations as one
+-- batch, and CONCURRENTLY cannot run inside a transaction. If an interrupted
+-- deploy leaves an INVALID index behind, IF NOT EXISTS will not rebuild it;
+-- drop it by hand and re-run.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_user_notification_channel_threads
+ ON user_notification (user_id, state, notification_id)
+ WHERE deleted_at IS NULL
+ AND event_item_type = 'channel'
+ AND notification_event_type <> 'channel_message_send';