Overview
PostgreSQL query optimization and schema design. Covers EXPLAIN ANALYZE, index types, query rewriting, partitioning, and connection pooling.
Capabilities
- Analyze query performance with EXPLAIN ANALYZE
- Design optimal indexes (B-tree, GIN, GiST, BRIN)
- Optimize slow queries through rewriting and restructuring
- Implement table partitioning for large datasets
- Configure connection pooling (PgBouncer, Supabase Pooler)
When to Use
Trigger phrases:
"postgres queries"
"PostgreSQL optimization — query tuning, schema design, indexing strategies, and "
Queries taking >100ms that should be <10ms
Database CPU or I/O is a bottleneck
Designing schema for a new high-traffic feature
Planning partitioning strategy for large tables
When NOT to Use
- Task is about deployment, not development (use deploy skills)
- Task is about code review, not writing (use review skills)
- You need to understand existing code first (use research skills)
- Task is about testing only (use test skills)
- Requirements are unclear (clarify first)
- Task is trivially simple (single line fix)
Pseudo Code
The postgres-queries workflow follows a standard pipeline pattern.
Core flow:
# postgres-queries primary flow
input = prepare(raw_data)
result = process(input, config={analysis, design, indexing, optimization, performance})
validate(result)
deliver(result)
Error handling:
on error:
log(error_details)
retry_with_backoff(max=3)
if still_failing: alert_and_escalate()
EXPLAIN ANALYZE
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT u.name, COUNT(o.id)
FROM users u
JOIN orders o ON o.user_id = u.id
WHERE o.created_at > NOW() - INTERVAL '30 days'
GROUP BY u.name;
Index Strategy
-- Composite index for common query pattern
CREATE INDEX idx_orders_user_date ON orders(user_id, created_at DESC);
-- Partial index for active records
CREATE INDEX idx_active_users ON users(email) WHERE deleted_at IS NULL;
-- GIN index for JSONB
CREATE INDEX idx_metadata ON products USING GIN(metadata);
Common Patterns
- EXPLAIN first: Always EXPLAIN ANALYZE before optimizing
- Composite indexes: Column order matters — put equality columns first, range last
- Partial indexes: Index only rows you query frequently
- Connection pooling: Use PgBouncer for >100 concurrent connections
How to Use
- Understand the requirement and existing codebase patterns
- Design the solution with error handling and testability in mind
- Implement incrementally with tests for each change
- Verify against expected outcomes (manual and automated)
- Document usage, edge cases, and integration points
- Review with team before merging to shared branches
Red Flags
- Skipping tests to ship faster: Untested code breaks in production when you least expect it
- No error handling in production code: Unhandled errors crash services and lose user data
- Hardcoded configuration values: Hardcoded values prevent environment switching and leak secrets
- Ignoring security implications: Missing input validation, auth bypasses, and injection vulnerabilities
- Over-engineering simple solutions: Premature abstraction adds complexity without proportional benefit
Verification
Process
- Analyze the task requirements
- Apply domain expertise
- Verify output quality
Anti-Rationalization Table
| Rationalization |
Reality |
| "Tests slow me down" |
Bugs slow you down 10x more. Tests are speed, not overhead. |
| "I will refactor later" |
Technical debt compounds. Refactor as you go. |
| "It works on my machine" |
If it is not in CI, it does not work. Ship proof, not claims. |
1---2name: postgres-queries3description: Use when postgreSQL optimization — query tuning, schema design, indexing strategies, and performance analysis. Use when working with postgres queries.4license: Apache-2.05---6789## Overview1011PostgreSQL query optimization and schema design. Covers EXPLAIN ANALYZE, index types, query rewriting, partitioning, and connection pooling.1213## Capabilities1415- Analyze query performance with EXPLAIN ANALYZE16- Design optimal indexes (B-tree, GIN, GiST, BRIN)17- Optimize slow queries through rewriting and restructuring18- Implement table partitioning for large datasets19- Configure connection pooling (PgBouncer, Supabase Pooler)2021## When to Use22**Trigger phrases:**23- "postgres queries"24- "PostgreSQL optimization — query tuning, schema design, indexing strategies, and "252627- Queries taking >100ms that should be <10ms28- Database CPU or I/O is a bottleneck29- Designing schema for a new high-traffic feature30- Planning partitioning strategy for large tables3132## When NOT to Use3334- Task is about deployment, not development (use deploy skills)35- Task is about code review, not writing (use review skills)36- You need to understand existing code first (use research skills)37- Task is about testing only (use test skills)38- Requirements are unclear (clarify first)39- Task is trivially simple (single line fix)404142## Pseudo Code4344The postgres-queries workflow follows a standard pipeline pattern.4546Core flow:47```48# postgres-queries primary flow49input = prepare(raw_data)50result = process(input, config={analysis, design, indexing, optimization, performance})51validate(result)52deliver(result)53```5455Error handling:56```57on error:58 log(error_details)59 retry_with_backoff(max=3)60 if still_failing: alert_and_escalate()61```626364### EXPLAIN ANALYZE65```sql66EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)67SELECT u.name, COUNT(o.id)68FROM users u69JOIN orders o ON o.user_id = u.id70WHERE o.created_at > NOW() - INTERVAL '30 days'71GROUP BY u.name;72```7374### Index Strategy75```sql76-- Composite index for common query pattern77CREATE INDEX idx_orders_user_date ON orders(user_id, created_at DESC);7879-- Partial index for active records80CREATE INDEX idx_active_users ON users(email) WHERE deleted_at IS NULL;8182-- GIN index for JSONB83CREATE INDEX idx_metadata ON products USING GIN(metadata);84```8586## Common Patterns8788- **EXPLAIN first**: Always EXPLAIN ANALYZE before optimizing89- **Composite indexes**: Column order matters — put equality columns first, range last90- **Partial indexes**: Index only rows you query frequently91- **Connection pooling**: Use PgBouncer for >100 concurrent connections9293## How to Use94951. Understand the requirement and existing codebase patterns962. Design the solution with error handling and testability in mind973. Implement incrementally with tests for each change984. Verify against expected outcomes (manual and automated)995. Document usage, edge cases, and integration points1006. Review with team before merging to shared branches101102## Red Flags103104- **Skipping tests to ship faster**: Untested code breaks in production when you least expect it105- **No error handling in production code**: Unhandled errors crash services and lose user data106- **Hardcoded configuration values**: Hardcoded values prevent environment switching and leak secrets107- **Ignoring security implications**: Missing input validation, auth bypasses, and injection vulnerabilities108- **Over-engineering simple solutions**: Premature abstraction adds complexity without proportional benefit109110## Verification111112- [ ] Skill output matches expected behavior113114## Process1151161. Analyze the task requirements1172. Apply domain expertise1183. Verify output quality119120## Anti-Rationalization Table121122| Rationalization | Reality |123|---|---|124| "Tests slow me down" | Bugs slow you down 10x more. Tests are speed, not overhead. |125| "I will refactor later" | Technical debt compounds. Refactor as you go. |126| "It works on my machine" | If it is not in CI, it does not work. Ship proof, not claims. |