diff options
author | Andrew Dunstan <andrew@dunslane.net> | 2021-04-24 10:13:07 -0400 |
---|---|---|
committer | Andrew Dunstan <andrew@dunslane.net> | 2021-04-24 10:13:07 -0400 |
commit | b859d94c638968ccbb517ac7e151bdd94ed7c16a (patch) | |
tree | 6f2b0c5d482919addd0d4a2c1fb019ba1533c01b | |
parent | aa271209f6d995488fc5cba9731415f974823990 (diff) | |
download | postgresql-b859d94c638968ccbb517ac7e151bdd94ed7c16a.tar.gz postgresql-b859d94c638968ccbb517ac7e151bdd94ed7c16a.zip |
Provide pg_amcheck with an --install-missing option
This will install amcheck in the database if not present. The default
schema is for the extension is pg_catalog, but this can be overridden by
providing a value for the option.
Mark Dilger, slightly editorialized by me.
(rather divergent)
Discussion: https://postgr.es/m/bdc0f7c2-09e3-ee57-8471-569dfb509234@dunslane.net
-rw-r--r-- | doc/src/sgml/ref/pg_amcheck.sgml | 17 | ||||
-rw-r--r-- | src/bin/pg_amcheck/pg_amcheck.c | 39 |
2 files changed, 56 insertions, 0 deletions
diff --git a/doc/src/sgml/ref/pg_amcheck.sgml b/doc/src/sgml/ref/pg_amcheck.sgml index d01e26faa81..d4989c9f231 100644 --- a/doc/src/sgml/ref/pg_amcheck.sgml +++ b/doc/src/sgml/ref/pg_amcheck.sgml @@ -218,6 +218,23 @@ PostgreSQL documentation </varlistentry> <varlistentry> + <term><option>--install-missing</option></term> + <term><option>--install-missing=<replaceable class="parameter">schema</replaceable></option></term> + <listitem> + <para> + Install any missing extensions that are required to check the + database(s). If not yet installed, each extension's objects will be + installed into the given + <replaceable class="parameter">schema</replaceable>, or if not specified + into schema <literal>pg_catalog</literal>. + </para> + <para> + At present, the only required extension is <xref linkend="amcheck"/>. + </para> + </listitem> + </varlistentry> + + <varlistentry> <term><option>-j <replaceable class="parameter">num</replaceable></option></term> <term><option>--jobs=<replaceable class="parameter">num</replaceable></option></term> <listitem> diff --git a/src/bin/pg_amcheck/pg_amcheck.c b/src/bin/pg_amcheck/pg_amcheck.c index bfe3d8e5949..09ebb4929a7 100644 --- a/src/bin/pg_amcheck/pg_amcheck.c +++ b/src/bin/pg_amcheck/pg_amcheck.c @@ -61,6 +61,13 @@ typedef struct AmcheckOptions bool show_progress; int jobs; + /* + * Whether to install missing extensions, and optionally the name of the + * schema in which to install the extension's objects. + */ + bool install_missing; + char *install_schema; + /* Objects to check or not to check, as lists of PatternInfo structs. */ PatternInfoArray include; PatternInfoArray exclude; @@ -109,6 +116,8 @@ static AmcheckOptions opts = { .strict_names = true, .show_progress = false, .jobs = 1, + .install_missing = false, + .install_schema = "pg_catalog", .include = {NULL, 0}, .exclude = {NULL, 0}, .excludetbl = false, @@ -259,6 +268,7 @@ main(int argc, char *argv[]) {"no-strict-names", no_argument, NULL, 10}, {"heapallindexed", no_argument, NULL, 11}, {"parent-check", no_argument, NULL, 12}, + {"install-missing", optional_argument, NULL, 13}, {NULL, 0, NULL, 0} }; @@ -435,6 +445,11 @@ main(int argc, char *argv[]) case 12: opts.parent_check = true; break; + case 13: + opts.install_missing = true; + if (optarg) + opts.install_schema = pg_strdup(optarg); + break; default: fprintf(stderr, _("Try \"%s --help\" for more information.\n"), @@ -544,6 +559,29 @@ main(int argc, char *argv[]) } /* + * Optionally install amcheck if not already installed in this + * database. + */ + if (opts.install_missing) + { + char *schema; + char *install_sql; + + /* + * Must re-escape the schema name for each database, as the + * escaping rules may change. + */ + schema = PQescapeIdentifier(conn, opts.install_schema, + strlen(opts.install_schema)); + install_sql = psprintf("CREATE EXTENSION IF NOT EXISTS amcheck WITH SCHEMA %s", + schema); + + executeCommand(conn, install_sql, opts.echo); + pfree(install_sql); + pfree(schema); + } + + /* * Verify that amcheck is installed for this next database. User * error could result in a database not having amcheck that should * have it, but we also could be iterating over multiple databases @@ -1153,6 +1191,7 @@ help(const char *progname) printf(_(" -V, --version output version information, then exit\n")); printf(_(" -P, --progress show progress information\n")); printf(_(" -?, --help show this help, then exit\n")); + printf(_(" --install-missing install missing extensions\n")); printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT); printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL); |