Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 522ed12

Browse files
committed
Add "--exclude-extension" to pg_dump's options.
This option (or equivalently specifying "exclude extension pattern" in a filter file) allows extensions matching the specified pattern to be excluded from the dump. Ayush Vatsa, reviewed by Junwang Zhao, Dean Rasheed, and Daniel Gustafsson. Discussion: https://postgr.es/m/CACX+KaP=VgVy9h-EUh598DTu+-fNr1jyEmpghC8rRp9s=w33Kg@mail.gmail.com
1 parent d63d486 commit 522ed12

File tree

3 files changed

+139
-16
lines changed

3 files changed

+139
-16
lines changed

doc/src/sgml/ref/pg_dump.sgml

+28-6
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,27 @@ PostgreSQL documentation
256256
</listitem>
257257
</varlistentry>
258258

259+
<varlistentry>
260+
<term><option>--exclude-extension=<replaceable class="parameter">pattern</replaceable></option></term>
261+
<listitem>
262+
<para>
263+
Do not dump any extensions matching <replaceable
264+
class="parameter">pattern</replaceable>. The pattern is
265+
interpreted according to the same rules as for <option>-e</option>.
266+
<option>--exclude-extension</option> can be given more than once to exclude extensions
267+
matching any of several patterns.
268+
</para>
269+
270+
<para>
271+
When both <option>-e</option> and <option>--exclude-extension</option> are given, the behavior
272+
is to dump just the extensions that match at least one <option>-e</option>
273+
switch but no <option>--exclude-extension</option> switches. If <option>--exclude-extension</option>
274+
appears without <option>-e</option>, then extensions matching <option>--exclude-extension</option> are
275+
excluded from what is otherwise a normal dump.
276+
</para>
277+
</listitem>
278+
</varlistentry>
279+
259280
<varlistentry>
260281
<term><option>-E <replaceable class="parameter">encoding</replaceable></option></term>
261282
<term><option>--encoding=<replaceable class="parameter">encoding</replaceable></option></term>
@@ -848,10 +869,11 @@ PostgreSQL documentation
848869
<option>--exclude-table-and-children</option> or
849870
<option>-T</option> for tables,
850871
<option>-n</option>/<option>--schema</option> for schemas,
851-
<option>--include-foreign-data</option> for data on foreign servers and
872+
<option>--include-foreign-data</option> for data on foreign servers,
852873
<option>--exclude-table-data</option>,
853-
<option>--exclude-table-data-and-children</option> for table data,
854-
<option>-e</option>/<option>--extension</option> for extensions.
874+
<option>--exclude-table-data-and-children</option> for table data, and
875+
<option>-e</option>/<option>--extension</option> or
876+
<option>--exclude-extension</option> for extensions.
855877
To read from <literal>STDIN</literal>, use <filename>-</filename> as the
856878
filename. The <option>--filter</option> option can be specified in
857879
conjunction with the above listed options for including or excluding
@@ -874,8 +896,7 @@ PostgreSQL documentation
874896
<listitem>
875897
<para>
876898
<literal>extension</literal>: extensions, works like the
877-
<option>--extension</option> option. This keyword can only be
878-
used with the <literal>include</literal> keyword.
899+
<option>-e</option>/<option>--extension</option> option.
879900
</para>
880901
</listitem>
881902
<listitem>
@@ -1278,7 +1299,8 @@ PostgreSQL documentation
12781299
</para>
12791300
<para>
12801301
This option has no effect
1281-
on <option>-N</option>/<option>--exclude-schema</option>,
1302+
on <option>--exclude-extension</option>,
1303+
<option>-N</option>/<option>--exclude-schema</option>,
12821304
<option>-T</option>/<option>--exclude-table</option>,
12831305
or <option>--exclude-table-data</option>. An exclude pattern failing
12841306
to match any objects is not considered an error.

src/bin/pg_dump/pg_dump.c

+32-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ static SimpleOidList foreign_servers_include_oids = {NULL, NULL};
136136
static SimpleStringList extension_include_patterns = {NULL, NULL};
137137
static SimpleOidList extension_include_oids = {NULL, NULL};
138138

139+
static SimpleStringList extension_exclude_patterns = {NULL, NULL};
140+
static SimpleOidList extension_exclude_oids = {NULL, NULL};
141+
139142
static const CatalogId nilCatalogId = {0, 0};
140143

141144
/* override for standard extra_float_digits setting */
@@ -437,6 +440,7 @@ main(int argc, char **argv)
437440
{"exclude-table-data-and-children", required_argument, NULL, 14},
438441
{"sync-method", required_argument, NULL, 15},
439442
{"filter", required_argument, NULL, 16},
443+
{"exclude-extension", required_argument, NULL, 17},
440444

