[PATCH 0/2] Parallelizing the archiver

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

[PATCH 0/2] Parallelizing the archiver

От:
Stepan Neretin <slpmcf@gmail.com>
Дата:
Вложения:
Hi,

Follow up to [1] and the older parallel archiver threads [2][3].

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] https://www.postgresql.org/message-id/CA%2BYyo5QRxow54ekSUWu4qzckYWhSg5BWiFfQ37UdU3QGBC3Kxg%40mail.gmail.com
[2] https://www.postgresql.org/message-id/flat/BC335D75-105B-403F-9473-976C8BBC32E3%40yandex-team.ru
[3] https://www.postgresql.org/message-id/BC4D6BB2-6976-4397-A417-A6A30EEDC63E%40amazon.com

Re: [PATCH 0/2] Parallelizing the archiver

От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:
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.



Re: [PATCH 0/2] Parallelizing the archiver

От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:
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.



FAQ