Managing Indexes¶
pGenie can analyze your queries and existing database indexes to keep your index set lean and effective. It both suggests new indexes to cover sequential scans and identifies existing indexes that are no longer used by any query and can be safely removed.
Running the Index Manager¶
From the root of your project, run:
pGenie will:
- Start a temporary PostgreSQL Docker container.
- Apply all migrations in
migrations/. - Query the database catalog to read all existing non-primary indexes.
- Run
EXPLAINon each query inqueries/to detect sequential scans. - Compare the set of existing indexes against the observed query needs.
- Print a ready-to-use SQL migration to stdout that drops redundant or unused indexes and creates missing ones.
Interpreting the Output¶
The command outputs a SQL migration combining all recommended changes. For example:
-- Auto-generated migration to optimize indexes
-- Drop redundant/excessive indexes
-- album_recording_idx on (recording) is not used by observed query needs
DROP INDEX "public"."album_recording_idx";
-- Create missing indexes
CREATE INDEX ON album (format);
CREATE INDEX ON album (name);
Each section explains what is being done and why:
- Drop redundant/excessive indexes — indexes that are not used by any observed query on the same table, exact duplicates of another index, or composite indexes whose trailing columns are never needed.
- Create missing indexes — new indexes for columns that appear in
WHEREclauses but have no covering index, causing sequential scans.
Writing the Migration to a File¶
To have pGenie write the migration directly to a numbered file in migrations/ instead of just printing it, pass --add-migration:
pGenie will determine the next available migration number (e.g. 5.sql) and write the migration there automatically. This requires that all existing migration files follow the N.sql naming convention.
Notes¶
- Index management is heuristic. pGenie reasons from the observed query patterns in your
queries/directory — it cannot know about queries issued by other clients or future access patterns. Always review the suggested migration before applying it. pgn manage-indexeson its own only prints the migration to stdout and does not modify any project files. To persist it either use--add-migrationor pipe its stdout to a file.- The command uses the same ephemeral Docker container approach as
pgn generate, so Docker must be running. - If you want to keep a redundant index for any reason (e.g. it is used by external tools), you can pass
--allow-redundant-indexesto emit warnings instead of drop statements for those cases.