supabase-postgres-best-practices

Applies Postgres performance, schema, security, and migration rules to SQL authoring and optimization tasks.

1|Updated Sep 15, 2026
One-click install
npx skills add https://github.com/amoai-tech/mdeai --skill supabase-postgres-best-practices-amoai-tech
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: supabase-postgres-best-practices
Source: https://github.com/amoai-tech/mdeai/tree/main/.claude/skills/supabase/references/official/supabase-postgres-best-practices
Command: npx skills add https://github.com/amoai-tech/mdeai --skill supabase-postgres-best-practices-amoai-tech

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing or changing Postgres schemas, queries, indexes, and RLS policies without proven rules leads to slow queries, connection exhaustion, deadlocks, and data leaks. This Skill provides a prioritized rule set maintained by Supabase so every database change follows tested best practices. ## Core Features & Use Cases - Prioritized Rule Categories: Eight 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 such as 10-100x faster queries. - Security and RLS Guidance: Covers Row Level Security policies, RLS performance optimization, SECURITY DEFINER functions, 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 policy performance patterns. ## Quick Start Ask the AI to review your Postgres schema or query using the Supabase Postgres best practices before writing any migration or SQL change.

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 with indexes?

Add indexes on columns used in WHERE and JOIN clauses to avoid sequential scans, which can be 100-1000x slower on large tables. Use composite indexes for multi-column filters, partial indexes for filtered queries, and covering indexes with INCLUDE to enable index-only scans.

What is the best way to write RLS policies in Supabase?

Enable row level security on the table, then create policies that wrap functions like auth.uid() in a SELECT subquery so they execute once instead of per row. Always index columns referenced in policies and use security definer functions in a private schema for complex checks.

Does Postgres 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 statements after use, or switch to session-mode pooling where the connection persists.

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

Postgres does not support IF NOT EXISTS for ADD CONSTRAINT, so the statement raises a syntax error. Wrap the alteration in a DO block that checks pg_constraint for the constraint name before adding it, making the migration idempotent.

When should I use cursor pagination instead of OFFSET?

Use cursor-based pagination whenever queries page deep into large tables, because OFFSET scans all skipped rows and slows linearly with page depth. Cursor pagination filters on the last seen key and uses an index, keeping performance constant regardless of page number.

What are the limitations of random UUIDs as primary keys?

Random UUIDv4 primary keys cause index fragmentation and scattered inserts on large tables. Prefer bigint identity columns for single databases, or time-ordered UUIDv7 via the pg_uuidv7 extension for distributed systems.