postgres-patterns

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

Updated Mar 18, 2026
One-click install
npx skills add https://github.com/freedom909/real-estate-saas --skill postgres-patterns-freedom909
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: postgres-patterns
Source: https://github.com/freedom909/real-estate-saas/tree/main/.trae/skills/postgres-patterns
Command: npx skills add https://github.com/freedom909/real-estate-saas --skill postgres-patterns-freedom909

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 in production. ## Core Features & Use Cases - Index Selection Guide: Cheat sheets mapping query patterns to B-tree, GIN, BRIN, composite, covering, and partial indexes. - Anti-Pattern Detection: Ready-to-run SQL queries that find unindexed foreign keys, slow queries via pg_stat_statements, and table bloat. - Operational Patterns: Templates for RLS policies, UPSERT, cursor pagination, queue processing with SKIP LOCKED, and server configuration. - Use Case: When a listings search endpoint becomes slow, use this Skill to identify the missing composite index, rewrite OFFSET pagination as cursor pagination, and verify the fix with pg_stat_statements. ## Quick Start Review my SQL migration and suggest the right indexes and data types using PostgreSQL best practices.

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 my query?▼

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

How to find slow queries in PostgreSQL?▼

Enable the pg_stat_statements extension, then query it for statements where mean_exec_time exceeds your threshold, ordered by mean_exec_time descending. This surfaces the slowest queries along with call counts so you can prioritize optimization.

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 timestamps, numeric(10,2) instead of float for money, and boolean for flags. These choices avoid precision loss and timezone bugs.

Does cursor pagination perform better than OFFSET in PostgreSQL?▼

Cursor pagination using WHERE id > last_id with ORDER BY and LIMIT runs in O(1) time, while OFFSET scans and discards rows in O(n) time. For large tables or infinite-scroll APIs, cursor pagination is the recommended approach.

Why is my Row Level Security policy slow in PostgreSQL?▼

RLS policies that call auth.uid() directly re-evaluate the function for every row. Wrapping it in a subselect, like (SELECT auth.uid()) = user_id, lets the planner cache the value and dramatically improves policy performance.

How do I detect unindexed foreign keys in PostgreSQL?▼

Query pg_constraint joined with pg_attribute for foreign key constraints, then check pg_index for a matching index on those columns. Foreign keys without indexes cause slow joins and locking issues during deletes on the referenced table.