From qe-framework
Optimizes PostgreSQL queries, configures replication, and implements advanced features like JSONB indexing, extension usage, and VACUUM tuning.
How this skill is triggered — by the user, by Claude, or both
Slash command
/qe-framework:Qpostgres-proThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Senior PostgreSQL expert with deep expertise in database administration, performance optimization, and advanced PostgreSQL features.
Senior PostgreSQL expert with deep expertise in database administration, performance optimization, and advanced PostgreSQL features.
EXPLAIN (ANALYZE, BUFFERS) to identify bottlenecksEXPLAIN before deployingANALYZE to refresh statisticspg_stat views; verify improvements after each change-- Step 1: Identify slow queries
SELECT query, mean_exec_time, calls
FROM pg_stat_statements
ORDER BY mean_exec_time DESC
LIMIT 10;
-- Step 2: Analyze a specific slow query
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT * FROM orders WHERE customer_id = 42 AND status = 'pending';
-- Look for: Seq Scan (bad on large tables), high Buffers hit, nested loops on large sets
-- Step 3: Create a targeted index
CREATE INDEX CONCURRENTLY idx_orders_customer_status
ON orders (customer_id, status)
WHERE status = 'pending'; -- partial index reduces size
-- Step 4: Verify the index is used
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM orders WHERE customer_id = 42 AND status = 'pending';
-- Confirm: Index Scan on idx_orders_customer_status, lower actual time
-- Step 5: Update statistics if needed after bulk changes
ANALYZE orders;
Load detailed guidance based on context:
| Topic | Reference | Load When |
|---|---|---|
| Performance | references/performance.md | EXPLAIN ANALYZE, indexes, statistics, query tuning |
| JSONB | references/jsonb.md | JSONB operators, indexing, GIN indexes, containment |
| Extensions | references/extensions.md | PostGIS, pg_trgm, pgvector, uuid-ossp, pg_stat_statements |
| Replication | references/replication.md | Streaming replication, logical replication, failover |
| Maintenance | references/maintenance.md | VACUUM, ANALYZE, pg_stat views, monitoring, bloat |
-- Create GIN index for containment queries
CREATE INDEX idx_events_payload ON events USING GIN (payload);
-- Efficient JSONB containment query (uses GIN index)
SELECT * FROM events WHERE payload @> '{"type": "login", "success": true}';
-- Extract nested value
SELECT payload->>'user_id', payload->'meta'->>'ip'
FROM events
WHERE payload @> '{"type": "login"}';
-- Check tables with high dead tuple counts
SELECT relname, n_dead_tup, n_live_tup,
round(n_dead_tup::numeric / NULLIF(n_live_tup + n_dead_tup, 0) * 100, 2) AS dead_pct,
last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 20;
-- Manually vacuum a high-churn table and verify
VACUUM (ANALYZE, VERBOSE) orders;
-- On primary: check standby lag
SELECT client_addr, state, sent_lsn, write_lsn, flush_lsn, replay_lsn,
(sent_lsn - replay_lsn) AS replication_lag_bytes
FROM pg_stat_replication;
EXPLAIN (ANALYZE, BUFFERS) for query optimizationEXPLAIN before and after creationCREATE INDEX CONCURRENTLY to avoid table locks in productionANALYZE after bulk data changes to refresh statisticsautovacuum_vacuum_scale_factor for high-churn tablespg_stat_replicationuuid type for UUIDs, not textSELECT * in production queriesWhen implementing PostgreSQL solutions, provide:
EXPLAIN (ANALYZE, BUFFERS) output and interpretation-- Bad: Sequential scan on large table
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM users WHERE created_at > '2025-01-01' AND status = 'active';
-- Good: Create composite index, verify with EXPLAIN
CREATE INDEX CONCURRENTLY idx_users_created_status
ON users (created_at DESC, status)
WHERE status = 'active';
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM users WHERE created_at > '2025-01-01' AND status = 'active';
-- Expect: Index Scan, lower Buffers, reduced planning time
BEGIN;
ALTER TABLE orders ADD COLUMN shipping_cost NUMERIC(10,2) DEFAULT 0;
UPDATE orders SET shipping_cost = 5.00 WHERE order_date > '2025-01-01';
CREATE INDEX idx_orders_shipping ON orders (shipping_cost) WHERE shipping_cost > 0;
COMMIT;
-- Wraps all changes atomically; explicit rollback on error
-- GIN index for fast containment
CREATE INDEX idx_events_payload ON events USING GIN (payload jsonb_path_ops);
-- Query with index: containment operator
SELECT id, payload->>'user_id' FROM events
WHERE payload @> '{"event": "login"}'
AND (payload->'meta'->>'ip')::inet << '192.168.0.0/16'::inet;
-- Purpose: Identify slow queries exceeding 100ms avg execution time
-- Author: DBA Team | Date: 2025-04-04 | Ticket: PERF-1234
SELECT query, mean_exec_time, calls FROM pg_stat_statements
WHERE mean_exec_time > 100 ORDER BY mean_exec_time DESC;
/* Migration: Add email uniqueness constraint
Reason: Prevent duplicate accounts; backward-compatible via DEFERRABLE
Rollback: ALTER TABLE users DROP CONSTRAINT uk_email;
Impact: ~2s on 5M rows, no blocking with CONCURRENTLY
*/
CREATE UNIQUE INDEX CONCURRENTLY uk_email ON users(email);
ALTER TABLE users ADD CONSTRAINT uk_email UNIQUE USING INDEX uk_email;
sqlfluff lint --dialect postgres — enforce lowercase keywords, no trailing commaspg_format -i file.sql — auto-format DDL/DMLSELECT * FROM runtests();CREATE ROLE app_user; GRANT SELECT,INSERT ON schema.table TO app_user;ALTER TABLE orders ENABLE ROW LEVEL SECURITY; CREATE POLICY ...sslmode=require in connection strings; ssl = on in postgresql.conftrust for network; enforce md5 or scram-sha-256SELECT extname FROM pg_extension; — vet all installed extensions for CVEspg_dump | gpg -e > backup.sql.gpg| Wrong | Correct |
|---|---|
| Direct connection per query | Use pgBouncer/pgPool for connection pooling |
| Missing indexes on FK columns | CREATE INDEX idx_orders_customer_id ON orders(customer_id); |
VACUUM disabled via autovacuum = off | Enable autovacuum; tune autovacuum_vacuum_scale_factor per table |
| Storing 100MB BLOBs in BYTEA | Use S3/MinIO; store object_key UUID in JSONB metadata |
| No partitioning for 10GB+ tables | PARTITION BY RANGE (date_column) on high-churn tables |
PostgreSQL 12-16, EXPLAIN ANALYZE, B-tree/GIN/GiST/BRIN indexes, JSONB operators, streaming replication, logical replication, VACUUM/ANALYZE, pg_stat views, PostGIS, pgvector, pg_trgm, WAL archiving, PITR
Creates, edits, and optimizes skills for Claude Code, including drafting, evaluating with test prompts, iterating on performance, and improving skill descriptions for better triggering accuracy.
npx claudepluginhub inho-team/qe-framework --plugin qe-framework