More consistent error reporting

repack_all_database can return an error message: in case of any error different
from "missing schema" return the error and keep processing the other databases
instead of printing and stopping the program.

The output of the program is now something like:

    $ pg_reorg --all
    pg_reorg: reorg database "contrib_regression"
    pg_reorg: reorg database "template1" ... skipped: pg_reorg is not installed in the database
This commit is contained in:
Daniele Varrazzo 2012-10-18 00:18:27 +01:00
parent 0af231dff5
commit c43b6bdceb
2 changed files with 19 additions and 14 deletions

View File

@ -83,7 +83,7 @@ typedef struct repack_index
} repack_index; } repack_index;
static void repack_all_databases(const char *order_by); static void repack_all_databases(const char *order_by);
static bool repack_one_database(const char *order_by, const char *table); static bool repack_one_database(const char *order_by, const char *table, char *errbuf, size_t errsize);
static void repack_one_table(const repack_table *table, const char *order_by); static void repack_one_table(const repack_table *table, const char *order_by);
static void repack_cleanup(bool fatal, void *userdata); static void repack_cleanup(bool fatal, void *userdata);
@ -152,10 +152,11 @@ main(int argc, char *argv[])
} }
else else
{ {
if (!repack_one_database(orderby, table)) char errbuf[256];
if (!repack_one_database(orderby, table, errbuf, sizeof(errbuf)))
ereport(ERROR, ereport(ERROR,
(errcode(ENOENT), (errcode(ERROR),
errmsg("%s is not installed", PROGRAM_NAME))); errmsg("%s", errbuf)));
} }
return 0; return 0;
@ -178,6 +179,7 @@ repack_all_databases(const char *orderby)
for (i = 0; i < PQntuples(result); i++) for (i = 0; i < PQntuples(result); i++)
{ {
bool ret; bool ret;
char errbuf[256];
dbname = PQgetvalue(result, i, 0); dbname = PQgetvalue(result, i, 0);
@ -187,14 +189,14 @@ repack_all_databases(const char *orderby)
fflush(stdout); fflush(stdout);
} }
ret = repack_one_database(orderby, NULL); ret = repack_one_database(orderby, NULL, errbuf, sizeof(errbuf));
if (pgut_log_level >= INFO) if (pgut_log_level >= INFO)
{ {
if (ret) if (ret)
printf("\n"); printf("\n");
else else
printf(" ... skipped\n"); printf(" ... skipped: %s\n", errbuf);
fflush(stdout); fflush(stdout);
} }
} }
@ -225,7 +227,7 @@ getoid(PGresult *res, int row, int col)
* Call repack_one_table for the target table or each table in a database. * Call repack_one_table for the target table or each table in a database.
*/ */
static bool static bool
repack_one_database(const char *orderby, const char *table) repack_one_database(const char *orderby, const char *table, char *errbuf, size_t errsize)
{ {
bool ret = true; bool ret = true;
PGresult *res; PGresult *res;
@ -261,21 +263,24 @@ repack_one_database(const char *orderby, const char *table)
res = execute_elevel(sql.data, 0, NULL, DEBUG2); res = execute_elevel(sql.data, 0, NULL, DEBUG2);
} }
/* on error skip the database */
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
if (sqlstate_equals(res, SQLSTATE_INVALID_SCHEMA_NAME)) if (sqlstate_equals(res, SQLSTATE_INVALID_SCHEMA_NAME))
{ {
/* Schema repack does not exist. Skip the database. */ /* Schema repack does not exist. Skip the database. */
ret = false; if (errbuf)
goto cleanup; snprintf(errbuf, errsize,
"%s is not installed in the database", PROGRAM_NAME);
} }
else else
{ {
/* exit otherwise */ /* Return the error message otherwise */
printf("%s", PQerrorMessage(connection)); if (errbuf)
PQclear(res); snprintf(errbuf, errsize, "%s", PQerrorMessage(connection));
exit(1);
} }
ret = false;
goto cleanup;
} }
num = PQntuples(res); num = PQntuples(res);

View File

@ -270,7 +270,7 @@ version load the script ``$SHAREDIR/contrib/uninstall_pg_repack.sql`` into the
database where the error occured and then load database where the error occured and then load
``$SHAREDIR/contrib/pg_repack.sql`` again. ``$SHAREDIR/contrib/pg_repack.sql`` again.
pg_repack: repack database "template1" ... skipped pg_repack: reorg database "template1" ... skipped: pg_repack is not installed in the database
pg_repack is not installed in the database when ``--all`` option is pg_repack is not installed in the database when ``--all`` option is
specified. specified.