Skip to content
This repository was archived by the owner on Jul 6, 2021. It is now read-only.

Commit a87446f

Browse files
committed
fix: F004 exclude empty tables from recommendations
In case when a table is empty (has 0 rows) its heap bloat is estimated as 100%. In some cases, such a table might be included in "F004 Recommendations" and considered as of top priority (P1). This leads to messages containing the wrong statement. This behavior was changed: from now on, empty tables are excluded from consideration. CI: a new test was added to check this case automatically.
2 parents 4caa7c5 + 23e1f9c commit a87446f

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

.ci/test_db_dump.sql

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ delete from bloated where i % 2 = 0;
5858

5959
-- F004
6060
create table t_with_bloat as select i from generate_series(1, 1000000) _(i);
61+
-- create table without data
62+
create table t_f004_empty as select i from generate_series(1, 1000000) _(i);
63+
alter table t_f004_empty set (autovacuum_enabled = 'off');
64+
analyze t_f004_empty;
6165

6266
-- h002 Supports fk
6367
create table t_red_fk_1 as select id::int8 from generate_series(0, 1000000) _(id);
@@ -117,3 +121,7 @@ CREATE TABLE test_schema."orders_A"
117121

118122
INSERT INTO test_schema."orders_A"(cnt) select id from generate_series(0, 100) _(id);
119123
SELECT setval('test_schema."orders_A_id_seq"'::regclass, 300000000, false);
124+
125+
-- t_f004 continue
126+
delete from t_f004_empty;
127+
analyze t_f004_empty;

