LCOV - code coverage report
Current view: top level - src/backend/access/gist - gistxlog.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 49 246 19.9 %
Date: 2019-11-21 13:06:38 Functions: 5 16 31.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * gistxlog.c
       4             :  *    WAL replay logic for GiST.
       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/gistxlog.c
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "access/bufmask.h"
      17             : #include "access/gist_private.h"
      18             : #include "access/gistxlog.h"
      19             : #include "access/heapam_xlog.h"
      20             : #include "access/transam.h"
      21             : #include "access/xloginsert.h"
      22             : #include "access/xlogutils.h"
      23             : #include "miscadmin.h"
      24             : #include "storage/procarray.h"
      25             : #include "utils/memutils.h"
      26             : #include "utils/rel.h"
      27             : 
      28             : static MemoryContext opCtx;     /* working memory for operations */
      29             : 
      30             : /*
      31             :  * Replay the clearing of F_FOLLOW_RIGHT flag on a child page.
      32             :  *
      33             :  * Even if the WAL record includes a full-page image, we have to update the
      34             :  * follow-right flag, because that change is not included in the full-page
      35             :  * image.  To be sure that the intermediate state with the wrong flag value is
      36             :  * not visible to concurrent Hot Standby queries, this function handles
      37             :  * restoring the full-page image as well as updating the flag.  (Note that
      38             :  * we never need to do anything else to the child page in the current WAL
      39             :  * action.)
      40             :  */
      41             : static void
      42           0 : gistRedoClearFollowRight(XLogReaderState *record, uint8 block_id)
      43             : {
      44           0 :     XLogRecPtr  lsn = record->EndRecPtr;
      45             :     Buffer      buffer;
      46             :     Page        page;
      47             :     XLogRedoAction action;
      48             : 
      49             :     /*
      50             :      * Note that we still update the page even if it was restored from a full
      51             :      * page image, because the updated NSN is not included in the image.
      52             :      */
      53           0 :     action = XLogReadBufferForRedo(record, block_id, &buffer);
      54           0 :     if (action == BLK_NEEDS_REDO || action == BLK_RESTORED)
      55             :     {
      56           0 :         page = BufferGetPage(buffer);
      57             : 
      58           0 :         GistPageSetNSN(page, lsn);
      59           0 :         GistClearFollowRight(page);
      60             : 
      61           0 :         PageSetLSN(page, lsn);
      62           0 :         MarkBufferDirty(buffer);
      63             :     }
      64           0 :     if (BufferIsValid(buffer))
      65           0 :         UnlockReleaseBuffer(buffer);
      66           0 : }
      67             : 
      68             : /*
      69             :  * redo any page update (except page split)
      70             :  */
      71             : static void
      72           0 : gistRedoPageUpdateRecord(XLogReaderState *record)
      73             : {
      74           0 :     XLogRecPtr  lsn = record->EndRecPtr;
      75           0 :     gistxlogPageUpdate *xldata = (gistxlogPageUpdate *) XLogRecGetData(record);
      76             :     Buffer      buffer;
      77             :     Page        page;
      78             : 
      79           0 :     if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
      80             :     {
      81             :         char       *begin;
      82             :         char       *data;
      83             :         Size        datalen;
      84           0 :         int         ninserted = 0;
      85             : 
      86           0 :         data = begin = XLogRecGetBlockData(record, 0, &datalen);
      87             : 
      88           0 :         page = (Page) BufferGetPage(buffer);
      89             : 
      90           0 :         if (xldata->ntodelete == 1 && xldata->ntoinsert == 1)
      91           0 :         {
      92             :             /*
      93             :              * When replacing one tuple with one other tuple, we must use
      94             :              * PageIndexTupleOverwrite for consistency with gistplacetopage.
      95             :              */
      96           0 :             OffsetNumber offnum = *((OffsetNumber *) data);
      97             :             IndexTuple  itup;
      98             :             Size        itupsize;
      99             : 
     100           0 :             data += sizeof(OffsetNumber);
     101           0 :             itup = (IndexTuple) data;
     102           0 :             itupsize = IndexTupleSize(itup);
     103           0 :             if (!PageIndexTupleOverwrite(page, offnum, (Item) itup, itupsize))
     104           0 :                 elog(ERROR, "failed to add item to GiST index page, size %d bytes",
     105             :                      (int) itupsize);
     106           0 :             data += itupsize;
     107             :             /* should be nothing left after consuming 1 tuple */
     108             :             Assert(data - begin == datalen);
     109             :             /* update insertion count for assert check below */
     110           0 :             ninserted++;
     111             :         }
     112           0 :         else if (xldata->ntodelete > 0)
     113             :         {
     114             :             /* Otherwise, delete old tuples if any */
     115           0 :             OffsetNumber *todelete = (OffsetNumber *) data;
     116             : 
     117           0 :             data += sizeof(OffsetNumber) * xldata->ntodelete;
     118             : 
     119           0 :             PageIndexMultiDelete(page, todelete, xldata->ntodelete);
     120           0 :             if (GistPageIsLeaf(page))
     121           0 :                 GistMarkTuplesDeleted(page);
     122             :         }
     123             : 
     124             :         /* Add new tuples if any */
     125           0 :         if (data - begin < datalen)
     126             :         {
     127           0 :             OffsetNumber off = (PageIsEmpty(page)) ? FirstOffsetNumber :
     128           0 :             OffsetNumberNext(PageGetMaxOffsetNumber(page));
     129             : 
     130           0 :             while (data - begin < datalen)
     131             :             {
     132           0 :                 IndexTuple  itup = (IndexTuple) data;
     133           0 :                 Size        sz = IndexTupleSize(itup);
     134             :                 OffsetNumber l;
     135             : 
     136           0 :                 data += sz;
     137             : 
     138           0 :                 l = PageAddItem(page, (Item) itup, sz, off, false, false);
     139           0 :                 if (l == InvalidOffsetNumber)
     140           0 :                     elog(ERROR, "failed to add item to GiST index page, size %d bytes",
     141             :                          (int) sz);
     142           0 :                 off++;
     143           0 :                 ninserted++;
     144             :             }
     145             :         }
     146             : 
     147             :         /* Check that XLOG record contained expected number of tuples */
     148             :         Assert(ninserted == xldata->ntoinsert);
     149             : 
     150           0 :         PageSetLSN(page, lsn);
     151           0 :         MarkBufferDirty(buffer);
     152             :     }
     153             : 
     154             :     /*
     155             :      * Fix follow-right data on left child page
     156             :      *
     157             :      * This must be done while still holding the lock on the target page. Note
     158             :      * that even if the target page no longer exists, we still attempt to
     159             :      * replay the change on the child page.
     160             :      */
     161           0 :     if (XLogRecHasBlockRef(record, 1))
     162           0 :         gistRedoClearFollowRight(record, 1);
     163             : 
     164           0 :     if (BufferIsValid(buffer))
     165           0 :         UnlockReleaseBuffer(buffer);
     166           0 : }
     167             : 
     168             : 
     169             : /*
     170             :  * redo delete on gist index page to remove tuples marked as DEAD during index
     171             :  * tuple insertion
     172             :  */
     173             : static void
     174           0 : gistRedoDeleteRecord(XLogReaderState *record)
     175             : {
     176           0 :     XLogRecPtr  lsn = record->EndRecPtr;
     177           0 :     gistxlogDelete *xldata = (gistxlogDelete *) XLogRecGetData(record);
     178             :     Buffer      buffer;
     179             :     Page        page;
     180             : 
     181             :     /*
     182             :      * If we have any conflict processing to do, it must happen before we
     183             :      * update the page.
     184             :      *
     185             :      * GiST delete records can conflict with standby queries.  You might think
     186             :      * that vacuum records would conflict as well, but we've handled that
     187             :      * already.  XLOG_HEAP2_CLEANUP_INFO records provide the highest xid
     188             :      * cleaned by the vacuum of the heap and so we can resolve any conflicts
     189             :      * just once when that arrives.  After that we know that no conflicts
     190             :      * exist from individual gist vacuum records on that index.
     191             :      */
     192           0 :     if (InHotStandby)
     193             :     {
     194             :         RelFileNode rnode;
     195             : 
     196           0 :         XLogRecGetBlockTag(record, 0, &rnode, NULL, NULL);
     197             : 
     198           0 :         ResolveRecoveryConflictWithSnapshot(xldata->latestRemovedXid, rnode);
     199             :     }
     200             : 
     201           0 :     if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
     202             :     {
     203           0 :         page = (Page) BufferGetPage(buffer);
     204             : 
     205           0 :         if (XLogRecGetDataLen(record) > SizeOfGistxlogDelete)
     206             :         {
     207             :             OffsetNumber *todelete;
     208             : 
     209           0 :             todelete = (OffsetNumber *) ((char *) xldata + SizeOfGistxlogDelete);
     210             : 
     211           0 :             PageIndexMultiDelete(page, todelete, xldata->ntodelete);
     212             :         }
     213             : 
     214           0 :         GistClearPageHasGarbage(page);
     215           0 :         GistMarkTuplesDeleted(page);
     216             : 
     217           0 :         PageSetLSN(page, lsn);
     218           0 :         MarkBufferDirty(buffer);
     219             :     }
     220             : 
     221           0 :     if (BufferIsValid(buffer))
     222           0 :         UnlockReleaseBuffer(buffer);
     223           0 : }
     224             : 
     225             : /*
     226             :  * Returns an array of index pointers.
     227             :  */
     228             : static IndexTuple *
     229           0 : decodePageSplitRecord(char *begin, int len, int *n)
     230             : {
     231             :     char       *ptr;
     232           0 :     int         i = 0;
     233             :     IndexTuple *tuples;
     234             : 
     235             :     /* extract the number of tuples */
     236           0 :     memcpy(n, begin, sizeof(int));
     237           0 :     ptr = begin + sizeof(int);
     238             : 
     239           0 :     tuples = palloc(*n * sizeof(IndexTuple));
     240             : 
     241           0 :     for (i = 0; i < *n; i++)
     242             :     {
     243             :         Assert(ptr - begin < len);
     244           0 :         tuples[i] = (IndexTuple) ptr;
     245           0 :         ptr += IndexTupleSize((IndexTuple) ptr);
     246             :     }
     247             :     Assert(ptr - begin == len);
     248             : 
     249           0 :     return tuples;
     250             : }
     251             : 
     252             : static void
     253           0 : gistRedoPageSplitRecord(XLogReaderState *record)
     254             : {
     255           0 :     XLogRecPtr  lsn = record->EndRecPtr;
     256           0 :     gistxlogPageSplit *xldata = (gistxlogPageSplit *) XLogRecGetData(record);
     257           0 :     Buffer      firstbuffer = InvalidBuffer;
     258             :     Buffer      buffer;
     259             :     Page        page;
     260             :     int         i;
     261           0 :     bool        isrootsplit = false;
     262             : 
     263             :     /*
     264             :      * We must hold lock on the first-listed page throughout the action,
     265             :      * including while updating the left child page (if any).  We can unlock
     266             :      * remaining pages in the list as soon as they've been written, because
     267             :      * there is no path for concurrent queries to reach those pages without
     268             :      * first visiting the first-listed page.
     269             :      */
     270             : 
     271             :     /* loop around all pages */
     272           0 :     for (i = 0; i < xldata->npage; i++)
     273             :     {
     274             :         int         flags;
     275             :         char       *data;
     276             :         Size        datalen;
     277             :         int         num;
     278             :         BlockNumber blkno;
     279             :         IndexTuple *tuples;
     280             : 
     281           0 :         XLogRecGetBlockTag(record, i + 1, NULL, NULL, &blkno);
     282           0 :         if (blkno == GIST_ROOT_BLKNO)
     283             :         {
     284             :             Assert(i == 0);
     285           0 :             isrootsplit = true;
     286             :         }
     287             : 
     288           0 :         buffer = XLogInitBufferForRedo(record, i + 1);
     289           0 :         page = (Page) BufferGetPage(buffer);
     290           0 :         data = XLogRecGetBlockData(record, i + 1, &datalen);
     291             : 
     292           0 :         tuples = decodePageSplitRecord(data, datalen, &num);
     293             : 
     294             :         /* ok, clear buffer */
     295           0 :         if (xldata->origleaf && blkno != GIST_ROOT_BLKNO)
     296           0 :             flags = F_LEAF;
     297             :         else
     298           0 :             flags = 0;
     299           0 :         GISTInitBuffer(buffer, flags);
     300             : 
     301             :         /* and fill it */
     302           0 :         gistfillbuffer(page, tuples, num, FirstOffsetNumber);
     303             : 
     304           0 :         if (blkno == GIST_ROOT_BLKNO)
     305             :         {
     306           0 :             GistPageGetOpaque(page)->rightlink = InvalidBlockNumber;
     307           0 :             GistPageSetNSN(page, xldata->orignsn);
     308           0 :             GistClearFollowRight(page);
     309             :         }
     310             :         else
     311             :         {
     312           0 :             if (i < xldata->npage - 1)
     313             :             {
     314             :                 BlockNumber nextblkno;
     315             : 
     316           0 :                 XLogRecGetBlockTag(record, i + 2, NULL, NULL, &nextblkno);
     317           0 :                 GistPageGetOpaque(page)->rightlink = nextblkno;
     318             :             }
     319             :             else
     320           0 :                 GistPageGetOpaque(page)->rightlink = xldata->origrlink;
     321           0 :             GistPageSetNSN(page, xldata->orignsn);
     322           0 :             if (i < xldata->npage - 1 && !isrootsplit &&
     323           0 :                 xldata->markfollowright)
     324           0 :                 GistMarkFollowRight(page);
     325             :             else
     326           0 :                 GistClearFollowRight(page);
     327             :         }
     328             : 
     329           0 :         PageSetLSN(page, lsn);
     330           0 :         MarkBufferDirty(buffer);
     331             : 
     332           0 :         if (i == 0)
     333           0 :             firstbuffer = buffer;
     334             :         else
     335           0 :             UnlockReleaseBuffer(buffer);
     336             :     }
     337             : 
     338             :     /* Fix follow-right data on left child page, if any */
     339           0 :     if (XLogRecHasBlockRef(record, 0))
     340           0 :         gistRedoClearFollowRight(record, 0);
     341             : 
     342             :     /* Finally, release lock on the first page */
     343           0 :     UnlockReleaseBuffer(firstbuffer);
     344           0 : }
     345             : 
     346             : /* redo page deletion */
     347             : static void
     348           0 : gistRedoPageDelete(XLogReaderState *record)
     349             : {
     350           0 :     XLogRecPtr  lsn = record->EndRecPtr;
     351           0 :     gistxlogPageDelete *xldata = (gistxlogPageDelete *) XLogRecGetData(record);
     352             :     Buffer      parentBuffer;
     353             :     Buffer      leafBuffer;
     354             : 
     355           0 :     if (XLogReadBufferForRedo(record, 0, &leafBuffer) == BLK_NEEDS_REDO)
     356             :     {
     357           0 :         Page        page = (Page) BufferGetPage(leafBuffer);
     358             : 
     359           0 :         GistPageSetDeleted(page, xldata->deleteXid);
     360             : 
     361           0 :         PageSetLSN(page, lsn);
     362           0 :         MarkBufferDirty(leafBuffer);
     363             :     }
     364             : 
     365           0 :     if (XLogReadBufferForRedo(record, 1, &parentBuffer) == BLK_NEEDS_REDO)
     366             :     {
     367           0 :         Page        page = (Page) BufferGetPage(parentBuffer);
     368             : 
     369           0 :         PageIndexTupleDelete(page, xldata->downlinkOffset);
     370             : 
     371           0 :         PageSetLSN(page, lsn);
     372           0 :         MarkBufferDirty(parentBuffer);
     373             :     }
     374             : 
     375           0 :     if (BufferIsValid(parentBuffer))
     376           0 :         UnlockReleaseBuffer(parentBuffer);
     377           0 :     if (BufferIsValid(leafBuffer))
     378           0 :         UnlockReleaseBuffer(leafBuffer);
     379           0 : }
     380             : 
     381             : static void
     382           0 : gistRedoPageReuse(XLogReaderState *record)
     383             : {
     384           0 :     gistxlogPageReuse *xlrec = (gistxlogPageReuse *) XLogRecGetData(record);
     385             : 
     386             :     /*
     387             :      * PAGE_REUSE records exist to provide a conflict point when we reuse
     388             :      * pages in the index via the FSM.  That's all they do though.
     389             :      *
     390             :      * latestRemovedXid was the page's deleteXid.  The deleteXid <
     391             :      * RecentGlobalXmin test in gistPageRecyclable() conceptually mirrors the
     392             :      * pgxact->xmin > limitXmin test in GetConflictingVirtualXIDs().
     393             :      * Consequently, one XID value achieves the same exclusion effect on
     394             :      * master and standby.
     395             :      */
     396           0 :     if (InHotStandby)
     397             :     {
     398           0 :         FullTransactionId latestRemovedFullXid = xlrec->latestRemovedFullXid;
     399           0 :         FullTransactionId nextFullXid = ReadNextFullTransactionId();
     400             :         uint64      diff;
     401             : 
     402             :         /*
     403             :          * ResolveRecoveryConflictWithSnapshot operates on 32-bit
     404             :          * TransactionIds, so truncate the logged FullTransactionId. If the
     405             :          * logged value is very old, so that XID wrap-around already happened
     406             :          * on it, there can't be any snapshots that still see it.
     407             :          */
     408           0 :         nextFullXid = ReadNextFullTransactionId();
     409           0 :         diff = U64FromFullTransactionId(nextFullXid) -
     410           0 :             U64FromFullTransactionId(latestRemovedFullXid);
     411           0 :         if (diff < MaxTransactionId / 2)
     412             :         {
     413             :             TransactionId latestRemovedXid;
     414             : 
     415           0 :             latestRemovedXid = XidFromFullTransactionId(latestRemovedFullXid);
     416           0 :             ResolveRecoveryConflictWithSnapshot(latestRemovedXid,
     417             :                                                 xlrec->node);
     418             :         }
     419             :     }
     420           0 : }
     421             : 
     422             : void
     423           0 : gist_redo(XLogReaderState *record)
     424             : {
     425           0 :     uint8       info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
     426             :     MemoryContext oldCxt;
     427             : 
     428             :     /*
     429             :      * GiST indexes do not require any conflict processing. NB: If we ever
     430             :      * implement a similar optimization we have in b-tree, and remove killed
     431             :      * tuples outside VACUUM, we'll need to handle that here.
     432             :      */
     433             : 
     434           0 :     oldCxt = MemoryContextSwitchTo(opCtx);
     435           0 :     switch (info)
     436             :     {
     437             :         case XLOG_GIST_PAGE_UPDATE:
     438           0 :             gistRedoPageUpdateRecord(record);
     439           0 :             break;
     440             :         case XLOG_GIST_DELETE:
     441           0 :             gistRedoDeleteRecord(record);
     442           0 :             break;
     443             :         case XLOG_GIST_PAGE_REUSE:
     444           0 :             gistRedoPageReuse(record);
     445           0 :             break;
     446             :         case XLOG_GIST_PAGE_SPLIT:
     447           0 :             gistRedoPageSplitRecord(record);
     448           0 :             break;
     449             :         case XLOG_GIST_PAGE_DELETE:
     450           0 :             gistRedoPageDelete(record);
     451           0 :             break;
     452             :         default:
     453           0 :             elog(PANIC, "gist_redo: unknown op code %u", info);
     454             :     }
     455             : 
     456           0 :     MemoryContextSwitchTo(oldCxt);
     457           0 :     MemoryContextReset(opCtx);
     458           0 : }
     459             : 
     460             : void
     461         116 : gist_xlog_startup(void)
     462             : {
     463         116 :     opCtx = createTempGistContext();
     464         116 : }
     465             : 
     466             : void
     467          86 : gist_xlog_cleanup(void)
     468             : {
     469          86 :     MemoryContextDelete(opCtx);
     470          86 : }
     471             : 
     472             : /*
     473             :  * Mask a Gist page before running consistency checks on it.
     474             :  */
     475             : void
     476           0 : gist_mask(char *pagedata, BlockNumber blkno)
     477             : {
     478           0 :     Page        page = (Page) pagedata;
     479             : 
     480           0 :     mask_page_lsn_and_checksum(page);
     481             : 
     482           0 :     mask_page_hint_bits(page);
     483           0 :     mask_unused_space(page);
     484             : 
     485             :     /*
     486             :      * NSN is nothing but a special purpose LSN. Hence, mask it for the same
     487             :      * reason as mask_page_lsn_and_checksum.
     488             :      */
     489           0 :     GistPageSetNSN(page, (uint64) MASK_MARKER);
     490             : 
     491             :     /*
     492             :      * We update F_FOLLOW_RIGHT flag on the left child after writing WAL
     493             :      * record. Hence, mask this flag. See gistplacetopage() for details.
     494             :      */
     495           0 :     GistMarkFollowRight(page);
     496             : 
     497           0 :     if (GistPageIsLeaf(page))
     498             :     {
     499             :         /*
     500             :          * In gist leaf pages, it is possible to modify the LP_FLAGS without
     501             :          * emitting any WAL record. Hence, mask the line pointer flags. See
     502             :          * gistkillitems() for details.
     503             :          */
     504           0 :         mask_lp_flags(page);
     505             :     }
     506             : 
     507             :     /*
     508             :      * During gist redo, we never mark a page as garbage. Hence, mask it to
     509             :      * ignore any differences.
     510             :      */
     511           0 :     GistClearPageHasGarbage(page);
     512           0 : }
     513             : 
     514             : /*
     515             :  * Write WAL record of a page split.
     516             :  */
     517             : XLogRecPtr
     518        2512 : gistXLogSplit(bool page_is_leaf,
     519             :               SplitedPageLayout *dist,
     520             :               BlockNumber origrlink, GistNSN orignsn,
     521             :               Buffer leftchildbuf, bool markfollowright)
     522             : {
     523             :     gistxlogPageSplit xlrec;
     524             :     SplitedPageLayout *ptr;
     525        2512 :     int         npage = 0;
     526             :     XLogRecPtr  recptr;
     527             :     int         i;
     528             : 
     529        7638 :     for (ptr = dist; ptr; ptr = ptr->next)
     530        5126 :         npage++;
     531             : 
     532        2512 :     xlrec.origrlink = origrlink;
     533        2512 :     xlrec.orignsn = orignsn;
     534        2512 :     xlrec.origleaf = page_is_leaf;
     535        2512 :     xlrec.npage = (uint16) npage;
     536        2512 :     xlrec.markfollowright = markfollowright;
     537             : 
     538        2512 :     XLogBeginInsert();
     539             : 
     540             :     /*
     541             :      * Include a full page image of the child buf. (only necessary if a
     542             :      * checkpoint happened since the child page was split)
     543             :      */
     544        2512 :     if (BufferIsValid(leftchildbuf))
     545           8 :         XLogRegisterBuffer(0, leftchildbuf, REGBUF_STANDARD);
     546             : 
     547             :     /*
     548             :      * NOTE: We register a lot of data. The caller must've called
     549             :      * XLogEnsureRecordSpace() to prepare for that. We cannot do it here,
     550             :      * because we're already in a critical section. If you change the number
     551             :      * of buffer or data registrations here, make sure you modify the
     552             :      * XLogEnsureRecordSpace() calls accordingly!
     553             :      */
     554        2512 :     XLogRegisterData((char *) &xlrec, sizeof(gistxlogPageSplit));
     555             : 
     556        2512 :     i = 1;
     557        7638 :     for (ptr = dist; ptr; ptr = ptr->next)
     558             :     {
     559        5126 :         XLogRegisterBuffer(i, ptr->buffer, REGBUF_WILL_INIT);
     560        5126 :         XLogRegisterBufData(i, (char *) &(ptr->block.num), sizeof(int));
     561        5126 :         XLogRegisterBufData(i, (char *) ptr->list, ptr->lenlist);
     562        5126 :         i++;
     563             :     }
     564             : 
     565        2512 :     recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_SPLIT);
     566             : 
     567        2512 :     return recptr;
     568             : }
     569             : 
     570             : /*
     571             :  * Write XLOG record describing a page deletion. This also includes removal of
     572             :  * downlink from the parent page.
     573             :  */
     574             : XLogRecPtr
     575         526 : gistXLogPageDelete(Buffer buffer, FullTransactionId xid,
     576             :                    Buffer parentBuffer, OffsetNumber downlinkOffset)
     577             : {
     578             :     gistxlogPageDelete xlrec;
     579             :     XLogRecPtr  recptr;
     580             : 
     581         526 :     xlrec.deleteXid = xid;
     582         526 :     xlrec.downlinkOffset = downlinkOffset;
     583             : 
     584         526 :     XLogBeginInsert();
     585         526 :     XLogRegisterData((char *) &xlrec, SizeOfGistxlogPageDelete);
     586             : 
     587         526 :     XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
     588         526 :     XLogRegisterBuffer(1, parentBuffer, REGBUF_STANDARD);
     589             : 
     590         526 :     recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_DELETE);
     591             : 
     592         526 :     return recptr;
     593             : }
     594             : 
     595             : /*
     596             :  * Write XLOG record about reuse of a deleted page.
     597             :  */
     598             : void
     599           0 : gistXLogPageReuse(Relation rel, BlockNumber blkno, FullTransactionId latestRemovedXid)
     600             : {
     601             :     gistxlogPageReuse xlrec_reuse;
     602             : 
     603             :     /*
     604             :      * Note that we don't register the buffer with the record, because this
     605             :      * operation doesn't modify the page. This record only exists to provide a
     606             :      * conflict point for Hot Standby.
     607             :      */
     608             : 
     609             :     /* XLOG stuff */
     610           0 :     xlrec_reuse.node = rel->rd_node;
     611           0 :     xlrec_reuse.block = blkno;
     612           0 :     xlrec_reuse.latestRemovedFullXid = latestRemovedXid;
     613             : 
     614           0 :     XLogBeginInsert();
     615           0 :     XLogRegisterData((char *) &xlrec_reuse, SizeOfGistxlogPageReuse);
     616             : 
     617           0 :     XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_REUSE);
     618           0 : }
     619             : 
     620             : /*
     621             :  * Write XLOG record describing a page update. The update can include any
     622             :  * number of deletions and/or insertions of tuples on a single index page.
     623             :  *
     624             :  * If this update inserts a downlink for a split page, also record that
     625             :  * the F_FOLLOW_RIGHT flag on the child page is cleared and NSN set.
     626             :  *
     627             :  * Note that both the todelete array and the tuples are marked as belonging
     628             :  * to the target buffer; they need not be stored in XLOG if XLogInsert decides
     629             :  * to log the whole buffer contents instead.
     630             :  */
     631             : XLogRecPtr
     632      372018 : gistXLogUpdate(Buffer buffer,
     633             :                OffsetNumber *todelete, int ntodelete,
     634             :                IndexTuple *itup, int ituplen,
     635             :                Buffer leftchildbuf)
     636             : {
     637             :     gistxlogPageUpdate xlrec;
     638             :     int         i;
     639             :     XLogRecPtr  recptr;
     640             : 
     641      372018 :     xlrec.ntodelete = ntodelete;
     642      372018 :     xlrec.ntoinsert = ituplen;
     643             : 
     644      372018 :     XLogBeginInsert();
     645      372018 :     XLogRegisterData((char *) &xlrec, sizeof(gistxlogPageUpdate));
     646             : 
     647      372018 :     XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
     648      372018 :     XLogRegisterBufData(0, (char *) todelete, sizeof(OffsetNumber) * ntodelete);
     649             : 
     650             :     /* new tuples */
     651      745350 :     for (i = 0; i < ituplen; i++)
     652      373332 :         XLogRegisterBufData(0, (char *) (itup[i]), IndexTupleSize(itup[i]));
     653             : 
     654             :     /*
     655             :      * Include a full page image of the child buf. (only necessary if a
     656             :      * checkpoint happened since the child page was split)
     657             :      */
     658      372018 :     if (BufferIsValid(leftchildbuf))
     659        2402 :         XLogRegisterBuffer(1, leftchildbuf, REGBUF_STANDARD);
     660             : 
     661      372018 :     recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_UPDATE);
     662             : 
     663      372018 :     return recptr;
     664             : }
     665             : 
     666             : /*
     667             :  * Write XLOG record describing a delete of leaf index tuples marked as DEAD
     668             :  * during new tuple insertion.  One may think that this case is already covered
     669             :  * by gistXLogUpdate().  But deletion of index tuples might conflict with
     670             :  * standby queries and needs special handling.
     671             :  */
     672             : XLogRecPtr
     673           0 : gistXLogDelete(Buffer buffer, OffsetNumber *todelete, int ntodelete,
     674             :                TransactionId latestRemovedXid)
     675             : {
     676             :     gistxlogDelete xlrec;
     677             :     XLogRecPtr  recptr;
     678             : 
     679           0 :     xlrec.latestRemovedXid = latestRemovedXid;
     680           0 :     xlrec.ntodelete = ntodelete;
     681             : 
     682           0 :     XLogBeginInsert();
     683           0 :     XLogRegisterData((char *) &xlrec, SizeOfGistxlogDelete);
     684             : 
     685             :     /*
     686             :      * We need the target-offsets array whether or not we store the whole
     687             :      * buffer, to allow us to find the latestRemovedXid on a standby server.
     688             :      */
     689           0 :     XLogRegisterData((char *) todelete, ntodelete * sizeof(OffsetNumber));
     690             : 
     691           0 :     XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
     692             : 
     693           0 :     recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_DELETE);
     694             : 
     695           0 :     return recptr;
     696             : }

Generated by: LCOV version 1.13