LCOV - code coverage report
Current view: top level - src/backend/access/gist - gistxlog.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 13.8 % 318 44
Test Date: 2026-01-26 10:56:24 Functions: 11.8 % 17 2
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 6.6 % 151 10

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

Generated by: LCOV version 2.3.2-1