diff --git a/bin/pg_repack.c b/bin/pg_repack.c index 5960ab9..98ce7b5 100644 --- a/bin/pg_repack.c +++ b/bin/pg_repack.c @@ -83,7 +83,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, 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_cleanup(bool fatal, void *userdata); @@ -152,10 +152,11 @@ main(int argc, char *argv[]) } else { - if (!repack_one_database(orderby, table)) + char errbuf[256]; + if (!repack_one_database(orderby, table, errbuf, sizeof(errbuf))) ereport(ERROR, - (errcode(ENOENT), - errmsg("%s is not installed", PROGRAM_NAME))); + (errcode(ERROR), + errmsg("%s", errbuf))); } return 0; @@ -178,6 +179,7 @@ repack_all_databases(const char *orderby) for (i = 0; i < PQntuples(result); i++) { bool ret; + char errbuf[256]; dbname = PQgetvalue(result, i, 0); @@ -187,14 +189,14 @@ repack_all_databases(const char *orderby) fflush(stdout); } - ret = repack_one_database(orderby, NULL); + ret = repack_one_database(orderby, NULL, errbuf, sizeof(errbuf)); if (pgut_log_level >= INFO) { if (ret) printf("\n"); else - printf(" ... skipped\n"); + printf(" ... skipped: %s\n", errbuf); 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. */ 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; PGresult *res; @@ -261,21 +263,24 @@ repack_one_database(const char *orderby, const char *table) 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. */ - ret = false; - goto cleanup; + if (errbuf) + snprintf(errbuf, errsize, + "%s is not installed in the database", PROGRAM_NAME); } else { - /* exit otherwise */ - printf("%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); diff --git a/doc/pg_repack.rst b/doc/pg_repack.rst index 672b89d..7820593 100644 --- a/doc/pg_repack.rst +++ b/doc/pg_repack.rst @@ -270,7 +270,7 @@ version load the script ``$SHAREDIR/contrib/uninstall_pg_repack.sql`` into the database where the error occured and then load ``$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 specified.