Adding pg_dump flag for parallel export to pipes

Поиск
Список
Период
Сортировка
Искать

Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
Hi Hackers,

We are proposing the ability to specify a pipe command to pg_dump by a
flag. And attaching the patch set.

Why : Currently it is quite simple to pipe the output of pg_dump for
text format to a pipe at command line and do any manipulations
necessary. Following is an example :

       pg_dump   | lz4 | pv -L 10k | ssh remote.host
"cat - > remote.dump.lz4"

Here we first compress the stream using lz4 and then send it over ssh
to a remote host to be saved as a file while rate-limiting the network
usage to 10KB/s.

Something like this is not possible for format=directory (-Fd) since
all you can provide is the directory name to store the individual
files. Note it is not possible to do this irrespective of the usage of
the parallel dump option ('--jobs' flag).

While the directory format supports compression using a flag, the rest
of the operations in the above example are not possible. And a pipe
command provides more flexibility in what compression algorithm one
wants to use.

This patch set provides pg_dump the ability to pipe the data in the
directory mode by using a new flag '--pipe-command' (in both parallel
and non-parallel mode).

We also add a similar option to pg_restore.

The following can be the major use cases of these changes :
  1. Stream pg_dump output to a cloud storage
  2. SSH the data to a remote host (with or without throttling)
  3. Custom compression options


Usage Examples : Here is an example of how the pipe-command will look like.

     pg_dump -Fd mydb --pipe-command="cat > dumpdir/%f" (dumpdir
should exist beforehand.)

This is equivalent to

     pg_dump -Fd mydb --file=dumpdir

(Please note that the flags '--file' or '--pipe-command' can't be used
together.)

For the more complex scenario as mentioned above, the command will be
(with the parallelism of 5) :

      pg_dump -Fd mydb -j 5 --pipe-command="lz4 | pv -L 10k | ssh
remote.host "cat > dumpdir/%f""

Please note the use of %f in the above examples. As a user would
almost always want to write the post-processing output to a file (or
perhaps a cloud location), we provide a format specifier %f in the
command. The implementation of pipe-command replaces these format
specifiers with the corresponding file names. These file names are the
same as they would be in the current usage of directory format with
'--file' flag (.dat, toc.dat, blob_NNN.toc,
blob_.dat).

The usage of this flag with pg_restore will also be similar. Here is
an example of restoring from a gzip compressed dump directory.

        pg_restore -C -Fd -d postgres --pipe-commnad="cat
dumpdir/%f.gz | gunzip"

The new flag in pg_restore also works with '-l' and '-L' options

        pg_restore -C -Fd -d postgres --pipe-commnad="cat dumpdir/%f" -L db.list


Implementation Details : Here are the major changes :
   1. We reuse the same variables which store the file name to store
the pipe command. And add a new bool fSpecIsPipe in _archiveHandle
(similar bools in pg_dump.c and pg_restore.c) to specify if it's a
pipe command.
    2. In the cases when the above bool is set to true, we use popen
and pclose instead of fopen and fclose.
     3. To enable the format specifier %f in the pipe-command, we make
changes to the file name creation logic in a few places. Currently the
file name (corresponding to a table or large object) is appended to
the directory name provided by '--file' command. In case of
'--pipe-command', we use 'replace_percent_placeholders' to replace %f
with the corresponding file name. This change is made for both table
files and LO TOC files.

With these core changes, the rest of the code continues working as-is.

We are attaching 4 patches for this change :

  001-pg_dump_pipe has the pg_dump pipe support code.
  002-pg_restore_pipe has the pg_restore pipe support.
  003-pg_dump_basic_tests has a few basic validation tests for
correctmflag combinations. We need to write more automated tests in
002_pg_dump.pl but have been running into some issues with environment
setup due to which certain pipe commands result in the shell process
becoming defunct. These same commands are working fine in manual
testing. We are still looking into this.
  004-pg_dump_documentation has the proposed documentation changes.

We are working on the above test issues and cleanup of the patches.

Open Questions : There are a couple of open questions in the implementation :

     1. Currently the LO TOC file (blob_NNN.toc) is opened in the
append mode. This is not possible with popen for the pipe command.
From reading the code, it seems to us that this file doesn't need to
be opened in the append mode. As '_StartLOs' is called once per
archive entry in WriteDataChunksForToCEntry followed by the dumper
function and then '_EndLOs', it should be okay to change this to 'w'
mode. But this code has been there since the start so we haven't made
that change yet. In the patch, we have changed it to 'w' pipe-command
only and added the ideas for potential solutions in the comments.
     2. We are also not sure yet on how to handle the environment
issues when trying to add new tests to 002_pg_dump.pl.

Please let us know what you think.

Thanks & Regards,
Nitin Motiani
Google

Re: Adding pg_dump flag for parallel export to pipes

От:
Hannu Krosing <hannuk@google.com>
Дата:

Just to bring this out separately : Does anybody have any idea why pipe commands close inside tests ?

Re: 003-pg_dump_basic_tests has a few basic validation tests for
correctmflag combinations. We need to write more automated tests in
002_pg_dump.pl but have been running into some issues with environment
setup due to which certain pipe commands result in the shell process
becoming defunct. These same commands are working fine in manual
testing. We are still looking into this.

----
Hannu


On Mon, Apr 7, 2025 at 7:17 PM Nitin Motiani <nitinmotiani@google.com> wrote:
Hi Hackers,

We are proposing the ability to specify a pipe command to pg_dump by a
flag. And attaching the patch set.

