Skip to content

Commit a4cd6ab

Browse files
committed
Add --section option to pg_dump and pg_restore.
Valid values are --pre-data, data and post-data. The option can be given more than once. --schema-only is equivalent to --section=pre-data --section=post-data. --data-only is equivalent to --section=data. Andrew Dunstan, reviewed by Joachim Wieland and Josh Berkus.
1 parent 4b43b48 commit a4cd6ab

File tree

10 files changed

+184
-6
lines changed

10 files changed

+184
-6
lines changed

doc/src/sgml/ref/pg_dump.sgml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ PostgreSQL documentation
116116
</para>
117117

118118
<para>
119-
This option is only meaningful for the plain-text format. For
120-
the archive formats, you can specify the option when you
121-
call <command>pg_restore</command>.
119+
This option is equivalent to specifying <option>--section=data</>.
122120
</para>
123121
</listitem>
124122
</varlistentry>
@@ -408,9 +406,29 @@ PostgreSQL documentation
408406
To exclude table data for only a subset of tables in the database,
409407
see <option>--exclude-table-data</>.
410408
</para>
409+
<para>
410+
This option is equivalent to specifying
411+
<option>--section=pre-data --section=post-data</>.
412+
</para>
411413
</listitem>
412414
</varlistentry>
413415

416+
<varlistentry>
417+
<term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
418+
<listitem>
419+
<para>
420+
Only dump the named section. The name can be one of <option>pre-data</>, <option>data</>
421+
and <option>post-data</>.
422+
This option can be specified more than once. The default is to dump all sections.
423+
</para>
424+
<para>
425+
Post-data items consist of definitions of indexes, triggers, rules
426+
and constraints other than check constraints.
427+
Pre-data items consist of all other data definition items.
428+
</para>
429+
</listitem>
430+
</varlistentry>
431+
414432
<varlistentry>
415433
<term><option>-S <replaceable class="parameter">username</replaceable></option></term>
416434
<term><option>--superuser=<replaceable class="parameter">username</replaceable></option></term>

doc/src/sgml/ref/pg_restore.sgml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@
9393
<para>
9494
Restore only the data, not the schema (data definitions).
9595
</para>
96+
<para>
97+
This option is equivalent to specifying <option>--section=data</>.
98+
</para>
9699
</listitem>
97100
</varlistentry>
98101

@@ -359,6 +362,10 @@
359362
(Do not confuse this with the <option>--schema</> option, which
360363
uses the word <quote>schema</> in a different meaning.)
361364
</para>
365+
<para>
366+
This option is equivalent to specifying
367+
<option>--section=pre-data --section=post-data</>.
368+
</para>
362369
</listitem>
363370
</varlistentry>
364371

@@ -504,6 +511,22 @@
504511
</listitem>
505512
</varlistentry>
506513

514+
<varlistentry>
515+
<term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
516+
<listitem>
517+
<para>
518+
Only restore the named section. The name can be one of <option>pre-data</>, <option>data</>
519+
and <option>post-data</>.
520+
This option can be specified more than once. The default is to restore all sections.
521+
</para>
522+
<para>
523+
Post-data items consist of definitions of indexes, triggers, rules
524+
and constraints other than check constraints.
525+
Pre-data items consist of all other data definition items.
526+
</para>
527+
</listitem>
528+
</varlistentry>
529+
507530
<varlistentry>
508531
<term><option>--use-set-session-authorization</option></term>
509532
<listitem>

src/bin/pg_dump/common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* common.c
44
* Catalog routines used by pg_dump; long ago these were shared
5-
* by another dump tool, but not anymore.
5+
* by another dump tool, but not anymore.
66
*
77
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California

src/bin/pg_dump/dumputils.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <ctype.h>
1818

1919
#include "dumputils.h"
20+
#include "pg_backup.h"
2021

2122
#include "parser/keywords.h"
2223

@@ -1262,3 +1263,32 @@ exit_horribly(const char *modulename, const char *fmt,...)
12621263

