Analyze and optimize PostgreSQL queries for OLTP and OLAP workloads with AI-assisted performance tuning, indexing strategies, and execution plan analysis.
"Perform a comprehensive PostgreSQL performance audit:1. Identify top 10 slowest queries from pg_stat_statements2. Analyze EXPLAIN plans for each3. Detect missing indexes with pg_stat_user_tables4. Find bloated tables needing VACUUM FULL5. Review configuration parameters6. Check for long-running transactions blocking others7. Provide prioritized optimization action plan"
Skill Content (2)
Additional code example for this skill.
postgresql-query-optimization.txt
"Optimize PostgreSQL for time-series data:1. 100M rows of sensor data per month2. Queries filter by device_id and time range3. Need 90-day retention with automated archival4. Implement declarative partitioning by month5. Add BRIN indexes on timestamp columns6. Configure autovacuum for partition management7. Create aggregate materialized views"
Skill Content (3)
Additional code example for this skill.
postgresql-query-optimization.txt
"Build high-performance full-text search:1. Search across title, description, tags fields2. Support phrase queries and ranking3. Handle 50M documents4. Sub-100ms query response time5. Use GIN indexes with tsvector6. Implement trigram similarity for typo tolerance7. Add weighted search across columns"
Skill Content (4)
Additional code example for this skill.
postgresql-query-optimization.txt
"Debug PostgreSQL replication lag:1. Replica is 30 seconds behind primary2. Analyze pg_stat_replication metrics3. Check for long-running queries on replica4. Identify write-heavy tables causing lag5. Tune max_wal_senders and wal_keep_size6. Recommend synchronous vs asynchronous replication7. Implement connection pooling strategy"
Features
Key capabilities and functionality
EXPLAIN plan analysis and visualization
Automatic index recommendation
Workload-specific tuning (OLTP/OLAP)
Query rewriting for performance
Use Cases
Common scenarios and applications
Optimize slow database queries
Design indexing strategies
Tune PostgreSQL configuration
Requirements
Prerequisites and dependencies
PostgreSQL 14+ (16+ recommended)
pg_stat_statements extension
EXPLAIN access permissions
psql or database client
Installation
Setup instructions and requirements
Claude Code Setup
sudo apt install postgresql-16
sudo systemctl start postgresql
psql -U postgres
CREATE EXTENSION pg_stat_statements;
ALTER SYSTEM SET shared_preload_libraries = 'pg_stat_statements';
Ask Claude: 'Analyze this slow query and optimize it'
Troubleshooting
Common issues and solutions
Index not being used by query planner
Run ANALYZE to update statistics, check WHERE clause matches index columns exactly, lower random_page_cost for SSDs, or use pg_hint_plan extension to force index usage.
Out of memory errors
Reduce work_mem or max_connections. Implement connection pooling with PgBouncer. Check for memory-intensive queries using hash joins or sorts.
Slow VACUUM operations
Increase maintenance_work_mem, run VACUUM during off-peak hours, consider VACUUM FREEZE for old tables, or use pg_repack for online table reorganization.
Usage Examples
Practical code examples demonstrating common use cases and implementation patterns