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

Commit 4317e02

Browse files
committed
Rewrite --section option to decouple it from --schema-only/--data-only.
The initial implementation of pg_dump's --section option supposed that the existing --schema-only and --data-only options could be made equivalent to --section settings. This is wrong, though, due to dubious but long since set-in-stone decisions about where to dump SEQUENCE SET items, as seen in bug report from Martin Pitt. (And I'm not totally convinced there weren't other bugs, either.) Undo that coupling and instead drive --section filtering off current-section state tracked as we scan through the TOC list to call _tocEntryRequired(). To make sure those decisions don't shift around and hopefully save a few cycles, run _tocEntryRequired() only once per TOC entry and save the result in a new TOC field. This required minor rejiggering of ACL handling but also allows a far cleaner implementation of inhibit_data_for_failed_table. Also, to ensure that pg_dump and pg_restore have the same behavior with respect to the --section switches, add _tocEntryRequired() filtering to WriteToc() and WriteDataChunks(), rather than trying to implement section filtering in an entirely orthogonal way in dumpDumpableObject(). This required adjusting the handling of the special ENCODING and STDSTRINGS items, but they were pretty weird before anyway. Minor other code review for the patch, too.
1 parent 4bc6fb5 commit 4317e02

File tree

10 files changed

+238
-238
lines changed

10 files changed

+238
-238
lines changed

doc/src/sgml/ref/pg_dump.sgml

+22-11
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ PostgreSQL documentation
113113
<listitem>
114114
<para>
115115
Dump only the data, not the schema (data definitions).
116+
Table data, large objects, and sequence values are dumped.
116117
</para>
117118

118119
<para>
119-
This option is equivalent to specifying <option>--section=data</>.
120+
This option is similar to, but for historical reasons not identical
121+
to, specifying <option>--section=data</>.
120122
</para>
121123
</listitem>
122124
</varlistentry>
@@ -403,12 +405,18 @@ PostgreSQL documentation
403405
Dump only the object definitions (schema), not data.
404406
</para>
405407
<para>
406-
To exclude table data for only a subset of tables in the database,
407-
see <option>--exclude-table-data</>.
408+
This option is the inverse of <option>--data-only</>.
409+
It is similar to, but for historical reasons not identical to,
410+
specifying
411+
<option>--section=pre-data --section=post-data</>.
408412
</para>
409413
<para>
410-
This option is equivalent to specifying
411-
<option>--section=pre-data --section=post-data</>.
414+
(Do not confuse this with the <option>--schema</> option, which
415+
uses the word <quote>schema</> in a different meaning.)
416+
</para>
417+
<para>
418+
To exclude table data for only a subset of tables in the database,
419+
see <option>--exclude-table-data</>.
412420
</para>
413421
</listitem>
414422
</varlistentry>
@@ -722,14 +730,17 @@ PostgreSQL documentation
722730
<term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
723731
<listitem>
724732
<para>
725-
Only dump the named section. The name can be one of <option>pre-data</>, <option>data</>
726-
and <option>post-data</>.
727-
This option can be specified more than once. The default is to dump all sections.
733+
Only dump the named section. The section name can be
734+
<option>pre-data</>, <option>data</>, or <option>post-data</>.
735+
This option can be specified more than once to select multiple
736+
sections. The default is to dump all sections.
728737
</para>
729738
<para>
730-
Post-data items consist of definitions of indexes, triggers, rules
731-
and constraints other than validated check constraints.
732-
Pre-data items consist of all other data definition items.
739+
The data section contains actual table data as well as large-object
740+
definitions.
741+
Post-data items consist of definitions of indexes, triggers, rules
742+
and constraints other than validated check constraints.
743+
Pre-data items consist of all other data definition items.
733744
</para>
734745
</listitem>
735746
</varlistentry>

doc/src/sgml/ref/pg_restore.sgml

