Skip to content

fix(rerank): take the buffer share lock around heap item reads - #381

Open
pavanmanishd wants to merge 1 commit into
timescale:mainfrom
pavanmanishd:fix/rerank-page-lock
Open

pavanmanishd wants to merge 1 commit into
timescale:mainfrom
pavanmanishd:fix/rerank-page-lock

Conversation

@pavanmanishd

@pavanmanishd pavanmanishd commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #117.

The read-stream rerank read heap tuples from a pinned buffer without a content lock, checking only ItemIdIsNormal. A pin keeps a page from being evicted, not from being changed: VACUUM can truncate the line-pointer array and PageAddItem can reuse a slot, so a TID can outlive its line pointer.

The fix holds BUFFER_LOCK_SHARE for the line-pointer read and the tuple header it points at, checks the offset the way heap_fetch does, then releases the lock before the slot store, the detoast and the distance. The buffer stays pinned for the whole loop, so the tuple body is still valid unlocked; visibility is unchanged by design, matching the non-stream path. The lock costs roughly 7% of the query; a 24-client load cannot resolve a cost that small, so the single-query A/B is the honest number.

@pavanmanishd
pavanmanishd force-pushed the fix/rerank-page-lock branch 6 times, most recently from 079eb7e to 5e44e90 Compare October 20, 2026 08:45
The read-stream rerank read heap tuples from a pinned buffer without a
content lock, checking only ItemIdIsNormal. A pin keeps the page from being
evicted, not from being changed: VACUUM can truncate the line-pointer array
and PageAddItem can reuse a slot, so a TID can outlive its line pointer.

Hold BUFFER_LOCK_SHARE for the line-pointer read and the tuple header it
points at, check the offset the way heap_fetch does, then release the lock
before the slot store, detoast and distance. The pin keeps the tuple body
valid, and visibility is unchanged by design to match the non-stream path.

Cost: an interleaved A/B over the same tree with only the lock calls removed
puts the rerank roughly 7% higher at LIMIT 5000 with the full pool.
@pavanmanishd
pavanmanishd marked this pull request as ready for review October 20, 2026 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pg_rerank_readstream reads heap pages without content lock or visibility checks

1 participant