postgres-patterns

Provides PostgreSQL patterns for indexing, schema design, query optimization, and row-level security.

Updated Mar 26, 2026
One-click install
npx skills add https://github.com/erwinv2k-TKG/AgentesVSC --skill postgres-patterns-erwinv2k-tkg
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: postgres-patterns
Source: https://github.com/erwinv2k-TKG/AgentesVSC/tree/main/packs/everything-claude-code/docs/zh-TW/skills/postgres-patterns
Command: npx skills add https://github.com/erwinv2k-TKG/AgentesVSC --skill postgres-patterns-erwinv2k-tkg

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 missing foreign key indexes or OFFSET pagination cause slow queries and table bloat. ## Core Features & Use Cases - Index Selection Guide: Cheat sheets mapping query patterns to B-tree, GIN, BRIN, composite, covering, and partial indexes. - Query Patterns: Ready-to-use SQL for UPSERT, cursor pagination, queue processing with SKIP LOCKED, and optimized RLS policies. - Anti-Pattern Detection: Diagnostic queries to find unindexed foreign keys, slow queries via pg_stat_statements, and table bloat. - Use Case: When writing a migration for a new orders table, consult the skill to choose bigint IDs, timestamptz columns, and a composite index on (status, created_at) matching your query filters. ## Quick Start Ask the assistant to review your SQL query or schema design using the postgres-patterns skill and suggest appropriate 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 my query?

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

How do I optimize PostgreSQL row-level security policies?

Wrap auth function calls in a SELECT subquery, such as USING ((SELECT auth.uid()) = user_id), so the planner evaluates it once per query instead of once per row. This significantly improves RLS policy performance.

What data types should I use in PostgreSQL schema design?

Use bigint for IDs, text instead of varchar(255), timestamptz instead of timestamp, numeric(10,2) for money instead of float, and boolean for flags. These choices avoid overflow, precision loss, and timezone bugs.

How do I find slow queries in PostgreSQL?

Enable the pg_stat_statements extension and query it for statements with mean_exec_time above your threshold, ordered by average execution time. The skill provides a ready-made query filtering statements over 100ms.

Why is OFFSET pagination slow in PostgreSQL?

OFFSET pagination scans and discards all skipped rows, making it O(n) as page depth grows. Cursor pagination using WHERE id > last_id with ORDER BY and LIMIT runs in O(1) regardless of position.

How do I detect missing indexes on foreign keys?

Query pg_constraint joined with pg_attribute for foreign key constraints, then check pg_index for a matching index. The skill includes a detection query that returns tables and columns lacking foreign key indexes.