LCOV - code coverage report
Current view: top level - src/backend/access/gist - gistbuildbuffers.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 0 218 0.0 %
Date: 2019-11-15 22:06:47 Functions: 0 17 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * gistbuildbuffers.c
       4             :  *    node buffer management functions for GiST buffering build algorithm.
       5             :  *
       6             :  *
       7             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/access/gist/gistbuildbuffers.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include "access/genam.h"
      18             : #include "access/gist_private.h"
      19             : #include "catalog/index.h"
      20             : #include "miscadmin.h"
      21             : #include "storage/buffile.h"
      22             : #include "storage/bufmgr.h"
      23             : #include "utils/memutils.h"
      24             : #include "utils/rel.h"
      25             : 
      26             : static GISTNodeBufferPage *gistAllocateNewPageBuffer(GISTBuildBuffers *gfbb);
      27             : static void gistAddLoadedBuffer(GISTBuildBuffers *gfbb,
      28             :                                 GISTNodeBuffer *nodeBuffer);
      29             : static void gistLoadNodeBuffer(GISTBuildBuffers *gfbb,
      30             :                                GISTNodeBuffer *nodeBuffer);
      31             : static void gistUnloadNodeBuffer(GISTBuildBuffers *gfbb,
      32             :                                  GISTNodeBuffer *nodeBuffer);
      33             : static void gistPlaceItupToPage(GISTNodeBufferPage *pageBuffer,
      34             :                                 IndexTuple item);
      35             : static void gistGetItupFromPage(GISTNodeBufferPage *pageBuffer,
      36             :                                 IndexTuple *item);
      37             : static long gistBuffersGetFreeBlock(GISTBuildBuffers *gfbb);
      38             : static void gistBuffersReleaseBlock(GISTBuildBuffers *gfbb, long blocknum);
      39             : 
      40             : static void ReadTempFileBlock(BufFile *file, long blknum, void *ptr);
      41             : static void WriteTempFileBlock(BufFile *file, long blknum, void *ptr);
      42             : 
      43             : 
      44             : /*
      45             :  * Initialize GiST build buffers.
      46             :  */
      47             : GISTBuildBuffers *
      48           0 : gistInitBuildBuffers(int pagesPerBuffer, int levelStep, int maxLevel)
      49             : {
      50             :     GISTBuildBuffers *gfbb;
      51             :     HASHCTL     hashCtl;
      52             : 
      53           0 :     gfbb = palloc(sizeof(GISTBuildBuffers));
      54           0 :     gfbb->pagesPerBuffer = pagesPerBuffer;
      55           0 :     gfbb->levelStep = levelStep;
      56             : 
      57             :     /*
      58             :      * Create a temporary file to hold buffer pages that are swapped out of
      59             :      * memory.
      60             :      */
      61           0 :     gfbb->pfile = BufFileCreateTemp(false);
      62           0 :     gfbb->nFileBlocks = 0;
      63             : 
      64             :     /* Initialize free page management. */
      65           0 :     gfbb->nFreeBlocks = 0;
      66           0 :     gfbb->freeBlocksLen = 32;
      67           0 :     gfbb->freeBlocks = (long *) palloc(gfbb->freeBlocksLen * sizeof(long));
      68             : 
      69             :     /*
      70             :      * Current memory context will be used for all in-memory data structures
      71             :      * of buffers which are persistent during buffering build.
      72             :      */
      73           0 :     gfbb->context = CurrentMemoryContext;
      74             : 
      75             :     /*
      76             :      * nodeBuffersTab hash is association between index blocks and it's
      77             :      * buffers.
      78             :      */
      79           0 :     memset(&hashCtl, 0, sizeof(hashCtl));
      80           0 :     hashCtl.keysize = sizeof(BlockNumber);
      81           0 :     hashCtl.entrysize = sizeof(GISTNodeBuffer);
      82           0 :     hashCtl.hcxt = CurrentMemoryContext;
      83           0 :     gfbb->nodeBuffersTab = hash_create("gistbuildbuffers",
      84             :                                        1024,
      85             :                                        &hashCtl,
      86             :                                        HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
      87             : 
      88           0 :     gfbb->bufferEmptyingQueue = NIL;
      89             : 
      90             :     /*
      91             :      * Per-level node buffers lists for final buffers emptying process. Node
      92             :      * buffers are inserted here when they are created.
      93             :      */
      94           0 :     gfbb->buffersOnLevelsLen = 1;
      95           0 :     gfbb->buffersOnLevels = (List **) palloc(sizeof(List *) *
      96           0 :                                              gfbb->buffersOnLevelsLen);
      97           0 :     gfbb->buffersOnLevels[0] = NIL;
      98             : 
      99             :     /*
     100             :      * Block numbers of node buffers which last pages are currently loaded
     101             :      * into main memory.
     102             :      */
     103           0 :     gfbb->loadedBuffersLen = 32;
     104           0 :     gfbb->loadedBuffers = (GISTNodeBuffer **) palloc(gfbb->loadedBuffersLen *
     105             :                                                      sizeof(GISTNodeBuffer *));
     106           0 :     gfbb->loadedBuffersCount = 0;
     107             : 
     108           0 :     gfbb->rootlevel = maxLevel;
     109             : 
     110           0 :     return gfbb;
     111             : }
     112             : 
     113             : /*
     114             :  * Returns a node buffer for given block. The buffer is created if it
     115             :  * doesn't exist yet.
     116             :  */
     117             : GISTNodeBuffer *
     118           0 : gistGetNodeBuffer(GISTBuildBuffers *gfbb, GISTSTATE *giststate,
     119             :                   BlockNumber nodeBlocknum, int level)
     120             : {
     121             :     GISTNodeBuffer *nodeBuffer;
     122             :     bool        found;
     123             : 
     124             :     /* Find node buffer in hash table */
     125           0 :     nodeBuffer = (GISTNodeBuffer *) hash_search(gfbb->nodeBuffersTab,
     126             :                                                 (const void *) &nodeBlocknum,
     127             :                                                 HASH_ENTER,
     128             :                                                 &found);
     129           0 :     if (!found)
     130             :     {
     131             :         /*
     132             :          * Node buffer wasn't found. Initialize the new buffer as empty.
     133             :          */
     134           0 :         MemoryContext oldcxt = MemoryContextSwitchTo(gfbb->context);
     135             : 
     136             :         /* nodeBuffer->nodeBlocknum is the hash key and was filled in already */
     137           0 :         nodeBuffer->blocksCount = 0;
     138           0 :         nodeBuffer->pageBlocknum = InvalidBlockNumber;
     139           0 :         nodeBuffer->pageBuffer = NULL;
     140           0 :         nodeBuffer->queuedForEmptying = false;
     141           0 :         nodeBuffer->isTemp = false;
     142           0 :         nodeBuffer->level = level;
     143             : 
     144             :         /*
     145             :          * Add this buffer to the list of buffers on this level. Enlarge
     146             :          * buffersOnLevels array if needed.
     147             :          */
     148           0 :         if (level >= gfbb->buffersOnLevelsLen)
     149             :         {
     150             :             int         i;
     151             : 
     152           0 :             gfbb->buffersOnLevels =
     153           0 :                 (List **) repalloc(gfbb->buffersOnLevels,
     154           0 :                                    (level + 1) * sizeof(List *));
     155             : 
     156             :             /* initialize the enlarged portion */
     157           0 :             for (i = gfbb->buffersOnLevelsLen; i <= level; i++)
     158           0 :                 gfbb->buffersOnLevels[i] = NIL;
     159           0 :             gfbb->buffersOnLevelsLen = level + 1;
     160             :         }
     161             : 
     162             :         /*
     163             :          * Prepend the new buffer to the list of buffers on this level. It's
     164             :          * not arbitrary that the new buffer is put to the beginning of the
     165             :          * list: in the final emptying phase we loop through all buffers at
     166             :          * each level, and flush them. If a page is split during the emptying,
     167             :          * it's more efficient to flush the new splitted pages first, before
     168             :          * moving on to pre-existing pages on the level. The buffers just
     169             :          * created during the page split are likely still in cache, so
     170             :          * flushing them immediately is more efficient than putting them to
     171             :          * the end of the queue.
     172             :          */
     173           0 :         gfbb->buffersOnLevels[level] = lcons(nodeBuffer,
     174           0 :                                              gfbb->buffersOnLevels[level]);
     175             : 
     176           0 :         MemoryContextSwitchTo(oldcxt);
     177             :     }
     178             : 
     179           0 :     return nodeBuffer;
     180             : }
     181             : 
     182             : /*
     183             :  * Allocate memory for a buffer page.
     184             :  */
     185             : static GISTNodeBufferPage *
     186           0 : gistAllocateNewPageBuffer(GISTBuildBuffers *gfbb)
     187             : {
     188             :     GISTNodeBufferPage *pageBuffer;
     189             : 
     190           0 :     pageBuffer = (GISTNodeBufferPage *) MemoryContextAllocZero(gfbb->context,
     191             :                                                                BLCKSZ);
     192           0 :     pageBuffer->prev = InvalidBlockNumber;
     193             : 
     194             :     /* Set page free space */
     195           0 :     PAGE_FREE_SPACE(pageBuffer) = BLCKSZ - BUFFER_PAGE_DATA_OFFSET;
     196           0 :     return pageBuffer;
     197             : }
     198             : 
     199             : /*
     200             :  * Add specified buffer into loadedBuffers array.
     201             :  */
     202             : static void
     203           0 : gistAddLoadedBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
     204             : {
     205             :     /* Never add a temporary buffer to the array */
     206           0 :     if (nodeBuffer->isTemp)
     207           0 :         return;
     208             : 
     209             :     /* Enlarge the array if needed */
     210           0 :     if (gfbb->loadedBuffersCount >= gfbb->loadedBuffersLen)
     211             :     {
     212           0 :         gfbb->loadedBuffersLen *= 2;
     213           0 :         gfbb->loadedBuffers = (GISTNodeBuffer **)
     214           0 :             repalloc(gfbb->loadedBuffers,
     215           0 :                      gfbb->loadedBuffersLen * sizeof(GISTNodeBuffer *));
     216             :     }
     217             : 
     218           0 :     gfbb->loadedBuffers[gfbb->loadedBuffersCount] = nodeBuffer;
     219           0 :     gfbb->loadedBuffersCount++;
     220             : }
     221             : 
     222             : /*
     223             :  * Load last page of node buffer into main memory.
     224             :  */
     225             : static void
     226           0 : gistLoadNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
     227             : {
     228             :     /* Check if we really should load something */
     229           0 :     if (!nodeBuffer->pageBuffer && nodeBuffer->blocksCount > 0)
     230             :     {
     231             :         /* Allocate memory for page */
     232           0 :         nodeBuffer->pageBuffer = gistAllocateNewPageBuffer(gfbb);
     233             : 
     234             :         /* Read block from temporary file */
     235           0 :         ReadTempFileBlock(gfbb->pfile, nodeBuffer->pageBlocknum,
     236           0 :                           nodeBuffer->pageBuffer);
     237             : 
     238             :         /* Mark file block as free */
     239           0 :         gistBuffersReleaseBlock(gfbb, nodeBuffer->pageBlocknum);
     240             : 
     241             :         /* Mark node buffer as loaded */
     242           0 :         gistAddLoadedBuffer(gfbb, nodeBuffer);
     243           0 :         nodeBuffer->pageBlocknum = InvalidBlockNumber;
     244             :     }
     245           0 : }
     246             : 
     247             : /*
     248             :  * Write last page of node buffer to the disk.
     249             :  */
     250             : static void
     251           0 : gistUnloadNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer)
     252             : {
     253             :     /* Check if we have something to write */
     254           0 :     if (nodeBuffer->pageBuffer)
     255             :     {
     256             :         BlockNumber blkno;
     257             : 
     258             :         /* Get free file block */
     259           0 :         blkno = gistBuffersGetFreeBlock(gfbb);
     260             : 
     261             :         /* Write block to the temporary file */
     262           0 :         WriteTempFileBlock(gfbb->pfile, blkno, nodeBuffer->pageBuffer);
     263             : 
     264             :         /* Free memory of that page */
     265           0 :         pfree(nodeBuffer->pageBuffer);
     266           0 :         nodeBuffer->pageBuffer = NULL;
     267             : 
     268             :         /* Save block number */
     269           0 :         nodeBuffer->pageBlocknum = blkno;
     270             :     }
     271           0 : }
     272             : 
     273             : /*
     274             :  * Write last pages of all node buffers to the disk.
     275             :  */
     276             : void
     277           0 : gistUnloadNodeBuffers(GISTBuildBuffers *gfbb)
     278             : {
     279             :     int         i;
     280             : 
     281             :     /* Unload all the buffers that have a page loaded in memory. */
     282           0 :     for (i = 0; i < gfbb->loadedBuffersCount; i++)
     283           0 :         gistUnloadNodeBuffer(gfbb, gfbb->loadedBuffers[i]);
     284             : 
     285             :     /* Now there are no node buffers with loaded last page */
     286           0 :     gfbb->loadedBuffersCount = 0;
     287           0 : }
     288             : 
     289             : /*
     290             :  * Add index tuple to buffer page.
     291             :  */
     292             : static void
     293           0 : gistPlaceItupToPage(GISTNodeBufferPage *pageBuffer, IndexTuple itup)
     294             : {
     295           0 :     Size        itupsz = IndexTupleSize(itup);
     296             :     char       *ptr;
     297             : 
     298             :     /* There should be enough of space. */
     299             :     Assert(PAGE_FREE_SPACE(pageBuffer) >= MAXALIGN(itupsz));
     300             : 
     301             :     /* Reduce free space value of page to reserve a spot for the tuple. */
     302           0 :     PAGE_FREE_SPACE(pageBuffer) -= MAXALIGN(itupsz);
     303             : 
     304             :     /* Get pointer to the spot we reserved (ie. end of free space). */
     305           0 :     ptr = (char *) pageBuffer + BUFFER_PAGE_DATA_OFFSET
     306           0 :         + PAGE_FREE_SPACE(pageBuffer);
     307             : 
     308             :     /* Copy the index tuple there. */
     309           0 :     memcpy(ptr, itup, itupsz);
     310           0 : }
     311             : 
     312             : /*
     313             :  * Get last item from buffer page and remove it from page.
     314             :  */
     315             : static void
     316           0 : gistGetItupFromPage(GISTNodeBufferPage *pageBuffer, IndexTuple *itup)
     317             : {
     318             :     IndexTuple  ptr;
     319             :     Size        itupsz;
     320             : 
     321             :     Assert(!PAGE_IS_EMPTY(pageBuffer)); /* Page shouldn't be empty */
     322             : 
     323             :     /* Get pointer to last index tuple */
     324           0 :     ptr = (IndexTuple) ((char *) pageBuffer
     325             :                         + BUFFER_PAGE_DATA_OFFSET
     326           0 :                         + PAGE_FREE_SPACE(pageBuffer));
     327           0 :     itupsz = IndexTupleSize(ptr);
     328             : 
     329             :     /* Make a copy of the tuple */
     330           0 :     *itup = (IndexTuple) palloc(itupsz);
     331           0 :     memcpy(*itup, ptr, itupsz);
     332             : 
     333             :     /* Mark the space used by the tuple as free */
     334           0 :     PAGE_FREE_SPACE(pageBuffer) += MAXALIGN(itupsz);
     335           0 : }
     336             : 
     337             : /*
     338             :  * Push an index tuple to node buffer.
     339             :  */
     340             : void
     341           0 : gistPushItupToNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer,
     342             :                          IndexTuple itup)
     343             : {
     344             :     /*
     345             :      * Most part of memory operations will be in buffering build persistent
     346             :      * context. So, let's switch to it.
     347             :      */
     348           0 :     MemoryContext oldcxt = MemoryContextSwitchTo(gfbb->context);
     349             : 
     350             :     /*
     351             :      * If the buffer is currently empty, create the first page.
     352             :      */
     353           0 :     if (nodeBuffer->blocksCount == 0)
     354             :     {
     355           0 :         nodeBuffer->pageBuffer = gistAllocateNewPageBuffer(gfbb);
     356           0 :         nodeBuffer->blocksCount = 1;
     357           0 :         gistAddLoadedBuffer(gfbb, nodeBuffer);
     358             :     }
     359             : 
     360             :     /* Load last page of node buffer if it wasn't in memory already */
     361           0 :     if (!nodeBuffer->pageBuffer)
     362           0 :         gistLoadNodeBuffer(gfbb, nodeBuffer);
     363             : 
     364             :     /*
     365             :      * Check if there is enough space on the last page for the tuple.
     366             :      */
     367           0 :     if (PAGE_NO_SPACE(nodeBuffer->pageBuffer, itup))
     368             :     {
     369             :         /*
     370             :          * Nope. Swap previous block to disk and allocate a new one.
     371             :          */
     372             :         BlockNumber blkno;
     373             : 
     374             :         /* Write filled page to the disk */
     375           0 :         blkno = gistBuffersGetFreeBlock(gfbb);
     376           0 :         WriteTempFileBlock(gfbb->pfile, blkno, nodeBuffer->pageBuffer);
     377             : 
     378             :         /*
     379             :          * Reset the in-memory page as empty, and link the previous block to
     380             :          * the new page by storing its block number in the prev-link.
     381             :          */
     382           0 :         PAGE_FREE_SPACE(nodeBuffer->pageBuffer) =
     383             :             BLCKSZ - MAXALIGN(offsetof(GISTNodeBufferPage, tupledata));
     384           0 :         nodeBuffer->pageBuffer->prev = blkno;
     385             : 
     386             :         /* We've just added one more page */
     387           0 :         nodeBuffer->blocksCount++;
     388             :     }
     389             : 
     390           0 :     gistPlaceItupToPage(nodeBuffer->pageBuffer, itup);
     391             : 
     392             :     /*
     393             :      * If the buffer just overflowed, add it to the emptying queue.
     394             :      */
     395           0 :     if (BUFFER_HALF_FILLED(nodeBuffer, gfbb) && !nodeBuffer->queuedForEmptying)
     396             :     {
     397           0 :         gfbb->bufferEmptyingQueue = lcons(nodeBuffer,
     398             :                                           gfbb->bufferEmptyingQueue);
     399           0 :         nodeBuffer->queuedForEmptying = true;
     400             :     }
     401             : 
     402             :     /* Restore memory context */
     403           0 :     MemoryContextSwitchTo(oldcxt);
     404           0 : }
     405             : 
     406             : /*
     407             :  * Removes one index tuple from node buffer. Returns true if success and false
     408             :  * if node buffer is empty.
     409             :  */
     410             : bool
     411           0 : gistPopItupFromNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer,
     412             :                           IndexTuple *itup)
     413             : {
     414             :     /*
     415             :      * If node buffer is empty then return false.
     416             :      */
     417           0 :     if (nodeBuffer->blocksCount <= 0)
     418           0 :         return false;
     419             : 
     420             :     /* Load last page of node buffer if needed */
     421           0 :     if (!nodeBuffer->pageBuffer)
     422           0 :         gistLoadNodeBuffer(gfbb, nodeBuffer);
     423             : 
     424             :     /*
     425             :      * Get index tuple from last non-empty page.
     426             :      */
     427           0 :     gistGetItupFromPage(nodeBuffer->pageBuffer, itup);
     428             : 
     429             :     /*
     430             :      * If we just removed the last tuple from the page, fetch previous page on
     431             :      * this node buffer (if any).
     432             :      */
     433           0 :     if (PAGE_IS_EMPTY(nodeBuffer->pageBuffer))
     434             :     {
     435             :         BlockNumber prevblkno;
     436             : 
     437             :         /*
     438             :          * blocksCount includes the page in pageBuffer, so decrease it now.
     439             :          */
     440           0 :         nodeBuffer->blocksCount--;
     441             : 
     442             :         /*
     443             :          * If there's more pages, fetch previous one.
     444             :          */
     445           0 :         prevblkno = nodeBuffer->pageBuffer->prev;
     446           0 :         if (prevblkno != InvalidBlockNumber)
     447             :         {
     448             :             /* There is a previous page. Fetch it. */
     449             :             Assert(nodeBuffer->blocksCount > 0);
     450           0 :             ReadTempFileBlock(gfbb->pfile, prevblkno, nodeBuffer->pageBuffer);
     451             : 
     452             :             /*
     453             :              * Now that we've read the block in memory, we can release its
     454             :              * on-disk block for reuse.
     455             :              */
     456           0 :             gistBuffersReleaseBlock(gfbb, prevblkno);
     457             :         }
     458             :         else
     459             :         {
     460             :             /* No more pages. Free memory. */
     461             :             Assert(nodeBuffer->blocksCount == 0);
     462           0 :             pfree(nodeBuffer->pageBuffer);
     463           0 :             nodeBuffer->pageBuffer = NULL;
     464             :         }
     465             :     }
     466           0 :     return true;
     467             : }
     468             : 
     469             : /*
     470             :  * Select a currently unused block for writing to.
     471             :  */
     472             : static long
     473           0 : gistBuffersGetFreeBlock(GISTBuildBuffers *gfbb)
     474             : {
     475             :     /*
     476             :      * If there are multiple free blocks, we select the one appearing last in
     477             :      * freeBlocks[].  If there are none, assign the next block at the end of
     478             :      * the file (causing the file to be extended).
     479             :      */
     480           0 :     if (gfbb->nFreeBlocks > 0)
     481           0 :         return gfbb->freeBlocks[--gfbb->nFreeBlocks];
     482             :     else
     483           0 :         return gfbb->nFileBlocks++;
     484             : }
     485             : 
     486             : /*
     487             :  * Return a block# to the freelist.
     488             :  */
     489             : static void
     490           0 : gistBuffersReleaseBlock(GISTBuildBuffers *gfbb, long blocknum)
     491             : {
     492             :     int         ndx;
     493             : 
     494             :     /* Enlarge freeBlocks array if full. */
     495           0 :     if (gfbb->nFreeBlocks >= gfbb->freeBlocksLen)
     496             :     {
     497           0 :         gfbb->freeBlocksLen *= 2;
     498           0 :         gfbb->freeBlocks = (long *) repalloc(gfbb->freeBlocks,
     499           0 :                                              gfbb->freeBlocksLen *
     500             :                                              sizeof(long));
     501             :     }
     502             : 
     503             :     /* Add blocknum to array */
     504           0 :     ndx = gfbb->nFreeBlocks++;
     505           0 :     gfbb->freeBlocks[ndx] = blocknum;
     506           0 : }
     507             : 
     508             : /*
     509             :  * Free buffering build data structure.
     510             :  */
     511             : void
     512           0 : gistFreeBuildBuffers(GISTBuildBuffers *gfbb)
     513             : {
     514             :     /* Close buffers file. */
     515           0 :     BufFileClose(gfbb->pfile);
     516             : 
     517             :     /* All other things will be freed on memory context release */
     518           0 : }
     519             : 
     520             : /*
     521             :  * Data structure representing information about node buffer for index tuples
     522             :  * relocation from splitted node buffer.
     523             :  */
     524             : typedef struct
     525             : {
     526             :     GISTENTRY   entry[INDEX_MAX_KEYS];
     527             :     bool        isnull[INDEX_MAX_KEYS];
     528             :     GISTPageSplitInfo *splitinfo;
     529             :     GISTNodeBuffer *nodeBuffer;
     530             : } RelocationBufferInfo;
     531             : 
     532             : /*
     533             :  * At page split, distribute tuples from the buffer of the split page to
     534             :  * new buffers for the created page halves. This also adjusts the downlinks
     535             :  * in 'splitinfo' to include the tuples in the buffers.
     536             :  */
     537             : void
     538           0 : gistRelocateBuildBuffersOnSplit(GISTBuildBuffers *gfbb, GISTSTATE *giststate,
     539             :                                 Relation r, int level,
     540             :                                 Buffer buffer, List *splitinfo)
     541             : {
     542             :     RelocationBufferInfo *relocationBuffersInfos;
     543             :     bool        found;
     544             :     GISTNodeBuffer *nodeBuffer;
     545             :     BlockNumber blocknum;
     546             :     IndexTuple  itup;
     547           0 :     int         splitPagesCount = 0,
     548             :                 i;
     549             :     GISTENTRY   entry[INDEX_MAX_KEYS];
     550             :     bool        isnull[INDEX_MAX_KEYS];
     551             :     GISTNodeBuffer oldBuf;
     552             :     ListCell   *lc;
     553             : 
     554             :     /* If the splitted page doesn't have buffers, we have nothing to do. */
     555           0 :     if (!LEVEL_HAS_BUFFERS(level, gfbb))
     556           0 :         return;
     557             : 
     558             :     /*
     559             :      * Get the node buffer of the splitted page.
     560             :      */
     561           0 :     blocknum = BufferGetBlockNumber(buffer);
     562           0 :     nodeBuffer = hash_search(gfbb->nodeBuffersTab, &blocknum,
     563             :                              HASH_FIND, &found);
     564           0 :     if (!found)
     565             :     {
     566             :         /* The page has no buffer, so we have nothing to do. */
     567           0 :         return;
     568             :     }
     569             : 
     570             :     /*
     571             :      * Make a copy of the old buffer, as we're going reuse it as the buffer
     572             :      * for the new left page, which is on the same block as the old page.
     573             :      * That's not true for the root page, but that's fine because we never
     574             :      * have a buffer on the root page anyway. The original algorithm as
     575             :      * described by Arge et al did, but it's of no use, as you might as well
     576             :      * read the tuples straight from the heap instead of the root buffer.
     577             :      */
     578             :     Assert(blocknum != GIST_ROOT_BLKNO);
     579           0 :     memcpy(&oldBuf, nodeBuffer, sizeof(GISTNodeBuffer));
     580           0 :     oldBuf.isTemp = true;
     581             : 
     582             :     /* Reset the old buffer, used for the new left page from now on */
     583           0 :     nodeBuffer->blocksCount = 0;
     584           0 :     nodeBuffer->pageBuffer = NULL;
     585           0 :     nodeBuffer->pageBlocknum = InvalidBlockNumber;
     586             : 
     587             :     /*
     588             :      * Allocate memory for information about relocation buffers.
     589             :      */
     590           0 :     splitPagesCount = list_length(splitinfo);
     591           0 :     relocationBuffersInfos =
     592           0 :         (RelocationBufferInfo *) palloc(sizeof(RelocationBufferInfo) *
     593             :                                         splitPagesCount);
     594             : 
     595             :     /*
     596             :      * Fill relocation buffers information for node buffers of pages produced
     597             :      * by split.
     598             :      */
     599           0 :     i = 0;
     600           0 :     foreach(lc, splitinfo)
     601             :     {
     602           0 :         GISTPageSplitInfo *si = (GISTPageSplitInfo *) lfirst(lc);
     603             :         GISTNodeBuffer *newNodeBuffer;
     604             : 
     605             :         /* Decompress parent index tuple of node buffer page. */
     606           0 :         gistDeCompressAtt(giststate, r,
     607             :                           si->downlink, NULL, (OffsetNumber) 0,
     608           0 :                           relocationBuffersInfos[i].entry,
     609           0 :                           relocationBuffersInfos[i].isnull);
     610             : 
     611             :         /*
     612             :          * Create a node buffer for the page. The leftmost half is on the same
     613             :          * block as the old page before split, so for the leftmost half this
     614             :          * will return the original buffer. The tuples on the original buffer
     615             :          * were relinked to the temporary buffer, so the original one is now
     616             :          * empty.
     617             :          */
     618           0 :         newNodeBuffer = gistGetNodeBuffer(gfbb, giststate, BufferGetBlockNumber(si->buf), level);
     619             : 
     620           0 :         relocationBuffersInfos[i].nodeBuffer = newNodeBuffer;
     621           0 :         relocationBuffersInfos[i].splitinfo = si;
     622             : 
     623           0 :         i++;
     624             :     }
     625             : 
     626             :     /*
     627             :      * Loop through all index tuples in the buffer of the page being split,
     628             :      * moving them to buffers for the new pages.  We try to move each tuple to
     629             :      * the page that will result in the lowest penalty for the leading column
     630             :      * or, in the case of a tie, the lowest penalty for the earliest column
     631             :      * that is not tied.
     632             :      *
     633             :      * The page searching logic is very similar to gistchoose().
     634             :      */
     635           0 :     while (gistPopItupFromNodeBuffer(gfbb, &oldBuf, &itup))
     636             :     {
     637             :         float       best_penalty[INDEX_MAX_KEYS];
     638             :         int         i,
     639             :                     which;
     640             :         IndexTuple  newtup;
     641             :         RelocationBufferInfo *targetBufferInfo;
     642             : 
     643           0 :         gistDeCompressAtt(giststate, r,
     644             :                           itup, NULL, (OffsetNumber) 0, entry, isnull);
     645             : 
     646             :         /* default to using first page (shouldn't matter) */
     647           0 :         which = 0;
     648             : 
     649             :         /*
     650             :          * best_penalty[j] is the best penalty we have seen so far for column
     651             :          * j, or -1 when we haven't yet examined column j.  Array entries to
     652             :          * the right of the first -1 are undefined.
     653             :          */
     654           0 :         best_penalty[0] = -1;
     655             : 
     656             :         /*
     657             :          * Loop over possible target pages, looking for one to move this tuple
     658             :          * to.
     659             :          */
     660           0 :         for (i = 0; i < splitPagesCount; i++)
     661             :         {
     662           0 :             RelocationBufferInfo *splitPageInfo = &relocationBuffersInfos[i];
     663             :             bool        zero_penalty;
     664             :             int         j;
     665             : 
     666           0 :             zero_penalty = true;
     667             : 
     668             :             /* Loop over index attributes. */
     669           0 :             for (j = 0; j < r->rd_att->natts; j++)
     670             :             {
     671             :                 float       usize;
     672             : 
     673             :                 /* Compute penalty for this column. */
     674           0 :                 usize = gistpenalty(giststate, j,
     675             :                                     &splitPageInfo->entry[j],
     676           0 :                                     splitPageInfo->isnull[j],
     677           0 :                                     &entry[j], isnull[j]);
     678           0 :                 if (usize > 0)
     679           0 :                     zero_penalty = false;
     680             : 
     681           0 :                 if (best_penalty[j] < 0 || usize < best_penalty[j])
     682             :                 {
     683             :                     /*
     684             :                      * New best penalty for column.  Tentatively select this
     685             :                      * page as the target, and record the best penalty.  Then
     686             :                      * reset the next column's penalty to "unknown" (and
     687             :                      * indirectly, the same for all the ones to its right).
     688             :                      * This will force us to adopt this page's penalty values
     689             :                      * as the best for all the remaining columns during
     690             :                      * subsequent loop iterations.
     691             :                      */
     692           0 :                     which = i;
     693           0 :                     best_penalty[j] = usize;
     694             : 
     695           0 :                     if (j < r->rd_att->natts - 1)
     696           0 :                         best_penalty[j + 1] = -1;
     697             :                 }
     698           0 :                 else if (best_penalty[j] == usize)
     699             :                 {
     700             :                     /*
     701             :                      * The current page is exactly as good for this column as
     702             :                      * the best page seen so far.  The next iteration of this
     703             :                      * loop will compare the next column.
     704             :                      */
     705             :                 }
     706             :                 else
     707             :                 {
     708             :                     /*
     709             :                      * The current page is worse for this column than the best
     710             :                      * page seen so far.  Skip the remaining columns and move
     711             :                      * on to the next page, if any.
     712             :                      */
     713           0 :                     zero_penalty = false;   /* so outer loop won't exit */
     714           0 :                     break;
     715             :                 }
     716             :             }
     717             : 
     718             :             /*
     719             :              * If we find a page with zero penalty for all columns, there's no
     720             :              * need to examine remaining pages; just break out of the loop and
     721             :              * return it.
     722             :              */
     723           0 :             if (zero_penalty)
     724           0 :                 break;
     725             :         }
     726             : 
     727             :         /* OK, "which" is the page index to push the tuple to */
     728           0 :         targetBufferInfo = &relocationBuffersInfos[which];
     729             : 
     730             :         /* Push item to selected node buffer */
     731           0 :         gistPushItupToNodeBuffer(gfbb, targetBufferInfo->nodeBuffer, itup);
     732             : 
     733             :         /* Adjust the downlink for this page, if needed. */
     734           0 :         newtup = gistgetadjusted(r, targetBufferInfo->splitinfo->downlink,
     735             :                                  itup, giststate);
     736           0 :         if (newtup)
     737             :         {
     738           0 :             gistDeCompressAtt(giststate, r,
     739             :                               newtup, NULL, (OffsetNumber) 0,
     740           0 :                               targetBufferInfo->entry,
     741           0 :                               targetBufferInfo->isnull);
     742             : 
     743           0 :             targetBufferInfo->splitinfo->downlink = newtup;
     744             :         }
     745             :     }
     746             : 
     747           0 :     pfree(relocationBuffersInfos);
     748             : }
     749             : 
     750             : 
     751             : /*
     752             :  * Wrappers around BufFile operations. The main difference is that these
     753             :  * wrappers report errors with ereport(), so that the callers don't need
     754             :  * to check the return code.
     755             :  */
     756             : 
     757             : static void
     758           0 : ReadTempFileBlock(BufFile *file, long blknum, void *ptr)
     759             : {
     760           0 :     if (BufFileSeekBlock(file, blknum) != 0)
     761           0 :         elog(ERROR, "could not seek temporary file: %m");
     762           0 :     if (BufFileRead(file, ptr, BLCKSZ) != BLCKSZ)
     763           0 :         elog(ERROR, "could not read temporary file: %m");
     764           0 : }
     765             : 
     766             : static void
     767           0 : WriteTempFileBlock(BufFile *file, long blknum, void *ptr)
     768             : {
     769           0 :     if (BufFileSeekBlock(file, blknum) != 0)
     770           0 :         elog(ERROR, "could not seek temporary file: %m");
     771           0 :     if (BufFileWrite(file, ptr, BLCKSZ) != BLCKSZ)
     772             :     {
     773             :         /*
     774             :          * the other errors in Read/WriteTempFileBlock shouldn't happen, but
     775             :          * an error at write can easily happen if you run out of disk space.
     776             :          */
     777           0 :         ereport(ERROR,
     778             :                 (errcode_for_file_access(),
     779             :                  errmsg("could not write block %ld of temporary file: %m",
     780             :                         blknum)));
     781             :     }
     782           0 : }

Generated by: LCOV version 1.13