Skip to content

Commit

Permalink
Remember last written LSN when it is first requested (#411)
Browse files Browse the repository at this point in the history
* Remember last written LSN when it is first requested

* Use rnode instead of rlocator

* Return updated LSN in SetLastWrittenLSN

* Remove wrong new line

---------

Co-authored-by: Konstantin Knizhnik <[email protected]>
  • Loading branch information
2 people authored and tristan957 committed May 10, 2024
1 parent 43a818e commit fa8f7d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
26 changes: 18 additions & 8 deletions src/backend/access/transam/xlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -6133,6 +6133,11 @@ GetLastWrittenLSN(RelFileNode rnode, ForkNumber forknum, BlockNumber blkno)
entry = hash_search(lastWrittenLsnCache, &key, HASH_FIND, NULL);
if (entry != NULL)
lsn = entry->lsn;
else
{
LWLockRelease(LastWrittenLsnLock);
return SetLastWrittenLSNForBlock(lsn, rnode, forknum, blkno);
}
}
else
{
Expand Down Expand Up @@ -6160,17 +6165,19 @@ GetLastWrittenLSN(RelFileNode rnode, ForkNumber forknum, BlockNumber blkno)
* rnode.relNode can be InvalidOid, in this case maxLastWrittenLsn is updated.
* SetLastWrittenLsn with dummy rnode is used by createdb and dbase_redo functions.
*/
void
XLogRecPtr
SetLastWrittenLSNForBlockRange(XLogRecPtr lsn, RelFileNode rnode, ForkNumber forknum, BlockNumber from, BlockNumber n_blocks)
{
if (lsn == InvalidXLogRecPtr || n_blocks == 0 || lastWrittenLsnCacheSize == 0)
return;
return lsn;

LWLockAcquire(LastWrittenLsnLock, LW_EXCLUSIVE);
if (rnode.relNode == InvalidOid)
{
if (lsn > XLogCtl->maxLastWrittenLsn)
XLogCtl->maxLastWrittenLsn = lsn;
else
lsn = XLogCtl->maxLastWrittenLsn;
}
else
{
Expand All @@ -6189,6 +6196,8 @@ SetLastWrittenLSNForBlockRange(XLogRecPtr lsn, RelFileNode rnode, ForkNumber for
{
if (lsn > entry->lsn)
entry->lsn = lsn;
else
lsn = entry->lsn;
/* Unlink from LRU list */
dlist_delete(&entry->lru_node);
}
Expand All @@ -6211,34 +6220,35 @@ SetLastWrittenLSNForBlockRange(XLogRecPtr lsn, RelFileNode rnode, ForkNumber for
}
}
LWLockRelease(LastWrittenLsnLock);
return lsn;
}

/*
* SetLastWrittenLSNForBlock -- Set maximal LSN for block
*/
void
XLogRecPtr
SetLastWrittenLSNForBlock(XLogRecPtr lsn, RelFileNode rnode, ForkNumber forknum, BlockNumber blkno)
{
SetLastWrittenLSNForBlockRange(lsn, rnode, forknum, blkno, 1);
return SetLastWrittenLSNForBlockRange(lsn, rnode, forknum, blkno, 1);
}

/*
* SetLastWrittenLSNForRelation -- Set maximal LSN for relation metadata
*/
void
XLogRecPtr
SetLastWrittenLSNForRelation(XLogRecPtr lsn, RelFileNode rnode, ForkNumber forknum)
{
SetLastWrittenLSNForBlock(lsn, rnode, forknum, REL_METADATA_PSEUDO_BLOCKNO);
return SetLastWrittenLSNForBlock(lsn, rnode, forknum, REL_METADATA_PSEUDO_BLOCKNO);
}

/*
* SetLastWrittenLSNForDatabase -- Set maximal LSN for the whole database
*/
void
XLogRecPtr
SetLastWrittenLSNForDatabase(XLogRecPtr lsn)
{
RelFileNode dummyNode = {InvalidOid, InvalidOid, InvalidOid};
SetLastWrittenLSNForBlock(lsn, dummyNode, MAIN_FORKNUM, 0);
return SetLastWrittenLSNForBlock(lsn, dummyNode, MAIN_FORKNUM, 0);
}

void
Expand Down
8 changes: 4 additions & 4 deletions src/include/access/xlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ extern XLogRecPtr GetLastImportantRecPtr(void);

/* neon specifics */

extern void SetLastWrittenLSNForBlock(XLogRecPtr lsn, RelFileNode relfilenode, ForkNumber forknum, BlockNumber blkno);
extern void SetLastWrittenLSNForBlockRange(XLogRecPtr lsn, RelFileNode relfilenode, ForkNumber forknum, BlockNumber from, BlockNumber n_blocks);
extern void SetLastWrittenLSNForDatabase(XLogRecPtr lsn);
extern void SetLastWrittenLSNForRelation(XLogRecPtr lsn, RelFileNode relfilenode, ForkNumber forknum);
extern XLogRecPtr SetLastWrittenLSNForBlock(XLogRecPtr lsn, RelFileNode relfilenode, ForkNumber forknum, BlockNumber blkno);
extern XLogRecPtr SetLastWrittenLSNForBlockRange(XLogRecPtr lsn, RelFileNode relfilenode, ForkNumber forknum, BlockNumber from, BlockNumber n_blocks);
extern XLogRecPtr SetLastWrittenLSNForDatabase(XLogRecPtr lsn);
extern XLogRecPtr SetLastWrittenLSNForRelation(XLogRecPtr lsn, RelFileNode relfilenode, ForkNumber forknum);
extern XLogRecPtr GetLastWrittenLSN(RelFileNode relfilenode, ForkNumber forknum, BlockNumber blkno);

extern void SetRedoStartLsn(XLogRecPtr RedoStartLSN);
Expand Down

0 comments on commit fa8f7d2

Please sign in to comment.