LCOV - code coverage report
Current view: top level - src/backend/access/hash - hash_xlog.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 91.1 % 462 421
Test Date: 2026-11-20 05:16:12 Functions: 100.0 % 15 15
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 62.9 % 210 132

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * hash_xlog.c
       4                 :             :  *    WAL replay logic for hash index.
       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/hash/hash_xlog.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : #include "postgres.h"
      16                 :             : 
      17                 :             : #include "access/bufmask.h"
      18                 :             : #include "access/hash.h"
      19                 :             : #include "access/hash_xlog.h"
      20                 :             : #include "access/xlogrecovery.h"
      21                 :             : #include "access/xlogutils.h"
      22                 :             : #include "storage/standby.h"
      23                 :             : 
      24                 :             : /*
      25                 :             :  * replay a hash index meta page
      26                 :             :  */
      27                 :             : static void
      28                 :          31 : hash_xlog_init_meta_page(XLogReaderState *record)
      29                 :             : {
      30                 :          31 :     XLogRecPtr  lsn = record->EndRecPtr;
      31                 :             :     Page        page;
      32                 :             :     Buffer      metabuf;
      33                 :             : 
      34                 :          31 :     xl_hash_init_meta_page *xlrec = (xl_hash_init_meta_page *) XLogRecGetData(record);
      35                 :             : 
      36                 :             :     /* create the index' metapage */
      37                 :          31 :     metabuf = XLogInitBufferForRedo(record, 0);
      38                 :             :     Assert(BufferIsValid(metabuf));
      39                 :          31 :     _hash_init_metabuffer(metabuf, xlrec->num_tuples, xlrec->procid,
      40                 :          31 :                           xlrec->ffactor, true);
      41                 :          31 :     page = BufferGetPage(metabuf);
      42                 :          31 :     PageSetLSN(page, lsn);
      43                 :          31 :     MarkBufferDirty(metabuf);
      44                 :          31 :     XLogFlushBufferForRedoIfInit(record, 0, metabuf);
      45                 :             : 
      46                 :             :     /* all done */
      47                 :          31 :     UnlockReleaseBuffer(metabuf);
      48                 :          31 : }
      49                 :             : 
      50                 :             : /*
      51                 :             :  * replay a hash index bitmap page
      52                 :             :  */
      53                 :             : static void
      54                 :          31 : hash_xlog_init_bitmap_page(XLogReaderState *record)
      55                 :             : {
      56                 :          31 :     XLogRecPtr  lsn = record->EndRecPtr;
      57                 :             :     Buffer      bitmapbuf;
      58                 :             :     Buffer      metabuf;
      59                 :             :     Page        page;
      60                 :             :     HashMetaPage metap;
      61                 :             :     uint32      num_buckets;
      62                 :             : 
      63                 :          31 :     xl_hash_init_bitmap_page *xlrec = (xl_hash_init_bitmap_page *) XLogRecGetData(record);
      64                 :             : 
      65                 :             :     /*
      66                 :             :      * Initialize bitmap page
      67                 :             :      */
      68                 :          31 :     bitmapbuf = XLogInitBufferForRedo(record, 0);
      69                 :          31 :     _hash_initbitmapbuffer(bitmapbuf, xlrec->bmsize, true);
      70                 :          31 :     PageSetLSN(BufferGetPage(bitmapbuf), lsn);
      71                 :          31 :     MarkBufferDirty(bitmapbuf);
      72                 :          31 :     XLogFlushBufferForRedoIfInit(record, 0, bitmapbuf);
      73                 :          31 :     UnlockReleaseBuffer(bitmapbuf);
      74                 :             : 
      75                 :             :     /* add the new bitmap page to the metapage's list of bitmaps */
      76         [ +  - ]:          31 :     if (XLogReadBufferForRedo(record, 1, &metabuf) == BLK_NEEDS_REDO)
      77                 :             :     {
      78                 :             :         /*
      79                 :             :          * Note: in normal operation, we'd update the metapage while still
      80                 :             :          * holding lock on the bitmap page.  But during replay it's not
      81                 :             :          * necessary to hold that lock, since nobody can see it yet; the
      82                 :             :          * creating transaction hasn't yet committed.
      83                 :             :          */
      84                 :          31 :         page = BufferGetPage(metabuf);
      85                 :          31 :         metap = HashPageGetMeta(page);
      86                 :             : 
      87                 :          31 :         num_buckets = metap->hashm_maxbucket + 1;
      88                 :          31 :         metap->hashm_mapp[metap->hashm_nmaps] = num_buckets + 1;
      89                 :          31 :         metap->hashm_nmaps++;
      90                 :             : 
      91                 :          31 :         PageSetLSN(page, lsn);
      92                 :          31 :         MarkBufferDirty(metabuf);
      93                 :          31 :         XLogFlushBufferForRedoIfInit(record, 1, metabuf);
      94                 :             :     }
      95         [ +  - ]:          31 :     if (BufferIsValid(metabuf))
      96                 :          31 :         UnlockReleaseBuffer(metabuf);
      97                 :          31 : }
      98                 :             : 
      99                 :             : /*
     100                 :             :  * replay a hash index insert without split
     101                 :             :  */
     102                 :             : static void
     103                 :      115819 : hash_xlog_insert(XLogReaderState *record)
     104                 :             : {
     105                 :             :     HashMetaPage metap;
     106                 :      115819 :     XLogRecPtr  lsn = record->EndRecPtr;
     107                 :      115819 :     xl_hash_insert *xlrec = (xl_hash_insert *) XLogRecGetData(record);
     108                 :             :     Buffer      buffer;
     109                 :             :     Page        page;
     110                 :             : 
     111         [ +  + ]:      115819 :     if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
     112                 :             :     {
     113                 :             :         Size        datalen;
     114                 :      114282 :         char       *datapos = XLogRecGetBlockData(record, 0, &datalen);
     115                 :             : 
     116                 :      114282 :         page = BufferGetPage(buffer);
     117                 :             : 
     118         [ -  + ]:      114282 :         if (PageAddItem(page, datapos, datalen, xlrec->offnum, false, false) == InvalidOffsetNumber)
     119         [ #  # ]:           0 :             elog(PANIC, "hash_xlog_insert: failed to add item");
     120                 :             : 
     121                 :      114282 :         PageSetLSN(page, lsn);
     122                 :      114282 :         MarkBufferDirty(buffer);
     123                 :             :     }
     124         [ +  - ]:      115819 :     if (BufferIsValid(buffer))
     125                 :      115819 :         UnlockReleaseBuffer(buffer);
     126                 :             : 
     127         [ +  + ]:      115819 :     if (XLogReadBufferForRedo(record, 1, &buffer) == BLK_NEEDS_REDO)
     128                 :             :     {
     129                 :             :         /*
     130                 :             :          * Note: in normal operation, we'd update the metapage while still
     131                 :             :          * holding lock on the page we inserted into.  But during replay it's
     132                 :             :          * not necessary to hold that lock, since no other index updates can
     133                 :             :          * be happening concurrently.
     134                 :             :          */
     135                 :      115788 :         page = BufferGetPage(buffer);
     136                 :      115788 :         metap = HashPageGetMeta(page);
     137                 :      115788 :         metap->hashm_ntuples += 1;
     138                 :             : 
     139                 :      115788 :         PageSetLSN(page, lsn);
     140                 :      115788 :         MarkBufferDirty(buffer);
     141                 :             :     }
     142         [ +  - ]:      115819 :     if (BufferIsValid(buffer))
     143                 :      115819 :         UnlockReleaseBuffer(buffer);
     144                 :      115819 : }
     145                 :             : 
     146                 :             : /*
     147                 :             :  * replay addition of overflow page for hash index
     148                 :             :  */
     149                 :             : static void
     150                 :          57 : hash_xlog_add_ovfl_page(XLogReaderState *record)
     151                 :             : {
     152                 :          57 :     XLogRecPtr  lsn = record->EndRecPtr;
     153                 :          57 :     xl_hash_add_ovfl_page *xlrec = (xl_hash_add_ovfl_page *) XLogRecGetData(record);
     154                 :             :     Buffer      leftbuf;
     155                 :             :     Buffer      ovflbuf;
     156                 :             :     Buffer      metabuf;
     157                 :             :     BlockNumber leftblk;
     158                 :             :     BlockNumber rightblk;
     159                 :          57 :     BlockNumber newmapblk = InvalidBlockNumber;
     160                 :             :     Page        ovflpage;
     161                 :             :     HashPageOpaque ovflopaque;
     162                 :             :     uint32     *num_bucket;
     163                 :             :     char       *data;
     164                 :             :     Size        datalen PG_USED_FOR_ASSERTS_ONLY;
     165                 :          57 :     bool        new_bmpage = false;
     166                 :             : 
     167                 :          57 :     XLogRecGetBlockTag(record, 0, NULL, NULL, &rightblk);
     168                 :          57 :     XLogRecGetBlockTag(record, 1, NULL, NULL, &leftblk);
     169                 :             : 
     170                 :          57 :     ovflbuf = XLogInitBufferForRedo(record, 0);
     171                 :             :     Assert(BufferIsValid(ovflbuf));
     172                 :             : 
     173                 :          57 :     data = XLogRecGetBlockData(record, 0, &datalen);
     174                 :          57 :     num_bucket = (uint32 *) data;
     175                 :             :     Assert(datalen == sizeof(uint32));
     176                 :          57 :     _hash_initbuf(ovflbuf, InvalidBlockNumber, *num_bucket, LH_OVERFLOW_PAGE,
     177                 :             :                   true);
     178                 :             :     /* update backlink */
     179                 :          57 :     ovflpage = BufferGetPage(ovflbuf);
     180                 :          57 :     ovflopaque = HashPageGetOpaque(ovflpage);
     181                 :          57 :     ovflopaque->hasho_prevblkno = leftblk;
     182                 :             : 
     183                 :          57 :     PageSetLSN(ovflpage, lsn);
     184                 :          57 :     MarkBufferDirty(ovflbuf);
     185                 :             : 
     186         [ +  - ]:          57 :     if (XLogReadBufferForRedo(record, 1, &leftbuf) == BLK_NEEDS_REDO)
     187                 :             :     {
     188                 :             :         Page        leftpage;
     189                 :             :         HashPageOpaque leftopaque;
     190                 :             : 
     191                 :          57 :         leftpage = BufferGetPage(leftbuf);
     192                 :          57 :         leftopaque = HashPageGetOpaque(leftpage);
     193                 :          57 :         leftopaque->hasho_nextblkno = rightblk;
     194                 :             : 
     195                 :          57 :         PageSetLSN(leftpage, lsn);
     196                 :          57 :         MarkBufferDirty(leftbuf);
     197                 :             :     }
     198                 :             : 
     199         [ +  - ]:          57 :     if (BufferIsValid(leftbuf))
     200                 :          57 :         UnlockReleaseBuffer(leftbuf);
     201                 :          57 :     UnlockReleaseBuffer(ovflbuf);
     202                 :             : 
     203                 :             :     /*
     204                 :             :      * Note: in normal operation, we'd update the bitmap and meta page while
     205                 :             :      * still holding lock on the overflow pages.  But during replay it's not
     206                 :             :      * necessary to hold those locks, since no other index updates can be
     207                 :             :      * happening concurrently.
     208                 :             :      */
     209   [ +  -  +  + ]:          57 :     if (XLogRecHasBlockRef(record, 2))
     210                 :             :     {
     211                 :             :         Buffer      mapbuffer;
     212                 :             : 
     213         [ +  + ]:          12 :         if (XLogReadBufferForRedo(record, 2, &mapbuffer) == BLK_NEEDS_REDO)
     214                 :             :         {
     215                 :           8 :             Page        mappage = BufferGetPage(mapbuffer);
     216                 :           8 :             uint32     *freep = NULL;
     217                 :             :             uint32     *bitmap_page_bit;
     218                 :             : 
     219                 :           8 :             freep = HashPageGetBitmap(mappage);
     220                 :             : 
     221                 :           8 :             data = XLogRecGetBlockData(record, 2, &datalen);
     222                 :           8 :             bitmap_page_bit = (uint32 *) data;
     223                 :             : 
     224                 :           8 :             SETBIT(freep, *bitmap_page_bit);
     225                 :             : 
     226                 :           8 :             PageSetLSN(mappage, lsn);
     227                 :           8 :             MarkBufferDirty(mapbuffer);
     228                 :             :         }
     229         [ +  - ]:          12 :         if (BufferIsValid(mapbuffer))
     230                 :          12 :             UnlockReleaseBuffer(mapbuffer);
     231                 :             :     }
     232                 :             : 
     233   [ +  -  -  + ]:          57 :     if (XLogRecHasBlockRef(record, 3))
     234                 :             :     {
     235                 :             :         Buffer      newmapbuf;
     236                 :             : 
     237                 :           0 :         newmapbuf = XLogInitBufferForRedo(record, 3);
     238                 :             : 
     239                 :           0 :         _hash_initbitmapbuffer(newmapbuf, xlrec->bmsize, true);
     240                 :             : 
     241                 :           0 :         new_bmpage = true;
     242                 :           0 :         newmapblk = BufferGetBlockNumber(newmapbuf);
     243                 :             : 
     244                 :           0 :         MarkBufferDirty(newmapbuf);
     245                 :           0 :         PageSetLSN(BufferGetPage(newmapbuf), lsn);
     246                 :             : 
     247                 :           0 :         UnlockReleaseBuffer(newmapbuf);
     248                 :             :     }
     249                 :             : 
     250         [ +  - ]:          57 :     if (XLogReadBufferForRedo(record, 4, &metabuf) == BLK_NEEDS_REDO)
     251                 :             :     {
     252                 :             :         HashMetaPage metap;
     253                 :             :         Page        page;
     254                 :             :         uint32     *firstfree_ovflpage;
     255                 :             : 
     256                 :          57 :         data = XLogRecGetBlockData(record, 4, &datalen);
     257                 :          57 :         firstfree_ovflpage = (uint32 *) data;
     258                 :             : 
     259                 :          57 :         page = BufferGetPage(metabuf);
     260                 :          57 :         metap = HashPageGetMeta(page);
     261                 :          57 :         metap->hashm_firstfree = *firstfree_ovflpage;
     262                 :             : 
     263         [ +  + ]:          57 :         if (!xlrec->bmpage_found)
     264                 :             :         {
     265                 :          45 :             metap->hashm_spares[metap->hashm_ovflpoint]++;
     266                 :             : 
     267         [ -  + ]:          45 :             if (new_bmpage)
     268                 :             :             {
     269                 :             :                 Assert(BlockNumberIsValid(newmapblk));
     270                 :             : 
     271                 :           0 :                 metap->hashm_mapp[metap->hashm_nmaps] = newmapblk;
     272                 :           0 :                 metap->hashm_nmaps++;
     273                 :           0 :                 metap->hashm_spares[metap->hashm_ovflpoint]++;
     274                 :             :             }
     275                 :             :         }
     276                 :             : 
     277                 :          57 :         PageSetLSN(page, lsn);
     278                 :          57 :         MarkBufferDirty(metabuf);
     279                 :             :     }
     280         [ +  - ]:          57 :     if (BufferIsValid(metabuf))
     281                 :          57 :         UnlockReleaseBuffer(metabuf);
     282                 :          57 : }
     283                 :             : 
     284                 :             : /*
     285                 :             :  * replay allocation of page for split operation
     286                 :             :  */
     287                 :             : static void
     288                 :          98 : hash_xlog_split_allocate_page(XLogReaderState *record)
     289                 :             : {
     290                 :          98 :     XLogRecPtr  lsn = record->EndRecPtr;
     291                 :          98 :     xl_hash_split_allocate_page *xlrec = (xl_hash_split_allocate_page *) XLogRecGetData(record);
     292                 :             :     Buffer      oldbuf;
     293                 :             :     Buffer      newbuf;
     294                 :             :     Buffer      metabuf;
     295                 :             :     XLogRedoAction action;
     296                 :             : 
     297                 :             :     /*
     298                 :             :      * To be consistent with normal operation, here we take cleanup locks on
     299                 :             :      * both the old and new buckets even though there can't be any concurrent
     300                 :             :      * inserts.
     301                 :             :      */
     302                 :             : 
     303                 :             :     /* replay the record for old bucket */
     304                 :          98 :     action = XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &oldbuf);
     305                 :             : 
     306                 :             :     /*
     307                 :             :      * Note that we still update the page even if it was restored from a full
     308                 :             :      * page image, because the special space is not included in the image.
     309                 :             :      */
     310   [ +  +  +  - ]:          98 :     if (action == BLK_NEEDS_REDO || action == BLK_RESTORED)
     311                 :             :     {
     312                 :             :         Page        oldpage;
     313                 :             :         HashPageOpaque oldopaque;
     314                 :             : 
     315                 :          98 :         oldpage = BufferGetPage(oldbuf);
     316                 :          98 :         oldopaque = HashPageGetOpaque(oldpage);
     317                 :             : 
     318                 :          98 :         oldopaque->hasho_flag = xlrec->old_bucket_flag;
     319                 :          98 :         oldopaque->hasho_prevblkno = xlrec->new_bucket;
     320                 :             : 
     321                 :          98 :         PageSetLSN(oldpage, lsn);
     322                 :          98 :         MarkBufferDirty(oldbuf);
     323                 :             :     }
     324                 :             : 
     325                 :             :     /* replay the record for new bucket */
     326                 :          98 :     XLogReadBufferForRedoExtended(record, 1, RBM_ZERO_AND_CLEANUP_LOCK, true,
     327                 :             :                                   &newbuf);
     328                 :          98 :     _hash_initbuf(newbuf, xlrec->new_bucket, xlrec->new_bucket,
     329                 :          98 :                   xlrec->new_bucket_flag, true);
     330                 :          98 :     MarkBufferDirty(newbuf);
     331                 :          98 :     PageSetLSN(BufferGetPage(newbuf), lsn);
     332                 :             : 
     333                 :             :     /*
     334                 :             :      * We can release the lock on old bucket early as well but doing here to
     335                 :             :      * consistent with normal operation.
     336                 :             :      */
     337         [ +  - ]:          98 :     if (BufferIsValid(oldbuf))
     338                 :          98 :         UnlockReleaseBuffer(oldbuf);
     339         [ +  - ]:          98 :     if (BufferIsValid(newbuf))
     340                 :          98 :         UnlockReleaseBuffer(newbuf);
     341                 :             : 
     342                 :             :     /*
     343                 :             :      * Note: in normal operation, we'd update the meta page while still
     344                 :             :      * holding lock on the old and new bucket pages.  But during replay it's
     345                 :             :      * not necessary to hold those locks, since no other bucket splits can be
     346                 :             :      * happening concurrently.
     347                 :             :      */
     348                 :             : 
     349                 :             :     /* replay the record for metapage changes */
     350         [ +  - ]:          98 :     if (XLogReadBufferForRedo(record, 2, &metabuf) == BLK_NEEDS_REDO)
     351                 :             :     {
     352                 :             :         Page        page;
     353                 :             :         HashMetaPage metap;
     354                 :             :         Size        datalen;
     355                 :             :         char       *data;
     356                 :             :         uint32     *uidata;
     357                 :             :         int         uidatacount;
     358                 :             : 
     359                 :          98 :         page = BufferGetPage(metabuf);
     360                 :          98 :         metap = HashPageGetMeta(page);
     361                 :          98 :         metap->hashm_maxbucket = xlrec->new_bucket;
     362                 :             : 
     363                 :          98 :         data = XLogRecGetBlockData(record, 2, &datalen);
     364                 :             : 
     365                 :             :         /*
     366                 :             :          * This cast is ok because XLogRecGetBlockData() returns a MAXALIGNed
     367                 :             :          * buffer.
     368                 :             :          */
     369                 :          98 :         uidata = (uint32 *) data;
     370                 :          98 :         uidatacount = 0;
     371                 :             : 
     372         [ +  + ]:          98 :         if (xlrec->flags & XLH_SPLIT_META_UPDATE_MASKS)
     373                 :             :         {
     374                 :           3 :             uint32      lowmask = uidata[uidatacount++];
     375                 :           3 :             uint32      highmask = uidata[uidatacount++];
     376                 :             : 
     377                 :             :             /* update metapage */
     378                 :           3 :             metap->hashm_lowmask = lowmask;
     379                 :           3 :             metap->hashm_highmask = highmask;
     380                 :             :         }
     381                 :             : 
     382         [ +  + ]:          98 :         if (xlrec->flags & XLH_SPLIT_META_UPDATE_SPLITPOINT)
     383                 :             :         {
     384                 :           8 :             uint32      ovflpoint = uidata[uidatacount++];
     385                 :           8 :             uint32      ovflpages = uidata[uidatacount++];
     386                 :             : 
     387                 :             :             /* update metapage */
     388                 :           8 :             metap->hashm_ovflpoint = ovflpoint;
     389                 :           8 :             metap->hashm_spares[ovflpoint] = ovflpages;
     390                 :             :         }
     391                 :             : 
     392                 :          98 :         MarkBufferDirty(metabuf);
     393                 :          98 :         PageSetLSN(BufferGetPage(metabuf), lsn);
     394                 :             :     }
     395                 :             : 
     396         [ +  - ]:          98 :     if (BufferIsValid(metabuf))
     397                 :          98 :         UnlockReleaseBuffer(metabuf);
     398                 :          98 : }
     399                 :             : 
     400                 :             : /*
     401                 :             :  * replay of split operation
     402                 :             :  */
     403                 :             : static void
     404                 :         111 : hash_xlog_split_page(XLogReaderState *record)
     405                 :             : {
     406                 :             :     Buffer      buf;
     407                 :             : 
     408         [ -  + ]:         111 :     if (XLogReadBufferForRedo(record, 0, &buf) != BLK_RESTORED)
     409         [ #  # ]:           0 :         elog(ERROR, "Hash split record did not contain a full-page image");
     410                 :             : 
     411                 :         111 :     UnlockReleaseBuffer(buf);
     412                 :         111 : }
     413                 :             : 
     414                 :             : /*
     415                 :             :  * replay completion of split operation
     416                 :             :  */
     417                 :             : static void
     418                 :          98 : hash_xlog_split_complete(XLogReaderState *record)
     419                 :             : {
     420                 :          98 :     XLogRecPtr  lsn = record->EndRecPtr;
     421                 :          98 :     xl_hash_split_complete *xlrec = (xl_hash_split_complete *) XLogRecGetData(record);
     422                 :             :     Buffer      oldbuf;
     423                 :             :     Buffer      newbuf;
     424                 :             :     XLogRedoAction action;
     425                 :             : 
     426                 :             :     /* replay the record for old bucket */
     427                 :          98 :     action = XLogReadBufferForRedo(record, 0, &oldbuf);
     428                 :             : 
     429                 :             :     /*
     430                 :             :      * Note that we still update the page even if it was restored from a full
     431                 :             :      * page image, because the bucket flag is not included in the image.
     432                 :             :      */
     433   [ -  +  -  - ]:          98 :     if (action == BLK_NEEDS_REDO || action == BLK_RESTORED)
     434                 :             :     {
     435                 :             :         Page        oldpage;
     436                 :             :         HashPageOpaque oldopaque;
     437                 :             : 
     438                 :          98 :         oldpage = BufferGetPage(oldbuf);
     439                 :          98 :         oldopaque = HashPageGetOpaque(oldpage);
     440                 :             : 
     441                 :          98 :         oldopaque->hasho_flag = xlrec->old_bucket_flag;
     442                 :             : 
     443                 :          98 :         PageSetLSN(oldpage, lsn);
     444                 :          98 :         MarkBufferDirty(oldbuf);
     445                 :             :     }
     446         [ +  - ]:          98 :     if (BufferIsValid(oldbuf))
     447                 :          98 :         UnlockReleaseBuffer(oldbuf);
     448                 :             : 
     449                 :             :     /* replay the record for new bucket */
     450                 :          98 :     action = XLogReadBufferForRedo(record, 1, &newbuf);
     451                 :             : 
     452                 :             :     /*
     453                 :             :      * Note that we still update the page even if it was restored from a full
     454                 :             :      * page image, because the bucket flag is not included in the image.
     455                 :             :      */
     456   [ -  +  -  - ]:          98 :     if (action == BLK_NEEDS_REDO || action == BLK_RESTORED)
     457                 :             :     {
     458                 :             :         Page        newpage;
     459                 :             :         HashPageOpaque nopaque;
     460                 :             : 
     461                 :          98 :         newpage = BufferGetPage(newbuf);
     462                 :          98 :         nopaque = HashPageGetOpaque(newpage);
     463                 :             : 
     464                 :          98 :         nopaque->hasho_flag = xlrec->new_bucket_flag;
     465                 :             : 
     466                 :          98 :         PageSetLSN(newpage, lsn);
     467                 :          98 :         MarkBufferDirty(newbuf);
     468                 :             :     }
     469         [ +  - ]:          98 :     if (BufferIsValid(newbuf))
     470                 :          98 :         UnlockReleaseBuffer(newbuf);
     471                 :          98 : }
     472                 :             : 
     473                 :             : /*
     474                 :             :  * replay move of page contents for squeeze operation of hash index
     475                 :             :  */
     476                 :             : static void
     477                 :           1 : hash_xlog_move_page_contents(XLogReaderState *record)
     478                 :             : {
     479                 :           1 :     XLogRecPtr  lsn = record->EndRecPtr;
     480                 :           1 :     xl_hash_move_page_contents *xldata = (xl_hash_move_page_contents *) XLogRecGetData(record);
     481                 :           1 :     Buffer      bucketbuf = InvalidBuffer;
     482                 :           1 :     Buffer      writebuf = InvalidBuffer;
     483                 :           1 :     Buffer      deletebuf = InvalidBuffer;
     484                 :             :     XLogRedoAction action;
     485                 :             : 
     486                 :             :     /*
     487                 :             :      * Ensure we have a cleanup lock on primary bucket page before we start
     488                 :             :      * with the actual replay operation.  This is to ensure that neither a
     489                 :             :      * scan can start nor a scan can be already-in-progress during the replay
     490                 :             :      * of this operation.  If we allow scans during this operation, then they
     491                 :             :      * can miss some records or show the same record multiple times.
     492                 :             :      */
     493         [ +  - ]:           1 :     if (xldata->is_prim_bucket_same_wrt)
     494                 :           1 :         action = XLogReadBufferForRedoExtended(record, 1, RBM_NORMAL, true, &writebuf);
     495                 :             :     else
     496                 :             :     {
     497                 :             :         /*
     498                 :             :          * The cleanup lock is needed only when hot standby is active, because
     499                 :             :          * scans cannot run before that. Skipping the read also avoids a torn
     500                 :             :          * copy of the primary page. This record does not modify that page, so
     501                 :             :          * it has no full-page image of it. In crash recovery, or on a standby
     502                 :             :          * started from a base backup, the page on disk can be torn and the
     503                 :             :          * image that repairs it can come later in the WAL.
     504                 :             :          */
     505         [ #  # ]:           0 :         if (HotStandbyActiveInReplay())
     506                 :             :         {
     507                 :             :             /*
     508                 :             :              * we don't care for return value as the purpose of reading
     509                 :             :              * bucketbuf is to ensure a cleanup lock on primary bucket page.
     510                 :             :              */
     511                 :           0 :             (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
     512                 :             :         }
     513                 :             : 
     514                 :           0 :         action = XLogReadBufferForRedo(record, 1, &writebuf);
     515                 :             :     }
     516                 :             : 
     517                 :             :     /* replay the record for adding entries in overflow buffer */
     518         [ +  - ]:           1 :     if (action == BLK_NEEDS_REDO)
     519                 :             :     {
     520                 :             :         Page        writepage;
     521                 :             :         char       *begin;
     522                 :             :         char       *data;
     523                 :             :         Size        datalen;
     524                 :           1 :         uint16      ninserted = 0;
     525                 :             : 
     526                 :           1 :         data = begin = XLogRecGetBlockData(record, 1, &datalen);
     527                 :             : 
     528                 :           1 :         writepage = BufferGetPage(writebuf);
     529                 :             : 
     530         [ +  - ]:           1 :         if (xldata->ntups > 0)
     531                 :             :         {
     532                 :           1 :             OffsetNumber *towrite = (OffsetNumber *) data;
     533                 :             : 
     534                 :           1 :             data += sizeof(OffsetNumber) * xldata->ntups;
     535                 :             : 
     536         [ +  + ]:         343 :             while (data - begin < datalen)
     537                 :             :             {
     538                 :         342 :                 IndexTuple  itup = (IndexTuple) data;
     539                 :             :                 Size        itemsz;
     540                 :             :                 OffsetNumber l;
     541                 :             : 
     542                 :         342 :                 itemsz = IndexTupleSize(itup);
     543                 :         342 :                 itemsz = MAXALIGN(itemsz);
     544                 :             : 
     545                 :         342 :                 data += itemsz;
     546                 :             : 
     547                 :         342 :                 l = PageAddItem(writepage, itup, itemsz, towrite[ninserted], false, false);
     548         [ -  + ]:         342 :                 if (l == InvalidOffsetNumber)
     549         [ #  # ]:           0 :                     elog(ERROR, "hash_xlog_move_page_contents: failed to add item to hash index page, size %zu bytes", itemsz);
     550                 :             : 
     551                 :         342 :                 ninserted++;
     552                 :             :             }
     553                 :             :         }
     554                 :             : 
     555                 :             :         /*
     556                 :             :          * number of tuples inserted must be same as requested in REDO record.
     557                 :             :          */
     558                 :             :         Assert(ninserted == xldata->ntups);
     559                 :             : 
     560                 :           1 :         PageSetLSN(writepage, lsn);
     561                 :           1 :         MarkBufferDirty(writebuf);
     562                 :             :     }
     563                 :             : 
     564                 :             :     /* replay the record for deleting entries from overflow buffer */
     565         [ +  - ]:           1 :     if (XLogReadBufferForRedo(record, 2, &deletebuf) == BLK_NEEDS_REDO)
     566                 :             :     {
     567                 :             :         Page        page;
     568                 :             :         char       *ptr;
     569                 :             :         Size        len;
     570                 :             : 
     571                 :           1 :         ptr = XLogRecGetBlockData(record, 2, &len);
     572                 :             : 
     573                 :           1 :         page = BufferGetPage(deletebuf);
     574                 :             : 
     575         [ +  - ]:           1 :         if (len > 0)
     576                 :             :         {
     577                 :             :             OffsetNumber *unused;
     578                 :             :             OffsetNumber *unend;
     579                 :             : 
     580                 :           1 :             unused = (OffsetNumber *) ptr;
     581                 :           1 :             unend = (OffsetNumber *) (ptr + len);
     582                 :             : 
     583         [ +  - ]:           1 :             if ((unend - unused) > 0)
     584                 :           1 :                 PageIndexMultiDelete(page, unused, unend - unused);
     585                 :             :         }
     586                 :             : 
     587                 :           1 :         PageSetLSN(page, lsn);
     588                 :           1 :         MarkBufferDirty(deletebuf);
     589                 :             :     }
     590                 :             : 
     591                 :             :     /*
     592                 :             :      * Replay is complete, now we can release the buffers. We release locks at
     593                 :             :      * end of replay operation to ensure that we hold lock on primary bucket
     594                 :             :      * page till end of operation.  We can optimize by releasing the lock on
     595                 :             :      * write buffer as soon as the operation for same is complete, if it is
     596                 :             :      * not same as primary bucket page, but that doesn't seem to be worth
     597                 :             :      * complicating the code.
     598                 :             :      */
     599         [ +  - ]:           1 :     if (BufferIsValid(deletebuf))
     600                 :           1 :         UnlockReleaseBuffer(deletebuf);
     601                 :             : 
     602         [ +  - ]:           1 :     if (BufferIsValid(writebuf))
     603                 :           1 :         UnlockReleaseBuffer(writebuf);
     604                 :             : 
     605         [ -  + ]:           1 :     if (BufferIsValid(bucketbuf))
     606                 :           0 :         UnlockReleaseBuffer(bucketbuf);
     607                 :           1 : }
     608                 :             : 
     609                 :             : /*
     610                 :             :  * replay squeeze page operation of hash index
     611                 :             :  */
     612                 :             : static void
     613                 :          32 : hash_xlog_squeeze_page(XLogReaderState *record)
     614                 :             : {
     615                 :          32 :     XLogRecPtr  lsn = record->EndRecPtr;
     616                 :          32 :     xl_hash_squeeze_page *xldata = (xl_hash_squeeze_page *) XLogRecGetData(record);
     617                 :          32 :     Buffer      bucketbuf = InvalidBuffer;
     618                 :          32 :     Buffer      writebuf = InvalidBuffer;
     619                 :             :     Buffer      ovflbuf;
     620                 :          32 :     Buffer      prevbuf = InvalidBuffer;
     621                 :             :     Buffer      mapbuf;
     622                 :             :     XLogRedoAction action;
     623                 :             : 
     624                 :             :     /*
     625                 :             :      * Ensure we have a cleanup lock on primary bucket page before we start
     626                 :             :      * with the actual replay operation.  This is to ensure that neither a
     627                 :             :      * scan can start nor a scan can be already-in-progress during the replay
     628                 :             :      * of this operation.  If we allow scans during this operation, then they
     629                 :             :      * can miss some records or show the same record multiple times.
     630                 :             :      */
     631         [ +  + ]:          32 :     if (xldata->is_prim_bucket_same_wrt)
     632                 :          24 :         action = XLogReadBufferForRedoExtended(record, 1, RBM_NORMAL, true, &writebuf);
     633                 :             :     else
     634                 :             :     {
     635                 :             :         /*
     636                 :             :          * The cleanup lock is needed only when hot standby is active, because
     637                 :             :          * scans cannot run before that. Skipping the read also avoids a torn
     638                 :             :          * copy of the primary page. This record does not modify that page, so
     639                 :             :          * it has no full-page image of it. In crash recovery, or on a standby
     640                 :             :          * started from a base backup, the page on disk can be torn and the
     641                 :             :          * image that repairs it can come later in the WAL.
     642                 :             :          */
     643         [ +  - ]:           8 :         if (HotStandbyActiveInReplay())
     644                 :             :         {
     645                 :             :             /*
     646                 :             :              * we don't care for return value as the purpose of reading
     647                 :             :              * bucketbuf is to ensure a cleanup lock on primary bucket page.
     648                 :             :              */
     649                 :           8 :             (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
     650                 :             :         }
     651                 :             : 
     652   [ +  +  +  + ]:           8 :         if (xldata->ntups > 0 || xldata->is_prev_bucket_same_wrt)
     653                 :           7 :             action = XLogReadBufferForRedo(record, 1, &writebuf);
     654                 :             :         else
     655                 :           1 :             action = BLK_NOTFOUND;
     656                 :             :     }
     657                 :             : 
     658                 :             :     /* replay the record for adding entries in overflow buffer */
     659         [ +  + ]:          32 :     if (action == BLK_NEEDS_REDO)
     660                 :             :     {
     661                 :             :         Page        writepage;
     662                 :             :         char       *begin;
     663                 :             :         char       *data;
     664                 :             :         Size        datalen;
     665                 :          29 :         uint16      ninserted = 0;
     666                 :          29 :         bool        mod_wbuf = false;
     667                 :             : 
     668                 :          29 :         data = begin = XLogRecGetBlockData(record, 1, &datalen);
     669                 :             : 
     670                 :          29 :         writepage = BufferGetPage(writebuf);
     671                 :             : 
     672         [ +  + ]:          29 :         if (xldata->ntups > 0)
     673                 :             :         {
     674                 :          15 :             OffsetNumber *towrite = (OffsetNumber *) data;
     675                 :             : 
     676                 :          15 :             data += sizeof(OffsetNumber) * xldata->ntups;
     677                 :             : 
     678         [ +  + ]:         627 :             while (data - begin < datalen)
     679                 :             :             {
     680                 :         612 :                 IndexTuple  itup = (IndexTuple) data;
     681                 :             :                 Size        itemsz;
     682                 :             :                 OffsetNumber l;
     683                 :             : 
     684                 :         612 :                 itemsz = IndexTupleSize(itup);
     685                 :         612 :                 itemsz = MAXALIGN(itemsz);
     686                 :             : 
     687                 :         612 :                 data += itemsz;
     688                 :             : 
     689                 :         612 :                 l = PageAddItem(writepage, itup, itemsz, towrite[ninserted], false, false);
     690         [ -  + ]:         612 :                 if (l == InvalidOffsetNumber)
     691         [ #  # ]:           0 :                     elog(ERROR, "hash_xlog_squeeze_page: failed to add item to hash index page, size %zu bytes", itemsz);
     692                 :             : 
     693                 :         612 :                 ninserted++;
     694                 :             :             }
     695                 :             : 
     696                 :          15 :             mod_wbuf = true;
     697                 :             :         }
     698                 :             :         else
     699                 :             :         {
     700                 :             :             /*
     701                 :             :              * Ensure that the required flags are set when there are no
     702                 :             :              * tuples.  See _hash_freeovflpage().
     703                 :             :              */
     704                 :             :             Assert(xldata->is_prim_bucket_same_wrt ||
     705                 :             :                    xldata->is_prev_bucket_same_wrt);
     706                 :             :         }
     707                 :             : 
     708                 :             :         /*
     709                 :             :          * number of tuples inserted must be same as requested in REDO record.
     710                 :             :          */
     711                 :             :         Assert(ninserted == xldata->ntups);
     712                 :             : 
     713                 :             :         /*
     714                 :             :          * if the page on which are adding tuples is a page previous to freed
     715                 :             :          * overflow page, then update its nextblkno.
     716                 :             :          */
     717         [ +  + ]:          29 :         if (xldata->is_prev_bucket_same_wrt)
     718                 :             :         {
     719                 :          11 :             HashPageOpaque writeopaque = HashPageGetOpaque(writepage);
     720                 :             : 
     721                 :          11 :             writeopaque->hasho_nextblkno = xldata->nextblkno;
     722                 :          11 :             mod_wbuf = true;
     723                 :             :         }
     724                 :             : 
     725                 :             :         /* Set LSN and mark writebuf dirty iff it is modified */
     726         [ +  + ]:          29 :         if (mod_wbuf)
     727                 :             :         {
     728                 :          18 :             PageSetLSN(writepage, lsn);
     729                 :          18 :             MarkBufferDirty(writebuf);
     730                 :             :         }
     731                 :             :     }
     732                 :             : 
     733                 :             :     /* replay the record for initializing overflow buffer */
     734         [ -  + ]:          32 :     if (XLogReadBufferForRedo(record, 2, &ovflbuf) == BLK_NEEDS_REDO)
     735                 :             :     {
     736                 :             :         Page        ovflpage;
     737                 :             :         HashPageOpaque ovflopaque;
     738                 :             : 
     739                 :           0 :         ovflpage = BufferGetPage(ovflbuf);
     740                 :             : 
     741                 :           0 :         _hash_pageinit(ovflpage, BufferGetPageSize(ovflbuf));
     742                 :             : 
     743                 :           0 :         ovflopaque = HashPageGetOpaque(ovflpage);
     744                 :             : 
     745                 :           0 :         ovflopaque->hasho_prevblkno = InvalidBlockNumber;
     746                 :           0 :         ovflopaque->hasho_nextblkno = InvalidBlockNumber;
     747                 :           0 :         ovflopaque->hasho_bucket = InvalidBucket;
     748                 :           0 :         ovflopaque->hasho_flag = LH_UNUSED_PAGE;
     749                 :           0 :         ovflopaque->hasho_page_id = HASHO_PAGE_ID;
     750                 :             : 
     751                 :           0 :         PageSetLSN(ovflpage, lsn);
     752                 :           0 :         MarkBufferDirty(ovflbuf);
     753                 :             :     }
     754         [ +  - ]:          32 :     if (BufferIsValid(ovflbuf))
     755                 :          32 :         UnlockReleaseBuffer(ovflbuf);
     756                 :             : 
     757                 :             :     /* replay the record for page previous to the freed overflow page */
     758   [ +  +  +  + ]:          53 :     if (!xldata->is_prev_bucket_same_wrt &&
     759                 :          21 :         XLogReadBufferForRedo(record, 3, &prevbuf) == BLK_NEEDS_REDO)
     760                 :             :     {
     761                 :          20 :         Page        prevpage = BufferGetPage(prevbuf);
     762                 :          20 :         HashPageOpaque prevopaque = HashPageGetOpaque(prevpage);
     763                 :             : 
     764                 :          20 :         prevopaque->hasho_nextblkno = xldata->nextblkno;
     765                 :             : 
     766                 :          20 :         PageSetLSN(prevpage, lsn);
     767                 :          20 :         MarkBufferDirty(prevbuf);
     768                 :             :     }
     769         [ +  + ]:          32 :     if (BufferIsValid(prevbuf))
     770                 :          21 :         UnlockReleaseBuffer(prevbuf);
     771                 :             : 
     772                 :             :     /* replay the record for page next to the freed overflow page */
     773   [ +  -  -  + ]:          32 :     if (XLogRecHasBlockRef(record, 4))
     774                 :             :     {
     775                 :             :         Buffer      nextbuf;
     776                 :             : 
     777         [ #  # ]:           0 :         if (XLogReadBufferForRedo(record, 4, &nextbuf) == BLK_NEEDS_REDO)
     778                 :             :         {
     779                 :           0 :             Page        nextpage = BufferGetPage(nextbuf);
     780                 :           0 :             HashPageOpaque nextopaque = HashPageGetOpaque(nextpage);
     781                 :             : 
     782                 :           0 :             nextopaque->hasho_prevblkno = xldata->prevblkno;
     783                 :             : 
     784                 :           0 :             PageSetLSN(nextpage, lsn);
     785                 :           0 :             MarkBufferDirty(nextbuf);
     786                 :             :         }
     787         [ #  # ]:           0 :         if (BufferIsValid(nextbuf))
     788                 :           0 :             UnlockReleaseBuffer(nextbuf);
     789                 :             :     }
     790                 :             : 
     791         [ +  + ]:          32 :     if (BufferIsValid(writebuf))
     792                 :          31 :         UnlockReleaseBuffer(writebuf);
     793                 :             : 
     794         [ +  + ]:          32 :     if (BufferIsValid(bucketbuf))
     795                 :           8 :         UnlockReleaseBuffer(bucketbuf);
     796                 :             : 
     797                 :             :     /*
     798                 :             :      * Note: in normal operation, we'd update the bitmap and meta page while
     799                 :             :      * still holding lock on the primary bucket page and overflow pages.  But
     800                 :             :      * during replay it's not necessary to hold those locks, since no other
     801                 :             :      * index updates can be happening concurrently.
     802                 :             :      */
     803                 :             :     /* replay the record for bitmap page */
     804         [ +  + ]:          32 :     if (XLogReadBufferForRedo(record, 5, &mapbuf) == BLK_NEEDS_REDO)
     805                 :             :     {
     806                 :          25 :         Page        mappage = BufferGetPage(mapbuf);
     807                 :          25 :         uint32     *freep = NULL;
     808                 :             :         char       *data;
     809                 :             :         uint32     *bitmap_page_bit;
     810                 :             :         Size        datalen;
     811                 :             : 
     812                 :          25 :         freep = HashPageGetBitmap(mappage);
     813                 :             : 
     814                 :          25 :         data = XLogRecGetBlockData(record, 5, &datalen);
     815                 :          25 :         bitmap_page_bit = (uint32 *) data;
     816                 :             : 
     817                 :          25 :         CLRBIT(freep, *bitmap_page_bit);
     818                 :             : 
     819                 :          25 :         PageSetLSN(mappage, lsn);
     820                 :          25 :         MarkBufferDirty(mapbuf);
     821                 :             :     }
     822         [ +  - ]:          32 :     if (BufferIsValid(mapbuf))
     823                 :          32 :         UnlockReleaseBuffer(mapbuf);
     824                 :             : 
     825                 :             :     /* replay the record for meta page */
     826   [ +  +  +  - ]:          32 :     if (XLogRecHasBlockRef(record, 6))
     827                 :             :     {
     828                 :             :         Buffer      metabuf;
     829                 :             : 
     830         [ +  + ]:          31 :         if (XLogReadBufferForRedo(record, 6, &metabuf) == BLK_NEEDS_REDO)
     831                 :             :         {
     832                 :             :             HashMetaPage metap;
     833                 :             :             Page        page;
     834                 :             :             char       *data;
     835                 :             :             uint32     *firstfree_ovflpage;
     836                 :             :             Size        datalen;
     837                 :             : 
     838                 :          28 :             data = XLogRecGetBlockData(record, 6, &datalen);
     839                 :          28 :             firstfree_ovflpage = (uint32 *) data;
     840                 :             : 
     841                 :          28 :             page = BufferGetPage(metabuf);
     842                 :          28 :             metap = HashPageGetMeta(page);
     843                 :          28 :             metap->hashm_firstfree = *firstfree_ovflpage;
     844                 :             : 
     845                 :          28 :             PageSetLSN(page, lsn);
     846                 :          28 :             MarkBufferDirty(metabuf);
     847                 :             :         }
     848         [ +  - ]:          31 :         if (BufferIsValid(metabuf))
     849                 :          31 :             UnlockReleaseBuffer(metabuf);
     850                 :             :     }
     851                 :          32 : }
     852                 :             : 
     853                 :             : /*
     854                 :             :  * replay delete operation of hash index
     855                 :             :  */
     856                 :             : static void
     857                 :         138 : hash_xlog_delete(XLogReaderState *record)
     858                 :             : {
     859                 :         138 :     XLogRecPtr  lsn = record->EndRecPtr;
     860                 :         138 :     xl_hash_delete *xldata = (xl_hash_delete *) XLogRecGetData(record);
     861                 :         138 :     Buffer      bucketbuf = InvalidBuffer;
     862                 :             :     Buffer      deletebuf;
     863                 :             :     Page        page;
     864                 :             :     XLogRedoAction action;
     865                 :             : 
     866                 :             :     /*
     867                 :             :      * Ensure we have a cleanup lock on primary bucket page before we start
     868                 :             :      * with the actual replay operation.  This is to ensure that neither a
     869                 :             :      * scan can start nor a scan can be already-in-progress during the replay
     870                 :             :      * of this operation.  If we allow scans during this operation, then they
     871                 :             :      * can miss some records or show the same record multiple times.
     872                 :             :      */
     873         [ +  + ]:         138 :     if (xldata->is_primary_bucket_page)
     874                 :         106 :         action = XLogReadBufferForRedoExtended(record, 1, RBM_NORMAL, true, &deletebuf);
     875                 :             :     else
     876                 :             :     {
     877                 :             :         /*
     878                 :             :          * The cleanup lock is needed only when hot standby is active, because
     879                 :             :          * scans cannot run before that. Skipping the read also avoids a torn
     880                 :             :          * copy of the primary page. This record does not modify that page, so
     881                 :             :          * it has no full-page image of it. In crash recovery, or on a standby
     882                 :             :          * started from a base backup, the page on disk can be torn and the
     883                 :             :          * image that repairs it can come later in the WAL.
     884                 :             :          */
     885         [ +  - ]:          32 :         if (HotStandbyActiveInReplay())
     886                 :             :         {
     887                 :             :             /*
     888                 :             :              * we don't care for return value as the purpose of reading
     889                 :             :              * bucketbuf is to ensure a cleanup lock on primary bucket page.
     890                 :             :              */
     891                 :          32 :             (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
     892                 :             :         }
     893                 :             : 
     894                 :          32 :         action = XLogReadBufferForRedo(record, 1, &deletebuf);
     895                 :             :     }
     896                 :             : 
     897                 :             :     /* replay the record for deleting entries in bucket page */
     898         [ +  + ]:         138 :     if (action == BLK_NEEDS_REDO)
     899                 :             :     {
     900                 :             :         char       *ptr;
     901                 :             :         Size        len;
     902                 :             : 
     903                 :         106 :         ptr = XLogRecGetBlockData(record, 1, &len);
     904                 :             : 
     905                 :         106 :         page = BufferGetPage(deletebuf);
     906                 :             : 
     907         [ +  - ]:         106 :         if (len > 0)
     908                 :             :         {
     909                 :             :             OffsetNumber *unused;
     910                 :             :             OffsetNumber *unend;
     911                 :             : 
     912                 :         106 :             unused = (OffsetNumber *) ptr;
     913                 :         106 :             unend = (OffsetNumber *) (ptr + len);
     914                 :             : 
     915         [ +  - ]:         106 :             if ((unend - unused) > 0)
     916                 :         106 :                 PageIndexMultiDelete(page, unused, unend - unused);
     917                 :             :         }
     918                 :             : 
     919                 :             :         /*
     920                 :             :          * Mark the page as not containing any LP_DEAD items only if
     921                 :             :          * clear_dead_marking flag is set to true. See comments in
     922                 :             :          * hashbucketcleanup() for details.
     923                 :             :          */
     924         [ -  + ]:         106 :         if (xldata->clear_dead_marking)
     925                 :             :         {
     926                 :             :             HashPageOpaque pageopaque;
     927                 :             : 
     928                 :           0 :             pageopaque = HashPageGetOpaque(page);
     929                 :           0 :             pageopaque->hasho_flag &= ~LH_PAGE_HAS_DEAD_TUPLES;
     930                 :             :         }
     931                 :             : 
     932                 :         106 :         PageSetLSN(page, lsn);
     933                 :         106 :         MarkBufferDirty(deletebuf);
     934                 :             :     }
     935         [ +  - ]:         138 :     if (BufferIsValid(deletebuf))
     936                 :         138 :         UnlockReleaseBuffer(deletebuf);
     937                 :             : 
     938         [ +  + ]:         138 :     if (BufferIsValid(bucketbuf))
     939                 :          32 :         UnlockReleaseBuffer(bucketbuf);
     940                 :         138 : }
     941                 :             : 
     942                 :             : /*
     943                 :             :  * replay split cleanup flag operation for primary bucket page.
     944                 :             :  */
     945                 :             : static void
     946                 :          98 : hash_xlog_split_cleanup(XLogReaderState *record)
     947                 :             : {
     948                 :          98 :     XLogRecPtr  lsn = record->EndRecPtr;
     949                 :             :     Buffer      buffer;
     950                 :             :     Page        page;
     951                 :             : 
     952         [ +  - ]:          98 :     if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
     953                 :             :     {
     954                 :             :         HashPageOpaque bucket_opaque;
     955                 :             : 
     956                 :          98 :         page = BufferGetPage(buffer);
     957                 :             : 
     958                 :          98 :         bucket_opaque = HashPageGetOpaque(page);
     959                 :          98 :         bucket_opaque->hasho_flag &= ~LH_BUCKET_NEEDS_SPLIT_CLEANUP;
     960                 :          98 :         PageSetLSN(page, lsn);
     961                 :          98 :         MarkBufferDirty(buffer);
     962                 :             :     }
     963         [ +  - ]:          98 :     if (BufferIsValid(buffer))
     964                 :          98 :         UnlockReleaseBuffer(buffer);
     965                 :          98 : }
     966                 :             : 
     967                 :             : /*
     968                 :             :  * replay for update meta page
     969                 :             :  */
     970                 :             : static void
     971                 :           9 : hash_xlog_update_meta_page(XLogReaderState *record)
     972                 :             : {
     973                 :             :     HashMetaPage metap;
     974                 :           9 :     XLogRecPtr  lsn = record->EndRecPtr;
     975                 :           9 :     xl_hash_update_meta_page *xldata = (xl_hash_update_meta_page *) XLogRecGetData(record);
     976                 :             :     Buffer      metabuf;
     977                 :             :     Page        page;
     978                 :             : 
     979         [ +  + ]:           9 :     if (XLogReadBufferForRedo(record, 0, &metabuf) == BLK_NEEDS_REDO)
     980                 :             :     {
     981                 :           5 :         page = BufferGetPage(metabuf);
     982                 :           5 :         metap = HashPageGetMeta(page);
     983                 :             : 
     984                 :           5 :         metap->hashm_ntuples = xldata->ntuples;
     985                 :             : 
     986                 :           5 :         PageSetLSN(page, lsn);
     987                 :           5 :         MarkBufferDirty(metabuf);
     988                 :             :     }
     989         [ +  - ]:           9 :     if (BufferIsValid(metabuf))
     990                 :           9 :         UnlockReleaseBuffer(metabuf);
     991                 :           9 : }
     992                 :             : 
     993                 :             : /*
     994                 :             :  * replay delete operation in hash index to remove
     995                 :             :  * tuples marked as DEAD during index tuple insertion.
     996                 :             :  */
     997                 :             : static void
     998                 :           1 : hash_xlog_vacuum_one_page(XLogReaderState *record)
     999                 :             : {
    1000                 :           1 :     XLogRecPtr  lsn = record->EndRecPtr;
    1001                 :             :     xl_hash_vacuum_one_page *xldata;
    1002                 :             :     Buffer      buffer;
    1003                 :             :     Buffer      metabuf;
    1004                 :             :     Page        page;
    1005                 :             :     XLogRedoAction action;
    1006                 :             :     HashPageOpaque pageopaque;
    1007                 :             :     OffsetNumber *toDelete;
    1008                 :             : 
    1009                 :           1 :     xldata = (xl_hash_vacuum_one_page *) XLogRecGetData(record);
    1010                 :           1 :     toDelete = xldata->offsets;
    1011                 :             : 
    1012                 :             :     /*
    1013                 :             :      * If we have any conflict processing to do, it must happen before we
    1014                 :             :      * update the page.
    1015                 :             :      *
    1016                 :             :      * Hash index records that are marked as LP_DEAD and being removed during
    1017                 :             :      * hash index tuple insertion can conflict with standby queries. You might
    1018                 :             :      * think that vacuum records would conflict as well, but we've handled
    1019                 :             :      * that already.  XLOG_HEAP2_PRUNE_VACUUM_SCAN records provide the highest
    1020                 :             :      * xid cleaned by the vacuum of the heap and so we can resolve any
    1021                 :             :      * conflicts just once when that arrives.  After that we know that no
    1022                 :             :      * conflicts exist from individual hash index vacuum records on that
    1023                 :             :      * index.
    1024                 :             :      */
    1025         [ +  - ]:           1 :     if (InHotStandby)
    1026                 :             :     {
    1027                 :             :         RelFileLocator rlocator;
    1028                 :             : 
    1029                 :           1 :         XLogRecGetBlockTag(record, 0, &rlocator, NULL, NULL);
    1030                 :           1 :         ResolveRecoveryConflictWithSnapshot(xldata->snapshotConflictHorizon,
    1031                 :           1 :                                             xldata->isCatalogRel,
    1032                 :             :                                             rlocator);
    1033                 :             :     }
    1034                 :             : 
    1035                 :           1 :     action = XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &buffer);
    1036                 :             : 
    1037         [ +  - ]:           1 :     if (action == BLK_NEEDS_REDO)
    1038                 :             :     {
    1039                 :           1 :         page = BufferGetPage(buffer);
    1040                 :             : 
    1041                 :           1 :         PageIndexMultiDelete(page, toDelete, xldata->ntuples);
    1042                 :             : 
    1043                 :             :         /*
    1044                 :             :          * Mark the page as not containing any LP_DEAD items. See comments in
    1045                 :             :          * _hash_vacuum_one_page() for details.
    1046                 :             :          */
    1047                 :           1 :         pageopaque = HashPageGetOpaque(page);
    1048                 :           1 :         pageopaque->hasho_flag &= ~LH_PAGE_HAS_DEAD_TUPLES;
    1049                 :             : 
    1050                 :           1 :         PageSetLSN(page, lsn);
    1051                 :           1 :         MarkBufferDirty(buffer);
    1052                 :             :     }
    1053         [ +  - ]:           1 :     if (BufferIsValid(buffer))
    1054                 :           1 :         UnlockReleaseBuffer(buffer);
    1055                 :             : 
    1056         [ +  - ]:           1 :     if (XLogReadBufferForRedo(record, 1, &metabuf) == BLK_NEEDS_REDO)
    1057                 :             :     {
    1058                 :             :         Page        metapage;
    1059                 :             :         HashMetaPage metap;
    1060                 :             : 
    1061                 :           1 :         metapage = BufferGetPage(metabuf);
    1062                 :           1 :         metap = HashPageGetMeta(metapage);
    1063                 :             : 
    1064                 :           1 :         metap->hashm_ntuples -= xldata->ntuples;
    1065                 :             : 
    1066                 :           1 :         PageSetLSN(metapage, lsn);
    1067                 :           1 :         MarkBufferDirty(metabuf);
    1068                 :             :     }
    1069         [ +  - ]:           1 :     if (BufferIsValid(metabuf))
    1070                 :           1 :         UnlockReleaseBuffer(metabuf);
    1071                 :           1 : }
    1072                 :             : 
    1073                 :             : void
    1074                 :      116524 : hash_redo(XLogReaderState *record)
    1075                 :             : {
    1076                 :      116524 :     uint8       info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
    1077                 :             : 
    1078   [ +  +  +  +  :      116524 :     switch (info)
          +  +  +  +  +  
             +  +  +  +  
                      - ]
    1079                 :             :     {
    1080                 :          31 :         case XLOG_HASH_INIT_META_PAGE:
    1081                 :          31 :             hash_xlog_init_meta_page(record);
    1082                 :          31 :             break;
    1083                 :          31 :         case XLOG_HASH_INIT_BITMAP_PAGE:
    1084                 :          31 :             hash_xlog_init_bitmap_page(record);
    1085                 :          31 :             break;
    1086                 :      115819 :         case XLOG_HASH_INSERT:
    1087                 :      115819 :             hash_xlog_insert(record);
    1088                 :      115819 :             break;
    1089                 :          57 :         case XLOG_HASH_ADD_OVFL_PAGE:
    1090                 :          57 :             hash_xlog_add_ovfl_page(record);
    1091                 :          57 :             break;
    1092                 :          98 :         case XLOG_HASH_SPLIT_ALLOCATE_PAGE:
    1093                 :          98 :             hash_xlog_split_allocate_page(record);
    1094                 :          98 :             break;
    1095                 :         111 :         case XLOG_HASH_SPLIT_PAGE:
    1096                 :         111 :             hash_xlog_split_page(record);
    1097                 :         111 :             break;
    1098                 :          98 :         case XLOG_HASH_SPLIT_COMPLETE:
    1099                 :          98 :             hash_xlog_split_complete(record);
    1100                 :          98 :             break;
    1101                 :           1 :         case XLOG_HASH_MOVE_PAGE_CONTENTS:
    1102                 :           1 :             hash_xlog_move_page_contents(record);
    1103                 :           1 :             break;
    1104                 :          32 :         case XLOG_HASH_SQUEEZE_PAGE:
    1105                 :          32 :             hash_xlog_squeeze_page(record);
    1106                 :          32 :             break;
    1107                 :         138 :         case XLOG_HASH_DELETE:
    1108                 :         138 :             hash_xlog_delete(record);
    1109                 :         138 :             break;
    1110                 :          98 :         case XLOG_HASH_SPLIT_CLEANUP:
    1111                 :          98 :             hash_xlog_split_cleanup(record);
    1112                 :          98 :             break;
    1113                 :           9 :         case XLOG_HASH_UPDATE_META_PAGE:
    1114                 :           9 :             hash_xlog_update_meta_page(record);
    1115                 :           9 :             break;
    1116                 :           1 :         case XLOG_HASH_VACUUM_ONE_PAGE:
    1117                 :           1 :             hash_xlog_vacuum_one_page(record);
    1118                 :           1 :             break;
    1119                 :           0 :         default:
    1120         [ #  # ]:           0 :             elog(PANIC, "hash_redo: unknown op code %u", info);
    1121                 :             :     }
    1122                 :      116524 : }
    1123                 :             : 
    1124                 :             : /*
    1125                 :             :  * Mask a hash page before performing consistency checks on it.
    1126                 :             :  */
    1127                 :             : void
    1128                 :      462252 : hash_mask(char *pagedata, BlockNumber blkno)
    1129                 :             : {
    1130                 :      462252 :     Page        page = (Page) pagedata;
    1131                 :             :     HashPageOpaque opaque;
    1132                 :             :     int         pagetype;
    1133                 :             : 
    1134                 :      462252 :     mask_page_lsn_and_checksum(page);
    1135                 :             : 
    1136                 :      462252 :     mask_page_hint_bits(page);
    1137                 :      462252 :     mask_unused_space(page);
    1138                 :             : 
    1139                 :      462252 :     opaque = HashPageGetOpaque(page);
    1140                 :             : 
    1141                 :      462252 :     pagetype = opaque->hasho_flag & LH_PAGE_TYPE;
    1142         [ -  + ]:      462252 :     if (pagetype == LH_UNUSED_PAGE)
    1143                 :             :     {
    1144                 :             :         /*
    1145                 :             :          * Mask everything on a UNUSED page.
    1146                 :             :          */
    1147                 :           0 :         mask_page_content(page);
    1148                 :             :     }
    1149   [ +  +  +  + ]:      462252 :     else if (pagetype == LH_BUCKET_PAGE ||
    1150                 :             :              pagetype == LH_OVERFLOW_PAGE)
    1151                 :             :     {
    1152                 :             :         /*
    1153                 :             :          * In hash bucket and overflow pages, it is possible to modify the
    1154                 :             :          * LP_FLAGS without emitting any WAL record. Hence, mask the line
    1155                 :             :          * pointer flags. See hashgettuple(), _hash_kill_items() for details.
    1156                 :             :          */
    1157                 :      230046 :         mask_lp_flags(page);
    1158                 :             :     }
    1159                 :             : 
    1160                 :             :     /*
    1161                 :             :      * It is possible that the hint bit LH_PAGE_HAS_DEAD_TUPLES may remain
    1162                 :             :      * unlogged. So, mask it. See _hash_kill_items() for details.
    1163                 :             :      */
    1164                 :      462252 :     opaque->hasho_flag &= ~LH_PAGE_HAS_DEAD_TUPLES;
    1165                 :      462252 : }
        

Generated by: LCOV version 2.0-1