Skip to content

Commit 5b6a162

Browse files
committed
Shuffle sanity checks into preliminary_checks().
This refactoring helps pave the way for online indexes-only repacking. Patch by Beena Emerson.
1 parent a942042 commit 5b6a162

File tree

1 file changed

+89
-70
lines changed

1 file changed

+89
-70
lines changed

bin/pg_repack.c

Lines changed: 89 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ typedef struct repack_index
173173

174174
static bool is_superuser(void);
175175
static void check_tablespace(void);
176+
static bool preliminary_checks(char *errbuf, size_t errsize);
176177
static void repack_all_databases(const char *order_by);
177178
static bool repack_one_database(const char *order_by, char *errbuf, size_t errsize);
178179
static void repack_one_table(const repack_table *table, const char *order_by);
@@ -339,6 +340,93 @@ check_tablespace()
339340
CLEARPGRES(res);
340341
}
341342

343+
/*
344+
* Perform sanity checks before beginning work. Make sure pg_repack is
345+
* installed in the database, the user is a superuser, etc.
346+
*/
347+
static bool
348+
preliminary_checks(char *errbuf, size_t errsize){
349+
bool ret = false;
350+
PGresult *res = NULL;
351+
352+
if (!is_superuser()) {
353+
if (errbuf)
354+
snprintf(errbuf, errsize, "You must be a superuser to use %s",
355+
PROGRAM_NAME);
356+
goto cleanup;
357+
}
358+
359+
/* Query the extension version. Exit if no match */
360+
res = execute_elevel("select repack.version(), repack.version_sql()",
361+
0, NULL, DEBUG2);
362+
if (PQresultStatus(res) == PGRES_TUPLES_OK)
363+
{
364+
const char *libver;
365+
char buf[64];
366+
367+
/* the string is something like "pg_repack 1.1.7" */
368+
snprintf(buf, sizeof(buf), "%s %s", PROGRAM_NAME, PROGRAM_VERSION);
369+
370+
/* check the version of the C library */
371+
libver = getstr(res, 0, 0);
372+
if (0 != strcmp(buf, libver))
373+
{
374+
if (errbuf)
375+
snprintf(errbuf, errsize,
376+
"program '%s' does not match database library '%s'",
377+
buf, libver);
378+
goto cleanup;
379+
}
380+
381+
/* check the version of the SQL extension */
382+
libver = getstr(res, 0, 1);
383+
if (0 != strcmp(buf, libver))
384+
{
385+
if (errbuf)
386+
snprintf(errbuf, errsize,
387+
"extension '%s' required, found extension '%s'",
388+
buf, libver);
389+
goto cleanup;
390+
}
391+
}
392+
else
393+
{
394+
if (sqlstate_equals(res, SQLSTATE_INVALID_SCHEMA_NAME)
395+
|| sqlstate_equals(res, SQLSTATE_UNDEFINED_FUNCTION))
396+
{
397+
/* Schema repack does not exist, or version too old (version
398+
* functions not found). Skip the database.
399+
*/
400+
if (errbuf)
401+
snprintf(errbuf, errsize,
402+
"%s %s is not installed in the database",
403+
PROGRAM_NAME, PROGRAM_VERSION);
404+
}
405+
else
406+
{
407+
/* Return the error message otherwise */
408+
if (errbuf)
409+
snprintf(errbuf, errsize, "%s", PQerrorMessage(connection));
410+
}
411+
goto cleanup;
412+
}
413+
CLEARPGRES(res);
414+
415+
/* Disable statement timeout. */
416+
command("SET statement_timeout = 0", 0, NULL);
417+
418+
/* Restrict search_path to system catalog. */
419+
command("SET search_path = pg_catalog, pg_temp, public", 0, NULL);
420+
421+
/* To avoid annoying "create implicit ..." messages. */
422+
command("SET client_min_messages = warning", 0, NULL);
423+
424+
ret = true;
425+
426+
cleanup:
427+
CLEARPGRES(res);
428+
return ret;
429+
}
342430

343431
/*
344432
* Call repack_one_database for each database.
@@ -424,77 +512,8 @@ repack_one_database(const char *orderby, char *errbuf, size_t errsize)
424512
if (jobs > 1)
425513
setup_workers(jobs);
426514

427-
if (!is_superuser()) {
428-
if (errbuf)
429-
snprintf(errbuf, errsize, "You must be a superuser to use %s",
430-
PROGRAM_NAME);
431-
goto cleanup;
432-
}
433-
434-
/* Query the extension version. Exit if no match */
435-
res = execute_elevel("select repack.version(), repack.version_sql()",
436-
0, NULL, DEBUG2);
437-
if (PQresultStatus(res) == PGRES_TUPLES_OK)
438-
{
439-
const char *libver;
440-
char buf[64];
441-
442-
/* the string is something like "pg_repack 1.1.7" */
443-
snprintf(buf, sizeof(buf), "%s %s", PROGRAM_NAME, PROGRAM_VERSION);
444-
445-
/* check the version of the C library */
446-
libver = getstr(res, 0, 0);
447-
if (0 != strcmp(buf, libver))
448-
{
449-
if (errbuf)
450-
snprintf(errbuf, errsize,
451-
"program '%s' does not match database library '%s'",
452-
buf, libver);
453-
goto cleanup;
454-
}
455-
456-
/* check the version of the SQL extension */
457-
libver = getstr(res, 0, 1);
458-
if (0 != strcmp(buf, libver))
459-
{
460-
if (errbuf)
461-
snprintf(errbuf, errsize,
462-
"extension '%s' required, found extension '%s'",
463-
buf, libver);
464-
goto cleanup;
465-
}
466-
}
467-
else
468-
{
469-
if (sqlstate_equals(res, SQLSTATE_INVALID_SCHEMA_NAME)
470-
|| sqlstate_equals(res, SQLSTATE_UNDEFINED_FUNCTION))
471-
{
472-
/* Schema repack does not exist, or version too old (version
473-
* functions not found). Skip the database.
474-
*/
475-
if (errbuf)
476-
snprintf(errbuf, errsize,
477-
"%s %s is not installed in the database",
478-
PROGRAM_NAME, PROGRAM_VERSION);
479-
}
480-
else
481-
{
482-
/* Return the error message otherwise */
483-
if (errbuf)
484-
snprintf(errbuf, errsize, "%s", PQerrorMessage(connection));
485-
}
515+
if (!preliminary_checks(errbuf, errsize))
486516
goto cleanup;
487-
}
488-
CLEARPGRES(res);
489-
490-
/* Disable statement timeout. */
491-
command("SET statement_timeout = 0", 0, NULL);
492-
493-
/* Restrict search_path to system catalog. */
494-
command("SET search_path = pg_catalog, pg_temp, public", 0, NULL);
495-
496-
/* To avoid annoying "create implicit ..." messages. */
497-
command("SET client_min_messages = warning", 0, NULL);
498517

499518
/* acquire target tables */
500519
appendStringInfoString(&sql,

0 commit comments

Comments
 (0)