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.

perf(notification): read entity notifications through a viewer index (#9051)

teo4ba387709b81
diff --git a/.sqlx/query-11aa415053064f569057bd100e3aa8686566813f38bd7e90d8d076b3cc5a8b94.json b/.sqlx/query-11aa415053064f569057bd100e3aa8686566813f38bd7e90d8d076b3cc5a8b94.json
deleted file mode 100644
index 31c867aa8c..0000000000
--- a/.sqlx/query-11aa415053064f569057bd100e3aa8686566813f38bd7e90d8d076b3cc5a8b94.json
+++ /dev/null
@@ -1,44 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH requested_entities AS (\n SELECT entity_type, entity_id\n FROM UNNEST($2::text[], $3::text[]) AS entity(entity_type, entity_id)\n )\n SELECT un.notification_id\n FROM user_notification un\n JOIN notification n ON n.id = un.notification_id\n WHERE un.user_id = $1\n AND un.deleted_at IS NULL\n AND (un.state = ANY($4::notification_state[]) OR ($5 AND un.seen_at IS NULL))\n AND EXISTS (\n SELECT 1\n FROM requested_entities entity\n WHERE (\n n.event_item_type = entity.entity_type\n AND n.event_item_id = entity.entity_id\n )\n OR (\n n.secondary_event_item_type = entity.entity_type\n AND n.secondary_event_item_id = entity.entity_id\n )\n OR (\n entity.entity_type = 'channel_message'\n AND COALESCE(n.metadata->>'messageId', n.metadata->>'message_id', '') = entity.entity_id\n )\n )\n ORDER BY un.created_at, un.notification_id\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "notification_id",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "TextArray",
- "TextArray",
- {
- "Custom": {
- "name": "notification_state[]",
- "kind": {
- "Array": {
- "Custom": {
- "name": "notification_state",
- "kind": {
- "Enum": [
- "unseen",
- "seen",
- "done"
- ]
- }
- }
- }
- }
- }
- },
- "Bool"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "11aa415053064f569057bd100e3aa8686566813f38bd7e90d8d076b3cc5a8b94"
-}
diff --git a/.sqlx/query-186eca9db06ab5e2d3b9785c178301f56fc2b750da8dbc8c30b8c9ea5ec7e886.json b/.sqlx/query-186eca9db06ab5e2d3b9785c178301f56fc2b750da8dbc8c30b8c9ea5ec7e886.json
new file mode 100644
index 0000000000..eaa2d412aa
--- /dev/null
+++ b/.sqlx/query-186eca9db06ab5e2d3b9785c178301f56fc2b750da8dbc8c30b8c9ea5ec7e886.json
@@ -0,0 +1,44 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH requested_entities AS (\n SELECT entity_type, entity_id\n FROM UNNEST($2::text[], $3::text[]) AS entity(entity_type, entity_id)\n )\n -- The primary entity reads only the viewer's rows from\n -- idx_user_notification_viewer_entity; secondary entities and\n -- message ids match few notifications each.\n SELECT matched.notification_id AS \"notification_id!\"\n FROM (\n SELECT un.notification_id, un.created_at\n FROM requested_entities entity\n JOIN user_notification un\n ON un.event_item_type = entity.entity_type\n AND un.event_item_id = entity.entity_id\n WHERE un.user_id = $1\n AND un.deleted_at IS NULL\n AND (un.state = ANY($4::notification_state[]) OR ($5 AND un.seen_at IS NULL))\n UNION\n SELECT un.notification_id, un.created_at\n FROM requested_entities entity\n JOIN notification n\n ON n.secondary_event_item_type = entity.entity_type\n AND n.secondary_event_item_id = entity.entity_id\n CROSS JOIN LATERAL (\n SELECT un.notification_id, un.created_at\n FROM user_notification un\n WHERE un.user_id = $1 AND un.notification_id = n.id\n AND un.deleted_at IS NULL\n AND (un.state = ANY($4::notification_state[]) OR ($5 AND un.seen_at IS NULL))\n -- Probe the viewer's row per notification: a plain join\n -- can scan the viewer's whole history for each entity.\n OFFSET 0\n ) un\n UNION\n SELECT un.notification_id, un.created_at\n FROM requested_entities entity\n JOIN notification n\n ON COALESCE(n.metadata->>'messageId', n.metadata->>'message_id', '') = entity.entity_id\n CROSS JOIN LATERAL (\n SELECT un.notification_id, un.created_at\n FROM user_notification un\n WHERE un.user_id = $1 AND un.notification_id = n.id\n AND un.deleted_at IS NULL\n AND (un.state = ANY($4::notification_state[]) OR ($5 AND un.seen_at IS NULL))\n OFFSET 0\n ) un\n WHERE entity.entity_type = 'channel_message'\n ) matched\n ORDER BY matched.created_at, matched.notification_id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "notification_id!",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "TextArray",
+ "TextArray",
+ {
+ "Custom": {
+ "name": "notification_state[]",
+ "kind": {
+ "Array": {
+ "Custom": {
+ "name": "notification_state",
+ "kind": {
+ "Enum": [
+ "unseen",
+ "seen",
+ "done"
+ ]
+ }
+ }
+ }
+ }
+ }
+ },
+ "Bool"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "186eca9db06ab5e2d3b9785c178301f56fc2b750da8dbc8c30b8c9ea5ec7e886"
+}
diff --git a/.sqlx/query-9e79198b97dd65b615bdf975fd82c175a75e283465907f992e09983da30953bf.json b/.sqlx/query-9e79198b97dd65b615bdf975fd82c175a75e283465907f992e09983da30953bf.json
deleted file mode 100644
index 62fb065963..0000000000
--- a/.sqlx/query-9e79198b97dd65b615bdf975fd82c175a75e283465907f992e09983da30953bf.json
+++ /dev/null
@@ -1,143 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH requested AS (\n SELECT entity_id, entity_type\n FROM unnest($2::text[], $3::text[]) AS entities(entity_id, entity_type)\n ),\n -- Scope uses all active notifications, not just the requested states: a\n -- seen reply still assigns an unseen root send to its thread row.\n -- Build scope evidence once per batch, never rescan history per send.\n channel_scopes AS MATERIALIZED (\n SELECT DISTINCT n.event_item_id AS channel_id,\n CASE WHEN n.notification_event_type = 'channel_mention'\n THEN COALESCE(n.metadata->>'messageId', n.metadata->>'message_id')\n END AS mentioned_message_id,\n COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') AS thread_id\n FROM requested\n JOIN notification n\n ON n.event_item_id = requested.entity_id AND n.event_item_type = 'channel'\n JOIN user_notification un ON un.notification_id = n.id\n WHERE $8::bool AND requested.entity_type = 'channel'\n AND un.user_id = $1 AND un.deleted_at IS NULL\n AND un.state = ANY($9::notification_state[])\n AND n.notification_event_type IN ('channel_mention', 'channel_message_reply')\n )\n SELECT requested.entity_id AS \"requested_id!\",\n requested.entity_type AS \"requested_type!\",\n matched.notification_id AS \"notification_id!\",\n matched.event_item_id AS \"event_item_id!\",\n matched.event_item_type AS \"event_item_type!\",\n matched.notification_event_type AS \"notification_event_type!\",\n matched.sent AS \"sent!\",\n matched.state AS \"state!: NotificationState\",\n matched.created_at AS \"created_at!\",\n matched.viewed_at,\n matched.notification_metadata,\n matched.sender_id\n FROM requested\n CROSS JOIN LATERAL (\n SELECT un.notification_id, n.event_item_id, n.event_item_type,\n n.notification_event_type, un.sent, un.state,\n un.created_at::timestamptz AS created_at,\n un.seen_at::timestamptz AS viewed_at,\n n.metadata AS notification_metadata, n.sender_id\n FROM (\n SELECT n.id FROM notification n\n WHERE n.event_item_id = requested.entity_id AND n.event_item_type = requested.entity_type\n UNION\n SELECT n.id FROM notification n\n WHERE n.secondary_event_item_id = requested.entity_id AND n.secondary_event_item_type = requested.entity_type\n UNION\n SELECT n.id FROM notification n\n WHERE requested.entity_type = 'channel_message'\n AND COALESCE(n.metadata->>'messageId', n.metadata->>'message_id', '') = requested.entity_id\n ) candidates\n JOIN notification n ON n.id = candidates.id\n JOIN user_notification un ON un.notification_id = n.id\n WHERE un.user_id = $1\n AND un.deleted_at IS NULL\n AND un.state = ANY($4::notification_state[])\n AND (cardinality($5::text[]) = 0 OR n.notification_event_type = ANY($5))\n AND (NOT $7::bool OR (\n n.notification_event_type IN ('channel_message_send', 'channel_mention', 'document_mention')\n AND COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') IS NULL\n ))\n AND (NOT $8::bool OR requested.entity_type <> 'channel' OR (\n n.notification_event_type = 'document_mention'\n OR (n.notification_event_type IN ('channel_message_send', 'channel_message_reply') AND (\n -- Mention stacks consume the mention, not its duplicate send/reply.\n EXISTS (\n SELECT 1 FROM channel_scopes scope\n WHERE scope.channel_id = n.event_item_id\n AND scope.mentioned_message_id = COALESCE(n.metadata->>'messageId', n.metadata->>'message_id')\n )\n OR (n.notification_event_type = 'channel_message_send' AND NOT EXISTS (\n SELECT 1 FROM channel_scopes scope\n WHERE scope.channel_id = n.event_item_id AND scope.thread_id <> ''\n AND scope.thread_id = COALESCE(n.metadata->>'messageId', n.metadata->>'message_id')\n ))\n OR (n.notification_event_type = 'channel_message_reply'\n AND NULLIF(COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id'), '') IS NULL)\n ))\n OR (n.notification_event_type = 'channel_mention' AND (\n COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') = ''\n OR (COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') IS NULL\n AND COALESCE(n.metadata->>'messageId', n.metadata->>'message_id') = ''\n AND EXISTS (SELECT 1 FROM channel_scopes scope\n WHERE scope.channel_id = n.event_item_id AND scope.thread_id = ''))\n ))\n ))\n ORDER BY un.created_at DESC, un.notification_id DESC\n LIMIT $6\n ) matched\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "requested_id!",
- "type_info": "Text"
- },
- {
- "ordinal": 1,
- "name": "requested_type!",
- "type_info": "Text"
- },
- {
- "ordinal": 2,
- "name": "notification_id!",
- "type_info": "Uuid"
- },
- {
- "ordinal": 3,
- "name": "event_item_id!",
- "type_info": "Text"
- },
- {
- "ordinal": 4,
- "name": "event_item_type!",
- "type_info": "Text"
- },
- {
- "ordinal": 5,
- "name": "notification_event_type!",
- "type_info": "Varchar"
- },
- {
- "ordinal": 6,
- "name": "sent!",
- "type_info": "Bool"
- },
- {
- "ordinal": 7,
- "name": "state!: NotificationState",
- "type_info": {
- "Custom": {
- "name": "notification_state",
- "kind": {
- "Enum": [
- "unseen",
- "seen",
- "done"
- ]
- }
- }
- }
- },
- {
- "ordinal": 8,
- "name": "created_at!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 9,
- "name": "viewed_at",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 10,
- "name": "notification_metadata",
- "type_info": "Jsonb"
- },
- {
- "ordinal": 11,
- "name": "sender_id",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "TextArray",
- "TextArray",
- {
- "Custom": {
- "name": "notification_state[]",
- "kind": {
- "Array": {
- "Custom": {
- "name": "notification_state",
- "kind": {
- "Enum": [
- "unseen",
- "seen",
- "done"
- ]
- }
- }
- }
- }
- }
- },
- "TextArray",
- "Int8",
- "Bool",
- "Bool",
- {
- "Custom": {
- "name": "notification_state[]",
- "kind": {
- "Array": {
- "Custom": {
- "name": "notification_state",
- "kind": {
- "Enum": [
- "unseen",
- "seen",
- "done"
- ]
- }
- }
- }
- }
- }
- }
- ]
- },
- "nullable": [
- null,
- null,
- false,
- false,
- false,
- false,
- false,
- false,
- null,
- null,
- false,
- true
- ]
- },
- "hash": "9e79198b97dd65b615bdf975fd82c175a75e283465907f992e09983da30953bf"
-}
diff --git a/.sqlx/query-b0322a63a0545f4efd2db5c4b8d049b6be4bfffb477bcece9c8968d309d22c95.json b/.sqlx/query-b0322a63a0545f4efd2db5c4b8d049b6be4bfffb477bcece9c8968d309d22c95.json
new file mode 100644
index 0000000000..5089c1c8e3
--- /dev/null
+++ b/.sqlx/query-b0322a63a0545f4efd2db5c4b8d049b6be4bfffb477bcece9c8968d309d22c95.json
@@ -0,0 +1,26 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO user_notification (\n notification_id,\n user_id,\n event_item_type,\n event_item_id,\n notification_event_type\n )\n SELECT $1, user_id, $3, $4, $5\n FROM UNNEST($2::text[]) as user_id\n RETURNING created_at::timestamptz as \"created_at!\"\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "created_at!",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "TextArray",
+ "Text",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "b0322a63a0545f4efd2db5c4b8d049b6be4bfffb477bcece9c8968d309d22c95"
+}
diff --git a/.sqlx/query-b89be6ddcd933ff1931751a8f4695b0d9178d912da60a4b8b104663645d547a4.json b/.sqlx/query-b89be6ddcd933ff1931751a8f4695b0d9178d912da60a4b8b104663645d547a4.json
new file mode 100644
index 0000000000..c6b11f06d4
--- /dev/null
+++ b/.sqlx/query-b89be6ddcd933ff1931751a8f4695b0d9178d912da60a4b8b104663645d547a4.json
@@ -0,0 +1,143 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH requested AS (\n SELECT entity_id, entity_type\n FROM unnest($2::text[], $3::text[]) AS entities(entity_id, entity_type)\n ),\n -- Scope uses all active notifications, not just the requested states: a\n -- seen reply still assigns an unseen root send to its thread row.\n -- Build scope evidence once per batch, never rescan history per send.\n channel_scopes AS MATERIALIZED (\n SELECT DISTINCT n.event_item_id AS channel_id,\n CASE WHEN n.notification_event_type = 'channel_mention'\n THEN COALESCE(n.metadata->>'messageId', n.metadata->>'message_id')\n END AS mentioned_message_id,\n COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') AS thread_id\n FROM requested\n JOIN user_notification un\n ON un.user_id = $1 AND un.deleted_at IS NULL\n AND un.event_item_type = 'channel' AND un.event_item_id = requested.entity_id\n AND un.state = ANY($9::notification_state[])\n AND un.notification_event_type IN ('channel_mention', 'channel_message_reply')\n JOIN notification n ON n.id = un.notification_id\n WHERE $8::bool AND requested.entity_type = 'channel'\n )\n SELECT requested.entity_id AS \"requested_id!\",\n requested.entity_type AS \"requested_type!\",\n matched.notification_id AS \"notification_id!\",\n matched.event_item_id AS \"event_item_id!\",\n matched.event_item_type AS \"event_item_type!\",\n matched.notification_event_type AS \"notification_event_type!\",\n matched.sent AS \"sent!\",\n matched.state AS \"state!: NotificationState\",\n matched.created_at AS \"created_at!\",\n matched.viewed_at,\n matched.notification_metadata,\n matched.sender_id\n FROM requested\n CROSS JOIN LATERAL (\n SELECT un.notification_id, n.event_item_id, n.event_item_type,\n n.notification_event_type, un.sent, un.state,\n un.created_at::timestamptz AS created_at,\n un.seen_at::timestamptz AS viewed_at,\n n.metadata AS notification_metadata, n.sender_id\n FROM (\n SELECT viewer_match.*\n FROM (\n SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at\n FROM user_notification un\n WHERE un.user_id = $1 AND un.deleted_at IS NULL\n AND un.state = ANY($4::notification_state[])\n AND un.event_item_type = requested.entity_type\n AND un.event_item_id = requested.entity_id\n UNION\n SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at\n FROM notification n\n CROSS JOIN LATERAL (\n SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at\n FROM user_notification un\n WHERE un.user_id = $1 AND un.notification_id = n.id\n AND un.deleted_at IS NULL AND un.state = ANY($4::notification_state[])\n -- Probe the viewer's row per notification: a plain join\n -- can scan the viewer's whole history for each entity.\n OFFSET 0\n ) un\n WHERE n.secondary_event_item_type = requested.entity_type\n AND n.secondary_event_item_id = requested.entity_id\n UNION\n SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at\n FROM notification n\n CROSS JOIN LATERAL (\n SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at\n FROM user_notification un\n WHERE un.user_id = $1 AND un.notification_id = n.id\n AND un.deleted_at IS NULL AND un.state = ANY($4::notification_state[])\n OFFSET 0\n ) un\n WHERE requested.entity_type = 'channel_message'\n AND COALESCE(n.metadata->>'messageId', n.metadata->>'message_id', '') = requested.entity_id\n ) viewer_match\n ORDER BY viewer_match.created_at DESC, viewer_match.notification_id DESC\n -- With no filter on the notification row, the newest matches\n -- are the result, so limit before reading notification rows.\n LIMIT CASE WHEN cardinality($5::text[]) = 0 AND NOT $7::bool AND NOT $8::bool THEN $6::bigint END\n ) un\n JOIN notification n ON n.id = un.notification_id\n WHERE (cardinality($5::text[]) = 0 OR n.notification_event_type = ANY($5))\n AND (NOT $7::bool OR (\n n.notification_event_type IN ('channel_message_send', 'channel_mention', 'document_mention')\n AND COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') IS NULL\n ))\n AND (NOT $8::bool OR requested.entity_type <> 'channel' OR (\n n.notification_event_type = 'document_mention'\n OR (n.notification_event_type IN ('channel_message_send', 'channel_message_reply') AND (\n -- Mention stacks consume the mention, not its duplicate send/reply.\n EXISTS (\n SELECT 1 FROM channel_scopes scope\n WHERE scope.channel_id = n.event_item_id\n AND scope.mentioned_message_id = COALESCE(n.metadata->>'messageId', n.metadata->>'message_id')\n )\n OR (n.notification_event_type = 'channel_message_send' AND NOT EXISTS (\n SELECT 1 FROM channel_scopes scope\n WHERE scope.channel_id = n.event_item_id AND scope.thread_id <> ''\n AND scope.thread_id = COALESCE(n.metadata->>'messageId', n.metadata->>'message_id')\n ))\n OR (n.notification_event_type = 'channel_message_reply'\n AND NULLIF(COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id'), '') IS NULL)\n ))\n OR (n.notification_event_type = 'channel_mention' AND (\n COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') = ''\n OR (COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') IS NULL\n AND COALESCE(n.metadata->>'messageId', n.metadata->>'message_id') = ''\n AND EXISTS (SELECT 1 FROM channel_scopes scope\n WHERE scope.channel_id = n.event_item_id AND scope.thread_id = ''))\n ))\n ))\n ORDER BY un.created_at DESC, un.notification_id DESC\n LIMIT $6\n ) matched\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "requested_id!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "requested_type!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "notification_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 3,
+ "name": "event_item_id!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "event_item_type!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "notification_event_type!",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 6,
+ "name": "sent!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 7,
+ "name": "state!: NotificationState",
+ "type_info": {
+ "Custom": {
+ "name": "notification_state",
+ "kind": {
+ "Enum": [
+ "unseen",
+ "seen",
+ "done"
+ ]
+ }
+ }
+ }
+ },
+ {
+ "ordinal": 8,
+ "name": "created_at!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 9,
+ "name": "viewed_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 10,
+ "name": "notification_metadata",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 11,
+ "name": "sender_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "TextArray",
+ "TextArray",
+ {
+ "Custom": {
+ "name": "notification_state[]",
+ "kind": {
+ "Array": {
+ "Custom": {
+ "name": "notification_state",
+ "kind": {
+ "Enum": [
+ "unseen",
+ "seen",
+ "done"
+ ]
+ }
+ }
+ }
+ }
+ }
+ },
+ "TextArray",
+ "Int8",
+ "Bool",
+ "Bool",
+ {
+ "Custom": {
+ "name": "notification_state[]",
+ "kind": {
+ "Array": {
+ "Custom": {
+ "name": "notification_state",
+ "kind": {
+ "Enum": [
+ "unseen",
+ "seen",
+ "done"
+ ]
+ }
+ }
+ }
+ }
+ }
+ }
+ ]
+ },
+ "nullable": [
+ null,
+ null,
+ null,
+ false,
+ false,
+ false,
+ null,
+ null,
+ null,
+ null,
+ false,
+ true
+ ]
+ },
+ "hash": "b89be6ddcd933ff1931751a8f4695b0d9178d912da60a4b8b104663645d547a4"
+}
diff --git a/.sqlx/query-efc740cd935fb67ef5c2a75b3c37cf16bf09d6e52e5f6e5c5b107ef8244a141d.json b/.sqlx/query-efc740cd935fb67ef5c2a75b3c37cf16bf09d6e52e5f6e5c5b107ef8244a141d.json
deleted file mode 100644
index 1cbb49a96b..0000000000
--- a/.sqlx/query-efc740cd935fb67ef5c2a75b3c37cf16bf09d6e52e5f6e5c5b107ef8244a141d.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO user_notification (notification_id, user_id)\n SELECT $1, user_id\n FROM UNNEST($2::text[]) as user_id\n RETURNING created_at::timestamptz as \"created_at!\"\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "created_at!",
- "type_info": "Timestamptz"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "TextArray"
- ]
- },
- "nullable": [
- null
- ]
- },
- "hash": "efc740cd935fb67ef5c2a75b3c37cf16bf09d6e52e5f6e5c5b107ef8244a141d"
-}
diff --git a/crates/macro_db_client/migrations/20261027201315_user_notification_entity_columns.sql b/crates/macro_db_client/migrations/20261027201315_user_notification_entity_columns.sql
new file mode 100644
index 0000000000..3db1b9efa8
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261027201315_user_notification_entity_columns.sql
@@ -0,0 +1,15 @@
+-- Entity reads ask for one viewer's notifications on one entity. The entity
+-- lives on `notification` and the viewer on `user_notification`, so no index
+-- covered both: every read scanned the entity's notifications for all users,
+-- which for a busy channel is every message it has had.
+-- Copy the entity and event type onto each recipient row so one index answers
+-- the read. A notification's entity and type never change after insert;
+-- notification ingress writes the copies with each recipient row.
+-- Fail fast instead of queueing every notification read behind a long
+-- transaction while waiting for the table lock.
+SET LOCAL lock_timeout = '5s';
+
+ALTER TABLE user_notification
+ ADD COLUMN event_item_type text,
+ ADD COLUMN event_item_id text,
+ ADD COLUMN notification_event_type text;
diff --git a/crates/macro_db_client/migrations/20261027201316_backfill_user_notification_entity.sql b/crates/macro_db_client/migrations/20261027201316_backfill_user_notification_entity.sql
new file mode 100644
index 0000000000..10744b1a71
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261027201316_backfill_user_notification_entity.sql
@@ -0,0 +1,45 @@
+-- no-transaction
+
+-- Copy each existing recipient row's entity from its notification. Batches
+-- walk the primary key and commit on their own, so no batch holds row locks
+-- for long and a rerun only rewrites the same values. Each batch updates an
+-- explicit list of keys: a key range lets the loop's reused plan merge-join the
+-- whole notification table on every batch. Run before the indexes are built
+-- so the updates don't also maintain them.
+-- Keep this file a single statement: COMMIT inside DO needs a top-level call.
+DO $$
+DECLARE
+ batch_size CONSTANT integer := 5000;
+ last_user_id text := '';
+ last_notification_id uuid := '00000000-0000-0000-0000-000000000000';
+ user_ids text[];
+ notification_ids uuid[];
+BEGIN
+ LOOP
+ SELECT array_agg(batch.user_id ORDER BY batch.user_id, batch.notification_id),
+ array_agg(batch.notification_id ORDER BY batch.user_id, batch.notification_id)
+ INTO user_ids, notification_ids
+ FROM (
+ SELECT user_id, notification_id
+ FROM user_notification
+ WHERE (user_id, notification_id) > (last_user_id, last_notification_id)
+ ORDER BY user_id, notification_id
+ LIMIT batch_size
+ ) batch;
+ EXIT WHEN user_ids IS NULL;
+
+ UPDATE user_notification un
+ SET event_item_type = n.event_item_type,
+ event_item_id = n.event_item_id,
+ notification_event_type = n.notification_event_type
+ FROM unnest(user_ids, notification_ids) AS batch(user_id, notification_id)
+ JOIN notification n ON n.id = batch.notification_id
+ WHERE un.user_id = batch.user_id
+ AND un.notification_id = batch.notification_id;
+ COMMIT;
+
+ last_user_id := user_ids[cardinality(user_ids)];
+ last_notification_id := notification_ids[cardinality(notification_ids)];
+ END LOOP;
+END
+$$;
diff --git a/crates/macro_db_client/migrations/20261027201317_user_notification_viewer_entity_idx.sql b/crates/macro_db_client/migrations/20261027201317_user_notification_viewer_entity_idx.sql
new file mode 100644
index 0000000000..960a5eeddc
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261027201317_user_notification_viewer_entity_idx.sql
@@ -0,0 +1,9 @@
+-- no-transaction
+-- One viewer's notifications on one entity, by state and newest first. The
+-- included columns let entity reads filter and order without the table.
+-- Must stay a single statement: sqlx sends no-transaction migrations as one
+-- batch, and CONCURRENTLY cannot run inside a transaction.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_user_notification_viewer_entity
+ ON user_notification (user_id, event_item_type, event_item_id, state, created_at DESC, notification_id DESC)
+ INCLUDE (notification_event_type, sent, seen_at)
+ WHERE deleted_at IS NULL;
diff --git a/crates/notification/AGENTS.md b/crates/notification/AGENTS.md
index 6018d2cda4..3352faa609 100644
--- a/crates/notification/AGENTS.md
+++ b/crates/notification/AGENTS.md
@@ -68,6 +68,12 @@ through the same service.
## Gotchas
+- `user_notification` carries copies of its notification's `event_item_type`,
+ `event_item_id` and `notification_event_type`. Entity reads find a viewer's
+ rows through `idx_user_notification_viewer_entity` on those copies, so a row
+ inserted without them never matches an entity read or mark-seen, and a
+ migration that rewrites those `notification` columns must update
+ `user_notification` too.
- Feature flags gate most adapters (`axum`, `ai_tool`, `clear_ios_push`,
`schema`, and the default `kafka` for the Kafka publishers and consumer); building without the feature a caller needs silently drops the
module instead of failing loudly — check `Cargo.toml` before assuming an
diff --git a/crates/notification/src/outbound/entity_notifications.rs b/crates/notification/src/outbound/entity_notifications.rs
index d26f6dda9d..096df1b4fb 100644
--- a/crates/notification/src/outbound/entity_notifications.rs
+++ b/crates/notification/src/outbound/entity_notifications.rs
@@ -1,6 +1,10 @@
//! Filtered notification edges, limited independently for every requested entity.
-//! The primary, secondary and message-id candidate paths each have their own
-//! index; the message-id path's `COALESCE` must match `idx_notification_message_id`.
+//! Each recipient row carries its notification's entity and type, so the
+//! primary-entity path reads only the viewer's rows from
+//! `idx_user_notification_viewer_entity` instead of the entity's notifications
+//! for every user. The secondary and message-id paths match few notifications
+//! each and keep their `notification` indexes; the message-id path's `COALESCE`
+//! must match `idx_notification_message_id`.
use std::{collections::HashMap, str::FromStr};
@@ -52,13 +56,13 @@ pub(super) async fn get_filtered_entity_notifications(
END AS mentioned_message_id,
COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') AS thread_id
FROM requested
- JOIN notification n
- ON n.event_item_id = requested.entity_id AND n.event_item_type = 'channel'
- JOIN user_notification un ON un.notification_id = n.id
+ JOIN user_notification un
+ ON un.user_id = $1 AND un.deleted_at IS NULL
+ AND un.event_item_type = 'channel' AND un.event_item_id = requested.entity_id
+ AND un.state = ANY($9::notification_state[])
+ AND un.notification_event_type IN ('channel_mention', 'channel_message_reply')
+ JOIN notification n ON n.id = un.notification_id
WHERE $8::bool AND requested.entity_type = 'channel'
- AND un.user_id = $1 AND un.deleted_at IS NULL
- AND un.state = ANY($9::notification_state[])
- AND n.notification_event_type IN ('channel_mention', 'channel_message_reply')
)
SELECT requested.entity_id AS "requested_id!",
requested.entity_type AS "requested_type!",
@@ -80,22 +84,48 @@ pub(super) async fn get_filtered_entity_notifications(
un.seen_at::timestamptz AS viewed_at,
n.metadata AS notification_metadata, n.sender_id
FROM (
- SELECT n.id FROM notification n
- WHERE n.event_item_id = requested.entity_id AND n.event_item_type = requested.entity_type
- UNION
- SELECT n.id FROM notification n
- WHERE n.secondary_event_item_id = requested.entity_id AND n.secondary_event_item_type = requested.entity_type
- UNION
- SELECT n.id FROM notification n
- WHERE requested.entity_type = 'channel_message'
- AND COALESCE(n.metadata->>'messageId', n.metadata->>'message_id', '') = requested.entity_id
- ) candidates
- JOIN notification n ON n.id = candidates.id
- JOIN user_notification un ON un.notification_id = n.id
- WHERE un.user_id = $1
- AND un.deleted_at IS NULL
- AND un.state = ANY($4::notification_state[])
- AND (cardinality($5::text[]) = 0 OR n.notification_event_type = ANY($5))
+ SELECT viewer_match.*
+ FROM (
+ SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at
+ FROM user_notification un
+ WHERE un.user_id = $1 AND un.deleted_at IS NULL
+ AND un.state = ANY($4::notification_state[])
+ AND un.event_item_type = requested.entity_type
+ AND un.event_item_id = requested.entity_id
+ UNION
+ SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at
+ FROM notification n
+ CROSS JOIN LATERAL (
+ SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at
+ FROM user_notification un
+ WHERE un.user_id = $1 AND un.notification_id = n.id
+ AND un.deleted_at IS NULL AND un.state = ANY($4::notification_state[])
+ -- Probe the viewer's row per notification: a plain join
+ -- can scan the viewer's whole history for each entity.
+ OFFSET 0
+ ) un
+ WHERE n.secondary_event_item_type = requested.entity_type
+ AND n.secondary_event_item_id = requested.entity_id
+ UNION
+ SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at
+ FROM notification n
+ CROSS JOIN LATERAL (
+ SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at
+ FROM user_notification un
+ WHERE un.user_id = $1 AND un.notification_id = n.id
+ AND un.deleted_at IS NULL AND un.state = ANY($4::notification_state[])
+ OFFSET 0
+ ) un
+ WHERE requested.entity_type = 'channel_message'
+ AND COALESCE(n.metadata->>'messageId', n.metadata->>'message_id', '') = requested.entity_id
+ ) viewer_match
+ ORDER BY viewer_match.created_at DESC, viewer_match.notification_id DESC
+ -- With no filter on the notification row, the newest matches
+ -- are the result, so limit before reading notification rows.
+ LIMIT CASE WHEN cardinality($5::text[]) = 0 AND NOT $7::bool AND NOT $8::bool THEN $6::bigint END
+ ) un
+ JOIN notification n ON n.id = un.notification_id
+ WHERE (cardinality($5::text[]) = 0 OR n.notification_event_type = ANY($5))
AND (NOT $7::bool OR (
n.notification_event_type IN ('channel_message_send', 'channel_mention', 'document_mention')
AND COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') IS NULL
diff --git a/crates/notification/src/outbound/repository.rs b/crates/notification/src/outbound/repository.rs
index f500448af4..6fd50a28aa 100644
--- a/crates/notification/src/outbound/repository.rs
+++ b/crates/notification/src/outbound/repository.rs
@@ -44,25 +44,6 @@ type UserNotificationListRow = (
Option<String>,
);
-#[derive(sqlx::FromRow)]
-struct EntityNotificationListRow {
- owner_id: String,
- notification_id: Uuid,
- event_item_id: String,
- event_item_type: String,
- secondary_event_item_id: Option<String>,
- secondary_event_item_type: Option<String>,
- sent: bool,
- state: NotificationState,
- created_at: DateTime<Utc>,
- viewed_at: Option<DateTime<Utc>>,
- updated_at: DateTime<Utc>,
- deleted_at: Option<DateTime<Utc>>,
- notification_metadata: serde_json::Value,
- notification_event_type: String,
- sender_id: Option<String>,
-}
-
struct UpdatedUserNotificationRow {
owner_id: String,
notification_id: Uuid,
@@ -294,46 +275,6 @@ fn push_entities_filter<'a>(
builder.push(")");
}
-fn message_ref_matches_row(
- message_id: &str,
- secondary_event_item_id: Option<&str>,
- secondary_event_item_type: Option<&str>,
- metadata: &serde_json::Value,
-) -> bool {
- let directly_targets_message = metadata
- .get("messageId")
- .or_else(|| metadata.get("message_id"))
- .and_then(|value| value.as_str())
- .is_some_and(|stored_message_id| stored_message_id == message_id);
- let targets_message_as_thread = secondary_event_item_type == Some("channel_message")
- && secondary_event_item_id == Some(message_id);
-
- directly_targets_message || targets_message_as_thread
-}
-
-fn notification_entity_matches_row(
- entity: &Entity<'_>,
- event_item_id: &str,
- event_item_type: &str,
- secondary_event_item_id: Option<&str>,
- secondary_event_item_type: Option<&str>,
- metadata: &serde_json::Value,
-) -> bool {
- let directly_targets_entity = event_item_type == entity.entity_type.as_ref()
- && event_item_id == entity.entity_id.as_ref();
- let targets_secondary_entity = secondary_event_item_type == Some(entity.entity_type.as_ref())
- && secondary_event_item_id == Some(entity.entity_id.as_ref());
- let targets_message = entity.entity_type == EntityType::ChannelMessage
- && message_ref_matches_row(
- entity.entity_id.as_ref(),
- secondary_event_item_id,
- secondary_event_item_type,
- metadata,
- );
-
- directly_targets_entity || targets_secondary_entity || targets_message
-}
-
fn push_cursor_filter(
builder: &mut QueryBuilder<'_, Postgres>,
cursor_timestamp: Option<DateTime<Utc>>,
@@ -800,13 +741,22 @@ impl NotificationDbOps for PgPool {
let created_at = sqlx::query_scalar!(
r#"
- INSERT INTO user_notification (notification_id, user_id)
- SELECT $1, user_id
+ INSERT INTO user_notification (
+ notification_id,
+ user_id,
+ event_item_type,
+ event_item_id,
+ notification_event_type
+ )
+ SELECT $1, user_id, $3, $4, $5
FROM UNNEST($2::text[]) as user_id
RETURNING created_at::timestamptz as "created_at!"
"#,
notification_id,
- &user_ids
+ &user_ids,
+ entity_type,
+ request.notification_entity.entity_id.as_ref(),
+ typename
)
.fetch_one(&mut *tx)
.await?;
@@ -1007,29 +957,51 @@ impl NotificationDbOps for PgPool {
SELECT entity_type, entity_id
FROM UNNEST($2::text[], $3::text[]) AS entity(entity_type, entity_id)
)
- SELECT un.notification_id
- FROM user_notification un
- JOIN notification n ON n.id = un.notification_id
- WHERE un.user_id = $1
- AND un.deleted_at IS NULL
- AND (un.state = ANY($4::notification_state[]) OR ($5 AND un.seen_at IS NULL))
- AND EXISTS (
- SELECT 1
- FROM requested_entities entity
- WHERE (
- n.event_item_type = entity.entity_type
- AND n.event_item_id = entity.entity_id
- )
- OR (
- n.secondary_event_item_type = entity.entity_type
- AND n.secondary_event_item_id = entity.entity_id
- )
- OR (
- entity.entity_type = 'channel_message'
- AND COALESCE(n.metadata->>'messageId', n.metadata->>'message_id', '') = entity.entity_id
- )
- )
- ORDER BY un.created_at, un.notification_id
+ -- The primary entity reads only the viewer's rows from
+ -- idx_user_notification_viewer_entity; secondary entities and
+ -- message ids match few notifications each.
+ SELECT matched.notification_id AS "notification_id!"
+ FROM (
+ SELECT un.notification_id, un.created_at
+ FROM requested_entities entity
+ JOIN user_notification un
+ ON un.event_item_type = entity.entity_type
+ AND un.event_item_id = entity.entity_id
+ WHERE un.user_id = $1
+ AND un.deleted_at IS NULL
+ AND (un.state = ANY($4::notification_state[]) OR ($5 AND un.seen_at IS NULL))
+ UNION
+ SELECT un.notification_id, un.created_at
+ FROM requested_entities entity
+ JOIN notification n
+ ON n.secondary_event_item_type = entity.entity_type
+ AND n.secondary_event_item_id = entity.entity_id
+ CROSS JOIN LATERAL (
+ SELECT un.notification_id, un.created_at
+ FROM user_notification un
+ WHERE un.user_id = $1 AND un.notification_id = n.id
+ AND un.deleted_at IS NULL
+ AND (un.state = ANY($4::notification_state[]) OR ($5 AND un.seen_at IS NULL))
+ -- Probe the viewer's row per notification: a plain join
+ -- can scan the viewer's whole history for each entity.
+ OFFSET 0
+ ) un
+ UNION
+ SELECT un.notification_id, un.created_at
+ FROM requested_entities entity
+ JOIN notification n
+ ON COALESCE(n.metadata->>'messageId', n.metadata->>'message_id', '') = entity.entity_id
+ CROSS JOIN LATERAL (
+ SELECT un.notification_id, un.created_at
+ FROM user_notification un
+ WHERE un.user_id = $1 AND un.notification_id = n.id
+ AND un.deleted_at IS NULL
+ AND (un.state = ANY($4::notification_state[]) OR ($5 AND un.seen_at IS NULL))
+ OFFSET 0
+ ) un
+ WHERE entity.entity_type = 'channel_message'
+ ) matched
+ ORDER BY matched.created_at, matched.notification_id
"#,
user_id.as_ref(),
&entity_types,
@@ -1285,144 +1257,10 @@ impl NotificationDbOps for PgPool {
entities: Vec<Entity<'static>>,
query: crate::domain::models::entity_query::EntityNotificationQuery,
) -> Result<HashMap<Entity<'static>, Vec<UserNotificationRow<serde_json::Value>>>, Report> {
- if query != Default::default() {
- return super::entity_notifications::get_filtered_entity_notifications(
- self, user_id, entities, query,
- )
- .await;
- }
- let mut seen_entities = HashSet::new();
- let entities = entities
- .into_iter()
- .filter(|entity| seen_entities.insert(entity.clone()))
- .collect::<Vec<_>>();
-
- let mut result = entities
- .iter()
- .cloned()
- .map(|entity| (entity, Vec::new()))
- .collect::<HashMap<_, _>>();
-
- if entities.is_empty() {
- return Ok(result);
- }
-
- let filters = NotificationListFilters {
- states: NotificationState::ACTIVE.to_vec(),
- include_types: Vec::new(),
- entities: entities.clone(),
- };
-
- let mut builder = QueryBuilder::new(
- r#"
- SELECT
- un.user_id as owner_id,
- un.notification_id,
- n.event_item_id,
- n.event_item_type,
- n.secondary_event_item_id,
- n.secondary_event_item_type,
- un.sent,
- un.state,
- un.created_at::timestamptz as created_at,
- un.seen_at::timestamptz as viewed_at,
- un.created_at::timestamptz as updated_at,
- un.deleted_at::timestamptz as deleted_at,
- n.metadata as notification_metadata,
- n.notification_event_type as notification_event_type,
- n.sender_id as sender_id
- FROM user_notification un
- JOIN notification n ON n.id = un.notification_id
- WHERE un.user_id = "#,
- );
- builder.push_bind(user_id.as_ref());
- push_notification_status_filters(&mut builder, &filters);
- push_entities_filter(&mut builder, &entities);
- builder.push(" ORDER BY un.created_at DESC, un.notification_id DESC");
-
- let rows = builder
- .build_query_as::<EntityNotificationListRow>()
- .fetch_all(self)
- .await?;
-
- for row in rows {
- let EntityNotificationListRow {
- owner_id,
- notification_id,
- event_item_id,
- event_item_type,
- secondary_event_item_id,
- secondary_event_item_type,
- sent,
- state,
- created_at,
- viewed_at,
- updated_at,
- deleted_at,
- notification_metadata,
- notification_event_type,
- sender_id,
- } = row;
-
- let entity = match EntityType::from_str(&event_item_type) {
- Ok(entity_type) => entity_type.with_entity_string(event_item_id.clone()),
- Err(e) => {
- tracing::debug!(?notification_id, error = ?e, "skipping invalid notification");
- continue;
- }
- };
- let sender_id = match sender_id
- .map(|s| MacroUserIdStr::parse_from_str(&s).map(CowLike::into_owned))
- .transpose()
- {
- Ok(sender_id) => sender_id,
- Err(e) => {
- tracing::debug!(?notification_id, error = ?e, "skipping invalid notification");
- continue;
- }
- };
- let owner_id = match MacroUserIdStr::parse_from_str(&owner_id).map(CowLike::into_owned)
- {
- Ok(owner_id) => owner_id,
- Err(e) => {
- tracing::debug!(?notification_id, error = ?e, "skipping invalid notification");
- continue;
- }
- };
-
- let notification = UserNotificationRow {
- owner_id,
- notification_id,
- notification_event_type: notification_event_type.clone(),
- entity,
- sent,
- state,
- created_at,
- viewed_at,
- updated_at,
- deleted_at,
- notification_metadata: notification_metadata.clone(),
- sender_id,
- };
-
- for requested_entity in &entities {
- if notification_entity_matches_row(
- requested_entity,
- &event_item_id,
- &event_item_type,
- secondary_event_item_id.as_deref(),
- secondary_event_item_type.as_deref(),
- &notification_metadata,
- ) {
- result
- .entry(requested_entity.clone())
- .or_default()
- .push(notification.clone());
- }
- }
- }
-
- Ok(result)
+ super::entity_notifications::get_filtered_entity_notifications(
+ self, user_id, entities, query,
+ )
+ .await
}
async fn get_user_notification_by_id<T: DeserializeOwned + Send>(
diff --git a/tooling/seed_cli/seed/local_e2e/channel_messages.sql b/tooling/seed_cli/seed/local_e2e/channel_messages.sql
index c80614f6ea..7976a702d4 100644
--- a/tooling/seed_cli/seed/local_e2e/channel_messages.sql
+++ b/tooling/seed_cli/seed/local_e2e/channel_messages.sql
@@ -341,10 +341,16 @@ VALUES (
INSERT INTO user_notification (
user_id,
notification_id,
- created_at
+ created_at,
+ event_item_type,
+ event_item_id,
+ notification_event_type
)
VALUES (
'00000000-0000-0000-0004-000000000001'::uuid,
- now() + interval '4 hours'
+ now() + interval '4 hours',
+ 'channel',
+ '00000000-0000-0000-0000-000000000001',
+ 'channel_mention'
);