Skip to content

Commit 5032cc7

Browse files
committed
Fix lastWrittenLsnCache eviction order
1 parent 8f60b04 commit 5032cc7

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

pgxn/neon/neon_lwlsncache.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ SetLastWrittenLSNForBlockRangeInternal(XLogRecPtr lsn,
332332
for (i = 0; i < n_blocks; i++)
333333
{
334334
key.blockNum = from + i;
335-
entry = hash_search(lastWrittenLsnCache, &key, HASH_ENTER, &found);
336-
if (found)
335+
entry = hash_search(lastWrittenLsnCache, &key, HASH_FIND, NULL);
336+
if (entry != NULL)
337337
{
338338
if (lsn > entry->lsn)
339339
entry->lsn = lsn;
@@ -344,8 +344,7 @@ SetLastWrittenLSNForBlockRangeInternal(XLogRecPtr lsn,
344344
}
345345
else
346346
{
347-
entry->lsn = lsn;
348-
if (hash_get_num_entries(lastWrittenLsnCache) > LwLsnCache->lastWrittenLsnCacheSize)
347+
if (hash_get_num_entries(lastWrittenLsnCache) >= LwLsnCache->lastWrittenLsnCacheSize)
349348
{
350349
/* Replace least recently used entry */
351350
LastWrittenLsnCacheEntry* victim = dlist_container(LastWrittenLsnCacheEntry, lru_node, dlist_pop_head_node(&LwLsnCache->lastWrittenLsnLRU));
@@ -355,6 +354,10 @@ SetLastWrittenLSNForBlockRangeInternal(XLogRecPtr lsn,
355354

356355
hash_search(lastWrittenLsnCache, victim, HASH_REMOVE, NULL);
357356
}
357+
358+
entry = hash_search(lastWrittenLsnCache, &key, HASH_ENTER, &found);
359+
Assert(!found);
360+
entry->lsn = lsn;
358361
}
359362
/* Link to the end of LRU list */
360363
dlist_push_tail(&LwLsnCache->lastWrittenLsnLRU, &entry->lru_node);
@@ -429,8 +432,8 @@ neon_set_lwlsn_block_v(const XLogRecPtr *lsns, NRelFileInfo relfilenode,
429432

430433
Assert(lsn >= WalSegMinSize);
431434
key.blockNum = blockno + i;
432-
entry = hash_search(lastWrittenLsnCache, &key, HASH_ENTER, &found);
433-
if (found)
435+
entry = hash_search(lastWrittenLsnCache, &key, HASH_FIND, NULL);
436+
if (entry != NULL)
434437
{
435438
if (lsn > entry->lsn)
436439
entry->lsn = lsn;
@@ -441,8 +444,7 @@ neon_set_lwlsn_block_v(const XLogRecPtr *lsns, NRelFileInfo relfilenode,
441444
}
442445
else
443446
{
444-
entry->lsn = lsn;
445-
if (hash_get_num_entries(lastWrittenLsnCache) > LwLsnCache->lastWrittenLsnCacheSize)
447+
if (hash_get_num_entries(lastWrittenLsnCache) >= LwLsnCache->lastWrittenLsnCacheSize)
446448
{
447449
/* Replace least recently used entry */
448450
LastWrittenLsnCacheEntry* victim = dlist_container(LastWrittenLsnCacheEntry, lru_node, dlist_pop_head_node(&LwLsnCache->lastWrittenLsnLRU));
@@ -452,6 +454,9 @@ neon_set_lwlsn_block_v(const XLogRecPtr *lsns, NRelFileInfo relfilenode,
452454

453455
hash_search(lastWrittenLsnCache, victim, HASH_REMOVE, NULL);
454456
}
457+
entry = hash_search(lastWrittenLsnCache, &key, HASH_ENTER, &found);
458+
Assert(!found);
459+
entry->lsn = lsn;
455460
}
456461
/* Link to the end of LRU list */
457462
dlist_push_tail(&LwLsnCache->lastWrittenLsnLRU, &entry->lru_node);

0 commit comments

Comments
 (0)