perf: add (workspace_id, email, created_at) partial index for sync hot path

This commit is contained in:
Diego Imbert
2026-05-28 17:14:09 +02:00
parent b236f93a90
commit 7f35f3a53f
2 changed files with 10 additions and 0 deletions
@@ -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;
@@ -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;