+23-12
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@
9292
<listitem>
9393
<para>
9494
Restore only the data, not the schema (data definitions).
95+
Table data, large objects, and sequence values are restored,
96+
if present in the archive.
9597
</para>
98+
9699
<para>
97-
This option is equivalent to specifying <option>--section=data</>.
100+
This option is similar to, but for historical reasons not identical
101+
to, specifying <option>--section=data</>.
98102
</para>
99103
</listitem>
100104
</varlistentry>
@@ -357,15 +361,19 @@
357361
<term><option>--schema-only</option></term>
358362
<listitem>
359363
<para>
360-
Restore only the schema (data definitions), not the data (table
361-
contents). Current sequence values will not be restored, either.
362-
(Do not confuse this with the <option>--schema</> option, which
363-
uses the word <quote>schema</> in a different meaning.)
364+
Restore only the schema (data definitions), not data,
365+
to the extent that schema entries are present in the archive.
364366
</para>
365367
<para>
366-
This option is equivalent to specifying
368+
This option is the inverse of <option>--data-only</>.
369+
It is similar to, but for historical reasons not identical to,
370+
specifying
367371
<option>--section=pre-data --section=post-data</>.
368372
</para>
373+
<para>
374+
(Do not confuse this with the <option>--schema</> option, which
375+
uses the word <quote>schema</> in a different meaning.)
376+
</para>
369377
</listitem>
370378
</varlistentry>
371379

@@ -515,14 +523,17 @@
515523
<term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
516524
<listitem>
517525
<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.
526+
Only restore the named section. The section name can be
527+
<option>pre-data</>, <option>data</>, or <option>post-data</>.
528+
This option can be specified more than once to select multiple
529+
sections. The default is to restore all sections.
521530
</para>
522531
<para>
523-
Post-data items consist of definitions of indexes, triggers, rules
524-
and constraints other than validated check constraints.
525-
Pre-data items consist of all other data definition items.
532+
The data section contains actual table data as well as large-object
533+
definitions.
534+
Post-data items consist of definitions of indexes, triggers, rules
535+
and constraints other than validated check constraints.
536+
Pre-data items consist of all other data definition items.
526537
</para>
527538
</listitem>
528539
</varlistentry>

src/bin/pg_dump/dumputils.c

+31-30
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <ctype.h>
1818

1919
#include "dumputils.h"
20-
#include "pg_backup.h"
2120

2221
#include "parser/keywords.h"
2322

@@ -1230,6 +1229,37 @@ emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer,
12301229
}
12311230

12321231

1232+
/*
1233+
* Parse a --section=foo command line argument.
1234+
*
1235+
* Set or update the bitmask in *dumpSections according to arg.
1236+
* dumpSections is initialised as DUMP_UNSECTIONED by pg_dump and
1237+
* pg_restore so they can know if this has even been called.
1238+
*/
1239+
void
1240+
set_dump_section(const char *arg, int *dumpSections)
1241+
{
1242+
/* if this is the first call, clear all the bits */
1243+
if (*dumpSections == DUMP_UNSECTIONED)
1244+
*dumpSections = 0;
1245+
1246+
if (strcmp(arg,"pre-data") == 0)
1247+
*dumpSections |= DUMP_PRE_DATA;
1248+
else if (strcmp(arg,"data") == 0)
1249+
*dumpSections |= DUMP_DATA;
1250+
else if (strcmp(arg,"post-data") == 0)
1251+
*dumpSections |= DUMP_POST_DATA;
1252+
else
1253+
{
1254+
fprintf(stderr, _("%s: unknown section name \"%s\")\n"),
1255+
progname, arg);
1256+
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
1257+
progname);
1258+
exit_nicely(1);
1259+
}
1260+
}
1261+
1262+
12331263
/*
12341264
* Write a printf-style message to stderr.
12351265
*
@@ -1279,35 +1309,6 @@ exit_horribly(const char *modulename, const char *fmt,...)
12791309
exit_nicely(1);
12801310
}
12811311

1282-
/*
1283-
* Set the bitmask in dumpSections according to the first argument.
1284-
* dumpSections is initialised as DUMP_UNSECTIONED by pg_dump and
1285-
* pg_restore so they can know if this has even been called.
1286-
*/
1287-
1288-
void
1289-
set_section (const char *arg, int *dumpSections)
1290-
{
1291-
/* if this is the first, clear all the bits */
1292-
if (*dumpSections == DUMP_UNSECTIONED)
1293-
*dumpSections = 0;
1294-
1295-
if (strcmp(arg,"pre-data") == 0)
1296-
*dumpSections |= DUMP_PRE_DATA;
1297-
else if (strcmp(arg,"data") == 0)
1298-
*dumpSections |= DUMP_DATA;
1299-
else if (strcmp(arg,"post-data") == 0)
1300-
*dumpSections |= DUMP_POST_DATA;
1301-
else
1302-
{
1303-
fprintf(stderr, _("%s: unknown section name \"%s\")\n"),
1304-
progname, arg);
1305-
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
1306-
progname);
1307-
exit_nicely(1);
1308-
}
1309-
}
1310-
13111312
/* Register a callback to be run when exit_nicely is invoked. */
13121313
void
13131314
on_exit_nicely(on_exit_nicely_callback function, void *arg)

