@@ -173,6 +173,7 @@ typedef struct repack_index
173
173
174
174
static bool is_superuser (void );
175
175
static void check_tablespace (void );
176
+ static bool preliminary_checks (char * errbuf , size_t errsize );
176
177
static void repack_all_databases (const char * order_by );
177
178
static bool repack_one_database (const char * order_by , char * errbuf , size_t errsize );
178
179
static void repack_one_table (const repack_table * table , const char * order_by );
@@ -339,6 +340,93 @@ check_tablespace()
339
340
CLEARPGRES (res );
340
341
}
341
342
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
+ }
342
430
343
431
/*
344
432
* Call repack_one_database for each database.
@@ -424,77 +512,8 @@ repack_one_database(const char *orderby, char *errbuf, size_t errsize)
424
512
if (jobs > 1 )
425
513
setup_workers (jobs );
426
514
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 ))
486
516
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 );
498
517
499
518
/* acquire target tables */
500
519
appendStringInfoString (& sql ,
0 commit comments