Re: Checkpointer write combining

From: Melanie Plageman <melanieplageman(at)gmail(dot)com>
To: Andrey Borodin <x4mmm(at)yandex-team(dot)ru>
Cc: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, Nazir Bilal Yavuz <byavuz81(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Andres Freund <andres(at)anarazel(dot)de>, tristan(dot)yim(at)gmail(dot)com
Subject: Re: Checkpointer write combining
Date: 2026-12-02 01:03:18
Message-ID: CAAKRu_b0jEbEcOasUY6UtVjdecYvcqVYFAfLqrn4E85GOUWmSw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Sep 14, 2026 at 2:29 PM Andrey Borodin <x4mmm(at)yandex-team(dot)ru> wrote:
>
> I continued reviewing v16, focusing on checkpointer and bgwriter.

Thanks! Attached v17 addresses the review comments in this email plus
fixes some issues I found on further self and LLM-review. It does not
yet include feedback from other emails. I wanted to get the new
version out since it needed a rebase.

> In 0004:
>
> > + CheckpointWriteDelay(flags, (double) num_processed / num_to_scan,
> > + processed);
>
> This is now called once per batch instead of once per processed buffer,
> but each call can still sleep for at most 100 ms. This can make smaller
> spread checkpoints finish much earlier.

In attached v17, I fixed the issue with spread checkpoint delay by
allowing up to one 100 ms delay per page processed. We recheck the
schedule after each delay so the checkpointer can resume writing as
soon as it falls behind.

> In 0006:
>
> > + for (; num_to_scan > 0; num_to_scan--, next_to_clean++)
> > ...
> > + if (++num_written >= bgwriter_lru_maxpages)
> > {
> > + PendingBgWriterStats.maxwritten_clean++;
> > + break;
> > }
>
> Reaching the limit skips the for-loop update. The last buffer is
> counted as reusable but omitted from scan progress. The saved position
> still points to it. Advancing and wrapping the scan position after
> selecting the buffer, before processing it, should avoid this.
> The issue survives in BgwriterWriteBuffers() later in the series.

I fixed this and went back to a loop structure that was more similar
to how it was before.

> In 0018:
>
> > + GatherContiguousDirtyBuffers(bufHdr, IOCONTEXT_NORMAL, &batch);
> > + WriteBuffers(&batch);
> > + CompleteWriteBuffers(&batch, wb_context);
> > + num_written += batch.n;
> >
> > + if (num_written >= lru_maxpages)
>
> The combined write can already exceed bgwriter_lru_maxpages by the time
> we check it. Calling run_bgwriter_cleaner(1) in the existing test
> writes all six contiguous buffers. Could GatherContiguousDirtyBuffers()
> also take the remaining per-round budget, so the documented limit
> still holds?

Yes, we could do something like this, but I was wondering if
bgwriter_lru_maxpages should really be in terms of pages still or if
we should think of it at least partially in terms of IOs. It is to
keep the bgwriter from doing too much work, but if it combines the
pages, then it is going to take up fewer IOPs. Maybe the easiest thing
to do is to just change my code to make sure it doesn't exceed
lru_maxpages and then people can tune it differently, though... I
haven't done anything yet in this version.

- Melanie

Attachment Content-Type Size
v17-0001-Allow-PinBuffer-to-skip-increasing-usage-count.patch text/x-patch 5.7 KB
v17-0002-Add-database-Oid-to-CkptSortItem.patch text/x-patch 2.5 KB
v17-0003-Inline-BufferSync-into-CheckPointBuffers.patch text/x-patch 7.0 KB
v17-0004-Write-combining-for-checkpointer.patch text/x-patch 30.1 KB
v17-0005-Add-checkpointer-write-combining-test.patch text/x-patch 11.6 KB
v17-0006-Remove-SyncOneBuffer-and-refactor-BgBufferSync.patch text/x-patch 9.2 KB
v17-0007-pg_stat_io-Count-buffer-eviction-after-invalidat.patch text/x-patch 3.0 KB
v17-0008-Call-BufferLockUnlock-directly-where-the-buffer-.patch text/x-patch 1.9 KB
v17-0009-Make-StrategyRejectBuffer-encapsulate-more-logic.patch text/x-patch 5.8 KB
v17-0010-Split-StrategyGetBuffer-into-source-specific-hel.patch text/x-patch 9.9 KB
v17-0011-Introduce-ClaimVictimBuffer-helper.patch text/x-patch 8.0 KB
v17-0012-Don-t-retry-the-strategy-ring-after-a-failed-clo.patch text/x-patch 18.9 KB
v17-0013-Write-combining-for-client-backends.patch text/x-patch 20.8 KB
v17-0014-Add-client-backend-write-combining-test.patch text/x-patch 13.8 KB
v17-0015-Add-COPY-FROM-write-combining-test.patch text/x-patch 2.2 KB
v17-0016-Add-VACUUM-write-combining-test.patch text/x-patch 2.3 KB
v17-0017-Add-bgwriter-helper-to-write-buffers.patch text/x-patch 5.8 KB
v17-0018-Write-combining-for-background-writer.patch text/x-patch 5.2 KB
v17-0019-Add-bgwriter-write-combining-test.patch text/x-patch 9.1 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Jelte Fennema-Nio 2026-12-02 01:08:58 Re: Reapply graceful socket shutdown on Windows (revert 29992a6a509)
Previous Message Ajin Cherian 2026-12-02 00:59:54 Re: Add aborted_txns and aborted_bytes to pg_stat_replication_slots