Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Tweak behavior of pg_dump --extension with configuration tables
authorMichael Paquier <michael@paquier.xyz>
Thu, 15 Apr 2021 01:03:46 +0000 (10:03 +0900)
committerMichael Paquier <michael@paquier.xyz>
Thu, 15 Apr 2021 01:03:46 +0000 (10:03 +0900)
6568cef, that introduced the option, had an inconsistent behavior when
it comes to configuration tables set up by pg_extension_config_dump, as
the data of all configuration tables would included in a dump even for
extensions not listed by a set of --extension switches.

The contents dumped changed depending on the schema where an extension
was installed when an extension was not listed.  For example, an
extension installed under the public schema would have its configuration
data not dumped even when not listed with --extension, which was
inconsistent with the case of an extension installed on a non-public
schema, where the configuration would be dumped.

Per discussion with Noah, we have settled down to the simple rule of
dumping configuration data of an extension if it is listed in
--extension (default is unchanged and backward-compatible, to dump
everything on sight if there are no extensions directly listed).  This
avoids some weird cases where the dumps depended on a --schema for one.

More tests are added to cover the gap, where we cross-check more
behaviors depending on --schema when an extension is not listed.

Reported-by: Noah Misch
Reviewed-by: Noah Misch
Discussion: https://postgr.es/m/20210404220802.GA728316@rfd.leadboat.com

doc/src/sgml/ref/pg_dump.sgml
src/bin/pg_dump/pg_dump.c
src/test/modules/test_pg_dump/t/001_base.pl

index 529b167c969a61dda877c5af4952d24eb18e88a6..67c2cbbec62e2307f8ec46a9968c728406b2d1fe 100644 (file)
@@ -234,6 +234,12 @@ PostgreSQL documentation
         shell from expanding the wildcards.
        </para>
 
+       <para>
+        Any configuration relation registered by
+        <function>pg_extension_config_dump</function> is included in the
+        dump if its extension is specified by <option>--extension</option>.
+       </para>
+
        <note>
         <para>
          When <option>-e</option> is specified,
index d0ea48961426c49cb482ab905c06dce939046d4d..391947340f4d2a51b2b946195494d3391395778a 100644 (file)
@@ -18271,7 +18271,8 @@ processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
     * Note that we create TableDataInfo objects even in schemaOnly mode, ie,
     * user data in a configuration table is treated like schema data. This
     * seems appropriate since system data in a config table would get
-    * reloaded by CREATE EXTENSION.
+    * reloaded by CREATE EXTENSION.  If the extension is not listed in the
+    * list of extensions to be included, none of its data is dumped.
     */
    for (i = 0; i < numExtensions; i++)
    {
@@ -18283,6 +18284,15 @@ processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
        int         nconfigitems = 0;
        int         nconditionitems = 0;
 
+       /*
+        * Check if this extension is listed as to include in the dump.  If
+        * not, any table data associated with it is discarded.
+        */
+       if (extension_include_oids.head != NULL &&
+           !simple_oid_list_member(&extension_include_oids,
+                                   curext->dobj.catId.oid))
+           continue;
+
        if (strlen(extconfig) != 0 || strlen(extcondition) != 0)
        {
            int         j;
index ef98c084939883f38335835d4cc002256f29cf83..1cc6f29ab69565333454b8205718e6df4edd03b4 100644 (file)
@@ -208,6 +208,34 @@ my %pgdump_runs = (
            'pg_dump', '--no-sync', "--file=$tempdir/without_extension.sql",
            '--extension=plpgsql', 'postgres',
        ],
+   },
+
+   # plgsql in the list of extensions blocks the dump of extension
+   # test_pg_dump.  "public" is the schema used by the extension
+   # test_pg_dump, but none of its objects should be dumped.
+   without_extension_explicit_schema => {
+       dump_cmd => [
+           'pg_dump',
+           '--no-sync',
+           "--file=$tempdir/without_extension_explicit_schema.sql",
+           '--extension=plpgsql',
+           '--schema=public',
+           'postgres',
+       ],
+   },
+
+   # plgsql in the list of extensions blocks the dump of extension
+   # test_pg_dump, but not the dump of objects not dependent on the
+   # extension located on a schema maintained by the extension.
+   without_extension_internal_schema => {
+       dump_cmd => [
+           'pg_dump',
+           '--no-sync',
+           "--file=$tempdir/without_extension_internal_schema.sql",
+           '--extension=plpgsql',
+           '--schema=regress_pg_dump_schema',
+           'postgres',
+       ],
    },);
 
 ###############################################################
@@ -632,6 +660,8 @@ my %tests = (
            pg_dumpall_globals => 1,
            section_data       => 1,
            section_pre_data   => 1,
+           # Excludes this schema as extension is not listed.
+           without_extension_explicit_schema => 1,
        },
    },
 
@@ -646,6 +676,8 @@ my %tests = (
            pg_dumpall_globals => 1,
            section_data       => 1,
            section_pre_data   => 1,
+           # Excludes this schema as extension is not listed.
+           without_extension_explicit_schema => 1,
        },
    },
 
@@ -662,6 +694,8 @@ my %tests = (
            %full_runs,
            schema_only      => 1,
            section_pre_data => 1,
+           # Excludes the extension and keeps the schema's data.
+           without_extension_internal_schema => 1,
        },
    },);