Navigation

Supabase Performance Cleanup

SQL scripts to fix RLS policies, duplicate indexes, and performance issues

8

Duplicate Indexes

100+

Duplicate Policies

25+

RLS Init Plan Issues

SQL Cleanup Scripts

Run these in Supabase SQL Editor.

PRODUCTION READY

Remove Duplicate Indexes

-- ============================================
-- DUPLICATE INDEX CLEANUP
-- Run in Supabase SQL Editor
-- ============================================

-- 1. Drop duplicate index on idioms table
DROP INDEX IF EXISTS public.idx_idioms_is_approved;
-- Keeping: idx_idioms_approved

-- 2. Drop duplicate index on class_enrollment table
DROP INDEX IF EXISTS public.idx_enrollment_student_email;
-- Keeping: idx_class_enrollment_student

-- 3. Drop duplicate index on currency_packs table
DROP INDEX IF EXISTS public.idx_currency_packs_stripe_price_id;
-- Keeping: idx_currency_packs_stripe

-- 4. Drop duplicate index on grammar_access table
DROP INDEX IF EXISTS public.idx_grammar_access_user_email;
-- Keeping: idx_grammar_access_user

-- 5. Drop duplicate index on teacher_assignments table
DROP INDEX IF EXISTS public.idx_assignments_class;
DROP INDEX IF EXISTS public.idx_assignments_teacher_email;
-- Keeping: idx_assignment_class_id, idx_assignment_teacher_email

-- 6. Drop duplicate indexes on user_progress table
DROP INDEX IF EXISTS public.idx_user_progress_user;
DROP INDEX IF EXISTS public.idx_user_progress_idiom_id;
-- Keeping: idx_user_progress_created_by, idx_user_progress_idiom

-- Verify cleanup
SELECT 
    schemaname,
    tablename,
    indexname
FROM pg_indexes
WHERE schemaname = 'public'
ORDER BY tablename, indexname;

Impact: Removes 8 redundant indexes. This will free up storage and improve write performance on INSERT/UPDATE/DELETE operations.

Important Notes

1. Backup First: Take a database snapshot before running any cleanup scripts.

2. Test in Staging: If possible, run these scripts in a staging environment first.

3. Run in Order: Execute scripts in this order: Indexes → RLS Performance → Duplicate Policies → Verification

4. Monitor Performance: After cleanup, monitor query performance and adjust as needed.

5. Service Role Policies: Some "Service role full access" policies may be needed for backend functions - review before dropping.