supabase-postgres-best-practices

Provides Postgres performance optimization rules and SQL best practices for Supabase projects.

1|Updated Apr 8, 2026
One-click install
npx skills add https://github.com/Domush/ai-agent-web-development-skills --skill supabase-postgres-best-practices-domush
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: supabase-postgres-best-practices
Source: https://github.com/Domush/ai-agent-web-development-skills/tree/main/skills/supabase-postgres-best-practices
Command: npx skills add https://github.com/Domush/ai-agent-web-development-skills --skill supabase-postgres-best-practices-domush

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Postgres databases often suffer from slow queries, connection exhaustion, missing indexes, and insecure Row-Level Security configurations. This Skill gives AI agents a structured, prioritized rule set so they can write, review, and optimize SQL correctly the first time instead of guessing at performance fixes. ## Core Features & Use Cases - Prioritized Rule Categories: 8 categories ranked by impact, from critical query performance and connection management to advanced features like full-text search and JSONB indexing. - Error-First SQL Examples: Each rule shows an incorrect pattern with explanation, then the correct SQL rewrite with quantified impact (e.g., 10-100x faster queries, 50% smaller indexes). - Supabase-Specific Guidance: Covers RLS policy optimization, connection pooling modes, prepared statement handling, and pg_stat_statements monitoring. - Use Case: When an agent writes a migration adding a foreign key, it consults the schema rules and automatically adds the required index, avoiding slow JOINs and table-locking CASCADE deletes. ## Quick Start Ask the agent to review your SQL schema or slow query using the Supabase Postgres best practices and suggest optimized rewrites.

Frequently Asked Questions about supabase-postgres-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I optimize slow Postgres queries in Supabase?▼

Start by running EXPLAIN ANALYZE on the slow query to identify sequential scans or missing indexes. Then add indexes on WHERE and JOIN columns, use composite indexes for multi-column filters, and consider partial indexes for filtered queries.

How to fix N+1 query problems in Postgres applications?▼

Replace per-item queries in loops with a single batch query using WHERE id = ANY($1::bigint[]) or a JOIN. This reduces database round trips from N+1 to one, often improving performance 10-100x.

Does connection pooling work with prepared statements in Postgres?▼

Prepared statements are tied to individual connections, so they conflict with transaction-mode pooling. Use unnamed prepared statements, deallocate after use, or switch to session-mode pooling where connections persist.

Why is my Row-Level Security policy slow in Supabase?▼

RLS policies that call functions like auth.uid() per row cause severe slowdowns. Wrap function calls in a subselect like (select auth.uid()) so they execute once, and add indexes on columns referenced in policies.

When should I use cursor pagination instead of OFFSET?▼

Use cursor-based (keyset) pagination whenever users paginate deep into large tables. OFFSET scans all skipped rows and gets slower with page depth, while cursor pagination with WHERE id > last_id stays O(1) using the index.

What index type should I use for JSONB columns in Postgres?▼

Use a GIN index for JSONB containment queries with the @> operator, since B-tree indexes cannot optimize them. For single-key lookups, an expression index on (attributes->>'key') is smaller and faster.