Allow aggressive VACUUM to freeze without a cleanup lock
Hi,
I encountered a case where an aggressive VACUUM remained waiting for a
buffer cleanup lock on a page containing a frequently locked row. There
were many concurrent SELECT FOR UPDATE transactions on the same row. The
transactions were individually short-lived, but their buffer pins
overlapped continuously, so VACUUM did not get an opportunity to acquire
the cleanup lock. The system was consuming XIDs quickly, so leaving an
anti-wraparound VACUUM blocked indefinitely would allow the age of unfrozen
XIDs to advance rapidly toward the wraparound danger threshold.
I also noticed a previous report of a similar problem, in which a VACUUM
FREEZE waited on BufferPin for several days while buffer pins from
successive readers overlapped [1]/messages/by-id/1059371874.2807306.1706727919318@mail.yahoo.com. A similar cleanup-lock starvation problem
was discussed in 2011 [2]/messages/by-id/BANLkTinmWFR1-mPu4nduUjxUfvWXZni-7Q@mail.gmail.com.
This seems particularly undesirable for an anti-wraparound VACUUM.
Skipping pruning can leave dead tuples and unused space behind, but
failing to freeze old XIDs can eventually prevent the database from
accepting writes.
The 2011 discussion [2]/messages/by-id/BANLkTinmWFR1-mPu4nduUjxUfvWXZni-7Q@mail.gmail.com raised the related question of whether VACUUM
could make progress after failing to acquire a cleanup lock. It found that
changing line pointers while holding only an exclusive buffer content lock
would be unsafe, because another backend can inspect a line pointer while
holding only a buffer pin. However, freezing tuple headers is different: an
exclusive buffer content lock is sufficient for that.
The idea here is deliberately narrow. It applies only to aggressive VACUUM.
After failing to acquire a cleanup lock, and before waiting for one, VACUUM
makes one freeze-only attempt under an exclusive buffer content lock (which
is less prone to starvation than a cleanup lock). It only makes a
best-effort attempt to freeze tuple headers. Dead tuples are left untouched,
but their XIDs and MultiXactIds are considered when determining whether the
relation freeze horizon can advance. If no dead tuple prevents that progress,
VACUUM can freeze the tuples that can be safely frozen and continue.
Otherwise, it falls back to waiting for a cleanup lock and running the
existing combined prune-and-freeze path.
Patch 1 refactors the per-page VACUUM path, where pruning and freezing are
currently coupled, to separate freeze planning and execution from pruning.
It does not change behavior: the pruning path still uses the same combined
prune-and-freeze WAL record. This prepares for freezing a page independently
of pruning.
Patch 2 adds the freeze-only path after cleanup-lock acquisition fails. The
existing path is unchanged when a cleanup lock is available. The new path is
used only when cleanup-lock contention would otherwise make an aggressive
VACUUM wait.
I have included a small reproducer using concurrent SELECT FOR UPDATE
transactions on one row. Without the patch, VACUUM FREEZE remains waiting
on BufferPin while the workload continues. With the patch, it freezes the
live tuple and completes while the workload is still running.
I would appreciate feedback on this approach.
---
Regards,
Jingtang
Alibaba Cloud
[1]: /messages/by-id/1059371874.2807306.1706727919318@mail.yahoo.com
[2]: /messages/by-id/BANLkTinmWFR1-mPu4nduUjxUfvWXZni-7Q@mail.gmail.com
Attachments:
t253544_1v1-0001-vacuum-Separate-heap-page-freezing-state.patchapplication/octet-stream; name=v1-0001-vacuum-Separate-heap-page-freezing-state.patchDownload+116-68
v1-0002-vacuum-Allow-freezing-without-a-cleanup-lock.patchapplication/octet-stream; name=v1-0002-vacuum-Allow-freezing-without-a-cleanup-lock.patchDownload+369-5
On Tue, Aug 25, 2026 at 6:32 AM Jingtang Zhang <mrdrivingduck@gmail.com> wrote:
The 2011 discussion [2] raised the related question of whether VACUUM
could make progress after failing to acquire a cleanup lock. It found that
changing line pointers while holding only an exclusive buffer content lock
would be unsafe, because another backend can inspect a line pointer while
holding only a buffer pin. However, freezing tuple headers is different: an
exclusive buffer content lock is sufficient for that.
I think this proposal (in the email) should explain why freezing xids
and mxids is safe with only an exclusive lock. AFAICT that 2011 thread
doesn't get specific. And today, lazy_scan_noprune() specifically
returns false and requires a cleanup lock during an aggressive vacuum
when freezing would be required. If this is not necessary, you should
explain why. Was it never required and we were being over-cautious?
Did something change in the code to make it safe and we forgot to
update lazy_scan_noprune()?
- Melanie
On Tue, Aug 25, 2026 at 10:06 AM Melanie Plageman
<melanieplageman@gmail.com> wrote:
I think this proposal (in the email) should explain why freezing xids
and mxids is safe with only an exclusive lock. AFAICT that 2011 thread
doesn't get specific.
As far as I know, the only fundamental reason we require a cleanup
lock is to make page defragmentation safe. Pruning as currently
defined implies page defragmentation, and freezing is closely tied to
pruning, so we tend to treat both as requiring a cleanup lock. But a
cleanup lock is likely not strictly necessary.
Freezing expects pruning to remove dead tuples, because we have no way
to freeze them. But it doesn't actually require page defragmentation.
So it is probably possible to invent a variant of pruning that doesn't
remove tuple storage, and thus has no need to defragment the page. I'm
thinking of an approach that performs an in-place update of xmin and
xmax, setting both to InvalidTransactionId. That would enable
repurposing lazy_scan_noprune into (say) lazy_scan_nodefrag, which
would always be able to freeze (and psuedo-prune) every page without
waiting for a cleanup lock.
Obviously I haven't prototyped this, so there might be some hard to
foresee difficulties. It's possible that pruning relies on cleanup
locks in a way nobody realizes right now -- it wouldn't be the first
time something like that happened. When I made the second pass over
the heap not require a cleanup lock for Postgres 14, it exposed race
conditions with how we set the visibility map in that pass.
Technically that wasn't my fault; the fixes for those bugs were in and
around heap_update IIRC.
--
Peter Geoghegan
On Tue, Aug 25, 2026 at 10:28 AM Peter Geoghegan <pg@bowt.ie> wrote:
As far as I know, the only fundamental reason we require a cleanup
lock is to make page defragmentation safe. Pruning as currently
defined implies page defragmentation, and freezing is closely tied to
pruning, so we tend to treat both as requiring a cleanup lock. But a
cleanup lock is likely not strictly necessary.
Fun fact: prior to the invention of HOT in 2007, we'd freeze with only
a *shared* lock in the first heap pass. The first heap pass collected
dead TIDs and performed freezing, but it didn't do any pruning.
Freezing wasn't tied to pruning (or anything like it) in the way it is
today.
Pruning didn't really exist (not as we know it today), but page
defragmentation did. At that time, we only removed dead tuples/tuple
storage and performed page defragmentation in VACUUM's second heap
pass (which required a full cleanup lock).
So it's reasonable to surmise that freezing could be done with only an
exclusive lock on top of the current VACUUM code (though not a shared
lock, which seems really dangerous). And that page defragmentation is
the only thing that necessitates a cleanup lock/makes this idea
difficult.
--
Peter Geoghegan
Hi Melanie, Peter, Jingtang,
So it's reasonable to surmise that freezing could be done with only an
exclusive lock on top of the current VACUUM code (though not a shared
lock, which seems really dangerous). And that page defragmentation is
the only thing that necessitates a cleanup lock/makes this idea difficult.
Agreed. Freezing itself only needs an exclusive lock, a cleanup lock is needed
for page defragmentation. I also agree with Melanie that the patch should
explain this if it is not already documented elsewhere.
I reviewed the patch and found a few issues.
First, the buffer lock is released and reacquired between
`lazy_scan_noprune()` and `lazy_scan_freeze()`. The page may be changed or
already frozen during this window, so this assertion is not always valid:
```C
Assert(state.pagefrz.freeze_required);
```
Second, the code may call heap_prepare_freeze_tuple() and then discard the
freeze plans after finding an old DEAD tuple. This function may create a new
MultiXactId, write WAL, and advance the MultiXact state. These side effects
remain even if the plans are discarded.
There is also a small naming issue. lazy_scan_noprune and lazy_scan_freeze are
quite confusing now. I think lazy_scan_noprune is not correct at this moment.
The patch adds FreezeState, HeapPageFreezeParams, and
HeapPageFreezeResult, but they overlap with PruneState,
PruneFreezeParams, and PruneFreezeResult. These abstractions add complexity
without much reuse.
Pages containing old XIDs or MultiXactIds in DEAD tuples still need a cleanup
lock. I think this is a reasonable limitation for this patch.
Best Regards,
Yuhang Qiu
Hi all,
Thanks for the reply.
Agreed. Freezing itself only needs an exclusive lock, a cleanup lock is needed
for page defragmentation. I also agree with Melanie that the patch should
explain this if it is not already documented elsewhere.
Yeah. Did some research from existing information.
A cleanup lock is for reorganizing a page. Pruning can remove or
change line pointers, and defragmentation can move tuple data. An
exclusive content lock does not wait for other buffer pins, so another
backend might still hold a reference to either. That is why pruning
needs a cleanup lock [1]https://github.com/postgres/postgres/blob/master/src/backend/access/heap/README.HOT#L228-L231. The WAL replay code makes the same split:
pruning needs a cleanup lock, while freezing and VACUUM's second heap
pass only need an ordinary exclusive lock [2]https://github.com/postgres/postgres/blob/master/src/include/access/heapam_xlog.h#L330-L335.
[1]: https://github.com/postgres/postgres/blob/master/src/backend/access/heap/README.HOT#L228-L231
[2]: https://github.com/postgres/postgres/blob/master/src/include/access/heapam_xlog.h#L330-L335
First, the buffer lock is released and reacquired between
`lazy_scan_noprune()` and `lazy_scan_freeze()`. The page may be changed or
already frozen during this window, so this assertion is not always valid:Second, the code may call heap_prepare_freeze_tuple() and then discard the
freeze plans after finding an old DEAD tuple. This function may create a new
MultiXactId, write WAL, and advance the MultiXact state. These side effects
remain even if the plans are discarded.
Thanks for the review. I fixed both issues you found. The freeze-only
path no longer assumes anything about the page after it reacquires the
buffer lock. It also scans the whole page before it prepares freeze
plans, so a fallback to the cleanup-lock path cannot leave MultiXact or
WAL side effects behind.
There is also a small naming issue. lazy_scan_noprune and lazy_scan_freeze are
quite confusing now. I think lazy_scan_noprune is not correct at this moment.
The current names describe three different paths: lazy_scan_prune()
does pruning and freezing, lazy_scan_freeze() does freezing only, and
lazy_scan_noprune() does neither. The last name predates this patch,
but I agree that it is less clear now.
The patch adds FreezeState, HeapPageFreezeParams, and
HeapPageFreezeResult, but they overlap with PruneState,
PruneFreezeParams, and PruneFreezeResult. These abstractions add complexity
without much reuse.
I also agree that the parameter and result types should share a common
freeze component. PruneFreezeParams and PruneFreezeResult have extra
pruning and VM state, but their freeze-related fields overlap with the
freeze-only types. The pruning-specific state should sit around the
common part instead of having two parallel sets of structures.
v2 patches are attached.
---
Regards,
Jingtang
Attachments:
t253544_6v2-0002-vacuum-Allow-freezing-without-a-cleanup-lock.patchapplication/octet-stream; name=v2-0002-vacuum-Allow-freezing-without-a-cleanup-lock.patchDownload+395-5
v2-0001-vacuum-Separate-heap-page-freezing-state.patchapplication/octet-stream; name=v2-0001-vacuum-Separate-heap-page-freezing-state.patchDownload+116-68
On Wed, Aug 26, 2026 at 1:27 PM Jingtang Zhang <mrdrivingduck@gmail.com> wrote:
There is also a small naming issue. lazy_scan_noprune and lazy_scan_freeze are
quite confusing now. I think lazy_scan_noprune is not correct at this moment.The current names describe three different paths: lazy_scan_prune()
does pruning and freezing, lazy_scan_freeze() does freezing only, and
lazy_scan_noprune() does neither. The last name predates this patch,
but I agree that it is less clear now.
I don't have time to pick up moving this project forward right now,
but after a quick glance at the patches, I wouldn't structure the code
the way you have it. I think what you'll want is some kind of refactor
of lazy_scan_new_or_empty() (which already escalates to an exclusive
lock for empty pages) and of lazy_scan_noprune(). This is already a
pretty complicated set of states and adding a new one where we hold a
different lock type and want to do a different subset of tasks and
based on various things may want to then do a different set of tasks
(i.e. go try to get the cleanup lock again if there are old enough
dead tuples but only if it was an aggressive vacuum which was checked
elsewhere, etc) without more refactoring of the existing code seems to
confusing to me.
Do you know if in your use case this would have been able to do the
freeze (i.e. you didn't have too-old dead tuples on the page) with
needing pruning?
- Melanie
Hi, Melanie.
Thanks for looking.
Do you know if in your use case this would have been able to do the
freeze (i.e. you didn't have too-old dead tuples on the page) with
needing pruning?
In this case, I believe the page would have been able to make freeze
progress without pruning. Whenever an anti-wraparound VACUUM became
stuck, we had to terminate every SELECT FOR UPDATE session accessing that
page before it could acquire the cleanup lock and proceed past the page
using the normal prune-and-freeze path. That pass would have removed any
removable DEAD tuples on the page. Across many subsequent
anti-wraparound VACUUM runs, the same page and the same row repeatedly
became the blocking point again. The recurring workload continuously
ran SELECT FOR UPDATE on that row, and later anti-wraparound VACUUM runs
again waited on BufferPin at the same page and row.
SELECT FOR UPDATE does not create a new tuple version. It only locks
the existing tuple, so repeated locking of the same logical row continues
to access the same physical tuple and the same heap page. There is no
mechanism that moves this hotspot to another page.
UPDATE behaves differently. Without pruning, old tuple versions cannot
have their space recycled on the current page. As the page fills, a
later update can no longer be HOT and must place the new current version
on another page. The original page is then no longer the hotspot for
that row, so the sustained pins can move away and it can get a chance to
be pruned. SELECT FOR UPDATE has no comparable escape path.
---
Regards,
Jingtang