supabase-postgres-best-practices

Provides Postgres performance optimization rules and SQL examples for queries, schemas, and security.

Updated May 13, 2026
One-click install
npx skills add https://github.com/sapatamuku-creator/mastersapatamuku --skill supabase-postgres-best-practices-sapatamuku-creator
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: supabase-postgres-best-practices
Source: https://github.com/sapatamuku-creator/mastersapatamuku/tree/main/releases/v2.7/.agents/skills/supabase-postgres-best-practices
Command: npx skills add https://github.com/sapatamuku-creator/mastersapatamuku --skill supabase-postgres-best-practices-sapatamuku-creator

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing and reviewing Postgres queries, schemas, and configurations without expert guidance often leads to slow queries, missing indexes, connection exhaustion, and insecure Row-Level Security policies. ## Core Features & Use Cases - Prioritized Rule Library: Covers 8 categories of Postgres best practices ranked by impact, from critical query performance and connection management to advanced features like full-text search and JSONB indexing. - Incorrect vs. Correct SQL Examples: Each rule shows the anti-pattern first, then the optimized solution with quantified impact metrics and EXPLAIN output analysis. - Supabase-Specific Guidance: Includes RLS policy optimization, connection pooling with PgBouncer, prepared statement handling, and safe migration patterns. - Use Case: When reviewing a slow endpoint, consult the query performance rules to identify missing indexes, then apply cursor-based pagination and composite indexes to fix N+1 and OFFSET pagination issues. ## Quick Start Review my Postgres schema and slow queries using the Supabase best practices rules and suggest optimizations.

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?▼

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

How to fix N+1 queries in Postgres applications?▼

Replace per-item queries in loops with a single batch query using WHERE id = ANY($1::bigint[]) with an array parameter, or rewrite the logic as 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 fail in transaction-mode pooling where connections are shared. Use unnamed prepared statements, deallocate statements after use, or switch to session-mode pooling to avoid conflicts.

Why is my Row Level Security policy slow?▼

RLS policies that call functions like auth.uid() directly execute them once per row. Wrap the function in a subselect like (select auth.uid()) so it is evaluated once, and add indexes on columns referenced in the policy.

When should I use cursor-based pagination instead of OFFSET?▼

Use cursor-based pagination whenever users paginate deep into large result sets. OFFSET scans all skipped rows, so page 10000 scans 200,000 rows, while keyset pagination with WHERE id > last_seen_id stays O(1) regardless of depth.

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. Choose jsonb_path_ops for a 2-3x smaller index if you only need the @> operator, or an expression index for single-key lookups.