database/optimizing-performance/SKILL.md
Diagnose and fix PostgreSQL performance issues using EXPLAIN ANALYZE, indexes, connection pooling, and query optimization.
npx skillsauth add 7a336e6e/skills Optimizing PerformanceInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Identify and resolve PostgreSQL query performance bottlenecks through systematic analysis with EXPLAIN ANALYZE, strategic index creation, connection pooling configuration, and query rewriting.
Always start by understanding what PostgreSQL is actually doing. Use EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) to get the execution plan with real timing data.
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT u.display_name, COUNT(t.id) AS task_count
FROM users u
JOIN task_assignments ta ON ta.user_id = u.id
JOIN tasks t ON t.id = ta.task_id
WHERE t.status = 'completed'
GROUP BY u.display_name
ORDER BY task_count DESC
LIMIT 10;
Look for these warning signs in the plan output:
Example plan showing a problem:
Seq Scan on tasks (cost=0.00..15432.00 rows=5023 width=18)
Filter: ((status)::text = 'completed'::text)
Rows Removed by Filter: 195000
Buffers: shared read=8320
This sequential scan reads 200k rows to return 5k. An index on status would fix this.
Choose the index type based on the query pattern. Refer to the index strategy reference for detailed guidance.
-- Fix the seq scan above with a B-tree index
CREATE INDEX ix_tasks_status ON tasks (status);
-- For a query filtering on status AND ordering by created_at
CREATE INDEX ix_tasks_status_created_at ON tasks (status, created_at DESC);
-- Partial index for a common filtered query
CREATE INDEX ix_tasks_active ON tasks (status, created_at)
WHERE status != 'archived';
After adding the index, re-run EXPLAIN ANALYZE to confirm improvement:
Index Scan using ix_tasks_status on tasks (cost=0.42..1523.15 rows=5023 width=18)
Index Cond: ((status)::text = 'completed'::text)
Buffers: shared hit=312
Apply these rules to every query:
SELECT *WHERE clauses rather than filtering in application codeLIMIT for paginated resultsEXISTS instead of IN for subqueries on large sets-- Bad: function on indexed column prevents index use
SELECT * FROM users WHERE LOWER(email) = '[email protected]';
-- Good: use a functional index or store normalized data
CREATE INDEX ix_users_email_lower ON users (LOWER(email));
SELECT * FROM users WHERE LOWER(email) = '[email protected]';
Avoid opening a new database connection per request. Use connection pooling to reuse connections.
from sqlalchemy import create_engine
engine = create_engine(
"postgresql://user:pass@localhost/mydb",
pool_size=10, # Number of persistent connections
max_overflow=20, # Additional connections under load
pool_timeout=30, # Seconds to wait for a connection
pool_recycle=1800, # Recycle connections after 30 minutes
pool_pre_ping=True, # Verify connection health before use
)
For high-traffic applications, consider pgBouncer in front of PostgreSQL with transaction-level pooling.
Enable the slow query log and review it regularly:
-- In postgresql.conf
-- log_min_duration_statement = 100 -- Log queries over 100ms
Use pg_stat_statements to find the most time-consuming queries:
SELECT query, calls, mean_exec_time, total_exec_time
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 20;
Produce a performance analysis report containing: the original query, the EXPLAIN ANALYZE output before optimization, the changes made (new indexes, rewritten query, configuration), and the EXPLAIN ANALYZE output after optimization with measured improvement.
development
Implement features using the Red-Green-Refactor cycle to ensure testability and correctness from the start.
data-ai
Manage the `tasks.md` ledger with strict locking and collision avoidance protocols to allow multiple agents to work in parallel safely.
development
The git-workflow skill defines branching conventions, commit message formats, and pull request standards that all agents must follow for consistent version control.
development
The environment-config skill standardizes how agents manage environment variables, secrets, and application configuration across local development and deployed environments.