LCOV - code coverage report
Current view: top level - src/backend/access/heap - hio.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 89.2 % 297 265
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 5 5
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 70.4 % 230 162

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * hio.c
       4                 :             :  *        POSTGRES heap access method input/output code.
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  *
      10                 :             :  * IDENTIFICATION
      11                 :             :  *        src/backend/access/heap/hio.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : 
      16                 :             : #include "postgres.h"
      17                 :             : 
      18                 :             : #include "access/heapam.h"
      19                 :             : #include "access/hio.h"
      20                 :             : #include "access/htup_details.h"
      21                 :             : #include "access/visibilitymap.h"
      22                 :             : #include "storage/bufmgr.h"
      23                 :             : #include "storage/freespace.h"
      24                 :             : #include "storage/lmgr.h"
      25                 :             : 
      26                 :             : 
      27                 :             : /*
      28                 :             :  * RelationPutHeapTuple - place tuple at specified page
      29                 :             :  *
      30                 :             :  * !!! EREPORT(ERROR) IS DISALLOWED HERE !!!  Must PANIC on failure!!!
      31                 :             :  *
      32                 :             :  * Note - caller must hold BUFFER_LOCK_EXCLUSIVE on the buffer.
      33                 :             :  */
      34                 :             : void
      35                 :     1925001 : RelationPutHeapTuple(Relation relation,
      36                 :             :                                          Buffer buffer,
      37                 :             :                                          HeapTuple tuple,
      38                 :             :                                          bool token)
      39                 :             : {
      40                 :     1925001 :         Page            pageHeader;
      41                 :     1925001 :         OffsetNumber offnum;
      42                 :             : 
      43                 :             :         /*
      44                 :             :          * A tuple that's being inserted speculatively should already have its
      45                 :             :          * token set.
      46                 :             :          */
      47   [ +  +  +  - ]:     1925001 :         Assert(!token || HeapTupleHeaderIsSpeculative(tuple->t_data));
      48                 :             : 
      49                 :             :         /*
      50                 :             :          * Do not allow tuples with invalid combinations of hint bits to be placed
      51                 :             :          * on a page.  This combination is detected as corruption by the
      52                 :             :          * contrib/amcheck logic, so if you disable this assertion, make
      53                 :             :          * corresponding changes there.
      54                 :             :          */
      55   [ -  +  #  # ]:     1925001 :         Assert(!((tuple->t_data->t_infomask & HEAP_XMAX_COMMITTED) &&
      56                 :             :                          (tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI)));
      57                 :             : 
      58                 :             :         /* Add the tuple to the page */
      59                 :     1925001 :         pageHeader = BufferGetPage(buffer);
      60                 :             : 
      61                 :     1925001 :         offnum = PageAddItem(pageHeader, tuple->t_data, tuple->t_len, InvalidOffsetNumber, false, true);
      62         [ +  - ]:     1925001 :         if (offnum == InvalidOffsetNumber)
      63   [ #  #  #  # ]:           0 :                 elog(PANIC, "failed to add tuple to page");
      64                 :             : 
      65                 :             :         /* Update tuple->t_self to the actual position where it was stored */
      66                 :     1925001 :         ItemPointerSet(&(tuple->t_self), BufferGetBlockNumber(buffer), offnum);
      67                 :             : 
      68                 :             :         /*
      69                 :             :          * Insert the correct position into CTID of the stored tuple, too (unless
      70                 :             :          * this is a speculative insertion, in which case the token is held in
      71                 :             :          * CTID field instead)
      72                 :             :          */
      73         [ +  + ]:     1925001 :         if (!token)
      74                 :             :         {
      75                 :     1924914 :                 ItemId          itemId = PageGetItemId(pageHeader, offnum);
      76                 :     1924914 :                 HeapTupleHeader item = (HeapTupleHeader) PageGetItem(pageHeader, itemId);
      77                 :             : 
      78                 :     1924914 :                 item->t_ctid = tuple->t_self;
      79                 :     1924914 :         }
      80                 :     1925001 : }
      81                 :             : 
      82                 :             : /*
      83                 :             :  * Read in a buffer in mode, using bulk-insert strategy if bistate isn't NULL.
      84                 :             :  */
      85                 :             : static Buffer
      86                 :     1744173 : ReadBufferBI(Relation relation, BlockNumber targetBlock,
      87                 :             :                          ReadBufferMode mode, BulkInsertState bistate)
      88                 :             : {
      89                 :     1744173 :         Buffer          buffer;
      90                 :             : 
      91                 :             :         /* If not bulk-insert, exactly like ReadBuffer */
      92         [ +  + ]:     1744173 :         if (!bistate)
      93                 :     2805958 :                 return ReadBufferExtended(relation, MAIN_FORKNUM, targetBlock,
      94                 :     1402979 :                                                                   mode, NULL);
      95                 :             : 
      96                 :             :         /* If we have the desired block already pinned, re-pin and return it */
      97         [ +  + ]:      341194 :         if (bistate->current_buf != InvalidBuffer)
      98                 :             :         {
      99         [ +  + ]:      331140 :                 if (BufferGetBlockNumber(bistate->current_buf) == targetBlock)
     100                 :             :                 {
     101                 :             :                         /*
     102                 :             :                          * Currently the LOCK variants are only used for extending
     103                 :             :                          * relation, which should never reach this branch.
     104                 :             :                          */
     105         [ +  - ]:      327559 :                         Assert(mode != RBM_ZERO_AND_LOCK &&
     106                 :             :                                    mode != RBM_ZERO_AND_CLEANUP_LOCK);
     107                 :             : 
     108                 :      327559 :                         IncrBufferRefCount(bistate->current_buf);
     109                 :      327559 :                         return bistate->current_buf;
     110                 :             :                 }
     111                 :             :                 /* ... else drop the old buffer */
     112                 :        3581 :                 ReleaseBuffer(bistate->current_buf);
     113                 :        3581 :                 bistate->current_buf = InvalidBuffer;
     114                 :        3581 :         }
     115                 :             : 
     116                 :             :         /* Perform a read using the buffer strategy */
     117                 :       27270 :         buffer = ReadBufferExtended(relation, MAIN_FORKNUM, targetBlock,
     118                 :       13635 :                                                                 mode, bistate->strategy);
     119                 :             : 
     120                 :             :         /* Save the selected block as target for future inserts */
     121                 :       13635 :         IncrBufferRefCount(buffer);
     122                 :       13635 :         bistate->current_buf = buffer;
     123                 :             : 
     124                 :       13635 :         return buffer;
     125                 :     1744173 : }
     126                 :             : 
     127                 :             : /*
     128                 :             :  * For each heap page which is all-visible, acquire a pin on the appropriate
     129                 :             :  * visibility map page, if we haven't already got one.
     130                 :             :  *
     131                 :             :  * To avoid complexity in the callers, either buffer1 or buffer2 may be
     132                 :             :  * InvalidBuffer if only one buffer is involved. For the same reason, block2
     133                 :             :  * may be smaller than block1.
     134                 :             :  *
     135                 :             :  * Returns whether buffer locks were temporarily released.
     136                 :             :  */
     137                 :             : static bool
     138                 :     1751482 : GetVisibilityMapPins(Relation relation, Buffer buffer1, Buffer buffer2,
     139                 :             :                                          BlockNumber block1, BlockNumber block2,
     140                 :             :                                          Buffer *vmbuffer1, Buffer *vmbuffer2)
     141                 :             : {
     142                 :     1751482 :         bool            need_to_pin_buffer1;
     143                 :     1751482 :         bool            need_to_pin_buffer2;
     144                 :     1751482 :         bool            released_locks = false;
     145                 :             : 
     146                 :             :         /*
     147                 :             :          * Swap buffers around to handle case of a single block/buffer, and to
     148                 :             :          * handle if lock ordering rules require to lock block2 first.
     149                 :             :          */
     150   [ +  +  +  + ]:     1758667 :         if (!BufferIsValid(buffer1) ||
     151         [ +  + ]:     1751358 :                 (BufferIsValid(buffer2) && block1 > block2))
     152                 :             :         {
     153                 :        5334 :                 Buffer          tmpbuf = buffer1;
     154                 :        5334 :                 Buffer     *tmpvmbuf = vmbuffer1;
     155                 :        5334 :                 BlockNumber tmpblock = block1;
     156                 :             : 
     157                 :        5334 :                 buffer1 = buffer2;
     158                 :        5334 :                 vmbuffer1 = vmbuffer2;
     159                 :        5334 :                 block1 = block2;
     160                 :             : 
     161                 :        5334 :                 buffer2 = tmpbuf;
     162                 :        5334 :                 vmbuffer2 = tmpvmbuf;
     163                 :        5334 :                 block2 = tmpblock;
     164                 :        5334 :         }
     165                 :             : 
     166         [ +  - ]:     1751482 :         Assert(BufferIsValid(buffer1));
     167   [ +  +  +  - ]:     1751482 :         Assert(buffer2 == InvalidBuffer || block1 <= block2);
     168                 :             : 
     169                 :     1751482 :         while (1)
     170                 :             :         {
     171                 :             :                 /* Figure out which pins we need but don't have. */
     172                 :     1752186 :                 need_to_pin_buffer1 = PageIsAllVisible(BufferGetPage(buffer1))
     173         [ +  + ]:     1751482 :                         && !visibilitymap_pin_ok(block1, *vmbuffer1);
     174                 :     1758667 :                 need_to_pin_buffer2 = buffer2 != InvalidBuffer
     175         [ +  + ]:     1751482 :                         && PageIsAllVisible(BufferGetPage(buffer2))
     176         [ +  + ]:        7185 :                         && !visibilitymap_pin_ok(block2, *vmbuffer2);
     177   [ +  -  -  + ]:     1751482 :                 if (!need_to_pin_buffer1 && !need_to_pin_buffer2)
     178                 :     1751482 :                         break;
     179                 :             : 
     180                 :             :                 /* We must unlock both buffers before doing any I/O. */
     181                 :           0 :                 released_locks = true;
     182                 :           0 :                 LockBuffer(buffer1, BUFFER_LOCK_UNLOCK);
     183   [ #  #  #  # ]:           0 :                 if (buffer2 != InvalidBuffer && buffer2 != buffer1)
     184                 :           0 :                         LockBuffer(buffer2, BUFFER_LOCK_UNLOCK);
     185                 :             : 
     186                 :             :                 /* Get pins. */
     187         [ #  # ]:           0 :                 if (need_to_pin_buffer1)
     188                 :           0 :                         visibilitymap_pin(relation, block1, vmbuffer1);
     189         [ #  # ]:           0 :                 if (need_to_pin_buffer2)
     190                 :           0 :                         visibilitymap_pin(relation, block2, vmbuffer2);
     191                 :             : 
     192                 :             :                 /* Relock buffers. */
     193                 :           0 :                 LockBuffer(buffer1, BUFFER_LOCK_EXCLUSIVE);
     194   [ #  #  #  # ]:           0 :                 if (buffer2 != InvalidBuffer && buffer2 != buffer1)
     195                 :           0 :                         LockBuffer(buffer2, BUFFER_LOCK_EXCLUSIVE);
     196                 :             : 
     197                 :             :                 /*
     198                 :             :                  * If there are two buffers involved and we pinned just one of them,
     199                 :             :                  * it's possible that the second one became all-visible while we were
     200                 :             :                  * busy pinning the first one.  If it looks like that's a possible
     201                 :             :                  * scenario, we'll need to make a second pass through this loop.
     202                 :             :                  */
     203         [ #  # ]:           0 :                 if (buffer2 == InvalidBuffer || buffer1 == buffer2
     204   [ #  #  #  #  :           0 :                         || (need_to_pin_buffer1 && need_to_pin_buffer2))
                   #  # ]
     205                 :           0 :                         break;
     206                 :             :         }
     207                 :             : 
     208                 :     3502964 :         return released_locks;
     209                 :     1751482 : }
     210                 :             : 
     211                 :             : /*
     212                 :             :  * Extend the relation. By multiple pages, if beneficial.
     213                 :             :  *
     214                 :             :  * If the caller needs multiple pages (num_pages > 1), we always try to extend
     215                 :             :  * by at least that much.
     216                 :             :  *
     217                 :             :  * If there is contention on the extension lock, we don't just extend "for
     218                 :             :  * ourselves", but we try to help others. We can do so by adding empty pages
     219                 :             :  * into the FSM. Typically there is no contention when we can't use the FSM.
     220                 :             :  *
     221                 :             :  * We do have to limit the number of pages to extend by to some value, as the
     222                 :             :  * buffers for all the extended pages need to, temporarily, be pinned. For now
     223                 :             :  * we define MAX_BUFFERS_TO_EXTEND_BY to be 64 buffers, it's hard to see
     224                 :             :  * benefits with higher numbers. This partially is because copyfrom.c's
     225                 :             :  * MAX_BUFFERED_TUPLES / MAX_BUFFERED_BYTES prevents larger multi_inserts.
     226                 :             :  *
     227                 :             :  * Returns a buffer for a newly extended block. If possible, the buffer is
     228                 :             :  * returned exclusively locked. *did_unlock is set to true if the lock had to
     229                 :             :  * be released, false otherwise.
     230                 :             :  *
     231                 :             :  *
     232                 :             :  * XXX: It would likely be beneficial for some workloads to extend more
     233                 :             :  * aggressively, e.g. using a heuristic based on the relation size.
     234                 :             :  */
     235                 :             : static Buffer
     236                 :       18011 : RelationAddBlocks(Relation relation, BulkInsertState bistate,
     237                 :             :                                   int num_pages, bool use_fsm, bool *did_unlock)
     238                 :             : {
     239                 :             : #define MAX_BUFFERS_TO_EXTEND_BY 64
     240                 :       18011 :         Buffer          victim_buffers[MAX_BUFFERS_TO_EXTEND_BY];
     241                 :       18011 :         BlockNumber first_block = InvalidBlockNumber;
     242                 :       18011 :         BlockNumber last_block = InvalidBlockNumber;
     243                 :       18011 :         uint32          extend_by_pages;
     244                 :       18011 :         uint32          not_in_fsm_pages;
     245                 :       18011 :         Buffer          buffer;
     246                 :       18011 :         Page            page;
     247                 :             : 
     248                 :             :         /*
     249                 :             :          * Determine by how many pages to try to extend by.
     250                 :             :          */
     251   [ +  +  +  + ]:       18011 :         if (bistate == NULL && !use_fsm)
     252                 :             :         {
     253                 :             :                 /*
     254                 :             :                  * If we have neither bistate, nor can use the FSM, we can't bulk
     255                 :             :                  * extend - there'd be no way to find the additional pages.
     256                 :             :                  */
     257                 :          28 :                 extend_by_pages = 1;
     258                 :          28 :         }
     259                 :             :         else
     260                 :             :         {
     261                 :       17983 :                 uint32          waitcount;
     262                 :             : 
     263                 :             :                 /*
     264                 :             :                  * Try to extend at least by the number of pages the caller needs. We
     265                 :             :                  * can remember the additional pages (either via FSM or bistate).
     266                 :             :                  */
     267                 :       17983 :                 extend_by_pages = num_pages;
     268                 :             : 
     269   [ +  +  +  + ]:       17983 :                 if (!RELATION_IS_LOCAL(relation))
     270                 :       13530 :                         waitcount = RelationExtensionLockWaiterCount(relation);
     271                 :             :                 else
     272                 :        4453 :                         waitcount = 0;
     273                 :             : 
     274                 :             :                 /*
     275                 :             :                  * Multiply the number of pages to extend by the number of waiters. Do
     276                 :             :                  * this even if we're not using the FSM, as it still relieves
     277                 :             :                  * contention, by deferring the next time this backend needs to
     278                 :             :                  * extend. In that case the extended pages will be found via
     279                 :             :                  * bistate->next_free.
     280                 :             :                  */
     281                 :       17983 :                 extend_by_pages += extend_by_pages * waitcount;
     282                 :             : 
     283                 :             :                 /* ---
     284                 :             :                  * If we previously extended using the same bistate, it's very likely
     285                 :             :                  * we'll extend some more. Try to extend by as many pages as
     286                 :             :                  * before. This can be important for performance for several reasons,
     287                 :             :                  * including:
     288                 :             :                  *
     289                 :             :                  * - It prevents mdzeroextend() switching between extending the
     290                 :             :                  *   relation in different ways, which is inefficient for some
     291                 :             :                  *   filesystems.
     292                 :             :                  *
     293                 :             :                  * - Contention is often intermittent. Even if we currently don't see
     294                 :             :                  *   other waiters (see above), extending by larger amounts can
     295                 :             :                  *   prevent future contention.
     296                 :             :                  * ---
     297                 :             :                  */
     298         [ +  + ]:       17983 :                 if (bistate)
     299         [ +  + ]:         802 :                         extend_by_pages = Max(extend_by_pages, bistate->already_extended_by);
     300                 :             : 
     301                 :             :                 /*
     302                 :             :                  * Can't extend by more than MAX_BUFFERS_TO_EXTEND_BY, we need to pin
     303                 :             :                  * them all concurrently.
     304                 :             :                  */
     305         [ +  + ]:       17983 :                 extend_by_pages = Min(extend_by_pages, MAX_BUFFERS_TO_EXTEND_BY);
     306                 :       17983 :         }
     307                 :             : 
     308                 :             :         /*
     309                 :             :          * How many of the extended pages should be entered into the FSM?
     310                 :             :          *
     311                 :             :          * If we have a bistate, only enter pages that we don't need ourselves
     312                 :             :          * into the FSM.  Otherwise every other backend will immediately try to
     313                 :             :          * use the pages this backend needs for itself, causing unnecessary
     314                 :             :          * contention.  If we don't have a bistate, we can't avoid the FSM.
     315                 :             :          *
     316                 :             :          * Never enter the page returned into the FSM, we'll immediately use it.
     317                 :             :          */
     318   [ +  +  +  + ]:       18011 :         if (num_pages > 1 && bistate == NULL)
     319                 :           3 :                 not_in_fsm_pages = 1;
     320                 :             :         else
     321                 :       18008 :                 not_in_fsm_pages = num_pages;
     322                 :             : 
     323                 :             :         /* prepare to put another buffer into the bistate */
     324   [ +  +  +  + ]:       18011 :         if (bistate && bistate->current_buf != InvalidBuffer)
     325                 :             :         {
     326                 :         350 :                 ReleaseBuffer(bistate->current_buf);
     327                 :         350 :                 bistate->current_buf = InvalidBuffer;
     328                 :         350 :         }
     329                 :             : 
     330                 :             :         /*
     331                 :             :          * Extend the relation. We ask for the first returned page to be locked,
     332                 :             :          * so that we are sure that nobody has inserted into the page
     333                 :             :          * concurrently.
     334                 :             :          *
     335                 :             :          * With the current MAX_BUFFERS_TO_EXTEND_BY there's no danger of
     336                 :             :          * [auto]vacuum trying to truncate later pages as REL_TRUNCATE_MINIMUM is
     337                 :             :          * way larger.
     338                 :             :          */
     339                 :       36022 :         first_block = ExtendBufferedRelBy(BMR_REL(relation), MAIN_FORKNUM,
     340         [ +  + ]:       18011 :                                                                           bistate ? bistate->strategy : NULL,
     341                 :             :                                                                           EB_LOCK_FIRST,
     342                 :       18011 :                                                                           extend_by_pages,
     343                 :       18011 :                                                                           victim_buffers,
     344                 :             :                                                                           &extend_by_pages);
     345                 :       18011 :         buffer = victim_buffers[0]; /* the buffer the function will return */
     346                 :       18011 :         last_block = first_block + (extend_by_pages - 1);
     347         [ +  - ]:       18011 :         Assert(first_block == BufferGetBlockNumber(buffer));
     348                 :             : 
     349                 :             :         /*
     350                 :             :          * Relation is now extended. Initialize the page. We do this here, before
     351                 :             :          * potentially releasing the lock on the page, because it allows us to
     352                 :             :          * double check that the page contents are empty (this should never
     353                 :             :          * happen, but if it does we don't want to risk wiping out valid data).
     354                 :             :          */
     355                 :       18011 :         page = BufferGetPage(buffer);
     356         [ +  - ]:       18011 :         if (!PageIsNew(page))
     357   [ #  #  #  # ]:           0 :                 elog(ERROR, "page %u of relation \"%s\" should be empty but is not",
     358                 :             :                          first_block,
     359                 :             :                          RelationGetRelationName(relation));
     360                 :             : 
     361                 :       18011 :         PageInit(page, BufferGetPageSize(buffer), 0);
     362                 :       18011 :         MarkBufferDirty(buffer);
     363                 :             : 
     364                 :             :         /*
     365                 :             :          * If we decided to put pages into the FSM, release the buffer lock (but
     366                 :             :          * not pin), we don't want to do IO while holding a buffer lock. This will
     367                 :             :          * necessitate a bit more extensive checking in our caller.
     368                 :             :          */
     369   [ +  +  +  + ]:       18011 :         if (use_fsm && not_in_fsm_pages < extend_by_pages)
     370                 :             :         {
     371                 :          61 :                 LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
     372                 :          61 :                 *did_unlock = true;
     373                 :          61 :         }
     374                 :             :         else
     375                 :       17950 :                 *did_unlock = false;
     376                 :             : 
     377                 :             :         /*
     378                 :             :          * Relation is now extended. Release pins on all buffers, except for the
     379                 :             :          * first (which we'll return).  If we decided to put pages into the FSM,
     380                 :             :          * we can do that as part of the same loop.
     381                 :             :          */
     382         [ +  + ]:       22913 :         for (uint32 i = 1; i < extend_by_pages; i++)
     383                 :             :         {
     384                 :        4902 :                 BlockNumber curBlock = first_block + i;
     385                 :             : 
     386         [ +  - ]:        4902 :                 Assert(curBlock == BufferGetBlockNumber(victim_buffers[i]));
     387         [ +  - ]:        4902 :                 Assert(BlockNumberIsValid(curBlock));
     388                 :             : 
     389                 :        4902 :                 ReleaseBuffer(victim_buffers[i]);
     390                 :             : 
     391   [ +  +  +  + ]:        4902 :                 if (use_fsm && i >= not_in_fsm_pages)
     392                 :             :                 {
     393                 :        1115 :                         Size            freespace = BufferGetPageSize(victim_buffers[i]) -
     394                 :             :                                 SizeOfPageHeaderData;
     395                 :             : 
     396                 :        1115 :                         RecordPageWithFreeSpace(relation, curBlock, freespace);
     397                 :        1115 :                 }
     398                 :        4902 :         }
     399                 :             : 
     400   [ +  +  +  + ]:       18011 :         if (use_fsm && not_in_fsm_pages < extend_by_pages)
     401                 :             :         {
     402                 :          61 :                 BlockNumber first_fsm_block = first_block + not_in_fsm_pages;
     403                 :             : 
     404                 :          61 :                 FreeSpaceMapVacuumRange(relation, first_fsm_block, last_block);
     405                 :          61 :         }
     406                 :             : 
     407         [ +  + ]:       18011 :         if (bistate)
     408                 :             :         {
     409                 :             :                 /*
     410                 :             :                  * Remember the additional pages we extended by, so we later can use
     411                 :             :                  * them without looking into the FSM.
     412                 :             :                  */
     413         [ +  + ]:         802 :                 if (extend_by_pages > 1)
     414                 :             :                 {
     415                 :         309 :                         bistate->next_free = first_block + 1;
     416                 :         309 :                         bistate->last_free = last_block;
     417                 :         309 :                 }
     418                 :             :                 else
     419                 :             :                 {
     420                 :         493 :                         bistate->next_free = InvalidBlockNumber;
     421                 :         493 :                         bistate->last_free = InvalidBlockNumber;
     422                 :             :                 }
     423                 :             : 
     424                 :             :                 /* maintain bistate->current_buf */
     425                 :         802 :                 IncrBufferRefCount(buffer);
     426                 :         802 :                 bistate->current_buf = buffer;
     427                 :         802 :                 bistate->already_extended_by += extend_by_pages;
     428                 :         802 :         }
     429                 :             : 
     430                 :       36022 :         return buffer;
     431                 :             : #undef MAX_BUFFERS_TO_EXTEND_BY
     432                 :       18011 : }
     433                 :             : 
     434                 :             : /*
     435                 :             :  * RelationGetBufferForTuple
     436                 :             :  *
     437                 :             :  *      Returns pinned and exclusive-locked buffer of a page in given relation
     438                 :             :  *      with free space >= given len.
     439                 :             :  *
     440                 :             :  *      If num_pages is > 1, we will try to extend the relation by at least that
     441                 :             :  *      many pages when we decide to extend the relation. This is more efficient
     442                 :             :  *      for callers that know they will need multiple pages
     443                 :             :  *      (e.g. heap_multi_insert()).
     444                 :             :  *
     445                 :             :  *      If otherBuffer is not InvalidBuffer, then it references a previously
     446                 :             :  *      pinned buffer of another page in the same relation; on return, this
     447                 :             :  *      buffer will also be exclusive-locked.  (This case is used by heap_update;
     448                 :             :  *      the otherBuffer contains the tuple being updated.)
     449                 :             :  *
     450                 :             :  *      The reason for passing otherBuffer is that if two backends are doing
     451                 :             :  *      concurrent heap_update operations, a deadlock could occur if they try
     452                 :             :  *      to lock the same two buffers in opposite orders.  To ensure that this
     453                 :             :  *      can't happen, we impose the rule that buffers of a relation must be
     454                 :             :  *      locked in increasing page number order.  This is most conveniently done
     455                 :             :  *      by having RelationGetBufferForTuple lock them both, with suitable care
     456                 :             :  *      for ordering.
     457                 :             :  *
     458                 :             :  *      NOTE: it is unlikely, but not quite impossible, for otherBuffer to be the
     459                 :             :  *      same buffer we select for insertion of the new tuple (this could only
     460                 :             :  *      happen if space is freed in that page after heap_update finds there's not
     461                 :             :  *      enough there).  In that case, the page will be pinned and locked only once.
     462                 :             :  *
     463                 :             :  *      We also handle the possibility that the all-visible flag will need to be
     464                 :             :  *      cleared on one or both pages.  If so, pin on the associated visibility map
     465                 :             :  *      page must be acquired before acquiring buffer lock(s), to avoid possibly
     466                 :             :  *      doing I/O while holding buffer locks.  The pins are passed back to the
     467                 :             :  *      caller using the input-output arguments vmbuffer and vmbuffer_other.
     468                 :             :  *      Note that in some cases the caller might have already acquired such pins,
     469                 :             :  *      which is indicated by these arguments not being InvalidBuffer on entry.
     470                 :             :  *
     471                 :             :  *      We normally use FSM to help us find free space.  However,
     472                 :             :  *      if HEAP_INSERT_SKIP_FSM is specified, we just append a new empty page to
     473                 :             :  *      the end of the relation if the tuple won't fit on the current target page.
     474                 :             :  *      This can save some cycles when we know the relation is new and doesn't
     475                 :             :  *      contain useful amounts of free space.
     476                 :             :  *
     477                 :             :  *      HEAP_INSERT_SKIP_FSM is also useful for non-WAL-logged additions to a
     478                 :             :  *      relation, if the caller holds exclusive lock and is careful to invalidate
     479                 :             :  *      relation's smgr_targblock before the first insertion --- that ensures that
     480                 :             :  *      all insertions will occur into newly added pages and not be intermixed
     481                 :             :  *      with tuples from other transactions.  That way, a crash can't risk losing
     482                 :             :  *      any committed data of other transactions.  (See heap_insert's comments
     483                 :             :  *      for additional constraints needed for safe usage of this behavior.)
     484                 :             :  *
     485                 :             :  *      The caller can also provide a BulkInsertState object to optimize many
     486                 :             :  *      insertions into the same relation.  This keeps a pin on the current
     487                 :             :  *      insertion target page (to save pin/unpin cycles) and also passes a
     488                 :             :  *      BULKWRITE buffer selection strategy object to the buffer manager.
     489                 :             :  *      Passing NULL for bistate selects the default behavior.
     490                 :             :  *
     491                 :             :  *      We don't fill existing pages further than the fillfactor, except for large
     492                 :             :  *      tuples in nearly-empty pages.  This is OK since this routine is not
     493                 :             :  *      consulted when updating a tuple and keeping it on the same page, which is
     494                 :             :  *      the scenario fillfactor is meant to reserve space for.
     495                 :             :  *
     496                 :             :  *      ereport(ERROR) is allowed here, so this routine *must* be called
     497                 :             :  *      before any (unlogged) changes are made in buffer pool.
     498                 :             :  */
     499                 :             : Buffer
     500                 :     1748859 : RelationGetBufferForTuple(Relation relation, Size len,
     501                 :             :                                                   Buffer otherBuffer, int options,
     502                 :             :                                                   BulkInsertState bistate,
     503                 :             :                                                   Buffer *vmbuffer, Buffer *vmbuffer_other,
     504                 :             :                                                   int num_pages)
     505                 :             : {
     506                 :     1748859 :         bool            use_fsm = !(options & HEAP_INSERT_SKIP_FSM);
     507                 :     1748859 :         Buffer          buffer = InvalidBuffer;
     508                 :     1748859 :         Page            page;
     509                 :     5246577 :         Size            nearlyEmptyFreeSpace,
     510                 :     1748859 :                                 pageFreeSpace = 0,
     511                 :     1748859 :                                 saveFreeSpace = 0,
     512                 :     1748859 :                                 targetFreeSpace = 0;
     513                 :     1748859 :         BlockNumber targetBlock,
     514                 :             :                                 otherBlock;
     515                 :     1748859 :         bool            unlockedTargetBuffer;
     516                 :     1748859 :         bool            recheckVmPins;
     517                 :             : 
     518                 :     1748859 :         len = MAXALIGN(len);            /* be conservative */
     519                 :             : 
     520                 :             :         /* if the caller doesn't know by how many pages to extend, extend by 1 */
     521         [ +  + ]:     1748859 :         if (num_pages <= 0)
     522                 :     1686603 :                 num_pages = 1;
     523                 :             : 
     524                 :             :         /* Bulk insert is not supported for updates, only inserts. */
     525   [ +  +  +  - ]:     1748859 :         Assert(otherBuffer == InvalidBuffer || !bistate);
     526                 :             : 
     527                 :             :         /*
     528                 :             :          * If we're gonna fail for oversize tuple, do it right away
     529                 :             :          */
     530         [ +  - ]:     1748859 :         if (len > MaxHeapTupleSize)
     531   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     532                 :             :                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     533                 :             :                                  errmsg("row is too big: size %zu, maximum size %zu",
     534                 :             :                                                 len, MaxHeapTupleSize)));
     535                 :             : 
     536                 :             :         /* Compute desired extra freespace due to fillfactor option */
     537         [ +  + ]:     1748859 :         saveFreeSpace = RelationGetTargetPageFreeSpace(relation,
     538                 :             :                                                                                                    HEAP_DEFAULT_FILLFACTOR);
     539                 :             : 
     540                 :             :         /*
     541                 :             :          * Since pages without tuples can still have line pointers, we consider
     542                 :             :          * pages "empty" when the unavailable space is slight.  This threshold is
     543                 :             :          * somewhat arbitrary, but it should prevent most unnecessary relation
     544                 :             :          * extensions while inserting large tuples into low-fillfactor tables.
     545                 :             :          */
     546                 :     1748859 :         nearlyEmptyFreeSpace = MaxHeapTupleSize -
     547                 :             :                 (MaxHeapTuplesPerPage / 8 * sizeof(ItemIdData));
     548         [ +  + ]:     1748859 :         if (len + saveFreeSpace > nearlyEmptyFreeSpace)
     549         [ +  + ]:        1012 :                 targetFreeSpace = Max(len, nearlyEmptyFreeSpace);
     550                 :             :         else
     551                 :     1747847 :                 targetFreeSpace = len + saveFreeSpace;
     552                 :             : 
     553         [ +  + ]:     1748859 :         if (otherBuffer != InvalidBuffer)
     554                 :        6576 :                 otherBlock = BufferGetBlockNumber(otherBuffer);
     555                 :             :         else
     556                 :     1742283 :                 otherBlock = InvalidBlockNumber;        /* just to keep compiler quiet */
     557                 :             : 
     558                 :             :         /*
     559                 :             :          * We first try to put the tuple on the same page we last inserted a tuple
     560                 :             :          * on, as cached in the BulkInsertState or relcache entry.  If that
     561                 :             :          * doesn't work, we ask the Free Space Map to locate a suitable page.
     562                 :             :          * Since the FSM's info might be out of date, we have to be prepared to
     563                 :             :          * loop around and retry multiple times. (To ensure this isn't an infinite
     564                 :             :          * loop, we must update the FSM with the correct amount of free space on
     565                 :             :          * each page that proves not to be suitable.)  If the FSM has no record of
     566                 :             :          * a page with enough free space, we give up and extend the relation.
     567                 :             :          *
     568                 :             :          * When use_fsm is false, we either put the tuple onto the existing target
     569                 :             :          * page or extend the relation.
     570                 :             :          */
     571   [ +  +  +  + ]:     1748859 :         if (bistate && bistate->current_buf != InvalidBuffer)
     572                 :      327559 :                 targetBlock = BufferGetBlockNumber(bistate->current_buf);
     573                 :             :         else
     574         [ +  + ]:     1421300 :                 targetBlock = RelationGetTargetBlock(relation);
     575                 :             : 
     576   [ +  +  +  + ]:     1748859 :         if (targetBlock == InvalidBlockNumber && use_fsm)
     577                 :             :         {
     578                 :             :                 /*
     579                 :             :                  * We have no cached target page, so ask the FSM for an initial
     580                 :             :                  * target.
     581                 :             :                  */
     582                 :        5454 :                 targetBlock = GetPageWithFreeSpace(relation, targetFreeSpace);
     583                 :        5454 :         }
     584                 :             : 
     585                 :             :         /*
     586                 :             :          * If the FSM knows nothing of the rel, try the last page before we give
     587                 :             :          * up and extend.  This avoids one-tuple-per-page syndrome during
     588                 :             :          * bootstrapping or in a recently-started system.
     589                 :             :          */
     590         [ +  + ]:     1753609 :         if (targetBlock == InvalidBlockNumber)
     591                 :             :         {
     592                 :        4750 :                 BlockNumber nblocks = RelationGetNumberOfBlocks(relation);
     593                 :             : 
     594         [ +  + ]:        4750 :                 if (nblocks > 0)
     595                 :        2065 :                         targetBlock = nblocks - 1;
     596                 :        4750 :         }
     597                 :             : 
     598                 :             : loop:
     599         [ +  + ]:     1768606 :         while (targetBlock != InvalidBlockNumber)
     600                 :             :         {
     601                 :             :                 /*
     602                 :             :                  * Read and exclusive-lock the target block, as well as the other
     603                 :             :                  * block if one was given, taking suitable care with lock ordering and
     604                 :             :                  * the possibility they are the same block.
     605                 :             :                  *
     606                 :             :                  * If the page-level all-visible flag is set, caller will need to
     607                 :             :                  * clear both that and the corresponding visibility map bit.  However,
     608                 :             :                  * by the time we return, we'll have x-locked the buffer, and we don't
     609                 :             :                  * want to do any I/O while in that state.  So we check the bit here
     610                 :             :                  * before taking the lock, and pin the page if it appears necessary.
     611                 :             :                  * Checking without the lock creates a risk of getting the wrong
     612                 :             :                  * answer, so we'll have to recheck after acquiring the lock.
     613                 :             :                  */
     614         [ +  + ]:     1750918 :                 if (otherBuffer == InvalidBuffer)
     615                 :             :                 {
     616                 :             :                         /* easy case */
     617                 :     1744173 :                         buffer = ReadBufferBI(relation, targetBlock, RBM_NORMAL, bistate);
     618         [ +  + ]:     1744173 :                         if (PageIsAllVisible(BufferGetPage(buffer)))
     619                 :         650 :                                 visibilitymap_pin(relation, targetBlock, vmbuffer);
     620                 :             : 
     621                 :             :                         /*
     622                 :             :                          * If the page is empty, pin vmbuffer to set all_frozen bit later.
     623                 :             :                          */
     624   [ +  +  +  - ]:     1744173 :                         if ((options & HEAP_INSERT_FROZEN) &&
     625                 :         495 :                                 (PageGetMaxOffsetNumber(BufferGetPage(buffer)) == 0))
     626                 :           0 :                                 visibilitymap_pin(relation, targetBlock, vmbuffer);
     627                 :             : 
     628                 :     1744173 :                         LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
     629                 :     1744173 :                 }
     630         [ +  + ]:        6745 :                 else if (otherBlock == targetBlock)
     631                 :             :                 {
     632                 :             :                         /* also easy case */
     633                 :         401 :                         buffer = otherBuffer;
     634         [ +  - ]:         401 :                         if (PageIsAllVisible(BufferGetPage(buffer)))
     635                 :           0 :                                 visibilitymap_pin(relation, targetBlock, vmbuffer);
     636                 :         401 :                         LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
     637                 :         401 :                 }
     638         [ +  + ]:        6344 :                 else if (otherBlock < targetBlock)
     639                 :             :                 {
     640                 :             :                         /* lock other buffer first */
     641                 :        5210 :                         buffer = ReadBuffer(relation, targetBlock);
     642         [ +  + ]:        5210 :                         if (PageIsAllVisible(BufferGetPage(buffer)))
     643                 :          34 :                                 visibilitymap_pin(relation, targetBlock, vmbuffer);
     644                 :        5210 :                         LockBuffer(otherBuffer, BUFFER_LOCK_EXCLUSIVE);
     645                 :        5210 :                         LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
     646                 :        5210 :                 }
     647                 :             :                 else
     648                 :             :                 {
     649                 :             :                         /* lock target buffer first */
     650                 :        1134 :                         buffer = ReadBuffer(relation, targetBlock);
     651         [ +  + ]:        1134 :                         if (PageIsAllVisible(BufferGetPage(buffer)))
     652                 :          10 :                                 visibilitymap_pin(relation, targetBlock, vmbuffer);
     653                 :        1134 :                         LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
     654                 :        1134 :                         LockBuffer(otherBuffer, BUFFER_LOCK_EXCLUSIVE);
     655                 :             :                 }
     656                 :             : 
     657                 :             :                 /*
     658                 :             :                  * We now have the target page (and the other buffer, if any) pinned
     659                 :             :                  * and locked.  However, since our initial PageIsAllVisible checks
     660                 :             :                  * were performed before acquiring the lock, the results might now be
     661                 :             :                  * out of date, either for the selected victim buffer, or for the
     662                 :             :                  * other buffer passed by the caller.  In that case, we'll need to
     663                 :             :                  * give up our locks, go get the pin(s) we failed to get earlier, and
     664                 :             :                  * re-lock.  That's pretty painful, but hopefully shouldn't happen
     665                 :             :                  * often.
     666                 :             :                  *
     667                 :             :                  * Note that there's a small possibility that we didn't pin the page
     668                 :             :                  * above but still have the correct page pinned anyway, either because
     669                 :             :                  * we've already made a previous pass through this loop, or because
     670                 :             :                  * caller passed us the right page anyway.
     671                 :             :                  *
     672                 :             :                  * Note also that it's possible that by the time we get the pin and
     673                 :             :                  * retake the buffer locks, the visibility map bit will have been
     674                 :             :                  * cleared by some other backend anyway.  In that case, we'll have
     675                 :             :                  * done a bit of extra work for no gain, but there's no real harm
     676                 :             :                  * done.
     677                 :             :                  */
     678                 :     3501836 :                 GetVisibilityMapPins(relation, buffer, otherBuffer,
     679                 :     1750918 :                                                          targetBlock, otherBlock, vmbuffer,
     680                 :     1750918 :                                                          vmbuffer_other);
     681                 :             : 
     682                 :             :                 /*
     683                 :             :                  * Now we can check to see if there's enough free space here. If so,
     684                 :             :                  * we're done.
     685                 :             :                  */
     686                 :     1750918 :                 page = BufferGetPage(buffer);
     687                 :             : 
     688                 :             :                 /*
     689                 :             :                  * If necessary initialize page, it'll be used soon.  We could avoid
     690                 :             :                  * dirtying the buffer here, and rely on the caller to do so whenever
     691                 :             :                  * it puts a tuple onto the page, but there seems not much benefit in
     692                 :             :                  * doing so.
     693                 :             :                  */
     694         [ +  + ]:     1750918 :                 if (PageIsNew(page))
     695                 :             :                 {
     696                 :        3613 :                         PageInit(page, BufferGetPageSize(buffer), 0);
     697                 :        3613 :                         MarkBufferDirty(buffer);
     698                 :        3613 :                 }
     699                 :             : 
     700                 :     1750918 :                 pageFreeSpace = PageGetHeapFreeSpace(page);
     701         [ +  + ]:     1750918 :                 if (targetFreeSpace <= pageFreeSpace)
     702                 :             :                 {
     703                 :             :                         /* use this page as future insert target, too */
     704                 :     1730848 :                         RelationSetTargetBlock(relation, targetBlock);
     705                 :     1730848 :                         return buffer;
     706                 :             :                 }
     707                 :             : 
     708                 :             :                 /*
     709                 :             :                  * Not enough space, so we must give up our page locks and pin (if
     710                 :             :                  * any) and prepare to look elsewhere.  We don't care which order we
     711                 :             :                  * unlock the two buffers in, so this can be slightly simpler than the
     712                 :             :                  * code above.
     713                 :             :                  */
     714                 :       20070 :                 LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
     715         [ +  + ]:       20070 :                 if (otherBuffer == InvalidBuffer)
     716                 :       19461 :                         ReleaseBuffer(buffer);
     717         [ +  + ]:         609 :                 else if (otherBlock != targetBlock)
     718                 :             :                 {
     719                 :         208 :                         LockBuffer(otherBuffer, BUFFER_LOCK_UNLOCK);
     720                 :         208 :                         ReleaseBuffer(buffer);
     721                 :         208 :                 }
     722                 :             : 
     723                 :             :                 /* Is there an ongoing bulk extension? */
     724   [ +  +  +  + ]:       20070 :                 if (bistate && bistate->next_free != InvalidBlockNumber)
     725                 :             :                 {
     726         [ +  - ]:        3523 :                         Assert(bistate->next_free <= bistate->last_free);
     727                 :             : 
     728                 :             :                         /*
     729                 :             :                          * We bulk extended the relation before, and there are still some
     730                 :             :                          * unused pages from that extension, so we don't need to look in
     731                 :             :                          * the FSM for a new page. But do record the free space from the
     732                 :             :                          * last page, somebody might insert narrower tuples later.
     733                 :             :                          */
     734         [ +  + ]:        3523 :                         if (use_fsm)
     735                 :        1070 :                                 RecordPageWithFreeSpace(relation, targetBlock, pageFreeSpace);
     736                 :             : 
     737                 :        3523 :                         targetBlock = bistate->next_free;
     738         [ +  + ]:        3523 :                         if (bistate->next_free >= bistate->last_free)
     739                 :             :                         {
     740                 :         237 :                                 bistate->next_free = InvalidBlockNumber;
     741                 :         237 :                                 bistate->last_free = InvalidBlockNumber;
     742                 :         237 :                         }
     743                 :             :                         else
     744                 :        3286 :                                 bistate->next_free++;
     745                 :        3523 :                 }
     746         [ +  + ]:       16547 :                 else if (!use_fsm)
     747                 :             :                 {
     748                 :             :                         /* Without FSM, always fall out of the loop and extend */
     749                 :         323 :                         break;
     750                 :             :                 }
     751                 :             :                 else
     752                 :             :                 {
     753                 :             :                         /*
     754                 :             :                          * Update FSM as to condition of this page, and ask for another
     755                 :             :                          * page to try.
     756                 :             :                          */
     757                 :       32448 :                         targetBlock = RecordAndGetPageWithFreeSpace(relation,
     758                 :       16224 :                                                                                                                 targetBlock,
     759                 :       16224 :                                                                                                                 pageFreeSpace,
     760                 :       16224 :                                                                                                                 targetFreeSpace);
     761                 :             :                 }
     762                 :             :         }
     763                 :             : 
     764                 :             :         /* Have to extend the relation */
     765                 :       18011 :         buffer = RelationAddBlocks(relation, bistate, num_pages, use_fsm,
     766                 :             :                                                            &unlockedTargetBuffer);
     767                 :             : 
     768                 :       18011 :         targetBlock = BufferGetBlockNumber(buffer);
     769                 :       18011 :         page = BufferGetPage(buffer);
     770                 :             : 
     771                 :             :         /*
     772                 :             :          * The page is empty, pin vmbuffer to set all_frozen bit. We don't want to
     773                 :             :          * do IO while the buffer is locked, so we unlock the page first if IO is
     774                 :             :          * needed (necessitating checks below).
     775                 :             :          */
     776         [ +  + ]:       18011 :         if (options & HEAP_INSERT_FROZEN)
     777                 :             :         {
     778         [ +  - ]:          65 :                 Assert(PageGetMaxOffsetNumber(page) == 0);
     779                 :             : 
     780         [ -  + ]:          65 :                 if (!visibilitymap_pin_ok(targetBlock, *vmbuffer))
     781                 :             :                 {
     782         [ -  + ]:          65 :                         if (!unlockedTargetBuffer)
     783                 :          65 :                                 LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
     784                 :          65 :                         unlockedTargetBuffer = true;
     785                 :          65 :                         visibilitymap_pin(relation, targetBlock, vmbuffer);
     786                 :          65 :                 }
     787                 :          65 :         }
     788                 :             : 
     789                 :             :         /*
     790                 :             :          * Reacquire locks if necessary.
     791                 :             :          *
     792                 :             :          * If the target buffer was unlocked above, or is unlocked while
     793                 :             :          * reacquiring the lock on otherBuffer below, it's unlikely, but possible,
     794                 :             :          * that another backend used space on this page. We check for that below,
     795                 :             :          * and retry if necessary.
     796                 :             :          */
     797                 :       18011 :         recheckVmPins = false;
     798         [ +  + ]:       18011 :         if (unlockedTargetBuffer)
     799                 :             :         {
     800                 :             :                 /* released lock on target buffer above */
     801         [ +  + ]:         126 :                 if (otherBuffer != InvalidBuffer)
     802                 :           2 :                         LockBuffer(otherBuffer, BUFFER_LOCK_EXCLUSIVE);
     803                 :         126 :                 LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
     804                 :         126 :                 recheckVmPins = true;
     805                 :         126 :         }
     806         [ +  + ]:       17885 :         else if (otherBuffer != InvalidBuffer)
     807                 :             :         {
     808                 :             :                 /*
     809                 :             :                  * We did not release the target buffer, and otherBuffer is valid,
     810                 :             :                  * need to lock the other buffer. It's guaranteed to be of a lower
     811                 :             :                  * page number than the new page.  To conform with the deadlock
     812                 :             :                  * prevent rules, we ought to lock otherBuffer first, but that would
     813                 :             :                  * give other backends a chance to put tuples on our page. To reduce
     814                 :             :                  * the likelihood of that, attempt to lock the other buffer
     815                 :             :                  * conditionally, that's very likely to work.
     816                 :             :                  *
     817                 :             :                  * Alternatively, we could acquire the lock on otherBuffer before
     818                 :             :                  * extending the relation, but that'd require holding the lock while
     819                 :             :                  * performing IO, which seems worse than an unlikely retry.
     820                 :             :                  */
     821         [ +  - ]:         438 :                 Assert(otherBuffer != buffer);
     822         [ +  - ]:         438 :                 Assert(targetBlock > otherBlock);
     823                 :             : 
     824         [ +  - ]:         438 :                 if (unlikely(!ConditionalLockBuffer(otherBuffer)))
     825                 :             :                 {
     826                 :           0 :                         unlockedTargetBuffer = true;
     827                 :           0 :                         LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
     828                 :           0 :                         LockBuffer(otherBuffer, BUFFER_LOCK_EXCLUSIVE);
     829                 :           0 :                         LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
     830                 :           0 :                 }
     831                 :         438 :                 recheckVmPins = true;
     832                 :         438 :         }
     833                 :             : 
     834                 :             :         /*
     835                 :             :          * If one of the buffers was unlocked (always the case if otherBuffer is
     836                 :             :          * valid), it's possible, although unlikely, that an all-visible flag
     837                 :             :          * became set.  We can use GetVisibilityMapPins to deal with that. It's
     838                 :             :          * possible that GetVisibilityMapPins() might need to temporarily release
     839                 :             :          * buffer locks, in which case we'll need to check if there's still enough
     840                 :             :          * space on the page below.
     841                 :             :          */
     842         [ +  + ]:       18011 :         if (recheckVmPins)
     843                 :             :         {
     844   [ +  -  +  - ]:        1128 :                 if (GetVisibilityMapPins(relation, otherBuffer, buffer,
     845                 :         564 :                                                                  otherBlock, targetBlock, vmbuffer_other,
     846                 :         564 :                                                                  vmbuffer))
     847                 :           0 :                         unlockedTargetBuffer = true;
     848                 :         564 :         }
     849                 :             : 
     850                 :             :         /*
     851                 :             :          * If the target buffer was temporarily unlocked since the relation
     852                 :             :          * extension, it's possible, although unlikely, that all the space on the
     853                 :             :          * page was already used. If so, we just retry from the start.  If we
     854                 :             :          * didn't unlock, something has gone wrong if there's not enough space -
     855                 :             :          * the test at the top should have prevented reaching this case.
     856                 :             :          */
     857                 :       18011 :         pageFreeSpace = PageGetHeapFreeSpace(page);
     858         [ +  - ]:       18011 :         if (len > pageFreeSpace)
     859                 :             :         {
     860         [ #  # ]:           0 :                 if (unlockedTargetBuffer)
     861                 :             :                 {
     862         [ #  # ]:           0 :                         if (otherBuffer != InvalidBuffer)
     863                 :           0 :                                 LockBuffer(otherBuffer, BUFFER_LOCK_UNLOCK);
     864                 :           0 :                         UnlockReleaseBuffer(buffer);
     865                 :             : 
     866                 :           0 :                         goto loop;
     867                 :             :                 }
     868   [ #  #  #  # ]:           0 :                 elog(PANIC, "tuple is too big: size %zu", len);
     869                 :           0 :         }
     870                 :             : 
     871                 :             :         /*
     872                 :             :          * Remember the new page as our target for future insertions.
     873                 :             :          *
     874                 :             :          * XXX should we enter the new page into the free space map immediately,
     875                 :             :          * or just keep it for this backend's exclusive use in the short run
     876                 :             :          * (until VACUUM sees it)?      Seems to depend on whether you expect the
     877                 :             :          * current backend to make more insertions or not, which is probably a
     878                 :             :          * good bet most of the time.  So for now, don't add it to FSM yet.
     879                 :             :          */
     880                 :       18011 :         RelationSetTargetBlock(relation, targetBlock);
     881                 :             : 
     882                 :       18011 :         return buffer;
     883                 :     1748859 : }
        

Generated by: LCOV version 2.3.2-1