@@ -61,6 +61,13 @@ typedef struct AmcheckOptions
61
61
bool show_progress ;
62
62
int jobs ;
63
63
64
+ /*
65
+ * Whether to install missing extensions, and optionally the name of the
66
+ * schema in which to install the extension's objects.
67
+ */
68
+ bool install_missing ;
69
+ char * install_schema ;
70
+
64
71
/* Objects to check or not to check, as lists of PatternInfo structs. */
65
72
PatternInfoArray include ;
66
73
PatternInfoArray exclude ;
@@ -109,6 +116,8 @@ static AmcheckOptions opts = {
109
116
.strict_names = true,
110
117
.show_progress = false,
111
118
.jobs = 1 ,
119
+ .install_missing = false,
120
+ .install_schema = "pg_catalog" ,
112
121
.include = {NULL , 0 },
113
122
.exclude = {NULL , 0 },
114
123
.excludetbl = false,
@@ -259,6 +268,7 @@ main(int argc, char *argv[])
259
268
{"no-strict-names" , no_argument , NULL , 10 },
260
269
{"heapallindexed" , no_argument , NULL , 11 },
261
270
{"parent-check" , no_argument , NULL , 12 },
271
+ {"install-missing" , optional_argument , NULL , 13 },
262
272
263
273
{NULL , 0 , NULL , 0 }
264
274
};
@@ -435,6 +445,11 @@ main(int argc, char *argv[])
435
445
case 12 :
436
446
opts .parent_check = true;
437
447
break ;
448
+ case 13 :
449
+ opts .install_missing = true;
450
+ if (optarg )
451
+ opts .install_schema = pg_strdup (optarg );
452
+ break ;
438
453
default :
439
454
fprintf (stderr ,
440
455
_ ("Try \"%s --help\" for more information.\n" ),
@@ -543,6 +558,29 @@ main(int argc, char *argv[])
543
558
conn = connectDatabase (& cparams , progname , opts .echo , false, true);
544
559
}
545
560
561
+ /*
562
+ * Optionally install amcheck if not already installed in this
563
+ * database.
564
+ */
565
+ if (opts .install_missing )
566
+ {
567
+ char * schema ;
568
+ char * install_sql ;
569
+
570
+ /*
571
+ * Must re-escape the schema name for each database, as the
572
+ * escaping rules may change.
573
+ */
574
+ schema = PQescapeIdentifier (conn , opts .install_schema ,
575
+ strlen (opts .install_schema ));
576
+ install_sql = psprintf ("CREATE EXTENSION IF NOT EXISTS amcheck WITH SCHEMA %s" ,
577
+ schema );
578
+
579
+ executeCommand (conn , install_sql , opts .echo );
580
+ pfree (install_sql );
581
+ pfree (schema );
582
+ }
583
+
546
584
/*
547
585
* Verify that amcheck is installed for this next database. User
548
586
* error could result in a database not having amcheck that should
@@ -1153,6 +1191,7 @@ help(const char *progname)
1153
1191
printf (_ (" -V, --version output version information, then exit\n" ));
1154
1192
printf (_ (" -P, --progress show progress information\n" ));
1155
1193
printf (_ (" -?, --help show this help, then exit\n" ));
1194
+ printf (_ (" --install-missing install missing extensions\n" ));
1156
1195
1157
1196
printf (_ ("\nReport bugs to <%s>.\n" ), PACKAGE_BUGREPORT );
1158
1197
printf (_ ("%s home page: <%s>\n" ), PACKAGE_NAME , PACKAGE_URL );
0 commit comments