[PATCH 0/2] Parallelizing the archiver
Thread notes
1 note#feature#moderate#enhancement
hackorum-bot#feature#moderate#enhancement
@global #feature #moderate #enhancement
Hi,
Follow up to [1]/messages/by-id/CA+Yyo5QRxow54ekSUWu4qzckYWhSg5BWiFfQ37UdU3QGBC3Kxg@mail.gmail.com and the older parallel archiver threads [2]/messages/by-id/BC335D75-105B-403F-9473-976C8BBC32E3@yandex-team.ru[3]/messages/by-id/BC4D6BB2-6976-4397-A417-A6A30EEDC63E@amazon.com.
Today archiving often runs in peaks. One file starts, finishes, then the
next starts, so throughput is stop and go. That is especially noticeable
when the archive target is slow, for example object storage such as S3 or
a distant NFS share, where each file pays a long wait. Async work keeps
several files in flight and reports each one as it completes, so
utilization stays high instead of sitting idle between handoffs.
There is a second pain point. pgarch currently blocks inside system() for
the whole archive_command, so a hung script can leave the archiver stuck
until someone kills the child by hand.
The idea here is simple. pgarch keeps a small fixed pool of archive
workers. Ready files go into free slots, and when a worker finishes the
next file takes its place. Core still marks .done and can cancel stuck
work on shutdown. archive_command_jobs sets the pool size, default 1.
On Unix values from 2 to 8 start one short lived archive_command child
per slot. The feature is not available on Windows yet. Existing sync
modules keep working as before.
Attached archive_jobs_bench.sh builds a .ready backlog, then times how
long archive_command_jobs takes to drain it. The command optionally
sleeps, then copies each WAL file into an archive directory.
PGBIN=/path/to/bin ./archive_jobs_bench.sh [rounds] [delay_ms/jobs ...]
delay=500ms + cp, 16 files (median of 3)
jobs=1 8.35s
jobs=2 4.18s (~2.0x)
jobs=4 2.12s (~3.9x)
jobs=8 1.10s (~7.6x)
delay=0 + cp, 120 files (median of 3)
jobs=1 1.93s
jobs=2 1.02s (~1.9x)
jobs=4 0.43s (~4.5x)
jobs=8 0.29s (~6.7x)
Andrey Borodin put the same peak load complaint from the walg side and
preferred a pipe style protocol over batches. Would it make sense to grow
this later into that shape, with pgarch feeding file names and getting
completed or failed files back one by one?
I used AI to sketch the first prototype, then cleaned it up by hand. The
series is still large, but the shape looks promising to me. Does this
direction seem reasonable?
Regards,
Stepan Neretin
[1]: /messages/by-id/CA+Yyo5QRxow54ekSUWu4qzckYWhSg5BWiFfQ37UdU3QGBC3Kxg@mail.gmail.com
/messages/by-id/CA+Yyo5QRxow54ekSUWu4qzckYWhSg5BWiFfQ37UdU3QGBC3Kxg@mail.gmail.com
[2]: /messages/by-id/BC335D75-105B-403F-9473-976C8BBC32E3@yandex-team.ru
/messages/by-id/BC335D75-105B-403F-9473-976C8BBC32E3@yandex-team.ru
[3]: /messages/by-id/BC4D6BB2-6976-4397-A417-A6A30EEDC63E@amazon.com
/messages/by-id/BC4D6BB2-6976-4397-A417-A6A30EEDC63E@amazon.com
Attachments:
t259648_10001-Support-async-archive-callbacks-in-pgarch.patchtext/x-patch; charset=US-ASCII; name=0001-Support-async-archive-callbacks-in-pgarch.patchDownload+551-134
0002-Add-archive_command_jobs.patchtext/x-patch; charset=US-ASCII; name=0002-Add-archive_command_jobs.patchDownload+533-54
archive_jobs_bench.shtext/x-shellscript; charset=US-ASCII; name=archive_jobs_bench.shDownload
Hi Stepan,
On November 22, 2026, Stepan Neretin wrote:
with pgarch feeding file names and getting
completed or failed files back one by one?
Yes, that is still the direction I'd prefer. I'd like us to consider
the archive/restore interface more broadly than parallel execution of
archive_command.
One missing piece is distinguishing a missing file from a failed
attempt to check or retrieve it. We use restore_command results to
discover timelines. Today, an ordinary command failure followed by a
missing local history file makes existsTimeLineHistory() return false.
An unavailable archive can therefore look like an unused timeline ID.
PostgreSQL needs to distinguish not-found from retryable and
non-retryable errors, rather than infer all of that from success or
failure of a shell command. That is a correctness issue, not a
throughput optimization.
For archiving, I'd like a stream of ready files and independent
per-file acknowledgments, without waiting for a whole batch. Completion
can be out of order. The contract should distinguish a file being
durably archived from merely being queued, and describe retry behavior.
Your submit/poll callbacks look like a useful starting point.
Tools such as WAL-G and pgBackRest already implement parallelism behind
the synchronous API. In WAL-G we predict adjacent .ready names to avoid
directory scans, periodically scan for missed or failed uploads, and
make our own scheduling decisions. PostgreSQL still sees what looks
like a request to push one file. I'd rather give these tools the
information they need than make them reconstruct it from archive_status.
I'd also like PostgreSQL to control archive/restore concurrency limits
and select work, while leaving execution to the tool. For example,
WAL-G prefetch guesses subsequent segment names and can issue useless
requests on the old timeline near a switch. When PostgreSQL already has
the relevant history, it knows where that switch is. Passing the
intended files to the tool would avoid duplicating that knowledge.
I don't suggest implementing all of this in one patch. Could we first
agree on the contract we want the async interface to support, including
a long-lived external process and the restore side, and then build it
incrementally?
Thank you!
Best regards, Andrey Borodin.
Hi Stepan,
On November 22, 2026, Stepan Neretin wrote:
with pgarch feeding file names and getting
completed or failed files back one by one?
Yes, that is still the direction I'd prefer. I'd like us to consider
the archive/restore interface more broadly than parallel execution of
archive_command.
One missing piece is distinguishing a missing file from a failed
attempt to check or retrieve it. We use restore_command results to
discover timelines. Today, an ordinary command failure followed by a
missing local history file makes existsTimeLineHistory() return false.
An unavailable archive can therefore look like an unused timeline ID.
PostgreSQL needs to distinguish not-found from retryable and
non-retryable errors, rather than infer all of that from success or
failure of a shell command. That is a correctness issue, not a
throughput optimization.
For archiving, I'd like a stream of ready files and independent
per-file acknowledgments, without waiting for a whole batch. Completion
can be out of order. The contract should distinguish a file being
durably archived from merely being queued, and describe retry behavior.
Your submit/poll callbacks look like a useful starting point.
Tools such as WAL-G and pgBackRest already implement parallelism behind
the synchronous API. In WAL-G we predict adjacent .ready names to avoid
directory scans, periodically scan for missed or failed uploads, and
make our own scheduling decisions. PostgreSQL still sees what looks
like a request to push one file. I'd rather give these tools the
information they need than make them reconstruct it from archive_status.
I'd also like PostgreSQL to control archive/restore concurrency limits
and select work, while leaving execution to the tool. For example,
WAL-G prefetch guesses subsequent segment names and can issue useless
requests on the old timeline near a switch. When PostgreSQL already has
the relevant history, it knows where that switch is. Passing the
intended files to the tool would avoid duplicating that knowledge.
I don't suggest implementing all of this in one patch. Could we first
agree on the contract we want the async interface to support, including
a long-lived external process and the restore side, and then build it
incrementally?
Thank you!
Best regards, Andrey Borodin.
On Mon, Nov 23, 2026 at 2:47 PM Andrey Borodin <x4mmm@yandex-team.ru> wrote:
Hi Stepan,
On November 22, 2026, Stepan Neretin wrote:
with pgarch feeding file names and getting
completed or failed files back one by one?Yes, that is still the direction I'd prefer. I'd like us to consider
the archive/restore interface more broadly than parallel execution of
archive_command.One missing piece is distinguishing a missing file from a failed
attempt to check or retrieve it. We use restore_command results to
discover timelines. Today, an ordinary command failure followed by a
missing local history file makes existsTimeLineHistory() return false.
An unavailable archive can therefore look like an unused timeline ID.
PostgreSQL needs to distinguish not-found from retryable and
non-retryable errors, rather than infer all of that from success or
failure of a shell command. That is a correctness issue, not a
throughput optimization.For archiving, I'd like a stream of ready files and independent
per-file acknowledgments, without waiting for a whole batch. Completion
can be out of order. The contract should distinguish a file being
durably archived from merely being queued, and describe retry behavior.
Your submit/poll callbacks look like a useful starting point.Tools such as WAL-G and pgBackRest already implement parallelism behind
the synchronous API. In WAL-G we predict adjacent .ready names to avoid
directory scans, periodically scan for missed or failed uploads, and
make our own scheduling decisions. PostgreSQL still sees what looks
like a request to push one file. I'd rather give these tools the
information they need than make them reconstruct it from archive_status.I'd also like PostgreSQL to control archive/restore concurrency limits
and select work, while leaving execution to the tool. For example,
WAL-G prefetch guesses subsequent segment names and can issue useless
requests on the old timeline near a switch. When PostgreSQL already has
the relevant history, it knows where that switch is. Passing the
intended files to the tool would avoid duplicating that knowledge.I don't suggest implementing all of this in one patch. Could we first
agree on the contract we want the async interface to support, including
a long-lived external process and the restore side, and then build it
incrementally?Thank you!
Best regards, Andrey Borodin.
Hi Andrey,
Thanks for the reply.
I agree: stream ready files, get answers one by one, keep a long-lived
helper, and grow the restore side later instead of doing everything at
once.
On Discord, Robert Haas and Christoph Berg said much the same about the
jobs-pool draft. Robert did not want one short-lived child per file:
warm connections and parallel transfer should live in one helper, or in
a C module. Christoph wanted archive_library and a simple stdin
protocol with per-file ok/fail answers. So please treat
archive_command_jobs as superseded.
This series is only the archive side. A module can keep the old sync
callback, or use submit/poll/cancel. Each file comes back as success,
retry, or fail, and only pgarch marks .done. archive_async_max limits
how many files are in flight (default 8). For shell users there is one
long-lived helper:
-> archive\t<file>\t<path>
<- archived\t<file>\tok|retry|fail
No handshake in v1, no %f/%p, Unix only.
Restore can reuse the same helper later with a second verb, plus a
missing answer so timeline probes can tell "not in archive" from "try
again". Something like:
-> restore\t<file>\t<path>
<- restored\t<file>\tok|missing|retry|fail
Classic restore_command would stay as it is. I would rather settle the
archive half first, then mail that as a follow-up.
Attached v2:
0001 Support async archive callbacks in pgarch.
0002 Add persistent archive_command protocol.
0003 Add TAP coverage for persistent archive_command.
If 0001 alone is easier to review first, I can hold the rest.
Does this direction look closer?
Regards,
Stepan Neretin
[1]: /messages/by-id/CA+Yyo5TfMpUE3s=WKrWcaYbBWbO2VNyRnRt_5hy-VPrMhjXVWg@mail.gmail.com
/messages/by-id/CA+Yyo5TfMpUE3s=WKrWcaYbBWbO2VNyRnRt_5hy-VPrMhjXVWg@mail.gmail.com
[2]: /messages/by-id/ECB4BF94-9B6F-4A85-ADA7-4AFBD77C03CD@yandex-team.ru
/messages/by-id/ECB4BF94-9B6F-4A85-ADA7-4AFBD77C03CD@yandex-team.ru
[3]: https://commitfest.postgresql.org/patch/7686/
Attachments:
t259648_40001-Support-async-archive-callbacks-in-pgarch.patchtext/x-patch; charset=US-ASCII; name=0001-Support-async-archive-callbacks-in-pgarch.patchDownload+639-136
0003-Add-TAP-coverage-for-persistent-archive_command.patchtext/x-patch; charset=US-ASCII; name=0003-Add-TAP-coverage-for-persistent-archive_command.patchDownload+264-1
0002-Add-persistent-archive_command-protocol.patchtext/x-patch; charset=US-ASCII; name=0002-Add-persistent-archive_command-protocol.patchDownload+727-10
On Sun, Nov 29, 2026 at 12:33 AM Stepan Neretin <slpmcf@gmail.com> wrote:
On Mon, Nov 23, 2026 at 2:47 PM Andrey Borodin <x4mmm@yandex-team.ru>
wrote:Hi Stepan,
On November 22, 2026, Stepan Neretin wrote:
with pgarch feeding file names and getting
completed or failed files back one by one?Yes, that is still the direction I'd prefer. I'd like us to consider
the archive/restore interface more broadly than parallel execution of
archive_command.One missing piece is distinguishing a missing file from a failed
attempt to check or retrieve it. We use restore_command results to
discover timelines. Today, an ordinary command failure followed by a
missing local history file makes existsTimeLineHistory() return false.
An unavailable archive can therefore look like an unused timeline ID.
PostgreSQL needs to distinguish not-found from retryable and
non-retryable errors, rather than infer all of that from success or
failure of a shell command. That is a correctness issue, not a
throughput optimization.For archiving, I'd like a stream of ready files and independent
per-file acknowledgments, without waiting for a whole batch. Completion
can be out of order. The contract should distinguish a file being
durably archived from merely being queued, and describe retry behavior.
Your submit/poll callbacks look like a useful starting point.Tools such as WAL-G and pgBackRest already implement parallelism behind
the synchronous API. In WAL-G we predict adjacent .ready names to avoid
directory scans, periodically scan for missed or failed uploads, and
make our own scheduling decisions. PostgreSQL still sees what looks
like a request to push one file. I'd rather give these tools the
information they need than make them reconstruct it from archive_status.I'd also like PostgreSQL to control archive/restore concurrency limits
and select work, while leaving execution to the tool. For example,
WAL-G prefetch guesses subsequent segment names and can issue useless
requests on the old timeline near a switch. When PostgreSQL already has
the relevant history, it knows where that switch is. Passing the
intended files to the tool would avoid duplicating that knowledge.I don't suggest implementing all of this in one patch. Could we first
agree on the contract we want the async interface to support, including
a long-lived external process and the restore side, and then build it
incrementally?Thank you!
Best regards, Andrey Borodin.
Hi Andrey,
Thanks for the reply.
I agree: stream ready files, get answers one by one, keep a long-lived
helper, and grow the restore side later instead of doing everything at
once.On Discord, Robert Haas and Christoph Berg said much the same about the
jobs-pool draft. Robert did not want one short-lived child per file:
warm connections and parallel transfer should live in one helper, or in
a C module. Christoph wanted archive_library and a simple stdin
protocol with per-file ok/fail answers. So please treat
archive_command_jobs as superseded.This series is only the archive side. A module can keep the old sync
callback, or use submit/poll/cancel. Each file comes back as success,
retry, or fail, and only pgarch marks .done. archive_async_max limits
how many files are in flight (default 8). For shell users there is one
long-lived helper:-> archive\t<file>\t<path>
<- archived\t<file>\tok|retry|failNo handshake in v1, no %f/%p, Unix only.
Restore can reuse the same helper later with a second verb, plus a
missing answer so timeline probes can tell "not in archive" from "try
again". Something like:-> restore\t<file>\t<path>
<- restored\t<file>\tok|missing|retry|failClassic restore_command would stay as it is. I would rather settle the
archive half first, then mail that as a follow-up.Attached v2:
0001 Support async archive callbacks in pgarch.
0002 Add persistent archive_command protocol.
0003 Add TAP coverage for persistent archive_command.If 0001 alone is easier to review first, I can hold the rest.
Does this direction look closer?
Regards,
Stepan Neretin[1]
/messages/by-id/CA+Yyo5TfMpUE3s=WKrWcaYbBWbO2VNyRnRt_5hy-VPrMhjXVWg@mail.gmail.com
[2]
/messages/by-id/ECB4BF94-9B6F-4A85-ADA7-4AFBD77C03CD@yandex-team.ru
[3] https://commitfest.postgresql.org/patch/7686/
Hi,
CFBot went red on Linux on the hung helper shutdown test. Debian images
use dash as /bin/sh and it forks the helper instead of executing it so
killing only the shell left the Perl process behind.
Fixed in the same series. The helper calls setsid and cancel signals
the process group. The TAP hang command ends with true so bash cannot
hide the same bug by exec.
Regards,
Stepan Neretin