441445
{NULL, 0, NULL, 0}
442446
};
@@ -672,6 +676,11 @@ main(int argc, char **argv)
672676
read_dump_filters(optarg, &dopt);
673677
break;
674678

679+
case 17: /* exclude extension(s) */
680+
simple_string_list_append(&extension_exclude_patterns,
681+
optarg);
682+
break;
683+
675684
default:
676685
/* getopt_long already emitted a complaint */
677686
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
@@ -890,6 +899,10 @@ main(int argc, char **argv)
890899
if (extension_include_oids.head == NULL)
891900
pg_fatal("no matching extensions were found");
892901
}
902+
expand_extension_name_patterns(fout, &extension_exclude_patterns,
903+
&extension_exclude_oids,
904+
false);
905+
/* non-matching exclusion patterns aren't an error */
893906

894907
/*
895908
* Dumping LOs is the default for dumps where an inclusion switch is not
@@ -1095,6 +1108,7 @@ help(const char *progname)
10951108
printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
10961109
printf(_(" -C, --create include commands to create database in dump\n"));
10971110
printf(_(" -e, --extension=PATTERN dump the specified extension(s) only\n"));
1111+
printf(_(" --exclude-extension=PATTERN do NOT dump the specified extension(s)\n"));
10981112
printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
10991113
printf(_(" -n, --schema=PATTERN dump the specified schema(s) only\n"));
11001114
printf(_(" -N, --exclude-schema=PATTERN do NOT dump the specified schema(s)\n"));
@@ -2028,6 +2042,12 @@ selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt)
20282042
extinfo->dobj.dump = extinfo->dobj.dump_contains =
20292043
dopt->include_everything ?
20302044
DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
2045+
2046+
/* check that the extension is not explicitly excluded */
2047+
if (extinfo->dobj.dump &&
2048+
simple_oid_list_member(&extension_exclude_oids,
2049+
extinfo->dobj.catId.oid))
2050+
extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_NONE;
20312051
}
20322052
}
20332053

@@ -18265,6 +18285,15 @@ processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
1826518285
curext->dobj.catId.oid))
1826618286
continue;
1826718287

18288+
/*
18289+
* Check if this extension is listed as to exclude in the dump. If
18290+
* yes, any table data associated with it is discarded.
18291+
*/
18292+
if (extension_exclude_oids.head != NULL &&
18293+
simple_oid_list_member(&extension_exclude_oids,
18294+
curext->dobj.catId.oid))
18295+
continue;
18296+
1826818297
if (strlen(extconfig) != 0 || strlen(extcondition) != 0)
1826918298
{
1827018299
int j;
@@ -18965,14 +18994,16 @@ read_dump_filters(const char *filename, DumpOptions *dopt)
1896518994
case FILTER_OBJECT_TYPE_FUNCTION:
1896618995
case FILTER_OBJECT_TYPE_INDEX:
1896718996
case FILTER_OBJECT_TYPE_TRIGGER:
18968-
case FILTER_OBJECT_TYPE_EXTENSION:
1896918997
case FILTER_OBJECT_TYPE_FOREIGN_DATA:
1897018998
pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
1897118999
"exclude",
1897219000
filter_object_type_name(objtype));
1897319001
exit_nicely(1);
1897419002
break;
1897519003

19004+
case FILTER_OBJECT_TYPE_EXTENSION:
19005+
simple_string_list_append(&extension_exclude_patterns, objname);
19006+
break;
1897619007
case FILTER_OBJECT_TYPE_TABLE_DATA:
1897719008
simple_string_list_append(&tabledata_exclude_patterns,
1897819009
objname);

src/test/modules/test_pg_dump/t/001_base.pl

+79-9
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,19 @@
220220
'--extension=test_pg_dump', 'postgres',
221221
],
222222
},
223+
exclude_extension => {
224+
dump_cmd => [
225+
'pg_dump', '--no-sync', "--file=$tempdir/exclude_extension.sql",
226+
'--exclude-extension=test_pg_dump', 'postgres',
227+
],
228+
},
229+
exclude_extension_filter => {
230+
dump_cmd => [
231+
'pg_dump', '--no-sync',
232+
"--file=$tempdir/exclude_extension_filter.sql",
233+
"--filter=$tempdir/exclude_extension_filter.txt", 'postgres',
234+
],
235+
},
223236

224237
# plpgsql in the list blocks the dump of extension test_pg_dump
225238
without_extension => {
@@ -299,6 +312,8 @@
299312
no_owner => 1,
300313
privileged_internals => 1,
301314
with_extension => 1,
315+
exclude_extension => 1,
316+
exclude_extension_filter => 1,
302317
without_extension => 1);
303318

