LCOV - differential code coverage report
Current view: top level - src/backend/access/gist - gistxlog.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: ba12a202ce1b5581dc0ed149cf3f637d7897ad5d vs 2866d8c7dbfc9d882a7d80fef93fbbe763709932 Lines: 91.2 % 250 228 22 1 227 1
Current Date: 2026-08-27 14:31:44 +0300 Functions: 87.5 % 16 14 2 1 13
Baseline: lcov-20260827-baseline Branches: 51.3 % 150 77 73 77
Baseline Date: 2026-08-27 14:31:58 +0300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 1 1 1
(30,360] days: 50.0 % 4 2 2 2
(360..) days: 91.8 % 245 225 20 225
Function coverage date bins:
(360..) days: 87.5 % 16 14 2 1 13
Branch coverage date bins:
(30,360] days: 16.7 % 6 1 5 1
(360..) days: 52.8 % 144 76 68 76

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

Generated by: LCOV version 2.0-1