LCOV - differential code coverage report
Current view: top level - src/backend/access/gist - gistxlog.c (source / functions) Coverage Total Hit UNC UIC UBC GBC GNC CBC EUB DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 79.1 % 253 200 3 50 6 194 11 6
Current Date: 2026-03-14 14:10:32 -0400 Functions: 75.0 % 16 12 1 3 4 8 1
Baseline: lcov-20260315-024220-baseline Branches: 49.3 % 152 75 5 4 68 2 1 72 1 5 1
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 66.7 % 9 6 3 6
(360..) days: 79.5 % 244 194 50 194
Function coverage date bins:
(360..) days: 75.0 % 16 12 1 3 4 8
Branch coverage date bins:
(30,360] days: 16.7 % 6 1 5 1
(360..) days: 50.3 % 147 74 4 68 2 72 1

 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
 4133 heikki.linnakangas@i       40                 :CBC         443 : gistRedoClearFollowRight(XLogReaderState *record, uint8 block_id)
                                 41                 :                : {
                                 42                 :            443 :     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                 :            443 :     action = XLogReadBufferForRedo(record, block_id, &buffer);
 4232                            52   [ +  +  +  - ]:            443 :     if (action == BLK_NEEDS_REDO || action == BLK_RESTORED)
                                 53                 :                :     {
 3616 kgrittn@postgresql.o       54                 :            443 :         page = BufferGetPage(buffer);
                                 55                 :                : 
 4805 heikki.linnakangas@i       56                 :            443 :         GistPageSetNSN(page, lsn);
 4871 tgl@sss.pgh.pa.us          57                 :            443 :         GistClearFollowRight(page);
                                 58                 :                : 
                                 59                 :            443 :         PageSetLSN(page, lsn);
                                 60                 :            443 :         MarkBufferDirty(buffer);
                                 61                 :                :     }
 4232 heikki.linnakangas@i       62         [ +  - ]:            443 :     if (BufferIsValid(buffer))
                                 63                 :            443 :         UnlockReleaseBuffer(buffer);
 7579 teodor@sigaev.ru           64                 :            443 : }
                                 65                 :                : 
                                 66                 :                : /*
                                 67                 :                :  * redo any page update (except page split)
                                 68                 :                :  */
                                 69                 :                : static void
 4133 heikki.linnakangas@i       70                 :          57457 : gistRedoPageUpdateRecord(XLogReaderState *record)
                                 71                 :                : {
                                 72                 :          57457 :     XLogRecPtr  lsn = record->EndRecPtr;
                                 73                 :          57457 :     gistxlogPageUpdate *xldata = (gistxlogPageUpdate *) XLogRecGetData(record);
                                 74                 :                :     Buffer      buffer;
                                 75                 :                :     Page        page;
                                 76                 :                : 
                                 77         [ +  + ]:          57457 :     if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
                                 78                 :                :     {
                                 79                 :                :         char       *begin;
                                 80                 :                :         char       *data;
                                 81                 :                :         Size        datalen;
 1272 tgl@sss.pgh.pa.us          82                 :          57095 :         int         ninserted PG_USED_FOR_ASSERTS_ONLY = 0;
                                 83                 :                : 
 4133 heikki.linnakangas@i       84                 :          57095 :         data = begin = XLogRecGetBlockData(record, 0, &datalen);
                                 85                 :                : 
  198 peter@eisentraut.org       86                 :GNC       57095 :         page = BufferGetPage(buffer);
                                 87                 :                : 
 3474 tgl@sss.pgh.pa.us          88   [ +  +  +  + ]:CBC       57095 :         if (xldata->ntodelete == 1 && xldata->ntoinsert == 1)
 4232 heikki.linnakangas@i       89                 :          20847 :         {
                                 90                 :                :             /*
                                 91                 :                :              * When replacing one tuple with one other tuple, we must use
                                 92                 :                :              * PageIndexTupleOverwrite for consistency with gistplacetopage.
                                 93                 :                :              */
 3474 tgl@sss.pgh.pa.us          94                 :          20847 :             OffsetNumber offnum = *((OffsetNumber *) data);
                                 95                 :                :             IndexTuple  itup;
                                 96                 :                :             Size        itupsize;
                                 97                 :                : 
                                 98                 :          20847 :             data += sizeof(OffsetNumber);
                                 99                 :          20847 :             itup = (IndexTuple) data;
                                100                 :          20847 :             itupsize = IndexTupleSize(itup);
  139 peter@eisentraut.org      101         [ -  + ]:GNC       20847 :             if (!PageIndexTupleOverwrite(page, offnum, itup, itupsize))
   96 peter@eisentraut.org      102         [ #  # ]:UNC           0 :                 elog(ERROR, "failed to add item to GiST index page, size %zu bytes", itupsize);
 3474 tgl@sss.pgh.pa.us         103                 :CBC       20847 :             data += itupsize;
                                104                 :                :             /* should be nothing left after consuming 1 tuple */
                                105         [ -  + ]:          20847 :             Assert(data - begin == datalen);
                                106                 :                :             /* update insertion count for assert check below */
                                107                 :          20847 :             ninserted++;
                                108                 :                :         }
                                109         [ +  + ]:          36248 :         else if (xldata->ntodelete > 0)
                                110                 :                :         {
                                111                 :                :             /* Otherwise, delete old tuples if any */
 4232 heikki.linnakangas@i      112                 :            439 :             OffsetNumber *todelete = (OffsetNumber *) data;
                                113                 :                : 
                                114                 :            439 :             data += sizeof(OffsetNumber) * xldata->ntodelete;
                                115                 :                : 
 3832 teodor@sigaev.ru          116                 :            439 :             PageIndexMultiDelete(page, todelete, xldata->ntodelete);
 4232 heikki.linnakangas@i      117         [ +  + ]:            439 :             if (GistPageIsLeaf(page))
                                118                 :              2 :                 GistMarkTuplesDeleted(page);
                                119                 :                :         }
                                120                 :                : 
                                121                 :                :         /* Add new tuples if any */
 4133                           122         [ +  + ]:          57095 :         if (data - begin < datalen)
                                123                 :                :         {
 4232                           124         [ +  + ]:          36246 :             OffsetNumber off = (PageIsEmpty(page)) ? FirstOffsetNumber :
 1031 tgl@sss.pgh.pa.us         125                 :          36181 :                 OffsetNumberNext(PageGetMaxOffsetNumber(page));
                                126                 :                : 
 4133 heikki.linnakangas@i      127         [ +  + ]:          72929 :             while (data - begin < datalen)
                                128                 :                :             {
 4232                           129                 :          36683 :                 IndexTuple  itup = (IndexTuple) data;
                                130                 :          36683 :                 Size        sz = IndexTupleSize(itup);
                                131                 :                :                 OffsetNumber l;
                                132                 :                : 
                                133                 :          36683 :                 data += sz;
                                134                 :                : 
  139 peter@eisentraut.org      135                 :GNC       36683 :                 l = PageAddItem(page, itup, sz, off, false, false);
 4232 heikki.linnakangas@i      136         [ -  + ]:CBC       36683 :                 if (l == InvalidOffsetNumber)
   96 peter@eisentraut.org      137         [ #  # ]:UNC           0 :                     elog(ERROR, "failed to add item to GiST index page, size %zu bytes", sz);
 4232 heikki.linnakangas@i      138                 :CBC       36683 :                 off++;
 4133                           139                 :          36683 :                 ninserted++;
                                140                 :                :             }
                                141                 :                :         }
                                142                 :                : 
                                143                 :                :         /* Check that XLOG record contained expected number of tuples */
                                144         [ -  + ]:          57095 :         Assert(ninserted == xldata->ntoinsert);
                                145                 :                : 
 4232                           146                 :          57095 :         PageSetLSN(page, lsn);
                                147                 :          57095 :         MarkBufferDirty(buffer);
                                148                 :                :     }
                                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                 :                :      */
 4133                           157   [ +  +  +  - ]:          57457 :     if (XLogRecHasBlockRef(record, 1))
                                158                 :            441 :         gistRedoClearFollowRight(record, 1);
                                159                 :                : 
 4232                           160         [ +  - ]:          57457 :     if (BufferIsValid(buffer))
                                161                 :          57457 :         UnlockReleaseBuffer(buffer);
 7579 teodor@sigaev.ru          162                 :          57457 : }
                                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
 2641 akorotkov@postgresql      170                 :UBC           0 : gistRedoDeleteRecord(XLogReaderState *record)
                                171                 :                : {
                                172                 :              0 :     XLogRecPtr  lsn = record->EndRecPtr;
                                173                 :              0 :     gistxlogDelete *xldata = (gistxlogDelete *) XLogRecGetData(record);
                                174                 :                :     Buffer      buffer;
                                175                 :                :     Page        page;
 1078 andres@anarazel.de        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                 :                :      */
 2641 akorotkov@postgresql      189         [ #  # ]:              0 :     if (InHotStandby)
                                190                 :                :     {
                                191                 :                :         RelFileLocator rlocator;
                                192                 :                : 
 1348 rhaas@postgresql.org      193                 :              0 :         XLogRecGetBlockTag(record, 0, &rlocator, NULL, NULL);
                                194                 :                : 
 1214 pg@bowt.ie                195                 :              0 :         ResolveRecoveryConflictWithSnapshot(xldata->snapshotConflictHorizon,
 1073 andres@anarazel.de        196                 :              0 :                                             xldata->isCatalogRel,
                                197                 :                :                                             rlocator);
                                198                 :                :     }
                                199                 :                : 
 2641 akorotkov@postgresql      200         [ #  # ]:              0 :     if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
                                201                 :                :     {
  198 peter@eisentraut.org      202                 :UNC           0 :         page = BufferGetPage(buffer);
                                203                 :                : 
 1078 andres@anarazel.de        204                 :UBC           0 :         PageIndexMultiDelete(page, toDelete, xldata->ntodelete);
                                205                 :                : 
 2641 akorotkov@postgresql      206                 :              0 :         GistClearPageHasGarbage(page);
                                207                 :              0 :         GistMarkTuplesDeleted(page);
                                208                 :                : 
                                209                 :              0 :         PageSetLSN(page, lsn);
                                210                 :              0 :         MarkBufferDirty(buffer);
                                211                 :                :     }
                                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 *
 4133 heikki.linnakangas@i      221                 :CBC         901 : decodePageSplitRecord(char *begin, int len, int *n)
                                222                 :                : {
                                223                 :                :     char       *ptr;
                                224                 :            901 :     int         i = 0;
                                225                 :                :     IndexTuple *tuples;
                                226                 :                : 
                                227                 :                :     /* extract the number of tuples */
                                228                 :            901 :     memcpy(n, begin, sizeof(int));
                                229                 :            901 :     ptr = begin + sizeof(int);
                                230                 :                : 
                                231                 :            901 :     tuples = palloc(*n * sizeof(IndexTuple));
                                232                 :                : 
                                233         [ +  + ]:          78225 :     for (i = 0; i < *n; i++)
                                234                 :                :     {
                                235         [ -  + ]:          77324 :         Assert(ptr - begin < len);
                                236                 :          77324 :         tuples[i] = (IndexTuple) ptr;
                                237                 :          77324 :         ptr += IndexTupleSize((IndexTuple) ptr);
                                238                 :                :     }
                                239         [ -  + ]:            901 :     Assert(ptr - begin == len);
                                240                 :                : 
                                241                 :            901 :     return tuples;
                                242                 :                : }
                                243                 :                : 
                                244                 :                : static void
                                245                 :            448 : gistRedoPageSplitRecord(XLogReaderState *record)
                                246                 :                : {
                                247                 :            448 :     XLogRecPtr  lsn = record->EndRecPtr;
 5561                           248                 :            448 :     gistxlogPageSplit *xldata = (gistxlogPageSplit *) XLogRecGetData(record);
 4871 tgl@sss.pgh.pa.us         249                 :            448 :     Buffer      firstbuffer = InvalidBuffer;
                                250                 :                :     Buffer      buffer;
                                251                 :                :     Page        page;
                                252                 :                :     int         i;
 5561 heikki.linnakangas@i      253                 :            448 :     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 */
 4133                           264         [ +  + ]:           1349 :     for (i = 0; i < xldata->npage; i++)
                                265                 :                :     {
                                266                 :                :         int         flags;
                                267                 :                :         char       *data;
                                268                 :                :         Size        datalen;
                                269                 :                :         int         num;
                                270                 :                :         BlockNumber blkno;
                                271                 :                :         IndexTuple *tuples;
                                272                 :                : 
                                273                 :            901 :         XLogRecGetBlockTag(record, i + 1, NULL, NULL, &blkno);
                                274         [ +  + ]:            901 :         if (blkno == GIST_ROOT_BLKNO)
                                275                 :                :         {
 5561                           276         [ -  + ]:              5 :             Assert(i == 0);
                                277                 :              5 :             isrootsplit = true;
                                278                 :                :         }
                                279                 :                : 
 4133                           280                 :            901 :         buffer = XLogInitBufferForRedo(record, i + 1);
  198 peter@eisentraut.org      281                 :GNC         901 :         page = BufferGetPage(buffer);
 4133 heikki.linnakangas@i      282                 :CBC         901 :         data = XLogRecGetBlockData(record, i + 1, &datalen);
                                283                 :                : 
                                284                 :            901 :         tuples = decodePageSplitRecord(data, datalen, &num);
                                285                 :                : 
                                286                 :                :         /* ok, clear buffer */
                                287   [ +  +  +  + ]:            901 :         if (xldata->origleaf && blkno != GIST_ROOT_BLKNO)
 5561                           288                 :            892 :             flags = F_LEAF;
                                289                 :                :         else
                                290                 :              9 :             flags = 0;
 7479 bruce@momjian.us          291                 :            901 :         GISTInitBuffer(buffer, flags);
                                292                 :                : 
                                293                 :                :         /* and fill it */
 4133 heikki.linnakangas@i      294                 :            901 :         gistfillbuffer(page, tuples, num, FirstOffsetNumber);
                                295                 :                : 
                                296         [ +  + ]:            901 :         if (blkno == GIST_ROOT_BLKNO)
                                297                 :                :         {
 5561                           298                 :              5 :             GistPageGetOpaque(page)->rightlink = InvalidBlockNumber;
 4805                           299                 :              5 :             GistPageSetNSN(page, xldata->orignsn);
 5561                           300                 :              5 :             GistClearFollowRight(page);
                                301                 :                :         }
                                302                 :                :         else
                                303                 :                :         {
 4133                           304         [ +  + ]:            896 :             if (i < xldata->npage - 1)
                                305                 :                :             {
                                306                 :                :                 BlockNumber nextblkno;
                                307                 :                : 
                                308                 :            448 :                 XLogRecGetBlockTag(record, i + 2, NULL, NULL, &nextblkno);
                                309                 :            448 :                 GistPageGetOpaque(page)->rightlink = nextblkno;
                                310                 :                :             }
                                311                 :                :             else
 5561                           312                 :            448 :                 GistPageGetOpaque(page)->rightlink = xldata->origrlink;
 4805                           313                 :            896 :             GistPageSetNSN(page, xldata->orignsn);
 4133                           314   [ +  +  +  + ]:            896 :             if (i < xldata->npage - 1 && !isrootsplit &&
 5302                           315         [ +  - ]:            443 :                 xldata->markfollowright)
 5561                           316                 :            443 :                 GistMarkFollowRight(page);
                                317                 :                :             else
                                318                 :            453 :                 GistClearFollowRight(page);
                                319                 :                :         }
                                320                 :                : 
 7573 teodor@sigaev.ru          321                 :            901 :         PageSetLSN(page, lsn);
 7289 tgl@sss.pgh.pa.us         322                 :            901 :         MarkBufferDirty(buffer);
                                323                 :                : 
 4871                           324         [ +  + ]:            901 :         if (i == 0)
                                325                 :            448 :             firstbuffer = buffer;
                                326                 :                :         else
                                327                 :            453 :             UnlockReleaseBuffer(buffer);
                                328                 :                :     }
                                329                 :                : 
                                330                 :                :     /* Fix follow-right data on left child page, if any */
 4133 heikki.linnakangas@i      331   [ +  -  +  + ]:            448 :     if (XLogRecHasBlockRef(record, 0))
                                332                 :              2 :         gistRedoClearFollowRight(record, 0);
                                333                 :                : 
                                334                 :                :     /* Finally, release lock on the first page */
 4871 tgl@sss.pgh.pa.us         335                 :            448 :     UnlockReleaseBuffer(firstbuffer);
 7579 teodor@sigaev.ru          336                 :            448 : }
                                337                 :                : 
                                338                 :                : /* redo page deletion */
                                339                 :                : static void
 2550 heikki.linnakangas@i      340                 :             81 : gistRedoPageDelete(XLogReaderState *record)
                                341                 :                : {
                                342                 :             81 :     XLogRecPtr  lsn = record->EndRecPtr;
                                343                 :             81 :     gistxlogPageDelete *xldata = (gistxlogPageDelete *) XLogRecGetData(record);
                                344                 :                :     Buffer      parentBuffer;
                                345                 :                :     Buffer      leafBuffer;
                                346                 :                : 
                                347         [ +  - ]:             81 :     if (XLogReadBufferForRedo(record, 0, &leafBuffer) == BLK_NEEDS_REDO)
                                348                 :                :     {
  198 peter@eisentraut.org      349                 :GNC          81 :         Page        page = BufferGetPage(leafBuffer);
                                350                 :                : 
 2426 heikki.linnakangas@i      351                 :CBC          81 :         GistPageSetDeleted(page, xldata->deleteXid);
                                352                 :                : 
 2550                           353                 :             81 :         PageSetLSN(page, lsn);
                                354                 :             81 :         MarkBufferDirty(leafBuffer);
                                355                 :                :     }
                                356                 :                : 
                                357         [ +  + ]:             81 :     if (XLogReadBufferForRedo(record, 1, &parentBuffer) == BLK_NEEDS_REDO)
                                358                 :                :     {
  198 peter@eisentraut.org      359                 :GNC          80 :         Page        page = BufferGetPage(parentBuffer);
                                360                 :                : 
 2550 heikki.linnakangas@i      361                 :CBC          80 :         PageIndexTupleDelete(page, xldata->downlinkOffset);
                                362                 :                : 
                                363                 :             80 :         PageSetLSN(page, lsn);
                                364                 :             80 :         MarkBufferDirty(parentBuffer);
                                365                 :                :     }
                                366                 :                : 
                                367         [ +  - ]:             81 :     if (BufferIsValid(parentBuffer))
                                368                 :             81 :         UnlockReleaseBuffer(parentBuffer);
                                369         [ +  - ]:             81 :     if (BufferIsValid(leafBuffer))
                                370                 :             81 :         UnlockReleaseBuffer(leafBuffer);
                                371                 :             81 : }
                                372                 :                : 
                                373                 :                : static void
 2550 heikki.linnakangas@i      374                 :UBC           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)
 1214 pg@bowt.ie                389                 :              0 :         ResolveRecoveryConflictWithSnapshotFullXid(xlrec->snapshotConflictHorizon,
 1073 andres@anarazel.de        390                 :              0 :                                                    xlrec->isCatalogRel,
                                391                 :                :                                                    xlrec->locator);
 2550 heikki.linnakangas@i      392                 :              0 : }
                                393                 :                : 
                                394                 :                : void
 4133 heikki.linnakangas@i      395                 :CBC       57986 : gist_redo(XLogReaderState *record)
                                396                 :                : {
                                397                 :          57986 :     uint8       info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
                                398                 :                :     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                 :                : 
 7579 teodor@sigaev.ru          406                 :          57986 :     oldCxt = MemoryContextSwitchTo(opCtx);
 7479 bruce@momjian.us          407   [ +  -  -  +  :          57986 :     switch (info)
                                           +  -  - ]
                                408                 :                :     {
 7290 tgl@sss.pgh.pa.us         409                 :          57457 :         case XLOG_GIST_PAGE_UPDATE:
 4133 heikki.linnakangas@i      410                 :          57457 :             gistRedoPageUpdateRecord(record);
 7579 teodor@sigaev.ru          411                 :          57457 :             break;
 2641 akorotkov@postgresql      412                 :UBC           0 :         case XLOG_GIST_DELETE:
                                413                 :              0 :             gistRedoDeleteRecord(record);
                                414                 :              0 :             break;
 2550 heikki.linnakangas@i      415                 :              0 :         case XLOG_GIST_PAGE_REUSE:
                                416                 :              0 :             gistRedoPageReuse(record);
                                417                 :              0 :             break;
 7479 bruce@momjian.us          418                 :CBC         448 :         case XLOG_GIST_PAGE_SPLIT:
 4133 heikki.linnakangas@i      419                 :            448 :             gistRedoPageSplitRecord(record);
 7579 teodor@sigaev.ru          420                 :            448 :             break;
 2550 heikki.linnakangas@i      421                 :             81 :         case XLOG_GIST_PAGE_DELETE:
                                422                 :             81 :             gistRedoPageDelete(record);
                                423                 :             81 :             break;
 7579 teodor@sigaev.ru          424                 :UBC           0 :         default:
                                425         [ #  # ]:              0 :             elog(PANIC, "gist_redo: unknown op code %u", info);
                                426                 :                :     }
                                427                 :                : 
 7579 teodor@sigaev.ru          428                 :CBC       57986 :     MemoryContextSwitchTo(oldCxt);
                                429                 :          57986 :     MemoryContextReset(opCtx);
                                430                 :          57986 : }
                                431                 :                : 
                                432                 :                : void
 7479 bruce@momjian.us          433                 :            215 : gist_xlog_startup(void)
                                434                 :                : {
 7579 teodor@sigaev.ru          435                 :            215 :     opCtx = createTempGistContext();
                                436                 :            215 : }
                                437                 :                : 
                                438                 :                : void
 7479 bruce@momjian.us          439                 :            155 : gist_xlog_cleanup(void)
                                440                 :                : {
 7579 teodor@sigaev.ru          441                 :            155 :     MemoryContextDelete(opCtx);
 7160 tgl@sss.pgh.pa.us         442                 :            155 : }
                                443                 :                : 
                                444                 :                : /*
                                445                 :                :  * Mask a Gist page before running consistency checks on it.
                                446                 :                :  */
                                447                 :                : void
 3322 rhaas@postgresql.org      448                 :         117196 : gist_mask(char *pagedata, BlockNumber blkno)
                                449                 :                : {
                                450                 :         117196 :     Page        page = (Page) pagedata;
                                451                 :                : 
 3096                           452                 :         117196 :     mask_page_lsn_and_checksum(page);
                                453                 :                : 
 3322                           454                 :         117196 :     mask_page_hint_bits(page);
                                455                 :         117196 :     mask_unused_space(page);
                                456                 :                : 
                                457                 :                :     /*
                                458                 :                :      * NSN is nothing but a special purpose LSN. Hence, mask it for the same
                                459                 :                :      * reason as mask_page_lsn_and_checksum.
                                460                 :                :      */
                                461                 :         117196 :     GistPageSetNSN(page, (uint64) MASK_MARKER);
                                462                 :                : 
                                463                 :                :     /*
                                464                 :                :      * We update F_FOLLOW_RIGHT flag on the left child after writing WAL
                                465                 :                :      * record. Hence, mask this flag. See gistplacetopage() for details.
                                466                 :                :      */
                                467                 :         117196 :     GistMarkFollowRight(page);
                                468                 :                : 
                                469         [ +  + ]:         117196 :     if (GistPageIsLeaf(page))
                                470                 :                :     {
                                471                 :                :         /*
                                472                 :                :          * In gist leaf pages, it is possible to modify the LP_FLAGS without
                                473                 :                :          * emitting any WAL record. Hence, mask the line pointer flags. See
                                474                 :                :          * gistkillitems() for details.
                                475                 :                :          */
                                476                 :          74450 :         mask_lp_flags(page);
                                477                 :                :     }
                                478                 :                : 
                                479                 :                :     /*
                                480                 :                :      * During gist redo, we never mark a page as garbage. Hence, mask it to
                                481                 :                :      * ignore any differences.
                                482                 :                :      */
                                483                 :         117196 :     GistClearPageHasGarbage(page);
                                484                 :         117196 : }
                                485                 :                : 
                                486                 :                : /*
                                487                 :                :  * Write WAL record of a page split.
                                488                 :                :  */
                                489                 :                : XLogRecPtr
 3547 alvherre@alvh.no-ip.      490                 :           1735 : gistXLogSplit(bool page_is_leaf,
                                491                 :                :               SplitPageLayout *dist,
                                492                 :                :               BlockNumber origrlink, GistNSN orignsn,
                                493                 :                :               Buffer leftchildbuf, bool markfollowright)
                                494                 :                : {
                                495                 :                :     gistxlogPageSplit xlrec;
                                496                 :                :     SplitPageLayout *ptr;
 4133 heikki.linnakangas@i      497                 :           1735 :     int         npage = 0;
                                498                 :                :     XLogRecPtr  recptr;
                                499                 :                :     int         i;
                                500                 :                : 
 5561                           501         [ +  + ]:           5261 :     for (ptr = dist; ptr; ptr = ptr->next)
 7573 teodor@sigaev.ru          502                 :           3526 :         npage++;
                                503                 :                : 
 5561 heikki.linnakangas@i      504                 :           1735 :     xlrec.origrlink = origrlink;
                                505                 :           1735 :     xlrec.orignsn = orignsn;
                                506                 :           1735 :     xlrec.origleaf = page_is_leaf;
                                507                 :           1735 :     xlrec.npage = (uint16) npage;
 5302                           508                 :           1735 :     xlrec.markfollowright = markfollowright;
                                509                 :                : 
 4133                           510                 :           1735 :     XLogBeginInsert();
                                511                 :                : 
                                512                 :                :     /*
                                513                 :                :      * Include a full page image of the child buf. (only necessary if a
                                514                 :                :      * checkpoint happened since the child page was split)
                                515                 :                :      */
 5561                           516         [ +  + ]:           1735 :     if (BufferIsValid(leftchildbuf))
 4133                           517                 :              6 :         XLogRegisterBuffer(0, leftchildbuf, REGBUF_STANDARD);
                                518                 :                : 
                                519                 :                :     /*
                                520                 :                :      * NOTE: We register a lot of data. The caller must've called
                                521                 :                :      * XLogEnsureRecordSpace() to prepare for that. We cannot do it here,
                                522                 :                :      * because we're already in a critical section. If you change the number
                                523                 :                :      * of buffer or data registrations here, make sure you modify the
                                524                 :                :      * XLogEnsureRecordSpace() calls accordingly!
                                525                 :                :      */
  397 peter@eisentraut.org      526                 :           1735 :     XLogRegisterData(&xlrec, sizeof(gistxlogPageSplit));
                                527                 :                : 
 4133 heikki.linnakangas@i      528                 :           1735 :     i = 1;
 5561                           529         [ +  + ]:           5261 :     for (ptr = dist; ptr; ptr = ptr->next)
                                530                 :                :     {
 4133                           531                 :           3526 :         XLogRegisterBuffer(i, ptr->buffer, REGBUF_WILL_INIT);
  397 peter@eisentraut.org      532                 :           3526 :         XLogRegisterBufData(i, &(ptr->block.num), sizeof(int));
                                533                 :           3526 :         XLogRegisterBufData(i, ptr->list, ptr->lenlist);
 4133 heikki.linnakangas@i      534                 :           3526 :         i++;
                                535                 :                :     }
                                536                 :                : 
                                537                 :           1735 :     recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_SPLIT);
                                538                 :                : 
 5561                           539                 :           1735 :     return recptr;
                                540                 :                : }
                                541                 :                : 
                                542                 :                : /*
                                543                 :                :  * Write XLOG record describing a page deletion. This also includes removal of
                                544                 :                :  * downlink from the parent page.
                                545                 :                :  */
                                546                 :                : XLogRecPtr
 2426                           547                 :            243 : gistXLogPageDelete(Buffer buffer, FullTransactionId xid,
                                548                 :                :                    Buffer parentBuffer, OffsetNumber downlinkOffset)
                                549                 :                : {
                                550                 :                :     gistxlogPageDelete xlrec;
                                551                 :                :     XLogRecPtr  recptr;
                                552                 :                : 
 2550                           553                 :            243 :     xlrec.deleteXid = xid;
                                554                 :            243 :     xlrec.downlinkOffset = downlinkOffset;
                                555                 :                : 
                                556                 :            243 :     XLogBeginInsert();
  397 peter@eisentraut.org      557                 :            243 :     XLogRegisterData(&xlrec, SizeOfGistxlogPageDelete);
                                558                 :                : 
 2550 heikki.linnakangas@i      559                 :            243 :     XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
                                560                 :            243 :     XLogRegisterBuffer(1, parentBuffer, REGBUF_STANDARD);
                                561                 :                : 
                                562                 :            243 :     recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_DELETE);
                                563                 :                : 
                                564                 :            243 :     return recptr;
                                565                 :                : }
                                566                 :                : 
                                567                 :                : /*
                                568                 :                :  * Write XLOG record about reuse of a deleted page.
                                569                 :                :  */
                                570                 :                : void
 1079 andres@anarazel.de        571                 :UBC           0 : gistXLogPageReuse(Relation rel, Relation heaprel,
                                572                 :                :                   BlockNumber blkno, FullTransactionId deleteXid)
                                573                 :                : {
                                574                 :                :     gistxlogPageReuse xlrec_reuse;
                                575                 :                : 
                                576                 :                :     /*
                                577                 :                :      * Note that we don't register the buffer with the record, because this
                                578                 :                :      * operation doesn't modify the page. This record only exists to provide a
                                579                 :                :      * conflict point for Hot Standby.
                                580                 :                :      */
                                581                 :                : 
                                582                 :                :     /* XLOG stuff */
 1078                           583   [ #  #  #  #  :              0 :     xlrec_reuse.isCatalogRel = RelationIsAccessibleInLogicalDecoding(heaprel);
                                     #  #  #  #  #  
                                     #  #  #  #  #  
                                     #  #  #  #  #  
                                           #  #  # ]
 1348 rhaas@postgresql.org      584                 :              0 :     xlrec_reuse.locator = rel->rd_locator;
 2550 heikki.linnakangas@i      585                 :              0 :     xlrec_reuse.block = blkno;
 1214 pg@bowt.ie                586                 :              0 :     xlrec_reuse.snapshotConflictHorizon = deleteXid;
                                587                 :                : 
 2550 heikki.linnakangas@i      588                 :              0 :     XLogBeginInsert();
  397 peter@eisentraut.org      589                 :              0 :     XLogRegisterData(&xlrec_reuse, SizeOfGistxlogPageReuse);
                                590                 :                : 
 2550 heikki.linnakangas@i      591                 :              0 :     XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_REUSE);
                                592                 :              0 : }
                                593                 :                : 
                                594                 :                : /*
                                595                 :                :  * Write XLOG record describing a page update. The update can include any
                                596                 :                :  * number of deletions and/or insertions of tuples on a single index page.
                                597                 :                :  *
                                598                 :                :  * If this update inserts a downlink for a split page, also record that
                                599                 :                :  * the F_FOLLOW_RIGHT flag on the child page is cleared and NSN set.
                                600                 :                :  *
                                601                 :                :  * Note that both the todelete array and the tuples are marked as belonging
                                602                 :                :  * to the target buffer; they need not be stored in XLOG if XLogInsert decides
                                603                 :                :  * to log the whole buffer contents instead.
                                604                 :                :  */
                                605                 :                : XLogRecPtr
 3547 alvherre@alvh.no-ip.      606                 :CBC      247881 : gistXLogUpdate(Buffer buffer,
                                607                 :                :                OffsetNumber *todelete, int ntodelete,
                                608                 :                :                IndexTuple *itup, int ituplen,
                                609                 :                :                Buffer leftchildbuf)
                                610                 :                : {
                                611                 :                :     gistxlogPageUpdate xlrec;
                                612                 :                :     int         i;
                                613                 :                :     XLogRecPtr  recptr;
                                614                 :                : 
 4871 tgl@sss.pgh.pa.us         615                 :         247881 :     xlrec.ntodelete = ntodelete;
 4133 heikki.linnakangas@i      616                 :         247881 :     xlrec.ntoinsert = ituplen;
                                617                 :                : 
                                618                 :         247881 :     XLogBeginInsert();
  397 peter@eisentraut.org      619                 :         247881 :     XLogRegisterData(&xlrec, sizeof(gistxlogPageUpdate));
                                620                 :                : 
 4133 heikki.linnakangas@i      621                 :         247881 :     XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
  397 peter@eisentraut.org      622                 :         247881 :     XLogRegisterBufData(0, todelete, sizeof(OffsetNumber) * ntodelete);
                                623                 :                : 
                                624                 :                :     /* new tuples */
 7290 tgl@sss.pgh.pa.us         625         [ +  + ]:         496880 :     for (i = 0; i < ituplen; i++)
  397 peter@eisentraut.org      626                 :         248999 :         XLogRegisterBufData(0, itup[i], IndexTupleSize(itup[i]));
                                627                 :                : 
                                628                 :                :     /*
                                629                 :                :      * Include a full page image of the child buf. (only necessary if a
                                630                 :                :      * checkpoint happened since the child page was split)
                                631                 :                :      */
 5561 heikki.linnakangas@i      632         [ +  + ]:         247881 :     if (BufferIsValid(leftchildbuf))
 4133                           633                 :           1673 :         XLogRegisterBuffer(1, leftchildbuf, REGBUF_STANDARD);
                                634                 :                : 
                                635                 :         247881 :     recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_UPDATE);
                                636                 :                : 
 7573 teodor@sigaev.ru          637                 :         247881 :     return recptr;
                                638                 :                : }
                                639                 :                : 
                                640                 :                : /*
                                641                 :                :  * Write XLOG record describing a delete of leaf index tuples marked as DEAD
                                642                 :                :  * during new tuple insertion.  One may think that this case is already covered
                                643                 :                :  * by gistXLogUpdate().  But deletion of index tuples might conflict with
                                644                 :                :  * standby queries and needs special handling.
                                645                 :                :  */
                                646                 :                : XLogRecPtr
 2641 akorotkov@postgresql      647                 :UBC           0 : gistXLogDelete(Buffer buffer, OffsetNumber *todelete, int ntodelete,
                                648                 :                :                TransactionId snapshotConflictHorizon, Relation heaprel)
                                649                 :                : {
                                650                 :                :     gistxlogDelete xlrec;
                                651                 :                :     XLogRecPtr  recptr;
                                652                 :                : 
 1078 andres@anarazel.de        653   [ #  #  #  #  :              0 :     xlrec.isCatalogRel = RelationIsAccessibleInLogicalDecoding(heaprel);
                                     #  #  #  #  #  
                                     #  #  #  #  #  
                                     #  #  #  #  #  
                                           #  #  # ]
 1214 pg@bowt.ie                654                 :              0 :     xlrec.snapshotConflictHorizon = snapshotConflictHorizon;
 2641 akorotkov@postgresql      655                 :              0 :     xlrec.ntodelete = ntodelete;
                                656                 :                : 
                                657                 :              0 :     XLogBeginInsert();
  397 peter@eisentraut.org      658                 :              0 :     XLogRegisterData(&xlrec, SizeOfGistxlogDelete);
                                659                 :                : 
                                660                 :                :     /*
                                661                 :                :      * We need the target-offsets array whether or not we store the whole
                                662                 :                :      * buffer, to allow us to find the snapshotConflictHorizon on a standby
                                663                 :                :      * server.
                                664                 :                :      */
                                665                 :              0 :     XLogRegisterData(todelete, ntodelete * sizeof(OffsetNumber));
                                666                 :                : 
 2641 akorotkov@postgresql      667                 :              0 :     XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
                                668                 :                : 
                                669                 :              0 :     recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_DELETE);
                                670                 :                : 
                                671                 :              0 :     return recptr;
                                672                 :                : }
        

Generated by: LCOV version 2.4-beta