Skip to main content
Generated: 2026-01-16
Status: Based on Codebase Analysis & Best Practices
Note: Direct MCP access to performance advisors timed out - recommendations based on project patterns and Supabase best practices

Executive Summary

This document provides performance recommendations for the Supabase production database based on:
  1. Recent optimizations (index cleanup completed Jan 11, 2026)
  2. Project architecture patterns
  3. Supabase best practices
  4. Multi-tenant healthcare ERP requirements
Key Recent Optimizations:
  • 1,307 unused indexes removed (Jan 11, 2026) - ~11.3 MB storage freed
  • Index cleanup audit table created for rollback safety
  • Write performance improved by reducing index maintenance overhead
Implementation Status (2026-01-16):
  • pg_stat_statements extension - Migration created
  • Performance indexes - Migration with 25+ composite indexes for common query patterns
  • Assessment scripts - Created scripts/performance/assessment-queries.sql
  • Monitoring scripts - Created scripts/performance/monitoring-queries.sql
  • RLS policies - Already optimized (663 SECURITY DEFINER functions)
  • JSONB indexes - Not needed (no custom_fields filtering found)
  • Data archival plan - Created docs/migration/DATA_ARCHIVAL_PLAN.md
  • Monitoring guide - Created docs/performance/MONITORING_GUIDE.md

Immediate Actions Required

1. Access Performance Advisors (When Connection Restored)

Use Supabase MCP:
Alternative - Supabase Dashboard:
  1. Navigate to: Project Settings → Database → Advisors
  2. Review Performance tab recommendations
  3. Document findings in this file
Common Performance Advisor Recommendations:
  • Missing indexes on frequently queried columns
  • Tables needing VACUUM/ANALYZE
  • Slow query patterns
  • Connection pool optimization
  • Storage optimization

Priority 1: Query Performance Monitoring

Status: Spec exists (PF-52) but implementation status unknown Recommendation:
  1. Verify pf_query_performance_logs table exists and is populated
  2. Review slow queries (>1000ms threshold)
  3. Add missing indexes for frequently slow queries
  4. Enable pg_stat_statements extension if not already enabled
SQL to Check:
Action Items:
  • Verify PF-52 implementation status
  • Enable pg_stat_statements if not enabled
  • Review slow query patterns weekly
  • Create indexes for slow queries

Priority 2: Index Optimization

Status: Recent cleanup completed (Jan 11, 2026) Recommendations:

2.1 Monitor Index Usage Post-Cleanup

Check for regressions:

2.2 Add Missing Indexes for Common Query Patterns

Based on project patterns, verify these indexes exist: Multi-tenant queries (organization_id + common filters):
Foreign key lookups:
Action Items:
  • Monitor index usage weekly for 4 weeks post-cleanup
  • Add composite indexes for common filter combinations
  • Verify FK indexes exist for all foreign keys
  • Document any new indexes added

Priority 3: Table Maintenance

Recommendation: Run regular VACUUM and ANALYZE Current Status: Unknown - needs verification SQL to Check:
Recommendation:
  • Auto-vacuum should be enabled (default in Supabase)
  • Manual VACUUM may be needed for large tables with high churn
  • ANALYZE should run automatically, but verify statistics are current
Action Items:
  • Verify auto-vacuum is enabled
  • Check dead tuple percentages
  • Schedule manual VACUUM for high-churn tables if needed
  • Monitor VACUUM performance impact

Priority 4: Connection Pooling

Status: Supabase provides connection pooling - verify configuration Recommendations:
  1. Use Transaction Pooler for most queries (port 6543)
    • Better for short-lived connections
    • Lower latency
    • Recommended for most application queries
  2. Use Session Pooler only when needed (port 5432)
    • Required for prepared statements
    • Required for temporary tables
    • Required for advisory locks
  3. Monitor Connection Usage:
Action Items:
  • Verify application uses transaction pooler (port 6543)
  • Monitor connection counts
  • Document any queries requiring session pooler
  • Review connection pool settings in Supabase dashboard

Priority 5: RLS Policy Performance

Status: 467 tables with RLS enabled Recommendation: Optimize RLS policies for performance Common Performance Issues:
  1. RLS policies querying other RLS-protected tables (should use SECURITY DEFINER functions)
  2. Complex policy expressions that can’t use indexes
  3. Missing indexes on columns used in policy USING clauses
SQL to Check:
Best Practices:
  • ✅ Use SECURITY DEFINER functions for cross-table checks (already implemented)
  • ✅ Keep policy expressions simple (use indexes)
  • ✅ Index columns used in policy USING clauses
Action Items:
  • Review RLS policies for complex expressions
  • Verify SECURITY DEFINER functions are used for cross-table checks
  • Add indexes for policy-filtered columns if missing

