Rewrite --section option to decouple it from --schema-only/--data-only.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 30 May 2012 03:22:14 +0000 (23:22 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 30 May 2012 03:22:14 +0000 (23:22 -0400)
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.

doc/src/sgml/ref/pg_dump.sgml
doc/src/sgml/ref/pg_restore.sgml
src/bin/pg_dump/dumputils.c
src/bin/pg_dump/dumputils.h
src/bin/pg_dump/pg_backup.h
src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_backup_archiver.h
src/bin/pg_dump/pg_backup_tar.c
src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_restore.c

index 12ddd2596621781762ce251f8a587696f237617c..450383083d7e1137c4e8c4604d7091ab4daa457a 100644 (file)
@@ -113,10 +113,12 @@ PostgreSQL documentation
       <listitem>
        <para>
         Dump only the data, not the schema (data definitions).
+        Table data, large objects, and sequence values are dumped.
        </para>
 
        <para>
-        This option is equivalent to specifying <option>--section=data</>.
+        This option is similar to, but for historical reasons not identical
+        to, specifying <option>--section=data</>.
        </para>
       </listitem>
      </varlistentry>
@@ -403,12 +405,18 @@ PostgreSQL documentation
         Dump only the object definitions (schema), not data.
        </para>
        <para>
-        To exclude table data for only a subset of tables in the database,
-        see <option>--exclude-table-data</>.
+        This option is the inverse of <option>--data-only</>.
+        It is similar to, but for historical reasons not identical to,
+        specifying
+        <option>--section=pre-data --section=post-data</>.
        </para>
        <para>
-        This option is equivalent to specifying
-        <option>--section=pre-data --section=post-data</>.
+        (Do not confuse this with the <option>--schema</> option, which
+        uses the word <quote>schema</> in a different meaning.)
+       </para>
+       <para>
+        To exclude table data for only a subset of tables in the database,
+        see <option>--exclude-table-data</>.
        </para>
       </listitem>
      </varlistentry>
@@ -722,14 +730,17 @@ PostgreSQL documentation
        <term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
        <listitem>
          <para>
-           Only dump the named section. The name can be one of <option>pre-data</>, <option>data</>
-           and <option>post-data</>.
-           This option can be specified more than once. The default is to dump all sections.
+          Only dump the named section. The section name can be
+          <option>pre-data</>, <option>data</>, or <option>post-data</>.
+          This option can be specified more than once to select multiple
+          sections. The default is to dump all sections.
          </para>
          <para>
-           Post-data items consist of definitions of indexes, triggers, rules
-           and constraints other than validated check constraints.
-           Pre-data items consist of all other data definition items.
+          The data section contains actual table data as well as large-object
+          definitions.
+          Post-data items consist of definitions of indexes, triggers, rules
+          and constraints other than validated check constraints.
+          Pre-data items consist of all other data definition items.
          </para>
        </listitem>
      </varlistentry>
index 513b946e571321373a970011a99b4c5fbdcca29a..bc3d2b7e90fe8760fbbeca735a86e332deb2f5eb 100644 (file)
       <listitem>
        <para>
         Restore only the data, not the schema (data definitions).
+        Table data, large objects, and sequence values are restored,
+        if present in the archive.
        </para>
+
        <para>
-        This option is equivalent to specifying <option>--section=data</>.
+        This option is similar to, but for historical reasons not identical
+        to, specifying <option>--section=data</>.
        </para>
       </listitem>
      </varlistentry>
       <term><option>--schema-only</option></term>
       <listitem>
        <para>
-        Restore only the schema (data definitions), not the data (table
-        contents).  Current sequence values will not be restored, either.
-        (Do not confuse this with the <option>--schema</> option, which
-        uses the word <quote>schema</> in a different meaning.)
+        Restore only the schema (data definitions), not data,
+        to the extent that schema entries are present in the archive.
        </para>
        <para>
-        This option is equivalent to specifying
+        This option is the inverse of <option>--data-only</>.
+        It is similar to, but for historical reasons not identical to,
+        specifying
         <option>--section=pre-data --section=post-data</>.
        </para>
+       <para>
+        (Do not confuse this with the <option>--schema</> option, which
+        uses the word <quote>schema</> in a different meaning.)
+       </para>
       </listitem>
      </varlistentry>
 
        <term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
        <listitem>
          <para>
-           Only restore the named section. The name can be one of <option>pre-data</>, <option>data</>
-           and <option>post-data</>.
-           This option can be specified more than once. The default is to restore all sections.
+          Only restore the named section. The section name can be
+          <option>pre-data</>, <option>data</>, or <option>post-data</>.
+          This option can be specified more than once to select multiple
+          sections. The default is to restore all sections.
          </para>
          <para>
-           Post-data items consist of definitions of indexes, triggers, rules
-           and constraints other than validated check constraints.
-           Pre-data items consist of all other data definition items.
+          The data section contains actual table data as well as large-object
+          definitions.
+          Post-data items consist of definitions of indexes, triggers, rules
+          and constraints other than validated check constraints.
+          Pre-data items consist of all other data definition items.
          </para>
        </listitem>
      </varlistentry>
index b662ad107e56e1a7b1006b83ebcb8b688f5bd6bb..623c250eb895ef6ad0bbd4098664086064192689 100644 (file)
@@ -17,7 +17,6 @@
 #include <ctype.h>
 
 #include "dumputils.h"
-#include "pg_backup.h"
 
 #include "parser/keywords.h"
 
@@ -1230,6 +1229,37 @@ emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer,
 }
 
 
