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

issue #3949 fix schema size collection for PostgreSQL with multiple s… #3954

Merged
merged 1 commit into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public String getSchemaName() {
* @param resourceType
* @param tableName
* @param isParamTable
* @param size
* @param tableSize
* @param rowEstimate
*/
public void accumulateTableSize(String resourceType, String tableName, boolean isParamTable, long size, long rowEstimate) {
public void accumulateTableSize(String resourceType, String tableName, boolean isParamTable, long tableSize, long rowEstimate) {
FHIRDbResourceSize resourceSize = resourceSizeMap.computeIfAbsent(resourceType, k -> new FHIRDbResourceSize());
resourceSize.accumulateTableSize(tableName, isParamTable, size, rowEstimate);
this.totalTableSize += size;
resourceSize.accumulateTableSize(tableName, isParamTable, tableSize, rowEstimate);
this.totalTableSize += tableSize;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,15 @@ public void run(String schemaName, Connection connection, IDatabaseTranslator tr

private void collectTableInfo(String schemaName, Connection connection, IDatabaseTranslator translator) {
final String SQL = ""
+ "SELECT table_name, row_estimate, total_bytes, index_bytes, toast_bytes,"
+ " total_bytes-index_bytes-coalesce(toast_bytes,0) AS table_bytes"
+ " FROM (SELECT c.oid,"
+ " nspname AS table_schema,"
+ " relname AS table_name,"
+ " c.reltuples AS row_estimate,"
+ " pg_total_relation_size(c.oid) AS total_bytes,"
+ " pg_indexes_size(c.oid) AS index_bytes,"
+ " pg_total_relation_size(reltoastrelid) AS toast_bytes"
+ " FROM pg_class c"
+ " LEFT JOIN pg_namespace n "
+ " ON n.oid = c.relnamespace"
+ " WHERE relkind = 'r'"
+ " AND nspname = ?"
+ " ) a";
+ "SELECT relname AS table_name, "
+ " c.reltuples AS row_estimate, "
+ " pg_table_size(c.oid) AS table_size, "
+ " pg_indexes_size(c.oid) AS index_size "
+ " FROM pg_class c "
+ " JOIN pg_namespace n "
+ " ON n.oid = c.relnamespace "
+ " WHERE relkind = 'r' "
+ " AND nspname = ?";

logger.info("Collecting PostgreSQL table size info for schema: '" + schemaName.toLowerCase() + "'");
SchemaSupport util = new SchemaSupport();
Expand All @@ -66,19 +60,18 @@ private void collectTableInfo(String schemaName, Connection connection, IDatabas
while (rs.next()) {
final String tableName = rs.getString(1);
final long rowEstimate = rs.getLong(2);
final long totalBytes = rs.getLong(3);
final long indexBytes = rs.getLong(4);
final long toastBytes = rs.getLong(5);
final long tableSize = rs.getLong(3);
final long indexSize = rs.getLong(4);

// Note resourceType will be null for tables we don't care about
final String resourceType = util.getResourceTypeFromTableName(tableName);
if (resourceType != null) {
final boolean isParamTable = util.isParamTable(tableName);

if (logger.isLoggable(Level.FINE)) {
logger.fine(String.format("%56s %34s %8d %10d %10d %10d", tableName, resourceType, rowEstimate, totalBytes, indexBytes, toastBytes));
logger.fine(String.format("%56s %34s %8d %10d %10d", tableName, resourceType, rowEstimate, tableSize, indexSize));
}
model.accumulateTableSize(resourceType, tableName, isParamTable, totalBytes - indexBytes, rowEstimate);
model.accumulateTableSize(resourceType, tableName, isParamTable, tableSize, rowEstimate);
}
}
} catch (SQLException x) {
Expand All @@ -88,19 +81,14 @@ private void collectTableInfo(String schemaName, Connection connection, IDatabas

private void collectIndexInfo(String schemaName, Connection connection, IDatabaseTranslator translator) {
final String SQL = ""
+ " SELECT t.tablename,"
+ " SELECT c.relname AS table_name, "
+ " psai.indexrelname AS index_name, "
+ " pg_relation_size(i.indexrelid) AS index_size, "
+ " CASE WHEN i.indisunique THEN 'Y' ELSE 'N' END AS is_unique, "
+ " psai.idx_scan AS number_of_scans, "
+ " psai.idx_tup_read AS tuples_read,"
+ " psai.idx_tup_fetch AS tuples_fetched "
+ " FROM pg_tables t "
+ " LEFT JOIN pg_class c ON t.tablename = c.relname "
+ " LEFT JOIN pg_index i ON c.oid = i.indrelid "
+ " LEFT JOIN pg_stat_all_indexes psai ON i.indexrelid = psai.indexrelid "
+ " WHERE t.schemaname = ? "
+ " AND psai.indexrelname IS NOT NULL "
+ " pg_relation_size(i.indexrelid) AS index_size "
+ " FROM pg_stat_all_indexes psai "
+ " JOIN pg_index i ON i.indexrelid = psai.indexrelid "
+ " JOIN pg_class c ON c.oid = i.indrelid "
+ " WHERE psai.schemaname = ? "
+ " AND psai.indexrelname IS NOT NULL"
;

logger.info("Collecting PostgreSQL index size info for schema: '" + schemaName.toLowerCase() + "'");
Expand Down