Skip to content

Commit a8c8823

Browse files
committed
fix: add missing indexes
Signed-off-by: Uroš Marolt <uros@marolt.me>
1 parent ccde47c commit a8c8823

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

backend/src/database/migrations/U1774430554__covering-indexes-for-member-identity-lookups.sql

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Query: findMembersByVerifiedUsernames
2+
--
3+
-- Joins memberIdentities on (platform, lower(value)) with WHERE:
4+
-- verified = true AND type = 'username' AND deletedAt IS NULL
5+
--
6+
-- The existing idx_memberIdentities_platform_type_lower_value_memberId is
7+
-- missing verified = true in its partial condition, so PostgreSQL must
8+
-- heap-fetch every row to recheck verified, which is expensive when there
9+
-- are many unverified identities.
10+
--
11+
-- This index adds verified = true to the partial condition and includes memberId
12+
-- so the join to members can read memberId from the index without a heap fetch.
13+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "idx_memberIdentities_verified_username_platform_lower_value"
14+
ON "memberIdentities" (platform, lower(value), "memberId")
15+
WHERE verified = true
16+
AND type = 'username'
17+
AND "deletedAt" IS NULL;
18+
19+
-- Query: findMembersByVerifiedEmails
20+
--
21+
-- Joins memberIdentities on lower(value) with WHERE:
22+
-- verified = true AND type = 'email' AND deletedAt IS NULL
23+
--
24+
-- The existing idx_memberIdentities_verified_email_lower_value has the right
25+
-- partial condition but only stores lower(value) — no memberId. Every matched
26+
-- index entry requires a heap fetch to get memberId for the join to members.
27+
-- Under concurrent insert/update load, those heap fetches queue behind buffer
28+
-- pin locks, causing multi-second delays even for small inputs.
29+
--
30+
-- This index adds memberId so the join to members can proceed without
31+
-- touching the memberIdentities heap pages.
32+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "idx_memberIdentities_verified_email_lower_value_memberid"
33+
ON "memberIdentities" (lower(value), "memberId")
34+
WHERE verified = true
35+
AND type = 'email'
36+
AND "deletedAt" IS NULL;

0 commit comments

Comments
 (0)