From 1b78e26b5901de39c24677362c78391cb7c39b6a Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Thu, 30 Mar 2023 17:48:57 -0500
Subject: [PATCH 3/4] WIP: pg_dump: support zstd workers

This is a separate commit since it's not essential; the zstd library is
frequently compiled without threading support, so the functionality
isn't very well-tested, and because use of zstd threads might
conceivably play poorly with pg_dump's use of threads under Windows.

Targetting postgres v17.
---
 doc/src/sgml/ref/pg_dump.sgml   | 8 ++++++--
 src/bin/pg_dump/compress_zstd.c | 4 ++++
 src/bin/pg_dump/pg_dump.c       | 4 ----
 3 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml
index e81e35c13b3..1d55ce05b21 100644
--- a/doc/src/sgml/ref/pg_dump.sgml
+++ b/doc/src/sgml/ref/pg_dump.sgml
@@ -681,8 +681,12 @@ PostgreSQL documentation
         as though it had been fed through <application>gzip</application>,
         <application>lz4</application>, or <application>zstd</application>;
         but the default is not to compress.
-        With zstd compression, <literal>long</literal> mode may improve the
-        compression ratio, at the cost of increased memory use.
+        With zstd compression, <literal>long</literal> and
+        <literal>workers</literal> options may be specified to enable long-distance
+        matching and threaded workers, respectively.
+        Long distance mode may improve the compression ratio, at the cost of
+        increased memory use.
+        Threaded workers allow leveraging multiple CPUs during compression.
        </para>
        <para>
         The tar archive format currently does not support compression at all.
diff --git a/src/bin/pg_dump/compress_zstd.c b/src/bin/pg_dump/compress_zstd.c
index 49a877ce010..f1f84ad69c4 100644
--- a/src/bin/pg_dump/compress_zstd.c
+++ b/src/bin/pg_dump/compress_zstd.c
@@ -85,6 +85,10 @@ _ZstdCStreamParams(pg_compress_specification compress)
 								  ZSTD_c_enableLongDistanceMatching,
 								  compress.long_distance, "long");
 
+	if (compress.options & PG_COMPRESSION_OPTION_WORKERS)
+		_Zstd_CCtx_setParam_or_die(cstream, ZSTD_c_nbWorkers,
+								   compress.workers, "workers");
+
 	return cstream;
 }
 
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index a426984046b..240dcdb0223 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -740,10 +740,6 @@ main(int argc, char **argv)
 	if (error_detail != NULL)
 		pg_fatal("%s", error_detail);
 
-	if (compression_spec.options & PG_COMPRESSION_OPTION_WORKERS)
-		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.
-- 
2.34.1