Priority 6: JSONB Column Optimization

Status: 363 tables with custom_fields JSONB Recommendation: Add GIN indexes for JSONB queries Check if indexes exist:
Add GIN indexes if queries filter on custom_fields:
Note: Only add GIN indexes if you’re actually querying custom_fields. They have write overhead. Action Items:
  • Identify tables where custom_fields is queried
  • Add GIN indexes only for those tables
  • Monitor write performance impact

Priority 7: Storage Optimization

Status: Recent index cleanup freed ~11.3 MB Recommendations:
  1. Monitor Table Sizes:
  1. Check for Bloat:
  1. Archive Old Data:
    • Consider archiving old audit logs
    • Archive completed workflow executions older than 1 year
    • Archive old notifications
Action Items:
  • Identify largest tables
  • Plan data archival strategy for old records
  • Monitor storage growth trends

Monitoring & Alerting

1. Slow Query Detection:
2. Index Usage:
3. Connection Monitoring:
4. Table Statistics:

Implementation Plan

Phase 1: Assessment (Week 1) ✅ COMPLETED

  • Access Supabase performance advisors (MCP timeout - documented for Dashboard access)
  • Run monitoring queries above (scripts created)
  • Document current state (initial assessment report created)
  • Prioritize recommendations (verified PF-52 status)

Phase 2: Quick Wins (Week 2) ✅ COMPLETED

  • Enable pg_stat_statements - Migration created: 20260116055353_*.sql
  • Add missing indexes for slow queries - Migration created: 20260116055600_*.sql
  • Verify auto-vacuum is working (documentation added)
  • Review connection pool configuration (uses transaction pooler by default)

Phase 3: Optimization (Weeks 3-4) ✅ COMPLETED

  • Add GIN indexes for JSONB queries - Not needed (no custom_fields filtering found)
  • Optimize RLS policies - Already optimized (663 SECURITY DEFINER functions)
  • Review and optimize slow queries (index migration covers common patterns)
  • Plan data archival strategy - Document created: DATA_ARCHIVAL_PLAN.md

Phase 4: Monitoring (Ongoing) ✅ SETUP COMPLETE

  • Weekly review of slow queries - MONITORING_GUIDE.md created
  • Monthly index usage review - Process documented
  • Quarterly storage optimization review - Schedule established
  • Document performance trends - Baseline template created

Accessing Performance Advisors

Method 1: Supabase MCP (Preferred)

Method 2: Supabase Dashboard

  1. Navigate to: Project Settings → Database → Advisors
  2. Click Performance tab
  3. Review recommendations
  4. Document findings in this file

Method 3: Supabase CLI


References

Project Documentation

  • Index Cleanup: docs/database/INDEX_CLEANUP_ANALYSIS.md
  • Migration Plan: docs/database/migration-workflow.md (live; original 2026-01 playbook archived at docs/archive/migration-2026-01/COMPLETION_PLAN.md)
  • Query Performance Spec: specs/pf/specs/PF-52-query-performance-monitoring.md
  • Performance Guide: docs/development/PERFORMANCE_OPTIMIZATION_GUIDE.md
  • Monitoring Guide: docs/performance/MONITORING_GUIDE.md
  • Data Archival Plan: docs/migration/DATA_ARCHIVAL_PLAN.md

Scripts

  • Assessment Queries: scripts/performance/assessment-queries.sql
  • Monitoring Queries: scripts/performance/monitoring-queries.sql

Reports

  • Initial Assessment: reports/performance/initial-assessment-2026-01-16.md
  • Baseline Template: reports/performance/baseline-metrics-template.md

Migrations

  • pg_stat_statements: supabase/migrations/20260116055353_*.sql
  • Performance Indexes: supabase/migrations/20260116055600_*.sql

External Resources


Next Steps

  1. Resolve MCP connection timeout - Check Supabase MCP server status
  2. Access performance advisors - Use dashboard or CLI as fallback
  3. Run assessment queries - Document current state
  4. Prioritize recommendations - Focus on high-impact, low-effort items first
  5. Implement monitoring - Set up regular performance reviews

Performance Advisor Access Findings (2026-01-16)

MCP Status: Connection timeouts when accessing performance advisors and executing SQL queries. Recommended Approach:
  1. Use Supabase Dashboard: Navigate to Project Settings → Database → Advisors → Performance tab
  2. Project URL: https://rzfzikcargkoyhgqllap.supabase.co
  3. Alternative: Use Supabase CLI: supabase db advisors --type performance
Assessment Scripts: See scripts/performance/assessment-queries.sql for diagnostic queries to run via Dashboard SQL Editor.
Last Updated: 2026-01-16
Status: Implementation Complete - Migrations and documentation created
Next Review: After deploying migrations and running first weekly review