12631264
exit(1);
12641265
}
1266+
1267+
/*
1268+
* Set the bitmask in dumpSections according to the first argument.
1269+
* dumpSections is initialised as DUMP_UNSECTIONED by pg_dump and
1270+
* pg_restore so they can know if this has even been called.
1271+
*/
1272+
1273+
void
1274+
set_section (const char *arg, int *dumpSections)
1275+
{
1276+
/* if this is the first, clear all the bits */
1277+
if (*dumpSections == DUMP_UNSECTIONED)
1278+
*dumpSections = 0;
1279+
1280+
if (strcmp(arg,"pre-data") == 0)
1281+
*dumpSections |= DUMP_PRE_DATA;
1282+
else if (strcmp(arg,"data") == 0)
1283+
*dumpSections |= DUMP_DATA;
1284+
else if (strcmp(arg,"post-data") == 0)
1285+
*dumpSections |= DUMP_POST_DATA;
1286+
else
1287+
{
1288+
fprintf(stderr, _("%s: unknown section name \"%s\")\n"),
1289+
progname, arg);
1290+
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
1291+
progname);
1292+
exit(1);
1293+
}
1294+
}

src/bin/pg_dump/dumputils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,6 @@ extern void vwrite_msg(const char *modulename, const char *fmt, va_list ap)
5858
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0)));
5959
extern void exit_horribly(const char *modulename, const char *fmt,...)
6060
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
61+
extern void set_section (const char *arg, int *dumpSections);
6162

6263
#endif /* DUMPUTILS_H */

src/bin/pg_dump/pg_backup.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ typedef enum _teSection
6969
SECTION_POST_DATA /* stuff to be processed after data */
7070
} teSection;
7171

72+
typedef enum
73+
{
74+
DUMP_PRE_DATA = 0x01,
75+
DUMP_DATA = 0x02,
76+
DUMP_POST_DATA = 0x04,
77+
DUMP_UNSECTIONED = 0xff
78+
} DumpSections;
79+
7280
/*
7381
* We may want to have some more user-readable data, but in the mean
7482
* time this gives us some abstraction and type checking.
@@ -111,6 +119,7 @@ typedef struct _restoreOptions
111119
int dropSchema;
112120
char *filename;
113121
int schemaOnly;
122+
int dumpSections;
114123
int verbose;
115124
int aclsSkip;
116125
int tocSummary;

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ NewRestoreOptions(void)
665665
/* set any fields that shouldn't default to zeroes */
666666
opts->format = archUnknown;
667667
opts->promptPassword = TRI_DEFAULT;
668+
opts->dumpSections = DUMP_UNSECTIONED;
668669

669670
return opts;
670671
}
@@ -2120,6 +2121,7 @@ ReadToc(ArchiveHandle *AH)
21202121
int depIdx;
21212122
int depSize;
21222123
TocEntry *te;
2124+
bool in_post_data = false;
21232125

21242126
AH->tocCount = ReadInt(AH);
21252127
AH->maxDumpId = 0;
@@ -2185,6 +2187,12 @@ ReadToc(ArchiveHandle *AH)
21852187
te->section = SECTION_PRE_DATA;
21862188
}
21872189

2190+
/* will stay true even for SECTION_NONE items */
2191+
if (te->section == SECTION_POST_DATA)
2192+
in_post_data = true;
2193+
2194+
te->inPostData = in_post_data;
2195+
21882196
te->defn = ReadStr(AH);
21892197
te->dropStmt = ReadStr(AH);
21902198

@@ -2334,6 +2342,17 @@ _tocEntryRequired(TocEntry *te, RestoreOptions *ropt, bool include_acls)
23342342
if (!ropt->createDB && strcmp(te->desc, "DATABASE") == 0)
23352343
return 0;
23362344