Why : Currently it is quite simple to pipe the output of pg_dump for
text format to a pipe at command line and do any manipulations
necessary. Following is an example :

       pg_dump <flags> <dbname> | lz4 | pv -L 10k | ssh remote.host
"cat - > remote.dump.lz4"

Here we first compress the stream using lz4 and then send it over ssh
to a remote host to be saved as a file while rate-limiting the network
usage to 10KB/s.

Something like this is not possible for format=directory (-Fd) since
all you can provide is the directory name to store the individual
files. Note it is not possible to do this irrespective of the usage of
the parallel dump option ('--jobs' flag).

While the directory format supports compression using a flag, the rest
of the operations in the above example are not possible. And a pipe
command provides more flexibility in what compression algorithm one
wants to use.

This patch set provides pg_dump the ability to pipe the data in the
directory mode by using a new flag '--pipe-command' (in both parallel
and non-parallel mode).

We also add a similar option to pg_restore.

The following can be the major use cases of these changes :
  1. Stream pg_dump output to a cloud storage
  2. SSH the data to a remote host (with or without throttling)
  3. Custom compression options


Usage Examples : Here is an example of how the pipe-command will look like.

     pg_dump -Fd mydb --pipe-command="cat > dumpdir/%f" (dumpdir
should exist beforehand.)

This is equivalent to

     pg_dump -Fd mydb --file=dumpdir

(Please note that the flags '--file' or '--pipe-command' can't be used
together.)

For the more complex scenario as mentioned above, the command will be
(with the parallelism of 5) :

      pg_dump -Fd mydb -j 5 --pipe-command="lz4 | pv -L 10k | ssh
remote.host "cat > dumpdir/%f""

Please note the use of %f in the above examples. As a user would
almost always want to write the post-processing output to a file (or
perhaps a cloud location), we provide a format specifier %f in the
command. The implementation of pipe-command replaces these format
specifiers with the corresponding file names. These file names are the
same as they would be in the current usage of directory format with
'--file' flag (<dump_id>.dat, toc.dat, blob_NNN.toc,
blob_<blob_id>.dat).

The usage of this flag with pg_restore will also be similar. Here is
an example of restoring from a gzip compressed dump directory.

        pg_restore -C -Fd -d postgres --pipe-commnad="cat
dumpdir/%f.gz | gunzip"

The new flag in pg_restore also works with '-l' and '-L' options

        pg_restore -C -Fd -d postgres --pipe-commnad="cat dumpdir/%f" -L db.list


Implementation Details : Here are the major changes :
   1. We reuse the same variables which store the file name to store
the pipe command. And add a new bool fSpecIsPipe in _archiveHandle
(similar bools in pg_dump.c and pg_restore.c) to specify if it's a
pipe command.
    2. In the cases when the above bool is set to true, we use popen
and pclose instead of fopen and fclose.
     3. To enable the format specifier %f in the pipe-command, we make
changes to the file name creation logic in a few places. Currently the
file name (corresponding to a table or large object) is appended to
the directory name provided by '--file' command. In case of
'--pipe-command', we use 'replace_percent_placeholders' to replace %f
with the corresponding file name. This change is made for both table
files and LO TOC files.

With these core changes, the rest of the code continues working as-is.

We are attaching 4 patches for this change :

  001-pg_dump_pipe has the pg_dump pipe support code.
  002-pg_restore_pipe has the pg_restore pipe support.
  003-pg_dump_basic_tests has a few basic validation tests for
correctmflag combinations. We need to write more automated tests in
002_pg_dump.pl but have been running into some issues with environment
setup due to which certain pipe commands result in the shell process
becoming defunct. These same commands are working fine in manual
testing. We are still looking into this.
  004-pg_dump_documentation has the proposed documentation changes.

We are working on the above test issues and cleanup of the patches.

Open Questions : There are a couple of open questions in the implementation :

     1. Currently the LO TOC file (blob_NNN.toc) is opened in the
append mode. This is not possible with popen for the pipe command.
From reading the code, it seems to us that this file doesn't need to
be opened in the append mode. As '_StartLOs' is called once per
archive entry in WriteDataChunksForToCEntry followed by the dumper
function and then '_EndLOs', it should be okay to change this to 'w'
mode. But this code has been there since the start so we haven't made
that change yet. In the patch, we have changed it to 'w' pipe-command
only and added the ideas for potential solutions in the comments.
     2. We are also not sure yet on how to handle the environment
issues when trying to add new tests to 002_pg_dump.pl.

Please let us know what you think.

Thanks & Regards,
Nitin Motiani
Google

Re: Adding pg_dump flag for parallel export to pipes

От:
Hannu Krosing <hannuk@google.com>
Дата:
If there are no objections we will add this to the commitfest

