supabase-postgres-best-practices

Provides Postgres performance optimization rules and SQL examples for query, schema, and connection tuning.

Updated May 31, 2026
One-click install
npx skills add https://github.com/decoutkhanqindev/Lich-Viet-Loc-Phat --skill supabase-postgres-best-practices-decoutkhanqindev
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: supabase-postgres-best-practices
Source: https://github.com/decoutkhanqindev/Lich-Viet-Loc-Phat/tree/main/.claude/skills/supabase-postgres-best-practices
Command: npx skills add https://github.com/decoutkhanqindev/Lich-Viet-Loc-Phat --skill supabase-postgres-best-practices-decoutkhanqindev

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Slow queries, missing indexes, connection exhaustion, and misconfigured Row-Level Security are common Postgres issues that are hard to diagnose without deep expertise. This Skill gives an AI assistant a structured, prioritized rulebook of Postgres best practices so it can write, review, and optimize SQL correctly the first time. ## Core Features & Use Cases - Prioritized Rule Library: 25+ rules across 8 categories (query performance, connection management, security/RLS, schema design, locking, data access, monitoring, advanced features), each ranked by impact from CRITICAL to LOW. - Incorrect vs. Correct SQL Examples: Every rule shows the anti-pattern first, then the optimized rewrite, with quantified impact (e.g., 100x faster with proper indexes, 10x throughput with SKIP LOCKED). - Supabase-Specific Guidance: Covers RLS policy optimization, connection pooling modes, prepared statement behavior with poolers, and auth-aware policies. - Use Case: Ask the assistant to review a slow query on a multi-tenant orders table; it applies rules like adding composite indexes, wrapping auth.uid() in a subselect for RLS performance, and switching OFFSET pagination to cursor-based pagination. ## Quick Start Ask the assistant to review and optimize your Postgres query or schema design using the Supabase best practices rules.

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 to find sequential scans and rows removed by filters, then add indexes on WHERE and JOIN columns. The rules cover composite indexes, partial indexes, covering indexes with INCLUDE, and choosing the right index type such as GIN for JSONB.

What is the best way to fix N+1 queries in Postgres?▼

Replace per-item queries in a loop 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 giving 10-100x fewer round trips.

How do I make Supabase RLS policies faster?▼

Wrap functions like auth.uid() in a subselect, for example (select auth.uid()) = user_id, so they are evaluated once instead of per row. Also add indexes on columns used in policies and use security definer functions for complex membership checks.

Does connection pooling work with prepared statements in Postgres?▼

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 the connection persists.

When should I use cursor pagination instead of OFFSET?▼

Use cursor-based (keyset) pagination whenever users page deeply into large result sets. OFFSET scans all skipped rows and gets slower per page, while WHERE id > last_seen_id uses an index and stays O(1) regardless of page depth.

Why do Postgres deadlocks happen and how do I prevent them?▼

Deadlocks occur when transactions lock rows in different orders. Prevent them by acquiring locks in a consistent order with SELECT ... ORDER BY id FOR UPDATE, or by combining updates into a single atomic statement.