
Postgresql Optimization
Install this when your agent should write or refactor PostgreSQL schemas and queries using JSONB, arrays, FTS, window functions, and extension-aware patterns—not portable lowest-common-denominator SQL
Overview
PostgreSQL Optimization is an agent skill most often used in Build (also Operate) that applies Postgres-specific types, indexes, and query patterns—including JSONB, arrays, FTS, and window functions—to improve schema des
Install
npx skills add https://github.com/github/awesome-copilot --skill postgresql-optimizationWhat is this skill?
- Deep JSONB patterns with GIN indexes, containment (@>), and path (#>>) queries
- Native array types, ANY/overlap operators, unnest, and array aggregation
- Coverage of custom types, range/geometric types, and full-text search
- Window functions and PostgreSQL extension ecosystem guidance
- Optimization mindset tied to Postgres-exclusive capabilities vs generic SQL
- Covers JSONB, array types, custom types, range/geometric types, full-text search, window functions, and extensions ecosy
Adoption & trust: 12.3k installs on skills.sh; 34.6k GitHub stars; 3/3 security scanners passed (skills.sh audits); trending (+100% hot-view momentum).
What problem does it solve?
Your agent writes portable SQL that misses Postgres strengths, leaving JSONB paths slow, array filters clumsy, and advanced features like FTS or window functions unused.
Who is it for?
Indie backend devs on a single Postgres instance who need advanced modeling (JSONB events, tags, search) without a dedicated DBA.
Skip if: Teams on MySQL/SQLite-only stacks, greenfield projects requiring zero vendor-specific SQL, or one-line typo fixes unrelated to Postgres features.
When should I use this skill?
PostgreSQL-specific development on selected SQL or entire project—JSONB, arrays, custom types, range/geometric types, full-text search, window functions, and extensions.
What do I get? / Deliverables
You get Postgres-aware DDL, indexes, and queries aligned with JSONB GIN, array ops, and extension-friendly patterns you can ship in migrations and tune later.
- Optimized DDL with appropriate indexes (e.g. GIN on JSONB)
- Postgres-specific query rewrites
- Guidance on arrays, FTS, and window-function usage
Recommended Skills
Journey fit
Spans multiple journey phases - primary shelf plus alternate fits below.
Build/backend is the canonical shelf because the skill centers on PostgreSQL-specific modeling and query patterns during application development. Backend subphase covers persistence layers, migrations, and query optimization that solo builders implement alongside APIs and services.
Where it fits
Design an events table with JSONB payloads, GIN indexes, and admin filters on nested user roles.
Model tag and category arrays on content tables with overlap queries for recommendation features.
Rewrite hot-path JSONB and array queries after EXPLAIN shows sequential scans on production traffic.
Add FTS and window-function reporting queries before launch load tests on Postgres.
How it compares
Use for PostgreSQL-native design depth instead of generic SQL assistants that ignore JSONB operators, GIN indexes, and array semantics.
Common Questions / FAQ
Who is postgresql-optimization for?
Solo builders and small teams building on PostgreSQL who want agent help with advanced types, indexing, and query patterns unique to Postgres.
When should I use postgresql-optimization?
Use it while implementing or refactoring backend schemas and queries in Build—JSONB documents, arrays, FTS, window functions—and again in Operate when iterating on slow production SQL against the same Postgres features.
Is postgresql-optimization safe to install?
Review the Security Audits panel on this page; proposed DDL and extension usage should be validated in staging and against your backup and access policies before production.
SKILL.md
READMESKILL.md - Postgresql Optimization
# PostgreSQL Development Assistant Expert PostgreSQL guidance for ${selection} (or entire project if no selection). Focus on PostgreSQL-specific features, optimization patterns, and advanced capabilities. ## � PostgreSQL-Specific Features ### JSONB Operations ```sql -- Advanced JSONB queries CREATE TABLE events ( id SERIAL PRIMARY KEY, data JSONB NOT NULL, created_at TIMESTAMPTZ DEFAULT NOW() ); -- GIN index for JSONB performance CREATE INDEX idx_events_data_gin ON events USING gin(data); -- JSONB containment and path queries SELECT * FROM events WHERE data @> '{"type": "login"}' AND data #>> '{user,role}' = 'admin'; -- JSONB aggregation SELECT jsonb_agg(data) FROM events WHERE data ? 'user_id'; ``` ### Array Operations ```sql -- PostgreSQL arrays CREATE TABLE posts ( id SERIAL PRIMARY KEY, tags TEXT[], categories INTEGER[] ); -- Array queries and operations SELECT * FROM posts WHERE 'postgresql' = ANY(tags); SELECT * FROM posts WHERE tags && ARRAY['database', 'sql']; SELECT * FROM posts WHERE array_length(tags, 1) > 3; -- Array aggregation SELECT array_agg(DISTINCT category) FROM posts, unnest(categories) as category; ``` ### Window Functions & Analytics ```sql -- Advanced window functions SELECT product_id, sale_date, amount, -- Running totals SUM(amount) OVER (PARTITION BY product_id ORDER BY sale_date) as running_total, -- Moving averages AVG(amount) OVER (PARTITION BY product_id ORDER BY sale_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) as moving_avg, -- Rankings DENSE_RANK() OVER (PARTITION BY EXTRACT(month FROM sale_date) ORDER BY amount DESC) as monthly_rank, -- Lag/Lead for comparisons LAG(amount, 1) OVER (PARTITION BY product_id ORDER BY sale_date) as prev_amount FROM sales; ``` ### Full-Text Search ```sql -- PostgreSQL full-text search CREATE TABLE documents ( id SERIAL PRIMARY KEY, title TEXT, content TEXT, search_vector tsvector ); -- Update search vector UPDATE documents SET search_vector = to_tsvector('english', title || ' ' || content); -- GIN index for search performance CREATE INDEX idx_documents_search ON documents USING gin(search_vector); -- Search queries SELECT * FROM documents WHERE search_vector @@ plainto_tsquery('english', 'postgresql database'); -- Ranking results SELECT *, ts_rank(search_vector, plainto_tsquery('postgresql')) as rank FROM documents WHERE search_vector @@ plainto_tsquery('postgresql') ORDER BY rank DESC; ``` ## � PostgreSQL Performance Tuning ### Query Optimization ```sql -- EXPLAIN ANALYZE for performance analysis EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) SELECT u.name, COUNT(o.id) as order_count FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.created_at > '2024-01-01'::date GROUP BY u.id, u.name; -- Identify slow queries from pg_stat_statements SELECT query, calls, total_time, mean_time, rows, 100.0 * shared_blks_hit / nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent FROM pg_stat_statements ORDER BY total_time DESC LIMIT 10; ``` ### Index Strategies ```sql -- Composite indexes for multi-column queries CREATE INDEX idx_orders_user_date ON orders(user_id, order_date); -- Partial indexes for filtered queries CREATE INDEX idx_active_users ON users(created_at) WHERE status = 'active'; -- Expression indexes for computed values CREATE INDEX idx_users_lower_email ON users(lower(email)); -- Covering indexes to avoid table lookups CREATE INDEX idx_orders_covering ON orders(user_id, status) INCLUDE (total, created_at); ``` ### Connection & Memory Management ```sql -- Check connection usage SELECT count(*) as