On Mon, Apr 7, 2025 at 9:48 PM Hannu Krosing  wrote:
>
>
> Just to bring this out separately : Does anybody have any idea why pipe commands close inside tests ?
>
> Re: 003-pg_dump_basic_tests has a few basic validation tests for
> correctmflag combinations. We need to write more automated tests in
> 002_pg_dump.pl but have been running into some issues with environment
> setup due to which certain pipe commands result in the shell process
> becoming defunct. These same commands are working fine in manual
> testing. We are still looking into this.
>
> ----
> Hannu
>
>
> On Mon, Apr 7, 2025 at 7:17 PM Nitin Motiani  wrote:
>>
>> Hi Hackers,
>>
>> We are proposing the ability to specify a pipe command to pg_dump by a
>> flag. And attaching the patch set.
>>
>> Why : Currently it is quite simple to pipe the output of pg_dump for
>> text format to a pipe at command line and do any manipulations
>> necessary. Following is an example :
>>
>>        pg_dump   | lz4 | pv -L 10k | ssh remote.host
>> "cat - > remote.dump.lz4"
>>
>> Here we first compress the stream using lz4 and then send it over ssh
>> to a remote host to be saved as a file while rate-limiting the network
>> usage to 10KB/s.
>>
>> Something like this is not possible for format=directory (-Fd) since
>> all you can provide is the directory name to store the individual
>> files. Note it is not possible to do this irrespective of the usage of
>> the parallel dump option ('--jobs' flag).
>>
>> While the directory format supports compression using a flag, the rest
>> of the operations in the above example are not possible. And a pipe
>> command provides more flexibility in what compression algorithm one
>> wants to use.
>>
>> This patch set provides pg_dump the ability to pipe the data in the
>> directory mode by using a new flag '--pipe-command' (in both parallel
>> and non-parallel mode).
>>
>> We also add a similar option to pg_restore.
>>
>> The following can be the major use cases of these changes :
>>   1. Stream pg_dump output to a cloud storage
>>   2. SSH the data to a remote host (with or without throttling)
>>   3. Custom compression options
>>
>>
>> Usage Examples : Here is an example of how the pipe-command will look like.
>>
>>      pg_dump -Fd mydb --pipe-command="cat > dumpdir/%f" (dumpdir
>> should exist beforehand.)
>>
>> This is equivalent to
>>
>>      pg_dump -Fd mydb --file=dumpdir
>>
>> (Please note that the flags '--file' or '--pipe-command' can't be used
>> together.)
>>
>> For the more complex scenario as mentioned above, the command will be
>> (with the parallelism of 5) :
>>
>>       pg_dump -Fd mydb -j 5 --pipe-command="lz4 | pv -L 10k | ssh
>> remote.host "cat > dumpdir/%f""
>>
>> Please note the use of %f in the above examples. As a user would
>> almost always want to write the post-processing output to a file (or
>> perhaps a cloud location), we provide a format specifier %f in the
>> command. The implementation of pipe-command replaces these format
>> specifiers with the corresponding file names. These file names are the
>> same as they would be in the current usage of directory format with
>> '--file' flag (.dat, toc.dat, blob_NNN.toc,
>> blob_.dat).
>>
>> The usage of this flag with pg_restore will also be similar. Here is
>> an example of restoring from a gzip compressed dump directory.
>>
>>         pg_restore -C -Fd -d postgres --pipe-commnad="cat
>> dumpdir/%f.gz | gunzip"
>>
>> The new flag in pg_restore also works with '-l' and '-L' options
>>
>>         pg_restore -C -Fd -d postgres --pipe-commnad="cat dumpdir/%f" -L db.list
>>
>>
>> Implementation Details : Here are the major changes :
>>    1. We reuse the same variables which store the file name to store
>> the pipe command. And add a new bool fSpecIsPipe in _archiveHandle
>> (similar bools in pg_dump.c and pg_restore.c) to specify if it's a
>> pipe command.
>>     2. In the cases when the above bool is set to true, we use popen
>> and pclose instead of fopen and fclose.
>>      3. To enable the format specifier %f in the pipe-command, we make
>> changes to the file name creation logic in a few places. Currently the
>> file name (corresponding to a table or large object) is appended to
>> the directory name provided by '--file' command. In case of
>> '--pipe-command', we use 'replace_percent_placeholders' to replace %f
>> with the corresponding file name. This change is made for both table
>> files and LO TOC files.
>>
>> With these core changes, the rest of the code continues working as-is.
>>
>> We are attaching 4 patches for this change :
>>
>>   001-pg_dump_pipe has the pg_dump pipe support code.
>>   002-pg_restore_pipe has the pg_restore pipe support.
>>   003-pg_dump_basic_tests has a few basic validation tests for
>> correctmflag combinations. We need to write more automated tests in
>> 002_pg_dump.pl but have been running into some issues with environment
>> setup due to which certain pipe commands result in the shell process
>> becoming defunct. These same commands are working fine in manual
>> testing. We are still looking into this.
>>   004-pg_dump_documentation has the proposed documentation changes.
>>
>> We are working on the above test issues and cleanup of the patches.
>>
>> Open Questions : There are a couple of open questions in the implementation :
>>
>>      1. Currently the LO TOC file (blob_NNN.toc) is opened in the
>> append mode. This is not possible with popen for the pipe command.
>> From reading the code, it seems to us that this file doesn't need to
>> be opened in the append mode. As '_StartLOs' is called once per
>> archive entry in WriteDataChunksForToCEntry followed by the dumper
>> function and then '_EndLOs', it should be okay to change this to 'w'
>> mode. But this code has been there since the start so we haven't made
>> that change yet. In the patch, we have changed it to 'w' pipe-command
>> only and added the ideas for potential solutions in the comments.
>>      2. We are also not sure yet on how to handle the environment
>> issues when trying to add new tests to 002_pg_dump.pl.
>>
>> Please let us know what you think.
>>
>> Thanks & Regards,
>> Nitin Motiani
>> Google


Re: Adding pg_dump flag for parallel export to pipes