+/*
+ * Parse a --section=foo command line argument.
+ *
+ * Set or update the bitmask in *dumpSections according to arg.
+ * dumpSections is initialised as DUMP_UNSECTIONED by pg_dump and
+ * pg_restore so they can know if this has even been called.
+ */
+void
+set_dump_section(const char *arg, int *dumpSections)
+{
+   /* if this is the first call, clear all the bits */
+   if (*dumpSections == DUMP_UNSECTIONED)
+       *dumpSections = 0;
+
+   if (strcmp(arg,"pre-data") == 0)
+       *dumpSections |= DUMP_PRE_DATA;
+   else if (strcmp(arg,"data") == 0)
+       *dumpSections |= DUMP_DATA;
+   else if (strcmp(arg,"post-data") == 0)
+       *dumpSections |= DUMP_POST_DATA;
+   else
+   {
+       fprintf(stderr, _("%s: unknown section name \"%s\")\n"),
+               progname, arg);
+       fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
+               progname);
+       exit_nicely(1);
+   }
+}
+
+
 /*
  * Write a printf-style message to stderr.
  *
@@ -1279,35 +1309,6 @@ exit_horribly(const char *modulename, const char *fmt,...)
    exit_nicely(1);
 }
 
-/*
- * Set the bitmask in dumpSections according to the first argument.
- * dumpSections is initialised as DUMP_UNSECTIONED by pg_dump and
- * pg_restore so they can know if this has even been called.
- */
-
-void
-set_section (const char *arg, int *dumpSections)
-{
-   /* if this is the first, clear all the bits */
-   if (*dumpSections == DUMP_UNSECTIONED)
-       *dumpSections = 0;
-
-   if (strcmp(arg,"pre-data") == 0)
-       *dumpSections |= DUMP_PRE_DATA;
-   else if (strcmp(arg,"data") == 0)
-       *dumpSections |= DUMP_DATA;
-   else if (strcmp(arg,"post-data") == 0)
-       *dumpSections |= DUMP_POST_DATA;
-   else
-   {
-       fprintf(stderr, _("%s: unknown section name \"%s\")\n"),
-               progname, arg);
-       fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
-               progname);
-       exit_nicely(1);
-   }
-}
-
 /* Register a callback to be run when exit_nicely is invoked. */
 void
 on_exit_nicely(on_exit_nicely_callback function, void *arg)
index 82cf940892cb5c03d6274269b4d46a8bb97b6f68..3d1ed9570abbbbee3e4856268052c4fb4f474465 100644 (file)