Rework code defining default compression for dir/custom formats in pg_dump
authorMichael Paquier <[email protected]>
Thu, 27 Apr 2023 04:34:05 +0000 (13:34 +0900)
committerMichael Paquier <[email protected]>
Thu, 27 Apr 2023 04:34:05 +0000 (13:34 +0900)
As written, pg_dump would call twice parse_compress_specification() for
the custom and directory formats to build a compression specification if
no compression option is defined, as these formats should be compressed
by default when compiled with zlib, or use no compression without zlib.
This made the code logic quite confusing, and the first compression
specification built would be incorrect before being overwritten by the
second one.

Rather than creating two compression specifications, this commit changes
a bit the order of the checks for the compression options so as
compression_algorithm_str is now set to a correct value for the custom
and format directory when no compression option is defined.  This makes
the code easier to understand, as parse_compress_specification() is now
called once for all the format, with or without user-specified
compression methods.  One comment was also confusing for the non-zlib
case, so remove it while on it.

This code has been introduced in 5e73a60 when adding support for
compression specifications in pg_dump.

Per discussion with Justin Pryzby and Georgios Kokolatos.

Discussion: https://postgr.es/m/20230225050214[email protected]

src/bin/pg_dump/pg_dump.c

index 058244cd17171c010ab96a36fe4c7b58888e5921..41a51ec5cd83534f9a62d79f64100be3fe66bd11 100644 (file)
@@ -721,6 +721,21 @@ main(int argc, char **argv)
        if (archiveFormat == archNull)
                plainText = 1;
 
+       /*
+        * Custom and directory formats are compressed by default with gzip when
+        * available, not the others.  If gzip is not available, no compression is
+        * done by default.
+        */
+       if ((archiveFormat == archCustom || archiveFormat == archDirectory) &&
+               !user_compression_defined)
+       {
+#ifdef HAVE_LIBZ
+               compression_algorithm_str = "gzip";
+#else
+               compression_algorithm_str = "none";
+#endif
+       }
+
        /*
         * Compression options
         */
@@ -749,21 +764,6 @@ main(int argc, char **argv)
                pg_log_warning("compression option \"%s\" is not currently supported by pg_dump",
                                           "workers");
 
-       /*
-        * Custom and directory formats are compressed by default with gzip when
-        * available, not the others.
-        */
-       if ((archiveFormat == archCustom || archiveFormat == archDirectory) &&
-               !user_compression_defined)
-       {
-#ifdef HAVE_LIBZ
-               parse_compress_specification(PG_COMPRESSION_GZIP, NULL,
-                                                                        &compression_spec);
-#else
-               /* Nothing to do in the default case */
-#endif
-       }
-
        /*
         * If emitting an archive format, we always want to emit a DATABASE item,
         * in case --create is specified at pg_restore time.