От:
Thomas Munro <thomas.munro@gmail.com>
Дата:
On Tue, Apr 8, 2025 at 7:48 AM Hannu Krosing  wrote:
> Just to bring this out separately : Does anybody have any idea why pipe commands close inside tests ?
>
> Re: 003-pg_dump_basic_tests has a few basic validation tests for
> correctmflag combinations. We need to write more automated tests in
> 002_pg_dump.pl but have been running into some issues with environment
> setup due to which certain pipe commands result in the shell process
> becoming defunct. These same commands are working fine in manual
> testing. We are still looking into this.

No comment on the wider project except that it looks generally useful,
and I can see that it's not possible to use the conventional POSIX
filename "-" to represent stdout, because you need to write to
multiple files so you need to come up with *something* along the lines
you're proposing here.  But I was interested in seeing if I could help
with that technical problem you mentioned above, and I don't see that
happening with the current patches.  Do I understand correctly that
the problem you encountered is in some other tests that you haven't
attached yet?  Could you post what you have so that others can see the
problem and perhaps have a chance of helping?  I also recommend using
git format-patch when you post patches so that you have a place to
write a commit message including a note about which bits are WIP and
known not to work correctly yet.


Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Thanks for the feedback, Thomas.

> No comment on the wider project except that it looks generally useful,
> and I can see that it's not possible to use the conventional POSIX
> filename "-" to represent stdout, because you need to write to
> multiple files so you need to come up with *something* along the lines
> you're proposing here.  But I was interested in seeing if I could help
> with that technical problem you mentioned above, and I don't see that
> happening with the current patches.  Do I understand correctly that
> the problem you encountered is in some other tests that you haven't
> attached yet?  Could you post what you have so that others can see the
> problem and perhaps have a chance of helping?

Yes, we didn't add the failed tests to the patch. We'll add those and
send new patches.

> I also recommend using
> git format-patch when you post patches so that you have a place to
> write a commit message including a note about which bits are WIP and
> known not to work correctly yet.

Will follow these recommendations when sending the next set of patches.

Regards,
Nitin Motiani
Google


Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
Hi,

Apologies for the delay on this thread.

On Mon, Apr 28, 2025 at 1:52 PM Nitin Motiani  wrote:
>
> Thanks for the feedback, Thomas.
>
> > Do I understand correctly that
> > the problem you encountered is in some other tests that you haven't
> > attached yet?  Could you post what you have so that others can see the
> > problem and perhaps have a chance of helping?
>
> Yes, we didn't add the failed tests to the patch. We'll add those and
> send new patches.
>

I'm attaching the patch files generated using git format-patch.

0001 has the pg_dump pipe support code.
0002 has the pg_restore pipe support.
0003 has a few basic validation tests for correct flag combinations.
0004 has the proposed documentation changes.

The above 4 are the same as before.

The 0005 patch is the new WIP patch file. This includes the tests
which we have been trying to add but which are failing (although the
same commands run fine manually).

The tests in this patch are added to src/bin/pg_dump/t/002_pg_dump.pl.
The original attempt was to have a test case with dump and restore
commands using the new flag and run it in multiple scenarios. But
since that was failing, for the ease of debugging I added a few
standalone tests which just run a pg_dump with the pipe-command flag.
In these tests, if the pipe-command is a simple command like 'cat' or
'gzip', the test passes. But if the pipe-command itself uses a pipe
(either to a file or another command), the test fails.

In the following test

 ['pg_dump', '-Fd', '-B', 'postgres', "--pipe-command=\"cat > $tempdir/%f\"",],]

I get the below error.

# 'sh: line 1: cat >
/usr/local/google/home/nitinmotiani/postgresql/src/bin/pg_dump/tmp_check/tmp_test_XpFO/toc.dat:
No such file or directory

I can see that the temp directory tmp_test_XpFO exists. Even when I
changed the test to use an absolute path to an existing directory, I
got the same error. When I do manual testing with the same
pipe-command, it works fine. That is why we think there is some issue
with our environment setup for the tap test where it is not able to
parse the command.

I also ran the following loop (started just before starting the test
run) to print the output of ps commands around 'cat >' to see what
happens.

 for i in $(seq 1 10000); do ps --forest -ef | grep "cat >" -A 5 >>
~/ps_output.txt; done

The printed results showed that the child process with the pipe
command became defunct.

 nitinmo+ 3180211 3180160  5 17:05 pts/1    00:00:00  |   |
   \_ /usr/local/google/home/nitinmotiani/postgresql/tmp_install/usr/local/pgsql/bin/pg_dump
-Fd -B p     ostgres --pipe-command="cat >
/usr/local/google/home/nitinmotiani/postgresql/src/bin/pg_dump/definite_dumpdir/%f"
 nitinmo+ 3180215 3180211  0 17:05 pts/1    00:00:00  |   |
       \_ [sh] 

We are not sure how to handle this issue. Please let us know your thoughts.

Thanks & Regards,
Nitin Motiani
Google

Re: Adding pg_dump flag for parallel export to pipes

От:
Hannu Krosing <hannuk@google.com>
Дата:
I have added this to the commitfest

We would be grateful for any reviews and feedback on this.

When adding to commitfest I tried to put Nitin as "first author" as he
has done the bulk of the work (I did just a quick pg_dump-only PoC)
but it looks like Commitfest just orders all provided authors
alphabetically .


Re: Adding pg_dump flag for parallel export to pipes

От:
Andrew Jackson <andrewjackson947@gmail.com>
Дата:
Hi,

Very interesting patch. One question: is it possible with this patch to pipe pg_dump directory output directly into pg_restore with this patch? Looking at the code I don't believe that is the case but figured I would ask.

