From 471775d8b6a4bb90dd0570a4c834e9cddd5fefa3 Mon Sep 17 00:00:00 2001 From: Ayush Vatsa Date: Mon, 25 Dec 2023 14:46:05 +0530 Subject: [PATCH] Add support for --exclude-extension in pg_dump When specified, extensions matching the given pattern are excluded in dumps. --- doc/src/sgml/ref/pg_dump.sgml | 24 +++++++++++++++++++++++- src/bin/pg_dump/pg_dump.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml index 0e5ba4f712..b3f3ff3188 100644 --- a/doc/src/sgml/ref/pg_dump.sgml +++ b/doc/src/sgml/ref/pg_dump.sgml @@ -256,6 +256,27 @@ PostgreSQL documentation + + + + + Do not dump any extensions matching pattern. The pattern is + interpreted according to the same rules as for . + can be given more than once to exclude extensions + matching any of several patterns. + + + + When both and are given, the behavior + is to dump just the extensions that match at least one + switch but no switches. If + appears without , then extensions matching are + excluded from what is otherwise a normal dump. + + + + @@ -1278,7 +1299,8 @@ PostgreSQL documentation This option has no effect - on /, + on , + /, /, or . An exclude pattern failing to match any objects is not considered an error. diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 8c0b5486b9..1185c74a54 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -136,6 +136,9 @@ static SimpleOidList foreign_servers_include_oids = {NULL, NULL}; static SimpleStringList extension_include_patterns = {NULL, NULL}; static SimpleOidList extension_include_oids = {NULL, NULL}; +static SimpleStringList extension_exclude_patterns = {NULL, NULL}; +static SimpleOidList extension_exclude_oids = {NULL, NULL}; + static const CatalogId nilCatalogId = {0, 0}; /* override for standard extra_float_digits setting */ @@ -436,6 +439,7 @@ main(int argc, char **argv) {"exclude-table-data-and-children", required_argument, NULL, 14}, {"sync-method", required_argument, NULL, 15}, {"filter", required_argument, NULL, 16}, + {"exclude-extension", required_argument, NULL, 17}, {NULL, 0, NULL, 0} }; @@ -671,6 +675,11 @@ main(int argc, char **argv) read_dump_filters(optarg, &dopt); break; + case 17: /* exclude extension(s) */ + simple_string_list_append(&extension_exclude_patterns, + optarg); + break; + default: /* getopt_long already emitted a complaint */ pg_log_error_hint("Try \"%s --help\" for more information.", progname); @@ -889,6 +898,10 @@ main(int argc, char **argv) if (extension_include_oids.head == NULL) pg_fatal("no matching extensions were found"); } + expand_extension_name_patterns(fout, &extension_exclude_patterns, + &extension_exclude_oids, + false); + /* non-matching exclusion patterns aren't an error */ /* * Dumping LOs is the default for dumps where an inclusion switch is not @@ -1094,6 +1107,7 @@ help(const char *progname) printf(_(" -c, --clean clean (drop) database objects before recreating\n")); printf(_(" -C, --create include commands to create database in dump\n")); printf(_(" -e, --extension=PATTERN dump the specified extension(s) only\n")); + printf(_(" --exclude-extension=PATTERN do NOT dump the specified extension(s)\n")); printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n")); printf(_(" -n, --schema=PATTERN dump the specified schema(s) only\n")); printf(_(" -N, --exclude-schema=PATTERN do NOT dump the specified schema(s)\n")); @@ -2025,6 +2039,11 @@ selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt) extinfo->dobj.dump = extinfo->dobj.dump_contains = dopt->include_everything ? DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; + + if (extension_exclude_oids.head != NULL && + simple_oid_list_member(&extension_exclude_oids, + extinfo->dobj.catId.oid)) + extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_NONE; } } @@ -18149,6 +18168,15 @@ processExtensionTables(Archive *fout, ExtensionInfo extinfo[], !simple_oid_list_member(&extension_include_oids, curext->dobj.catId.oid)) continue; + + /* + * Check if this extension is listed as to exclude in the dump. If + * yes, any table data associated with it is discarded. + */ + if (extension_exclude_oids.head != NULL && + simple_oid_list_member(&extension_exclude_oids, + curext->dobj.catId.oid)) + continue; if (strlen(extconfig) != 0 || strlen(extcondition) != 0) { -- 2.41.0