Skip to content

Commit 78ebda6

Browse files
committed
Speed up truncation of temporary relations.
Previously, truncating a temporary relation required scanning the entire local buffer pool once per relation fork to invalidate buffers. This could be slow, especially with a large local buffers, as the scan was repeated multiple times. A similar issue with regular tables (shared buffers) was addressed in commit 6d05086 by scanning the buffer pool only once for all forks. This commit applies the same optimization to temporary relations, improving truncation performance. Author: Daniil Davydov <[email protected]> Reviewed-by: Michael Paquier <[email protected]> Reviewed-by: Fujii Masao <[email protected]> Reviewed-by: Dilip Kumar <[email protected]> Reviewed-by: Maxim Orlov <[email protected]> Discussion: https://postgr.es/m/CAJDiXggNqsJOH7C5co4jA8nDk8vw-=sokyh5s1_TENWnC6Ofcg@mail.gmail.com
1 parent 931766a commit 78ebda6

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

src/backend/storage/buffer/bufmgr.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,11 +4550,9 @@ DropRelationBuffers(SMgrRelation smgr_reln, ForkNumber *forkNum,
45504550
if (RelFileLocatorBackendIsTemp(rlocator))
45514551
{
45524552
if (rlocator.backend == MyProcNumber)
4553-
{
4554-
for (j = 0; j < nforks; j++)
4555-
DropRelationLocalBuffers(rlocator.locator, forkNum[j],
4556-
firstDelBlock[j]);
4557-
}
4553+
DropRelationLocalBuffers(rlocator.locator, forkNum, nforks,
4554+
firstDelBlock);
4555+
45584556
return;
45594557
}
45604558

src/backend/storage/buffer/localbuf.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -660,10 +660,11 @@ InvalidateLocalBuffer(BufferDesc *bufHdr, bool check_unreferenced)
660660
* See DropRelationBuffers in bufmgr.c for more notes.
661661
*/
662662
void
663-
DropRelationLocalBuffers(RelFileLocator rlocator, ForkNumber forkNum,
664-
BlockNumber firstDelBlock)
663+
DropRelationLocalBuffers(RelFileLocator rlocator, ForkNumber *forkNum,
664+
int nforks, BlockNumber *firstDelBlock)
665665
{
666666
int i;
667+
int j;
667668

668669
for (i = 0; i < NLocBuffer; i++)
669670
{
@@ -672,12 +673,18 @@ DropRelationLocalBuffers(RelFileLocator rlocator, ForkNumber forkNum,
672673

673674
buf_state = pg_atomic_read_u32(&bufHdr->state);
674675

675-
if ((buf_state & BM_TAG_VALID) &&
676-
BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator) &&
677-
BufTagGetForkNum(&bufHdr->tag) == forkNum &&
678-
bufHdr->tag.blockNum >= firstDelBlock)
676+
if (!(buf_state & BM_TAG_VALID) ||
677+
!BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator))
678+
continue;
679+
680+
for (j = 0; j < nforks; j++)
679681
{
680-
InvalidateLocalBuffer(bufHdr, true);
682+
if (BufTagGetForkNum(&bufHdr->tag) == forkNum[j] &&
683+
bufHdr->tag.blockNum >= firstDelBlock[j])
684+
{
685+
InvalidateLocalBuffer(bufHdr, true);
686+
break;
687+
}
681688
}
682689
}
683690
}

src/include/storage/buf_internals.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ extern bool StartLocalBufferIO(BufferDesc *bufHdr, bool forInput, bool nowait);
486486
extern void FlushLocalBuffer(BufferDesc *bufHdr, SMgrRelation reln);
487487
extern void InvalidateLocalBuffer(BufferDesc *bufHdr, bool check_unreferenced);
488488
extern void DropRelationLocalBuffers(RelFileLocator rlocator,
489-
ForkNumber forkNum,
490-
BlockNumber firstDelBlock);
489+
ForkNumber *forkNum, int nforks,
490+
BlockNumber *firstDelBlock);
491491
extern void DropRelationAllLocalBuffers(RelFileLocator rlocator);
492492
extern void AtEOXact_LocalBuffers(bool isCommit);
493493

0 commit comments

Comments
 (0)