Thanks,
Andrew Jackson

Re: Adding pg_dump flag for parallel export to pipes

От:
Andrew Jackson <andrewjackson947@gmail.com>
Дата:
Hi,

Went ahead and experimented with your patch a bit. To answer my previous question this patch can be used to pipe pg_dump directly into pg_restore. This should absolutely be added as another use case to your list above as it is a well known limitation that you can use pg_dump/psql to do buffered copy but only with a single process, while using pg_dump/pg_restore is capable of multiprocessed copy but it must be saved to disk in its entirety before the restore can begin. This is extremely frustrating when dealing with large databases where you don't want multiple copies saved on disk and because it's not as fast as it can be. With this patch you can get the best of both worlds. 

 Example dump
```bash
pg_dump --jobs=4 -Fd "${connection_str}" --pipe-command="mkfifo dumpdir/%f; cat >> dumpdir/%f"
```

Example restore run in different process
```bash
pg_restore --jobs=4 -Fd --dbname="${another_connection_str}" ./dumpdir
```
Thanks,
Andrew Jackson

Re: Adding pg_dump flag for parallel export to pipes

От:
Dilip Kumar <dilipbalaut@gmail.com>
Дата:
On Thu, Jun 5, 2025 at 6:09 PM Nitin Motiani  wrote:
>
> Hi,
>
> Apologies for the delay on this thread.
>
> On Mon, Apr 28, 2025 at 1:52 PM Nitin Motiani  wrote:
> >
> > Thanks for the feedback, Thomas.
> >
> > > Do I understand correctly that
> > > the problem you encountered is in some other tests that you haven't
> > > attached yet?  Could you post what you have so that others can see the
> > > problem and perhaps have a chance of helping?
> >
> > Yes, we didn't add the failed tests to the patch. We'll add those and
> > send new patches.
> >
>
> I'm attaching the patch files generated using git format-patch.
>
> 0001 has the pg_dump pipe support code.
> 0002 has the pg_restore pipe support.
> 0003 has a few basic validation tests for correct flag combinations.
> 0004 has the proposed documentation changes.
>
> The above 4 are the same as before.
>
> The 0005 patch is the new WIP patch file. This includes the tests
> which we have been trying to add but which are failing (although the
> same commands run fine manually).
>
> The tests in this patch are added to src/bin/pg_dump/t/002_pg_dump.pl.
> The original attempt was to have a test case with dump and restore
> commands using the new flag and run it in multiple scenarios. But
> since that was failing, for the ease of debugging I added a few
> standalone tests which just run a pg_dump with the pipe-command flag.
> In these tests, if the pipe-command is a simple command like 'cat' or
> 'gzip', the test passes. But if the pipe-command itself uses a pipe
> (either to a file or another command), the test fails.
>
> In the following test
>
>  ['pg_dump', '-Fd', '-B', 'postgres', "--pipe-command=\"cat > $tempdir/%f\"",],]
>
> I get the below error.
>
> # 'sh: line 1: cat >
> /usr/local/google/home/nitinmotiani/postgresql/src/bin/pg_dump/tmp_check/tmp_test_XpFO/toc.dat:
> No such file or directory
>
> I can see that the temp directory tmp_test_XpFO exists. Even when I
> changed the test to use an absolute path to an existing directory, I
> got the same error. When I do manual testing with the same
> pipe-command, it works fine. That is why we think there is some issue
> with our environment setup for the tap test where it is not able to
> parse the command.
>
> I also ran the following loop (started just before starting the test
> run) to print the output of ps commands around 'cat >' to see what
> happens.
>
>  for i in $(seq 1 10000); do ps --forest -ef | grep "cat >" -A 5 >>
> ~/ps_output.txt; done
>
> The printed results showed that the child process with the pipe
> command became defunct.
>
>  nitinmo+ 3180211 3180160  5 17:05 pts/1    00:00:00  |   |
>    \_ /usr/local/google/home/nitinmotiani/postgresql/tmp_install/usr/local/pgsql/bin/pg_dump
> -Fd -B p     ostgres --pipe-command="cat >
> /usr/local/google/home/nitinmotiani/postgresql/src/bin/pg_dump/definite_dumpdir/%f"
>  nitinmo+ 3180215 3180211  0 17:05 pts/1    00:00:00  |   |
>        \_ [sh] 
>
> We are not sure how to handle this issue. Please let us know your thoughts.

The latest patch set is not applying on HEAD can you rebase the patch
set.  And also there are many TODOs in the patch, if those TODOs are
just good to do and you are planning for future development better to
get rid of those.  OTOH if some of those TODOs are mandatory to do
before we can commit the patch then are you planning to work on those
soon?  I am planning to review this patch so are you planning to send
the rebased version with implementing the TODO which are required for
the first version.

-- 
Regards,
Dilip Kumar
Google


Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
On Tue, Sep 9, 2025 at 12:07 PM Dilip Kumar  wrote:
>
>
> The latest patch set is not applying on HEAD can you rebase the patch
> set.  And also there are many TODOs in the patch, if those TODOs are
> just good to do and you are planning for future development better to
> get rid of those.  OTOH if some of those TODOs are mandatory to do
> before we can commit the patch then are you planning to work on those
> soon?  I am planning to review this patch so are you planning to send
> the rebased version with implementing the TODO which are required for
> the first version.
>

