diff --git a/backend/migrations/20260528143710_draft_user_sync_schema.down.sql b/backend/migrations/20260528143710_draft_user_sync_schema.down.sql index b458f05d31..2ba104a1d0 100644 --- a/backend/migrations/20260528143710_draft_user_sync_schema.down.sql +++ b/backend/migrations/20260528143710_draft_user_sync_schema.down.sql @@ -1,5 +1,6 @@ ALTER TABLE draft DROP COLUMN id; +DROP INDEX IF EXISTS draft_user_sync_idx; DROP INDEX IF EXISTS draft_pkey_legacy; DROP INDEX IF EXISTS draft_pkey_with_user; diff --git a/backend/migrations/20260528143710_draft_user_sync_schema.up.sql b/backend/migrations/20260528143710_draft_user_sync_schema.up.sql index ed0b389c16..96c5c6e4af 100644 --- a/backend/migrations/20260528143710_draft_user_sync_schema.up.sql +++ b/backend/migrations/20260528143710_draft_user_sync_schema.up.sql @@ -61,4 +61,13 @@ CREATE UNIQUE INDEX draft_pkey_legacy ON draft (workspace_id, path, typ) WHERE email IS NULL; +-- Hot path for `sync_drafts`: each active editor polls +-- `WHERE workspace_id = ? AND email = ? [AND created_at > ?]` every 2-10s. +-- Neither partial unique index above helps (both lead with `path, typ`); +-- this covers the initial sync (leading two columns) and the incremental +-- missed-drafts query (range scan on `created_at`). +CREATE INDEX draft_user_sync_idx + ON draft (workspace_id, email, created_at) + WHERE email IS NOT NULL; + ALTER TABLE draft ADD COLUMN id BIGSERIAL PRIMARY KEY;