.gitlab-ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ variables:
5555
- data_dir=$(cat ./artifacts/test/nodes.json | jq -r '.last_check | .dir') && l001_indexes_size=$(cat ./artifacts/test/json_reports/$data_dir/L001_table_sizes.json | jq '.results .postgres .data .tables_data_total .indexes_size_bytes_sum') && echo $l001_indexes_size
5656
- diff=$((f005_indexes_size - l001_indexes_size)) && diff=${diff#-} && echo $diff && ratio=$((diff * 100 / l001_indexes_size)) && echo $ratio && ([[ $ratio -gt "5" ]]) && exit 303
5757
- echo "F005/L001 Total indexes size passed"
58+
- ./checkup -h postgres --username test_user --project test --dbname dbname -e 1 --file ./resources/checks/F004_heap_bloat.sh
59+
- data_dir=$(cat ./artifacts/test/nodes.json | jq -r '.last_check | .dir') && msg=$(grep "can be reduced 0.00 times" ./artifacts/test/md_reports/$data_dir/F004.md) && [[ ! -z "$msg" ]] && exit 304
5860
- ([[ "$CI_COMMIT_REF_NAME" != "master" ]]) && exit 0
5961
# Check small indexes
6062
- .ci/prepare_test_db.sh postgres

pghrep/src/checkup/f004/f004.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const F004_GENERAL_INFO string = "F004_GENERAL_INFO"
2020
const WARNING_BLOAT_RATIO float32 = 40.0
2121
const CRITICAL_BLOAT_RATIO float32 = 90.0
2222
const CRITICAL_TOTAL_BLOAT_RATIO float32 = 20.0
23-
const MIN_INDEX_SIZE_TO_ANALYZE int64 = 1024 * 1024
23+
const MIN_TABLE_SIZE_TO_ANALYZE int64 = 1024 * 1024
2424

2525
func appendTable(list []string, tableBloatData F004HeapBloat) []string {
2626
return append(list, fmt.Sprintf(TABLE_DETAILS, tableBloatData.TableName,
@@ -48,11 +48,12 @@ func F004Process(report F004Report) checkup.ReportResult {
4848
}
4949
for _, table := range sortedTables {
5050
heapBloatData := hostData.Data.HeapBloat[table]
51-
if (heapBloatData.RealSizeBytes > MIN_INDEX_SIZE_TO_ANALYZE) && (heapBloatData.BloatRatioPercent >= WARNING_BLOAT_RATIO) &&
52-
(heapBloatData.BloatRatioPercent < CRITICAL_BLOAT_RATIO) {
51+
if (heapBloatData.RealSizeBytes > MIN_TABLE_SIZE_TO_ANALYZE) && (heapBloatData.BloatRatioPercent >= WARNING_BLOAT_RATIO) &&
52+
(heapBloatData.BloatRatioPercent < CRITICAL_BLOAT_RATIO) && (heapBloatData.BloatRatioFactor > 0) {
5353
warningTables = appendTable(warningTables, heapBloatData)
5454
}
55-
if heapBloatData.RealSizeBytes > MIN_INDEX_SIZE_TO_ANALYZE && heapBloatData.BloatRatioPercent >= CRITICAL_BLOAT_RATIO {
55+
if (heapBloatData.RealSizeBytes > MIN_TABLE_SIZE_TO_ANALYZE) && (heapBloatData.BloatRatioPercent >= CRITICAL_BLOAT_RATIO) &&
56+
(heapBloatData.BloatRatioFactor > 0) {
5657
criticalTables = appendTable(criticalTables, heapBloatData)
5758
}
5859
}

pghrep/src/checkup/f005/f005.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const F005_BLOAT_EXCESS_INFO string = "F005_BLOAT_EXCESS_INFO"
2222
const WARNING_BLOAT_RATIO float32 = 40.0
2323
const CRITICAL_BLOAT_RATIO float32 = 90.0
2424
const CRITICAL_TOTAL_BLOAT_RATIO float32 = 20.0
25-
const MIN_TABLE_SIZE_TO_ANALYZE int64 = 1024 * 1024
25+
const MIN_INDEX_SIZE_TO_ANALYZE int64 = 1024 * 1024
2626

2727
func appendIndex(list []string, indexBloatData F005IndexBloat) []string {
2828
return append(list, fmt.Sprintf(INDEX_DETAILS, indexBloatData.IndexName,
@@ -52,15 +52,17 @@ func F005Process(report F005Report) checkup.ReportResult {
5252
}
5353
for _, index := range sortedIndexes {
5454
indexBloatData := hostData.Data.IndexBloat[index]
55-
if totalBloatIsCritical && indexBloatData.RealSizeBytes > MIN_TABLE_SIZE_TO_ANALYZE && i < 5 {
55+
if totalBloatIsCritical && indexBloatData.RealSizeBytes > MIN_INDEX_SIZE_TO_ANALYZE && i < 5 &&
56+
(indexBloatData.BloatRatioFactor > 0) {
5657
top5Indexes = appendIndex(top5Indexes, indexBloatData)
5758
i++
5859
}
59-
if indexBloatData.RealSizeBytes > MIN_TABLE_SIZE_TO_ANALYZE && indexBloatData.BloatRatioPercent >= CRITICAL_BLOAT_RATIO {
60+
if indexBloatData.RealSizeBytes > MIN_INDEX_SIZE_TO_ANALYZE && indexBloatData.BloatRatioPercent >= CRITICAL_BLOAT_RATIO &&
61+
(indexBloatData.BloatRatioFactor > 0) {
6062
criticalIndexes = appendIndex(criticalIndexes, indexBloatData)
6163
}
62-
if (indexBloatData.RealSizeBytes > MIN_TABLE_SIZE_TO_ANALYZE) && (indexBloatData.BloatRatioPercent >= WARNING_BLOAT_RATIO) &&
63-
(indexBloatData.BloatRatioPercent < CRITICAL_BLOAT_RATIO) {
64+
if (indexBloatData.RealSizeBytes > MIN_INDEX_SIZE_TO_ANALYZE) && (indexBloatData.BloatRatioPercent >= WARNING_BLOAT_RATIO) &&
65+
(indexBloatData.BloatRatioPercent < CRITICAL_BLOAT_RATIO) && (indexBloatData.BloatRatioFactor > 0) {
6466
warningIndexes = appendIndex(warningIndexes, indexBloatData)
6567
}
6668
}

0 commit comments

Comments
 (0)