Thanks for the feedback, Dilip. We will rebase the patch soon and send
it. Regarding the TODOs, some of those are plans for future
development (i.e. refactor). There are also TODOs in the first patch
file 0001 which are actually removed in file 0002. We can clean those
up or combine the two files. Other than that, some are about the open
questions. We will remove those from the code and will discuss those
issues on the thread.

Thanks,
Nitin Motiani
Google


Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
Hi Dilip,

I have rebased the patches and cleaned up the TODOs. Please take a
look. The fifth patch is still WIP because I'm working on figuring out
the test failure. I'll report my findings on that soon.

Thanks,
Nitin Motiani
Google


On Tue, Sep 9, 2025 at 8:41 PM Nitin Motiani  wrote:
>
> On Tue, Sep 9, 2025 at 12:07 PM Dilip Kumar  wrote:
> >
> >
> > The latest patch set is not applying on HEAD can you rebase the patch
> > set.  And also there are many TODOs in the patch, if those TODOs are
> > just good to do and you are planning for future development better to
> > get rid of those.  OTOH if some of those TODOs are mandatory to do
> > before we can commit the patch then are you planning to work on those
> > soon?  I am planning to review this patch so are you planning to send
> > the rebased version with implementing the TODO which are required for
> > the first version.
> >
>
> Thanks for the feedback, Dilip. We will rebase the patch soon and send
> it. Regarding the TODOs, some of those are plans for future
> development (i.e. refactor). There are also TODOs in the first patch
> file 0001 which are actually removed in file 0002. We can clean those
> up or combine the two files. Other than that, some are about the open
> questions. We will remove those from the code and will discuss those
> issues on the thread.
>
> Thanks,
> Nitin Motiani
> Google

Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
On Tue, Jan 13, 2026 at 4:20 PM Nitin Motiani  wrote:
>
> Hi Dilip,
>
> I have rebased the patches and cleaned up the TODOs. Please take a
> look. The fifth patch is still WIP because I'm working on figuring out
> the test failure. I'll report my findings on that soon.
>

Attaching the new version after rebasing and fixing a fallthrough bug
introduced in v6.

Thanks,
Nitin Motiani
Google

Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
Hi,

I fixed the failing tap test. Attaching rebased patches along with the
new test. The last patch is still WIP because I need to add more test
cases.

Thanks,
Nitin Motiani
Google

Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
Rebased and added some extra logs for testing.

Thanks

Re: Adding pg_dump flag for parallel export to pipes

От:
Mahendra Singh Thalor <mahi6run@gmail.com>
Дата:
On Wed, 4 Mar 2026 at 15:27, Nitin Motiani <nitinmotiani@google.com> wrote:
>
> Rebased and added some extra logs for testing.
>
> Thanks

Thanks Nitin for the updated patch.

+1 for this idea.

Here are my few review comments.

Comment1: 
02 is not applying so this should be rebased.

fix:
if (!no_globals)
                        n_errors = restore_global_objects(global_path, tmpopts, filespec_is_pipe);

Comment2:
03 patch has test cases. Please change these error messages as per other errors in code.

+command_fails_like(
+       [ 'pg_dump', '-Fd', '--pipe-command="cat"', '--compress=lz4', 'test'],
+       qr/\Qpg_dump: hint: Option --pipe-command is not supported with any compression type\E/,
+       'pg_dump: hint: Option --pipe-command is not supported with any compression type'
+);

Above should be:
+command_fails_like(
+       [ 'pg_dump', '-Fd', '--pipe-command="cat"', '--compress=lz4', 'test'],
+       qr/\Qpg_dump: error: option --pipe-command is not supported with any compression type\E/,
+       'pg_dump option --pipe-command is not supported with any compression type'
+);

Comment3:
Please can we rename "--pipe-command" to "--pipe" only.

Commen4: --pipe or --pipe-command should be added into the usage function in pg_dump.c and pg_restore.c files.

Comment5: I think we can support this new pipe option with pg_dumpall also as we support directory mode in pg_dumpall from v19.

Comment6:
mst@localhost:~/pg60/postgres/inst/bin$ ./pg_dump -Fd  --pipe-command="a" -d postgres
sh: line 1: a: command not found
pg_dump: error: could not close file: Success
pg_dump: error: could not close TOC file: Success
mst@localhost:~/pg60/postgres/inst/bin$

we should add more processing for the  "--pipe-command" argument so that we don't get the above error. I think we should validate the path.

Comment7: + bool path_is_pipe_command)
I think we can rename to is_pipe, similarly filespec_is_pipe -> is_pipe, fSpecIsPipe->is_pipe

Comment8:
+        This option is only supported with the directory output
+        format. It can be used to write to multiple streams which
+        otherwise would not be possible with the directory mode.
+        For each stream, it starts a process which runs the
+        specified command and pipes the pg_dump output to this
+        process.
+        This option is not valid if <option>--file</option>
+        is also specified.
+       </para>
+       <para>
+        The pipe-command can be used to perform operations like compress
+        using a custom algorithm, filter, or write the output to a cloud
+        storage etc. The user would need a way to pipe the final output of
+        each stream to a file. To handle that, the pipe command supports a format
+        specifier %f. And all the instances of %f in the command string
+        will be replaced with the corresponding file name which
+        would have been used in the directory mode with <option>--file</option>.
+        See <xref linkend="pg-dump-examples"/> below.
+       </para>
+      </listitem>
+     </varlistentry>

please use 80 chars in all lines.

Comment9:
mst@localhost:~/pg60/postgres/inst/bin$ ./pg_restore dumpdir --pipe-command="cat"
pg_restore: hint: Only one of [filespec, --pipe-command] allowed

