Mehmet Ergülcü

Which indexes really need a rebuild? Fragmentation on SQL Server, PostgreSQL and Oracle

· 5 min

goclipostgresqlsql-serveroracledatabase

Over time the indexes of a busy database get untidy: rows are inserted in random order, others are deleted, and the pages of the index end up half empty and out of order. Rebuilding every index every night fixes that, but it is wasteful: most of them were fine. The better question is which ones actually need it. db-index-maintainer (the binary is called indexapp) is a command line tool that answers it. It looks at the fragmentation of each index in a SQL Server, PostgreSQL or Oracle database and picks the action. The first version was a JavaFX desktop application for SQL Server only; this is the Go rewrite.

I tried it on a real PostgreSQL 18 database in Docker. Everything below is output from that session. The SQL Server and Oracle parts come from the project's documentation, and I did not run them for this post.

Install it

go install github.com/ergulcu/db-index-maintainer/cmd/indexapp@latest

The database drivers are pure Go, so one binary works on Windows, Linux and macOS without any client library.

Make a messy index

I made a table of orders with a random UUID as its primary key. Random keys are the classic way to fragment a B-tree index, because every new row lands in a different place. I inserted 400,000 rows, then deleted about 60 % of them at random, which leaves holes.

CREATE TABLE sales.orders (id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
                           customer int NOT NULL, note text NOT NULL DEFAULT 'x');
CREATE INDEX orders_customer ON sales.orders (customer);
INSERT INTO sales.orders (customer) SELECT (random()*5000)::int FROM generate_series(1, 400000);
DELETE FROM sales.orders WHERE random() < 0.6;

PostgreSQL itself can measure the fragmentation of a B-tree index, with the pgstattuple extension. Its answer for the schema is:

index            pages   leaf fragmentation
orders_customer    505   50.1 %
orders_pkey       2117   49.8 %
small_pkey           2    0.0 %

Always start with a dry run

--dry-run shows every index with the decision and the SQL it would run, and changes nothing. The connection string comes from the environment, so the password does not appear on the command line:

$ indexapp --driver postgres --dry-run
postgres: 3 indexes, 3 selected
  skip        sales.orders.orders_customer: only 505 pages (below 1000)
  rebuild     sales.orders.orders_pkey: fragmentation 49.8% (rebuild from 30%)
              REINDEX INDEX "sales"."orders_pkey";
  skip        sales.small.small_pkey: only 2 pages (below 1000)
done: 0 rebuilt, 0 reorganized, 2 skipped, 0 failed, 0 not started, 1 planned (dry run)

Three things are visible here. The tool's numbers (49.8 %) are the same as PostgreSQL's own. It decides by thresholds: below 5 % fragmentation an index is left alone, between 5 % and 30 % it is reorganized, from 30 % it is rebuilt (--reorganize-at and --rebuild-at change them). And it skips small indexes: below 1,000 pages (--min-pages) they are left alone, so orders_customer is skipped even though it is just as fragmented. Lowering the limit changes the plan:

$ indexapp --driver postgres --schema sales --min-pages 100 --dry-run
  rebuild     sales.orders.orders_customer: fragmentation 50.1% (rebuild from 30%)
  rebuild     sales.orders.orders_pkey: fragmentation 49.8% (rebuild from 30%)
  skip        sales.small.small_pkey: only 2 pages (below 100)

Do it, online

$ indexapp --driver postgres --schema sales --online
postgres: 3 indexes, 3 selected
  skip        sales.orders.orders_customer: only 505 pages (below 1000)
  rebuild     sales.orders.orders_pkey in 113ms: fragmentation 49.8% (rebuild from 30%)
  skip        sales.small.small_pkey: only 2 pages (below 1000)
done: 1 rebuilt, 0 reorganized, 2 skipped, 0 failed, 0 not started

--online keeps the table usable while the index is rebuilt. On PostgreSQL that means REINDEX INDEX CONCURRENTLY. I measured again with PostgreSQL's own function:

index            pages   leaf fragmentation
orders_customer    505   50.1 %            (skipped, as planned)
orders_pkey        617    0.0 %            (was 2117 pages, 49.8 %)

The rebuilt index went from 2,117 pages to 617 and from about 50 % fragmentation to 0. The rows are the same; they are just packed together again. A second dry run now says skip ... orders_pkey: fragmentation 0.0% (below 5%), so running the tool every night costs almost nothing on indexes that are fine.

When the tool cannot measure

On PostgreSQL the measurement needs the pgstattuple extension. I dropped it and ran the dry run again:

$ indexapp --driver postgres --schema sales --min-pages 100 --dry-run
  rebuild     sales.orders.orders_customer: fragmentation unknown
  rebuild     sales.orders.orders_pkey: fragmentation unknown

By default, an index whose fragmentation is unknown is rebuilt. Where that is too aggressive, --when-unknown skip leaves them alone:

$ indexapp --driver postgres --schema sales --min-pages 100 --when-unknown skip --dry-run
  skip        sales.orders.orders_customer: fragmentation unknown
  skip        sales.orders.orders_pkey: fragmentation unknown
done: 0 rebuilt, 0 reorganized, 3 skipped, 0 failed, 0 not started, 0 planned (dry run)

The three databases are not the same

The tool hides the differences, but you should know them. This table is from the project's documentation:

SQL Server PostgreSQL Oracle
Fragmentation sys.dm_db_index_physical_stats (LIMITED) of the current database pgstatindex leaf fragmentation, if the pgstattuple extension is installed (B-tree only; it reads the whole index); otherwise unknown unknown, because measuring would lock the table
"Clustered" (--kind clustered) clustered indexes primary key indexes primary key indexes
Rebuild ALTER INDEX ... REBUILD REINDEX INDEX ALTER INDEX ... REBUILD
Reorganize ALTER INDEX ... REORGANIZE not available: the 5–30 % range is skipped ALTER INDEX ... COALESCE
Online Enterprise or Developer edition, Azure SQL always (PostgreSQL 12 and later) Enterprise Edition

Two consequences. On Oracle the fragmentation is always unknown, so with the defaults every selected index is rebuilt; decide with --when-unknown and --kind what you want. On PostgreSQL there is no reorganize step, so an index between 5 % and 30 % is skipped.

Fitting it into a quiet night

Maintenance belongs in the small hours, and a run should not drag on into the morning:

indexapp --driver oracle --host db3 --user app --database ORCLPDB1 --password-stdin \
         --kind clustered --mode rebuild --at "2026-11-20 02:00" --window 1h < password.txt

--at starts at a local time. --window means that no new index is started after that much time has passed. --wait pauses after each index for as long as it took.

For scripts, the exit code says what happened: 0 when everything went well, 1 when an index failed or did not start, and 2 for wrong options (I tried --mode explode: indexapp: --mode must be auto, rebuild or reorganize, exit 2).

The password does not have to be on the command line: use INDEXAPP_PASSWORD, --password-stdin, or a full connection string in INDEXAPP_DSN.

Source code: github.com/ergulcu/db-index-maintainer

© 2026 Mehmet Ergülcü SvelteKit · Tailwind · <1 KB JS