]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Speed up truncation of temporary relations.
authorFujii Masao <fujii@postgresql.org>
Fri, 4 Jul 2025 00:03:58 +0000 (09:03 +0900)
committerFujii Masao <fujii@postgresql.org>
Fri, 4 Jul 2025 00:03:58 +0000 (09:03 +0900)
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 6d05086c0a7 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 <3danissimo@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Dilip Kumar <dilipbalaut@gmail.com>
Reviewed-by: Maxim Orlov <orlovmg@gmail.com>
Discussion: https://postgr.es/m/CAJDiXggNqsJOH7C5co4jA8nDk8vw-=sokyh5s1_TENWnC6Ofcg@mail.gmail.com

src/backend/storage/buffer/bufmgr.c
src/backend/storage/buffer/localbuf.c
src/include/storage/buf_internals.h

index 667aa0c0c78d4d101163a0b4ca4aaf84a3106ab6..bd68d7e0ca9ee2d01aa8209c58c6e3a517bcab66 100644 (file)
@@ -4550,11 +4550,9 @@ DropRelationBuffers(SMgrRelation smgr_reln, ForkNumber *forkNum,
        if (RelFileLocatorBackendIsTemp(rlocator))
        {
                if (rlocator.backend == MyProcNumber)
-               {
-                       for (j = 0; j < nforks; j++)
-                               DropRelationLocalBuffers(rlocator.locator, forkNum[j],
-                                                                                firstDelBlock[j]);
-               }
+                       DropRelationLocalBuffers(rlocator.locator, forkNum, nforks,
+                                                                        firstDelBlock);
+
                return;
        }
 
index ba26627f7b00d5412108bd6bf12f83db1a862857..3da9c41ee1d7ad73dc260efd6a01696538e2c801 100644 (file)
@@ -660,10 +660,11 @@ InvalidateLocalBuffer(BufferDesc *bufHdr, bool check_unreferenced)
  *             See DropRelationBuffers in bufmgr.c for more notes.
  */
 void
-DropRelationLocalBuffers(RelFileLocator rlocator, ForkNumber forkNum,
-                                                BlockNumber firstDelBlock)
+DropRelationLocalBuffers(RelFileLocator rlocator, ForkNumber *forkNum,
+                                                int nforks, BlockNumber *firstDelBlock)
 {
        int                     i;
+       int                     j;
 
        for (i = 0; i < NLocBuffer; i++)
        {
@@ -672,12 +673,18 @@ DropRelationLocalBuffers(RelFileLocator rlocator, ForkNumber forkNum,
 
                buf_state = pg_atomic_read_u32(&bufHdr->state);
 
-               if ((buf_state & BM_TAG_VALID) &&
-                       BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator) &&
-                       BufTagGetForkNum(&bufHdr->tag) == forkNum &&
-                       bufHdr->tag.blockNum >= firstDelBlock)
+               if (!(buf_state & BM_TAG_VALID) ||
+                       !BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator))
+                       continue;
+
+               for (j = 0; j < nforks; j++)
                {
-                       InvalidateLocalBuffer(bufHdr, true);
+                       if (BufTagGetForkNum(&bufHdr->tag) == forkNum[j] &&
+                               bufHdr->tag.blockNum >= firstDelBlock[j])
+                       {
+                               InvalidateLocalBuffer(bufHdr, true);
+                               break;
+                       }
                }
        }
 }
index 0dec7d93b3b27e5aaf615228b96b47f257116888..52a71b138f736c71387cdee0fe6ca7dabd06db2d 100644 (file)
@@ -486,8 +486,8 @@ extern bool StartLocalBufferIO(BufferDesc *bufHdr, bool forInput, bool nowait);
 extern void FlushLocalBuffer(BufferDesc *bufHdr, SMgrRelation reln);
 extern void InvalidateLocalBuffer(BufferDesc *bufHdr, bool check_unreferenced);
 extern void DropRelationLocalBuffers(RelFileLocator rlocator,
-                                                                        ForkNumber forkNum,
-                                                                        BlockNumber firstDelBlock);
+                                                                        ForkNumber *forkNum, int nforks,
+                                                                        BlockNumber *firstDelBlock);
 extern void DropRelationAllLocalBuffers(RelFileLocator rlocator);
 extern void AtEOXact_LocalBuffers(bool isCommit);