Skip to content

Commit

Permalink
Fix size calculation in --optimize vacuum
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
timopollmeier committed Mar 16, 2021
1 parent 71c4c12 commit b1d80a8
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -56451,50 +56451,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)
{
Expand Down

0 comments on commit b1d80a8

Please sign in to comment.