supabase-postgres-best-practices

Provides Postgres performance optimization rules for queries, schemas, connections, and RLS policies.

2|1|Updated Apr 17, 2025
One-click install
npx skills add https://github.com/abraham-yusuf/pernikahan-web --skill supabase-postgres-best-practices-abraham-yusuf
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: supabase-postgres-best-practices
Source: https://github.com/abraham-yusuf/pernikahan-web/tree/main/.agents/skills/supabase-postgres-best-practices
Command: npx skills add https://github.com/abraham-yusuf/pernikahan-web --skill supabase-postgres-best-practices-abraham-yusuf

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Slow queries, missing indexes, connection exhaustion, and poorly written Row-Level Security policies are the most common causes of Postgres performance issues, and they are hard to diagnose without deep database expertise. ## Core Features & Use Cases - Prioritized Rule Library: 30+ optimization rules organized into 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 metrics such as "100x faster queries" or "50% smaller index". - Supabase-Specific Guidance: Covers RLS policy optimization, connection pooling modes, prepared statement handling, and auth.uid() caching patterns specific to Supabase deployments. - Use Case: When writing a migration that adds a foreign key or reviewing a slow dashboard query, consult the relevant rule file to apply the correct index strategy, pagination pattern, or transaction scope. ## Quick Start Review my SQL schema and queries using the supabase-postgres-best-practices skill and suggest optimizations for any slow or unindexed operations.

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 adding indexes on WHERE and JOIN columns, which can yield 100-1000x speedups on large tables. Use EXPLAIN ANALYZE to identify sequential scans, then apply composite, partial, or covering indexes matching your query filters.

How to fix N+1 query problems in Postgres?

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

Does connection pooling work with prepared statements?

Named prepared statements fail in transaction-mode pooling because connections are shared between requests. Use unnamed prepared statements, deallocate after use, or switch to session-mode pooling where connections persist.

Why is my RLS policy slow on large tables?

Functions like auth.uid() are called once per row unless wrapped in a SELECT subquery, which caches the result. Rewrite policies as (select auth.uid()) = user_id and add indexes on columns referenced in policies.

When should I use OFFSET pagination vs cursor pagination?

OFFSET pagination scans all skipped rows, degrading linearly with page depth. Cursor-based (keyset) pagination using WHERE id > last_seen_id maintains constant O(1) performance regardless of page number.

What index type should I use for JSONB columns?

Use GIN indexes for JSONB containment queries with the @> operator, since B-tree indexes cannot optimize them. The jsonb_path_ops operator class produces indexes 2-3x smaller when only containment queries are needed.