
Postgres Patterns
Apply Supabase-aligned PostgreSQL patterns for indexes, types, schema design, RLS, and pooling while writing SQL and migrations.
Overview
Postgres Patterns is an agent skill for the Build phase that supplies PostgreSQL query, schema, indexing, RLS, and pooling patterns based on Supabase best practices.
Install
npx skills add https://github.com/affaan-m/everything-claude-code --skill postgres-patternsWhat is this skill?
- Index cheat sheet mapping query shapes to B-tree, GIN, BRIN, and composite strategies
- Data-type guidance (bigint IDs, text, timestamptz, numeric money, boolean flags)
- Composite index ordering: equality columns before range columns
- Row Level Security and connection-pooling reminders for production Postgres
- Points to database-reviewer agent for deeper audits beyond this quick reference
- Index cheat sheet covers 6 query-pattern rows (B-tree, composite, GIN, BRIN)
- Data-type quick reference lists 5 common use-case rows
Adoption & trust: 6.4k installs on skills.sh; 210k GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
You are writing Postgres migrations or queries and need correct index types, column types, and security patterns without guessing from outdated blog posts.
Who is it for?
Indie backend builders on Supabase or self-hosted Postgres who want checklist-level DDL and query discipline during active development.
Skip if: Frontend-only tasks, non-Postgres databases, or teams that want automated load testing without human schema review.
When should I use this skill?
Writing SQL queries or migrations, designing schemas, troubleshooting slow queries, implementing Row Level Security, or setting up connection pooling.
What do I get? / Deliverables
Your agent applies concrete index and schema rules— and escalates to database-reviewer when a full migration audit is needed.
- Index and schema recommendations aligned to query patterns
- RLS and pooling guidance snippets for migrations
Recommended Skills
Journey fit
Build/backend is the canonical home for database design and query work that ships inside your product. Backend covers SQL, migrations, and data-layer security—not frontend UI or launch distribution.
How it compares
Quick-reference skill patterns, not a hosted database MCP or automatic query profiler.
Common Questions / FAQ
Who is postgres-patterns for?
Solo developers and small teams building on PostgreSQL—especially Supabase—who want agent-guided schema, index, and RLS conventions while coding.
When should I use postgres-patterns?
During Build/backend when writing SQL or migrations, debugging slow queries, adding RLS policies, or setting up pooling—any time Postgres DDL needs to match production-grade habits.
Is postgres-patterns safe to install?
See the Security Audits panel on this Prism page; the skill advises on SQL and security patterns but does not replace reviewing migrations and permissions in your own repo.
SKILL.md
READMESKILL.md - Postgres Patterns
# PostgreSQL Patterns Quick reference for PostgreSQL best practices. For detailed guidance, use the `database-reviewer` agent. ## When to Activate - Writing SQL queries or migrations - Designing database schemas - Troubleshooting slow queries - Implementing Row Level Security - Setting up connection pooling ## Quick Reference ### Index Cheat Sheet | Query Pattern | Index Type | Example | |--------------|------------|---------| | `WHERE col = value` | B-tree (default) | `CREATE INDEX idx ON t (col)` | | `WHERE col > value` | B-tree | `CREATE INDEX idx ON t (col)` | | `WHERE a = x AND b > y` | Composite | `CREATE INDEX idx ON t (a, b)` | | `WHERE jsonb @> '{}'` | GIN | `CREATE INDEX idx ON t USING gin (col)` | | `WHERE tsv @@ query` | GIN | `CREATE INDEX idx ON t USING gin (col)` | | Time-series ranges | BRIN | `CREATE INDEX idx ON t USING brin (col)` | ### Data Type Quick Reference | Use Case | Correct Type | Avoid | |----------|-------------|-------| | IDs | `bigint` | `int`, random UUID | | Strings | `text` | `varchar(255)` | | Timestamps | `timestamptz` | `timestamp` | | Money | `numeric(10,2)` | `float` | | Flags | `boolean` | `varchar`, `int` | ### Common Patterns **Composite Index Order:** ```sql -- Equality columns first, then range columns CREATE INDEX idx ON orders (status, created_at); -- Works for: WHERE status = 'pending' AND created_at > '2024-01-01' ``` **Covering Index:** ```sql CREATE INDEX idx ON users (email) INCLUDE (name, created_at); -- Avoids table lookup for SELECT email, name, created_at ``` **Partial Index:** ```sql CREATE INDEX idx ON users (email) WHERE deleted_at IS NULL; -- Smaller index, only includes active users ``` **RLS Policy (Optimized):** ```sql CREATE POLICY policy ON orders USING ((SELECT auth.uid()) = user_id); -- Wrap in SELECT! ``` **UPSERT:** ```sql INSERT INTO settings (user_id, key, value) VALUES (123, 'theme', 'dark') ON CONFLICT (user_id, key) DO UPDATE SET value = EXCLUDED.value; ``` **Cursor Pagination:** ```sql SELECT * FROM products WHERE id > $last_id ORDER BY id LIMIT 20; -- O(1) vs OFFSET which is O(n) ``` **Queue Processing:** ```sql UPDATE jobs SET status = 'processing' WHERE id = ( SELECT id FROM jobs WHERE status = 'pending' ORDER BY created_at LIMIT 1 FOR UPDATE SKIP LOCKED ) RETURNING *; ``` ### Anti-Pattern Detection ```sql -- Find unindexed foreign keys SELECT conrelid::regclass, a.attname FROM pg_constraint c JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = ANY(c.conkey) WHERE c.contype = 'f' AND NOT EXISTS ( SELECT 1 FROM pg_index i WHERE i.indrelid = c.conrelid AND a.attnum = ANY(i.indkey) ); -- Find slow queries SELECT query, mean_exec_time, calls FROM pg_stat_statements WHERE mean_exec_time > 100 ORDER BY mean_exec_time DESC; -- Check table bloat SELECT relname, n_dead_tup, last_vacuum FROM pg_stat_user_tables WHERE n_dead_tup > 1000 ORDER BY n_dead_tup DESC; ``` ### Configuration Template ```sql -- Connection limits (adjust for RAM) ALTER SYSTEM SET max_connections = 100; ALTER SYSTEM SET work_mem = '8MB'; -- Timeouts ALTER SYSTEM SET idle_in_transaction_session_timeout = '30s'; ALTER SYSTEM SET statement_timeout = '30s'; -- Monitoring CREATE EXTENSION IF NOT EXISTS pg_stat_statements; -- Security defaults REVOKE ALL ON SCHEMA public FROM public; SELECT pg_reload_conf(); ``` ## Related - Agent: `database-reviewer` - Full database review workflow - Skill: `clickhouse-io` - ClickHouse analytics patterns - Skill: `backend-patterns` - API and backend patterns --- *Based on Supabase Agent Skills (credit: Supabase team) (MIT License)*