pg_basebackup compressed tar to stdout
pg_basebackup currently doesn't allow compressed tar to stdout. That
should be added to make the interface consistent, and specifically to
allow common idoms like
pg_basebackup -Ft -z -D - | ssh tar -x -z -f -
Small patch attached.
Attachments:
pg_basebackup-tar-gzip-stdout.patchtext/x-patch; charset=UTF-8; name=pg_basebackup-tar-gzip-stdout.patchDownload
diff --git i/doc/src/sgml/ref/pg_basebackup.sgml w/doc/src/sgml/ref/pg_basebackup.sgml
index 8a7b833..32fa9f8 100644
--- i/doc/src/sgml/ref/pg_basebackup.sgml
+++ w/doc/src/sgml/ref/pg_basebackup.sgml
@@ -174,8 +174,7 @@ PostgreSQL documentation
<listitem>
<para>
Enables gzip compression of tar file output. Compression is only
- available when generating tar files, and is not available when sending
- output to standard output.
+ available when using the tar format.
</para>
</listitem>
</varlistentry>
diff --git i/src/bin/pg_basebackup/pg_basebackup.c w/src/bin/pg_basebackup/pg_basebackup.c
index 1f31fe0..713c3af 100644
--- i/src/bin/pg_basebackup/pg_basebackup.c
+++ w/src/bin/pg_basebackup/pg_basebackup.c
@@ -261,7 +261,20 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
* Base tablespaces
*/
if (strcmp(basedir, "-") == 0)
- tarfile = stdout;
+ {
+ if (compresslevel > 0)
+ {
+ ztarfile = gzdopen(dup(fileno(stdout)), "wb");
+ if (gzsetparams(ztarfile, compresslevel, Z_DEFAULT_STRATEGY) != Z_OK)
+ {
+ fprintf(stderr, _("%s: could not set compression level %i: %s\n"),
+ progname, compresslevel, get_gz_error(ztarfile));
+ disconnect_and_exit(1);
+ }
+ }
+ else
+ tarfile = stdout;
+ }
else
{
#ifdef HAVE_LIBZ
@@ -384,7 +397,12 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
}
}
- if (strcmp(basedir, "-") != 0)
+ if (strcmp(basedir, "-") == 0)
+ {
+ if (ztarfile)
+ gzclose(ztarfile);
+ }
+ else
{
#ifdef HAVE_LIBZ
if (ztarfile != NULL)
@@ -1076,14 +1094,6 @@ main(int argc, char **argv)
progname);
exit(1);
}
-#else
- if (compresslevel > 0 && strcmp(basedir, "-") == 0)
- {
- fprintf(stderr,
- _("%s: compression is not supported on standard output\n"),
- progname);
- exit(1);
- }
#endif
/*
Peter Eisentraut <peter_e@gmx.net> writes:
pg_basebackup currently doesn't allow compressed tar to stdout. That
should be added to make the interface consistent, and specifically to
allow common idoms like
pg_basebackup -Ft -z -D - | ssh tar -x -z -f -
Small patch attached.
I have not bothered to read this in context, but the visible part of the
patch makes it look like you broke the not-HAVE_LIBZ case ... other than
that gripe, no objection.
regards, tom lane
On tor, 2011-05-26 at 17:06 -0400, Tom Lane wrote:
Peter Eisentraut <peter_e@gmx.net> writes:
pg_basebackup currently doesn't allow compressed tar to stdout. That
should be added to make the interface consistent, and specifically to
allow common idoms likepg_basebackup -Ft -z -D - | ssh tar -x -z -f -
Small patch attached.
I have not bothered to read this in context, but the visible part of the
patch makes it look like you broke the not-HAVE_LIBZ case ... other than
that gripe, no objection.
Ah yes, that needs some fine-tuning.