mirror of
https://github.com/postgres/postgres.git
synced 2025-08-27 07:42:10 +03:00
Cache smgrnblocks() results in recovery.
Avoid repeatedly calling lseek(SEEK_END) during recovery by caching the size of each fork. For now, we can't use the same technique in other processes, because we lack a shared invalidation mechanism. Do this by generalizing the pre-existing caching used by FSM and VM to support all forks. Discussion: https://postgr.es/m/CAEepm%3D3SSw-Ty1DFcK%3D1rU-K6GSzYzfdD4d%2BZwapdN7dTa6%3DnQ%40mail.gmail.com
This commit is contained in:
@@ -561,17 +561,16 @@ vm_readbuf(Relation rel, BlockNumber blkno, bool extend)
|
||||
* If we haven't cached the size of the visibility map fork yet, check it
|
||||
* first.
|
||||
*/
|
||||
if (rel->rd_smgr->smgr_vm_nblocks == InvalidBlockNumber)
|
||||
if (rel->rd_smgr->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] == InvalidBlockNumber)
|
||||
{
|
||||
if (smgrexists(rel->rd_smgr, VISIBILITYMAP_FORKNUM))
|
||||
rel->rd_smgr->smgr_vm_nblocks = smgrnblocks(rel->rd_smgr,
|
||||
VISIBILITYMAP_FORKNUM);
|
||||
smgrnblocks(rel->rd_smgr, VISIBILITYMAP_FORKNUM);
|
||||
else
|
||||
rel->rd_smgr->smgr_vm_nblocks = 0;
|
||||
rel->rd_smgr->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] = 0;
|
||||
}
|
||||
|
||||
/* Handle requests beyond EOF */
|
||||
if (blkno >= rel->rd_smgr->smgr_vm_nblocks)
|
||||
if (blkno >= rel->rd_smgr->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM])
|
||||
{
|
||||
if (extend)
|
||||
vm_extend(rel, blkno + 1);
|
||||
@@ -641,11 +640,13 @@ vm_extend(Relation rel, BlockNumber vm_nblocks)
|
||||
* Create the file first if it doesn't exist. If smgr_vm_nblocks is
|
||||
* positive then it must exist, no need for an smgrexists call.
|
||||
*/
|
||||
if ((rel->rd_smgr->smgr_vm_nblocks == 0 ||
|
||||
rel->rd_smgr->smgr_vm_nblocks == InvalidBlockNumber) &&
|
||||
if ((rel->rd_smgr->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] == 0 ||
|
||||
rel->rd_smgr->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] == InvalidBlockNumber) &&
|
||||
!smgrexists(rel->rd_smgr, VISIBILITYMAP_FORKNUM))
|
||||
smgrcreate(rel->rd_smgr, VISIBILITYMAP_FORKNUM, false);
|
||||
|
||||
/* Invalidate cache so that smgrnblocks() asks the kernel. */
|
||||
rel->rd_smgr->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] = InvalidBlockNumber;
|
||||
vm_nblocks_now = smgrnblocks(rel->rd_smgr, VISIBILITYMAP_FORKNUM);
|
||||
|
||||
/* Now extend the file */
|
||||
@@ -667,8 +668,5 @@ vm_extend(Relation rel, BlockNumber vm_nblocks)
|
||||
*/
|
||||
CacheInvalidateSmgr(rel->rd_smgr->smgr_rnode);
|
||||
|
||||
/* Update local cache with the up-to-date size */
|
||||
rel->rd_smgr->smgr_vm_nblocks = vm_nblocks_now;
|
||||
|
||||
UnlockRelationForExtension(rel, ExclusiveLock);
|
||||
}
|
||||
|
Reference in New Issue
Block a user