Skip to content

Fix lastWrittenLsnCache eviction order#12906

Open
mingjiegao wants to merge 1 commit into
neondatabase:mainfrom
mingjiegao:fix-last-written-lsn-cache-eviction-order
Open

Fix lastWrittenLsnCache eviction order#12906
mingjiegao wants to merge 1 commit into
neondatabase:mainfrom
mingjiegao:fix-last-written-lsn-cache-eviction-order

Conversation

@mingjiegao

Copy link
Copy Markdown

Problem

lastWrittenLsnCache is expected to stay within neon.last_written_lsn_cache_size. However, when the cache is full, inserting before eviction can require one extra shared hash entry.

With a fixed-size shared hash table, this can fail with out-of-shared-memory.

Summary

Fixes #12905.

This PR changes lastWrittenLsnCache so that when a new entry needs to be inserted and the cache is already full, an existing entry is evicted before inserting the new one.

Previously, the code inserted the new entry first and evicted afterwards. This could temporarily require neon.last_written_lsn_cache_size + 1 hash entries.

Fix

For new keys, the code now ensures capacity before calling HASH_ENTER:

  1. Check whether the entry already exists.
  2. If it does not exist and the cache is full, evict an existing entry first.
  3. Insert the new entry only after capacity has been made available.

Testing

  • Manually reproduced the issue by setting neon.last_written_lsn_cache_size = 10 and adding HASH_FIXED_SIZE to the ShmemInitHash call for lastWrittenLsnCache.
  • Verified that after the fix, the same workload no longer fails with out-of-shared-memory.

I did not add an automated test because the failure depends on shared hash table sizing and concurrent workload behavior. I would appreciate guidance on the best test location for this case.

@mingjiegao mingjiegao requested review from a team as code owners June 4, 2026 05:31
@mingjiegao mingjiegao force-pushed the fix-last-written-lsn-cache-eviction-order branch from a1d9985 to 5032cc7 Compare June 4, 2026 05: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.

lastWrittenLsnCache may temporarily exceed configured size before eviction

1 participant