supabase-postgres-best-practices

Optimizes Postgres queries, schemas, and configurations using Supabase performance guidelines.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Slow queries, missing indexes, connection exhaustion, and insecure Row Level Security policies are common Postgres issues that degrade application performance and reliability. This Skill provides a prioritized, example-driven rule set so you can write, review, and optimize Postgres code correctly the first time. ## Core Features & Use Cases - Query Performance Rules: Guidance on missing indexes, composite indexes, covering indexes, partial indexes, and choosing the right index type (B-tree, GIN, BRIN, Hash). - Connection & Security Best Practices: Connection pooling, prepared statement handling, idle timeouts, least-privilege roles, and RLS policy optimization. - Schema & Data Patterns: Data type selection, primary key strategy, foreign key indexing, partitioning, batch inserts, upserts, cursor pagination, and N+1 elimination. - Use Case: While writing a Drizzle ORM migration for a multi-tenant app, consult the RLS and foreign key indexing rules to add proper indexes and performant security policies before deploying. ## Quick Start Ask the AI to review your SQL query or schema design against the Supabase Postgres best practices 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 fix slow Postgres queries caused by missing indexes?

Add indexes on columns used in WHERE clauses and JOIN conditions to eliminate sequential scans. Use EXPLAIN ANALYZE to confirm the query switches from a Seq Scan to an Index Scan, which can be 100-1000x faster on large tables.

What is the best way to handle connection pooling in Postgres?

Use a pooler like PgBouncer between your application and Postgres, sizing the pool at roughly (CPU cores * 2) + spindle count. Transaction mode works for most apps, but use session mode if you rely on named prepared statements.

How do I make Row Level Security policies faster in Supabase?

Wrap functions like auth.uid() in a SELECT subquery so they execute once instead of per row, and add indexes on columns referenced in policies. For complex checks, use security definer functions to avoid per-row overhead.

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

Use cursor-based (keyset) pagination with a WHERE clause on the sort column instead of OFFSET. OFFSET scans all skipped rows and slows down on deep pages, while cursor pagination maintains consistent O(1) performance.

When should I partition a Postgres table?

Partition tables exceeding roughly 100 million rows, especially time-series data queried by date ranges. Range partitioning by timestamp lets queries scan only relevant partitions and allows instant deletion of old data via DROP TABLE.

Why do prepared statements fail with transaction-mode pooling?

Named prepared statements are bound to individual connections, but transaction-mode pooling reassigns connections per transaction. Use unnamed prepared statements, deallocate after use, or switch to session-mode pooling.