From 37fa344c4d3acedf1277ac78d4a97fb7cb4482a2 Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Thu, 5 Mar 2026 17:10:33 -0500 Subject: [PATCH] fix bad migration perf --- ...05100000_fix_profile_posts_performance.sql | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/supabase/migrations/20260305100000_fix_profile_posts_performance.sql b/supabase/migrations/20260305100000_fix_profile_posts_performance.sql index 5407ba38..ddcd251c 100644 --- a/supabase/migrations/20260305100000_fix_profile_posts_performance.sql +++ b/supabase/migrations/20260305100000_fix_profile_posts_performance.sql @@ -1,23 +1,19 @@ -- Add missing indexes on document foreign keys used by get_profile_posts -CREATE INDEX IF NOT EXISTS comments_on_documents_document_idx +CREATE INDEX CONCURRENTLY IF NOT EXISTS comments_on_documents_document_idx ON public.comments_on_documents (document); -CREATE INDEX IF NOT EXISTS document_mentions_in_bsky_document_idx +CREATE INDEX CONCURRENTLY IF NOT EXISTS document_mentions_in_bsky_document_idx ON public.document_mentions_in_bsky (document); -CREATE INDEX IF NOT EXISTS documents_in_publications_document_idx +CREATE INDEX CONCURRENTLY IF NOT EXISTS documents_in_publications_document_idx ON public.documents_in_publications (document); --- Add a stored generated column to extract the DID from at:// URIs --- at://did:plc:xxx/collection/rkey -> did:plc:xxx -ALTER TABLE public.documents - ADD COLUMN identity_did text GENERATED ALWAYS AS (split_part(uri, '/', 3)) STORED; +-- Expression index to look up documents by DID without adding a column +-- at://did:plc:xxx/collection/rkey -> split_part gives did:plc:xxx +CREATE INDEX CONCURRENTLY IF NOT EXISTS documents_identity_did_sort_idx + ON public.documents (split_part(uri, '/', 3), sort_date DESC, uri DESC); --- Composite index for efficient profile post lookups: filter by DID, order by sort_date -CREATE INDEX documents_identity_did_sort_idx - ON public.documents (identity_did, sort_date DESC, uri DESC); - --- Rewrite get_profile_posts to use the new identity_did column +-- Rewrite get_profile_posts to use the expression index CREATE OR REPLACE FUNCTION get_profile_posts( p_did text, p_cursor_sort_date timestamptz DEFAULT NULL, @@ -55,7 +51,7 @@ AS $$ WHERE dip.document = d.uri LIMIT 1 ) pub ON true - WHERE d.identity_did = p_did + WHERE split_part(d.uri, '/', 3) = p_did AND ( p_cursor_sort_date IS NULL OR d.sort_date < p_cursor_sort_date -- 2.51.2