Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for different schemes (not only for public) #17

Merged
merged 8 commits into from
Dec 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tests fix #1
  • Loading branch information
ivan.vakhrushev committed Dec 22, 2019
commit 6b1f3e363035e7dac9930c09f8899b9b3ce8966c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public class IndexMaintenanceImpl implements IndexMaintenance {
" join pg_catalog.pg_index i on i.indrelid = c.conrelid and (c.conkey::int[] <@ i.indkey::int[])\n" +
" where c.contype = 'f'\n" +
")\n" +
"select psui.relname as table_name,\n" +
" psui.indexrelname as index_name,\n" +
"select psui.relid::regclass::text as table_name,\n" +
" psui.indexrelid::regclass::text as index_name,\n" +
" pg_relation_size(i.indexrelid) as index_size,\n" +
" psui.idx_scan as index_scans\n" +
"from pg_catalog.pg_stat_user_indexes psui\n" +
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/mfvanek/pg/utils/QueryExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static <T> List<T> executeQuery(@Nonnull final PgConnection pgConnection,
@Nonnull final PgContext pgContext,
@Nonnull final String sqlQuery,
@Nonnull final ResultSetExtractor<T> rse) {
LOGGER.debug("Executing query: {}", sqlQuery);
LOGGER.debug("Executing query with context {}: {}", pgContext, sqlQuery);
try (Connection connection = pgConnection.getDataSource().getConnection();
PreparedStatement statement = connection.prepareStatement(Objects.requireNonNull(sqlQuery))) {
statement.setString(1, pgContext.getSchemaName());
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/sql/unused_indexes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ with foreign_key_indexes as (
join pg_catalog.pg_index i on i.indrelid = c.conrelid and (c.conkey::int[] <@ i.indkey::int[])
where c.contype = 'f'
)
select psui.relname as table_name,
psui.indexrelname as index_name,
select psui.relid::regclass::text as table_name,
psui.indexrelid::regclass::text as index_name,
pg_relation_size(i.indexrelid) as index_size,
psui.idx_scan as index_scans
from pg_catalog.pg_stat_user_indexes psui
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void getTablesWithMissingIndexesOnDatabaseWithThem(final String schemaName) {
executeTestOnDatabase(schemaName,
dbp -> dbp.withReferences().withData(),
ctx -> {
tryToFindAccountByClientId(schemaName, AMOUNT_OF_TRIES);
tryToFindAccountByClientId(schemaName);
final var tables = indexesHealth.getTablesWithMissingIndexes(ctx);
assertNotNull(tables);
assertThat(tables, hasSize(1));
Expand Down Expand Up @@ -356,8 +356,7 @@ void shouldResetCounters(final String schemaName) {
executeTestOnDatabase(schemaName,
dbp -> dbp.withReferences().withData(),
ctx -> {
tryToFindAccountByClientId(schemaName, AMOUNT_OF_TRIES);
waitForStatisticsCollector();
tryToFindAccountByClientId(schemaName);
assertThat(getSeqScansForAccounts(ctx), greaterThanOrEqualTo(AMOUNT_OF_TRIES));
indexesHealth.resetStatistics();
waitForStatisticsCollector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ void getTablesWithMissingIndexesOnDatabaseWithThem(final String schemaName) {
executeTestOnDatabase(schemaName,
dbp -> dbp.withReferences().withData(),
ctx -> {
tryToFindAccountByClientId(schemaName, AMOUNT_OF_TRIES);
tryToFindAccountByClientId(schemaName);
var tables = indexMaintenance.getTablesWithMissingIndexes(ctx);
assertNotNull(tables);
assertEquals(1, tables.size());
assertThat(tables, hasSize(1));
var table = tables.get(0);
if (isDefaultSchema(schemaName)) {
assertEquals("accounts", table.getTableName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void shouldResetCounters(final String schemaName) {
executeTestOnDatabase(schemaName,
dbp -> dbp.withReferences().withData(),
ctx -> {
tryToFindAccountByClientId(schemaName, AMOUNT_OF_TRIES);
tryToFindAccountByClientId(schemaName);
final PgContext pgContext = PgContext.of(schemaName);
assertThat(getSeqScansForAccounts(pgContext), greaterThanOrEqualTo(AMOUNT_OF_TRIES));
statisticsMaintenance.resetStatistics();
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/mfvanek/pg/utils/DatabaseAwareTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ protected boolean isDefaultSchema(@Nonnull final String schemaName) {
return "public".equals(schemaName);
}

protected void tryToFindAccountByClientId(@Nonnull final String schemaName,
final long amountOfTries) {
protected void tryToFindAccountByClientId(@Nonnull final String schemaName) {
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
for (int counter = 0; counter < amountOfTries; ++counter) {
for (int counter = 0; counter < AMOUNT_OF_TRIES; ++counter) {
statement.execute(String.format(
"select count(*) from %s.accounts where client_id = 1::bigint", schemaName));
}
waitForStatisticsCollector();
} catch (SQLException e) {
throw new RuntimeException(e);
}
Expand Down