pg-index-health is a Java library for analyzing and maintaining indexes health in Postgresql databases.
- 9.6
- 10
- 11
pg-index-health 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).
pg_index_health uses the PostgreSQL's statistics collector.
You can call pg_stat_reset()
to reset all statistics counters for the current database to zero.
import io.github.mfvanek.pg.connection.HighAvailabilityPgConnectionFactoryImpl;
import io.github.mfvanek.pg.connection.PgConnectionFactoryImpl;
import io.github.mfvanek.pg.index.health.logger.Exclusions;
import io.github.mfvanek.pg.index.health.logger.SimpleHealthLogger;
import io.github.mfvanek.pg.index.maintenance.MaintenanceFactoryImpl;
import io.github.mfvanek.pg.model.MemoryUnit;
import io.github.mfvanek.pg.model.PgContext;
public class DemoApp {
public static void main(String[] args) {
forTesting();
forProduction();
}
private static void forTesting() {
final String writeUrl = "jdbc:postgresql://host-name-1:6432,host-name-2:6432,host-name-3:6432/db_name_testing?targetServerType=master&ssl=true&prepareThreshold=0&preparedStatementCacheQueries=0&sslmode=require";
final String readUrl = "jdbc:postgresql://host-name-1:6432,host-name-2:6432,host-name-3:6432/db_name_testing?targetServerType=preferSlave&loadBalanceHosts=true&ssl=true&prepareThreshold=0&preparedStatementCacheQueries=0&sslmode=require";
final String userName = "user_name_testing";
final String password = "password_testing";
final HighAvailabilityPgConnectionFactory haPgConnectionFactory = new HighAvailabilityPgConnectionFactoryImpl(new PgConnectionFactoryImpl());
final HighAvailabilityPgConnection haPgConnection = haPgConnectionFactory.of(writeUrl, userName, password, readUrl);
final IndexesHealth indexesHealth = new IndexesHealthImpl(haPgConnection, new MaintenanceFactoryImpl());
final IndexesHealthLogger logger = new SimpleHealthLogger(indexesHealth);
logger.logAll(Exclusions.empty(), PgContext.ofPublic())
.forEach(System.out::println);
// Resetting current statistics
// indexesHealth.resetStatistics();
}
private static void forProduction() {
final String writeUrl = "jdbc:postgresql://host-name-1:6432,host-name-2:6432,host-name-3:6432/db_name_production?ssl=true&targetServerType=master&prepareThreshold=0&preparedStatementCacheQueries=0&connectTimeout=2&socketTimeout=50&loginTimeout=10&sslmode=require";
final String readUrl = "jdbc:postgresql://host-name-1:6432,host-name-2:6432,host-name-3:6432,host-name-4:6432,host-name-5:6432/db_name_production?ssl=true&targetServerType=preferSlave&loadBalanceHosts=true&prepareThreshold=0&preparedStatementCacheQueries=0&connectTimeout=2&socketTimeout=50&loginTimeout=10&sslmode=require";
final String cascadeAsyncReadUrl = "jdbc:postgresql://host-name-6:6432/db_name_production?ssl=true&targetServerType=preferSlave&loadBalanceHosts=true&prepareThreshold=0&preparedStatementCacheQueries=0&connectTimeout=2&socketTimeout=50&loginTimeout=10&sslmode=require";
final String userName = "user_name_production";
final String password = "password_production";
final HighAvailabilityPgConnectionFactory haPgConnectionFactory = new HighAvailabilityPgConnectionFactoryImpl(new PgConnectionFactoryImpl());
final HighAvailabilityPgConnection haPgConnection = haPgConnectionFactory.of(writeUrl, userName, password, readUrl, cascadeAsyncReadUrl);
final IndexesHealth indexesHealth = new IndexesHealthImpl(haPgConnection, new MaintenanceFactoryImpl());
final Exclusions exclusions = Exclusions.builder()
.withIndexSizeThreshold(10, MemoryUnit.MB)
.withTableSizeThreshold(10, MemoryUnit.MB)
.build();
final IndexesHealthLogger logger = new SimpleHealthLogger(indexesHealth);
logger.logAll(exclusions, PgContext.ofPublic())
.forEach(System.out::println);
}
}