Install any skill in seconds. Free to start, no credit card required.
Get Started Free →PostgreSQL optimization including indexes, query plans, partitioning, JSONB operations, and connection pooling
.claude/skills/bilal140202-postgres-optimization/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-19 | ✗→✓ | ▲ Improved | 4% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 98% | 0% |
| case-05 | ✓→✓ | = Same ✓ | 78% | 0% |
| case-06 | ✓→✓ | = Same ✓ | 2% | 0% |
| case-07 | ✓→✓ | = Same ✓ | -4% | 0% |
sql-- B-tree index for equality and range queries (default) CREATE INDEX idx_orders_customer_id ON orders (customer_id); -- Composite index (column order matters: equality columns first, range last) CREATE INDEX idx_orders_status_created ON orders (status, created_at DESC); -- Partial index (smaller, faster for filtered queries) CREATE INDEX idx_orders_pending ON orders (created_at) WHERE status = 'pending'; -- Covering index (avoids table lookup entirely) CREATE INDEX idx_users_email_name ON users (email) INCLUDE (name, avatar_url); -- GIN index for JSONB containment queries CREATE INDEX idx_products_metadata ON products USING GIN (metadata); -- GiST index for full-text search CREATE INDEX idx_articles_search ON articles USING GiST ( to_tsvector('english', title || ' ' || body) ); -- Concurrent index creation (no table lock) CREATE INDEX CONCURRENTLY idx_large_table_col ON large_table (col);
sqlEXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) SELECT o.id, o.total, u.name FROM orders o JOIN users u ON o.user_id = u.id WHERE o.status = 'shipped' AND o.created_at > NOW() - INTERVAL '30 days' ORDER BY o.created_at DESC LIMIT 20;
Key things to look for in the plan:
Seq Scan on large tables indicates a missing indexNested Loop with high row estimates suggests missing join indexSort without Index Scan means the sort is happening in memory/diskBuffers: shared hit vs shared read shows cache efficiencysqlCREATE TABLE events ( id BIGINT GENERATED ALWAYS AS IDENTITY, event_type TEXT NOT NULL, payload JSONB NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ) PARTITION BY RANGE (created_at); CREATE TABLE events_2024_q1 PARTITION OF events FOR VALUES FROM ('2024-01-01') TO ('2024-04-01'); CREATE TABLE events_2024_q2 PARTITION OF events FOR VALUES FROM ('2024-04-01') TO ('2024-07-01'); -- Index on each partition (inherited automatically in PG 11+) CREATE INDEX ON events (created_at, event_type);
Partition tables with more than 10M rows when queries consistently filter on the partition key.
sql-- Query nested JSONB fields SELECT * FROM products WHERE metadata @> '{"category": "electronics"}' AND (metadata ->> 'price')::numeric < 500; -- Update nested JSONB UPDATE products SET metadata = jsonb_set(metadata, '{stock}', to_jsonb(stock - 1)) WHERE id = 'abc'; -- Aggregate JSONB arrays SELECT id, jsonb_array_elements_text(metadata -> 'tags') AS tag FROM products WHERE metadata ? 'tags';
ini# pgbouncer.ini [databases] app = host=localhost port=5432 dbname=app [pgbouncer] pool_mode = transaction max_client_conn = 1000 default_pool_size = 25 min_pool_size = 5 reserve_pool_size = 5 server_idle_timeout = 300
Use transaction-level pooling for web applications. Session-level pooling for apps that use prepared statements or temp tables.
sql-- Check for slow queries SELECT query, calls, mean_exec_time, total_exec_time FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10; -- Find unused indexes SELECT indexrelname, idx_scan, pg_size_pretty(pg_relation_size(indexrelid)) FROM pg_stat_user_indexes WHERE idx_scan = 0 ORDER BY pg_relation_size(indexrelid) DESC;
SELECT * when only a few columns are neededEXPLAIN ANALYZE to verify index usageVACUUM FULL during peak hours (locks the entire table)pg_stat_statements)EXPLAIN ANALYZE run on all critical queriespg_stat_statements enabled for query performance monitoring| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→fail | 6,411 | 4,814 | -25% | 1 | 1 | 0% | 1,181 | 2,227 | +89% | 0 | 0 | — |
case-02 | fail→fail | 12,870 | 7,825 | -39% | 1 | 1 | 0% | 2,620 | 2,841 | +8% | 0 | 0 | — |
case-03 | pass→pass | 5,231 | 3,798 | -27% | 1 | 1 | 0% | 1,045 | 2,068 | +98% | 0 | 0 | — |
case-04 | fail→fail | 10,122 | 6,604 | -35% | 1 | 1 | 0% | 2,115 | 2,495 | +18% | 0 | 0 | — |
case-05 | pass→pass | 5,280 | 3,414 | -35% | 1 | 1 | 0% | 1,037 | 1,849 | +78% | 0 | 0 | — |
case-06 | pass→pass | 11,759 | 6,793 | -42% | 1 | 1 | 0% | 2,439 | 2,497 | +2% | 0 | 0 | — |
case-07 | pass→pass | 9,453 | 4,289 | -55% | 1 | 1 | 0% | 2,259 | 2,179 | -4% | 0 | 0 | — |
case-08 | pass→pass | 6,864 | 4,962 | -28% | 1 | 1 | 0% | 1,196 | 2,064 | +73% | 0 | 0 | — |
case-09 | pass→pass | 10,259 | 7,661 | -25% | 1 | 1 | 0% | 1,863 | 2,407 | +29% | 0 | 0 | — |
case-10 | pass→pass | 6,531 | 4,302 | -34% | 1 | 1 | 0% | 1,458 | 2,101 | +44% | 0 | 0 | — |
case-11 | pass→pass | 4,579 | 3,752 | -18% | 1 | 1 | 0% | 895 | 1,929 | +116% | 0 | 0 | — |
case-12 | pass→pass | 10,275 | 2,823 | -73% | 1 | 1 | 0% | 1,891 | 1,733 | -8% | 0 | 0 | — |
case-13 | pass→pass | 9,144 | 10,835 | +18% | 1 | 1 | 0% | 1,674 | 3,088 | +84% | 0 | 0 | — |
case-14 | pass→pass | 9,705 | 6,432 | -34% | 1 | 1 | 0% | 1,744 | 2,400 | +38% | 0 | 0 | — |
case-15 | pass→pass | 11,185 | 9,334 | -17% | 1 | 1 | 0% | 1,989 | 2,786 | +40% | 0 | 0 | — |
case-16 | pass→pass | 6,213 | 2,891 | -53% | 1 | 1 | 0% | 1,267 | 1,758 | +39% | 0 | 0 | — |
case-17 | pass→pass | 7,329 | 5,021 | -31% | 1 | 1 | 0% | 1,527 | 2,159 | +41% | 0 | 0 | — |
case-18 | pass→pass | 7,042 | 6,544 | -7% | 1 | 1 | 0% | 1,409 | 2,520 | +79% | 0 | 0 | — |
case-19 | fail→pass | 8,415 | 2,514 | -70% | 1 | 1 | 0% | 1,686 | 1,757 | +4% | 0 | 0 | — |
case-20 | pass→pass | 7,680 | 4,970 | -35% | 1 | 1 | 0% | 1,548 | 2,213 | +43% | 0 | 0 | — |
case-21 | pass→pass | 4,657 | 4,457 | -4% | 1 | 1 | 0% | 918 | 2,159 | +135% | 0 | 0 | — |
case-22 | pass→pass | 5,262 | 4,393 | -17% | 1 | 1 | 0% | 1,097 | 2,079 | +90% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +5 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.