supabase-postgres-best-practices

Provides prioritized Postgres performance optimization rules with incorrect and correct SQL examples.

Updated Jul 31, 2026
One-click install
npx skills add https://github.com/AarnavBaddam/skills --skill supabase-postgres-best-practices-aarnavbaddam
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: supabase-postgres-best-practices
Source: https://github.com/AarnavBaddam/skills/tree/main/supabase-best-practices
Command: npx skills add https://github.com/AarnavBaddam/skills --skill supabase-postgres-best-practices-aarnavbaddam

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Slow queries, connection exhaustion, and poorly designed schemas are common Postgres issues that are hard to diagnose without deep expertise. This Skill gives AI agents a structured, impact-prioritized rule set so they can write, review, and optimize Postgres SQL correctly the first time. ## Core Features & Use Cases - Prioritized Rule Library: 8 categories of optimization rules 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 an explanation, followed by the corrected SQL with quantified impact (e.g., 10-100x faster queries). - Supabase-Specific Guidance: Covers Row-Level Security policies, connection pooling modes, prepared statement handling, and auth-aware query patterns. - Use Case: When asked to design a multi-tenant orders table, the agent applies rules for foreign key indexes, RLS policies with cached auth.uid() calls, and appropriate data types, producing production-quality schema SQL. ## Quick Start Ask the agent to review your Postgres query or schema design and apply the Supabase best practices rules to optimize it.

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 and rows removed by filters, then apply composite, partial, or covering indexes based on your query patterns.

How to fix N+1 query problems in Postgres?

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, 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 for the entire session.

How do I make Row-Level Security policies faster?

Wrap functions like auth.uid() in a SELECT subquery so they execute once instead of per row, which can be 100x faster on large tables. Also add indexes on columns referenced in policies and use security definer functions for complex checks.

When should I use OFFSET pagination vs cursor pagination?

OFFSET pagination scans all skipped rows, becoming slower on deeper pages. Cursor-based (keyset) pagination using WHERE id > last_seen_id maintains constant O(1) performance regardless of page depth, making it the better choice for large datasets.

Why does my Postgres migration fail when adding constraints?

Postgres does not support ADD CONSTRAINT IF NOT EXISTS syntax, causing a syntax error. Wrap the ALTER TABLE in a DO block that checks pg_constraint for the constraint name before adding it, making migrations idempotent.