Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions pgxn/neon/neon_lwlsncache.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ SetLastWrittenLSNForBlockRangeInternal(XLogRecPtr lsn,
for (i = 0; i < n_blocks; i++)
{
key.blockNum = from + i;
entry = hash_search(lastWrittenLsnCache, &key, HASH_ENTER, &found);
if (found)
entry = hash_search(lastWrittenLsnCache, &key, HASH_FIND, NULL);
if (entry != NULL)
{
if (lsn > entry->lsn)
entry->lsn = lsn;
Expand All @@ -344,8 +344,7 @@ SetLastWrittenLSNForBlockRangeInternal(XLogRecPtr lsn,
}
else
{
entry->lsn = lsn;
if (hash_get_num_entries(lastWrittenLsnCache) > LwLsnCache->lastWrittenLsnCacheSize)
if (hash_get_num_entries(lastWrittenLsnCache) >= LwLsnCache->lastWrittenLsnCacheSize)
{
/* Replace least recently used entry */
LastWrittenLsnCacheEntry* victim = dlist_container(LastWrittenLsnCacheEntry, lru_node, dlist_pop_head_node(&LwLsnCache->lastWrittenLsnLRU));
Expand All @@ -355,6 +354,10 @@ SetLastWrittenLSNForBlockRangeInternal(XLogRecPtr lsn,

hash_search(lastWrittenLsnCache, victim, HASH_REMOVE, NULL);
}

entry = hash_search(lastWrittenLsnCache, &key, HASH_ENTER, &found);
Assert(!found);
entry->lsn = lsn;
}
/* Link to the end of LRU list */
dlist_push_tail(&LwLsnCache->lastWrittenLsnLRU, &entry->lru_node);
Expand Down Expand Up @@ -429,8 +432,8 @@ neon_set_lwlsn_block_v(const XLogRecPtr *lsns, NRelFileInfo relfilenode,

Assert(lsn >= WalSegMinSize);
key.blockNum = blockno + i;
entry = hash_search(lastWrittenLsnCache, &key, HASH_ENTER, &found);
if (found)
entry = hash_search(lastWrittenLsnCache, &key, HASH_FIND, NULL);
if (entry != NULL)
{
if (lsn > entry->lsn)
entry->lsn = lsn;
Expand All @@ -441,8 +444,7 @@ neon_set_lwlsn_block_v(const XLogRecPtr *lsns, NRelFileInfo relfilenode,
}
else
{
entry->lsn = lsn;
if (hash_get_num_entries(lastWrittenLsnCache) > LwLsnCache->lastWrittenLsnCacheSize)
if (hash_get_num_entries(lastWrittenLsnCache) >= LwLsnCache->lastWrittenLsnCacheSize)
{
/* Replace least recently used entry */
LastWrittenLsnCacheEntry* victim = dlist_container(LastWrittenLsnCacheEntry, lru_node, dlist_pop_head_node(&LwLsnCache->lastWrittenLsnLRU));
Expand All @@ -452,6 +454,9 @@ neon_set_lwlsn_block_v(const XLogRecPtr *lsns, NRelFileInfo relfilenode,

hash_search(lastWrittenLsnCache, victim, HASH_REMOVE, NULL);
}
entry = hash_search(lastWrittenLsnCache, &key, HASH_ENTER, &found);
Assert(!found);
entry->lsn = lsn;
}
/* Link to the end of LRU list */
dlist_push_tail(&LwLsnCache->lastWrittenLsnLRU, &entry->lru_node);
Expand Down