supabase-performance-tuning
Optimize Supabase query performance with indexes, EXPLAIN ANALYZE, connection pooling, column selection, pagination, RPC functions, materialized views, and diagnostics. Use when queries are slow, connections are exhausted, response payloads are bloated, or when preparing a Supabase project for production-scale traffic. Trigger with phrases like "supabase performance", "supabase slow queries", "optimize supabase", "supabase index", "supabase connection pool", "supabase pagination", "supabase explain analyze".
What this skill does
# Supabase Performance Tuning ## Overview Systematically improve Supabase query and database performance across three layers: PostgreSQL engine (indexes, query plans, materialized views), Supabase infrastructure (Supavisor connection pooling, Edge Functions, read replicas), and client SDK patterns (column selection, pagination, RPC functions). Every technique here is measurable — run `EXPLAIN ANALYZE` before and after to confirm the improvement. ## Prerequisites - Supabase project (local or hosted) with `@supabase/supabase-js` v2+ installed - Supabase CLI installed (`npx supabase --version` to verify) - Access to the SQL Editor in the Supabase Dashboard or a direct Postgres connection - `pg_stat_statements` extension enabled (Step 1 covers this) ## Instructions ### Step 1: Diagnose — Find What Is Slow Start every performance effort with data. Enable `pg_stat_statements` and run the Supabase CLI diagnostics to identify bottlenecks before optimizing. **Enable the stats extension:** ```sql CREATE EXTENSION IF NOT EXISTS pg_stat_statements; ``` **Find the slowest queries by average execution time:** ```sql SELECT query, calls, mean_exec_time::numeric(10,2) AS avg_ms, total_exec_time::numeric(10,2) AS total_ms, rows FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 10; ``` **Check index usage and cache hit rates with the Supabase CLI:** ```bash # Which indexes are actually being used? npx supabase inspect db index-usage # What percentage of queries are served from cache vs disk? npx supabase inspect db cache-hit # Tables consuming the most space npx supabase inspect db table-sizes ``` **Inspect active connections for pooling issues:** ```sql SELECT state, count(*), max(age(now(), state_change)) AS max_age FROM pg_stat_activity WHERE datname = current_database() GROUP BY state; ``` If `idle` connections exceed your plan's limit or `active` queries show high `max_age`, connection pooling (Step 2) and query optimization (Step 3) are the priority. ### Step 2: Indexes and Query Plans Indexes are the single highest-impact optimization. Use `EXPLAIN ANALYZE` to read query plans, then create targeted indexes. **Read a query plan:** ```sql EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) SELECT * FROM users WHERE email = '[email protected]'; ``` Look for `Seq Scan` on large tables — that means no index is being used. After adding an index, the plan should show `Index Scan` or `Index Only Scan`. **Create a basic index:** ```sql CREATE INDEX idx_users_email ON users(email); ``` **Create a composite index for multi-column filters:** ```sql -- Optimizes: WHERE user_id = ? AND created_at > ? ORDER BY created_at DESC CREATE INDEX idx_orders_user_created ON orders(user_id, created_at DESC); ``` **Create a partial index to cover a common filter pattern:** ```sql -- Only indexes incomplete todos — much smaller and faster than full-table index CREATE INDEX idx_todos_user_incomplete ON todos(user_id, inserted_at DESC) WHERE is_complete = false; ``` **Find missing indexes on foreign keys (common source of slow JOINs):** ```sql SELECT tc.table_name, kcu.column_name AS fk_column, 'CREATE INDEX idx_' || tc.table_name || '_' || kcu.column_name || ' ON public.' || tc.table_name || '(' || kcu.column_name || ');' AS fix FROM information_schema.table_constraints tc JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name LEFT JOIN pg_indexes i ON i.tablename = tc.table_name AND i.indexdef LIKE '%' || kcu.column_name || '%' WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_schema = 'public' AND i.indexname IS NULL; ``` **Find unused indexes (candidates for removal to reduce write overhead):** ```sql SELECT schemaname, relname, indexrelname, idx_scan FROM pg_stat_user_indexes WHERE idx_scan = 0 AND schemaname = 'public' ORDER BY pg_relation_size(indexrelid) DESC; ``` Always use `CREATE INDEX CONCURRENTLY` on production tables to avoid locking writes during index creation. ### Step 3: Client SDK and Infrastructure Optimization Optimize the Supabase JS client calls, then leverage infrastructure features for scale. **Select only needed columns — avoid `select('*')`:** ```typescript import { createClient } from '@supabase/supabase-js' const supabase = createClient( process.env.SUPABASE_URL!, process.env.SUPABASE_ANON_KEY! ) // BAD: fetches every column, large payloads const { data } = await supabase.from('users').select('*') // GOOD: only the columns you need const { data } = await supabase.from('users').select('id, name, avatar_url') ``` **Paginate with `.range()` instead of loading all rows:** ```typescript // Page 1: rows 0-49 const { data: page1 } = await supabase .from('products') .select('id, name, price') .order('created_at', { ascending: false }) .range(0, 49) // Page 2: rows 50-99 const { data: page2 } = await supabase .from('products') .select('id, name, price') .order('created_at', { ascending: false }) .range(50, 99) ``` **Use RPC functions to push complex logic to Postgres:** ```sql -- Create a server-side function for an expensive aggregation CREATE OR REPLACE FUNCTION get_dashboard_stats(org_id uuid) RETURNS json AS $$ SELECT json_build_object( 'total_users', (SELECT count(*) FROM users WHERE organization_id = org_id), 'active_projects', (SELECT count(*) FROM projects WHERE organization_id = org_id AND status = 'active'), 'tasks_completed_30d', (SELECT count(*) FROM tasks t JOIN projects p ON p.id = t.project_id WHERE p.organization_id = org_id AND t.completed_at > now() - interval '30 days') ); $$ LANGUAGE sql STABLE; ``` ```typescript // One network call instead of three separate queries const { data } = await supabase.rpc('get_dashboard_stats', { org_id: 'your-org-uuid' }) ``` **Create materialized views for expensive aggregations:** ```sql -- Precompute a leaderboard instead of recalculating on every request CREATE MATERIALIZED VIEW leaderboard AS SELECT u.id, u.username, count(t.id) AS tasks_completed, rank() OVER (ORDER BY count(t.id) DESC) AS rank FROM users u LEFT JOIN tasks t ON t.assignee_id = u.id AND t.status = 'done' GROUP BY u.id, u.username; -- Create an index on the materialized view CREATE UNIQUE INDEX idx_leaderboard_user ON leaderboard(id); -- Refresh on a schedule (e.g., via pg_cron or a cron Edge Function) REFRESH MATERIALIZED VIEW CONCURRENTLY leaderboard; ``` **Configure connection pooling with Supavisor:** ```typescript // For serverless environments (Vercel, Netlify, Cloudflare Workers): // Use the pooled connection string with transaction mode // Dashboard → Settings → Database → Connection string → "Transaction mode" // The JS SDK uses PostgREST (HTTP) which has its own pooling — no config needed. // Direct Postgres clients (Prisma, Drizzle, pg) need the pooled string: import { Pool } from 'pg' const pool = new Pool({ connectionString: 'postgres://postgres.[ref]:[pwd]@aws-0-[region].pooler.supabase.com:6543/postgres', max: 5, // Keep low in serverless — Supavisor manages the upstream pool idleTimeoutMillis: 10000, }) ``` **Use Edge Functions for compute-heavy operations close to data:** ```typescript // supabase/functions/generate-report/index.ts // Edge Functions run in the same region as your database — low latency import { createClient } from 'https://esm.sh/@supabase/supabase-js@2' Deno.serve(async (req) => { const supabase = createClient( Deno.env.get('SUPABASE_URL')!, Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')! ) // Heavy aggregation runs next to the database, not in the user's browser const { data } = await supabase.rpc('get_dashboard_stats', { org_id: (await req.json()).org_id }) return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } }) }) ``` **Enable read replicas on Pro+ plans** for read-heavy workloads — route analytics and reporting queries to the replica to offload th
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.