diff --git a/bin/pg_repack.c b/bin/pg_repack.c index ec25251..c264d77 100644 --- a/bin/pg_repack.c +++ b/bin/pg_repack.c @@ -107,7 +107,7 @@ typedef struct repack_index } repack_index; static void repack_all_databases(const char *order_by); -static bool repack_one_database(const char *order_by); +static bool repack_one_database(const char *order_by, char *errbuf, size_t errsize); static void repack_one_table(const repack_table *table, const char *order_by); static void repack_cleanup(bool fatal, const repack_table *table); @@ -178,10 +178,11 @@ main(int argc, char *argv[]) } else { - if (!repack_one_database(orderby)) + char errbuf[256]; + if (!repack_one_database(orderby, errbuf, sizeof(errbuf))) ereport(ERROR, - (errcode(ENOENT), - errmsg("%s is not installed", PROGRAM_NAME))); + (errcode(ERROR), + errmsg("%s", errbuf))); } return 0; @@ -204,23 +205,24 @@ repack_all_databases(const char *orderby) for (i = 0; i < PQntuples(result); i++) { bool ret; + char errbuf[256]; dbname = PQgetvalue(result, i, 0); if (pgut_log_level >= INFO) { - printf("%s: repack database \"%s\"", PROGRAM_NAME, dbname); + printf("%s: repack database \"%s\"\n", PROGRAM_NAME, dbname); fflush(stdout); } - ret = repack_one_database(orderby); + ret = repack_one_database(orderby, errbuf, sizeof(errbuf)); if (pgut_log_level >= INFO) { if (ret) printf("\n"); else - printf(" ... skipped\n"); + printf(" ... skipped: %s\n", errbuf); fflush(stdout); } } @@ -252,7 +254,7 @@ getoid(PGresult *res, int row, int col) * Call repack_one_table for the target table or each table in a database. */ static bool -repack_one_database(const char *orderby) +repack_one_database(const char *orderby, char *errbuf, size_t errsize) { bool ret = true; PGresult *res; @@ -303,22 +305,25 @@ repack_one_database(const char *orderby) res = execute_elevel(sql.data, 0, NULL, DEBUG2); } + /* on error skip the database */ if (PQresultStatus(res) != PGRES_TUPLES_OK) { if (sqlstate_equals(res, SQLSTATE_INVALID_SCHEMA_NAME)) { /* Schema repack does not exist. Skip the database. */ - PQclear(res); - ret = false; - goto cleanup; + if (errbuf) + snprintf(errbuf, errsize, + "%s is not installed in the database", PROGRAM_NAME); + } else { - /* exit otherwise */ - elog(ERROR, "%s", PQerrorMessage(connection)); - PQclear(res); - exit(1); + /* Return the error message otherwise */ + if (errbuf) + snprintf(errbuf, errsize, "%s", PQerrorMessage(connection)); } + ret = false; + goto cleanup; } num = PQntuples(res);