Re: Buffer locking is special (hints, checksums, AIO writes)
Heikki Linnakangas <hlinnaka@iki.fi>
Commits
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Require share-exclusive lock to set hint bits and to flush
- 82467f627bd4 19 (unreleased) landed
-
lwlock: Remove ForEachLWLockHeldByMe
- 55fbfb738b00 19 (unreleased) landed
-
bufmgr: Implement buffer content locks independently of lwlocks
- fcb9c977aa5f 19 (unreleased) landed
-
bufmgr: Change BufferDesc.state to be a 64-bit atomic
- dac328c8a682 19 (unreleased) landed
-
heapam: Add batch mode mvcc check and use it in page mode
- 0b96e734c590 19 (unreleased) landed
-
freespace: Don't modify page without any lock
- 45f658dacb9c 19 (unreleased) landed
-
heapam: Move logic to handle HEAP_MOVED into a helper function
- 548de59d93d5 19 (unreleased) landed
-
bufmgr: Optimize & harmonize LockBufHdr(), LWLockWaitListLock()
- 09ae2c8bac8d 19 (unreleased) landed
-
bufmgr: Add one-entry cache for private refcount
- 30df61990c67 19 (unreleased) landed
-
bufmgr: Separate keys for private refcount infrastructure
- edbaaea0a95e 19 (unreleased) landed
-
Add pg_atomic_unlocked_write_u64
- 7902a47c20b1 19 (unreleased) landed
-
Rename BUFFERPIN wait event class to BUFFER
- 6c5c393b7403 19 (unreleased) landed
-
bufmgr: Turn BUFFER_LOCK_* into an enum
- 156680055dc5 19 (unreleased) landed
-
lwlock: Fix, currently harmless, bug in LWLockWakeup()
- 81f773895321 19 (unreleased) landed
- da3971496531 15.16 landed
- 89c8a1b9069f 16.12 landed
- 427e886a79a5 17.8 landed
- 332693e75969 14.21 landed
- 8082b759d9b5 18.2 landed
-
bufmgr: Use atomic sub for unpinning buffers
- 5310fac6e0fc 19 (unreleased) landed
-
bufmgr: Allow some buffer state modifications while holding header lock
- c75ebc657ffc 19 (unreleased) landed
-
bufmgr: Fix valgrind checking for buffers pinned in StrategyGetBuffer()
- c819d1017ddb 19 (unreleased) landed
-
bufmgr: Don't lock buffer header in StrategyGetBuffer()
- 5e8998592879 19 (unreleased) landed
-
bufmgr: fewer calls to BufferDescriptorGetContentLock
- 3baae90013df 19 (unreleased) landed
-
bufmgr: Fix signedness of mask variable in BufferSync()
- 2a2e1b470b9b 19 (unreleased) landed
-
bufmgr: Introduce FlushUnlockedBuffer
- 3c2b97b29ee3 19 (unreleased) landed
-
Improve ReadRecentBuffer() scalability
- 819dc118c0f6 19 (unreleased) landed
On 25/11/2025 22:46, Andres Freund wrote: > On 2025-11-25 15:02:02 -0500, Melanie Plageman wrote: >> On Tue, Nov 25, 2025 at 11:54 AM Andres Freund <andres@anarazel.de> wrote: >>>> --- a/src/backend/storage/freespace/freespace.c >>>> +++ b/src/backend/storage/freespace/freespace.c >>>> @@ -904,14 +904,22 @@ fsm_vacuum_page(Relation rel, FSMAddress addr, >>>> max_avail = fsm_get_max_avail(page); >>>> /* >>>> - * Reset the next slot pointer. This encourages the use of low-numbered >>>> - * pages, increasing the chances that a later vacuum can truncate the >>>> - * relation. We don't bother with a lock here, nor with marking the page >>>> - * dirty if it wasn't already, since this is just a hint. >>>> + * Try to reset the next slot pointer. This encourages the use of >>>> + * low-numbered pages, increasing the chances that a later vacuum can >>>> + * truncate the relation. We don't bother with a lock here, nor with >>>> + * marking the page dirty if it wasn't already, since this is just a hint. >>>> + * >>>> + * To be allowed to update the page without an exclusive lock, we have to >>>> + * use the hint bit infrastructure. >>>> */ >>>> >>>> What the heck? This didn't even take a share lock before... >>> >>> It is insane. I do not understand how anybody thought this was ok. I think I >>> probably should split this out into a separate commit, since it's so insane. >> >> Aren't you going to backport having it take a lock? Then it can be >> separate (e.g. have it take a share lock in the "fix" commit and then >> this commit bumps it to share-exclusive). > > I wasn't thinking we should backport that. It's been this way for ages, > without having caused known issues.... I wrote that originally :-). The FSM code always treats the FSM page contents as potentially corrupted garbage. FSM page updates are not WAL-logged, for starters. And as the comment says, the next slot pointer is just a hint for where within the page to start looking for free space. Page checksums were added later, and now we know about the problems of modifying a page a write() is in progress, on some filesystems with filesystem-level checksums. So it's a good idea to tighten it up, but it was fine back then. >>>> diff --git a/src/backend/storage/freespace/fsmpage.c >>>> b/src/backend/storage/freesp> >>>> /* >>>> * Update the next-target pointer. Note that we do this even if we're only >>>> * holding a shared lock, on the grounds that it's better to use a shared >>>> * lock and get a garbled next pointer every now and then, than take the >>>> * concurrency hit of an exclusive lock. >>>> >>>> We appear to avoid the garbling now? >>> >>> I don't think so. Two backends concurrently can do fsm_search_avail() and >>> one backend might set a hint to a page that is already used up by the other >>> one. At least I think so? >> >> Maybe I don't know what it meant by garbled, but I thought it was >> talking about two backends each trying to set fp_next_slot. If they >> now have to have a share-exclusive lock and they can't both have a >> share-exclusive lock at the same time, then it seems like that >> wouldn't be a problem. It sounds like you may be talking about a >> backend taking up the freespace of a page that is referred to by the >> fp_next_slot? > > Yes, a version of the latter. The value that fp_next_slot will be set to can > be outdated by the time we actually set it, unless we do all of > fsm_search_avail() under some form of exclusive lock - clearly not something > desirable. I'm pretty sure the "garbled" in the comment means the former, not the latter. I.e. it means that the pointer itself might become garbage. Would be good to update the comment if that's no longer possible. But speaking of that: why do we not allow two processes to concurrently set hint bits on a page anymore? - Heikki