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

Fix size calculation in --optimize vacuum (bp #1447) #1450

Merged
merged 4 commits into from
Mar 17, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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

Expand Down
53 changes: 20 additions & 33 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -55247,50 +55247,37 @@ 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)
{
Expand Down