postgres-patterns

Provides PostgreSQL patterns for query optimization, indexing, schema design, and Row Level Security.

Updated Aug 17, 2026
One-click install
npx skills add https://github.com/prabaljainn/my-claude-code-setup --skill postgres-patterns-prabaljainn
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: postgres-patterns
Source: https://github.com/prabaljainn/my-claude-code-setup/tree/main/claude/skills/postgres-patterns
Command: npx skills add https://github.com/prabaljainn/my-claude-code-setup --skill postgres-patterns-prabaljainn

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing efficient PostgreSQL queries and schemas requires knowing which index types, data types, and patterns fit each situation, and mistakes like unindexed foreign keys or OFFSET pagination cause slow queries and bloated tables. ## Core Features & Use Cases - Index and Data Type Cheat Sheets: Quick-reference tables mapping query patterns to the right index type (B-tree, GIN, BRIN) and use cases to correct column types (timestamptz, numeric, bigint). - Ready-to-Use SQL Patterns: Copy-paste examples for composite and partial indexes, optimized RLS policies, UPSERT, cursor pagination, and SKIP LOCKED queue processing. - Anti-Pattern Detection Queries: Diagnostic SQL to find unindexed foreign keys, slow queries via pg_stat_statements, and table bloat. - Use Case: When a query on your orders table is slow, use the index cheat sheet to add a composite index on (status, created_at) and verify the fix with the slow-query detection query. ## Quick Start Ask the assistant to review your PostgreSQL query or schema using the postgres-patterns skill and suggest the right indexes and data types.

Frequently Asked Questions about postgres-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I choose the right PostgreSQL index type for a query?▼

Match the query pattern to the index type: B-tree for equality and range comparisons, composite indexes for multi-column filters with equality columns first, GIN for jsonb containment and full-text search, and BRIN for time-series range scans.

How do I optimize a slow PostgreSQL query?▼

Start by checking pg_stat_statements for queries with high mean execution time, then add appropriate indexes such as composite, covering, or partial indexes. Replace OFFSET pagination with cursor pagination using WHERE id > last_id for O(1) performance.

What data types should I use in PostgreSQL schema design?▼

Use bigint for IDs, text instead of varchar(255) for strings, timestamptz instead of timestamp for time values, numeric(10,2) for money instead of float, and boolean for flags rather than varchar or int.

How do I write an optimized Row Level Security policy in PostgreSQL?▼

Wrap the auth function call in a SELECT subquery, for example USING ((SELECT auth.uid()) = user_id). This prevents the function from being re-evaluated for every row, which significantly improves policy performance.

How do I find unindexed foreign keys in PostgreSQL?▼

Query pg_constraint joined with pg_attribute for foreign key constraints, then check pg_index to confirm no index covers the constrained columns. The skill provides a ready-to-run SQL query for this detection.

When should I use SKIP LOCKED in PostgreSQL?▼

Use SELECT ... FOR UPDATE SKIP LOCKED when implementing queue processing, so multiple workers can claim pending jobs without blocking each other. Combine it with an UPDATE ... RETURNING statement to atomically mark and fetch the next job.