postgresql-optimization

Creates and optimizes PostgreSQL queries, indexes, and schemas using JSONB, arrays, and window functions.

Updated Sep 10, 2026
One-click install
npx skills add https://github.com/serpro-workshop-fortaleza/sifap-modernization-paula --skill postgresql-optimization-serpro-workshop-fortaleza
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: postgresql-optimization
Source: https://github.com/serpro-workshop-fortaleza/sifap-modernization-paula/tree/main/.github/skills/postgresql-optimization
Command: npx skills add https://github.com/serpro-workshop-fortaleza/sifap-modernization-paula --skill postgresql-optimization-serpro-workshop-fortaleza

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Slow queries, missing indexes, and underused PostgreSQL features cause poor database performance. This Skill guides the creation and tuning of PostgreSQL queries, schemas, and indexes using the database's native capabilities, with before/after benchmarks verified through EXPLAIN ANALYZE. ## Core Features & Use Cases - Query and index optimization: Analyze execution plans with EXPLAIN (ANALYZE, BUFFERS), design composite, partial, expression, and covering indexes, and eliminate sequential scans on large tables. - Advanced PostgreSQL features: Apply JSONB operations with GIN indexes, array types, range types with exclusion constraints, geometric types, full-text search, window functions, and recursive CTEs. - Monitoring and maintenance: Use pg_stat_statements to find slow queries, detect unused indexes, and track table and index sizes. - Use Case: A report query performs a sequential scan on a large orders table. The Skill captures a baseline plan, adds a justified partial index, and confirms the latency reduction with a new EXPLAIN ANALYZE run, delivered as a Flyway forward-only migration. ## Quick Start Ask the AI to optimize a slow PostgreSQL query by analyzing its execution plan and proposing the right index or rewrite with before and after benchmarks.

Frequently Asked Questions about postgresql-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I optimize a slow PostgreSQL query?

Start by running EXPLAIN (ANALYZE, BUFFERS) to capture the baseline execution plan and latency. Look for sequential scans on large tables, then add an appropriate index or rewrite the query, and confirm the improvement with a second EXPLAIN run.

How to speed up JSONB queries in PostgreSQL?

Create a GIN index on the JSONB column and use containment operators like @> instead of casting to text with LIKE. This lets PostgreSQL use the index for key and path lookups instead of scanning every row.

What index type should I use in PostgreSQL?

Use composite B-tree indexes for multi-column filters and ordering, partial indexes for filtered queries, expression indexes for computed values, and GIN indexes for JSONB, arrays, and full-text search. Each index should be justified against its write cost.

Does PostgreSQL pagination with OFFSET perform well?

OFFSET pagination degrades on large datasets because the database scans and discards skipped rows. Keyset (cursor-based) pagination using WHERE id > last_id with ORDER BY and LIMIT performs consistently regardless of page depth.

How do I find slow queries in PostgreSQL?

Enable the pg_stat_statements extension and query it ordered by total_time to identify the most expensive statements. It reports calls, mean time, rows, and buffer hit percentages for each normalized query.

When should I not add another index in PostgreSQL?

Avoid indexes that duplicate existing ones, target rarely queried columns, or remain unused according to pg_stat_user_indexes. Every index adds write overhead and storage cost, so each one must be justified by measured query patterns.