supabase-postgres-best-practices

Applies Postgres performance, schema, security, and migration rules to SQL authoring and database diagnostics.

Updated Aug 21, 2025
One-click install
npx skills add https://github.com/Adithiya-S/AI-Study-Companion --skill supabase-postgres-best-practices-adithiya-s
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: supabase-postgres-best-practices
Source: https://github.com/Adithiya-S/AI-Study-Companion/tree/main/.agents/skills/supabase-postgres-best-practices
Command: npx skills add https://github.com/Adithiya-S/AI-Study-Companion --skill supabase-postgres-best-practices-adithiya-s

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing and maintaining Postgres schemas, queries, and RLS policies without established rules leads to slow queries, connection exhaustion, deadlocks, and data leaks. This Skill provides a prioritized rule set so every SQL change follows proven Postgres best practices. ## Core Features & Use Cases - Prioritized Rule Categories: Covers 8 impact-ranked categories from query performance and connection management (critical) to advanced features like full-text search and JSONB indexing. - Incorrect vs. Correct SQL Examples: Each rule file shows the anti-pattern first, then the optimized rewrite with quantified impact (e.g., 100x faster queries with proper indexes). - Security & RLS Guidance: Includes Row-Level Security setup, performance optimization for policies, SECURITY DEFINER usage, and least-privilege role design. - Use Case: Before writing a migration that adds a foreign key or an RLS policy, load this Skill to get the correct index strategy, idempotent constraint syntax, and performant policy pattern. ## Quick Start Review my Postgres schema and slow queries using the Supabase Postgres best practices rules and suggest optimized SQL.

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 find sequential scans and rows removed by filters. Then add indexes on WHERE and JOIN columns, use composite or partial indexes for multi-column filters, and check pg_stat_statements for the highest total-time queries.

How do I write RLS policies in Supabase without hurting performance?

Wrap functions like auth.uid() in a SELECT subquery so they execute once instead of per row, and always index columns referenced in policies. For complex checks, use SECURITY DEFINER functions in a private schema with an explicit auth.uid() check inside.

Does Postgres automatically index foreign key columns?

No, Postgres does not create indexes on foreign key columns automatically. You must create them manually, otherwise JOINs and ON DELETE CASCADE operations trigger full table scans and lock the table.

Why does ADD CONSTRAINT IF NOT EXISTS fail in Postgres migrations?

Postgres does not support IF NOT EXISTS for ADD CONSTRAINT, so that syntax raises a SQLSTATE 42601 error. Use a DO block that checks pg_constraint for the constraint name before executing ALTER TABLE.

When should I use connection pooling with Postgres?

Use connection pooling for any application with concurrent users, since each Postgres connection consumes 1-3MB of RAM. A pooler like PgBouncer in transaction mode lets hundreds of users share a small pool of connections, but avoid named prepared statements in transaction mode.

Should I use OFFSET or cursor-based pagination in Postgres?

Use cursor-based (keyset) pagination with WHERE id > last_seen_id for consistent O(1) performance at any page depth. OFFSET scans all skipped rows, so page 10000 reads hundreds of thousands of rows before returning results.