Skip to content

Commit

Permalink
Large last written lsn cache (#177)
Browse files Browse the repository at this point in the history
Maintain cache of last written LSN for each relation segment (8 Mb).
  • Loading branch information
knizhnik authored and tristan957 committed Nov 8, 2023
1 parent 49afd03 commit 0d8c0e8
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 36 deletions.
31 changes: 21 additions & 10 deletions contrib/neon/pagestore_smgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ static char *hexdump_page(char *page);

const int SmgrTrace = DEBUG5;

/*
* Pseudo block number used to associate LSN with relation metadata (relation size)
*/
#define REL_METADATA_PSEUDO_BLOCKNO InvalidBlockNumber

page_server_api *page_server;

/* GUCs */
Expand Down Expand Up @@ -558,7 +563,7 @@ zenith_wallog_page(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
* Remember the LSN on this page. When we read the page again, we must
* read the same or newer version of it.
*/
SetLastWrittenPageLSN(lsn);
SetLastWrittenLSN(lsn, reln->smgr_rnode.node.relNode, blocknum, blocknum);
}


Expand Down Expand Up @@ -603,7 +608,7 @@ zm_adjust_lsn(XLogRecPtr lsn)
* Return LSN for requesting pages and number of blocks from page server
*/
static XLogRecPtr
zenith_get_request_lsn(bool *latest)
zenith_get_request_lsn(bool *latest, Oid rnode, BlockNumber blkno)
{
XLogRecPtr lsn;

Expand All @@ -630,9 +635,9 @@ zenith_get_request_lsn(bool *latest)
* so our request cannot concern those.
*/
*latest = true;
lsn = GetLastWrittenPageLSN();
lsn = GetLastWrittenLSN(rnode, blkno);
Assert(lsn != InvalidXLogRecPtr);
elog(DEBUG1, "zenith_get_request_lsn GetLastWrittenPageLSN lsn %X/%X ",
elog(DEBUG1, "zenith_get_request_lsn GetLastWrittenLSN lsn %X/%X ",
(uint32) ((lsn) >> 32), (uint32) (lsn));

lsn = zm_adjust_lsn(lsn);
Expand Down Expand Up @@ -716,7 +721,7 @@ zenith_exists(SMgrRelation reln, ForkNumber forkNum)
return false;
}

request_lsn = zenith_get_request_lsn(&latest);
request_lsn = zenith_get_request_lsn(&latest, reln->smgr_rnode.node.relNode, REL_METADATA_PSEUDO_BLOCKNO);
{
ZenithExistsRequest request = {
.req.tag = T_ZenithExistsRequest,
Expand Down Expand Up @@ -791,7 +796,7 @@ zenith_create(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
*
* FIXME: This is currently not just an optimization, but required for
* correctness. Postgres can call smgrnblocks() on the newly-created
* relation. Currently, we don't call SetLastWrittenPageLSN() when a new
* relation. Currently, we don't call SetLastWrittenLSN() when a new
* relation created, so if we didn't remember the size in the relsize
* cache, we might call smgrnblocks() on the newly-created relation before
* the creation WAL record hass been received by the page server.
Expand Down Expand Up @@ -904,6 +909,8 @@ zenith_extend(SMgrRelation reln, ForkNumber forkNum, BlockNumber blkno,
if (IS_LOCAL_REL(reln))
mdextend(reln, forkNum, blkno, buffer, skipFsync);
#endif

SetLastWrittenLSN(lsn, reln->smgr_rnode.node.relNode, REL_METADATA_PSEUDO_BLOCKNO, REL_METADATA_PSEUDO_BLOCKNO);
}

/*
Expand Down Expand Up @@ -1079,7 +1086,7 @@ zenith_read(SMgrRelation reln, ForkNumber forkNum, BlockNumber blkno,
elog(ERROR, "unknown relpersistence '%c'", reln->smgr_relpersistence);
}

request_lsn = zenith_get_request_lsn(&latest);
request_lsn = zenith_get_request_lsn(&latest, reln->smgr_rnode.node.relNode, blkno);
zenith_read_at_lsn(reln->smgr_rnode.node, forkNum, blkno, request_lsn, latest, buffer);

#ifdef DEBUG_COMPARE_LOCAL
Expand Down Expand Up @@ -1284,7 +1291,7 @@ zenith_nblocks(SMgrRelation reln, ForkNumber forknum)
return n_blocks;
}

request_lsn = zenith_get_request_lsn(&latest);
request_lsn = zenith_get_request_lsn(&latest, reln->smgr_rnode.node.relNode, REL_METADATA_PSEUDO_BLOCKNO);
{
ZenithNblocksRequest request = {
.req.tag = T_ZenithNblocksRequest,
Expand Down Expand Up @@ -1344,7 +1351,7 @@ zenith_dbsize(Oid dbNode)
XLogRecPtr request_lsn;
bool latest;

request_lsn = zenith_get_request_lsn(&latest);
request_lsn = zenith_get_request_lsn(&latest, InvalidOid, REL_METADATA_PSEUDO_BLOCKNO);
{
ZenithDbSizeRequest request = {
.req.tag = T_ZenithDbSizeRequest,
Expand Down Expand Up @@ -1431,7 +1438,11 @@ zenith_truncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
*/
XLogFlush(lsn);

SetLastWrittenPageLSN(lsn);
/*
* Truncate may affect several chunks of relations. So we should either update last written LSN for all of them,
* either update LSN for "dummy" metadata block. Second approach seems to be more efficient.
*/
SetLastWrittenLSN(lsn, reln->smgr_rnode.node.relNode, REL_METADATA_PSEUDO_BLOCKNO, REL_METADATA_PSEUDO_BLOCKNO);

#ifdef DEBUG_COMPARE_LOCAL
if (IS_LOCAL_REL(reln))
Expand Down
2 changes: 1 addition & 1 deletion src/backend/access/gin/gininsert.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ ginbuild(Relation heap, Relation index, IndexInfo *indexInfo)
log_newpage_range(index, MAIN_FORKNUM,
0, RelationGetNumberOfBlocks(index),
true);
SetLastWrittenLSN(XactLastRecEnd, index->rd_smgr->smgr_rnode.node.relNode, 0, RelationGetNumberOfBlocks(index));
}
SetLastWrittenPageLSN(XactLastRecEnd);

smgr_end_unlogged_build(index->rd_smgr);

Expand Down
8 changes: 5 additions & 3 deletions src/backend/access/gist/gistbuild.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,10 @@ gistbuild(Relation heap, Relation index, IndexInfo *indexInfo)
log_newpage_range(index, MAIN_FORKNUM,
0, RelationGetNumberOfBlocks(index),
true);
SetLastWrittenLSN(XactLastRecEnd,
index->rd_smgr->smgr_rnode.node.relNode,
0, RelationGetNumberOfBlocks(index));
}
SetLastWrittenPageLSN(XactLastRecEnd);

smgr_end_unlogged_build(index->rd_smgr);
}

Expand Down Expand Up @@ -467,7 +468,8 @@ gist_indexsortbuild(GISTBuildState *state)

lsn = log_newpage(&state->indexrel->rd_node, MAIN_FORKNUM, GIST_ROOT_BLKNO,
pagestate->page, true);
SetLastWrittenPageLSN(lsn);
SetLastWrittenLSN(lsn, state->indexrel->rd_smgr->smgr_rnode.node.relNode,
GIST_ROOT_BLKNO, GIST_ROOT_BLKNO);
}

pfree(pagestate->page);
Expand Down
3 changes: 2 additions & 1 deletion src/backend/access/spgist/spginsert.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ spgbuild(Relation heap, Relation index, IndexInfo *indexInfo)
log_newpage_range(index, MAIN_FORKNUM,
0, RelationGetNumberOfBlocks(index),
true);
SetLastWrittenLSN(XactLastRecEnd, index->rd_smgr->smgr_rnode.node.relNode,
0, RelationGetNumberOfBlocks(index));
}
SetLastWrittenPageLSN(XactLastRecEnd);

smgr_end_unlogged_build(index->rd_smgr);

Expand Down
Loading

0 comments on commit 0d8c0e8

Please sign in to comment.