perf(server): index messageChannel/calendarChannel for per-workspace sync crons (#20678)

## Summary

The messaging/calendar import crons each iterate every active workspace
and execute one `find` per workspace against `core."messageChannel"` /
`core."calendarChannel"` with the shape:

```
WHERE "workspaceId" = $1 AND "isSyncEnabled" = true AND "syncStage" = $2 [AND "type" <> $3]
```

There is currently no index supporting that shape, so the planner does a
seq scan on each table for every iteration. On prod-eu (RDS Performance
Insights, `rds-prod-eu-one`), these two queries are the top two by load
— together ~12 AAS, ~12 calls/sec — and have been the primary
contributor to the sustained 100% CPU since active workspace count grew.

This PR adds composite indexes on `(workspaceId, isSyncEnabled,
syncStage)` for both tables as an instance migration in 2.6.0.
This commit is contained in:
Charles Bochet
2026-05-18 16:31:03 +02:00
committed by GitHub
parent 8f3c336e62
commit d03480472c
4 changed files with 46 additions and 0 deletions
@@ -4,6 +4,7 @@ import {
Column,
CreateDateColumn,
Entity,
Index,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
@@ -35,6 +36,11 @@ registerEnumType(CalendarChannelContactAutoCreationPolicy, {
});
@Entity({ name: 'calendarChannel', schema: 'core' })
@Index('IDX_CALENDAR_CHANNEL_WORKSPACE_ID_SYNC_ENABLED_SYNC_STAGE', [
'workspaceId',
'isSyncEnabled',
'syncStage',
])
export class CalendarChannelEntity extends WorkspaceRelatedEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@@ -4,6 +4,7 @@ import {
Column,
CreateDateColumn,
Entity,
Index,
JoinColumn,
ManyToOne,
OneToMany,
@@ -47,6 +48,11 @@ registerEnumType(MessageChannelPendingGroupEmailsAction, {
});
@Entity({ name: 'messageChannel', schema: 'core' })
@Index('IDX_MESSAGE_CHANNEL_WORKSPACE_ID_SYNC_ENABLED_SYNC_STAGE', [
'workspaceId',
'isSyncEnabled',
'syncStage',
])
export class MessageChannelEntity extends WorkspaceRelatedEntity {
@PrimaryGeneratedColumn('uuid')
id: string;