[PATCH] vacuum: stop using stream ring after failsafe

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

[PATCH] vacuum: stop using stream ring after failsafe

От:
Jingtang Zhang <mrdrivingduck@gmail.com>
Дата:
Вложения:
Hi,

4830f1024325 made VACUUM's failsafe abandon its buffer access strategy
and use normal shared-buffer replacement.  Commit 9256822608f3 later
converted VACUUM's first phase to use streaming read I/O.

Was it an oversight in the read stream conversion that the stream is
initialized with VACUUM's strategy, but is not updated when failsafe
clears vacrel->bstrategy?  As a result, the active stream continues to
use the VACUUM ring for later heap reads.

This patch allows a read stream's strategy to be changed for future I/O.
VACUUM switches the active stream to a NULL strategy when failsafe
activates, without modifying in-flight I/O operations.

The stream calculates its pin limit and queue capacity when it is
created. It uses the original strategy.  This patch deliberately changes
only buffer replacement for future reads; it does not resize the stream.

I am unsure whether a generic strategy setter is the right interface.  A
new strategy must accommodate the stream's fixed maximum number of pins.

I tested this manually with a 600000-block unlogged table and a 256 kB
VACUUM buffer usage limit.  A second session advanced the XID counter
after VACUUM began scanning, causing the 4 GB periodic check to enter
failsafe mode.  On my test instance, without this patch, the table's
main fork retained exactly 32 buffers in pg_buffercache.  With this
patch, it retained 30665 buffers after failsafe activation.

---
Regards,
Jingtang

Re: [PATCH] vacuum: stop using stream ring after failsafe

От:
Melanie Plageman <melanieplageman@gmail.com>
Дата:
Вложения:
On Wed, Jul 22, 2026 at 12:53 PM Jingtang Zhang  wrote:
>
> 4830f1024325 made VACUUM's failsafe abandon its buffer access strategy
> and use normal shared-buffer replacement.  Commit 9256822608f3 later
> converted VACUUM's first phase to use streaming read I/O.
>
> Was it an oversight in the read stream conversion that the stream is
> initialized with VACUUM's strategy, but is not updated when failsafe
> clears vacrel->bstrategy?  As a result, the active stream continues to
> use the VACUUM ring for later heap reads.

Thanks for the report and patch. Yes, this was an oversight.

> This patch allows a read stream's strategy to be changed for future I/O.
> VACUUM switches the active stream to a NULL strategy when failsafe
> activates, without modifying in-flight I/O operations.

Right, because it can change during an ongoing scan, there has to be
some way to set it for a new read after the strategy was deactivated.
And we have to make sure we retain information on the IOContext that
read operations were started in for accounting purposes.

For master, I actually think what we should do is save the IOContext
in the ReadBuffersOperation instead of the BufferAccessStrategy. I
think it is cleaner since you only need the IOContext when completing
the IO and you can pass the BufferAccessStrategy directly to
StartReadBuffers() without saving it in the ReadBuffersOperation.

And, when it comes to this patch and being able to "deactivate" the
buffer access strategy, we need a way to do so for future IOs without
affecting in-progress IOs, and this structure seems like the best way
to do that.

I've included two commits targeted at master. The first replaces
BufferAccessStrategy with IOContext in the ReadBuffersOperation and
the second resets the BufferAccessStrategy in the read stream to fix
the reported bug with failsafe mode. (attached and prefixed "master").

I'll note I did add an include of pgstat.h in bufmgr.h which is pretty
undesirable. I'm not sure if I should make a header for the
IOContext/IOObject stuff to avoid this.

For backbranches, we should just do what you did in your patch. I
included an updated version of it with a few tweaks. Luckily
ReadStream is opaque, so these changes are still ABI-compatible
(attached and prefixed "backbranches"). My proposed changes to master
would make ABI-breaking changes to ReadBuffersOperation, so we
shouldn't do that in backbranches.

- Melanie

Re: [PATCH] vacuum: stop using stream ring after failsafe

От:
Melanie Plageman <melanieplageman@gmail.com>
Дата:
Вложения:
On Thu, Aug 6, 2026 at 4:42 PM Melanie Plageman
 wrote:
>
> Instead, I propose we just take a small hit to the accounting and
> simply clear the ReadBuffersOperations->strategy even of in-progress
> IOs. It should only misattribute a bit of wait time -- and only for a
> limited number of IOs once -- when we enter failsafe mode. It makes
> for a simple, backpatchable solution. I've attached here what I intend
> to commit later today.

Okay, I pushed it, but right after pushing it I suddenly had the
thought, omg, it loops through all the ReadBuffersOperations and sets
them to NULL for _every_ page after entering failsafe mode. And we
obviously ought to fix that. Proposed patch to do that with a simple
local variable is attached. This time I'll let this one sit for longer
before pushing to avoid mistakes like last time.

- Melanie

Re: [PATCH] vacuum: stop using stream ring after failsafe

От:
Melanie Plageman <melanieplageman@gmail.com>
Дата:
On Thu, Aug 6, 2026 at 6:04 PM Tom Lane  wrote:
>
> Melanie Plageman  writes:
> > Okay, I pushed it, but right after pushing it I suddenly had the
> > thought, omg, it loops through all the ReadBuffersOperations and sets
> > them to NULL for _every_ page after entering failsafe mode. And we
> > obviously ought to fix that. Proposed patch to do that with a simple
> > local variable is attached. This time I'll let this one sit for longer
> > before pushing to avoid mistakes like last time.
>
> Don't wait too long, unless you want to revert 112c26838 et al
> till after the upcoming releases.  As things stand right now,
> we'd ship what is in git.

roger that. I'm planning to push in the morning which will be before
Saturday's freeze.

- Melanie


FAQ