Skip to content

Commit 3c73a02

Browse files
schmiddydvarrazzo
authored andcommitted
More consistent error reporting
This patch is a port of Daniele's commit 0be414ad10c32d from his own fork, "error_reporting" branch. reorg_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
1 parent 8ba92a1 commit 3c73a02

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

bin/pg_repack.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ typedef struct repack_index
107107
} repack_index;
108108

109109
static void repack_all_databases(const char *order_by);
110-
static bool repack_one_database(const char *order_by);
110+
static bool repack_one_database(const char *order_by, char *errbuf, size_t errsize);
111111
static void repack_one_table(const repack_table *table, const char *order_by);
112112
static void repack_cleanup(bool fatal, const repack_table *table);
113113

@@ -178,10 +178,11 @@ main(int argc, char *argv[])
178178
}
179179
else
180180
{
181-
if (!repack_one_database(orderby))
181+
char errbuf[256];
182+
if (!repack_one_database(orderby, errbuf, sizeof(errbuf)))
182183
ereport(ERROR,
183-
(errcode(ENOENT),
184-
errmsg("%s is not installed", PROGRAM_NAME)));
184+
(errcode(ERROR),
185+
errmsg("%s", errbuf)));
185186
}
186187

187188
return 0;
@@ -204,23 +205,24 @@ repack_all_databases(const char *orderby)
204205
for (i = 0; i < PQntuples(result); i++)
205206
{
206207
bool ret;
208+
char errbuf[256];
207209

208210
dbname = PQgetvalue(result, i, 0);
209211

210212
if (pgut_log_level >= INFO)
211213
{
212-
printf("%s: repack database \"%s\"", PROGRAM_NAME, dbname);
214+
printf("%s: repack database \"%s\"\n", PROGRAM_NAME, dbname);
213215
fflush(stdout);
214216
}
215217

216-
ret = repack_one_database(orderby);
218+
ret = repack_one_database(orderby, errbuf, sizeof(errbuf));
217219

218220
if (pgut_log_level >= INFO)
219221
{
220222
if (ret)
221223
printf("\n");
222224
else
223-
printf(" ... skipped\n");
225+
printf(" ... skipped: %s\n", errbuf);
224226
fflush(stdout);
225227
}
226228
}
@@ -252,7 +254,7 @@ getoid(PGresult *res, int row, int col)
252254
* Call repack_one_table for the target table or each table in a database.
253255
*/
254256
static bool
255-
repack_one_database(const char *orderby)
257+
repack_one_database(const char *orderby, char *errbuf, size_t errsize)
256258
{
257259
bool ret = true;
258260
PGresult *res;
@@ -303,22 +305,25 @@ repack_one_database(const char *orderby)
303305
res = execute_elevel(sql.data, 0, NULL, DEBUG2);
304306
}
305307

308+
/* on error skip the database */
306309
if (PQresultStatus(res) != PGRES_TUPLES_OK)
307310
{
308311
if (sqlstate_equals(res, SQLSTATE_INVALID_SCHEMA_NAME))
309312
{
310313
/* Schema repack does not exist. Skip the database. */
311-
PQclear(res);
312-
ret = false;
313-
goto cleanup;
314+
if (errbuf)
315+
snprintf(errbuf, errsize,
316+
"%s is not installed in the database", PROGRAM_NAME);
317+
314318
}
315319
else
316320
{
317-
/* exit otherwise */
318-
elog(ERROR, "%s", PQerrorMessage(connection));
319-
PQclear(res);
320-
exit(1);
321+
/* Return the error message otherwise */
322+
if (errbuf)
323+
snprintf(errbuf, errsize, "%s", PQerrorMessage(connection));
321324
}
325+
ret = false;
326+
goto cleanup;
322327
}
323328

324329
num = PQntuples(res);

0 commit comments

Comments
 (0)