mfvanek/pg-index-health
GitHub: mfvanek/pg-index-health
pg-index-health 是一个可嵌入 CI/CD 的 PostgreSQL schema 静态分析库,用于自动检测数据库结构反模式并推广最佳实践。
Stars: 199 | Forks: 22
# 
**pg-index-health** 是一个用于 PostgreSQL 的可嵌入式 schema linter,能够检测常见的反模式并推广最佳实践。
[](https://github.com/mfvanek/pg-index-health/actions/workflows/tests.yml "Java CI")
[](https://search.maven.org/artifact/io.github.mfvanek/pg-index-health/ "Maven Central")
[](https://github.com/mfvanek/pg-index-health/blob/master/LICENSE "Apache License 2.0")
[](https://javadoc.io/doc/io.github.mfvanek/pg-index-health "javadoc")
[](https://codecov.io/gh/mfvanek/pg-index-health)
[](https://sonarcloud.io/summary/new_code?id=mfvanek_pg-index-health)
[](https://sonarcloud.io/summary/new_code?id=mfvanek_pg-index-health)
[](https://sonarcloud.io/summary/new_code?id=mfvanek_pg-index-health)
[](https://sonarcloud.io/summary/new_code?id=mfvanek_pg-index-health)
[](https://sonarcloud.io/summary/new_code?id=mfvanek_pg-index-health)
[](https://dashboard.stryker-mutator.io/reports/github.com/mfvanek/pg-index-health/master)
## 这是什么?
**pg-index-health** 是一个 Java 库,旨在分析 PostgreSQL 数据库的 schema,并帮助开发者构建高效、可靠的应用程序。
它会扫描数据库结构,以识别那些往往在引发生产环境问题之前容易被忽视的常见 schema 级别隐患。
通过将 **pg-index-health** 嵌入到您的 CI/CD pipeline 中,您可以主动捕获 schema 设计缺陷、强制执行一致性,并在其影响性能或可靠性之前遵守 PostgreSQL 的最佳实践。
## 可用的检查
所有可用的检查(规则/诊断)列表可以在[这里](doc/available_checks.md)找到。
在代码中,您可以通过 [StandardChecksOnHost](pg-index-health-core/src/main/java/io/github/mfvanek/pg/core/checks/host/StandardChecksOnHost.java) 或 [StandardChecksOnCluster](pg-index-health/src/main/java/io/github/mfvanek/pg/health/checks/cluster/StandardChecksOnCluster.java) 类的实例获取所有检查。
### 用户自定义(定制)检查
您可以[如链接所示](doc/custom_checks.md)添加自己的数据库结构检查。
## 安装说明
使用 Gradle:
```
implementation("io.github.mfvanek:pg-index-health:0.41.1")
```
使用 Maven:
```
io.github.mfvanek
pg-index-health
0.41.1
```
## 如何使用
在您的项目中使用 **pg-index-health** 主要有两种场景:
* 在本地和 CI 中进行单元/功能测试(参见下方的**标准测试**),以确保数据库结构符合最佳实践;
* 在生产环境的 runtime 中,收集索引、表和序列的健康数据并进行膨胀(bloat)监控。
所有这些情况都在 [pg-index-health-demo](https://github.com/mfvanek/pg-index-health-demo) 项目中提供了示例。
## 与 Spring Boot 集成
还有一个用于单元/集成测试的 Spring Boot starter [pg-index-health-test-starter](spring-boot-integration%2Fpg-index-health-test-starter)。
您可以在 [pg-index-health-demo](https://github.com/mfvanek/pg-index-health-demo) 项目中找到更多示例。
### Starter 安装
使用 Gradle:
```
testImplementation("io.github.mfvanek:pg-index-health-test-starter:0.41.1")
```
使用 Maven:
```
io.github.mfvanek
pg-index-health-test-starter
0.41.1
test
```
### 标准测试
[如链接所示](doc/standard_test.md)在您的项目中添加一个标准测试。理想情况下,所有检查都应通过并返回空结果。
### Spring Boot 兼容性
| Spring Boot | 最低 JDK 版本 | pg-index-health-test-starter |
|-------------|-------------|------------------------------|
| 2.7.x | 11 | 0.8.x — 0.15.x |
| 3.3.x | 17 | 0.20.x |
| 3.4.x | 17 | 0.30.x — 0.31.x |
| 3.5.x | 17 | 0.40.x — 0.41.x |
#### Spring Boot 3 兼容性
* 从 [0.9.0.1](https://github.com/mfvanek/pg-index-health-test-starter/releases/tag/v.0.9.0.1) 开始
添加了对 [Spring Boot 3.0](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#auto-configuration-files) 的支持
* 从 [0.10.2](https://github.com/mfvanek/pg-index-health-test-starter/releases/tag/v.0.10.2) 开始
添加了对 [Spring Boot 3.2](https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-6.x#parameter-name-retention) 的支持
#### Spring Boot 4 兼容性
* 从 [0.30.2](https://github.com/mfvanek/pg-index-health/releases/tag/v.0.30.2) 开始
添加了对 [Spring Boot 4.0](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-4.0-Migration-Guide) 的支持
## 支持的 PostgreSQL 版本
[](https://www.postgresql.org/about/news/postgresql-14-released-2318/ "PostgreSQL 14")
[](https://www.postgresql.org/about/news/postgresql-15-released-2526/ "PostgreSQL 15")
[](https://www.postgresql.org/about/news/postgresql-16-released-2715/ "PostgreSQL 16")
[](https://www.postgresql.org/about/news/postgresql-17-released-2936/ "PostgreSQL 17")
[](https://www.postgresql.org/about/news/postgresql-18-released-3142/ "PostgreSQL 18")
### 对先前版本的 PostgreSQL 的支持
不再保证与先前版本的 PostgreSQL 兼容,但极有可能是兼容的。
我们仅关注当前处于维护阶段的 PostgreSQL 版本。
有关更多信息,请参阅 [PostgreSQL 版本控制策略](https://www.postgresql.org/support/versioning/)。
## 支持的 Java 版本
支持 [Java 17](https://www.java.com/en/) 及以上版本。
- 对于兼容 **Java 11** 的版本,请查看 [0.15.0](https://github.com/mfvanek/pg-index-health/releases/tag/v.0.15.0) 及更低版本。
## 文章与出版物
### 英文
* [pg-index-health – 针对你 PostgreSQL 数据库的静态分析工具](https://dev.to/mfvanek/pg-index-health-a-static-analysis-tool-for-you-postgresql-database-2no5)
### 俄文
* [pg-index-health – PostgreSQL 数据库结构的静态分析](https://habr.com/ru/articles/871546/)
* [Java 开发者眼中的 PostgreSQL 索引健康](https://habr.com/ru/post/490824/)
* [DBA:查找无用的索引](https://habr.com/ru/companies/tensor/articles/488104/)
* [“数据库结构静态分析”系列文章](https://habr.com/ru/articles/800121/)
## 问题、议题、功能请求与贡献
* 如果您对该库有任何疑问或问题,请[提交一个 issue](https://github.com/mfvanek/pg-index-health/issues)。
* 随时欢迎您的贡献!有关更多详细信息,请参阅[贡献指南](CONTRIBUTING.md)。
* 我们使用 [Testcontainers](https://www.testcontainers.org/) 来测试 **pg-index-health**。
因此,您的机器上需要安装 [Docker](https://www.docker.com/)。
## 类似的解决方案
- [PostgreSQL Wiki - Don't Do This](https://wiki.postgresql.org/wiki/Don%27t_Do_This) — 一个简短的常见错误列表。
- [db_verifier](https://github.com/sdblist/db_verifier) — 用于检查数据库结构错误或非推荐做法的脚本。
- [SchemaCrawler Linter](https://www.schemacrawler.com/lint.html) — 一个免费的数据库 schema linter。
- [Squawk](https://github.com/sbdchd/squawk) — 一个针对 Postgres 迁移的 linter。
- [schemalint](https://github.com/kristiandupont/schemalint) — 一个针对 Postgres 数据库 schema 的 linter。
- [index-digest](https://github.com/macbre/index-digest) — 一个针对 MySQL 和 MariaDB 的数据库 linter。
- [Azimutt](https://azimutt.app/features/analysis) — 一个针对数据库的 linter。
标签:JS文件枚举, LNA, PostgreSQL, 代码规范, 后台面板检测, 域名枚举, 数据库, 测试用例, 请求拦截