Above error should be similar to the other errors in pg_restore.

Comment10:
mst@localhost:~/pg60/postgres/inst/bin$ ./pg_restore  --pipe-command="cat" -d postgres --format=tar
pg_restore: error: could not open TOC file "cat" for input: No such file or directory
mst@localhost:~/pg60/postgres/inst/bin$

we should add an error for non-directory format. If no format is specified, then we can continue or for unknown format also we can report an error.

I will do some more review and testing.

--
Thanks and Regards
Mahendra Singh Thalor
EnterpriseDB: http://www.enterprisedb.com

Re: Adding pg_dump flag for parallel export to pipes

От:
Hannu Krosing <hannuk@google.com>
Дата:
On Sat, Mar 14, 2026 at 10:37 PM Mahendra Singh Thalor
 wrote:
>
Thanks for reviewing this.

> Comment3:
> Please can we rename "--pipe-command" to "--pipe" only.

the original choice was inspired by the COPY command which has

COPY ... TO { 'filename' | PROGRAM 'command' | STDOUT }
COPY ... FROM { 'filename' | PROGRAM 'command' | STDIN }

and the lack of TO or FROM, so PROGRAM 'command' became --pipe-command

Other options could be --to-pipe for pg_dump and --from-pipe for pg_restore.
Or --to-program , --from-program to be closer to COPY syntax,

Or even --pipe-command-pattern to signify that there is some name
replacement done for each invocation

But thinking more about it the option name does indeed not need to be
fully self-documenting :)

So I would be ok to shortening it to just --pipe

---
Regards
Hannu


Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
Thanks a lot for your feedback Mahendra. I have rebased the changes. I'll follow up on your other comments regarding cleanup and the shortened flag name. I'm currently uploading the latest version post rebase as I've made some changes to fix the test failures. Will let cfbot run those once.

Regards
Nitin Motiani,
Google

Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
Hi Mahendra,

I'm attaching the latest version of the patch. This incorporates
several suggestions Mahendra made. I've also fixed the failing test
and added support for Windows in the test by using perlbin instead of
cat. Here is the list of changes in this version:

1. Changed pipe-command to pip.
2. Added better error handling for invalid command.
3. Added shell escaping in the command before setting it as the file path.
4. I decided to change the mode to PG_BINARY_W for the large object
toc files even in the standard case. If there is a concern, we can
revert to the older version.
5. Added a bunch of new tests for various scenarios and parallelism.

Currently these changes are in a separate commit. After the review, I
can squash the first 3 commits of pg_dump, pg_restore, and the fixes
in one commit.

On Sat, Mar 14, 2026 at 10:37 PM Mahendra Singh Thalor
 wrote:
>
> Comment5: I think we can support this new pipe option with pg_dumpall also as we support directory mode in pg_dumpall from v19.

Regarding this, I haven't looked into the details of how that would
work. If the rest look good, we can consider implementing this in
another patch. For the time being, I've skipped this option in
pg_dumpall and the global restore. Let me know what you think.

Thanks

Nitin Motiani,
Google

Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
I did another round of rebase and added a couple of extra tests.
Changed error messages in the tests for windows.


> 3. Added shell escaping in the command before setting it as the file path.

Removed this because it might have been causing Windows test failures.
Since the command runs from the client, this step isn't necessary.

Thanks

Nitin Motiani
Google

Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
Changed how pipe commands are quoted in the Windows test. The latest
versions are attached.

Thanks

Nitin Motiani
Google

Re: Adding pg_dump flag for parallel export to pipes

От:
solai v <solai.cdac@gmail.com>
Дата:
Hi all,

Thank you for the updated patch.

On Fri, May 22, 2026 at 1:03 PM Nitin Motiani  wrote:
>
> Changed how pipe commands are quoted in the Windows test. The latest
> versions are attached.

I worked on reproducing the current limitation around parallel dumps
and then tested the latest v16 patch adding --pipe support for
pg_dump. To begin with, I verified the existing behavior.
For example:
pg_dump postgres | gzip > dump.sql.gz works, but does not support parallelism,
whereas:
pg_dump -Fd -j 4 -f dumpdir postgres
du -sh dumpdir
21M dumpdir
requires intermediate disk storage. This demonstrates the current
limitation where users must choose between parallelism and streaming
pipelines.
I then tested the patch introducing --pipe support. The feature is
quite useful for modern workflows where users want to stream dump
output directly to compression or upload pipelines without relying on
intermediate storage. Basic functionality worked as expected.
For example:
pg_dump -p 55432 -Fd -j 4 --pipe="cat > dump.out" postgres, produced a
~38MB output file,
and:
pg_dump -p 55432 -Fd -j 4 --pipe="gzip > dump.gz" postgres produced, a
compressed file (~11MB).
The initial contents appeared valid:
gunzip -c dump.gz | head
1
2
3
...
Also, no intermediate directory was created, confirming that the patch
enables streaming without filesystem-backed staging. Error handling
also behaved correctly.
For example:
--pipe="invalid_cmd"
resulted in:
pg_dump: error: pipe command failed: command not found
and:
--pipe="gzip | false"
resulted in:
pg_dump: error: pipe command failed: child process exited with exit code 1
However, I observed an important issue when using the feature with
multiple parallel workers. Since the pipe command is executed per
output file, using: --pipe="gzip > dump.gz", it results in multiple
workers invoking independent gzip processes that all write to the same
output file. This leads to corrupted or truncated output.
In my testing:
gunzip -c dump.gz > dump.sql
failed with:
gzip: dump.gz: unexpected end of file
This suggests that concurrent writes to a shared output target are not
coordinated and can result in invalid dumps. It would be helpful to
clarify expected usage patterns here. For example: whether users are
expected to generate distinct outputs per worker, or whether
safeguards should be implemented to prevent multiple workers from
writing to the same destination. Additionally, during failure
scenarios I observed backend logs such as:
FATAL: connection to client lost
Broken pipe
While this is expected when the pipe terminates prematurely, it may be
worth considering whether error messaging or cleanup behavior can be
made clearer from the user perspective.
Overall, the feature is valuable and aligns well with modern backup
workflows. However, behavior in multi-worker scenarios with shared
pipe targets may need further clarification or safeguards to avoid
data corruption. Looking forward to more feedback.


