Best for
- Creating or modifying PostgreSQL indexes
- Analyzing query plans with EXPLAIN
- Debugging slow queries or missing index usage
prowler-cloud/prowler/skills/postgresql-indexing/SKILL.md
PostgreSQL indexing best practices for Prowler: index design, partial indexes, partitioned table indexing, EXPLAIN ANALYZE validation, concurrent operations, monitoring, and maintenance. Trigger: When creating or modifying PostgreSQL indexes, analyzing query performance with EXPLAIN, debugging slow queries, reviewing index usage statistics, reindexing, dropping indexes, or working with partitioned table indexes. Also trigger when discussing index strategies, partial indexes, or index maintenance
Decision brief
PostgreSQL indexing best practices for Prowler: index design, partial indexes, partitioned table indexing, EXPLAIN ANALYZE validation, concurrent operations, monitoring, and maintenance. Trigger: When creating or modifying PostgreSQL indexes, analyzing query performance with EXPLAIN, debugging slow queries, reviewing index usage statistics, reindexing, drop…
In this controlled same-task single run, enabling postgresql-indexing changed the output from 2942 non-whitespace characters and 11 headings to 3314 characters and 12 headings. Matches among 8 signals extracted from the pinned source changed from 5 to 5. Both actual outputs are shown; this is a structural observation, not a quality score or a universal performance claim.
Create a test strategy and representative test cases for a JSON API schema comparison feature. Include failure cases and a clear verification procedure. The deliverable must specifically reflect this user intent: PostgreSQL indexing best practices for Prowler: index design, partial indexes, partitioned table indexing, EXPLAIN ANALYZE validation, concurrent operations, monitoring, and maintenance. Trigger: When creating or modifying PostgreSQL indexes, analyzing query performance with EXPLAIN, debugging slow queries, reviewing index usage statistics, reindexing, dropping indexes, or working with partitioned table indexes. Also trigger when discussing index strategies, partial indexes, or index maintenance

Baseline: 2942 non-whitespace characters, 11 headings, and 44 list items.

