pg-index-health-sql is a collection of SQL queries for analyzing PostgreSQL schemas, detecting issues and promoting best practices.
pg-index-health-sql provides a curated set of raw SQL queries designed to identify common schema issues in PostgreSQL databases — such as redundant indexes, missing primary keys or inefficient table structures.
It enables lightweight, database-native analysis without requiring external libraries or frameworks, making it suitable for direct use in psql sessions, scripts, or monitoring dashboards.
Compatibility with PostgreSQL versions 10, 11 and 12 is no longer guaranteed, but it is very likely.
We focus only on the currently maintained versions of PostgreSQL.
For more information please see PostgreSQL Versioning Policy.
pg-index-health-sql allows you to detect the following problems:
- Invalid (broken) indexes (sql).
- Duplicated (completely identical) indexes (sql).
- Intersected (partially identical) indexes (sql).
- Unused indexes (sql).
- Foreign keys without associated indexes (sql).
- Indexes with null values (sql).
- Tables with missing indexes (sql).
- Tables without primary key (sql).
- Indexes bloat (sql).
- Tables bloat (sql).
- Tables without description (sql).
- Columns without description (sql).
- Columns with json type (sql).
- Columns of serial types that are not primary keys (sql).
- Functions without description (sql).
- Indexes with boolean (sql).
- Tables with not valid constraints (sql).
- B-tree indexes on array columns (sql).
- Sequence overflow (sql).
- Primary keys with serial types (sql).
- Duplicated (completely identical) foreign keys (sql).
- Intersected (partially identical) foreign keys (sql).
- Objects with possible name overflow (sql).
- Tables not linked to other tables (sql).
- Foreign keys with unmatched column type (sql).
- Tables with zero or one column (sql).
- Objects whose names do not follow naming convention (sql).
- Columns whose names do not follow naming convention (sql).
- Primary keys with varchar columns instead of uuids (sql).
- Columns with varchar(n) type (sql).
- Indexes with unnecessary where-clause on not null column (sql).
- Primary keys that are most likely natural keys (sql).
- Columns with money type (sql).
- Indexes with a timestamp in the middle (sql).
To run super-linter locally:
docker run \
-e RUN_LOCAL=true \
-e USE_FIND_ALGORITHM=true \
-e VALIDATE_SQLFLUFF=true \
-v $(pwd):/tmp/lint \
ghcr.io/super-linter/super-linter:slim-v7.4.0
Use cmd
on Windows:
docker run ^
-e RUN_LOCAL=true ^
-e USE_FIND_ALGORITHM=true ^
-e VALIDATE_SQLFLUFF=true ^
-v "%cd%":/tmp/lint ^
ghcr.io/super-linter/super-linter:slim-v7.4.0
See https://github.com/super-linter/super-linter/blob/main/dependencies/python/sqlfluff.txt
docker run --rm ^
-v "%cd%\.github\linters\.sqlfluff":/sql/.sqlfluff:ro ^
-v "%cd%":/sql ^
-e SQLFLUFF_CONFIG=/sql/.sqlfluff ^
sqlfluff/sqlfluff:3.4.0 lint /sql