Regards.
Solai


Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
On Fri, May 22, 2026 at 4:04 PM solai v  wrote:
> I then tested the patch introducing --pipe support. The feature is
> quite useful for modern workflows where users want to stream dump
> output directly to compression or upload pipelines without relying on
> intermediate storage.

Thank you for the feedback.

> gzip: dump.gz: unexpected end of file
> This suggests that concurrent writes to a shared output target are not
> coordinated and can result in invalid dumps. It would be helpful to
> clarify expected usage patterns here. For example: whether users are
> expected to generate distinct outputs per worker, or whether
> safeguards should be implemented to prevent multiple workers from
> writing to the same destination.

I added a warning for cases where the pipe command provided with
parallel dump and restore doesn't contain a `%f`. We can also add it
to the documentation. Let me know what you think.

> scenarios I observed backend logs such as:
> FATAL: connection to client lost
> Broken pipe
> While this is expected when the pipe terminates prematurely, it may be
> worth considering whether error messaging or cleanup behavior can be
> made clearer from the user perspective.

I added the failed command to the error message. I'm not sure if we
can do any auto-cleanup commands which succeeded.

Thanks & Regards

Nitin Motiani
Google

Re: Adding pg_dump flag for parallel export to pipes

От:
solai v <solai.cdac@gmail.com>
Дата:
Hi all,

On Tue, Jun 9, 2026 at 12:04 PM Nitin Motiani  wrote:
>
> On Fri, May 22, 2026 at 4:04 PM solai v  wrote:
> > I then tested the patch introducing --pipe support. The feature is
> > quite useful for modern workflows where users want to stream dump
> > output directly to compression or upload pipelines without relying on
> > intermediate storage.
>
> Thank you for the feedback.
>
> > gzip: dump.gz: unexpected end of file
> > This suggests that concurrent writes to a shared output target are not
> > coordinated and can result in invalid dumps. It would be helpful to
> > clarify expected usage patterns here. For example: whether users are
> > expected to generate distinct outputs per worker, or whether
> > safeguards should be implemented to prevent multiple workers from
> > writing to the same destination.
>
> I added a warning for cases where the pipe command provided with
> parallel dump and restore doesn't contain a `%f`. We can also add it
> to the documentation. Let me know what you think.
>
> > scenarios I observed backend logs such as:
> > FATAL: connection to client lost
> > Broken pipe
> > While this is expected when the pipe terminates prematurely, it may be
> > worth considering whether error messaging or cleanup behavior can be
> > made clearer from the user perspective.
>
> I added the failed command to the error message. I'm not sure if we
> can do any auto-cleanup commands which succeeded.
>


Thank you for the updated patches. I reviewed and tested the latest
v17 patch series in my current cluster. I tested the new --pipe
functionality with both serial and parallel directory dumps and
correctly generated:
pipe_dump/
    toc.dat
    3475.dat
    3476.dat
The same behavior was verified with parallel jobs (-j 4). I also
tested compression through an external command:
pg_dump -p 55432 -Fd -j 4 \
    --pipe='gzip > pipe_dump/%f.gz' \
    postgres
which successfully generated:
pipe_dump/
    toc.dat.gz
    3475.dat.gz
    3476.dat.gz
without creating an intermediate dump directory.
I verified error handling (--pipe='invalid_cmd') as well and correctly
reported the error as "pg_dump: error: pipe command failed:
"invalid_cmd": command not found".
Similarly, --pipe='gzip | false' correctly propagated the child
process failure. I also performed an end-to-end validation of the new
functionality by restoring the generated archive using pg_restore
--pipe. The restore completed successfully, and row counts for the
test tables matched the original database confirming that dump
generation, compression, restore, and data integrity all worked
correctly. One particularly useful improvement I noticed is the
handling of parallel jobs when the %f placeholder is omitted,
producing the warning: "pg_dump: warning: parallel jobs with --pipe
usually require the "%f" placeholder to avoid data corruption from
multiple workers writing to the same file". This is an usability
improvement, as it explicitly warns users against a common misuse that
could otherwise lead to corrupted output when multiple workers write
to the same destination file. Overall, the patch series worked well
and the new --pipe support behaves correctly in my testing, also the
newly added warning for missing %f significantly improves the user
experience for parallel jobs. The patch looks good to me.


Regards,
Solai


Re: Adding pg_dump flag for parallel export to pipes

От:
Nitin Motiani <nitinmotiani@google.com>
Дата:
Вложения:
The pipe command parsing for Windows keeps failing. So I'm skipping
the tests on Windows for now.

Thanks

Nitin Motiani,
Google
FAQ