With Skill: 3314 non-whitespace characters, 12 headings, and 51 list items.
| Observation | Without Skill | With Skill |
|---|---|---|
| Source-signal coverage | 5/8: index, design, partial, indexes, columns | 5/8: index, design, partial, columns, where |
| Output structure | 2942 chars · 11 headings · 44 list items · 0 code blocks | 3314 chars · 12 headings · 51 list items · 0 code blocks |
| Verification and caution signals | 20 verification signals · 8 risk/limitation signals | 31 verification signals · 18 risk/limitation signals |
Use the postgresql-indexing Skill pinned at 0b9791ffdc6e for my task. Follow its source-specific constraints around `postgresql-indexing`, `index`, `design`, `partial`, then return the finished deliverable with explicit assumptions, verification, failure conditions, and limits. Do not treat the Skill text as a factual source or claim that a single demonstration proves universal performance.
Compatibility matrix
| Platform | Status | Evidence | What to check |
|---|---|---|---|
| Codex | Not declared | No explicit evidence | Portability before use |
| Claude Code | Not declared | No explicit evidence | Portability before use |
| Cursor | Not declared | No explicit evidence | Portability before use |
| Gemini CLI | Not declared | No explicit evidence | Portability before use |
Installation
The source command is displayed only when detected. A safe inspection prompt is always available so your agent can explain every action before execution.
npx skills add https://github.com/prowler-cloud/prowler --skill "skills/postgresql-indexing"Inspect the Agent Skill "postgresql-indexing" from https://github.com/prowler-cloud/prowler/blob/9898cf7364ec22888305a8a14ffdc6cbbb55a6f0/skills/postgresql-indexing/SKILL.md at commit 9898cf7364ec22888305a8a14ffdc6cbbb55a6f0. List every install step, command, network request, credential, file read/write, external action, and rollback step. Explain whether it fits my task. Do not install or execute anything until I approve.
Workflow
The planner may choose a sequential scan on small datasets. Toggle enableseqscan = off to confirm the index path works, then re-enable it.
Query pgstatallindexes to find indexes that are never or rarely scanned:
Creating or modifying PostgreSQL indexes
When a column has a fixed value for the query (e.g., state = 'completed'), put it in the WHERE clause of the index, not in the indexed columns. Otherwise the planner cannot exploit the ordering of the other columns.
When a column has a fixed value for the query (e.g., state = 'completed'), put it in the WHERE clause of the index, not in the indexed columns. Otherwise the planner cannot exploit the ordering of the other columns.
Permission review
No configured static risk pattern was detected
This is not proof of safety. Runtime behavior, indirect dependencies, and hidden external systems are outside the static scan.
Evidence record
| Signal | Value | Evidence type | Meaning |
|---|---|---|---|
| Quality score | 100/100 | Computed | Documentation, specificity, maintenance, and trust rules |
| Repository stars | 14,671 | Source | Repository attention, not individual Skill quality |
| Compatibility | 0 platforms | Source | Declared in the catalog source record |
| Usage guide | tested outcome page | Tested | Generated or reviewed according to the visible evidence level |
Pinned source
EXPLAINWhen a column has a fixed value for the query (e.g., state = 'completed'), put it in the WHERE clause of the index, not in the indexed columns. Otherwise the planner cannot exploit the ordering of the other columns.
-- Bad: state in the key wastes space and breaks ordering
CREATE INDEX idx_scans_tenant_state ON scans (tenant_id, state, inserted_at DESC);
-- Good: state as a filter, planner uses tenant_id + inserted_at ordering
CREATE INDEX idx_scans_tenant_ins_completed ON scans (tenant_id, inserted_at DESC)
WHERE state = 'completed';
Put high-selectivity columns first (columns that filter out the most rows). For composite indexes, the leftmost column must appear in the query's WHERE clause for the index to be used.
Never assume an index is being used. Run EXPLAIN (ANALYZE, BUFFERS) to confirm.
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM users
WHERE email = '[email protected]';
Use Postgres EXPLAIN Visualizer (pev) to visualize query plans and identify bottlenecks.
The planner may choose a sequential scan on small datasets. Toggle enable_seqscan = off to confirm the index path works, then re-enable it.
SET enable_seqscan = off;
EXPLAIN (ANALYZE, BUFFERS)
SELECT DISTINCT ON (provider_id) provider_id
FROM scans
WHERE tenant_id = '95383b24-da01-44b5-a713-0d9920d554db'
AND state = 'completed'
ORDER BY provider_id, inserted_at DESC;
SET enable_seqscan = on; -- always re-enable after testing
This is for validation only. Never leave enable_seqscan = off in production.
Every extra index has three costs that compound:
Write overhead. Every INSERT and UPDATE must maintain all indexes. Extra indexes also kill HOT (Heap-Only-Tuple) updates, which normally skip index maintenance when unindexed columns change.
Planning time. The planner evaluates more execution paths per index. On simple OLTP queries, planning time can exceed execution time by 4x when index count is high.
Lock contention (fastpath limit). PostgreSQL uses a fast path for the first 16 locks per backend. After 16 relations (table + its indexes), it falls back to slower LWLock mechanisms. At high QPS (100+), this causes LockManager wait events.
Rules:
Two indexes are redundant when:
(a) is redundant to (a, b), but NOT to (b, a)Column order matters. For partial indexes, the WHERE clause must also match.
-- Quick check: find indexes that share a leading column on the same table
SELECT
a.indrelid::regclass AS table_name,
a.indexrelid::regclass AS index_a,
b.indexrelid::regclass AS index_b,
pg_size_pretty(pg_relation_size(a.indexrelid)) AS size_a,
pg_size_pretty(pg_relation_size(b.indexrelid)) AS size_b
FROM pg_index a
JOIN pg_index b ON a.indrelid = b.indrelid
AND a.indexrelid != b.indexrelid
AND a.indkey::text = (
SELECT string_agg(x::text, ' ')
FROM unnest(b.indkey[:array_length(a.indkey, 1)]) AS x
)
WHERE NOT a.indisunique;
Before dropping: verify on all workload nodes (primary + replicas), use DROP INDEX CONCURRENTLY, and monitor for plan regressions.
Query pg_stat_all_indexes to find indexes that are never or rarely scanned:
SELECT
idxstat.schemaname AS schema_name,
idxstat.relname AS table_name,
idxstat.indexrelname AS index_name,
idxstat.idx_scan AS index_scans_count,
idxstat.last_idx_scan AS last_idx_scan_timestamp,
pg_size_pretty(pg_relation_size(idxstat.indexrelid)) AS index_size
FROM pg_stat_all_indexes AS idxstat
JOIN pg_index i ON idxstat.indexrelid = i.indexrelid
WHERE idxstat.schemaname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
AND NOT i.indisunique
ORDER BY idxstat.idx_scan ASC, idxstat.last_idx_scan ASC;
Indexes with idx_scan = 0 and no recent last_idx_scan are candidates for removal.
Before dropping, verify:
stats_reset in pg_stat_database)-- Check when stats were last reset
SELECT stats_reset, age(now(), stats_reset)
FROM pg_stat_database
WHERE datname = current_database();
Do not assume index creation succeeded. Use pg_stat_progress_create_index (Postgres 12+) to watch progress live:
SELECT * FROM pg_stat_progress_create_index;
In psql, use \watch 5 to refresh every 5 seconds for a live dashboard view. CREATE INDEX CONCURRENTLY and REINDEX CONCURRENTLY have more phases than standard operations: monitor for blocking sessions and wait events.
Check for invalid indexes regularly:
SELECT c.relname AS index_name, i.indisvalid
FROM pg_class c
JOIN pg_index i ON i.indexrelid = c.oid
WHERE i.indisvalid = false;
Invalid indexes are ignored by the planner. They waste space and cause inconsistent query performance, especially on partitioned tables where some partitions may have valid indexes and others do not.
Never create or drop indexes without CONCURRENTLY on live tables. Without it, the operation holds a lock that blocks all writes.
-- Create
CREATE INDEX CONCURRENTLY IF NOT EXISTS index_name ON table_name (column_name);
-- Drop
DROP INDEX CONCURRENTLY IF EXISTS index_name;
DROP INDEX CONCURRENTLY cannot run inside a transaction block.
Makes scripts idempotent. Safe to re-run without errors from duplicate or missing indexes.
CREATE INDEX CONCURRENTLY can fail without raising an error. The result is an invalid index that the planner ignores. This is particularly dangerous on partitioned tables: some partitions get valid indexes, others don't, causing inconsistent query performance.
After any concurrent index creation, always validate:
SELECT c.relname, i.indisvalid
FROM pg_class c
JOIN pg_index i ON i.indexrelid = c.oid
WHERE c.relname LIKE '%your_index_name%';
Rebuild invalid indexes without locking writes:
REINDEX INDEX CONCURRENTLY index_name;
When CREATE INDEX CONCURRENTLY or REINDEX INDEX CONCURRENTLY is interrupted, temporary indexes may remain:
| Suffix | Meaning | Action |
|---|---|---|
_ccnew | New index being built, incomplete | Drop it and retry REINDEX CONCURRENTLY |
_ccold | Old index being replaced, rebuild succeeded | Safe to drop |
-- Example: both original and temp are invalid
-- users_emails_2019 btree (col) INVALID
-- users_emails_2019_ccnew btree (col) INVALID
-- Drop the failed new one, then retry
DROP INDEX CONCURRENTLY IF EXISTS users_emails_2019_ccnew;
REINDEX INDEX CONCURRENTLY users_emails_2019;
These leftovers clutter the schema, confuse developers, and waste disk space. Clean them up.
As stated in PostgreSQL documentation, ALTER INDEX ... ATTACH PARTITION prevents dropping malfunctioning or non-performant indexes from individual partitions. An attached index cannot be dropped by itself and is automatically dropped if its parent index is dropped.
This removes the ability to manage indexes per-partition, which we need for:
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_child_partition
ON child_partition (column_name);
CREATE INDEX IF NOT EXISTS idx_parent
ON parent_table (column_name);
PostgreSQL will automatically recognize partition-level indexes as part of the parent index definition when the index names and definitions match.
For time-based partitions (findings uses monthly partitions):
all_partitions=False default in create_index_on_partitions handles this automaticallyOver time, B-tree indexes accumulate bloat from updates and deletes. VACUUM reclaims heap space but does NOT rebalance B-tree pages. Periodic reindexing is necessary for heavily updated tables.
Indexes with estimated bloat above 50% are candidates for REINDEX CONCURRENTLY. Check bloat with tools like pgstattuple or bloat estimation queries.
Three things slow degradation:
If you rebuild two indexes on the same table in parallel, PostgreSQL detects a deadlock and kills one session. To rebuild many indexes across multiple sessions safely, assign all indexes for a given table to the same session:
\set NUMBER_OF_SESSIONS 10
SELECT
format('%I.%I', n.nspname, c.relname) AS table_fqn,
format('%I.%I', n.nspname, i.relname) AS index_fqn,
mod(
hashtext(format('%I.%I', n.nspname, c.relname)) & 2147483647,
:NUMBER_OF_SESSIONS
) AS session_id
FROM pg_index idx
JOIN pg_class c ON idx.indrelid = c.oid
JOIN pg_class i ON idx.indexrelid = i.oid
JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE n.nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema')
ORDER BY table_fqn, index_fqn;
Then run each session's indexes in a separate REINDEX INDEX CONCURRENTLY call. Set NUMBER_OF_SESSIONS based on max_parallel_maintenance_workers and available I/O.
After dropping an index, run VACUUM and ANALYZE to reclaim space and update planner statistics:
-- Full vacuum + analyze (can be heavy on large tables)
VACUUM (ANALYZE) your_table;
-- Lightweight alternative for huge tables: just update statistics
ANALYZE your_table;
-- Validate query uses an index
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;
-- Check index creation progress
SELECT * FROM pg_stat_progress_create_index;
-- Find invalid indexes
SELECT c.relname, i.indisvalid
FROM pg_class c JOIN pg_index i ON i.indexrelid = c.oid
WHERE i.indisvalid = false;
-- Find unused indexes
SELECT relname, indexrelname, idx_scan, pg_size_pretty(pg_relation_size(indexrelid))
FROM pg_stat_all_indexes
WHERE schemaname = 'public' AND idx_scan = 0;
-- Create index safely
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_name ON table (columns);
-- Drop index safely
DROP INDEX CONCURRENTLY IF EXISTS idx_name;
-- Rebuild invalid index
REINDEX INDEX CONCURRENTLY idx_name;
-- Post-drop maintenance
VACUUM (ANALYZE) table_name;
Prerequisite: Install Context7 MCP server for up-to-date documentation lookup.
| Library | Context7 ID | Use for |
|---|---|---|
| PostgreSQL | /websites/postgresql_org_docs_current | Index types, EXPLAIN, partitioned table indexing, REINDEX |
Example queries:
mcp_context7_query-docs(libraryId="/websites/postgresql_org_docs_current", query="CREATE INDEX CONCURRENTLY partitioned table")
mcp_context7_query-docs(libraryId="/websites/postgresql_org_docs_current", query="EXPLAIN ANALYZE BUFFERS query plan")
mcp_context7_query-docs(libraryId="/websites/postgresql_org_docs_current", query="partial index WHERE clause")
mcp_context7_query-docs(libraryId="/websites/postgresql_org_docs_current", query="REINDEX CONCURRENTLY invalid index")
mcp_context7_query-docs(libraryId="/websites/postgresql_org_docs_current", query="pg_stat_all_indexes monitoring")
Note: Use
mcp_context7_resolve-library-idfirst if you need to find the correct library ID.
Frequently asked questions
PostgreSQL indexing best practices for Prowler: index design, partial indexes, partitioned table indexing, EXPLAIN ANALYZE validation, concurrent operations, monitoring, and maintenance. Trigger: When creating or modifying PostgreSQL indexes, analyzing query performance with EXPLAIN, debugging slow queries, reviewing index usage statistics, reindexing, drop…
The source record exposes this install command: npx skills add https://github.com/prowler-cloud/prowler --skill "skills/postgresql-indexing". Inspect the command and pinned source before running it.
Alternatives
yonatangross/orchestkit
Grade work that already exists and decide whether it can merge. Runs the project's current unit, integration, and E2E suites plus security scanning and type checking, scores every dimension 0-10, and returns a merge verdict with a VERIFIED-vs-CLAIMED evidence manifest. Writes no test files and edits no source. Use when verifying changes are ready to merge. Use /ork:cover instead when the tests still have to be written.
microsoft/Sico
Execute Android UI workflows on a sandbox device, review results, and produce a structured execution report.
laurigates/claude-plugins
Analyze test results and create a fix plan with subagents. Use when triaging failing tests, analyzing JUnit XML, planning fixes for accessibility/security, or categorizing flaky/E2E failures.
mgiovani/cc-arsenal
Multi-agent review team: architecture, security, performance, testing, style, docs/UX, plus an adversary that cross-examines the other 6, for security-sensitive, architectural, or large PRs (15+ files) where a single-agent pass risks missing cross-cutting issues. Use for auth/payments/PII changes, schema/pattern changes, compliance sign-off, or when asked to 'get the review team on this' / 'multi-agent review' / 'thorough review before merge'. For a standard PR or a quick pre-merge check, use /r