···11-- Add missing indexes on document foreign keys used by get_profile_posts
22-CREATE INDEX IF NOT EXISTS comments_on_documents_document_idx
22+CREATE INDEX CONCURRENTLY IF NOT EXISTS comments_on_documents_document_idx
33 ON public.comments_on_documents (document);
4455-CREATE INDEX IF NOT EXISTS document_mentions_in_bsky_document_idx
55+CREATE INDEX CONCURRENTLY IF NOT EXISTS document_mentions_in_bsky_document_idx
66 ON public.document_mentions_in_bsky (document);
7788-CREATE INDEX IF NOT EXISTS documents_in_publications_document_idx
88+CREATE INDEX CONCURRENTLY IF NOT EXISTS documents_in_publications_document_idx
99 ON public.documents_in_publications (document);
10101111--- Add a stored generated column to extract the DID from at:// URIs
1212--- at://did:plc:xxx/collection/rkey -> did:plc:xxx
1313-ALTER TABLE public.documents
1414- ADD COLUMN identity_did text GENERATED ALWAYS AS (split_part(uri, '/', 3)) STORED;
1515-1616--- Composite index for efficient profile post lookups: filter by DID, order by sort_date
1717-CREATE INDEX documents_identity_did_sort_idx
1818- ON public.documents (identity_did, sort_date DESC, uri DESC);
1111+-- Expression index to look up documents by DID without adding a column
1212+-- at://did:plc:xxx/collection/rkey -> split_part gives did:plc:xxx
1313+CREATE INDEX CONCURRENTLY IF NOT EXISTS documents_identity_did_sort_idx
1414+ ON public.documents (split_part(uri, '/', 3), sort_date DESC, uri DESC);
19152020--- Rewrite get_profile_posts to use the new identity_did column
1616+-- Rewrite get_profile_posts to use the expression index
2117CREATE OR REPLACE FUNCTION get_profile_posts(
2218 p_did text,
2319 p_cursor_sort_date timestamptz DEFAULT NULL,
···5551 WHERE dip.document = d.uri
5652 LIMIT 1
5753 ) pub ON true
5858- WHERE d.identity_did = p_did
5454+ WHERE split_part(d.uri, '/', 3) = p_did
5955 AND (
6056 p_cursor_sort_date IS NULL
6157 OR d.sort_date < p_cursor_sort_date