2345+
/* skip (all but) post data section as required */
2346+
/* table data is filtered if necessary lower down */
2347+
if (ropt->dumpSections != DUMP_UNSECTIONED)
2348+
{
2349+
if (!(ropt->dumpSections & DUMP_POST_DATA) && te->inPostData)
2350+
return 0;
2351+
if (!(ropt->dumpSections & DUMP_PRE_DATA) && ! te->inPostData && strcmp(te->desc, "TABLE DATA") != 0)
2352+
return 0;
2353+
}
2354+
2355+
23372356
/* Check options for selective dump/restore */
23382357
if (ropt->schemaNames)
23392358
{

src/bin/pg_dump/pg_backup_archiver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ typedef struct _tocEntry
287287
void *dataDumperArg; /* Arg for above routine */
288288
void *formatData; /* TOC Entry data specific to file format */
289289

290+
/* in post data? not quite the same as section, might be SECTION_NONE */
291+
bool inPostData;
292+
290293
/* working state (needed only for parallel restore) */
291294
struct _tocEntry *par_prev; /* list links for pending/ready items; */
292295
struct _tocEntry *par_next; /* these are NULL if not in either list */

src/bin/pg_dump/pg_dump.c

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ PGconn *g_conn; /* the database connection */
9191
/* various user-settable parameters */
9292
bool schemaOnly;
9393
bool dataOnly;
94+
int dumpSections; /* bitmask of chosen sections */
9495
bool aclsSkip;
9596
const char *lockWaitTimeout;
9697

@@ -250,7 +251,6 @@ static void do_sql_command(PGconn *conn, const char *query);
250251
static void check_sql_result(PGresult *res, PGconn *conn, const char *query,
251252
ExecStatusType expected);
252253

253-
254254
int
255255
main(int argc, char **argv)
256256
{
@@ -332,6 +332,7 @@ main(int argc, char **argv)
332332
{"no-tablespaces", no_argument, &outputNoTablespaces, 1},
333333
{"quote-all-identifiers", no_argument, &quote_all_identifiers, 1},
334334
{"role", required_argument, NULL, 3},
335+
{"section", required_argument, NULL, 5},
335336
{"serializable-deferrable", no_argument, &serializable_deferrable, 1},
336337
{"use-set-session-authorization", no_argument, &use_setsessauth, 1},
337338
{"no-security-labels", no_argument, &no_security_labels, 1},
@@ -349,6 +350,7 @@ main(int argc, char **argv)
349350
strcpy(g_opaque_type, "opaque");
350351

351352
dataOnly = schemaOnly = false;
353+
dumpSections = DUMP_UNSECTIONED;
352354
lockWaitTimeout = NULL;
353355

354356
progname = get_progname(argv[0]);
@@ -494,6 +496,10 @@ main(int argc, char **argv)
494496
simple_string_list_append(&tabledata_exclude_patterns, optarg);
495497
break;
496498

499+
case 5: /* section */
500+
set_section(optarg, &dumpSections);
501+
break;
502+
497503
default:
498504
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
499505
exit(1);
@@ -524,6 +530,22 @@ main(int argc, char **argv)
524530
exit(1);
525531
}
526532

533+
if ((dataOnly || schemaOnly) && dumpSections != DUMP_UNSECTIONED)
534+
{
535+
write_msg(NULL, "options -s/--schema-only and -a/--data-only cannot be used with --section\n");
536+
exit(1);
537+
}
538+
539+
if (dataOnly)
540+
dumpSections = DUMP_DATA;
541+
else if (schemaOnly)
542+
dumpSections = DUMP_PRE_DATA | DUMP_POST_DATA;
543+
else if ( dumpSections != DUMP_UNSECTIONED)
544+
{
545+
dataOnly = dumpSections == DUMP_DATA;
546+
schemaOnly = !(dumpSections & DUMP_DATA);
547+
}
548+
527549
if (dataOnly && outputClean)
528550
{
529551
write_msg(NULL, "options -c/--clean and -a/--data-only cannot be used together\n");
@@ -871,6 +893,7 @@ help(const char *progname)
871893
printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
872894
printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
873895
printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
896+
printf(_(" --section=SECTION dump named section (pre-data, data or post-data)\n"));
874897
printf(_(" --serializable-deferrable wait until the dump can run without anomalies\n"));
875898
printf(_(" --use-set-session-authorization\n"
876899
" use SET SESSION AUTHORIZATION commands instead of\n"
@@ -1107,7 +1130,7 @@ selectDumpableTable(TableInfo *tbinfo)
11071130
tbinfo->dobj.dumpdata = true;
11081131
else
11091132
tbinfo->dobj.dumpdata = false;
1110-
1133+
11111134
}
11121135

11131136
/*
@@ -7093,6 +7116,28 @@ collectComments(Archive *fout, CommentItem **items)
70937116
static void
70947117
dumpDumpableObject(Archive *fout, DumpableObject *dobj)
70957118
{
7119+
7120+
bool skip = false;
7121+
7122+
switch (dobj->objType)
7123+
{
7124+
case DO_INDEX:
7125+
case DO_TRIGGER:
7126+
case DO_CONSTRAINT:
7127+
case DO_FK_CONSTRAINT:
7128+
case DO_RULE:
7129+
skip = !(dumpSections & DUMP_POST_DATA);
7130+
break;
7131+
case DO_TABLE_DATA:
7132+
skip = !(dumpSections & DUMP_DATA);
7133+
break;
7134+
default:
7135+
skip = !(dumpSections & DUMP_PRE_DATA);
7136+
}
7137+
7138+
if (skip)
7139+
return;
7140+
70967141
switch (dobj->objType)
70977142
{
70987143
case DO_NAMESPACE:

src/bin/pg_dump/pg_restore.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ main(int argc, char **argv)
118118
{"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
119119
{"no-tablespaces", no_argument, &outputNoTablespaces, 1},
120120
{"role", required_argument, NULL, 2},
121+
{"section", required_argument, NULL, 3},
121122
{"use-set-session-authorization", no_argument, &use_setsessauth, 1},
122123
{"no-security-labels", no_argument, &no_security_labels, 1},
123124

@@ -272,6 +273,10 @@ main(int argc, char **argv)
272273
opts->use_role = optarg;
273274
break;
274275

276+
case 3: /* section */
277+
set_section(optarg, &(opts->dumpSections));
278+
break;
279+
275280
default:
276281
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
277282
exit(1);
@@ -294,6 +299,30 @@ main(int argc, char **argv)
294299
exit(1);
295300
}
296301

302+
if (opts->dataOnly && opts->schemaOnly)
303+
{
304+
fprintf(stderr, _("%s: options -s/--schema-only and -a/--data-only cannot be used together\n"),
305+
progname);
306+
exit(1);
307+
}
308+
309+
if ((opts->dataOnly || opts->schemaOnly) && (opts->dumpSections != DUMP_UNSECTIONED))
310+
{
311+
fprintf(stderr, _("%s: options -s/--schema-only and -a/--data-only cannot be used with --section\n"),
312+
progname);
313+
exit(1);
314+
}
315+
316+
if (opts->dataOnly)
317+
opts->dumpSections = DUMP_DATA;
318+
else if (opts->schemaOnly)
319+
opts->dumpSections = DUMP_PRE_DATA | DUMP_POST_DATA;
320+
else if ( opts->dumpSections != DUMP_UNSECTIONED)
321+
{
322+
opts->dataOnly = opts->dumpSections == DUMP_DATA;
323+
opts->schemaOnly = !(opts->dumpSections & DUMP_DATA);
324+
}
325+
297326
/* Should get at most one of -d and -f, else user is confused */
298327
if (opts->dbname)
299328
{
@@ -434,6 +463,7 @@ usage(const char *progname)
434463
" created\n"));
435464
printf(_(" --no-security-labels do not restore security labels\n"));
436465
printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
466+
printf(_(" --section=SECTION restore named section (pre-data, data or post-data)\n"));
437467
printf(_(" --use-set-session-authorization\n"
438468
" use SET SESSION AUTHORIZATION commands instead of\n"
439469
" ALTER OWNER commands to set ownership\n"));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy