From 444bb634391d3cfbc5b61b691f0ff82bdcb60f7e Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Tue, 16 Mar 2021 09:20:30 +0100 Subject: [PATCH 1/3] Fix size calculation in `--optimize vacuum` The database size was still trying to stat a filesystem path as used by SQLite as the database name. This is changed to use the SQL function pg_database_size instead and also the case where the size increases is now handled. (cherry picked from commit b1d80a85c5892005fdb14c74c662cb66cfd82d57) --- src/manage_sql.c | 52 +++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 919637a14..78fd43168 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -55235,50 +55235,40 @@ manage_optimize (GSList *log_config, const db_conn_info_t *database, ret = 0; if (strcasecmp (name, "vacuum") == 0) { - struct stat state; - long long int old_size, new_size; + gchar *quoted_db_name; + unsigned long long int old_size, new_size; old_size = 0LL; new_size = 0LL; - ret = stat (database->name, &state); - if (ret) - switch (errno) - { - case ENOENT: - break; - default: - g_warning ("%s: failed to stat database: %s", - __func__, - strerror (errno)); - } - else - old_size = state.st_size; + + quoted_db_name = sql_quote (sql_database ()); + + old_size = sql_int64_0 ("SELECT pg_database_size ('%s')", + quoted_db_name); sql ("VACUUM;"); - ret = stat (database->name, &state); - if (ret) - switch (errno) - { - case ENOENT: - break; - default: - g_warning ("%s: failed to stat database: %s", - __func__, - strerror (errno)); - } - else - new_size = state.st_size; + new_size = sql_int64_0 ("SELECT pg_database_size ('%s')", + quoted_db_name); + + g_free (quoted_db_name); - if (old_size && new_size) + if (old_size <= 0 || new_size <= 0) + success_text = g_strdup_printf ("Optimized: vacuum."); + else if (new_size <= old_size) success_text = g_strdup_printf ("Optimized: vacuum." " Database file size reduced by" - " %lld MiB (%0.1f %%).\n", + " %llu MiB (%0.1f %%).\n", (old_size - new_size) / (1024 * 1024), (old_size - new_size) * 100.0 / old_size); else - success_text = g_strdup_printf ("Optimized: vacuum."); + success_text = g_strdup_printf ("Optimized: vacuum." + " Database file size *increased* by" + " %llu MiB (%0.1f %%).\n", + (new_size - old_size) / (1024 * 1024), + (new_size - old_size) + * 100.0 / old_size); } else if (strcasecmp (name, "analyze") == 0) { From 042f959eb27c264e83f30919038143cbb4f4e1c4 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Tue, 16 Mar 2021 09:47:45 +0100 Subject: [PATCH 2/3] Add vacuum size calculation fix to CHANGELOG (cherry picked from commit f42fa43a3b94583f00975659a8fb841f53ac92b5) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c4e8ac6b..d4a47d5d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Fix SQL escaping when adding VT references [#1429](https://github.com/greenbone/gvmd/pull/1429) - Update report run status more consistently [#1434](https://github.com/greenbone/gvmd/pull/1434) - Improve modify_override errors, fix no NVT case [#1435](https://github.com/greenbone/gvmd/pull/1435) +- Fix size calculation in `--optimize vacuum` [#1447](https://github.com/greenbone/gvmd/pull/1447) ### Removed From 3c233fec6220bfda44ffc8f43e35cf72dc04e180 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Tue, 16 Mar 2021 10:34:22 +0100 Subject: [PATCH 3/3] Remove unused initializations in manage_optimize In the "vacuum" case, old_size and new_size no longer need to be initialized with 0. (cherry picked from commit b4d48f73c4c5722e8a9dbcbb9947a5aa5d979a20) --- src/manage_sql.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/manage_sql.c b/src/manage_sql.c index 78fd43168..77d6489bc 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -55238,9 +55238,6 @@ manage_optimize (GSList *log_config, const db_conn_info_t *database, gchar *quoted_db_name; unsigned long long int old_size, new_size; - old_size = 0LL; - new_size = 0LL; - quoted_db_name = sql_quote (sql_database ()); old_size = sql_int64_0 ("SELECT pg_database_size ('%s')",