0d4b69713d
- add covering indexes for flagged foreign keys - optimize RLS policies to use select auth.uid() - consolidate posts update policies into one permissive rule - document linter findings and decisions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1.8 KiB
1.8 KiB
Database performance notes (Supabase linter)
This project had two performance-related linter groups:
- RLS auth function evaluation
- Unindexed foreign keys
What was changed
- Added migration
infrastructure/migrations/002_perf_linter_fixes.sql. - Updated
infrastructure/migrations/001_schema.sqlto keep fresh setups aligned with the same fixes.
RLS changes
The affected policies now use:
(select auth.uid())instead ofauth.uid()
This avoids repeated per-row evaluation in RLS plans for these policies:
profiles_updateposts_selectposts_insertposts_update_ownresonances_insertresonances_deletewhispers_selectwhispers_insertfollows_insertfollows_deletereports_insertreports_select_own
Also, duplicate permissive update policies on public.posts were consolidated by removing posts_softdelete and keeping a single posts_update_own.
FK index changes
Added covering indexes:
idx_follows_following_idonpublic.follows(following_id)idx_reports_reporter_idonpublic.reports(reporter_id)idx_resonances_user_idonpublic.resonances(user_id)idx_whispers_from_user_idonpublic.whispers(from_user_id)idx_whispers_post_idonpublic.whispers(post_id)
About "unused index" findings
unused_index is based on pg_stat_user_indexes.idx_scan, which can read as zero after restarts or during low traffic.
In this environment, PostgreSQL was recently restarted, so these were not dropped automatically.
Suggested review query:
select
schemaname,
relname as table_name,
indexrelname as index_name,
idx_scan
from pg_stat_user_indexes
where schemaname = 'public'
order by idx_scan asc, indexrelname;
Drop an index only after observing sustained zero usage over representative production traffic.