src/bin/pg_dump/dumputils.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
#include "libpq-fe.h"
2020
#include "pqexpbuffer.h"
2121

22+
typedef enum /* bits returned by set_dump_section */
23+
{
24+
DUMP_PRE_DATA = 0x01,
25+
DUMP_DATA = 0x02,
26+
DUMP_POST_DATA = 0x04,
27+
DUMP_UNSECTIONED = 0xff
28+
} DumpSections;
29+
30+
typedef void (*on_exit_nicely_callback) (int code, void *arg);
31+
2232
extern int quote_all_identifiers;
2333
extern const char *progname;
2434

@@ -52,15 +62,13 @@ extern void buildShSecLabelQuery(PGconn *conn, const char *catalog_name,
5262
uint32 objectId, PQExpBuffer sql);
5363
extern void emitShSecLabels(PGconn *conn, PGresult *res,
5464
PQExpBuffer buffer, const char *target, const char *objname);
65+
extern void set_dump_section(const char *arg, int *dumpSections);
5566
extern void write_msg(const char *modulename, const char *fmt,...)
5667
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
5768
extern void vwrite_msg(const char *modulename, const char *fmt, va_list ap)
5869
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0)));
5970
extern void exit_horribly(const char *modulename, const char *fmt,...)
6071
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3), noreturn));
61-
extern void set_section (const char *arg, int *dumpSections);
62-
63-
typedef void (*on_exit_nicely_callback) (int code, void *arg);
6472
extern void on_exit_nicely(on_exit_nicely_callback function, void *arg);
6573
extern void exit_nicely(int code) __attribute__((noreturn));
6674

src/bin/pg_dump/pg_backup.h

+4-11
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ typedef enum _teSection
6868
SECTION_POST_DATA /* stuff to be processed after data */
6969
} teSection;
7070

71-
typedef enum
72-
{
73-
DUMP_PRE_DATA = 0x01,
74-
DUMP_DATA = 0x02,
75-
DUMP_POST_DATA = 0x04,
76-
DUMP_UNSECTIONED = 0xff
77-
} DumpSections;
78-
7971
/*
8072
* We may want to have some more user-readable data, but in the mean
8173
* time this gives us some abstraction and type checking.
@@ -114,9 +106,9 @@ typedef struct _restoreOptions
114106
int no_security_labels; /* Skip security label entries */
115107
char *superuser; /* Username to use as superuser */
116108
char *use_role; /* Issue SET ROLE to this */
117-
int dataOnly;
118109
int dropSchema;
119110
const char *filename;
111+
int dataOnly;
120112
int schemaOnly;
121113
int dumpSections;
122114
int verbose;
@@ -187,7 +179,9 @@ extern int EndBlob(Archive *AH, Oid oid);
187179

188180
extern void CloseArchive(Archive *AH);
189181

190-
extern void RestoreArchive(Archive *AH, RestoreOptions *ropt);
182+
extern void SetArchiveRestoreOptions(Archive *AH, RestoreOptions *ropt);
183+
184+
extern void RestoreArchive(Archive *AH);
191185

192186
/* Open an existing archive */
193187
extern Archive *OpenArchive(const char *FileSpec, const ArchiveFormat fmt);
@@ -203,7 +197,6 @@ extern RestoreOptions *NewRestoreOptions(void);
203197

204198
/* Rearrange and filter TOC entries */
205199
extern void SortTocFromFile(Archive *AHX, RestoreOptions *ropt);
206-
extern void InitDummyWantedList(Archive *AHX, RestoreOptions *ropt);
207200

208201
/* Convenience functions used only when writing DATA */
209202
extern int archputs(const char *s, Archive *AH);

0 commit comments

Comments
 (0)