304319
my %tests = (
@@ -325,7 +340,12 @@
325340
schema_only => 1,
326341
section_pre_data => 1,
327342
},
328-
unlike => { binary_upgrade => 1, without_extension => 1 },
343+
unlike => {
344+
binary_upgrade => 1,
345+
exclude_extension => 1,
346+
exclude_extension_filter => 1,
347+
without_extension => 1
348+
},
329349
},
330350
331351
'CREATE ROLE regress_dump_test_role' => {
@@ -434,7 +454,11 @@
434454
section_data => 1,
435455
extension_schema => 1,
436456
},
437-
unlike => { without_extension => 1, },
457+
unlike => {
458+
exclude_extension => 1,
459+
exclude_extension_filter => 1,
460+
without_extension => 1,
461+
},
438462
},
439463
440464
'CREATE TABLE regress_pg_dump_table' => {
@@ -460,6 +484,8 @@
460484
unlike => {
461485
binary_upgrade => 1,
462486
exclude_table => 1,
487+
exclude_extension => 1,
488+
exclude_extension_filter => 1,
463489
without_extension => 1,
464490
},
465491
},
@@ -483,7 +509,12 @@
483509
schema_only => 1,
484510
section_pre_data => 1,
485511
},
486-
unlike => { no_privs => 1, without_extension => 1, },
512+
unlike => {
513+
no_privs => 1,
514+
exclude_extension => 1,
515+
exclude_extension_filter => 1,
516+
without_extension => 1,
517+
},
487518
},
488519
489520
'REVOKE GRANT OPTION FOR UPDATE ON SEQUENCE wgo_then_regular' => {
@@ -500,7 +531,12 @@
500531
schema_only => 1,
501532
section_pre_data => 1,
502533
},
503-
unlike => { no_privs => 1, without_extension => 1, },
534+
unlike => {
535+
no_privs => 1,
536+
exclude_extension => 1,
537+
exclude_extension_filter => 1,
538+
without_extension => 1,
539+
},
504540
},
505541
506542
'CREATE ACCESS METHOD regress_test_am' => {
@@ -520,7 +556,11 @@
520556
schema_only => 1,
521557
section_pre_data => 1,
522558
},
523-
unlike => { without_extension => 1, },
559+
unlike => {
560+
exclude_extension => 1,
561+
exclude_extension_filter => 1,
562+
without_extension => 1,
563+
},
524564
},
525565
526566
'GRANT SELECT regress_pg_dump_table_added pre-ALTER EXTENSION' => {
@@ -545,7 +585,12 @@
545585
schema_only => 1,
546586
section_pre_data => 1,
547587
},
548-
unlike => { no_privs => 1, without_extension => 1, },
588+
unlike => {
589+
no_privs => 1,
590+
exclude_extension => 1,
591+
exclude_extension_filter => 1,
592+
without_extension => 1,
593+
},
549594
},
550595
551596
'GRANT SELECT ON TABLE regress_pg_dump_table' => {
@@ -579,7 +624,12 @@
579624
schema_only => 1,
580625
section_pre_data => 1,
581626
},
582-
unlike => { no_privs => 1, without_extension => 1 },
627+
unlike => {
628+
no_privs => 1,
629+
exclude_extension => 1,
630+
exclude_extension_filter => 1,
631+
without_extension => 1
632+
},
583633
},
584634
585635
'GRANT USAGE ON regress_pg_dump_table_col1_seq TO regress_dump_test_role'
@@ -595,7 +645,12 @@
595645
schema_only => 1,
596646
section_pre_data => 1,
597647
},
598-
unlike => { no_privs => 1, without_extension => 1, },
648+
unlike => {
649+
no_privs => 1,
650+
exclude_extension => 1,
651+
exclude_extension_filter => 1,
652+
without_extension => 1,
653+
},
599654
},
600655
601656
'GRANT USAGE ON regress_pg_dump_seq TO regress_dump_test_role' => {
@@ -617,7 +672,12 @@
617672
schema_only => 1,
618673
section_pre_data => 1,
619674
},
620-
unlike => { no_privs => 1, without_extension => 1, },
675+
unlike => {
676+
no_privs => 1,
677+
exclude_extension => 1,
678+
exclude_extension_filter => 1,
679+
without_extension => 1,
680+
},
621681
},
622682
623683
# Objects included in extension part of a schema created by this extension */
@@ -818,6 +878,16 @@
818878
# Send the combined set of commands to psql
819879
$node->safe_psql('postgres', $create_sql);
820880
881+
#########################################
882+
# Create filter file for exclude_extension_filter test
883+
884+
my $filterfile;
885+
886+
open $filterfile, '>', "$tempdir/exclude_extension_filter.txt"
887+
or die "unable to open filter file for writing";
888+
print $filterfile "exclude extension test_pg_dump\n";
889+
close $filterfile;
890+
821891
#########################################
822892
# Run all runs
823893

0 commit comments

Comments
 (0)