Skip to content

Commit 9052cff

Browse files
committed
add pg_restore logic
1 parent 83aa5a9 commit 9052cff

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/bin/pg_dump/pg_backup_archiver.c

+11
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,17 @@ _tocEntryRequired(TocEntry *te, RestoreOptions *ropt, bool include_acls)
23852385
if (!ropt->createDB && strcmp(te->desc, "DATABASE") == 0)
23862386
return 0;
23872387

2388+
/* skip (all but) post data section as required */
2389+
/* table data is filtered if necessary lower down */
2390+
if (ropt->dumpSections != DUMP_UNSECTIONED)
2391+
{
2392+
if (!(ropt->dumpSections & DUMP_POST_DATA) && te->inPostData)
2393+
return 0;
2394+
if (!(ropt->dumpSections & DUMP_PRE_DATA) && ! te->inPostData && strcmp(te->desc, "TABLE DATA") != 0)
2395+
return 0;
2396+
}
2397+
2398+
23882399
/* Check options for selective dump/restore */
23892400
if (ropt->schemaNames)
23902401
{

src/bin/pg_dump/pg_restore.c

+57
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ extern int optind;
6161

6262

6363
static void usage(const char *progname);
64+
static void set_section (int *dumpSections, const char *arg);
6465

6566
typedef struct option optType;
6667

@@ -116,6 +117,7 @@ main(int argc, char **argv)
116117
{"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
117118
{"no-tablespaces", no_argument, &outputNoTablespaces, 1},
118119
{"role", required_argument, NULL, 2},
120+
{"section", required_argument, NULL, 3},
119121
{"use-set-session-authorization", no_argument, &use_setsessauth, 1},
120122
{"no-security-labels", no_argument, &no_security_labels, 1},
121123

@@ -270,6 +272,10 @@ main(int argc, char **argv)
270272
opts->use_role = optarg;
271273
break;
272274

275+
case 3: /* section */
276+
set_section(&(opts->dumpSections), optarg);
277+
break;
278+
273279
default:
274280
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
275281
exit(1);
@@ -292,6 +298,33 @@ main(int argc, char **argv)
292298
exit(1);
293299
}
294300

301+
if (opts->dataOnly && opts->schemaOnly)
302+
{
303+
fprintf(stderr, _("%s: options -s/--schema-only and -a/--data-only cannot be used together\n"),
304+
progname);
305+
exit(1);
306+
}
307+
308+
if ((opts->dataOnly || opts->schemaOnly) && (opts->dumpSections != DUMP_UNSECTIONED))
309+
{
310+
fprintf(stderr, _("%s: options -s/--schema-only and -a/--data-only cannot be used with --section\n"),
311+
progname);
312+
exit(1);
313+
}
314+
315+
if (opts->dataOnly)
316+
opts->dumpSections = DUMP_DATA;
317+
else if (opts->schemaOnly)
318+
opts->dumpSections = DUMP_PRE_DATA | DUMP_POST_DATA;
319+
else if ( opts->dumpSections != DUMP_UNSECTIONED)
320+
{
321+
opts->dataOnly = opts->dumpSections == DUMP_DATA;
322+
opts->schemaOnly = !(opts->dumpSections & DUMP_DATA);
323+
}
324+
325+
326+
fprintf(stderr,"dumpsection: %d, dataOnly: %d, schemaOnly: %d\n", opts->dumpSections, opts->dataOnly, opts->schemaOnly);
327+
295328
/* Should get at most one of -d and -f, else user is confused */
296329
if (opts->dbname)
297330
{
@@ -432,6 +465,7 @@ usage(const char *progname)
432465
" created\n"));
433466
printf(_(" --no-security-labels do not restore security labels\n"));
434467
printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
468+
printf(_(" --section=SECTION restore named section (pre-data, data or post-data)\n"));
435469
printf(_(" --use-set-session-authorization\n"
436470
" use SET SESSION AUTHORIZATION commands instead of\n"
437471
" ALTER OWNER commands to set ownership\n"));
@@ -447,3 +481,26 @@ usage(const char *progname)
447481
printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
448482
printf(_("Report bugs to <[email protected]>.\n"));
449483
}
484+
485+
static void
486+
set_section (int *dumpSections, const char *arg)
487+
{
488+
/* if this is the first, clear all the bits */
489+
if (*dumpSections == DUMP_UNSECTIONED)
490+
*dumpSections = 0;
491+
492+
if (strcmp(arg,"pre-data") == 0)
493+
*dumpSections |= DUMP_PRE_DATA;
494+
else if (strcmp(arg,"data") == 0)
495+
*dumpSections |= DUMP_DATA;
496+
else if (strcmp(arg,"post-data") == 0)
497+
*dumpSections |= DUMP_POST_DATA;
498+
else
499+
{
500+
fprintf(stderr, _("%s: unknown section name \"%s\")\n"),
501+
progname, arg);
502+
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
503+
progname);
504+
exit(1);
505+
}
506+
}

0 commit comments

Comments
 (0)