db-index-maintainer
· 1 min
goclisql-serverpostgresqloracle
Rebuilds or reorganizes the indexes of a SQL Server, PostgreSQL or Oracle database, picking the action from the fragmentation of each index. A command line tool written in Go; the earlier version was a JavaFX desktop application for SQL Server only.
What is inside?
- One binary for Windows, Linux and macOS. The drivers are pure Go, so no client libraries are needed.
--dry-runlists every index with the decision and the SQL, and changes nothing. Always start there.- In
automode an index is left alone below 5 % fragmentation, reorganized between 5 and 30 %, and rebuilt above 30 %. The thresholds and a minimum page count are options. --onlinekeeps the tables usable during rebuilds where the database supports it.--atstarts at a local time and--windowstarts no new index after a given duration, so a maintenance run fits a quiet night.- The exit code is 0 when everything went well, 1 when an index failed or did not start, 2 for wrong options.
go install github.com/ergulcu/db-index-maintainer/cmd/indexapp@latest
# SQL Server: password from the environment, plan only
INDEXAPP_PASSWORD=... indexapp --driver sqlserver --host db1 --user maint --database Shop --dry-run
# PostgreSQL: online (REINDEX CONCURRENTLY), only the "sales" schema
indexapp --driver postgres --dsn "postgres://maint:secret@db2:5432/shop" --schema sales --online
Each database measures fragmentation differently: SQL Server with sys.dm_db_index_physical_stats, PostgreSQL with pgstatindex when the pgstattuple extension is installed, and Oracle does not measure it because that would lock the table, so the tool rebuilds those indexes unless told otherwise.
Source code: github.com/ergulcu/db-index-maintainer