LCOV - code coverage report
Current view: top level - src/backend/storage/buffer - bufmgr.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 90.7 % 2182 1979
Test Date: 2026-07-16 16:15:41 Functions: 93.7 % 142 133
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 78.9 % 1300 1026

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * bufmgr.c
       4                 :             :  *    buffer manager interface routines
       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/storage/buffer/bufmgr.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : /*
      16                 :             :  * Principal entry points:
      17                 :             :  *
      18                 :             :  * ReadBuffer() -- find or create a buffer holding the requested page,
      19                 :             :  *      and pin it so that no one can destroy it while this process
      20                 :             :  *      is using it.
      21                 :             :  *
      22                 :             :  * StartReadBuffer() -- as above, with separate wait step
      23                 :             :  * StartReadBuffers() -- multiple block version
      24                 :             :  * WaitReadBuffers() -- second step of above
      25                 :             :  *
      26                 :             :  * ReleaseBuffer() -- unpin a buffer
      27                 :             :  *
      28                 :             :  * MarkBufferDirty() -- mark a pinned buffer's contents as "dirty".
      29                 :             :  *      The disk write is delayed until buffer replacement or checkpoint.
      30                 :             :  *
      31                 :             :  * See also these files:
      32                 :             :  *      freelist.c -- chooses victim for buffer replacement
      33                 :             :  *      buf_table.c -- manages the buffer lookup table
      34                 :             :  */
      35                 :             : #include "postgres.h"
      36                 :             : 
      37                 :             : #include <sys/file.h>
      38                 :             : #include <unistd.h>
      39                 :             : 
      40                 :             : #include "access/tableam.h"
      41                 :             : #include "access/xloginsert.h"
      42                 :             : #include "access/xlogutils.h"
      43                 :             : #ifdef USE_ASSERT_CHECKING
      44                 :             : #include "catalog/pg_tablespace_d.h"
      45                 :             : #endif
      46                 :             : #include "catalog/storage.h"
      47                 :             : #include "catalog/storage_xlog.h"
      48                 :             : #include "common/hashfn.h"
      49                 :             : #include "executor/instrument.h"
      50                 :             : #include "lib/binaryheap.h"
      51                 :             : #include "miscadmin.h"
      52                 :             : #include "pg_trace.h"
      53                 :             : #include "pgstat.h"
      54                 :             : #include "postmaster/bgwriter.h"
      55                 :             : #include "storage/aio.h"
      56                 :             : #include "storage/buf_internals.h"
      57                 :             : #include "storage/bufmgr.h"
      58                 :             : #include "storage/fd.h"
      59                 :             : #include "storage/ipc.h"
      60                 :             : #include "storage/lmgr.h"
      61                 :             : #include "storage/proc.h"
      62                 :             : #include "storage/proclist.h"
      63                 :             : #include "storage/procsignal.h"
      64                 :             : #include "storage/read_stream.h"
      65                 :             : #include "storage/smgr.h"
      66                 :             : #include "storage/standby.h"
      67                 :             : #include "utils/memdebug.h"
      68                 :             : #include "utils/ps_status.h"
      69                 :             : #include "utils/rel.h"
      70                 :             : #include "utils/resowner.h"
      71                 :             : #include "utils/timestamp.h"
      72                 :             : #include "utils/wait_event.h"
      73                 :             : 
      74                 :             : 
      75                 :             : /* Note: these two macros only work on shared buffers, not local ones! */
      76                 :             : #define BufHdrGetBlock(bufHdr)  ((Block) (BufferBlocks + ((Size) (bufHdr)->buf_id) * BLCKSZ))
      77                 :             : #define BufferGetLSN(bufHdr)    (PageGetLSN(BufHdrGetBlock(bufHdr)))
      78                 :             : 
      79                 :             : /* Note: this macro only works on local buffers, not shared ones! */
      80                 :             : #define LocalBufHdrGetBlock(bufHdr) \
      81                 :             :     LocalBufferBlockPointers[-((bufHdr)->buf_id + 2)]
      82                 :             : 
      83                 :             : /* Bits in SyncOneBuffer's return value */
      84                 :             : #define BUF_WRITTEN             0x01
      85                 :             : #define BUF_REUSABLE            0x02
      86                 :             : 
      87                 :             : #define RELS_BSEARCH_THRESHOLD      20
      88                 :             : 
      89                 :             : /*
      90                 :             :  * This is the size (in the number of blocks) above which we scan the
      91                 :             :  * entire buffer pool to remove the buffers for all the pages of relation
      92                 :             :  * being dropped. For the relations with size below this threshold, we find
      93                 :             :  * the buffers by doing lookups in BufMapping table.
      94                 :             :  */
      95                 :             : #define BUF_DROP_FULL_SCAN_THRESHOLD        (uint64) (NBuffers / 32)
      96                 :             : 
      97                 :             : /*
      98                 :             :  * This is separated out from PrivateRefCountEntry to allow for copying all
      99                 :             :  * the data members via struct assignment.
     100                 :             :  */
     101                 :             : typedef struct PrivateRefCountData
     102                 :             : {
     103                 :             :     /*
     104                 :             :      * How many times has the buffer been pinned by this backend.
     105                 :             :      */
     106                 :             :     int32       refcount;
     107                 :             : 
     108                 :             :     /*
     109                 :             :      * Is the buffer locked by this backend? BUFFER_LOCK_UNLOCK indicates that
     110                 :             :      * the buffer is not locked.
     111                 :             :      */
     112                 :             :     BufferLockMode lockmode;
     113                 :             : } PrivateRefCountData;
     114                 :             : 
     115                 :             : typedef struct PrivateRefCountEntry
     116                 :             : {
     117                 :             :     /*
     118                 :             :      * Note that this needs to be same as the entry's corresponding
     119                 :             :      * PrivateRefCountArrayKeys[i], if the entry is stored in the array. We
     120                 :             :      * store it in both places as this is used for the hashtable key and
     121                 :             :      * because it is more convenient (passing around a PrivateRefCountEntry
     122                 :             :      * suffices to identify the buffer) and faster (checking the keys array is
     123                 :             :      * faster when checking many entries, checking the entry is faster if just
     124                 :             :      * checking a single entry).
     125                 :             :      */
     126                 :             :     Buffer      buffer;
     127                 :             : 
     128                 :             :     char        status;
     129                 :             : 
     130                 :             :     PrivateRefCountData data;
     131                 :             : } PrivateRefCountEntry;
     132                 :             : 
     133                 :             : #define SH_PREFIX refcount
     134                 :             : #define SH_ELEMENT_TYPE PrivateRefCountEntry
     135                 :             : #define SH_KEY_TYPE Buffer
     136                 :             : #define SH_KEY buffer
     137                 :             : #define SH_HASH_KEY(tb, key) murmurhash32((uint32) (key))
     138                 :             : #define SH_EQUAL(tb, a, b) ((a) == (b))
     139                 :             : #define SH_SCOPE static inline
     140                 :             : #define SH_DECLARE
     141                 :             : #define SH_DEFINE
     142                 :             : #include "lib/simplehash.h"
     143                 :             : 
     144                 :             : /* 64 bytes, about the size of a cache line on common systems */
     145                 :             : #define REFCOUNT_ARRAY_ENTRIES 8
     146                 :             : 
     147                 :             : /*
     148                 :             :  * Status of buffers to checkpoint for a particular tablespace, used
     149                 :             :  * internally in BufferSync.
     150                 :             :  */
     151                 :             : typedef struct CkptTsStatus
     152                 :             : {
     153                 :             :     /* oid of the tablespace */
     154                 :             :     Oid         tsId;
     155                 :             : 
     156                 :             :     /*
     157                 :             :      * Checkpoint progress for this tablespace. To make progress comparable
     158                 :             :      * between tablespaces the progress is, for each tablespace, measured as a
     159                 :             :      * number between 0 and the total number of to-be-checkpointed pages. Each
     160                 :             :      * page checkpointed in this tablespace increments this space's progress
     161                 :             :      * by progress_slice.
     162                 :             :      */
     163                 :             :     float8      progress;
     164                 :             :     float8      progress_slice;
     165                 :             : 
     166                 :             :     /* number of to-be checkpointed pages in this tablespace */
     167                 :             :     int         num_to_scan;
     168                 :             :     /* already processed pages in this tablespace */
     169                 :             :     int         num_scanned;
     170                 :             : 
     171                 :             :     /* current offset in CkptBufferIds for this tablespace */
     172                 :             :     int         index;
     173                 :             : } CkptTsStatus;
     174                 :             : 
     175                 :             : /*
     176                 :             :  * Type for array used to sort SMgrRelations
     177                 :             :  *
     178                 :             :  * FlushRelationsAllBuffers shares the same comparator function with
     179                 :             :  * DropRelationsAllBuffers. Pointer to this struct and RelFileLocator must be
     180                 :             :  * compatible.
     181                 :             :  */
     182                 :             : typedef struct SMgrSortArray
     183                 :             : {
     184                 :             :     RelFileLocator rlocator;    /* This must be the first member */
     185                 :             :     SMgrRelation srel;
     186                 :             : } SMgrSortArray;
     187                 :             : 
     188                 :             : /* GUC variables */
     189                 :             : bool        zero_damaged_pages = false;
     190                 :             : int         bgwriter_lru_maxpages = 100;
     191                 :             : double      bgwriter_lru_multiplier = 2.0;
     192                 :             : bool        track_io_timing = false;
     193                 :             : 
     194                 :             : /*
     195                 :             :  * How many buffers PrefetchBuffer callers should try to stay ahead of their
     196                 :             :  * ReadBuffer calls by.  Zero means "never prefetch".  This value is only used
     197                 :             :  * for buffers not belonging to tablespaces that have their
     198                 :             :  * effective_io_concurrency parameter set.
     199                 :             :  */
     200                 :             : int         effective_io_concurrency = DEFAULT_EFFECTIVE_IO_CONCURRENCY;
     201                 :             : 
     202                 :             : /*
     203                 :             :  * Like effective_io_concurrency, but used by maintenance code paths that might
     204                 :             :  * benefit from a higher setting because they work on behalf of many sessions.
     205                 :             :  * Overridden by the tablespace setting of the same name.
     206                 :             :  */
     207                 :             : int         maintenance_io_concurrency = DEFAULT_MAINTENANCE_IO_CONCURRENCY;
     208                 :             : 
     209                 :             : /*
     210                 :             :  * Limit on how many blocks should be handled in single I/O operations.
     211                 :             :  * StartReadBuffers() callers should respect it, as should other operations
     212                 :             :  * that call smgr APIs directly.  It is computed as the minimum of underlying
     213                 :             :  * GUCs io_combine_limit_guc and io_max_combine_limit.
     214                 :             :  */
     215                 :             : int         io_combine_limit = DEFAULT_IO_COMBINE_LIMIT;
     216                 :             : int         io_combine_limit_guc = DEFAULT_IO_COMBINE_LIMIT;
     217                 :             : int         io_max_combine_limit = DEFAULT_IO_COMBINE_LIMIT;
     218                 :             : 
     219                 :             : /*
     220                 :             :  * GUC variables about triggering kernel writeback for buffers written; OS
     221                 :             :  * dependent defaults are set via the GUC mechanism.
     222                 :             :  */
     223                 :             : int         checkpoint_flush_after = DEFAULT_CHECKPOINT_FLUSH_AFTER;
     224                 :             : int         bgwriter_flush_after = DEFAULT_BGWRITER_FLUSH_AFTER;
     225                 :             : int         backend_flush_after = DEFAULT_BACKEND_FLUSH_AFTER;
     226                 :             : 
     227                 :             : /* local state for LockBufferForCleanup */
     228                 :             : static BufferDesc *PinCountWaitBuf = NULL;
     229                 :             : 
     230                 :             : /*
     231                 :             :  * Backend-Private refcount management:
     232                 :             :  *
     233                 :             :  * Each buffer also has a private refcount that keeps track of the number of
     234                 :             :  * times the buffer is pinned in the current process.  This is so that the
     235                 :             :  * shared refcount needs to be modified only once if a buffer is pinned more
     236                 :             :  * than once by an individual backend.  It's also used to check that no
     237                 :             :  * buffers are still pinned at the end of transactions and when exiting. We
     238                 :             :  * also use this mechanism to track whether this backend has a buffer locked,
     239                 :             :  * and, if so, in what mode.
     240                 :             :  *
     241                 :             :  *
     242                 :             :  * To avoid - as we used to - requiring an array with NBuffers entries to keep
     243                 :             :  * track of local buffers, we use a small sequentially searched array
     244                 :             :  * (PrivateRefCountArrayKeys, with the corresponding data stored in
     245                 :             :  * PrivateRefCountArray) and an overflow hash table (PrivateRefCountHash) to
     246                 :             :  * keep track of backend local pins.
     247                 :             :  *
     248                 :             :  * Until no more than REFCOUNT_ARRAY_ENTRIES buffers are pinned at once, all
     249                 :             :  * refcounts are kept track of in the array; after that, new array entries
     250                 :             :  * displace old ones into the hash table. That way a frequently used entry
     251                 :             :  * can't get "stuck" in the hashtable while infrequent ones clog the array.
     252                 :             :  *
     253                 :             :  * Note that in most scenarios the number of pinned buffers will not exceed
     254                 :             :  * REFCOUNT_ARRAY_ENTRIES.
     255                 :             :  *
     256                 :             :  *
     257                 :             :  * To enter a buffer into the refcount tracking mechanism first reserve a free
     258                 :             :  * entry using ReservePrivateRefCountEntry() and then later, if necessary,
     259                 :             :  * fill it with NewPrivateRefCountEntry(). That split lets us avoid doing
     260                 :             :  * memory allocations in NewPrivateRefCountEntry() which can be important
     261                 :             :  * because in some scenarios it's called with a spinlock held...
     262                 :             :  */
     263                 :             : static Buffer PrivateRefCountArrayKeys[REFCOUNT_ARRAY_ENTRIES];
     264                 :             : static struct PrivateRefCountEntry PrivateRefCountArray[REFCOUNT_ARRAY_ENTRIES];
     265                 :             : static refcount_hash *PrivateRefCountHash = NULL;
     266                 :             : static int32 PrivateRefCountOverflowed = 0;
     267                 :             : static uint32 PrivateRefCountClock = 0;
     268                 :             : static int  ReservedRefCountSlot = -1;
     269                 :             : static int  PrivateRefCountEntryLast = -1;
     270                 :             : 
     271                 :             : static uint32 MaxProportionalPins;
     272                 :             : 
     273                 :             : static void ReservePrivateRefCountEntry(void);
     274                 :             : static PrivateRefCountEntry *NewPrivateRefCountEntry(Buffer buffer);
     275                 :             : static PrivateRefCountEntry *GetPrivateRefCountEntry(Buffer buffer, bool do_move);
     276                 :             : static inline int32 GetPrivateRefCount(Buffer buffer);
     277                 :             : static void ForgetPrivateRefCountEntry(PrivateRefCountEntry *ref);
     278                 :             : 
     279                 :             : /* ResourceOwner callbacks to hold in-progress I/Os and buffer pins */
     280                 :             : static void ResOwnerReleaseBufferIO(Datum res);
     281                 :             : static char *ResOwnerPrintBufferIO(Datum res);
     282                 :             : static void ResOwnerReleaseBuffer(Datum res);
     283                 :             : static char *ResOwnerPrintBuffer(Datum res);
     284                 :             : 
     285                 :             : const ResourceOwnerDesc buffer_io_resowner_desc =
     286                 :             : {
     287                 :             :     .name = "buffer io",
     288                 :             :     .release_phase = RESOURCE_RELEASE_BEFORE_LOCKS,
     289                 :             :     .release_priority = RELEASE_PRIO_BUFFER_IOS,
     290                 :             :     .ReleaseResource = ResOwnerReleaseBufferIO,
     291                 :             :     .DebugPrint = ResOwnerPrintBufferIO
     292                 :             : };
     293                 :             : 
     294                 :             : const ResourceOwnerDesc buffer_resowner_desc =
     295                 :             : {
     296                 :             :     .name = "buffer",
     297                 :             :     .release_phase = RESOURCE_RELEASE_BEFORE_LOCKS,
     298                 :             :     .release_priority = RELEASE_PRIO_BUFFER_PINS,
     299                 :             :     .ReleaseResource = ResOwnerReleaseBuffer,
     300                 :             :     .DebugPrint = ResOwnerPrintBuffer
     301                 :             : };
     302                 :             : 
     303                 :             : /*
     304                 :             :  * Ensure that the PrivateRefCountArray has sufficient space to store one more
     305                 :             :  * entry. This has to be called before using NewPrivateRefCountEntry() to fill
     306                 :             :  * a new entry - but it's perfectly fine to not use a reserved entry.
     307                 :             :  */
     308                 :             : static void
     309                 :    89053843 : ReservePrivateRefCountEntry(void)
     310                 :             : {
     311                 :             :     /* Already reserved (or freed), nothing to do */
     312         [ +  + ]:    89053843 :     if (ReservedRefCountSlot != -1)
     313                 :    83806797 :         return;
     314                 :             : 
     315                 :             :     /*
     316                 :             :      * First search for a free entry the array, that'll be sufficient in the
     317                 :             :      * majority of cases.
     318                 :             :      */
     319                 :             :     {
     320                 :             :         int         i;
     321                 :             : 
     322         [ +  + ]:    47223414 :         for (i = 0; i < REFCOUNT_ARRAY_ENTRIES; i++)
     323                 :             :         {
     324         [ +  + ]:    41976368 :             if (PrivateRefCountArrayKeys[i] == InvalidBuffer)
     325                 :             :             {
     326                 :    30709119 :                 ReservedRefCountSlot = i;
     327                 :             : 
     328                 :             :                 /*
     329                 :             :                  * We could return immediately, but iterating till the end of
     330                 :             :                  * the array allows compiler-autovectorization.
     331                 :             :                  */
     332                 :             :             }
     333                 :             :         }
     334                 :             : 
     335         [ +  + ]:     5247046 :         if (ReservedRefCountSlot != -1)
     336                 :     5058235 :             return;
     337                 :             :     }
     338                 :             : 
     339                 :             :     /*
     340                 :             :      * No luck. All array entries are full. Move one array entry into the hash
     341                 :             :      * table.
     342                 :             :      */
     343                 :             :     {
     344                 :             :         /*
     345                 :             :          * Move entry from the current clock position in the array into the
     346                 :             :          * hashtable. Use that slot.
     347                 :             :          */
     348                 :             :         int         victim_slot;
     349                 :             :         PrivateRefCountEntry *victim_entry;
     350                 :             :         PrivateRefCountEntry *hashent;
     351                 :             :         bool        found;
     352                 :             : 
     353                 :             :         /* select victim slot */
     354                 :      188811 :         victim_slot = PrivateRefCountClock++ % REFCOUNT_ARRAY_ENTRIES;
     355                 :      188811 :         victim_entry = &PrivateRefCountArray[victim_slot];
     356                 :      188811 :         ReservedRefCountSlot = victim_slot;
     357                 :             : 
     358                 :             :         /* Better be used, otherwise we shouldn't get here. */
     359                 :             :         Assert(PrivateRefCountArrayKeys[victim_slot] != InvalidBuffer);
     360                 :             :         Assert(PrivateRefCountArray[victim_slot].buffer != InvalidBuffer);
     361                 :             :         Assert(PrivateRefCountArrayKeys[victim_slot] == PrivateRefCountArray[victim_slot].buffer);
     362                 :             : 
     363                 :             :         /* enter victim array entry into hashtable */
     364                 :      188811 :         hashent = refcount_insert(PrivateRefCountHash,
     365                 :             :                                   PrivateRefCountArrayKeys[victim_slot],
     366                 :             :                                   &found);
     367                 :             :         Assert(!found);
     368                 :             :         /* move data from the entry in the array to the hash entry */
     369                 :      188811 :         hashent->data = victim_entry->data;
     370                 :             : 
     371                 :             :         /* clear the now free array slot */
     372                 :      188811 :         PrivateRefCountArrayKeys[victim_slot] = InvalidBuffer;
     373                 :      188811 :         victim_entry->buffer = InvalidBuffer;
     374                 :             : 
     375                 :             :         /* clear the whole data member, just for future proofing */
     376                 :      188811 :         memset(&victim_entry->data, 0, sizeof(victim_entry->data));
     377                 :      188811 :         victim_entry->data.refcount = 0;
     378                 :      188811 :         victim_entry->data.lockmode = BUFFER_LOCK_UNLOCK;
     379                 :             : 
     380                 :      188811 :         PrivateRefCountOverflowed++;
     381                 :             :     }
     382                 :             : }
     383                 :             : 
     384                 :             : /*
     385                 :             :  * Fill a previously reserved refcount entry.
     386                 :             :  */
     387                 :             : static PrivateRefCountEntry *
     388                 :    77357639 : NewPrivateRefCountEntry(Buffer buffer)
     389                 :             : {
     390                 :             :     PrivateRefCountEntry *res;
     391                 :             : 
     392                 :             :     /* only allowed to be called when a reservation has been made */
     393                 :             :     Assert(ReservedRefCountSlot != -1);
     394                 :             : 
     395                 :             :     /* use up the reserved entry */
     396                 :    77357639 :     res = &PrivateRefCountArray[ReservedRefCountSlot];
     397                 :             : 
     398                 :             :     /* and fill it */
     399                 :    77357639 :     PrivateRefCountArrayKeys[ReservedRefCountSlot] = buffer;
     400                 :    77357639 :     res->buffer = buffer;
     401                 :    77357639 :     res->data.refcount = 0;
     402                 :    77357639 :     res->data.lockmode = BUFFER_LOCK_UNLOCK;
     403                 :             : 
     404                 :             :     /* update cache for the next lookup */
     405                 :    77357639 :     PrivateRefCountEntryLast = ReservedRefCountSlot;
     406                 :             : 
     407                 :    77357639 :     ReservedRefCountSlot = -1;
     408                 :             : 
     409                 :    77357639 :     return res;
     410                 :             : }
     411                 :             : 
     412                 :             : /*
     413                 :             :  * Slow-path for GetPrivateRefCountEntry(). This is big enough to not be worth
     414                 :             :  * inlining. This particularly seems to be true if the compiler is capable of
     415                 :             :  * auto-vectorizing the code, as that imposes additional stack-alignment
     416                 :             :  * requirements etc.
     417                 :             :  */
     418                 :             : static pg_noinline PrivateRefCountEntry *
     419                 :   106884869 : GetPrivateRefCountEntrySlow(Buffer buffer, bool do_move)
     420                 :             : {
     421                 :             :     PrivateRefCountEntry *res;
     422                 :   106884869 :     int         match = -1;
     423                 :             :     int         i;
     424                 :             : 
     425                 :             :     /*
     426                 :             :      * First search for references in the array, that'll be sufficient in the
     427                 :             :      * majority of cases.
     428                 :             :      */
     429         [ +  + ]:   961963821 :     for (i = 0; i < REFCOUNT_ARRAY_ENTRIES; i++)
     430                 :             :     {
     431         [ +  + ]:   855078952 :         if (PrivateRefCountArrayKeys[i] == buffer)
     432                 :             :         {
     433                 :    31827046 :             match = i;
     434                 :             :             /* see ReservePrivateRefCountEntry() for why we don't return */
     435                 :             :         }
     436                 :             :     }
     437                 :             : 
     438         [ +  + ]:   106884869 :     if (likely(match != -1))
     439                 :             :     {
     440                 :             :         /* update cache for the next lookup */
     441                 :    31827046 :         PrivateRefCountEntryLast = match;
     442                 :             : 
     443                 :    31827046 :         return &PrivateRefCountArray[match];
     444                 :             :     }
     445                 :             : 
     446                 :             :     /*
     447                 :             :      * By here we know that the buffer, if already pinned, isn't residing in
     448                 :             :      * the array.
     449                 :             :      *
     450                 :             :      * Only look up the buffer in the hashtable if we've previously overflowed
     451                 :             :      * into it.
     452                 :             :      */
     453         [ +  + ]:    75057823 :     if (PrivateRefCountOverflowed == 0)
     454                 :    74401131 :         return NULL;
     455                 :             : 
     456                 :      656692 :     res = refcount_lookup(PrivateRefCountHash, buffer);
     457                 :             : 
     458         [ +  + ]:      656692 :     if (res == NULL)
     459                 :      418054 :         return NULL;
     460         [ +  + ]:      238638 :     else if (!do_move)
     461                 :             :     {
     462                 :             :         /* caller doesn't want us to move the hash entry into the array */
     463                 :      141814 :         return res;
     464                 :             :     }
     465                 :             :     else
     466                 :             :     {
     467                 :             :         /* move buffer from hashtable into the free array slot */
     468                 :             :         PrivateRefCountEntry *free;
     469                 :             :         PrivateRefCountData data;
     470                 :             : 
     471                 :             :         /* Save data and delete from hashtable while res is still valid */
     472                 :       96824 :         data = res->data;
     473                 :       96824 :         refcount_delete_item(PrivateRefCountHash, res);
     474                 :             :         Assert(PrivateRefCountOverflowed > 0);
     475                 :       96824 :         PrivateRefCountOverflowed--;
     476                 :             : 
     477                 :             :         /* Ensure there's a free array slot */
     478                 :       96824 :         ReservePrivateRefCountEntry();
     479                 :             : 
     480                 :             :         /* Use up the reserved slot */
     481                 :             :         Assert(ReservedRefCountSlot != -1);
     482                 :       96824 :         free = &PrivateRefCountArray[ReservedRefCountSlot];
     483                 :             :         Assert(PrivateRefCountArrayKeys[ReservedRefCountSlot] == free->buffer);
     484                 :             :         Assert(free->buffer == InvalidBuffer);
     485                 :             : 
     486                 :             :         /* and fill it */
     487                 :       96824 :         free->buffer = buffer;
     488                 :       96824 :         free->data = data;
     489                 :       96824 :         PrivateRefCountArrayKeys[ReservedRefCountSlot] = buffer;
     490                 :             :         /* update cache for the next lookup */
     491                 :       96824 :         PrivateRefCountEntryLast = ReservedRefCountSlot;
     492                 :             : 
     493                 :       96824 :         ReservedRefCountSlot = -1;
     494                 :             : 
     495                 :       96824 :         return free;
     496                 :             :     }
     497                 :             : }
     498                 :             : 
     499                 :             : /*
     500                 :             :  * Return the PrivateRefCount entry for the passed buffer.
     501                 :             :  *
     502                 :             :  * Returns NULL if a buffer doesn't have a refcount entry. Otherwise, if
     503                 :             :  * do_move is true, and the entry resides in the hashtable the entry is
     504                 :             :  * optimized for frequent access by moving it to the array.
     505                 :             :  */
     506                 :             : static inline PrivateRefCountEntry *
     507                 :   443248385 : GetPrivateRefCountEntry(Buffer buffer, bool do_move)
     508                 :             : {
     509                 :             :     Assert(BufferIsValid(buffer));
     510                 :             :     Assert(!BufferIsLocal(buffer));
     511                 :             : 
     512                 :             :     /*
     513                 :             :      * It's very common to look up the same buffer repeatedly. To make that
     514                 :             :      * fast, we have a one-entry cache.
     515                 :             :      *
     516                 :             :      * In contrast to the loop in GetPrivateRefCountEntrySlow(), here it
     517                 :             :      * faster to check PrivateRefCountArray[].buffer, as in the case of a hit
     518                 :             :      * fewer addresses are computed and fewer cachelines are accessed. Whereas
     519                 :             :      * in GetPrivateRefCountEntrySlow()'s case, checking
     520                 :             :      * PrivateRefCountArrayKeys saves a lot of memory accesses.
     521                 :             :      */
     522         [ +  + ]:   443248385 :     if (likely(PrivateRefCountEntryLast != -1) &&
     523         [ +  + ]:   443232087 :         likely(PrivateRefCountArray[PrivateRefCountEntryLast].buffer == buffer))
     524                 :             :     {
     525                 :   336363516 :         return &PrivateRefCountArray[PrivateRefCountEntryLast];
     526                 :             :     }
     527                 :             : 
     528                 :             :     /*
     529                 :             :      * The code for the cached lookup is small enough to be worth inlining
     530                 :             :      * into the caller. In the miss case however, that empirically doesn't
     531                 :             :      * seem worth it.
     532                 :             :      */
     533                 :   106884869 :     return GetPrivateRefCountEntrySlow(buffer, do_move);
     534                 :             : }
     535                 :             : 
     536                 :             : /*
     537                 :             :  * Returns how many times the passed buffer is pinned by this backend.
     538                 :             :  *
     539                 :             :  * Only works for shared memory buffers!
     540                 :             :  */
     541                 :             : static inline int32
     542                 :     3312443 : GetPrivateRefCount(Buffer buffer)
     543                 :             : {
     544                 :             :     PrivateRefCountEntry *ref;
     545                 :             : 
     546                 :             :     Assert(BufferIsValid(buffer));
     547                 :             :     Assert(!BufferIsLocal(buffer));
     548                 :             : 
     549                 :             :     /*
     550                 :             :      * Not moving the entry - that's ok for the current users, but we might
     551                 :             :      * want to change this one day.
     552                 :             :      */
     553                 :     3312443 :     ref = GetPrivateRefCountEntry(buffer, false);
     554                 :             : 
     555         [ +  + ]:     3312443 :     if (ref == NULL)
     556                 :          30 :         return 0;
     557                 :     3312413 :     return ref->data.refcount;
     558                 :             : }
     559                 :             : 
     560                 :             : /*
     561                 :             :  * Release resources used to track the reference count of a buffer which we no
     562                 :             :  * longer have pinned and don't want to pin again immediately.
     563                 :             :  */
     564                 :             : static void
     565                 :    77357639 : ForgetPrivateRefCountEntry(PrivateRefCountEntry *ref)
     566                 :             : {
     567                 :             :     Assert(ref->data.refcount == 0);
     568                 :             :     Assert(ref->data.lockmode == BUFFER_LOCK_UNLOCK);
     569                 :             : 
     570   [ +  -  +  + ]:    77357639 :     if (ref >= &PrivateRefCountArray[0] &&
     571                 :             :         ref < &PrivateRefCountArray[REFCOUNT_ARRAY_ENTRIES])
     572                 :             :     {
     573                 :    77265652 :         ref->buffer = InvalidBuffer;
     574                 :    77265652 :         PrivateRefCountArrayKeys[ref - PrivateRefCountArray] = InvalidBuffer;
     575                 :             : 
     576                 :             : 
     577                 :             :         /*
     578                 :             :          * Mark the just used entry as reserved - in many scenarios that
     579                 :             :          * allows us to avoid ever having to search the array/hash for free
     580                 :             :          * entries.
     581                 :             :          */
     582                 :    77265652 :         ReservedRefCountSlot = ref - PrivateRefCountArray;
     583                 :             :     }
     584                 :             :     else
     585                 :             :     {
     586                 :       91987 :         refcount_delete_item(PrivateRefCountHash, ref);
     587                 :             :         Assert(PrivateRefCountOverflowed > 0);
     588                 :       91987 :         PrivateRefCountOverflowed--;
     589                 :             :     }
     590                 :    77357639 : }
     591                 :             : 
     592                 :             : /*
     593                 :             :  * BufferIsPinned
     594                 :             :  *      True iff the buffer is pinned (also checks for valid buffer number).
     595                 :             :  *
     596                 :             :  *      NOTE: what we check here is that *this* backend holds a pin on
     597                 :             :  *      the buffer.  We do not care whether some other backend does.
     598                 :             :  */
     599                 :             : #define BufferIsPinned(bufnum) \
     600                 :             : ( \
     601                 :             :     !BufferIsValid(bufnum) ? \
     602                 :             :         false \
     603                 :             :     : \
     604                 :             :         BufferIsLocal(bufnum) ? \
     605                 :             :             (LocalRefCount[-(bufnum) - 1] > 0) \
     606                 :             :         : \
     607                 :             :     (GetPrivateRefCount(bufnum) > 0) \
     608                 :             : )
     609                 :             : 
     610                 :             : 
     611                 :             : static Buffer ReadBuffer_common(Relation rel,
     612                 :             :                                 SMgrRelation smgr, char smgr_persistence,
     613                 :             :                                 ForkNumber forkNum, BlockNumber blockNum,
     614                 :             :                                 ReadBufferMode mode, BufferAccessStrategy strategy);
     615                 :             : static BlockNumber ExtendBufferedRelCommon(BufferManagerRelation bmr,
     616                 :             :                                            ForkNumber fork,
     617                 :             :                                            BufferAccessStrategy strategy,
     618                 :             :                                            uint32 flags,
     619                 :             :                                            uint32 extend_by,
     620                 :             :                                            BlockNumber extend_upto,
     621                 :             :                                            Buffer *buffers,
     622                 :             :                                            uint32 *extended_by);
     623                 :             : static BlockNumber ExtendBufferedRelShared(BufferManagerRelation bmr,
     624                 :             :                                            ForkNumber fork,
     625                 :             :                                            BufferAccessStrategy strategy,
     626                 :             :                                            uint32 flags,
     627                 :             :                                            uint32 extend_by,
     628                 :             :                                            BlockNumber extend_upto,
     629                 :             :                                            Buffer *buffers,
     630                 :             :                                            uint32 *extended_by);
     631                 :             : static bool PinBuffer(BufferDesc *buf, BufferAccessStrategy strategy,
     632                 :             :                       bool skip_if_not_valid);
     633                 :             : static void PinBuffer_Locked(BufferDesc *buf);
     634                 :             : static void UnpinBuffer(BufferDesc *buf);
     635                 :             : static void UnpinBufferNoOwner(BufferDesc *buf);
     636                 :             : static void BufferSync(int flags);
     637                 :             : static int  SyncOneBuffer(int buf_id, bool skip_recently_used,
     638                 :             :                           WritebackContext *wb_context);
     639                 :             : static void WaitIO(BufferDesc *buf);
     640                 :             : static void AbortBufferIO(Buffer buffer);
     641                 :             : static void shared_buffer_write_error_callback(void *arg);
     642                 :             : static void local_buffer_write_error_callback(void *arg);
     643                 :             : static inline BufferDesc *BufferAlloc(SMgrRelation smgr,
     644                 :             :                                       char relpersistence,
     645                 :             :                                       ForkNumber forkNum,
     646                 :             :                                       BlockNumber blockNum,
     647                 :             :                                       BufferAccessStrategy strategy,
     648                 :             :                                       bool *foundPtr, IOContext io_context);
     649                 :             : static bool AsyncReadBuffers(ReadBuffersOperation *operation, int *nblocks_progress);
     650                 :             : static void CheckReadBuffersOperation(ReadBuffersOperation *operation, bool is_complete);
     651                 :             : 
     652                 :             : static pg_always_inline void TrackBufferHit(IOObject io_object,
     653                 :             :                                             IOContext io_context,
     654                 :             :                                             Relation rel, char persistence, SMgrRelation smgr,
     655                 :             :                                             ForkNumber forknum, BlockNumber blocknum);
     656                 :             : static Buffer GetVictimBuffer(BufferAccessStrategy strategy, IOContext io_context);
     657                 :             : static void FlushUnlockedBuffer(BufferDesc *buf, SMgrRelation reln,
     658                 :             :                                 IOObject io_object, IOContext io_context);
     659                 :             : static void FlushBuffer(BufferDesc *buf, SMgrRelation reln,
     660                 :             :                         IOObject io_object, IOContext io_context);
     661                 :             : static void FindAndDropRelationBuffers(RelFileLocator rlocator,
     662                 :             :                                        ForkNumber forkNum,
     663                 :             :                                        BlockNumber nForkBlock,
     664                 :             :                                        BlockNumber firstDelBlock);
     665                 :             : static void RelationCopyStorageUsingBuffer(RelFileLocator srclocator,
     666                 :             :                                            RelFileLocator dstlocator,
     667                 :             :                                            ForkNumber forkNum, bool permanent);
     668                 :             : static void AtProcExit_Buffers(int code, Datum arg);
     669                 :             : static void CheckForBufferLeaks(void);
     670                 :             : #ifdef USE_ASSERT_CHECKING
     671                 :             : static void AssertNotCatalogBufferLock(Buffer buffer, BufferLockMode mode);
     672                 :             : #endif
     673                 :             : static int  rlocator_comparator(const void *p1, const void *p2);
     674                 :             : static inline int buffertag_comparator(const BufferTag *ba, const BufferTag *bb);
     675                 :             : static inline int ckpt_buforder_comparator(const CkptSortItem *a, const CkptSortItem *b);
     676                 :             : static int  ts_ckpt_progress_comparator(Datum a, Datum b, void *arg);
     677                 :             : 
     678                 :             : static void BufferLockAcquire(Buffer buffer, BufferDesc *buf_hdr, BufferLockMode mode);
     679                 :             : static void BufferLockUnlock(Buffer buffer, BufferDesc *buf_hdr);
     680                 :             : static bool BufferLockConditional(Buffer buffer, BufferDesc *buf_hdr, BufferLockMode mode);
     681                 :             : static bool BufferLockHeldByMeInMode(BufferDesc *buf_hdr, BufferLockMode mode);
     682                 :             : static bool BufferLockHeldByMe(BufferDesc *buf_hdr);
     683                 :             : static inline void BufferLockDisown(Buffer buffer, BufferDesc *buf_hdr);
     684                 :             : static inline int BufferLockDisownInternal(Buffer buffer, BufferDesc *buf_hdr);
     685                 :             : static inline bool BufferLockAttempt(BufferDesc *buf_hdr, BufferLockMode mode);
     686                 :             : static void BufferLockQueueSelf(BufferDesc *buf_hdr, BufferLockMode mode);
     687                 :             : static void BufferLockDequeueSelf(BufferDesc *buf_hdr);
     688                 :             : static void BufferLockWakeup(BufferDesc *buf_hdr, bool wake_exclusive);
     689                 :             : static void BufferLockProcessRelease(BufferDesc *buf_hdr, BufferLockMode mode, uint64 lockstate);
     690                 :             : static inline uint64 BufferLockReleaseSub(BufferLockMode mode);
     691                 :             : 
     692                 :             : 
     693                 :             : /*
     694                 :             :  * Implementation of PrefetchBuffer() for shared buffers.
     695                 :             :  */
     696                 :             : PrefetchBufferResult
     697                 :       39980 : PrefetchSharedBuffer(SMgrRelation smgr_reln,
     698                 :             :                      ForkNumber forkNum,
     699                 :             :                      BlockNumber blockNum)
     700                 :             : {
     701                 :       39980 :     PrefetchBufferResult result = {InvalidBuffer, false};
     702                 :             :     BufferTag   newTag;         /* identity of requested block */
     703                 :             :     uint32      newHash;        /* hash value for newTag */
     704                 :             :     LWLock     *newPartitionLock;   /* buffer partition lock for it */
     705                 :             :     int         buf_id;
     706                 :             : 
     707                 :             :     Assert(BlockNumberIsValid(blockNum));
     708                 :             : 
     709                 :             :     /* create a tag so we can lookup the buffer */
     710                 :       39980 :     InitBufferTag(&newTag, &smgr_reln->smgr_rlocator.locator,
     711                 :             :                   forkNum, blockNum);
     712                 :             : 
     713                 :             :     /* determine its hash code and partition lock ID */
     714                 :       39980 :     newHash = BufTableHashCode(&newTag);
     715                 :       39980 :     newPartitionLock = BufMappingPartitionLock(newHash);
     716                 :             : 
     717                 :             :     /* see if the block is in the buffer pool already */
     718                 :       39980 :     LWLockAcquire(newPartitionLock, LW_SHARED);
     719                 :       39980 :     buf_id = BufTableLookup(&newTag, newHash);
     720                 :       39980 :     LWLockRelease(newPartitionLock);
     721                 :             : 
     722                 :             :     /* If not in buffers, initiate prefetch */
     723         [ +  + ]:       39980 :     if (buf_id < 0)
     724                 :             :     {
     725                 :             : #ifdef USE_PREFETCH
     726                 :             :         /*
     727                 :             :          * Try to initiate an asynchronous read.  This returns false in
     728                 :             :          * recovery if the relation file doesn't exist.
     729                 :             :          */
     730   [ +  +  +  - ]:       18885 :         if ((io_direct_flags & IO_DIRECT_DATA) == 0 &&
     731                 :        9316 :             smgrprefetch(smgr_reln, forkNum, blockNum, 1))
     732                 :             :         {
     733                 :        9316 :             result.initiated_io = true;
     734                 :             :         }
     735                 :             : #endif                          /* USE_PREFETCH */
     736                 :             :     }
     737                 :             :     else
     738                 :             :     {
     739                 :             :         /*
     740                 :             :          * Report the buffer it was in at that time.  The caller may be able
     741                 :             :          * to avoid a buffer table lookup, but it's not pinned and it must be
     742                 :             :          * rechecked!
     743                 :             :          */
     744                 :       30411 :         result.recent_buffer = buf_id + 1;
     745                 :             :     }
     746                 :             : 
     747                 :             :     /*
     748                 :             :      * If the block *is* in buffers, we do nothing.  This is not really ideal:
     749                 :             :      * the block might be just about to be evicted, which would be stupid
     750                 :             :      * since we know we are going to need it soon.  But the only easy answer
     751                 :             :      * is to bump the usage_count, which does not seem like a great solution:
     752                 :             :      * when the caller does ultimately touch the block, usage_count would get
     753                 :             :      * bumped again, resulting in too much favoritism for blocks that are
     754                 :             :      * involved in a prefetch sequence. A real fix would involve some
     755                 :             :      * additional per-buffer state, and it's not clear that there's enough of
     756                 :             :      * a problem to justify that.
     757                 :             :      */
     758                 :             : 
     759                 :       39980 :     return result;
     760                 :             : }
     761                 :             : 
     762                 :             : /*
     763                 :             :  * PrefetchBuffer -- initiate asynchronous read of a block of a relation
     764                 :             :  *
     765                 :             :  * This is named by analogy to ReadBuffer but doesn't actually allocate a
     766                 :             :  * buffer.  Instead it tries to ensure that a future ReadBuffer for the given
     767                 :             :  * block will not be delayed by the I/O.  Prefetching is optional.
     768                 :             :  *
     769                 :             :  * There are three possible outcomes:
     770                 :             :  *
     771                 :             :  * 1.  If the block is already cached, the result includes a valid buffer that
     772                 :             :  * could be used by the caller to avoid the need for a later buffer lookup, but
     773                 :             :  * it's not pinned, so the caller must recheck it.
     774                 :             :  *
     775                 :             :  * 2.  If the kernel has been asked to initiate I/O, the initiated_io member is
     776                 :             :  * true.  Currently there is no way to know if the data was already cached by
     777                 :             :  * the kernel and therefore didn't really initiate I/O, and no way to know when
     778                 :             :  * the I/O completes other than using synchronous ReadBuffer().
     779                 :             :  *
     780                 :             :  * 3.  Otherwise, the buffer wasn't already cached by PostgreSQL, and
     781                 :             :  * USE_PREFETCH is not defined (this build doesn't support prefetching due to
     782                 :             :  * lack of a kernel facility), direct I/O is enabled, or the underlying
     783                 :             :  * relation file wasn't found and we are in recovery.  (If the relation file
     784                 :             :  * wasn't found and we are not in recovery, an error is raised).
     785                 :             :  */
     786                 :             : PrefetchBufferResult
     787                 :       28995 : PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
     788                 :             : {
     789                 :             :     Assert(RelationIsValid(reln));
     790                 :             :     Assert(BlockNumberIsValid(blockNum));
     791                 :             : 
     792         [ +  + ]:       28995 :     if (RelationUsesLocalBuffers(reln))
     793                 :             :     {
     794                 :             :         /* see comments in ReadBuffer_common */
     795   [ +  -  -  + ]:        1357 :         if (RELATION_IS_OTHER_TEMP(reln))
     796         [ #  # ]:           0 :             ereport(ERROR,
     797                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     798                 :             :                      errmsg("cannot access temporary tables of other sessions")));
     799                 :             : 
     800                 :             :         /* pass it off to localbuf.c */
     801                 :        1357 :         return PrefetchLocalBuffer(RelationGetSmgr(reln), forkNum, blockNum);
     802                 :             :     }
     803                 :             :     else
     804                 :             :     {
     805                 :             :         /* pass it to the shared buffer version */
     806                 :       27638 :         return PrefetchSharedBuffer(RelationGetSmgr(reln), forkNum, blockNum);
     807                 :             :     }
     808                 :             : }
     809                 :             : 
     810                 :             : /*
     811                 :             :  * ReadRecentBuffer -- try to pin a block in a recently observed buffer
     812                 :             :  *
     813                 :             :  * Compared to ReadBuffer(), this avoids a buffer mapping lookup when it's
     814                 :             :  * successful.  Return true if the buffer is valid and still has the expected
     815                 :             :  * tag.  In that case, the buffer is pinned and the usage count is bumped.
     816                 :             :  */
     817                 :             : bool
     818                 :        5114 : ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum,
     819                 :             :                  Buffer recent_buffer)
     820                 :             : {
     821                 :             :     BufferDesc *bufHdr;
     822                 :             :     BufferTag   tag;
     823                 :             :     uint64      buf_state;
     824                 :             : 
     825                 :             :     Assert(BufferIsValid(recent_buffer));
     826                 :             : 
     827                 :        5114 :     ResourceOwnerEnlarge(CurrentResourceOwner);
     828                 :        5114 :     ReservePrivateRefCountEntry();
     829                 :        5114 :     InitBufferTag(&tag, &rlocator, forkNum, blockNum);
     830                 :             : 
     831         [ +  + ]:        5114 :     if (BufferIsLocal(recent_buffer))
     832                 :             :     {
     833                 :         140 :         int         b = -recent_buffer - 1;
     834                 :             : 
     835                 :         140 :         bufHdr = GetLocalBufferDescriptor(b);
     836                 :         140 :         buf_state = pg_atomic_read_u64(&bufHdr->state);
     837                 :             : 
     838                 :             :         /* Is it still valid and holding the right tag? */
     839   [ +  -  +  - ]:         140 :         if ((buf_state & BM_VALID) && BufferTagsEqual(&tag, &bufHdr->tag))
     840                 :             :         {
     841                 :         140 :             PinLocalBuffer(bufHdr, true);
     842                 :             : 
     843                 :         140 :             pgBufferUsage.local_blks_hit++;
     844                 :             : 
     845                 :         140 :             return true;
     846                 :             :         }
     847                 :             :     }
     848                 :             :     else
     849                 :             :     {
     850                 :        4974 :         bufHdr = GetBufferDescriptor(recent_buffer - 1);
     851                 :             : 
     852                 :             :         /*
     853                 :             :          * Is it still valid and holding the right tag?  We do an unlocked tag
     854                 :             :          * comparison first, to make it unlikely that we'll increment the
     855                 :             :          * usage counter of the wrong buffer, if someone calls us with a very
     856                 :             :          * out of date recent_buffer.  Then we'll check it again if we get the
     857                 :             :          * pin.
     858                 :             :          */
     859   [ +  +  +  + ]:        9915 :         if (BufferTagsEqual(&tag, &bufHdr->tag) &&
     860                 :        4941 :             PinBuffer(bufHdr, NULL, true))
     861                 :             :         {
     862         [ +  - ]:        4935 :             if (BufferTagsEqual(&tag, &bufHdr->tag))
     863                 :             :             {
     864                 :        4935 :                 pgBufferUsage.shared_blks_hit++;
     865                 :        4935 :                 return true;
     866                 :             :             }
     867                 :           0 :             UnpinBuffer(bufHdr);
     868                 :             :         }
     869                 :             :     }
     870                 :             : 
     871                 :          39 :     return false;
     872                 :             : }
     873                 :             : 
     874                 :             : /*
     875                 :             :  * ReadBuffer -- a shorthand for ReadBufferExtended, for reading from main
     876                 :             :  *      fork with RBM_NORMAL mode and default strategy.
     877                 :             :  */
     878                 :             : Buffer
     879                 :    63512762 : ReadBuffer(Relation reln, BlockNumber blockNum)
     880                 :             : {
     881                 :    63512762 :     return ReadBufferExtended(reln, MAIN_FORKNUM, blockNum, RBM_NORMAL, NULL);
     882                 :             : }
     883                 :             : 
     884                 :             : /*
     885                 :             :  * ReadBufferExtended -- returns a buffer containing the requested
     886                 :             :  *      block of the requested relation.  If the blknum
     887                 :             :  *      requested is P_NEW, extend the relation file and
     888                 :             :  *      allocate a new block.  (Caller is responsible for
     889                 :             :  *      ensuring that only one backend tries to extend a
     890                 :             :  *      relation at the same time!)
     891                 :             :  *
     892                 :             :  * Returns: the buffer number for the buffer containing
     893                 :             :  *      the block read.  The returned buffer has been pinned.
     894                 :             :  *      Does not return on error --- elog's instead.
     895                 :             :  *
     896                 :             :  * Assume when this function is called, that reln has been opened already.
     897                 :             :  *
     898                 :             :  * In RBM_NORMAL mode, the page is read from disk, and the page header is
     899                 :             :  * validated.  An error is thrown if the page header is not valid.  (But
     900                 :             :  * note that an all-zero page is considered "valid"; see
     901                 :             :  * PageIsVerified().)
     902                 :             :  *
     903                 :             :  * RBM_ZERO_ON_ERROR is like the normal mode, but if the page header is not
     904                 :             :  * valid, the page is zeroed instead of throwing an error. This is intended
     905                 :             :  * for non-critical data, where the caller is prepared to repair errors.
     906                 :             :  *
     907                 :             :  * In RBM_ZERO_AND_LOCK mode, if the page isn't in buffer cache already, it's
     908                 :             :  * filled with zeros instead of reading it from disk.  Useful when the caller
     909                 :             :  * is going to fill the page from scratch, since this saves I/O and avoids
     910                 :             :  * unnecessary failure if the page-on-disk has corrupt page headers.
     911                 :             :  * The page is returned locked to ensure that the caller has a chance to
     912                 :             :  * initialize the page before it's made visible to others.
     913                 :             :  * Caution: do not use this mode to read a page that is beyond the relation's
     914                 :             :  * current physical EOF; that is likely to cause problems in md.c when
     915                 :             :  * the page is modified and written out. P_NEW is OK, though.
     916                 :             :  *
     917                 :             :  * RBM_ZERO_AND_CLEANUP_LOCK is the same as RBM_ZERO_AND_LOCK, but acquires
     918                 :             :  * a cleanup-strength lock on the page.
     919                 :             :  *
     920                 :             :  * RBM_NORMAL_NO_LOG mode is treated the same as RBM_NORMAL here.
     921                 :             :  *
     922                 :             :  * If strategy is not NULL, a nondefault buffer access strategy is used.
     923                 :             :  * See buffer/README for details.
     924                 :             :  */
     925                 :             : inline Buffer
     926                 :    75033713 : ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum,
     927                 :             :                    ReadBufferMode mode, BufferAccessStrategy strategy)
     928                 :             : {
     929                 :             :     Buffer      buf;
     930                 :             : 
     931                 :             :     /*
     932                 :             :      * Read the buffer, and update pgstat counters to reflect a cache hit or
     933                 :             :      * miss.  The other-session temp-relation check is enforced by
     934                 :             :      * ReadBuffer_common().
     935                 :             :      */
     936                 :    75033713 :     buf = ReadBuffer_common(reln, RelationGetSmgr(reln), 0,
     937                 :             :                             forkNum, blockNum, mode, strategy);
     938                 :             : 
     939                 :    75033688 :     return buf;
     940                 :             : }
     941                 :             : 
     942                 :             : 
     943                 :             : /*
     944                 :             :  * ReadBufferWithoutRelcache -- like ReadBufferExtended, but doesn't require
     945                 :             :  *      a relcache entry for the relation.
     946                 :             :  *
     947                 :             :  * Pass permanent = true for a RELPERSISTENCE_PERMANENT relation, and
     948                 :             :  * permanent = false for a RELPERSISTENCE_UNLOGGED relation. This function
     949                 :             :  * cannot be used for temporary relations (and making that work might be
     950                 :             :  * difficult, unless we only want to read temporary relations for our own
     951                 :             :  * ProcNumber).
     952                 :             :  */
     953                 :             : Buffer
     954                 :     5995719 : ReadBufferWithoutRelcache(RelFileLocator rlocator, ForkNumber forkNum,
     955                 :             :                           BlockNumber blockNum, ReadBufferMode mode,
     956                 :             :                           BufferAccessStrategy strategy, bool permanent)
     957                 :             : {
     958                 :     5995719 :     SMgrRelation smgr = smgropen(rlocator, INVALID_PROC_NUMBER);
     959                 :             : 
     960         [ +  - ]:     5995719 :     return ReadBuffer_common(NULL, smgr,
     961                 :             :                              permanent ? RELPERSISTENCE_PERMANENT : RELPERSISTENCE_UNLOGGED,
     962                 :             :                              forkNum, blockNum,
     963                 :             :                              mode, strategy);
     964                 :             : }
     965                 :             : 
     966                 :             : /*
     967                 :             :  * Convenience wrapper around ExtendBufferedRelBy() extending by one block.
     968                 :             :  */
     969                 :             : Buffer
     970                 :       57449 : ExtendBufferedRel(BufferManagerRelation bmr,
     971                 :             :                   ForkNumber forkNum,
     972                 :             :                   BufferAccessStrategy strategy,
     973                 :             :                   uint32 flags)
     974                 :             : {
     975                 :             :     Buffer      buf;
     976                 :       57449 :     uint32      extend_by = 1;
     977                 :             : 
     978                 :       57449 :     ExtendBufferedRelBy(bmr, forkNum, strategy, flags, extend_by,
     979                 :             :                         &buf, &extend_by);
     980                 :             : 
     981                 :       57449 :     return buf;
     982                 :             : }
     983                 :             : 
     984                 :             : /*
     985                 :             :  * Extend relation by multiple blocks.
     986                 :             :  *
     987                 :             :  * Tries to extend the relation by extend_by blocks. Depending on the
     988                 :             :  * availability of resources the relation may end up being extended by a
     989                 :             :  * smaller number of pages (unless an error is thrown, always by at least one
     990                 :             :  * page). *extended_by is updated to the number of pages the relation has been
     991                 :             :  * extended to.
     992                 :             :  *
     993                 :             :  * buffers needs to be an array that is at least extend_by long. Upon
     994                 :             :  * completion, the first extend_by array elements will point to a pinned
     995                 :             :  * buffer.
     996                 :             :  *
     997                 :             :  * If EB_LOCK_FIRST is part of flags, the first returned buffer is
     998                 :             :  * locked. This is useful for callers that want a buffer that is guaranteed to
     999                 :             :  * be empty.
    1000                 :             :  */
    1001                 :             : BlockNumber
    1002                 :      211697 : ExtendBufferedRelBy(BufferManagerRelation bmr,
    1003                 :             :                     ForkNumber fork,
    1004                 :             :                     BufferAccessStrategy strategy,
    1005                 :             :                     uint32 flags,
    1006                 :             :                     uint32 extend_by,
    1007                 :             :                     Buffer *buffers,
    1008                 :             :                     uint32 *extended_by)
    1009                 :             : {
    1010                 :             :     Assert((bmr.rel != NULL) != (bmr.smgr != NULL));
    1011                 :             :     Assert(bmr.smgr == NULL || bmr.relpersistence != '\0');
    1012                 :             :     Assert(extend_by > 0);
    1013                 :             : 
    1014         [ +  - ]:      211697 :     if (bmr.relpersistence == '\0')
    1015                 :      211697 :         bmr.relpersistence = bmr.rel->rd_rel->relpersistence;
    1016                 :             : 
    1017                 :      211697 :     return ExtendBufferedRelCommon(bmr, fork, strategy, flags,
    1018                 :             :                                    extend_by, InvalidBlockNumber,
    1019                 :             :                                    buffers, extended_by);
    1020                 :             : }
    1021                 :             : 
    1022                 :             : /*
    1023                 :             :  * Extend the relation so it is at least extend_to blocks large, return buffer
    1024                 :             :  * (extend_to - 1).
    1025                 :             :  *
    1026                 :             :  * This is useful for callers that want to write a specific page, regardless
    1027                 :             :  * of the current size of the relation (e.g. useful for visibilitymap and for
    1028                 :             :  * crash recovery).
    1029                 :             :  */
    1030                 :             : Buffer
    1031                 :       56030 : ExtendBufferedRelTo(BufferManagerRelation bmr,
    1032                 :             :                     ForkNumber fork,
    1033                 :             :                     BufferAccessStrategy strategy,
    1034                 :             :                     uint32 flags,
    1035                 :             :                     BlockNumber extend_to,
    1036                 :             :                     ReadBufferMode mode)
    1037                 :             : {
    1038                 :             :     BlockNumber current_size;
    1039                 :       56030 :     uint32      extended_by = 0;
    1040                 :       56030 :     Buffer      buffer = InvalidBuffer;
    1041                 :             :     Buffer      buffers[64];
    1042                 :             : 
    1043                 :             :     Assert((bmr.rel != NULL) != (bmr.smgr != NULL));
    1044                 :             :     Assert(bmr.smgr == NULL || bmr.relpersistence != '\0');
    1045                 :             :     Assert(extend_to != InvalidBlockNumber && extend_to > 0);
    1046                 :             : 
    1047         [ +  + ]:       56030 :     if (bmr.relpersistence == '\0')
    1048                 :        9492 :         bmr.relpersistence = bmr.rel->rd_rel->relpersistence;
    1049                 :             : 
    1050                 :             :     /*
    1051                 :             :      * If desired, create the file if it doesn't exist.  If
    1052                 :             :      * smgr_cached_nblocks[fork] is positive then it must exist, no need for
    1053                 :             :      * an smgrexists call.
    1054                 :             :      */
    1055         [ +  + ]:       56030 :     if ((flags & EB_CREATE_FORK_IF_NEEDED) &&
    1056   [ +  -  +  + ]:        9492 :         (BMR_GET_SMGR(bmr)->smgr_cached_nblocks[fork] == 0 ||
    1057   [ +  -  -  + ]:          31 :          BMR_GET_SMGR(bmr)->smgr_cached_nblocks[fork] == InvalidBlockNumber) &&
    1058   [ +  -  +  + ]:        9461 :         !smgrexists(BMR_GET_SMGR(bmr), fork))
    1059                 :             :     {
    1060                 :        9429 :         LockRelationForExtension(bmr.rel, ExclusiveLock);
    1061                 :             : 
    1062                 :             :         /* recheck, fork might have been created concurrently */
    1063   [ +  -  +  + ]:        9429 :         if (!smgrexists(BMR_GET_SMGR(bmr), fork))
    1064         [ +  - ]:        9428 :             smgrcreate(BMR_GET_SMGR(bmr), fork, flags & EB_PERFORMING_RECOVERY);
    1065                 :             : 
    1066                 :        9429 :         UnlockRelationForExtension(bmr.rel, ExclusiveLock);
    1067                 :             :     }
    1068                 :             : 
    1069                 :             :     /*
    1070                 :             :      * If requested, invalidate size cache, so that smgrnblocks asks the
    1071                 :             :      * kernel.
    1072                 :             :      */
    1073         [ +  + ]:       56030 :     if (flags & EB_CLEAR_SIZE_CACHE)
    1074         [ +  - ]:        9492 :         BMR_GET_SMGR(bmr)->smgr_cached_nblocks[fork] = InvalidBlockNumber;
    1075                 :             : 
    1076                 :             :     /*
    1077                 :             :      * Estimate how many pages we'll need to extend by. This avoids acquiring
    1078                 :             :      * unnecessarily many victim buffers.
    1079                 :             :      */
    1080         [ +  + ]:       56030 :     current_size = smgrnblocks(BMR_GET_SMGR(bmr), fork);
    1081                 :             : 
    1082                 :             :     /*
    1083                 :             :      * Since no-one else can be looking at the page contents yet, there is no
    1084                 :             :      * difference between an exclusive lock and a cleanup-strength lock. Note
    1085                 :             :      * that we pass the original mode to ReadBuffer_common() below, when
    1086                 :             :      * falling back to reading the buffer to a concurrent relation extension.
    1087                 :             :      */
    1088   [ +  +  -  + ]:       56030 :     if (mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK)
    1089                 :       46140 :         flags |= EB_LOCK_TARGET;
    1090                 :             : 
    1091         [ +  + ]:      114301 :     while (current_size < extend_to)
    1092                 :             :     {
    1093                 :       58271 :         uint32      num_pages = lengthof(buffers);
    1094                 :             :         BlockNumber first_block;
    1095                 :             : 
    1096         [ +  + ]:       58271 :         if ((uint64) current_size + num_pages > extend_to)
    1097                 :       58205 :             num_pages = extend_to - current_size;
    1098                 :             : 
    1099                 :       58271 :         first_block = ExtendBufferedRelCommon(bmr, fork, strategy, flags,
    1100                 :             :                                               num_pages, extend_to,
    1101                 :             :                                               buffers, &extended_by);
    1102                 :             : 
    1103                 :       58271 :         current_size = first_block + extended_by;
    1104                 :             :         Assert(num_pages != 0 || current_size >= extend_to);
    1105                 :             : 
    1106         [ +  + ]:      125372 :         for (uint32 i = 0; i < extended_by; i++)
    1107                 :             :         {
    1108         [ +  + ]:       67101 :             if (first_block + i != extend_to - 1)
    1109                 :       11077 :                 ReleaseBuffer(buffers[i]);
    1110                 :             :             else
    1111                 :       56024 :                 buffer = buffers[i];
    1112                 :             :         }
    1113                 :             :     }
    1114                 :             : 
    1115                 :             :     /*
    1116                 :             :      * It's possible that another backend concurrently extended the relation.
    1117                 :             :      * In that case read the buffer.
    1118                 :             :      *
    1119                 :             :      * XXX: Should we control this via a flag?
    1120                 :             :      */
    1121         [ +  + ]:       56030 :     if (buffer == InvalidBuffer)
    1122                 :             :     {
    1123                 :             :         Assert(extended_by == 0);
    1124         [ +  - ]:           6 :         buffer = ReadBuffer_common(bmr.rel, BMR_GET_SMGR(bmr), bmr.relpersistence,
    1125                 :             :                                    fork, extend_to - 1, mode, strategy);
    1126                 :             :     }
    1127                 :             : 
    1128                 :       56030 :     return buffer;
    1129                 :             : }
    1130                 :             : 
    1131                 :             : /*
    1132                 :             :  * Lock and optionally zero a buffer, as part of the implementation of
    1133                 :             :  * RBM_ZERO_AND_LOCK or RBM_ZERO_AND_CLEANUP_LOCK.  The buffer must be already
    1134                 :             :  * pinned.  If the buffer is not already valid, it is zeroed and made valid.
    1135                 :             :  */
    1136                 :             : static void
    1137                 :      355806 : ZeroAndLockBuffer(Buffer buffer, ReadBufferMode mode, bool already_valid)
    1138                 :             : {
    1139                 :             :     BufferDesc *bufHdr;
    1140                 :             :     bool        need_to_zero;
    1141                 :      355806 :     bool        isLocalBuf = BufferIsLocal(buffer);
    1142                 :             :     StartBufferIOResult sbres;
    1143                 :             : 
    1144                 :             :     Assert(mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK);
    1145                 :             : 
    1146         [ +  + ]:      355806 :     if (already_valid)
    1147                 :             :     {
    1148                 :             :         /*
    1149                 :             :          * If the caller already knew the buffer was valid, we can skip some
    1150                 :             :          * header interaction.  The caller just wants to lock the buffer.
    1151                 :             :          */
    1152                 :       38578 :         need_to_zero = false;
    1153                 :             :     }
    1154                 :             :     else
    1155                 :             :     {
    1156         [ +  + ]:      317228 :         if (isLocalBuf)
    1157                 :             :         {
    1158                 :             :             /* Simple case for non-shared buffers. */
    1159                 :          30 :             bufHdr = GetLocalBufferDescriptor(-buffer - 1);
    1160                 :          30 :             sbres = StartLocalBufferIO(bufHdr, true, true, NULL);
    1161                 :             :         }
    1162                 :             :         else
    1163                 :             :         {
    1164                 :             :             /*
    1165                 :             :              * Take BM_IO_IN_PROGRESS, or discover that BM_VALID has been set
    1166                 :             :              * concurrently.  Even though we aren't doing I/O, that ensures
    1167                 :             :              * that we don't zero a page that someone else has pinned.  An
    1168                 :             :              * exclusive content lock wouldn't be enough, because readers are
    1169                 :             :              * allowed to drop the content lock after determining that a tuple
    1170                 :             :              * is visible (see buffer access rules in README).
    1171                 :             :              */
    1172                 :      317198 :             bufHdr = GetBufferDescriptor(buffer - 1);
    1173                 :      317198 :             sbres = StartSharedBufferIO(bufHdr, true, true, NULL);
    1174                 :             :         }
    1175                 :             : 
    1176                 :             :         Assert(sbres != BUFFER_IO_IN_PROGRESS);
    1177                 :      317228 :         need_to_zero = sbres == BUFFER_IO_READY_FOR_IO;
    1178                 :             :     }
    1179                 :             : 
    1180         [ +  + ]:      355806 :     if (need_to_zero)
    1181                 :             :     {
    1182                 :      317228 :         memset(BufferGetPage(buffer), 0, BLCKSZ);
    1183                 :             : 
    1184                 :             :         /*
    1185                 :             :          * Grab the buffer content lock before marking the page as valid, to
    1186                 :             :          * make sure that no other backend sees the zeroed page before the
    1187                 :             :          * caller has had a chance to initialize it.
    1188                 :             :          *
    1189                 :             :          * Since no-one else can be looking at the page contents yet, there is
    1190                 :             :          * no difference between an exclusive lock and a cleanup-strength
    1191                 :             :          * lock. (Note that we cannot use LockBuffer() or
    1192                 :             :          * LockBufferForCleanup() here, because they assert that the buffer is
    1193                 :             :          * already valid.)
    1194                 :             :          */
    1195         [ +  + ]:      317228 :         if (!isLocalBuf)
    1196                 :      317198 :             LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
    1197                 :             : 
    1198                 :             :         /* Set BM_VALID, terminate IO, and wake up any waiters */
    1199         [ +  + ]:      317228 :         if (isLocalBuf)
    1200                 :          30 :             TerminateLocalBufferIO(bufHdr, false, BM_VALID, false);
    1201                 :             :         else
    1202                 :      317198 :             TerminateBufferIO(bufHdr, false, BM_VALID, true, false);
    1203                 :             :     }
    1204         [ +  + ]:       38578 :     else if (!isLocalBuf)
    1205                 :             :     {
    1206                 :             :         /*
    1207                 :             :          * The buffer is valid, so we can't zero it.  The caller still expects
    1208                 :             :          * the page to be locked on return.
    1209                 :             :          */
    1210         [ +  + ]:       38558 :         if (mode == RBM_ZERO_AND_LOCK)
    1211                 :       38488 :             LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
    1212                 :             :         else
    1213                 :          70 :             LockBufferForCleanup(buffer);
    1214                 :             :     }
    1215                 :      355806 : }
    1216                 :             : 
    1217                 :             : /*
    1218                 :             :  * Pin a buffer for a given block.  *foundPtr is set to true if the block was
    1219                 :             :  * already present, or false if more work is required to either read it in or
    1220                 :             :  * zero it.
    1221                 :             :  */
    1222                 :             : static pg_always_inline Buffer
    1223                 :    85787139 : PinBufferForBlock(Relation rel,
    1224                 :             :                   SMgrRelation smgr,
    1225                 :             :                   char persistence,
    1226                 :             :                   ForkNumber forkNum,
    1227                 :             :                   BlockNumber blockNum,
    1228                 :             :                   BufferAccessStrategy strategy,
    1229                 :             :                   IOObject io_object,
    1230                 :             :                   IOContext io_context,
    1231                 :             :                   bool *foundPtr)
    1232                 :             : {
    1233                 :             :     BufferDesc *bufHdr;
    1234                 :             : 
    1235                 :             :     Assert(blockNum != P_NEW);
    1236                 :             : 
    1237                 :             :     /* Persistence should be set before */
    1238                 :             :     Assert((persistence == RELPERSISTENCE_TEMP ||
    1239                 :             :             persistence == RELPERSISTENCE_PERMANENT ||
    1240                 :             :             persistence == RELPERSISTENCE_UNLOGGED));
    1241                 :             : 
    1242                 :             :     TRACE_POSTGRESQL_BUFFER_READ_START(forkNum, blockNum,
    1243                 :             :                                        smgr->smgr_rlocator.locator.spcOid,
    1244                 :             :                                        smgr->smgr_rlocator.locator.dbOid,
    1245                 :             :                                        smgr->smgr_rlocator.locator.relNumber,
    1246                 :             :                                        smgr->smgr_rlocator.backend);
    1247                 :             : 
    1248         [ +  + ]:    85787139 :     if (persistence == RELPERSISTENCE_TEMP)
    1249                 :     1648797 :         bufHdr = LocalBufferAlloc(smgr, forkNum, blockNum, foundPtr);
    1250                 :             :     else
    1251                 :    84138342 :         bufHdr = BufferAlloc(smgr, persistence, forkNum, blockNum,
    1252                 :             :                              strategy, foundPtr, io_context);
    1253                 :             : 
    1254         [ +  + ]:    85787131 :     if (*foundPtr)
    1255                 :    83886008 :         TrackBufferHit(io_object, io_context, rel, persistence, smgr, forkNum, blockNum);
    1256                 :             : 
    1257         [ +  + ]:    85787131 :     if (rel)
    1258                 :             :     {
    1259                 :             :         /*
    1260                 :             :          * While pgBufferUsage's "read" counter isn't bumped unless we reach
    1261                 :             :          * WaitReadBuffers() (so, not for hits, and not for buffers that are
    1262                 :             :          * zeroed instead), the per-relation stats always count them.
    1263                 :             :          */
    1264   [ +  +  +  +  :    79534062 :         pgstat_count_buffer_read(rel);
                   +  + ]
    1265                 :             :     }
    1266                 :             : 
    1267                 :    85787131 :     return BufferDescriptorGetBuffer(bufHdr);
    1268                 :             : }
    1269                 :             : 
    1270                 :             : /*
    1271                 :             :  * ReadBuffer_common -- common logic for all ReadBuffer variants
    1272                 :             :  *
    1273                 :             :  * smgr is required, rel is optional unless using P_NEW.
    1274                 :             :  */
    1275                 :             : static pg_always_inline Buffer
    1276                 :    81029438 : ReadBuffer_common(Relation rel, SMgrRelation smgr, char smgr_persistence,
    1277                 :             :                   ForkNumber forkNum,
    1278                 :             :                   BlockNumber blockNum, ReadBufferMode mode,
    1279                 :             :                   BufferAccessStrategy strategy)
    1280                 :             : {
    1281                 :             :     ReadBuffersOperation operation;
    1282                 :             :     Buffer      buffer;
    1283                 :             :     int         flags;
    1284                 :             :     char        persistence;
    1285                 :             : 
    1286                 :             :     /*
    1287                 :             :      * Reject attempts to read non-local temporary relations; we would be
    1288                 :             :      * likely to get wrong data since we have no visibility into the owning
    1289                 :             :      * session's local buffers.  This is the canonical place for the check,
    1290                 :             :      * covering the ReadBufferExtended() entry point and any other caller that
    1291                 :             :      * supplies a Relation.
    1292                 :             :      */
    1293   [ +  +  +  +  :    81029438 :     if (rel && RELATION_IS_OTHER_TEMP(rel))
                   +  + ]
    1294         [ +  - ]:           2 :         ereport(ERROR,
    1295                 :             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1296                 :             :                  errmsg("cannot access temporary tables of other sessions")));
    1297                 :             : 
    1298                 :             :     /*
    1299                 :             :      * Backward compatibility path, most code should use ExtendBufferedRel()
    1300                 :             :      * instead, as acquiring the extension lock inside ExtendBufferedRel()
    1301                 :             :      * scales a lot better.
    1302                 :             :      */
    1303         [ +  + ]:    81029436 :     if (unlikely(blockNum == P_NEW))
    1304                 :             :     {
    1305                 :         322 :         uint32      flags = EB_SKIP_EXTENSION_LOCK;
    1306                 :             : 
    1307                 :             :         /*
    1308                 :             :          * Since no-one else can be looking at the page contents yet, there is
    1309                 :             :          * no difference between an exclusive lock and a cleanup-strength
    1310                 :             :          * lock.
    1311                 :             :          */
    1312   [ +  -  -  + ]:         322 :         if (mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK)
    1313                 :           0 :             flags |= EB_LOCK_FIRST;
    1314                 :             : 
    1315                 :         322 :         return ExtendBufferedRel(BMR_REL(rel), forkNum, strategy, flags);
    1316                 :             :     }
    1317                 :             : 
    1318         [ +  + ]:    81029114 :     if (rel)
    1319                 :    75033395 :         persistence = rel->rd_rel->relpersistence;
    1320                 :             :     else
    1321                 :     5995719 :         persistence = smgr_persistence;
    1322                 :             : 
    1323   [ +  +  +  +  :    81029114 :     if (unlikely(mode == RBM_ZERO_AND_CLEANUP_LOCK ||
                   +  + ]
    1324                 :             :                  mode == RBM_ZERO_AND_LOCK))
    1325                 :             :     {
    1326                 :             :         bool        found;
    1327                 :             :         IOContext   io_context;
    1328                 :             :         IOObject    io_object;
    1329                 :             : 
    1330         [ +  + ]:      355806 :         if (persistence == RELPERSISTENCE_TEMP)
    1331                 :             :         {
    1332                 :          50 :             io_context = IOCONTEXT_NORMAL;
    1333                 :          50 :             io_object = IOOBJECT_TEMP_RELATION;
    1334                 :             :         }
    1335                 :             :         else
    1336                 :             :         {
    1337                 :      355756 :             io_context = IOContextForStrategy(strategy);
    1338                 :      355756 :             io_object = IOOBJECT_RELATION;
    1339                 :             :         }
    1340                 :             : 
    1341                 :      355806 :         buffer = PinBufferForBlock(rel, smgr, persistence,
    1342                 :             :                                    forkNum, blockNum, strategy,
    1343                 :             :                                    io_object, io_context, &found);
    1344                 :      355806 :         ZeroAndLockBuffer(buffer, mode, found);
    1345                 :      355806 :         return buffer;
    1346                 :             :     }
    1347                 :             : 
    1348                 :             :     /*
    1349                 :             :      * Signal that we are going to immediately wait. If we're immediately
    1350                 :             :      * waiting, there is no benefit in actually executing the IO
    1351                 :             :      * asynchronously, it would just add dispatch overhead.
    1352                 :             :      */
    1353                 :    80673308 :     flags = READ_BUFFERS_SYNCHRONOUSLY;
    1354         [ +  + ]:    80673308 :     if (mode == RBM_ZERO_ON_ERROR)
    1355                 :     2027239 :         flags |= READ_BUFFERS_ZERO_ON_ERROR;
    1356                 :    80673308 :     operation.smgr = smgr;
    1357                 :    80673308 :     operation.rel = rel;
    1358                 :    80673308 :     operation.persistence = persistence;
    1359                 :    80673308 :     operation.forknum = forkNum;
    1360                 :    80673308 :     operation.strategy = strategy;
    1361         [ +  + ]:    80673308 :     if (StartReadBuffer(&operation,
    1362                 :             :                         &buffer,
    1363                 :             :                         blockNum,
    1364                 :             :                         flags))
    1365                 :      772551 :         WaitReadBuffers(&operation);
    1366                 :             : 
    1367                 :    80673285 :     return buffer;
    1368                 :             : }
    1369                 :             : 
    1370                 :             : static pg_always_inline bool
    1371                 :    85247212 : StartReadBuffersImpl(ReadBuffersOperation *operation,
    1372                 :             :                      Buffer *buffers,
    1373                 :             :                      BlockNumber blockNum,
    1374                 :             :                      int *nblocks,
    1375                 :             :                      int flags,
    1376                 :             :                      bool allow_forwarding)
    1377                 :             : {
    1378                 :    85247212 :     int         actual_nblocks = *nblocks;
    1379                 :    85247212 :     int         maxcombine = 0;
    1380                 :             :     bool        did_start_io;
    1381                 :             :     IOContext   io_context;
    1382                 :             :     IOObject    io_object;
    1383                 :             : 
    1384                 :             :     Assert(*nblocks == 1 || allow_forwarding);
    1385                 :             :     Assert(*nblocks > 0);
    1386                 :             :     Assert(*nblocks <= MAX_IO_COMBINE_LIMIT);
    1387                 :             : 
    1388                 :             :     /* see comments in ReadBuffer_common */
    1389   [ +  +  +  +  :    85247212 :     if (operation->rel && RELATION_IS_OTHER_TEMP(operation->rel))
                   -  + ]
    1390         [ #  # ]:           0 :         ereport(ERROR,
    1391                 :             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1392                 :             :                  errmsg("cannot access temporary tables of other sessions")));
    1393                 :             : 
    1394         [ +  + ]:    85247212 :     if (operation->persistence == RELPERSISTENCE_TEMP)
    1395                 :             :     {
    1396                 :     1640551 :         io_context = IOCONTEXT_NORMAL;
    1397                 :     1640551 :         io_object = IOOBJECT_TEMP_RELATION;
    1398                 :             :     }
    1399                 :             :     else
    1400                 :             :     {
    1401                 :    83606661 :         io_context = IOContextForStrategy(operation->strategy);
    1402                 :    83606661 :         io_object = IOOBJECT_RELATION;
    1403                 :             :     }
    1404                 :             : 
    1405         [ +  + ]:    86831121 :     for (int i = 0; i < actual_nblocks; ++i)
    1406                 :             :     {
    1407                 :             :         bool        found;
    1408                 :             : 
    1409   [ +  +  +  + ]:    85433541 :         if (allow_forwarding && buffers[i] != InvalidBuffer)
    1410                 :        2208 :         {
    1411                 :             :             BufferDesc *bufHdr;
    1412                 :             : 
    1413                 :             :             /*
    1414                 :             :              * This is a buffer that was pinned by an earlier call to
    1415                 :             :              * StartReadBuffers(), but couldn't be handled in one operation at
    1416                 :             :              * that time.  The operation was split, and the caller has passed
    1417                 :             :              * an already pinned buffer back to us to handle the rest of the
    1418                 :             :              * operation.  It must continue at the expected block number.
    1419                 :             :              */
    1420                 :             :             Assert(BufferGetBlockNumber(buffers[i]) == blockNum + i);
    1421                 :             : 
    1422                 :             :             /*
    1423                 :             :              * It might be an already valid buffer (a hit) that followed the
    1424                 :             :              * final contiguous block of an earlier I/O (a miss) marking the
    1425                 :             :              * end of it, or a buffer that some other backend has since made
    1426                 :             :              * valid by performing the I/O for us, in which case we can handle
    1427                 :             :              * it as a hit now.  It is safe to check for a BM_VALID flag with
    1428                 :             :              * a relaxed load, because we got a fresh view of it while pinning
    1429                 :             :              * it in the previous call.
    1430                 :             :              *
    1431                 :             :              * On the other hand if we don't see BM_VALID yet, it must be an
    1432                 :             :              * I/O that was split by the previous call and we need to try to
    1433                 :             :              * start a new I/O from this block.  We're also racing against any
    1434                 :             :              * other backend that might start the I/O or even manage to mark
    1435                 :             :              * it BM_VALID after this check, but StartBufferIO() will handle
    1436                 :             :              * those cases.
    1437                 :             :              */
    1438         [ +  + ]:        2208 :             if (BufferIsLocal(buffers[i]))
    1439                 :          16 :                 bufHdr = GetLocalBufferDescriptor(-buffers[i] - 1);
    1440                 :             :             else
    1441                 :        2192 :                 bufHdr = GetBufferDescriptor(buffers[i] - 1);
    1442                 :             :             Assert(pg_atomic_read_u64(&bufHdr->state) & BM_TAG_VALID);
    1443                 :        2208 :             found = pg_atomic_read_u64(&bufHdr->state) & BM_VALID;
    1444                 :             :         }
    1445                 :             :         else
    1446                 :             :         {
    1447                 :    85431325 :             buffers[i] = PinBufferForBlock(operation->rel,
    1448                 :             :                                            operation->smgr,
    1449                 :    85431333 :                                            operation->persistence,
    1450                 :             :                                            operation->forknum,
    1451                 :             :                                            blockNum + i,
    1452                 :             :                                            operation->strategy,
    1453                 :             :                                            io_object, io_context,
    1454                 :             :                                            &found);
    1455                 :             :         }
    1456                 :             : 
    1457         [ +  + ]:    85433533 :         if (found)
    1458                 :             :         {
    1459                 :             :             /*
    1460                 :             :              * We have a hit.  If it's the first block in the requested range,
    1461                 :             :              * we can return it immediately and report that WaitReadBuffers()
    1462                 :             :              * does not need to be called.  If the initial value of *nblocks
    1463                 :             :              * was larger, the caller will have to call again for the rest.
    1464                 :             :              */
    1465         [ +  + ]:    83849624 :             if (i == 0)
    1466                 :             :             {
    1467                 :    83847428 :                 *nblocks = 1;
    1468                 :             : 
    1469                 :             : #ifdef USE_ASSERT_CHECKING
    1470                 :             : 
    1471                 :             :                 /*
    1472                 :             :                  * Initialize enough of ReadBuffersOperation to make
    1473                 :             :                  * CheckReadBuffersOperation() work. Outside of assertions
    1474                 :             :                  * that's not necessary when no IO is issued.
    1475                 :             :                  */
    1476                 :             :                 operation->buffers = buffers;
    1477                 :             :                 operation->blocknum = blockNum;
    1478                 :             :                 operation->nblocks = 1;
    1479                 :             :                 operation->nblocks_done = 1;
    1480                 :             :                 CheckReadBuffersOperation(operation, true);
    1481                 :             : #endif
    1482                 :    83847428 :                 return false;
    1483                 :             :             }
    1484                 :             : 
    1485                 :             :             /*
    1486                 :             :              * Otherwise we already have an I/O to perform, but this block
    1487                 :             :              * can't be included as it is already valid.  Split the I/O here.
    1488                 :             :              * There may or may not be more blocks requiring I/O after this
    1489                 :             :              * one, we haven't checked, but they can't be contiguous with this
    1490                 :             :              * one in the way.  We'll leave this buffer pinned, forwarding it
    1491                 :             :              * to the next call, avoiding the need to unpin it here and re-pin
    1492                 :             :              * it in the next call.
    1493                 :             :              */
    1494                 :        2196 :             actual_nblocks = i;
    1495                 :        2196 :             break;
    1496                 :             :         }
    1497                 :             :         else
    1498                 :             :         {
    1499                 :             :             /*
    1500                 :             :              * Check how many blocks we can cover with the same IO. The smgr
    1501                 :             :              * implementation might e.g. be limited due to a segment boundary.
    1502                 :             :              */
    1503   [ +  +  +  + ]:     1583909 :             if (i == 0 && actual_nblocks > 1)
    1504                 :             :             {
    1505                 :       37858 :                 maxcombine = smgrmaxcombine(operation->smgr,
    1506                 :             :                                             operation->forknum,
    1507                 :             :                                             blockNum);
    1508         [ -  + ]:       37858 :                 if (unlikely(maxcombine < actual_nblocks))
    1509                 :             :                 {
    1510         [ #  # ]:           0 :                     elog(DEBUG2, "limiting nblocks at %u from %u to %u",
    1511                 :             :                          blockNum, actual_nblocks, maxcombine);
    1512                 :           0 :                     actual_nblocks = maxcombine;
    1513                 :             :                 }
    1514                 :             :             }
    1515                 :             :         }
    1516                 :             :     }
    1517                 :     1399776 :     *nblocks = actual_nblocks;
    1518                 :             : 
    1519                 :             :     /* Populate information needed for I/O. */
    1520                 :     1399776 :     operation->buffers = buffers;
    1521                 :     1399776 :     operation->blocknum = blockNum;
    1522                 :     1399776 :     operation->flags = flags;
    1523                 :     1399776 :     operation->nblocks = actual_nblocks;
    1524                 :     1399776 :     operation->nblocks_done = 0;
    1525                 :     1399776 :     pgaio_wref_clear(&operation->io_wref);
    1526                 :             : 
    1527                 :             :     /*
    1528                 :             :      * When using AIO, start the IO in the background. If not, issue prefetch
    1529                 :             :      * requests if desired by the caller.
    1530                 :             :      *
    1531                 :             :      * The reason we have a dedicated path for IOMETHOD_SYNC here is to
    1532                 :             :      * de-risk the introduction of AIO somewhat. It's a large architectural
    1533                 :             :      * change, with lots of chances for unanticipated performance effects.
    1534                 :             :      *
    1535                 :             :      * Use of IOMETHOD_SYNC already leads to not actually performing IO
    1536                 :             :      * asynchronously, but without the check here we'd execute IO earlier than
    1537                 :             :      * we used to. Eventually this IOMETHOD_SYNC specific path should go away.
    1538                 :             :      */
    1539         [ +  + ]:     1399776 :     if (io_method != IOMETHOD_SYNC)
    1540                 :             :     {
    1541                 :             :         /*
    1542                 :             :          * Try to start IO asynchronously. It's possible that no IO needs to
    1543                 :             :          * be started, if another backend already performed the IO.
    1544                 :             :          *
    1545                 :             :          * Note that if an IO is started, it might not cover the entire
    1546                 :             :          * requested range, e.g. because an intermediary block has been read
    1547                 :             :          * in by another backend.  In that case any "trailing" buffers we
    1548                 :             :          * already pinned above will be "forwarded" by read_stream.c to the
    1549                 :             :          * next call to StartReadBuffers().
    1550                 :             :          *
    1551                 :             :          * This is signalled to the caller by decrementing *nblocks *and*
    1552                 :             :          * reducing operation->nblocks. The latter is done here, but not below
    1553                 :             :          * WaitReadBuffers(), as in WaitReadBuffers() we can't "shorten" the
    1554                 :             :          * overall read size anymore, we need to retry until done in its
    1555                 :             :          * entirety or until failed.
    1556                 :             :          */
    1557                 :     1398393 :         did_start_io = AsyncReadBuffers(operation, nblocks);
    1558                 :             : 
    1559                 :     1398378 :         operation->nblocks = *nblocks;
    1560                 :             :     }
    1561                 :             :     else
    1562                 :             :     {
    1563                 :        1383 :         operation->flags |= READ_BUFFERS_SYNCHRONOUSLY;
    1564                 :             : 
    1565         [ +  + ]:        1383 :         if (flags & READ_BUFFERS_ISSUE_ADVICE)
    1566                 :             :         {
    1567                 :             :             /*
    1568                 :             :              * In theory we should only do this if PinBufferForBlock() had to
    1569                 :             :              * allocate new buffers above.  That way, if two calls to
    1570                 :             :              * StartReadBuffers() were made for the same blocks before
    1571                 :             :              * WaitReadBuffers(), only the first would issue the advice.
    1572                 :             :              * That'd be a better simulation of true asynchronous I/O, which
    1573                 :             :              * would only start the I/O once, but isn't done here for
    1574                 :             :              * simplicity.
    1575                 :             :              */
    1576                 :          19 :             smgrprefetch(operation->smgr,
    1577                 :             :                          operation->forknum,
    1578                 :             :                          blockNum,
    1579                 :             :                          actual_nblocks);
    1580                 :             :         }
    1581                 :             : 
    1582                 :             :         /*
    1583                 :             :          * Indicate that WaitReadBuffers() should be called. WaitReadBuffers()
    1584                 :             :          * will initiate the necessary IO.
    1585                 :             :          */
    1586                 :        1383 :         did_start_io = true;
    1587                 :             :     }
    1588                 :             : 
    1589                 :     1399761 :     CheckReadBuffersOperation(operation, !did_start_io);
    1590                 :             : 
    1591                 :     1399761 :     return did_start_io;
    1592                 :             : }
    1593                 :             : 
    1594                 :             : /*
    1595                 :             :  * Begin reading a range of blocks beginning at blockNum and extending for
    1596                 :             :  * *nblocks.  *nblocks and the buffers array are in/out parameters.  On entry,
    1597                 :             :  * the buffers elements covered by *nblocks must hold either InvalidBuffer or
    1598                 :             :  * buffers forwarded by an earlier call to StartReadBuffers() that was split
    1599                 :             :  * and is now being continued.  On return, *nblocks holds the number of blocks
    1600                 :             :  * accepted by this operation.  If it is less than the original number then
    1601                 :             :  * this operation has been split, but buffer elements up to the original
    1602                 :             :  * requested size may hold forwarded buffers to be used for a continuing
    1603                 :             :  * operation.  The caller must either start a new I/O beginning at the block
    1604                 :             :  * immediately following the blocks accepted by this call and pass those
    1605                 :             :  * buffers back in, or release them if it chooses not to.  It shouldn't make
    1606                 :             :  * any other use of or assumptions about forwarded buffers.
    1607                 :             :  *
    1608                 :             :  * If false is returned, no I/O is necessary and the buffers covered by
    1609                 :             :  * *nblocks on exit are valid and ready to be accessed.  If true is returned,
    1610                 :             :  * an I/O has been started, and WaitReadBuffers() must be called with the same
    1611                 :             :  * operation object before the buffers covered by *nblocks on exit can be
    1612                 :             :  * accessed.  Along with the operation object, the caller-supplied array of
    1613                 :             :  * buffers must remain valid until WaitReadBuffers() is called, and any
    1614                 :             :  * forwarded buffers must also be preserved for a continuing call unless
    1615                 :             :  * they are explicitly released.
    1616                 :             :  */
    1617                 :             : bool
    1618                 :     2108478 : StartReadBuffers(ReadBuffersOperation *operation,
    1619                 :             :                  Buffer *buffers,
    1620                 :             :                  BlockNumber blockNum,
    1621                 :             :                  int *nblocks,
    1622                 :             :                  int flags)
    1623                 :             : {
    1624                 :     2108478 :     return StartReadBuffersImpl(operation, buffers, blockNum, nblocks, flags,
    1625                 :             :                                 true /* expect forwarded buffers */ );
    1626                 :             : }
    1627                 :             : 
    1628                 :             : /*
    1629                 :             :  * Single block version of the StartReadBuffers().  This might save a few
    1630                 :             :  * instructions when called from another translation unit, because it is
    1631                 :             :  * specialized for nblocks == 1.
    1632                 :             :  *
    1633                 :             :  * This version does not support "forwarded" buffers: they cannot be created
    1634                 :             :  * by reading only one block and *buffer is ignored on entry.
    1635                 :             :  */
    1636                 :             : bool
    1637                 :    83138734 : StartReadBuffer(ReadBuffersOperation *operation,
    1638                 :             :                 Buffer *buffer,
    1639                 :             :                 BlockNumber blocknum,
    1640                 :             :                 int flags)
    1641                 :             : {
    1642                 :    83138734 :     int         nblocks = 1;
    1643                 :             :     bool        result;
    1644                 :             : 
    1645                 :    83138734 :     result = StartReadBuffersImpl(operation, buffer, blocknum, &nblocks, flags,
    1646                 :             :                                   false /* single block, no forwarding */ );
    1647                 :             :     Assert(nblocks == 1);       /* single block can't be short */
    1648                 :             : 
    1649                 :    83138719 :     return result;
    1650                 :             : }
    1651                 :             : 
    1652                 :             : /*
    1653                 :             :  * Perform sanity checks on the ReadBuffersOperation.
    1654                 :             :  */
    1655                 :             : static void
    1656                 :     4199321 : CheckReadBuffersOperation(ReadBuffersOperation *operation, bool is_complete)
    1657                 :             : {
    1658                 :             : #ifdef USE_ASSERT_CHECKING
    1659                 :             :     Assert(operation->nblocks_done <= operation->nblocks);
    1660                 :             :     Assert(!is_complete || operation->nblocks == operation->nblocks_done);
    1661                 :             : 
    1662                 :             :     for (int i = 0; i < operation->nblocks; i++)
    1663                 :             :     {
    1664                 :             :         Buffer      buffer = operation->buffers[i];
    1665                 :             :         BufferDesc *buf_hdr = BufferIsLocal(buffer) ?
    1666                 :             :             GetLocalBufferDescriptor(-buffer - 1) :
    1667                 :             :             GetBufferDescriptor(buffer - 1);
    1668                 :             : 
    1669                 :             :         Assert(BufferGetBlockNumber(buffer) == operation->blocknum + i);
    1670                 :             :         Assert(pg_atomic_read_u64(&buf_hdr->state) & BM_TAG_VALID);
    1671                 :             : 
    1672                 :             :         if (i < operation->nblocks_done)
    1673                 :             :             Assert(pg_atomic_read_u64(&buf_hdr->state) & BM_VALID);
    1674                 :             :     }
    1675                 :             : #endif
    1676                 :     4199321 : }
    1677                 :             : 
    1678                 :             : /*
    1679                 :             :  * We track various stats related to buffer hits. Because this is done in a
    1680                 :             :  * few separate places, this helper exists for convenience.
    1681                 :             :  */
    1682                 :             : static pg_always_inline void
    1683                 :    83888344 : TrackBufferHit(IOObject io_object, IOContext io_context,
    1684                 :             :                Relation rel, char persistence, SMgrRelation smgr,
    1685                 :             :                ForkNumber forknum, BlockNumber blocknum)
    1686                 :             : {
    1687                 :             :     TRACE_POSTGRESQL_BUFFER_READ_DONE(forknum,
    1688                 :             :                                       blocknum,
    1689                 :             :                                       smgr->smgr_rlocator.locator.spcOid,
    1690                 :             :                                       smgr->smgr_rlocator.locator.dbOid,
    1691                 :             :                                       smgr->smgr_rlocator.locator.relNumber,
    1692                 :             :                                       smgr->smgr_rlocator.backend,
    1693                 :             :                                       true);
    1694                 :             : 
    1695         [ +  + ]:    83888344 :     if (persistence == RELPERSISTENCE_TEMP)
    1696                 :     1637744 :         pgBufferUsage.local_blks_hit += 1;
    1697                 :             :     else
    1698                 :    82250600 :         pgBufferUsage.shared_blks_hit += 1;
    1699                 :             : 
    1700                 :    83888344 :     pgstat_count_io_op(io_object, io_context, IOOP_HIT, 1, 0);
    1701                 :             : 
    1702         [ +  + ]:    83888344 :     if (VacuumCostActive)
    1703                 :     2978392 :         VacuumCostBalance += VacuumCostPageHit;
    1704                 :             : 
    1705         [ +  + ]:    83888344 :     if (rel)
    1706   [ +  +  +  +  :    78152375 :         pgstat_count_buffer_hit(rel);
                   +  + ]
    1707                 :    83888344 : }
    1708                 :             : 
    1709                 :             : /*
    1710                 :             :  * Helper for WaitReadBuffers() that processes the results of a readv
    1711                 :             :  * operation, raising an error if necessary.
    1712                 :             :  */
    1713                 :             : static void
    1714                 :     1397141 : ProcessReadBuffersResult(ReadBuffersOperation *operation)
    1715                 :             : {
    1716                 :     1397141 :     PgAioReturn *aio_ret = &operation->io_return;
    1717                 :     1397141 :     PgAioResultStatus rs = aio_ret->result.status;
    1718                 :     1397141 :     int         newly_read_blocks = 0;
    1719                 :             : 
    1720                 :             :     Assert(pgaio_wref_valid(&operation->io_wref));
    1721                 :             :     Assert(aio_ret->result.status != PGAIO_RS_UNKNOWN);
    1722                 :             : 
    1723                 :             :     /*
    1724                 :             :      * SMGR reports the number of blocks successfully read as the result of
    1725                 :             :      * the IO operation. Thus we can simply add that to ->nblocks_done.
    1726                 :             :      */
    1727                 :             : 
    1728         [ +  + ]:     1397141 :     if (likely(rs != PGAIO_RS_ERROR))
    1729                 :     1397112 :         newly_read_blocks = aio_ret->result.result;
    1730                 :             : 
    1731   [ +  +  +  + ]:     1397141 :     if (rs == PGAIO_RS_ERROR || rs == PGAIO_RS_WARNING)
    1732         [ +  + ]:          46 :         pgaio_result_report(aio_ret->result, &aio_ret->target_data,
    1733                 :             :                             rs == PGAIO_RS_ERROR ? ERROR : WARNING);
    1734         [ +  + ]:     1397095 :     else if (aio_ret->result.status == PGAIO_RS_PARTIAL)
    1735                 :             :     {
    1736                 :             :         /*
    1737                 :             :          * We'll retry, so we just emit a debug message to the server log (or
    1738                 :             :          * not even that in prod scenarios).
    1739                 :             :          */
    1740                 :         109 :         pgaio_result_report(aio_ret->result, &aio_ret->target_data, DEBUG1);
    1741         [ +  - ]:         109 :         elog(DEBUG3, "partial read, will retry");
    1742                 :             :     }
    1743                 :             : 
    1744                 :             :     Assert(newly_read_blocks > 0);
    1745                 :             :     Assert(newly_read_blocks <= MAX_IO_COMBINE_LIMIT);
    1746                 :             : 
    1747                 :     1397112 :     operation->nblocks_done += newly_read_blocks;
    1748                 :             : 
    1749                 :             :     Assert(operation->nblocks_done <= operation->nblocks);
    1750                 :     1397112 : }
    1751                 :             : 
    1752                 :             : /*
    1753                 :             :  * Wait for the IO operation initiated by StartReadBuffers() et al to
    1754                 :             :  * complete.
    1755                 :             :  *
    1756                 :             :  * Returns true if we needed to wait for the IO operation, false otherwise.
    1757                 :             :  */
    1758                 :             : bool
    1759                 :     1399046 : WaitReadBuffers(ReadBuffersOperation *operation)
    1760                 :             : {
    1761                 :     1399046 :     PgAioReturn *aio_ret = &operation->io_return;
    1762                 :             :     IOContext   io_context;
    1763                 :             :     IOObject    io_object;
    1764                 :     1399046 :     bool        needed_wait = false;
    1765                 :             : 
    1766         [ +  + ]:     1399046 :     if (operation->persistence == RELPERSISTENCE_TEMP)
    1767                 :             :     {
    1768                 :        2429 :         io_context = IOCONTEXT_NORMAL;
    1769                 :        2429 :         io_object = IOOBJECT_TEMP_RELATION;
    1770                 :             :     }
    1771                 :             :     else
    1772                 :             :     {
    1773                 :     1396617 :         io_context = IOContextForStrategy(operation->strategy);
    1774                 :     1396617 :         io_object = IOOBJECT_RELATION;
    1775                 :             :     }
    1776                 :             : 
    1777                 :             :     /*
    1778                 :             :      * If we get here without an IO operation having been issued, the
    1779                 :             :      * io_method == IOMETHOD_SYNC path must have been used. Otherwise the
    1780                 :             :      * caller should not have called WaitReadBuffers().
    1781                 :             :      *
    1782                 :             :      * In the case of IOMETHOD_SYNC, we start - as we used to before the
    1783                 :             :      * introducing of AIO - the IO in WaitReadBuffers(). This is done as part
    1784                 :             :      * of the retry logic below, no extra code is required.
    1785                 :             :      *
    1786                 :             :      * This path is expected to eventually go away.
    1787                 :             :      */
    1788   [ +  +  +  - ]:     1399046 :     if (!pgaio_wref_valid(&operation->io_wref) && io_method != IOMETHOD_SYNC)
    1789         [ #  # ]:           0 :         elog(ERROR, "waiting for read operation that didn't read");
    1790                 :             : 
    1791                 :             :     /*
    1792                 :             :      * To handle partial reads, and IOMETHOD_SYNC, we re-issue IO until we're
    1793                 :             :      * done. We may need multiple retries, not just because we could get
    1794                 :             :      * multiple partial reads, but also because some of the remaining
    1795                 :             :      * to-be-read buffers may have been read in by other backends, limiting
    1796                 :             :      * the IO size.
    1797                 :             :      */
    1798                 :             :     while (true)
    1799                 :        1497 :     {
    1800                 :             :         int         ignored_nblocks_progress;
    1801                 :             : 
    1802                 :     1400543 :         CheckReadBuffersOperation(operation, false);
    1803                 :             : 
    1804                 :             :         /*
    1805                 :             :          * If there is an IO associated with the operation, we may need to
    1806                 :             :          * wait for it.
    1807                 :             :          */
    1808         [ +  + ]:     1400543 :         if (pgaio_wref_valid(&operation->io_wref))
    1809                 :             :         {
    1810                 :             :             /*
    1811                 :             :              * Track the time spent waiting for the IO to complete. As
    1812                 :             :              * tracking a wait even if we don't actually need to wait
    1813                 :             :              *
    1814                 :             :              * a) is not cheap, due to the timestamping overhead
    1815                 :             :              *
    1816                 :             :              * b) reports some time as waiting, even if we never waited
    1817                 :             :              *
    1818                 :             :              * we first check if we already know the IO is complete.
    1819                 :             :              *
    1820                 :             :              * Note that operation->io_return is uninitialized for foreign IO,
    1821                 :             :              * so we cannot use the cheaper PGAIO_RS_UNKNOWN pre-check.
    1822                 :             :              */
    1823   [ +  +  +  + ]:     1399151 :             if ((operation->foreign_io || aio_ret->result.status == PGAIO_RS_UNKNOWN) &&
    1824         [ +  + ]:      604428 :                 !pgaio_wref_check_done(&operation->io_wref))
    1825                 :             :             {
    1826                 :      298012 :                 instr_time  io_start = pgstat_prepare_io_time(track_io_timing);
    1827                 :             : 
    1828                 :      298012 :                 pgaio_wref_wait(&operation->io_wref);
    1829                 :      298012 :                 needed_wait = true;
    1830                 :             : 
    1831                 :             :                 /*
    1832                 :             :                  * The IO operation itself was already counted earlier, in
    1833                 :             :                  * AsyncReadBuffers(), this just accounts for the wait time.
    1834                 :             :                  */
    1835                 :      298012 :                 pgstat_count_io_op_time(io_object, io_context, IOOP_READ,
    1836                 :             :                                         io_start, 0, 0);
    1837                 :             :             }
    1838                 :             :             else
    1839                 :             :             {
    1840                 :             :                 Assert(pgaio_wref_check_done(&operation->io_wref));
    1841                 :             :             }
    1842                 :             : 
    1843         [ +  + ]:     1399151 :             if (unlikely(operation->foreign_io))
    1844                 :             :             {
    1845                 :        2010 :                 Buffer      buffer = operation->buffers[operation->nblocks_done];
    1846                 :        2010 :                 BufferDesc *desc = BufferIsLocal(buffer) ?
    1847         [ -  + ]:        2010 :                     GetLocalBufferDescriptor(-buffer - 1) :
    1848                 :        2010 :                     GetBufferDescriptor(buffer - 1);
    1849                 :        2010 :                 uint64      buf_state = pg_atomic_read_u64(&desc->state);
    1850                 :             : 
    1851         [ +  + ]:        2010 :                 if (buf_state & BM_VALID)
    1852                 :             :                 {
    1853                 :        2008 :                     BlockNumber blocknum = operation->blocknum + operation->nblocks_done;
    1854                 :             : 
    1855                 :        2008 :                     operation->nblocks_done += 1;
    1856                 :             :                     Assert(operation->nblocks_done <= operation->nblocks);
    1857                 :             : 
    1858                 :             :                     /*
    1859                 :             :                      * Track this as a 'hit' for this backend. The backend
    1860                 :             :                      * performing the IO will track it as a 'read'.
    1861                 :             :                      */
    1862                 :        2008 :                     TrackBufferHit(io_object, io_context,
    1863                 :        2008 :                                    operation->rel, operation->persistence,
    1864                 :             :                                    operation->smgr, operation->forknum,
    1865                 :             :                                    blocknum);
    1866                 :             :                 }
    1867                 :             : 
    1868                 :             :                 /*
    1869                 :             :                  * If the foreign IO failed and left the buffer invalid,
    1870                 :             :                  * nblocks_done is not incremented. The retry loop below will
    1871                 :             :                  * call AsyncReadBuffers() which will attempt the IO itself.
    1872                 :             :                  */
    1873                 :             :             }
    1874                 :             :             else
    1875                 :             :             {
    1876                 :             :                 /*
    1877                 :             :                  * We now are sure the IO completed. Check the results. This
    1878                 :             :                  * includes reporting on errors if there were any.
    1879                 :             :                  */
    1880                 :     1397141 :                 ProcessReadBuffersResult(operation);
    1881                 :             :             }
    1882                 :             :         }
    1883                 :             : 
    1884                 :             :         /*
    1885                 :             :          * Most of the time, the one IO we already started, will read in
    1886                 :             :          * everything.  But we need to deal with partial reads and buffers not
    1887                 :             :          * needing IO anymore.
    1888                 :             :          */
    1889         [ +  + ]:     1400514 :         if (operation->nblocks_done == operation->nblocks)
    1890                 :     1399017 :             break;
    1891                 :             : 
    1892         [ -  + ]:        1497 :         CHECK_FOR_INTERRUPTS();
    1893                 :             : 
    1894                 :             :         /*
    1895                 :             :          * If the IO completed only partially, we need to perform additional
    1896                 :             :          * work, consider that a form of having had to wait.
    1897                 :             :          */
    1898                 :        1497 :         needed_wait = true;
    1899                 :             : 
    1900                 :             :         /*
    1901                 :             :          * This may only complete the IO partially, either because some
    1902                 :             :          * buffers were already valid, or because of a partial read.
    1903                 :             :          *
    1904                 :             :          * NB: In contrast to after the AsyncReadBuffers() call in
    1905                 :             :          * StartReadBuffers(), we do *not* reduce
    1906                 :             :          * ReadBuffersOperation->nblocks here, callers expect the full
    1907                 :             :          * operation to be completed at this point (as more operations may
    1908                 :             :          * have been queued).
    1909                 :             :          */
    1910                 :        1497 :         AsyncReadBuffers(operation, &ignored_nblocks_progress);
    1911                 :             :     }
    1912                 :             : 
    1913                 :     1399017 :     CheckReadBuffersOperation(operation, true);
    1914                 :             : 
    1915                 :             :     /* NB: READ_DONE tracepoint was already executed in completion callback */
    1916                 :     1399017 :     return needed_wait;
    1917                 :             : }
    1918                 :             : 
    1919                 :             : /*
    1920                 :             :  * Initiate IO for the ReadBuffersOperation
    1921                 :             :  *
    1922                 :             :  * This function only starts a single IO at a time. The size of the IO may be
    1923                 :             :  * limited to below the to-be-read blocks, if one of the buffers has
    1924                 :             :  * concurrently been read in. If the first to-be-read buffer is already valid,
    1925                 :             :  * no IO will be issued.
    1926                 :             :  *
    1927                 :             :  * To support retries after partial reads, the first operation->nblocks_done
    1928                 :             :  * buffers are skipped.
    1929                 :             :  *
    1930                 :             :  * On return *nblocks_progress is updated to reflect the number of buffers
    1931                 :             :  * affected by the call. If the first buffer is valid, *nblocks_progress is
    1932                 :             :  * set to 1 and operation->nblocks_done is incremented.
    1933                 :             :  *
    1934                 :             :  * Returns true if IO was initiated or is already in progress (foreign IO),
    1935                 :             :  * false if the buffer was already valid.
    1936                 :             :  */
    1937                 :             : static bool
    1938                 :     1399890 : AsyncReadBuffers(ReadBuffersOperation *operation, int *nblocks_progress)
    1939                 :             : {
    1940                 :     1399890 :     Buffer     *buffers = &operation->buffers[0];
    1941                 :     1399890 :     int         flags = operation->flags;
    1942                 :     1399890 :     ForkNumber  forknum = operation->forknum;
    1943                 :     1399890 :     char        persistence = operation->persistence;
    1944                 :     1399890 :     int16       nblocks_done = operation->nblocks_done;
    1945                 :     1399890 :     BlockNumber blocknum = operation->blocknum + nblocks_done;
    1946                 :     1399890 :     Buffer     *io_buffers = &operation->buffers[nblocks_done];
    1947                 :     1399890 :     int         io_buffers_len = 0;
    1948                 :             :     PgAioHandle *ioh;
    1949                 :     1399890 :     uint32      ioh_flags = 0;
    1950                 :             :     void       *io_pages[MAX_IO_COMBINE_LIMIT];
    1951                 :             :     IOContext   io_context;
    1952                 :             :     IOObject    io_object;
    1953                 :             :     instr_time  io_start;
    1954                 :             :     StartBufferIOResult status;
    1955                 :             : 
    1956         [ +  + ]:     1399890 :     if (persistence == RELPERSISTENCE_TEMP)
    1957                 :             :     {
    1958                 :        2821 :         io_context = IOCONTEXT_NORMAL;
    1959                 :        2821 :         io_object = IOOBJECT_TEMP_RELATION;
    1960                 :             :     }
    1961                 :             :     else
    1962                 :             :     {
    1963                 :     1397069 :         io_context = IOContextForStrategy(operation->strategy);
    1964                 :     1397069 :         io_object = IOOBJECT_RELATION;
    1965                 :             :     }
    1966                 :             : 
    1967                 :             :     /*
    1968                 :             :      * When this IO is executed synchronously, either because the caller will
    1969                 :             :      * immediately block waiting for the IO or because IOMETHOD_SYNC is used,
    1970                 :             :      * the AIO subsystem needs to know.
    1971                 :             :      */
    1972         [ +  + ]:     1399890 :     if (flags & READ_BUFFERS_SYNCHRONOUSLY)
    1973                 :      786810 :         ioh_flags |= PGAIO_HF_SYNCHRONOUS;
    1974                 :             : 
    1975         [ +  + ]:     1399890 :     if (persistence == RELPERSISTENCE_TEMP)
    1976                 :        2821 :         ioh_flags |= PGAIO_HF_REFERENCES_LOCAL;
    1977                 :             : 
    1978                 :             :     /*
    1979                 :             :      * If zero_damaged_pages is enabled, add the READ_BUFFERS_ZERO_ON_ERROR
    1980                 :             :      * flag. The reason for that is that, hopefully, zero_damaged_pages isn't
    1981                 :             :      * set globally, but on a per-session basis. The completion callback,
    1982                 :             :      * which may be run in other processes, e.g. in IO workers, may have a
    1983                 :             :      * different value of the zero_damaged_pages GUC.
    1984                 :             :      *
    1985                 :             :      * XXX: We probably should eventually use a different flag for
    1986                 :             :      * zero_damaged_pages, so we can report different log levels / error codes
    1987                 :             :      * for zero_damaged_pages and ZERO_ON_ERROR.
    1988                 :             :      */
    1989         [ +  + ]:     1399890 :     if (zero_damaged_pages)
    1990                 :          16 :         flags |= READ_BUFFERS_ZERO_ON_ERROR;
    1991                 :             : 
    1992                 :             :     /*
    1993                 :             :      * For the same reason as with zero_damaged_pages we need to use this
    1994                 :             :      * backend's ignore_checksum_failure value.
    1995                 :             :      */
    1996         [ +  + ]:     1399890 :     if (ignore_checksum_failure)
    1997                 :           8 :         flags |= READ_BUFFERS_IGNORE_CHECKSUM_FAILURES;
    1998                 :             : 
    1999                 :             : 
    2000                 :             :     /*
    2001                 :             :      * To be allowed to report stats in the local completion callback we need
    2002                 :             :      * to prepare to report stats now. This ensures we can safely report the
    2003                 :             :      * checksum failure even in a critical section.
    2004                 :             :      */
    2005                 :     1399890 :     pgstat_prepare_report_checksum_failure(operation->smgr->smgr_rlocator.locator.dbOid);
    2006                 :             : 
    2007                 :             :     /*
    2008                 :             :      * We must get an IO handle before StartBufferIO(), as pgaio_io_acquire()
    2009                 :             :      * might block, which we don't want after setting IO_IN_PROGRESS. If we
    2010                 :             :      * don't need to do the IO, we'll release the handle.
    2011                 :             :      *
    2012                 :             :      * If we need to wait for IO before we can get a handle, submit
    2013                 :             :      * already-staged IO first, so that other backends don't need to wait.
    2014                 :             :      * There wouldn't be a deadlock risk, as pgaio_io_acquire() just needs to
    2015                 :             :      * wait for already submitted IO, which doesn't require additional locks,
    2016                 :             :      * but it could still cause undesirable waits.
    2017                 :             :      *
    2018                 :             :      * A secondary benefit is that this would allow us to measure the time in
    2019                 :             :      * pgaio_io_acquire() without causing undue timer overhead in the common,
    2020                 :             :      * non-blocking, case.  However, currently the pgstats infrastructure
    2021                 :             :      * doesn't really allow that, as it a) asserts that an operation can't
    2022                 :             :      * have time without operations b) doesn't have an API to report
    2023                 :             :      * "accumulated" time.
    2024                 :             :      */
    2025                 :     1399890 :     ioh = pgaio_io_acquire_nb(CurrentResourceOwner, &operation->io_return);
    2026         [ +  + ]:     1399890 :     if (unlikely(!ioh))
    2027                 :             :     {
    2028                 :        3120 :         pgaio_submit_staged();
    2029                 :        3120 :         ioh = pgaio_io_acquire(CurrentResourceOwner, &operation->io_return);
    2030                 :             :     }
    2031                 :             : 
    2032                 :     1399890 :     operation->foreign_io = false;
    2033                 :     1399890 :     pgaio_wref_clear(&operation->io_wref);
    2034                 :             : 
    2035                 :             :     /*
    2036                 :             :      * Try to start IO on the first buffer in a new run of blocks. If AIO is
    2037                 :             :      * in progress, be it in this backend or another backend, we just
    2038                 :             :      * associate the wait reference with the operation and wait in
    2039                 :             :      * WaitReadBuffers(). This turns out to be important for performance in
    2040                 :             :      * two workloads:
    2041                 :             :      *
    2042                 :             :      * 1) A read stream that has to read the same block multiple times within
    2043                 :             :      * the readahead distance. This can happen e.g. for the table accesses of
    2044                 :             :      * an index scan.
    2045                 :             :      *
    2046                 :             :      * 2) Concurrent scans by multiple backends on the same relation.
    2047                 :             :      *
    2048                 :             :      * If we were to synchronously wait for the in-progress IO, we'd not be
    2049                 :             :      * able to keep enough I/O in flight.
    2050                 :             :      *
    2051                 :             :      * If we do find there is ongoing I/O for the buffer, we set up a 1-block
    2052                 :             :      * ReadBuffersOperation that WaitReadBuffers then can wait on.
    2053                 :             :      *
    2054                 :             :      * It's possible that another backend has started IO on the buffer but not
    2055                 :             :      * yet set its wait reference. In this case, we have no choice but to wait
    2056                 :             :      * for either the wait reference to be valid or the IO to be done.
    2057                 :             :      */
    2058                 :     1399890 :     status = StartBufferIO(buffers[nblocks_done], true, true,
    2059                 :             :                            &operation->io_wref);
    2060         [ +  + ]:     1399890 :     if (status != BUFFER_IO_READY_FOR_IO)
    2061                 :             :     {
    2062                 :        2338 :         pgaio_io_release(ioh);
    2063                 :        2338 :         *nblocks_progress = 1;
    2064         [ +  + ]:        2338 :         if (status == BUFFER_IO_ALREADY_DONE)
    2065                 :             :         {
    2066                 :             :             /*
    2067                 :             :              * Someone has already completed this block, we're done.
    2068                 :             :              *
    2069                 :             :              * When IO is necessary, ->nblocks_done is updated in
    2070                 :             :              * ProcessReadBuffersResult(), but that is not called if no IO is
    2071                 :             :              * necessary. Thus update here.
    2072                 :             :              */
    2073                 :         328 :             operation->nblocks_done += 1;
    2074                 :             :             Assert(operation->nblocks_done <= operation->nblocks);
    2075                 :             : 
    2076                 :             :             Assert(!pgaio_wref_valid(&operation->io_wref));
    2077                 :             : 
    2078                 :             :             /*
    2079                 :             :              * Report and track this as a 'hit' for this backend, even though
    2080                 :             :              * it must have started out as a miss in PinBufferForBlock(). The
    2081                 :             :              * other backend will track this as a 'read'.
    2082                 :             :              */
    2083                 :         328 :             TrackBufferHit(io_object, io_context,
    2084                 :         328 :                            operation->rel, operation->persistence,
    2085                 :             :                            operation->smgr, operation->forknum,
    2086                 :             :                            blocknum);
    2087                 :         328 :             return false;
    2088                 :             :         }
    2089                 :             : 
    2090                 :             :         /* The IO is already in-progress */
    2091                 :             :         Assert(status == BUFFER_IO_IN_PROGRESS);
    2092                 :             :         Assert(pgaio_wref_valid(&operation->io_wref));
    2093                 :        2010 :         operation->foreign_io = true;
    2094                 :             : 
    2095                 :        2010 :         return true;
    2096                 :             :     }
    2097                 :             : 
    2098                 :             :     Assert(io_buffers[0] == buffers[nblocks_done]);
    2099                 :     1397552 :     io_pages[0] = BufferGetBlock(buffers[nblocks_done]);
    2100                 :     1397552 :     io_buffers_len = 1;
    2101                 :             : 
    2102                 :             :     /*
    2103                 :             :      * NB: As little code as possible should be added between the
    2104                 :             :      * StartBufferIO() above, the further StartBufferIO()s below and the
    2105                 :             :      * smgrstartreadv(), as some of the buffers are now marked as
    2106                 :             :      * IO_IN_PROGRESS and will thus cause other backends to wait.
    2107                 :             :      */
    2108                 :             : 
    2109                 :             :     /*
    2110                 :             :      * How many neighboring-on-disk blocks can we scatter-read into other
    2111                 :             :      * buffers at the same time?  In this case we don't wait if we see an I/O
    2112                 :             :      * already in progress (see comment above).
    2113                 :             :      */
    2114         [ +  + ]:     1582318 :     for (int i = nblocks_done + 1; i < operation->nblocks; i++)
    2115                 :             :     {
    2116                 :             :         /* Must be consecutive block numbers. */
    2117                 :             :         Assert(BufferGetBlockNumber(buffers[i - 1]) ==
    2118                 :             :                BufferGetBlockNumber(buffers[i]) - 1);
    2119                 :             : 
    2120                 :      184770 :         status = StartBufferIO(buffers[i], true, false, NULL);
    2121         [ +  + ]:      184770 :         if (status != BUFFER_IO_READY_FOR_IO)
    2122                 :           4 :             break;
    2123                 :             : 
    2124                 :             :         Assert(io_buffers[io_buffers_len] == buffers[i]);
    2125                 :             : 
    2126                 :      184766 :         io_pages[io_buffers_len++] = BufferGetBlock(buffers[i]);
    2127                 :             :     }
    2128                 :             : 
    2129                 :             :     /* get a reference to wait for in WaitReadBuffers() */
    2130                 :     1397552 :     pgaio_io_get_wref(ioh, &operation->io_wref);
    2131                 :             : 
    2132                 :             :     /* provide the list of buffers to the completion callbacks */
    2133                 :     1397552 :     pgaio_io_set_handle_data_32(ioh, (uint32 *) io_buffers, io_buffers_len);
    2134                 :             : 
    2135         [ +  + ]:     1397552 :     pgaio_io_register_callbacks(ioh,
    2136                 :             :                                 persistence == RELPERSISTENCE_TEMP ?
    2137                 :             :                                 PGAIO_HCB_LOCAL_BUFFER_READV :
    2138                 :             :                                 PGAIO_HCB_SHARED_BUFFER_READV,
    2139                 :             :                                 flags);
    2140                 :             : 
    2141                 :     1397552 :     pgaio_io_set_flag(ioh, ioh_flags);
    2142                 :             : 
    2143                 :             :     /* ---
    2144                 :             :      * Even though we're trying to issue IO asynchronously, track the time
    2145                 :             :      * in smgrstartreadv():
    2146                 :             :      * - if io_method == IOMETHOD_SYNC, we will always perform the IO
    2147                 :             :      *   immediately
    2148                 :             :      * - the io method might not support the IO (e.g. worker IO for a temp
    2149                 :             :      *   table)
    2150                 :             :      * ---
    2151                 :             :      */
    2152                 :     1397552 :     io_start = pgstat_prepare_io_time(track_io_timing);
    2153                 :     1397552 :     smgrstartreadv(ioh, operation->smgr, forknum,
    2154                 :             :                    blocknum,
    2155                 :             :                    io_pages, io_buffers_len);
    2156                 :     1397537 :     pgstat_count_io_op_time(io_object, io_context, IOOP_READ,
    2157                 :     1397537 :                             io_start, 1, io_buffers_len * BLCKSZ);
    2158                 :             : 
    2159         [ +  + ]:     1397537 :     if (persistence == RELPERSISTENCE_TEMP)
    2160                 :        2819 :         pgBufferUsage.local_blks_read += io_buffers_len;
    2161                 :             :     else
    2162                 :     1394718 :         pgBufferUsage.shared_blks_read += io_buffers_len;
    2163                 :             : 
    2164                 :             :     /*
    2165                 :             :      * Track vacuum cost when issuing IO, not after waiting for it. Otherwise
    2166                 :             :      * we could end up issuing a lot of IO in a short timespan, despite a low
    2167                 :             :      * cost limit.
    2168                 :             :      */
    2169         [ +  + ]:     1397537 :     if (VacuumCostActive)
    2170                 :       21345 :         VacuumCostBalance += VacuumCostPageMiss * io_buffers_len;
    2171                 :             : 
    2172                 :     1397537 :     *nblocks_progress = io_buffers_len;
    2173                 :             : 
    2174                 :     1397537 :     return true;
    2175                 :             : }
    2176                 :             : 
    2177                 :             : /*
    2178                 :             :  * BufferAlloc -- subroutine for PinBufferForBlock.  Handles lookup of a shared
    2179                 :             :  *      buffer.  If no buffer exists already, selects a replacement victim and
    2180                 :             :  *      evicts the old page, but does NOT read in new page.
    2181                 :             :  *
    2182                 :             :  * "strategy" can be a buffer replacement strategy object, or NULL for
    2183                 :             :  * the default strategy.  The selected buffer's usage_count is advanced when
    2184                 :             :  * using the default strategy, but otherwise possibly not (see PinBuffer).
    2185                 :             :  *
    2186                 :             :  * The returned buffer is pinned and is already marked as holding the
    2187                 :             :  * desired page.  If it already did have the desired page, *foundPtr is
    2188                 :             :  * set true.  Otherwise, *foundPtr is set false.
    2189                 :             :  *
    2190                 :             :  * io_context is passed as an output parameter to avoid calling
    2191                 :             :  * IOContextForStrategy() when there is a shared buffers hit and no IO
    2192                 :             :  * statistics need be captured.
    2193                 :             :  *
    2194                 :             :  * No locks are held either at entry or exit.
    2195                 :             :  */
    2196                 :             : static pg_always_inline BufferDesc *
    2197                 :    84138342 : BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
    2198                 :             :             BlockNumber blockNum,
    2199                 :             :             BufferAccessStrategy strategy,
    2200                 :             :             bool *foundPtr, IOContext io_context)
    2201                 :             : {
    2202                 :             :     BufferTag   newTag;         /* identity of requested block */
    2203                 :             :     uint32      newHash;        /* hash value for newTag */
    2204                 :             :     LWLock     *newPartitionLock;   /* buffer partition lock for it */
    2205                 :             :     int         existing_buf_id;
    2206                 :             :     Buffer      victim_buffer;
    2207                 :             :     BufferDesc *victim_buf_hdr;
    2208                 :             :     uint64      victim_buf_state;
    2209                 :    84138342 :     uint64      set_bits = 0;
    2210                 :             : 
    2211                 :             :     /* Make sure we will have room to remember the buffer pin */
    2212                 :    84138342 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    2213                 :    84138342 :     ReservePrivateRefCountEntry();
    2214                 :             : 
    2215                 :             :     /* create a tag so we can lookup the buffer */
    2216                 :    84138342 :     InitBufferTag(&newTag, &smgr->smgr_rlocator.locator, forkNum, blockNum);
    2217                 :             : 
    2218                 :             :     /* determine its hash code and partition lock ID */
    2219                 :    84138342 :     newHash = BufTableHashCode(&newTag);
    2220                 :    84138342 :     newPartitionLock = BufMappingPartitionLock(newHash);
    2221                 :             : 
    2222                 :             :     /* see if the block is in the buffer pool already */
    2223                 :    84138342 :     LWLockAcquire(newPartitionLock, LW_SHARED);
    2224                 :    84138342 :     existing_buf_id = BufTableLookup(&newTag, newHash);
    2225         [ +  + ]:    84138342 :     if (existing_buf_id >= 0)
    2226                 :             :     {
    2227                 :             :         BufferDesc *buf;
    2228                 :             :         bool        valid;
    2229                 :             : 
    2230                 :             :         /*
    2231                 :             :          * Found it.  Now, pin the buffer so no one can steal it from the
    2232                 :             :          * buffer pool, and check to see if the correct data has been loaded
    2233                 :             :          * into the buffer.
    2234                 :             :          */
    2235                 :    82250040 :         buf = GetBufferDescriptor(existing_buf_id);
    2236                 :             : 
    2237                 :    82250040 :         valid = PinBuffer(buf, strategy, false);
    2238                 :             : 
    2239                 :             :         /* Can release the mapping lock as soon as we've pinned it */
    2240                 :    82250040 :         LWLockRelease(newPartitionLock);
    2241                 :             : 
    2242                 :    82250040 :         *foundPtr = true;
    2243                 :             : 
    2244         [ +  + ]:    82250040 :         if (!valid)
    2245                 :             :         {
    2246                 :             :             /*
    2247                 :             :              * We can only get here if (a) someone else is still reading in
    2248                 :             :              * the page, (b) a previous read attempt failed, or (c) someone
    2249                 :             :              * called StartReadBuffers() but not yet WaitReadBuffers().
    2250                 :             :              */
    2251                 :        2002 :             *foundPtr = false;
    2252                 :             :         }
    2253                 :             : 
    2254                 :    82250040 :         return buf;
    2255                 :             :     }
    2256                 :             : 
    2257                 :             :     /*
    2258                 :             :      * Didn't find it in the buffer pool.  We'll have to initialize a new
    2259                 :             :      * buffer.  Remember to unlock the mapping lock while doing the work.
    2260                 :             :      */
    2261                 :     1888302 :     LWLockRelease(newPartitionLock);
    2262                 :             : 
    2263                 :             :     /*
    2264                 :             :      * Acquire a victim buffer. Somebody else might try to do the same, we
    2265                 :             :      * don't hold any conflicting locks. If so we'll have to undo our work
    2266                 :             :      * later.
    2267                 :             :      */
    2268                 :     1888302 :     victim_buffer = GetVictimBuffer(strategy, io_context);
    2269                 :     1888302 :     victim_buf_hdr = GetBufferDescriptor(victim_buffer - 1);
    2270                 :             : 
    2271                 :             :     /*
    2272                 :             :      * Try to make a hashtable entry for the buffer under its new tag. If
    2273                 :             :      * somebody else inserted another buffer for the tag, we'll release the
    2274                 :             :      * victim buffer we acquired and use the already inserted one.
    2275                 :             :      */
    2276                 :     1888302 :     LWLockAcquire(newPartitionLock, LW_EXCLUSIVE);
    2277                 :     1888302 :     existing_buf_id = BufTableInsert(&newTag, newHash, victim_buf_hdr->buf_id);
    2278         [ +  + ]:     1888302 :     if (existing_buf_id >= 0)
    2279                 :             :     {
    2280                 :             :         BufferDesc *existing_buf_hdr;
    2281                 :             :         bool        valid;
    2282                 :             : 
    2283                 :             :         /*
    2284                 :             :          * Got a collision. Someone has already done what we were about to do.
    2285                 :             :          * We'll just handle this as if it were found in the buffer pool in
    2286                 :             :          * the first place.  First, give up the buffer we were planning to
    2287                 :             :          * use.
    2288                 :             :          *
    2289                 :             :          * We could do this after releasing the partition lock, but then we'd
    2290                 :             :          * have to call ResourceOwnerEnlarge() & ReservePrivateRefCountEntry()
    2291                 :             :          * before acquiring the lock, for the rare case of such a collision.
    2292                 :             :          */
    2293                 :         612 :         UnpinBuffer(victim_buf_hdr);
    2294                 :             : 
    2295                 :             :         /* remaining code should match code at top of routine */
    2296                 :             : 
    2297                 :         612 :         existing_buf_hdr = GetBufferDescriptor(existing_buf_id);
    2298                 :             : 
    2299                 :         612 :         valid = PinBuffer(existing_buf_hdr, strategy, false);
    2300                 :             : 
    2301                 :             :         /* Can release the mapping lock as soon as we've pinned it */
    2302                 :         612 :         LWLockRelease(newPartitionLock);
    2303                 :             : 
    2304                 :         612 :         *foundPtr = true;
    2305                 :             : 
    2306         [ +  + ]:         612 :         if (!valid)
    2307                 :             :         {
    2308                 :             :             /*
    2309                 :             :              * We can only get here if (a) someone else is still reading in
    2310                 :             :              * the page, (b) a previous read attempt failed, or (c) someone
    2311                 :             :              * called StartReadBuffers() but not yet WaitReadBuffers().
    2312                 :             :              */
    2313                 :         384 :             *foundPtr = false;
    2314                 :             :         }
    2315                 :             : 
    2316                 :         612 :         return existing_buf_hdr;
    2317                 :             :     }
    2318                 :             : 
    2319                 :             :     /*
    2320                 :             :      * Need to lock the buffer header too in order to change its tag.
    2321                 :             :      */
    2322                 :     1887690 :     victim_buf_state = LockBufHdr(victim_buf_hdr);
    2323                 :             : 
    2324                 :             :     /* some sanity checks while we hold the buffer header lock */
    2325                 :             :     Assert(BUF_STATE_GET_REFCOUNT(victim_buf_state) == 1);
    2326                 :             :     Assert(!(victim_buf_state & (BM_TAG_VALID | BM_VALID | BM_DIRTY | BM_IO_IN_PROGRESS)));
    2327                 :             : 
    2328                 :     1887690 :     victim_buf_hdr->tag = newTag;
    2329                 :             : 
    2330                 :             :     /*
    2331                 :             :      * Make sure BM_PERMANENT is set for buffers that must be written at every
    2332                 :             :      * checkpoint.  Unlogged buffers only need to be written at shutdown
    2333                 :             :      * checkpoints, except for their "init" forks, which need to be treated
    2334                 :             :      * just like permanent relations.
    2335                 :             :      */
    2336                 :     1887690 :     set_bits |= BM_TAG_VALID | BUF_USAGECOUNT_ONE;
    2337   [ +  +  +  + ]:     1887690 :     if (relpersistence == RELPERSISTENCE_PERMANENT || forkNum == INIT_FORKNUM)
    2338                 :     1887301 :         set_bits |= BM_PERMANENT;
    2339                 :             : 
    2340                 :     1887690 :     UnlockBufHdrExt(victim_buf_hdr, victim_buf_state,
    2341                 :             :                     set_bits, 0, 0);
    2342                 :             : 
    2343                 :     1887690 :     LWLockRelease(newPartitionLock);
    2344                 :             : 
    2345                 :             :     /*
    2346                 :             :      * Buffer contents are currently invalid.
    2347                 :             :      */
    2348                 :     1887690 :     *foundPtr = false;
    2349                 :             : 
    2350                 :     1887690 :     return victim_buf_hdr;
    2351                 :             : }
    2352                 :             : 
    2353                 :             : /*
    2354                 :             :  * InvalidateBuffer -- mark a shared buffer invalid.
    2355                 :             :  *
    2356                 :             :  * The buffer header spinlock must be held at entry.  We drop it before
    2357                 :             :  * returning.  (This is sane because the caller must have locked the
    2358                 :             :  * buffer in order to be sure it should be dropped.)
    2359                 :             :  *
    2360                 :             :  * This is used only in contexts such as dropping a relation.  We assume
    2361                 :             :  * that no other backend could possibly be interested in using the page,
    2362                 :             :  * so the only reason the buffer might be pinned is if someone else is
    2363                 :             :  * trying to write it out.  We have to let them finish before we can
    2364                 :             :  * reclaim the buffer.
    2365                 :             :  *
    2366                 :             :  * The buffer could get reclaimed by someone else while we are waiting
    2367                 :             :  * to acquire the necessary locks; if so, don't mess it up.
    2368                 :             :  */
    2369                 :             : static void
    2370                 :      127443 : InvalidateBuffer(BufferDesc *buf)
    2371                 :             : {
    2372                 :             :     BufferTag   oldTag;
    2373                 :             :     uint32      oldHash;        /* hash value for oldTag */
    2374                 :             :     LWLock     *oldPartitionLock;   /* buffer partition lock for it */
    2375                 :             :     uint32      oldFlags;
    2376                 :             :     uint64      buf_state;
    2377                 :             : 
    2378                 :             :     /* Save the original buffer tag before dropping the spinlock */
    2379                 :      127443 :     oldTag = buf->tag;
    2380                 :             : 
    2381                 :      127443 :     UnlockBufHdr(buf);
    2382                 :             : 
    2383                 :             :     /*
    2384                 :             :      * Need to compute the old tag's hashcode and partition lock ID. XXX is it
    2385                 :             :      * worth storing the hashcode in BufferDesc so we need not recompute it
    2386                 :             :      * here?  Probably not.
    2387                 :             :      */
    2388                 :      127443 :     oldHash = BufTableHashCode(&oldTag);
    2389                 :      127443 :     oldPartitionLock = BufMappingPartitionLock(oldHash);
    2390                 :             : 
    2391                 :      127443 : retry:
    2392                 :             : 
    2393                 :             :     /*
    2394                 :             :      * Acquire exclusive mapping lock in preparation for changing the buffer's
    2395                 :             :      * association.
    2396                 :             :      */
    2397                 :      127443 :     LWLockAcquire(oldPartitionLock, LW_EXCLUSIVE);
    2398                 :             : 
    2399                 :             :     /* Re-lock the buffer header */
    2400                 :      127443 :     buf_state = LockBufHdr(buf);
    2401                 :             : 
    2402                 :             :     /* If it's changed while we were waiting for lock, do nothing */
    2403         [ -  + ]:      127443 :     if (!BufferTagsEqual(&buf->tag, &oldTag))
    2404                 :             :     {
    2405                 :           0 :         UnlockBufHdr(buf);
    2406                 :           0 :         LWLockRelease(oldPartitionLock);
    2407                 :           0 :         return;
    2408                 :             :     }
    2409                 :             : 
    2410                 :             :     /*
    2411                 :             :      * We assume the reason for it to be pinned is that either we were
    2412                 :             :      * asynchronously reading the page in before erroring out or someone else
    2413                 :             :      * is flushing the page out.  Wait for the IO to finish.  (This could be
    2414                 :             :      * an infinite loop if the refcount is messed up... it would be nice to
    2415                 :             :      * time out after awhile, but there seems no way to be sure how many loops
    2416                 :             :      * may be needed.  Note that if the other guy has pinned the buffer but
    2417                 :             :      * not yet done StartBufferIO, WaitIO will fall through and we'll
    2418                 :             :      * effectively be busy-looping here.)
    2419                 :             :      */
    2420         [ -  + ]:      127443 :     if (BUF_STATE_GET_REFCOUNT(buf_state) != 0)
    2421                 :             :     {
    2422                 :           0 :         UnlockBufHdr(buf);
    2423                 :           0 :         LWLockRelease(oldPartitionLock);
    2424                 :             :         /* safety check: should definitely not be our *own* pin */
    2425         [ #  # ]:           0 :         if (GetPrivateRefCount(BufferDescriptorGetBuffer(buf)) > 0)
    2426         [ #  # ]:           0 :             elog(ERROR, "buffer is pinned in InvalidateBuffer");
    2427                 :           0 :         WaitIO(buf);
    2428                 :           0 :         goto retry;
    2429                 :             :     }
    2430                 :             : 
    2431                 :             :     /*
    2432                 :             :      * An invalidated buffer should not have any backends waiting to lock the
    2433                 :             :      * buffer, therefore BM_LOCK_WAKE_IN_PROGRESS should not be set.
    2434                 :             :      */
    2435                 :             :     Assert(!(buf_state & BM_LOCK_WAKE_IN_PROGRESS));
    2436                 :             : 
    2437                 :             :     /*
    2438                 :             :      * Clear out the buffer's tag and flags.  We must do this to ensure that
    2439                 :             :      * linear scans of the buffer array don't think the buffer is valid.
    2440                 :             :      */
    2441                 :      127443 :     oldFlags = buf_state & BUF_FLAG_MASK;
    2442                 :      127443 :     ClearBufferTag(&buf->tag);
    2443                 :             : 
    2444                 :      127443 :     UnlockBufHdrExt(buf, buf_state,
    2445                 :             :                     0,
    2446                 :             :                     BUF_FLAG_MASK | BUF_USAGECOUNT_MASK,
    2447                 :             :                     0);
    2448                 :             : 
    2449                 :             :     /*
    2450                 :             :      * Remove the buffer from the lookup hashtable, if it was in there.
    2451                 :             :      */
    2452         [ +  - ]:      127443 :     if (oldFlags & BM_TAG_VALID)
    2453                 :      127443 :         BufTableDelete(&oldTag, oldHash);
    2454                 :             : 
    2455                 :             :     /*
    2456                 :             :      * Done with mapping lock.
    2457                 :             :      */
    2458                 :      127443 :     LWLockRelease(oldPartitionLock);
    2459                 :             : }
    2460                 :             : 
    2461                 :             : /*
    2462                 :             :  * Helper routine for GetVictimBuffer()
    2463                 :             :  *
    2464                 :             :  * Needs to be called on a buffer with a valid tag, pinned, but without the
    2465                 :             :  * buffer header spinlock held.
    2466                 :             :  *
    2467                 :             :  * Returns true if the buffer can be reused, in which case the buffer is only
    2468                 :             :  * pinned by this backend and marked as invalid, false otherwise.
    2469                 :             :  */
    2470                 :             : static bool
    2471                 :     1332857 : InvalidateVictimBuffer(BufferDesc *buf_hdr)
    2472                 :             : {
    2473                 :             :     uint64      buf_state;
    2474                 :             :     uint32      hash;
    2475                 :             :     LWLock     *partition_lock;
    2476                 :             :     BufferTag   tag;
    2477                 :             : 
    2478                 :             :     Assert(GetPrivateRefCount(BufferDescriptorGetBuffer(buf_hdr)) == 1);
    2479                 :             : 
    2480                 :             :     /* have buffer pinned, so it's safe to read tag without lock */
    2481                 :     1332857 :     tag = buf_hdr->tag;
    2482                 :             : 
    2483                 :     1332857 :     hash = BufTableHashCode(&tag);
    2484                 :     1332857 :     partition_lock = BufMappingPartitionLock(hash);
    2485                 :             : 
    2486                 :     1332857 :     LWLockAcquire(partition_lock, LW_EXCLUSIVE);
    2487                 :             : 
    2488                 :             :     /* lock the buffer header */
    2489                 :     1332857 :     buf_state = LockBufHdr(buf_hdr);
    2490                 :             : 
    2491                 :             :     /*
    2492                 :             :      * We have the buffer pinned nobody else should have been able to unset
    2493                 :             :      * this concurrently.
    2494                 :             :      */
    2495                 :             :     Assert(buf_state & BM_TAG_VALID);
    2496                 :             :     Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    2497                 :             :     Assert(BufferTagsEqual(&buf_hdr->tag, &tag));
    2498                 :             : 
    2499                 :             :     /*
    2500                 :             :      * If somebody else pinned the buffer since, or even worse, dirtied it,
    2501                 :             :      * give up on this buffer: It's clearly in use.
    2502                 :             :      */
    2503   [ +  +  +  + ]:     1332857 :     if (BUF_STATE_GET_REFCOUNT(buf_state) != 1 || (buf_state & BM_DIRTY))
    2504                 :             :     {
    2505                 :             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    2506                 :             : 
    2507                 :         402 :         UnlockBufHdr(buf_hdr);
    2508                 :         402 :         LWLockRelease(partition_lock);
    2509                 :             : 
    2510                 :         402 :         return false;
    2511                 :             :     }
    2512                 :             : 
    2513                 :             :     /*
    2514                 :             :      * An invalidated buffer should not have any backends waiting to lock the
    2515                 :             :      * buffer, therefore BM_LOCK_WAKE_IN_PROGRESS should not be set.
    2516                 :             :      */
    2517                 :             :     Assert(!(buf_state & BM_LOCK_WAKE_IN_PROGRESS));
    2518                 :             : 
    2519                 :             :     /*
    2520                 :             :      * Clear out the buffer's tag and flags and usagecount.  This is not
    2521                 :             :      * strictly required, as BM_TAG_VALID/BM_VALID needs to be checked before
    2522                 :             :      * doing anything with the buffer. But currently it's beneficial, as the
    2523                 :             :      * cheaper pre-check for several linear scans of shared buffers use the
    2524                 :             :      * tag (see e.g. FlushDatabaseBuffers()).
    2525                 :             :      */
    2526                 :     1332455 :     ClearBufferTag(&buf_hdr->tag);
    2527                 :     1332455 :     UnlockBufHdrExt(buf_hdr, buf_state,
    2528                 :             :                     0,
    2529                 :             :                     BUF_FLAG_MASK | BUF_USAGECOUNT_MASK,
    2530                 :             :                     0);
    2531                 :             : 
    2532                 :             :     Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    2533                 :             : 
    2534                 :             :     /* finally delete buffer from the buffer mapping table */
    2535                 :     1332455 :     BufTableDelete(&tag, hash);
    2536                 :             : 
    2537                 :     1332455 :     LWLockRelease(partition_lock);
    2538                 :             : 
    2539                 :     1332455 :     buf_state = pg_atomic_read_u64(&buf_hdr->state);
    2540                 :             :     Assert(!(buf_state & (BM_DIRTY | BM_VALID | BM_TAG_VALID)));
    2541                 :             :     Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    2542                 :             :     Assert(BUF_STATE_GET_REFCOUNT(pg_atomic_read_u64(&buf_hdr->state)) > 0);
    2543                 :             : 
    2544                 :     1332455 :     return true;
    2545                 :             : }
    2546                 :             : 
    2547                 :             : static Buffer
    2548                 :     2170202 : GetVictimBuffer(BufferAccessStrategy strategy, IOContext io_context)
    2549                 :             : {
    2550                 :             :     BufferDesc *buf_hdr;
    2551                 :             :     Buffer      buf;
    2552                 :             :     uint64      buf_state;
    2553                 :             :     bool        from_ring;
    2554                 :             : 
    2555                 :             :     /*
    2556                 :             :      * Ensure, before we pin a victim buffer, that there's a free refcount
    2557                 :             :      * entry and resource owner slot for the pin.
    2558                 :             :      */
    2559                 :     2170202 :     ReservePrivateRefCountEntry();
    2560                 :     2170202 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    2561                 :             : 
    2562                 :             :     /* we return here if a prospective victim buffer gets used concurrently */
    2563                 :       22473 : again:
    2564                 :             : 
    2565                 :             :     /*
    2566                 :             :      * Select a victim buffer.  The buffer is returned pinned and owned by
    2567                 :             :      * this backend.
    2568                 :             :      */
    2569                 :     2192675 :     buf_hdr = StrategyGetBuffer(strategy, &buf_state, &from_ring);
    2570                 :     2192675 :     buf = BufferDescriptorGetBuffer(buf_hdr);
    2571                 :             : 
    2572                 :             :     /*
    2573                 :             :      * We shouldn't have any other pins for this buffer.
    2574                 :             :      */
    2575                 :     2192675 :     CheckBufferIsPinnedOnce(buf);
    2576                 :             : 
    2577                 :             :     /*
    2578                 :             :      * If the buffer was dirty, try to write it out.  There is a race
    2579                 :             :      * condition here, another backend could dirty the buffer between
    2580                 :             :      * StrategyGetBuffer() checking that it is not in use and invalidating the
    2581                 :             :      * buffer below. That's addressed by InvalidateVictimBuffer() verifying
    2582                 :             :      * that the buffer is not dirty.
    2583                 :             :      */
    2584         [ +  + ]:     2192675 :     if (buf_state & BM_DIRTY)
    2585                 :             :     {
    2586                 :             :         Assert(buf_state & BM_TAG_VALID);
    2587                 :             :         Assert(buf_state & BM_VALID);
    2588                 :             : 
    2589                 :             :         /*
    2590                 :             :          * We need a share-exclusive lock on the buffer contents to write it
    2591                 :             :          * out (else we might write invalid data, eg because someone else is
    2592                 :             :          * compacting the page contents while we write).  We must use a
    2593                 :             :          * conditional lock acquisition here to avoid deadlock.  Even though
    2594                 :             :          * the buffer was not pinned (and therefore surely not locked) when
    2595                 :             :          * StrategyGetBuffer returned it, someone else could have pinned and
    2596                 :             :          * (share-)exclusive-locked it by the time we get here. If we try to
    2597                 :             :          * get the lock unconditionally, we'd block waiting for them; if they
    2598                 :             :          * later block waiting for us, deadlock ensues. (This has been
    2599                 :             :          * observed to happen when two backends are both trying to split btree
    2600                 :             :          * index pages, and the second one just happens to be trying to split
    2601                 :             :          * the page the first one got from StrategyGetBuffer.)
    2602                 :             :          */
    2603         [ -  + ]:      378031 :         if (!BufferLockConditional(buf, buf_hdr, BUFFER_LOCK_SHARE_EXCLUSIVE))
    2604                 :             :         {
    2605                 :             :             /*
    2606                 :             :              * Someone else has locked the buffer, so give it up and loop back
    2607                 :             :              * to get another one.
    2608                 :             :              */
    2609                 :           0 :             UnpinBuffer(buf_hdr);
    2610                 :           0 :             goto again;
    2611                 :             :         }
    2612                 :             : 
    2613                 :             :         /*
    2614                 :             :          * If using a nondefault strategy, and this victim came from the
    2615                 :             :          * strategy ring, let the strategy decide whether to reject it when
    2616                 :             :          * reusing it would require a WAL flush.  This only applies to
    2617                 :             :          * permanent buffers; unlogged buffers can have fake LSNs, so
    2618                 :             :          * XLogNeedsFlush() is not meaningful for them.
    2619                 :             :          *
    2620                 :             :          * We need to hold the content lock in at least share-exclusive mode
    2621                 :             :          * to safely inspect the page LSN, so this couldn't have been done
    2622                 :             :          * inside StrategyGetBuffer().
    2623                 :             :          */
    2624   [ +  +  +  + ]:      378031 :         if (strategy && from_ring &&
    2625   [ +  +  +  + ]:      196111 :             buf_state & BM_PERMANENT &&
    2626         [ +  + ]:      124738 :             XLogNeedsFlush(BufferGetLSN(buf_hdr)) &&
    2627                 :       26692 :             StrategyRejectBuffer(strategy, buf_hdr, from_ring))
    2628                 :             :         {
    2629                 :       22071 :             UnlockReleaseBuffer(buf);
    2630                 :       22071 :             goto again;
    2631                 :             :         }
    2632                 :             : 
    2633                 :             :         /* OK, do the I/O */
    2634                 :      355960 :         FlushBuffer(buf_hdr, NULL, IOOBJECT_RELATION, io_context);
    2635                 :      355960 :         LockBuffer(buf, BUFFER_LOCK_UNLOCK);
    2636                 :             : 
    2637                 :      355960 :         ScheduleBufferTagForWriteback(&BackendWritebackContext, io_context,
    2638                 :             :                                       &buf_hdr->tag);
    2639                 :             :     }
    2640                 :             : 
    2641                 :             : 
    2642         [ +  + ]:     2170604 :     if (buf_state & BM_VALID)
    2643                 :             :     {
    2644                 :             :         /*
    2645                 :             :          * When a BufferAccessStrategy is in use, blocks evicted from shared
    2646                 :             :          * buffers are counted as IOOP_EVICT in the corresponding context
    2647                 :             :          * (e.g. IOCONTEXT_BULKWRITE). Shared buffers are evicted by a
    2648                 :             :          * strategy in two cases: 1) while initially claiming buffers for the
    2649                 :             :          * strategy ring 2) to replace an existing strategy ring buffer
    2650                 :             :          * because it is pinned or in use and cannot be reused.
    2651                 :             :          *
    2652                 :             :          * Blocks evicted from buffers already in the strategy ring are
    2653                 :             :          * counted as IOOP_REUSE in the corresponding strategy context.
    2654                 :             :          *
    2655                 :             :          * At this point, we can accurately count evictions and reuses,
    2656                 :             :          * because we have successfully claimed the valid buffer. Previously,
    2657                 :             :          * we may have been forced to release the buffer due to concurrent
    2658                 :             :          * pinners or erroring out.
    2659                 :             :          */
    2660                 :     1330339 :         pgstat_count_io_op(IOOBJECT_RELATION, io_context,
    2661         [ +  + ]:     1330339 :                            from_ring ? IOOP_REUSE : IOOP_EVICT, 1, 0);
    2662                 :             :     }
    2663                 :             : 
    2664                 :             :     /*
    2665                 :             :      * If the buffer has an entry in the buffer mapping table, delete it. This
    2666                 :             :      * can fail because another backend could have pinned or dirtied the
    2667                 :             :      * buffer.
    2668                 :             :      */
    2669   [ +  +  +  + ]:     2170604 :     if ((buf_state & BM_TAG_VALID) && !InvalidateVictimBuffer(buf_hdr))
    2670                 :             :     {
    2671                 :         402 :         UnpinBuffer(buf_hdr);
    2672                 :         402 :         goto again;
    2673                 :             :     }
    2674                 :             : 
    2675                 :             :     /* a final set of sanity checks */
    2676                 :             : #ifdef USE_ASSERT_CHECKING
    2677                 :             :     buf_state = pg_atomic_read_u64(&buf_hdr->state);
    2678                 :             : 
    2679                 :             :     Assert(BUF_STATE_GET_REFCOUNT(buf_state) == 1);
    2680                 :             :     Assert(!(buf_state & (BM_TAG_VALID | BM_VALID | BM_DIRTY)));
    2681                 :             : 
    2682                 :             :     CheckBufferIsPinnedOnce(buf);
    2683                 :             : #endif
    2684                 :             : 
    2685                 :     2170202 :     return buf;
    2686                 :             : }
    2687                 :             : 
    2688                 :             : /*
    2689                 :             :  * Return the maximum number of buffers that a backend should try to pin once,
    2690                 :             :  * to avoid exceeding its fair share.  This is the highest value that
    2691                 :             :  * GetAdditionalPinLimit() could ever return.  Note that it may be zero on a
    2692                 :             :  * system with a very small buffer pool relative to max_connections.
    2693                 :             :  */
    2694                 :             : uint32
    2695                 :      765890 : GetPinLimit(void)
    2696                 :             : {
    2697                 :      765890 :     return MaxProportionalPins;
    2698                 :             : }
    2699                 :             : 
    2700                 :             : /*
    2701                 :             :  * Return the maximum number of additional buffers that this backend should
    2702                 :             :  * pin if it wants to stay under the per-backend limit, considering the number
    2703                 :             :  * of buffers it has already pinned.  Unlike LimitAdditionalPins(), the limit
    2704                 :             :  * return by this function can be zero.
    2705                 :             :  */
    2706                 :             : uint32
    2707                 :     4199438 : GetAdditionalPinLimit(void)
    2708                 :             : {
    2709                 :             :     uint32      estimated_pins_held;
    2710                 :             : 
    2711                 :             :     /*
    2712                 :             :      * We get the number of "overflowed" pins for free, but don't know the
    2713                 :             :      * number of pins in PrivateRefCountArray.  The cost of calculating that
    2714                 :             :      * exactly doesn't seem worth it, so just assume the max.
    2715                 :             :      */
    2716                 :     4199438 :     estimated_pins_held = PrivateRefCountOverflowed + REFCOUNT_ARRAY_ENTRIES;
    2717                 :             : 
    2718                 :             :     /* Is this backend already holding more than its fair share? */
    2719         [ +  + ]:     4199438 :     if (estimated_pins_held > MaxProportionalPins)
    2720                 :     1341112 :         return 0;
    2721                 :             : 
    2722                 :     2858326 :     return MaxProportionalPins - estimated_pins_held;
    2723                 :             : }
    2724                 :             : 
    2725                 :             : /*
    2726                 :             :  * Limit the number of pins a batch operation may additionally acquire, to
    2727                 :             :  * avoid running out of pinnable buffers.
    2728                 :             :  *
    2729                 :             :  * One additional pin is always allowed, on the assumption that the operation
    2730                 :             :  * requires at least one to make progress.
    2731                 :             :  */
    2732                 :             : void
    2733                 :      254998 : LimitAdditionalPins(uint32 *additional_pins)
    2734                 :             : {
    2735                 :             :     uint32      limit;
    2736                 :             : 
    2737         [ +  + ]:      254998 :     if (*additional_pins <= 1)
    2738                 :      239380 :         return;
    2739                 :             : 
    2740                 :       15618 :     limit = GetAdditionalPinLimit();
    2741                 :       15618 :     limit = Max(limit, 1);
    2742         [ +  + ]:       15618 :     if (limit < *additional_pins)
    2743                 :       10068 :         *additional_pins = limit;
    2744                 :             : }
    2745                 :             : 
    2746                 :             : /*
    2747                 :             :  * Logic shared between ExtendBufferedRelBy(), ExtendBufferedRelTo(). Just to
    2748                 :             :  * avoid duplicating the tracing and relpersistence related logic.
    2749                 :             :  */
    2750                 :             : static BlockNumber
    2751                 :      269968 : ExtendBufferedRelCommon(BufferManagerRelation bmr,
    2752                 :             :                         ForkNumber fork,
    2753                 :             :                         BufferAccessStrategy strategy,
    2754                 :             :                         uint32 flags,
    2755                 :             :                         uint32 extend_by,
    2756                 :             :                         BlockNumber extend_upto,
    2757                 :             :                         Buffer *buffers,
    2758                 :             :                         uint32 *extended_by)
    2759                 :             : {
    2760                 :             :     BlockNumber first_block;
    2761                 :             : 
    2762                 :             :     TRACE_POSTGRESQL_BUFFER_EXTEND_START(fork,
    2763                 :             :                                          BMR_GET_SMGR(bmr)->smgr_rlocator.locator.spcOid,
    2764                 :             :                                          BMR_GET_SMGR(bmr)->smgr_rlocator.locator.dbOid,
    2765                 :             :                                          BMR_GET_SMGR(bmr)->smgr_rlocator.locator.relNumber,
    2766                 :             :                                          BMR_GET_SMGR(bmr)->smgr_rlocator.backend,
    2767                 :             :                                          extend_by);
    2768                 :             : 
    2769         [ +  + ]:      269968 :     if (bmr.relpersistence == RELPERSISTENCE_TEMP)
    2770                 :             :     {
    2771                 :             :         /*
    2772                 :             :          * Reject attempts to extend non-local temporary relations; we have no
    2773                 :             :          * ability to transfer about-to-be-created local buffers into the
    2774                 :             :          * owning session's local buffers.  This is the canonical place for
    2775                 :             :          * the check, covering any attempt to extend a non-local temporary
    2776                 :             :          * relation.
    2777                 :             :          */
    2778   [ +  -  +  -  :       14970 :         if (bmr.rel && RELATION_IS_OTHER_TEMP(bmr.rel))
                   +  + ]
    2779         [ +  - ]:           1 :             ereport(ERROR,
    2780                 :             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2781                 :             :                      errmsg("cannot access temporary tables of other sessions")));
    2782                 :             : 
    2783                 :       14969 :         first_block = ExtendBufferedRelLocal(bmr, fork, flags,
    2784                 :             :                                              extend_by, extend_upto,
    2785                 :             :                                              buffers, &extend_by);
    2786                 :             :     }
    2787                 :             :     else
    2788                 :      254998 :         first_block = ExtendBufferedRelShared(bmr, fork, strategy, flags,
    2789                 :             :                                               extend_by, extend_upto,
    2790                 :             :                                               buffers, &extend_by);
    2791                 :      269967 :     *extended_by = extend_by;
    2792                 :             : 
    2793                 :             :     TRACE_POSTGRESQL_BUFFER_EXTEND_DONE(fork,
    2794                 :             :                                         BMR_GET_SMGR(bmr)->smgr_rlocator.locator.spcOid,
    2795                 :             :                                         BMR_GET_SMGR(bmr)->smgr_rlocator.locator.dbOid,
    2796                 :             :                                         BMR_GET_SMGR(bmr)->smgr_rlocator.locator.relNumber,
    2797                 :             :                                         BMR_GET_SMGR(bmr)->smgr_rlocator.backend,
    2798                 :             :                                         *extended_by,
    2799                 :             :                                         first_block);
    2800                 :             : 
    2801                 :      269967 :     return first_block;
    2802                 :             : }
    2803                 :             : 
    2804                 :             : /*
    2805                 :             :  * Implementation of ExtendBufferedRelBy() and ExtendBufferedRelTo() for
    2806                 :             :  * shared buffers.
    2807                 :             :  */
    2808                 :             : static BlockNumber
    2809                 :      254998 : ExtendBufferedRelShared(BufferManagerRelation bmr,
    2810                 :             :                         ForkNumber fork,
    2811                 :             :                         BufferAccessStrategy strategy,
    2812                 :             :                         uint32 flags,
    2813                 :             :                         uint32 extend_by,
    2814                 :             :                         BlockNumber extend_upto,
    2815                 :             :                         Buffer *buffers,
    2816                 :             :                         uint32 *extended_by)
    2817                 :             : {
    2818                 :             :     BlockNumber first_block;
    2819                 :      254998 :     IOContext   io_context = IOContextForStrategy(strategy);
    2820                 :             :     instr_time  io_start;
    2821                 :             : 
    2822                 :      254998 :     LimitAdditionalPins(&extend_by);
    2823                 :             : 
    2824                 :             :     /*
    2825                 :             :      * Acquire victim buffers for extension without holding extension lock.
    2826                 :             :      * Writing out victim buffers is the most expensive part of extending the
    2827                 :             :      * relation, particularly when doing so requires WAL flushes. Zeroing out
    2828                 :             :      * the buffers is also quite expensive, so do that before holding the
    2829                 :             :      * extension lock as well.
    2830                 :             :      *
    2831                 :             :      * These pages are pinned by us and not valid. While we hold the pin they
    2832                 :             :      * can't be acquired as victim buffers by another backend.
    2833                 :             :      */
    2834         [ +  + ]:      536898 :     for (uint32 i = 0; i < extend_by; i++)
    2835                 :             :     {
    2836                 :             :         Block       buf_block;
    2837                 :             : 
    2838                 :      281900 :         buffers[i] = GetVictimBuffer(strategy, io_context);
    2839                 :      281900 :         buf_block = BufHdrGetBlock(GetBufferDescriptor(buffers[i] - 1));
    2840                 :             : 
    2841                 :             :         /* new buffers are zero-filled */
    2842   [ +  -  +  -  :      281900 :         MemSet(buf_block, 0, BLCKSZ);
          +  -  -  +  -  
                      - ]
    2843                 :             :     }
    2844                 :             : 
    2845                 :             :     /*
    2846                 :             :      * Lock relation against concurrent extensions, unless requested not to.
    2847                 :             :      *
    2848                 :             :      * We use the same extension lock for all forks. That's unnecessarily
    2849                 :             :      * restrictive, but currently extensions for forks don't happen often
    2850                 :             :      * enough to make it worth locking more granularly.
    2851                 :             :      *
    2852                 :             :      * Note that another backend might have extended the relation by the time
    2853                 :             :      * we get the lock.
    2854                 :             :      */
    2855         [ +  + ]:      254998 :     if (!(flags & EB_SKIP_EXTENSION_LOCK))
    2856                 :      199290 :         LockRelationForExtension(bmr.rel, ExclusiveLock);
    2857                 :             : 
    2858                 :             :     /*
    2859                 :             :      * If requested, invalidate size cache, so that smgrnblocks asks the
    2860                 :             :      * kernel.
    2861                 :             :      */
    2862         [ +  + ]:      254998 :     if (flags & EB_CLEAR_SIZE_CACHE)
    2863         [ +  - ]:       10035 :         BMR_GET_SMGR(bmr)->smgr_cached_nblocks[fork] = InvalidBlockNumber;
    2864                 :             : 
    2865         [ +  + ]:      254998 :     first_block = smgrnblocks(BMR_GET_SMGR(bmr), fork);
    2866                 :             : 
    2867                 :             :     /*
    2868                 :             :      * Now that we have the accurate relation size, check if the caller wants
    2869                 :             :      * us to extend to only up to a specific size. If there were concurrent
    2870                 :             :      * extensions, we might have acquired too many buffers and need to release
    2871                 :             :      * them.
    2872                 :             :      */
    2873         [ +  + ]:      254998 :     if (extend_upto != InvalidBlockNumber)
    2874                 :             :     {
    2875                 :       57899 :         uint32      orig_extend_by = extend_by;
    2876                 :             : 
    2877         [ -  + ]:       57899 :         if (first_block > extend_upto)
    2878                 :           0 :             extend_by = 0;
    2879         [ +  + ]:       57899 :         else if ((uint64) first_block + extend_by > extend_upto)
    2880                 :           6 :             extend_by = extend_upto - first_block;
    2881                 :             : 
    2882         [ +  + ]:       57909 :         for (uint32 i = extend_by; i < orig_extend_by; i++)
    2883                 :             :         {
    2884                 :          10 :             BufferDesc *buf_hdr = GetBufferDescriptor(buffers[i] - 1);
    2885                 :             : 
    2886                 :          10 :             UnpinBuffer(buf_hdr);
    2887                 :             :         }
    2888                 :             : 
    2889         [ +  + ]:       57899 :         if (extend_by == 0)
    2890                 :             :         {
    2891         [ +  - ]:           6 :             if (!(flags & EB_SKIP_EXTENSION_LOCK))
    2892                 :           6 :                 UnlockRelationForExtension(bmr.rel, ExclusiveLock);
    2893                 :           6 :             *extended_by = extend_by;
    2894                 :           6 :             return first_block;
    2895                 :             :         }
    2896                 :             :     }
    2897                 :             : 
    2898                 :             :     /* Fail if relation is already at maximum possible length */
    2899         [ -  + ]:      254992 :     if ((uint64) first_block + extend_by >= MaxBlockNumber)
    2900   [ #  #  #  #  :           0 :         ereport(ERROR,
          #  #  #  #  #  
                      # ]
    2901                 :             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    2902                 :             :                  errmsg("cannot extend relation %s beyond %u blocks",
    2903                 :             :                         relpath(BMR_GET_SMGR(bmr)->smgr_rlocator, fork).str,
    2904                 :             :                         MaxBlockNumber)));
    2905                 :             : 
    2906                 :             :     /*
    2907                 :             :      * Insert buffers into buffer table, mark as IO_IN_PROGRESS.
    2908                 :             :      *
    2909                 :             :      * This needs to happen before we extend the relation, because as soon as
    2910                 :             :      * we do, other backends can start to read in those pages.
    2911                 :             :      */
    2912         [ +  + ]:      536882 :     for (uint32 i = 0; i < extend_by; i++)
    2913                 :             :     {
    2914                 :      281890 :         Buffer      victim_buf = buffers[i];
    2915                 :      281890 :         BufferDesc *victim_buf_hdr = GetBufferDescriptor(victim_buf - 1);
    2916                 :             :         BufferTag   tag;
    2917                 :             :         uint32      hash;
    2918                 :             :         LWLock     *partition_lock;
    2919                 :             :         int         existing_id;
    2920                 :             : 
    2921                 :             :         /* in case we need to pin an existing buffer below */
    2922                 :      281890 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    2923                 :      281890 :         ReservePrivateRefCountEntry();
    2924                 :             : 
    2925         [ +  + ]:      281890 :         InitBufferTag(&tag, &BMR_GET_SMGR(bmr)->smgr_rlocator.locator, fork,
    2926                 :             :                       first_block + i);
    2927                 :      281890 :         hash = BufTableHashCode(&tag);
    2928                 :      281890 :         partition_lock = BufMappingPartitionLock(hash);
    2929                 :             : 
    2930                 :      281890 :         LWLockAcquire(partition_lock, LW_EXCLUSIVE);
    2931                 :             : 
    2932                 :      281890 :         existing_id = BufTableInsert(&tag, hash, victim_buf_hdr->buf_id);
    2933                 :             : 
    2934                 :             :         /*
    2935                 :             :          * We get here only in the corner case where we are trying to extend
    2936                 :             :          * the relation but we found a pre-existing buffer. This can happen
    2937                 :             :          * because a prior attempt at extending the relation failed, and
    2938                 :             :          * because mdread doesn't complain about reads beyond EOF (when
    2939                 :             :          * zero_damaged_pages is ON) and so a previous attempt to read a block
    2940                 :             :          * beyond EOF could have left a "valid" zero-filled buffer.
    2941                 :             :          *
    2942                 :             :          * This has also been observed when relation was overwritten by
    2943                 :             :          * external process. Since the legitimate cases should always have
    2944                 :             :          * left a zero-filled buffer, complain if not PageIsNew.
    2945                 :             :          */
    2946         [ -  + ]:      281890 :         if (existing_id >= 0)
    2947                 :             :         {
    2948                 :           0 :             BufferDesc *existing_hdr = GetBufferDescriptor(existing_id);
    2949                 :             :             Block       buf_block;
    2950                 :             :             bool        valid;
    2951                 :             : 
    2952                 :             :             /*
    2953                 :             :              * Pin the existing buffer before releasing the partition lock,
    2954                 :             :              * preventing it from being evicted.
    2955                 :             :              */
    2956                 :           0 :             valid = PinBuffer(existing_hdr, strategy, false);
    2957                 :             : 
    2958                 :           0 :             LWLockRelease(partition_lock);
    2959                 :           0 :             UnpinBuffer(victim_buf_hdr);
    2960                 :             : 
    2961                 :           0 :             buffers[i] = BufferDescriptorGetBuffer(existing_hdr);
    2962                 :           0 :             buf_block = BufHdrGetBlock(existing_hdr);
    2963                 :             : 
    2964   [ #  #  #  # ]:           0 :             if (valid && !PageIsNew((Page) buf_block))
    2965   [ #  #  #  #  :           0 :                 ereport(ERROR,
          #  #  #  #  #  
                      # ]
    2966                 :             :                         (errmsg("unexpected data beyond EOF in block %u of relation \"%s\"",
    2967                 :             :                                 existing_hdr->tag.blockNum,
    2968                 :             :                                 relpath(BMR_GET_SMGR(bmr)->smgr_rlocator, fork).str)));
    2969                 :             : 
    2970                 :             :             /*
    2971                 :             :              * We *must* do smgr[zero]extend before succeeding, else the page
    2972                 :             :              * will not be reserved by the kernel, and the next P_NEW call
    2973                 :             :              * will decide to return the same page.  Clear the BM_VALID bit,
    2974                 :             :              * do StartSharedBufferIO() and proceed.
    2975                 :             :              *
    2976                 :             :              * Loop to handle the very small possibility that someone re-sets
    2977                 :             :              * BM_VALID between our clearing it and StartSharedBufferIO
    2978                 :             :              * inspecting it.
    2979                 :             :              */
    2980                 :             :             while (true)
    2981                 :           0 :             {
    2982                 :             :                 StartBufferIOResult sbres;
    2983                 :             : 
    2984                 :           0 :                 pg_atomic_fetch_and_u64(&existing_hdr->state, ~BM_VALID);
    2985                 :             : 
    2986                 :           0 :                 sbres = StartSharedBufferIO(existing_hdr, true, true, NULL);
    2987                 :             : 
    2988         [ #  # ]:           0 :                 if (sbres != BUFFER_IO_ALREADY_DONE)
    2989                 :           0 :                     break;
    2990                 :             :             }
    2991                 :             :         }
    2992                 :             :         else
    2993                 :             :         {
    2994                 :             :             uint64      buf_state;
    2995                 :      281890 :             uint64      set_bits = 0;
    2996                 :             : 
    2997                 :      281890 :             buf_state = LockBufHdr(victim_buf_hdr);
    2998                 :             : 
    2999                 :             :             /* some sanity checks while we hold the buffer header lock */
    3000                 :             :             Assert(!(buf_state & (BM_VALID | BM_TAG_VALID | BM_DIRTY)));
    3001                 :             :             Assert(BUF_STATE_GET_REFCOUNT(buf_state) == 1);
    3002                 :             : 
    3003                 :      281890 :             victim_buf_hdr->tag = tag;
    3004                 :             : 
    3005                 :      281890 :             set_bits |= BM_TAG_VALID | BUF_USAGECOUNT_ONE;
    3006   [ +  +  +  + ]:      281890 :             if (bmr.relpersistence == RELPERSISTENCE_PERMANENT || fork == INIT_FORKNUM)
    3007                 :      276462 :                 set_bits |= BM_PERMANENT;
    3008                 :             : 
    3009                 :      281890 :             UnlockBufHdrExt(victim_buf_hdr, buf_state,
    3010                 :             :                             set_bits, 0,
    3011                 :             :                             0);
    3012                 :             : 
    3013                 :      281890 :             LWLockRelease(partition_lock);
    3014                 :             : 
    3015                 :             :             /* XXX: could combine the locked operations in it with the above */
    3016                 :      281890 :             StartSharedBufferIO(victim_buf_hdr, true, true, NULL);
    3017                 :             :         }
    3018                 :             :     }
    3019                 :             : 
    3020                 :      254992 :     io_start = pgstat_prepare_io_time(track_io_timing);
    3021                 :             : 
    3022                 :             :     /*
    3023                 :             :      * Note: if smgrzeroextend fails, we will end up with buffers that are
    3024                 :             :      * allocated but not marked BM_VALID.  The next relation extension will
    3025                 :             :      * still select the same block number (because the relation didn't get any
    3026                 :             :      * longer on disk) and so future attempts to extend the relation will find
    3027                 :             :      * the same buffers (if they have not been recycled) but come right back
    3028                 :             :      * here to try smgrzeroextend again.
    3029                 :             :      *
    3030                 :             :      * We don't need to set checksum for all-zero pages.
    3031                 :             :      */
    3032         [ +  + ]:      254992 :     smgrzeroextend(BMR_GET_SMGR(bmr), fork, first_block, extend_by, false);
    3033                 :             : 
    3034                 :             :     /*
    3035                 :             :      * Release the file-extension lock; it's now OK for someone else to extend
    3036                 :             :      * the relation some more.
    3037                 :             :      *
    3038                 :             :      * We remove IO_IN_PROGRESS after this, as waking up waiting backends can
    3039                 :             :      * take noticeable time.
    3040                 :             :      */
    3041         [ +  + ]:      254992 :     if (!(flags & EB_SKIP_EXTENSION_LOCK))
    3042                 :      199284 :         UnlockRelationForExtension(bmr.rel, ExclusiveLock);
    3043                 :             : 
    3044                 :      254992 :     pgstat_count_io_op_time(IOOBJECT_RELATION, io_context, IOOP_EXTEND,
    3045                 :      254992 :                             io_start, 1, extend_by * BLCKSZ);
    3046                 :             : 
    3047                 :             :     /* Set BM_VALID, terminate IO, and wake up any waiters */
    3048         [ +  + ]:      536882 :     for (uint32 i = 0; i < extend_by; i++)
    3049                 :             :     {
    3050                 :      281890 :         Buffer      buf = buffers[i];
    3051                 :      281890 :         BufferDesc *buf_hdr = GetBufferDescriptor(buf - 1);
    3052                 :      281890 :         bool        lock = false;
    3053                 :             : 
    3054   [ +  +  +  + ]:      281890 :         if (flags & EB_LOCK_FIRST && i == 0)
    3055                 :      196771 :             lock = true;
    3056         [ +  + ]:       85119 :         else if (flags & EB_LOCK_TARGET)
    3057                 :             :         {
    3058                 :             :             Assert(extend_upto != InvalidBlockNumber);
    3059         [ +  + ]:       46712 :             if (first_block + i + 1 == extend_upto)
    3060                 :       46140 :                 lock = true;
    3061                 :             :         }
    3062                 :             : 
    3063         [ +  + ]:      281890 :         if (lock)
    3064                 :      242911 :             LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
    3065                 :             : 
    3066                 :      281890 :         TerminateBufferIO(buf_hdr, false, BM_VALID, true, false);
    3067                 :             :     }
    3068                 :             : 
    3069                 :      254992 :     pgBufferUsage.shared_blks_written += extend_by;
    3070                 :             : 
    3071                 :      254992 :     *extended_by = extend_by;
    3072                 :             : 
    3073                 :      254992 :     return first_block;
    3074                 :             : }
    3075                 :             : 
    3076                 :             : /*
    3077                 :             :  * BufferIsLockedByMe
    3078                 :             :  *
    3079                 :             :  *      Checks if this backend has the buffer locked in any mode.
    3080                 :             :  *
    3081                 :             :  * Buffer must be pinned.
    3082                 :             :  */
    3083                 :             : bool
    3084                 :           0 : BufferIsLockedByMe(Buffer buffer)
    3085                 :             : {
    3086                 :             :     BufferDesc *bufHdr;
    3087                 :             : 
    3088                 :             :     Assert(BufferIsPinned(buffer));
    3089                 :             : 
    3090         [ #  # ]:           0 :     if (BufferIsLocal(buffer))
    3091                 :             :     {
    3092                 :             :         /* Content locks are not maintained for local buffers. */
    3093                 :           0 :         return true;
    3094                 :             :     }
    3095                 :             :     else
    3096                 :             :     {
    3097                 :           0 :         bufHdr = GetBufferDescriptor(buffer - 1);
    3098                 :           0 :         return BufferLockHeldByMe(bufHdr);
    3099                 :             :     }
    3100                 :             : }
    3101                 :             : 
    3102                 :             : /*
    3103                 :             :  * BufferIsLockedByMeInMode
    3104                 :             :  *
    3105                 :             :  *      Checks if this backend has the buffer locked in the specified mode.
    3106                 :             :  *
    3107                 :             :  * Buffer must be pinned.
    3108                 :             :  */
    3109                 :             : bool
    3110                 :           0 : BufferIsLockedByMeInMode(Buffer buffer, BufferLockMode mode)
    3111                 :             : {
    3112                 :             :     BufferDesc *bufHdr;
    3113                 :             : 
    3114                 :             :     Assert(BufferIsPinned(buffer));
    3115                 :             : 
    3116         [ #  # ]:           0 :     if (BufferIsLocal(buffer))
    3117                 :             :     {
    3118                 :             :         /* Content locks are not maintained for local buffers. */
    3119                 :           0 :         return true;
    3120                 :             :     }
    3121                 :             :     else
    3122                 :             :     {
    3123                 :           0 :         bufHdr = GetBufferDescriptor(buffer - 1);
    3124                 :           0 :         return BufferLockHeldByMeInMode(bufHdr, mode);
    3125                 :             :     }
    3126                 :             : }
    3127                 :             : 
    3128                 :             : /*
    3129                 :             :  * BufferIsDirty
    3130                 :             :  *
    3131                 :             :  *      Checks if buffer is already dirty.
    3132                 :             :  *
    3133                 :             :  * Buffer must be pinned and [share-]exclusive-locked.  (Without such a lock,
    3134                 :             :  * the result may be stale before it's returned.)
    3135                 :             :  */
    3136                 :             : bool
    3137                 :       29470 : BufferIsDirty(Buffer buffer)
    3138                 :             : {
    3139                 :             :     BufferDesc *bufHdr;
    3140                 :             : 
    3141                 :             :     Assert(BufferIsPinned(buffer));
    3142                 :             : 
    3143         [ +  + ]:       29470 :     if (BufferIsLocal(buffer))
    3144                 :             :     {
    3145                 :        9523 :         int         bufid = -buffer - 1;
    3146                 :             : 
    3147                 :        9523 :         bufHdr = GetLocalBufferDescriptor(bufid);
    3148                 :             :         /* Content locks are not maintained for local buffers. */
    3149                 :             :     }
    3150                 :             :     else
    3151                 :             :     {
    3152                 :       19947 :         bufHdr = GetBufferDescriptor(buffer - 1);
    3153                 :             :         Assert(BufferIsLockedByMeInMode(buffer, BUFFER_LOCK_SHARE_EXCLUSIVE) ||
    3154                 :             :                BufferIsLockedByMeInMode(buffer, BUFFER_LOCK_EXCLUSIVE));
    3155                 :             :     }
    3156                 :             : 
    3157                 :       29470 :     return pg_atomic_read_u64(&bufHdr->state) & BM_DIRTY;
    3158                 :             : }
    3159                 :             : 
    3160                 :             : /*
    3161                 :             :  * MarkBufferDirty
    3162                 :             :  *
    3163                 :             :  *      Marks buffer contents as dirty (actual write happens later).
    3164                 :             :  *
    3165                 :             :  * Buffer must be pinned and exclusive-locked.  (If caller does not hold
    3166                 :             :  * exclusive lock, then somebody could be in process of writing the buffer,
    3167                 :             :  * leading to risk of bad data written to disk.)
    3168                 :             :  */
    3169                 :             : void
    3170                 :    34868609 : MarkBufferDirty(Buffer buffer)
    3171                 :             : {
    3172                 :             :     BufferDesc *bufHdr;
    3173                 :             :     uint64      buf_state;
    3174                 :             :     uint64      old_buf_state;
    3175                 :             : 
    3176         [ -  + ]:    34868609 :     if (!BufferIsValid(buffer))
    3177         [ #  # ]:           0 :         elog(ERROR, "bad buffer ID: %d", buffer);
    3178                 :             : 
    3179         [ +  + ]:    34868609 :     if (BufferIsLocal(buffer))
    3180                 :             :     {
    3181                 :     1580070 :         MarkLocalBufferDirty(buffer);
    3182                 :     1580070 :         return;
    3183                 :             :     }
    3184                 :             : 
    3185                 :    33288539 :     bufHdr = GetBufferDescriptor(buffer - 1);
    3186                 :             : 
    3187                 :             :     Assert(BufferIsPinned(buffer));
    3188                 :             :     Assert(BufferIsLockedByMeInMode(buffer, BUFFER_LOCK_EXCLUSIVE));
    3189                 :             : 
    3190                 :             :     /*
    3191                 :             :      * NB: We have to wait for the buffer header spinlock to be not held, as
    3192                 :             :      * TerminateBufferIO() relies on the spinlock.
    3193                 :             :      */
    3194                 :    33288539 :     old_buf_state = pg_atomic_read_u64(&bufHdr->state);
    3195                 :             :     for (;;)
    3196                 :             :     {
    3197         [ +  + ]:    33288870 :         if (old_buf_state & BM_LOCKED)
    3198                 :         489 :             old_buf_state = WaitBufHdrUnlocked(bufHdr);
    3199                 :             : 
    3200                 :    33288870 :         buf_state = old_buf_state;
    3201                 :             : 
    3202                 :             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    3203                 :    33288870 :         buf_state |= BM_DIRTY;
    3204                 :             : 
    3205         [ +  + ]:    33288870 :         if (pg_atomic_compare_exchange_u64(&bufHdr->state, &old_buf_state,
    3206                 :             :                                            buf_state))
    3207                 :    33288539 :             break;
    3208                 :             :     }
    3209                 :             : 
    3210                 :             :     /*
    3211                 :             :      * If the buffer was not dirty already, do vacuum accounting.
    3212                 :             :      */
    3213         [ +  + ]:    33288539 :     if (!(old_buf_state & BM_DIRTY))
    3214                 :             :     {
    3215                 :      800673 :         pgBufferUsage.shared_blks_dirtied++;
    3216         [ +  + ]:      800673 :         if (VacuumCostActive)
    3217                 :        8508 :             VacuumCostBalance += VacuumCostPageDirty;
    3218                 :             :     }
    3219                 :             : }
    3220                 :             : 
    3221                 :             : /*
    3222                 :             :  * ReleaseAndReadBuffer -- combine ReleaseBuffer() and ReadBuffer()
    3223                 :             :  *
    3224                 :             :  * Formerly, this saved one cycle of acquiring/releasing the BufMgrLock
    3225                 :             :  * compared to calling the two routines separately.  Now it's mainly just
    3226                 :             :  * a convenience function.  However, if the passed buffer is valid and
    3227                 :             :  * already contains the desired block, we just return it as-is; and that
    3228                 :             :  * does save considerable work compared to a full release and reacquire.
    3229                 :             :  *
    3230                 :             :  * Note: it is OK to pass buffer == InvalidBuffer, indicating that no old
    3231                 :             :  * buffer actually needs to be released.  This case is the same as ReadBuffer,
    3232                 :             :  * but can save some tests in the caller.
    3233                 :             :  */
    3234                 :             : Buffer
    3235                 :        3646 : ReleaseAndReadBuffer(Buffer buffer,
    3236                 :             :                      Relation relation,
    3237                 :             :                      BlockNumber blockNum)
    3238                 :             : {
    3239                 :        3646 :     ForkNumber  forkNum = MAIN_FORKNUM;
    3240                 :             :     BufferDesc *bufHdr;
    3241                 :             : 
    3242         [ +  - ]:        3646 :     if (BufferIsValid(buffer))
    3243                 :             :     {
    3244                 :             :         Assert(BufferIsPinned(buffer));
    3245         [ +  + ]:        3646 :         if (BufferIsLocal(buffer))
    3246                 :             :         {
    3247                 :          50 :             bufHdr = GetLocalBufferDescriptor(-buffer - 1);
    3248   [ -  +  -  - ]:          50 :             if (bufHdr->tag.blockNum == blockNum &&
    3249         [ #  # ]:           0 :                 BufTagMatchesRelFileLocator(&bufHdr->tag, &relation->rd_locator) &&
    3250                 :           0 :                 BufTagGetForkNum(&bufHdr->tag) == forkNum)
    3251                 :           0 :                 return buffer;
    3252                 :          50 :             UnpinLocalBuffer(buffer);
    3253                 :             :         }
    3254                 :             :         else
    3255                 :             :         {
    3256                 :        3596 :             bufHdr = GetBufferDescriptor(buffer - 1);
    3257                 :             :             /* we have pin, so it's ok to examine tag without spinlock */
    3258   [ -  +  -  - ]:        3596 :             if (bufHdr->tag.blockNum == blockNum &&
    3259         [ #  # ]:           0 :                 BufTagMatchesRelFileLocator(&bufHdr->tag, &relation->rd_locator) &&
    3260                 :           0 :                 BufTagGetForkNum(&bufHdr->tag) == forkNum)
    3261                 :           0 :                 return buffer;
    3262                 :        3596 :             UnpinBuffer(bufHdr);
    3263                 :             :         }
    3264                 :             :     }
    3265                 :             : 
    3266                 :        3646 :     return ReadBuffer(relation, blockNum);
    3267                 :             : }
    3268                 :             : 
    3269                 :             : /*
    3270                 :             :  * PinBuffer -- make buffer unavailable for replacement.
    3271                 :             :  *
    3272                 :             :  * For the default access strategy, the buffer's usage_count is incremented
    3273                 :             :  * when we first pin it; for other strategies we just make sure the usage_count
    3274                 :             :  * isn't zero.  (The idea of the latter is that we don't want synchronized
    3275                 :             :  * heap scans to inflate the count, but we need it to not be zero to discourage
    3276                 :             :  * other backends from stealing buffers from our ring.  As long as we cycle
    3277                 :             :  * through the ring faster than the global clock-sweep cycles, buffers in
    3278                 :             :  * our ring won't be chosen as victims for replacement by other backends.)
    3279                 :             :  *
    3280                 :             :  * This should be applied only to shared buffers, never local ones.
    3281                 :             :  *
    3282                 :             :  * Since buffers are pinned/unpinned very frequently, pin buffers without
    3283                 :             :  * taking the buffer header lock; instead update the state variable in loop of
    3284                 :             :  * CAS operations. Hopefully it's just a single CAS.
    3285                 :             :  *
    3286                 :             :  * Note that ResourceOwnerEnlarge() and ReservePrivateRefCountEntry()
    3287                 :             :  * must have been done already.
    3288                 :             :  *
    3289                 :             :  * Returns true if buffer is BM_VALID, else false.  This provision allows
    3290                 :             :  * some callers to avoid an extra spinlock cycle.  If skip_if_not_valid is
    3291                 :             :  * true, then a false return value also indicates that the buffer was
    3292                 :             :  * (recently) invalid and has not been pinned.
    3293                 :             :  */
    3294                 :             : static bool
    3295                 :    82255593 : PinBuffer(BufferDesc *buf, BufferAccessStrategy strategy,
    3296                 :             :           bool skip_if_not_valid)
    3297                 :             : {
    3298                 :    82255593 :     Buffer      b = BufferDescriptorGetBuffer(buf);
    3299                 :             :     bool        result;
    3300                 :             :     PrivateRefCountEntry *ref;
    3301                 :             : 
    3302                 :             :     Assert(!BufferIsLocal(b));
    3303                 :             :     Assert(ReservedRefCountSlot != -1);
    3304                 :             : 
    3305                 :    82255593 :     ref = GetPrivateRefCountEntry(b, true);
    3306                 :             : 
    3307         [ +  + ]:    82255593 :     if (ref == NULL)
    3308                 :             :     {
    3309                 :             :         uint64      buf_state;
    3310                 :             :         uint64      old_buf_state;
    3311                 :             : 
    3312                 :    74819155 :         old_buf_state = pg_atomic_read_u64(&buf->state);
    3313                 :             :         for (;;)
    3314                 :             :         {
    3315   [ +  +  +  +  :    74842936 :             if (unlikely(skip_if_not_valid && !(old_buf_state & BM_VALID)))
                   +  + ]
    3316                 :           6 :                 return false;
    3317                 :             : 
    3318                 :             :             /*
    3319                 :             :              * We're not allowed to increase the refcount while the buffer
    3320                 :             :              * header spinlock is held. Wait for the lock to be released.
    3321                 :             :              */
    3322         [ +  + ]:    74842930 :             if (unlikely(old_buf_state & BM_LOCKED))
    3323                 :             :             {
    3324                 :         157 :                 old_buf_state = WaitBufHdrUnlocked(buf);
    3325                 :             : 
    3326                 :             :                 /* perform checks at the top of the loop again */
    3327                 :         157 :                 continue;
    3328                 :             :             }
    3329                 :             : 
    3330                 :    74842773 :             buf_state = old_buf_state;
    3331                 :             : 
    3332                 :             :             /* increase refcount */
    3333                 :    74842773 :             buf_state += BUF_REFCOUNT_ONE;
    3334                 :             : 
    3335         [ +  + ]:    74842773 :             if (strategy == NULL)
    3336                 :             :             {
    3337                 :             :                 /* Default case: increase usagecount unless already max. */
    3338         [ +  + ]:    74010288 :                 if (BUF_STATE_GET_USAGECOUNT(buf_state) < BM_MAX_USAGE_COUNT)
    3339                 :     3781941 :                     buf_state += BUF_USAGECOUNT_ONE;
    3340                 :             :             }
    3341                 :             :             else
    3342                 :             :             {
    3343                 :             :                 /*
    3344                 :             :                  * Ring buffers shouldn't evict others from pool.  Thus we
    3345                 :             :                  * don't make usagecount more than 1.
    3346                 :             :                  */
    3347         [ +  + ]:      832485 :                 if (BUF_STATE_GET_USAGECOUNT(buf_state) == 0)
    3348                 :       33917 :                     buf_state += BUF_USAGECOUNT_ONE;
    3349                 :             :             }
    3350                 :             : 
    3351         [ +  + ]:    74842773 :             if (pg_atomic_compare_exchange_u64(&buf->state, &old_buf_state,
    3352                 :             :                                                buf_state))
    3353                 :             :             {
    3354                 :    74819149 :                 result = (buf_state & BM_VALID) != 0;
    3355                 :             : 
    3356                 :    74819149 :                 TrackNewBufferPin(b);
    3357                 :    74819149 :                 break;
    3358                 :             :             }
    3359                 :             :         }
    3360                 :             :     }
    3361                 :             :     else
    3362                 :             :     {
    3363                 :             :         /*
    3364                 :             :          * If we previously pinned the buffer, it is likely to be valid, but
    3365                 :             :          * it may not be if StartReadBuffers() was called and
    3366                 :             :          * WaitReadBuffers() hasn't been called yet.  We'll check by loading
    3367                 :             :          * the flags without locking.  This is racy, but it's OK to return
    3368                 :             :          * false spuriously: when WaitReadBuffers() calls StartBufferIO(),
    3369                 :             :          * it'll see that it's now valid.
    3370                 :             :          *
    3371                 :             :          * Note: We deliberately avoid a Valgrind client request here.
    3372                 :             :          * Individual access methods can optionally superimpose buffer page
    3373                 :             :          * client requests on top of our client requests to enforce that
    3374                 :             :          * buffers are only accessed while locked (and pinned).  It's possible
    3375                 :             :          * that the buffer page is legitimately non-accessible here.  We
    3376                 :             :          * cannot meddle with that.
    3377                 :             :          */
    3378                 :     7436438 :         result = (pg_atomic_read_u64(&buf->state) & BM_VALID) != 0;
    3379                 :             : 
    3380                 :             :         Assert(ref->data.refcount > 0);
    3381                 :     7436438 :         ref->data.refcount++;
    3382                 :     7436438 :         ResourceOwnerRememberBuffer(CurrentResourceOwner, b);
    3383                 :             :     }
    3384                 :             : 
    3385                 :    82255587 :     return result;
    3386                 :             : }
    3387                 :             : 
    3388                 :             : /*
    3389                 :             :  * PinBuffer_Locked -- as above, but caller already locked the buffer header.
    3390                 :             :  * The spinlock is released before return.
    3391                 :             :  *
    3392                 :             :  * As this function is called with the spinlock held, the caller has to
    3393                 :             :  * previously call ReservePrivateRefCountEntry() and
    3394                 :             :  * ResourceOwnerEnlarge(CurrentResourceOwner);
    3395                 :             :  *
    3396                 :             :  * Currently, no callers of this function want to modify the buffer's
    3397                 :             :  * usage_count at all, so there's no need for a strategy parameter.
    3398                 :             :  * Also we don't bother with a BM_VALID test (the caller could check that for
    3399                 :             :  * itself).
    3400                 :             :  *
    3401                 :             :  * Also all callers only ever use this function when it's known that the
    3402                 :             :  * buffer can't have a preexisting pin by this backend. That allows us to skip
    3403                 :             :  * searching the private refcount array & hash, which is a boon, because the
    3404                 :             :  * spinlock is still held.
    3405                 :             :  *
    3406                 :             :  * Note: use of this routine is frequently mandatory, not just an optimization
    3407                 :             :  * to save a spin lock/unlock cycle, because we need to pin a buffer before
    3408                 :             :  * its state can change under us.
    3409                 :             :  */
    3410                 :             : static void
    3411                 :      345815 : PinBuffer_Locked(BufferDesc *buf)
    3412                 :             : {
    3413                 :             :     uint64      old_buf_state;
    3414                 :             : 
    3415                 :             :     /*
    3416                 :             :      * As explained, We don't expect any preexisting pins. That allows us to
    3417                 :             :      * manipulate the PrivateRefCount after releasing the spinlock
    3418                 :             :      */
    3419                 :             :     Assert(GetPrivateRefCountEntry(BufferDescriptorGetBuffer(buf), false) == NULL);
    3420                 :             : 
    3421                 :             :     /*
    3422                 :             :      * Since we hold the buffer spinlock, we can update the buffer state and
    3423                 :             :      * release the lock in one operation.
    3424                 :             :      */
    3425                 :      345815 :     old_buf_state = pg_atomic_read_u64(&buf->state);
    3426                 :             : 
    3427                 :      345815 :     UnlockBufHdrExt(buf, old_buf_state,
    3428                 :             :                     0, 0, 1);
    3429                 :             : 
    3430                 :      345815 :     TrackNewBufferPin(BufferDescriptorGetBuffer(buf));
    3431                 :      345815 : }
    3432                 :             : 
    3433                 :             : /*
    3434                 :             :  * Support for waking up another backend that is waiting for the cleanup lock
    3435                 :             :  * to be released using BM_PIN_COUNT_WAITER.
    3436                 :             :  *
    3437                 :             :  * See LockBufferForCleanup().
    3438                 :             :  *
    3439                 :             :  * Expected to be called just after releasing a buffer pin (in a BufferDesc,
    3440                 :             :  * not just reducing the backend-local pincount for the buffer).
    3441                 :             :  */
    3442                 :             : static void
    3443                 :           5 : WakePinCountWaiter(BufferDesc *buf)
    3444                 :             : {
    3445                 :             :     /*
    3446                 :             :      * Acquire the buffer header lock, re-check that there's a waiter. Another
    3447                 :             :      * backend could have unpinned this buffer, and already woken up the
    3448                 :             :      * waiter.
    3449                 :             :      *
    3450                 :             :      * There's no danger of the buffer being replaced after we unpinned it
    3451                 :             :      * above, as it's pinned by the waiter. The waiter removes
    3452                 :             :      * BM_PIN_COUNT_WAITER if it stops waiting for a reason other than this
    3453                 :             :      * backend waking it up.
    3454                 :             :      */
    3455                 :           5 :     uint64      buf_state = LockBufHdr(buf);
    3456                 :             : 
    3457         [ +  - ]:           5 :     if ((buf_state & BM_PIN_COUNT_WAITER) &&
    3458         [ +  - ]:           5 :         BUF_STATE_GET_REFCOUNT(buf_state) == 1)
    3459                 :           5 :     {
    3460                 :             :         /* we just released the last pin other than the waiter's */
    3461                 :           5 :         int         wait_backend_pgprocno = buf->wait_backend_pgprocno;
    3462                 :             : 
    3463                 :           5 :         UnlockBufHdrExt(buf, buf_state,
    3464                 :             :                         0, BM_PIN_COUNT_WAITER,
    3465                 :             :                         0);
    3466                 :           5 :         ProcSendSignal(wait_backend_pgprocno);
    3467                 :             :     }
    3468                 :             :     else
    3469                 :           0 :         UnlockBufHdr(buf);
    3470                 :           5 : }
    3471                 :             : 
    3472                 :             : /*
    3473                 :             :  * UnpinBuffer -- make buffer available for replacement.
    3474                 :             :  *
    3475                 :             :  * This should be applied only to shared buffers, never local ones.  This
    3476                 :             :  * always adjusts CurrentResourceOwner.
    3477                 :             :  */
    3478                 :             : static void
    3479                 :    49624078 : UnpinBuffer(BufferDesc *buf)
    3480                 :             : {
    3481                 :    49624078 :     Buffer      b = BufferDescriptorGetBuffer(buf);
    3482                 :             : 
    3483                 :    49624078 :     ResourceOwnerForgetBuffer(CurrentResourceOwner, b);
    3484                 :    49624078 :     UnpinBufferNoOwner(buf);
    3485                 :    49624078 : }
    3486                 :             : 
    3487                 :             : static void
    3488                 :    49630815 : UnpinBufferNoOwner(BufferDesc *buf)
    3489                 :             : {
    3490                 :             :     PrivateRefCountEntry *ref;
    3491                 :    49630815 :     Buffer      b = BufferDescriptorGetBuffer(buf);
    3492                 :             : 
    3493                 :             :     Assert(!BufferIsLocal(b));
    3494                 :             : 
    3495                 :             :     /* not moving as we're likely deleting it soon anyway */
    3496                 :    49630815 :     ref = GetPrivateRefCountEntry(b, false);
    3497                 :             :     Assert(ref != NULL);
    3498                 :             :     Assert(ref->data.refcount > 0);
    3499                 :    49630815 :     ref->data.refcount--;
    3500         [ +  + ]:    49630815 :     if (ref->data.refcount == 0)
    3501                 :             :     {
    3502                 :             :         uint64      old_buf_state;
    3503                 :             : 
    3504                 :             :         /*
    3505                 :             :          * Mark buffer non-accessible to Valgrind.
    3506                 :             :          *
    3507                 :             :          * Note that the buffer may have already been marked non-accessible
    3508                 :             :          * within access method code that enforces that buffers are only
    3509                 :             :          * accessed while a buffer lock is held.
    3510                 :             :          */
    3511                 :             :         VALGRIND_MAKE_MEM_NOACCESS(BufHdrGetBlock(buf), BLCKSZ);
    3512                 :             : 
    3513                 :             :         /*
    3514                 :             :          * I'd better not still hold the buffer content lock. Can't use
    3515                 :             :          * BufferIsLockedByMe(), as that asserts the buffer is pinned.
    3516                 :             :          */
    3517                 :             :         Assert(!BufferLockHeldByMe(buf));
    3518                 :             : 
    3519                 :             :         /* decrement the shared reference count */
    3520                 :    30001665 :         old_buf_state = pg_atomic_fetch_sub_u64(&buf->state, BUF_REFCOUNT_ONE);
    3521                 :             : 
    3522                 :             :         /* Support LockBufferForCleanup() */
    3523         [ +  + ]:    30001665 :         if (old_buf_state & BM_PIN_COUNT_WAITER)
    3524                 :           4 :             WakePinCountWaiter(buf);
    3525                 :             : 
    3526                 :    30001665 :         ForgetPrivateRefCountEntry(ref);
    3527                 :             :     }
    3528                 :    49630815 : }
    3529                 :             : 
    3530                 :             : /*
    3531                 :             :  * Set up backend-local tracking of a buffer pinned the first time by this
    3532                 :             :  * backend.
    3533                 :             :  */
    3534                 :             : inline void
    3535                 :    77357639 : TrackNewBufferPin(Buffer buf)
    3536                 :             : {
    3537                 :             :     PrivateRefCountEntry *ref;
    3538                 :             : 
    3539                 :    77357639 :     ref = NewPrivateRefCountEntry(buf);
    3540                 :    77357639 :     ref->data.refcount++;
    3541                 :             : 
    3542                 :    77357639 :     ResourceOwnerRememberBuffer(CurrentResourceOwner, buf);
    3543                 :             : 
    3544                 :             :     /*
    3545                 :             :      * This is the first pin for this page by this backend, mark its page as
    3546                 :             :      * defined to valgrind. While the page contents might not actually be
    3547                 :             :      * valid yet, we don't currently guarantee that such pages are marked
    3548                 :             :      * undefined or non-accessible.
    3549                 :             :      *
    3550                 :             :      * It's not necessarily the prettiest to do this here, but otherwise we'd
    3551                 :             :      * need this block of code in multiple places.
    3552                 :             :      */
    3553                 :             :     VALGRIND_MAKE_MEM_DEFINED(BufHdrGetBlock(GetBufferDescriptor(buf - 1)),
    3554                 :             :                               BLCKSZ);
    3555                 :    77357639 : }
    3556                 :             : 
    3557                 :             : #define ST_SORT sort_checkpoint_bufferids
    3558                 :             : #define ST_ELEMENT_TYPE CkptSortItem
    3559                 :             : #define ST_COMPARE(a, b) ckpt_buforder_comparator(a, b)
    3560                 :             : #define ST_SCOPE static
    3561                 :             : #define ST_DEFINE
    3562                 :             : #include "lib/sort_template.h"
    3563                 :             : 
    3564                 :             : /*
    3565                 :             :  * BufferSync -- Write out all dirty buffers in the pool.
    3566                 :             :  *
    3567                 :             :  * This is called at checkpoint time to write out all dirty shared buffers.
    3568                 :             :  * The checkpoint request flags should be passed in.  If CHECKPOINT_FAST is
    3569                 :             :  * set, we disable delays between writes; if CHECKPOINT_IS_SHUTDOWN,
    3570                 :             :  * CHECKPOINT_END_OF_RECOVERY or CHECKPOINT_FLUSH_UNLOGGED is set, we write
    3571                 :             :  * even unlogged buffers, which are otherwise skipped.  The remaining flags
    3572                 :             :  * currently have no effect here.
    3573                 :             :  */
    3574                 :             : static void
    3575                 :        1950 : BufferSync(int flags)
    3576                 :             : {
    3577                 :             :     uint64      buf_state;
    3578                 :             :     int         buf_id;
    3579                 :             :     int         num_to_scan;
    3580                 :             :     int         num_spaces;
    3581                 :             :     int         num_processed;
    3582                 :             :     int         num_written;
    3583                 :        1950 :     CkptTsStatus *per_ts_stat = NULL;
    3584                 :             :     Oid         last_tsid;
    3585                 :             :     binaryheap *ts_heap;
    3586                 :             :     int         i;
    3587                 :        1950 :     uint64      mask = BM_DIRTY;
    3588                 :             :     WritebackContext wb_context;
    3589                 :             : 
    3590                 :             :     /*
    3591                 :             :      * Unless this is a shutdown checkpoint or we have been explicitly told,
    3592                 :             :      * we write only permanent, dirty buffers.  But at shutdown or end of
    3593                 :             :      * recovery, we write all dirty buffers.
    3594                 :             :      */
    3595         [ +  + ]:        1950 :     if (!((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY |
    3596                 :             :                     CHECKPOINT_FLUSH_UNLOGGED))))
    3597                 :        1064 :         mask |= BM_PERMANENT;
    3598                 :             : 
    3599                 :             :     /*
    3600                 :             :      * Loop over all buffers, and mark the ones that need to be written with
    3601                 :             :      * BM_CHECKPOINT_NEEDED.  Count them as we go (num_to_scan), so that we
    3602                 :             :      * can estimate how much work needs to be done.
    3603                 :             :      *
    3604                 :             :      * This allows us to write only those pages that were dirty when the
    3605                 :             :      * checkpoint began, and not those that get dirtied while it proceeds.
    3606                 :             :      * Whenever a page with BM_CHECKPOINT_NEEDED is written out, either by us
    3607                 :             :      * later in this function, or by normal backends or the bgwriter cleaning
    3608                 :             :      * scan, the flag is cleared.  Any buffer dirtied after this point won't
    3609                 :             :      * have the flag set.
    3610                 :             :      *
    3611                 :             :      * Note that if we fail to write some buffer, we may leave buffers with
    3612                 :             :      * BM_CHECKPOINT_NEEDED still set.  This is OK since any such buffer would
    3613                 :             :      * certainly need to be written for the next checkpoint attempt, too.
    3614                 :             :      */
    3615                 :        1950 :     num_to_scan = 0;
    3616         [ +  + ]:    13971310 :     for (buf_id = 0; buf_id < NBuffers; buf_id++)
    3617                 :             :     {
    3618                 :    13969360 :         BufferDesc *bufHdr = GetBufferDescriptor(buf_id);
    3619                 :    13969360 :         uint64      set_bits = 0;
    3620                 :             : 
    3621                 :             :         /*
    3622                 :             :          * Header spinlock is enough to examine BM_DIRTY, see comment in
    3623                 :             :          * SyncOneBuffer.
    3624                 :             :          */
    3625                 :    13969360 :         buf_state = LockBufHdr(bufHdr);
    3626                 :             : 
    3627         [ +  + ]:    13969360 :         if ((buf_state & mask) == mask)
    3628                 :             :         {
    3629                 :             :             CkptSortItem *item;
    3630                 :             : 
    3631                 :      334783 :             set_bits = BM_CHECKPOINT_NEEDED;
    3632                 :             : 
    3633                 :      334783 :             item = &CkptBufferIds[num_to_scan++];
    3634                 :      334783 :             item->buf_id = buf_id;
    3635                 :      334783 :             item->tsId = bufHdr->tag.spcOid;
    3636                 :      334783 :             item->relNumber = BufTagGetRelNumber(&bufHdr->tag);
    3637                 :      334783 :             item->forkNum = BufTagGetForkNum(&bufHdr->tag);
    3638                 :      334783 :             item->blockNum = bufHdr->tag.blockNum;
    3639                 :             :         }
    3640                 :             : 
    3641                 :    13969360 :         UnlockBufHdrExt(bufHdr, buf_state,
    3642                 :             :                         set_bits, 0,
    3643                 :             :                         0);
    3644                 :             : 
    3645                 :             :         /* Check for barrier events in case NBuffers is large. */
    3646         [ +  + ]:    13969360 :         if (ProcSignalBarrierPending)
    3647                 :           1 :             ProcessProcSignalBarrier();
    3648                 :             :     }
    3649                 :             : 
    3650         [ +  + ]:        1950 :     if (num_to_scan == 0)
    3651                 :         733 :         return;                 /* nothing to do */
    3652                 :             : 
    3653                 :        1217 :     WritebackContextInit(&wb_context, &checkpoint_flush_after);
    3654                 :             : 
    3655                 :             :     TRACE_POSTGRESQL_BUFFER_SYNC_START(NBuffers, num_to_scan);
    3656                 :             : 
    3657                 :             :     /*
    3658                 :             :      * Sort buffers that need to be written to reduce the likelihood of random
    3659                 :             :      * IO. The sorting is also important for the implementation of balancing
    3660                 :             :      * writes between tablespaces. Without balancing writes we'd potentially
    3661                 :             :      * end up writing to the tablespaces one-by-one; possibly overloading the
    3662                 :             :      * underlying system.
    3663                 :             :      */
    3664                 :        1217 :     sort_checkpoint_bufferids(CkptBufferIds, num_to_scan);
    3665                 :             : 
    3666                 :        1217 :     num_spaces = 0;
    3667                 :             : 
    3668                 :             :     /*
    3669                 :             :      * Allocate progress status for each tablespace with buffers that need to
    3670                 :             :      * be flushed. This requires the to-be-flushed array to be sorted.
    3671                 :             :      */
    3672                 :        1217 :     last_tsid = InvalidOid;
    3673         [ +  + ]:      336000 :     for (i = 0; i < num_to_scan; i++)
    3674                 :             :     {
    3675                 :             :         CkptTsStatus *s;
    3676                 :             :         Oid         cur_tsid;
    3677                 :             : 
    3678                 :      334783 :         cur_tsid = CkptBufferIds[i].tsId;
    3679                 :             : 
    3680                 :             :         /*
    3681                 :             :          * Grow array of per-tablespace status structs, every time a new
    3682                 :             :          * tablespace is found.
    3683                 :             :          */
    3684   [ +  +  +  + ]:      334783 :         if (last_tsid == InvalidOid || last_tsid != cur_tsid)
    3685                 :        1840 :         {
    3686                 :             :             Size        sz;
    3687                 :             : 
    3688                 :        1840 :             num_spaces++;
    3689                 :             : 
    3690                 :             :             /*
    3691                 :             :              * Not worth adding grow-by-power-of-2 logic here - even with a
    3692                 :             :              * few hundred tablespaces this should be fine.
    3693                 :             :              */
    3694                 :        1840 :             sz = sizeof(CkptTsStatus) * num_spaces;
    3695                 :             : 
    3696         [ +  + ]:        1840 :             if (per_ts_stat == NULL)
    3697                 :        1217 :                 per_ts_stat = (CkptTsStatus *) palloc(sz);
    3698                 :             :             else
    3699                 :         623 :                 per_ts_stat = (CkptTsStatus *) repalloc(per_ts_stat, sz);
    3700                 :             : 
    3701                 :        1840 :             s = &per_ts_stat[num_spaces - 1];
    3702                 :        1840 :             memset(s, 0, sizeof(*s));
    3703                 :        1840 :             s->tsId = cur_tsid;
    3704                 :             : 
    3705                 :             :             /*
    3706                 :             :              * The first buffer in this tablespace. As CkptBufferIds is sorted
    3707                 :             :              * by tablespace all (s->num_to_scan) buffers in this tablespace
    3708                 :             :              * will follow afterwards.
    3709                 :             :              */
    3710                 :        1840 :             s->index = i;
    3711                 :             : 
    3712                 :             :             /*
    3713                 :             :              * progress_slice will be determined once we know how many buffers
    3714                 :             :              * are in each tablespace, i.e. after this loop.
    3715                 :             :              */
    3716                 :             : 
    3717                 :        1840 :             last_tsid = cur_tsid;
    3718                 :             :         }
    3719                 :             :         else
    3720                 :             :         {
    3721                 :      332943 :             s = &per_ts_stat[num_spaces - 1];
    3722                 :             :         }
    3723                 :             : 
    3724                 :      334783 :         s->num_to_scan++;
    3725                 :             : 
    3726                 :             :         /* Check for barrier events. */
    3727         [ -  + ]:      334783 :         if (ProcSignalBarrierPending)
    3728                 :           0 :             ProcessProcSignalBarrier();
    3729                 :             :     }
    3730                 :             : 
    3731                 :             :     Assert(num_spaces > 0);
    3732                 :             : 
    3733                 :             :     /*
    3734                 :             :      * Build a min-heap over the write-progress in the individual tablespaces,
    3735                 :             :      * and compute how large a portion of the total progress a single
    3736                 :             :      * processed buffer is.
    3737                 :             :      */
    3738                 :        1217 :     ts_heap = binaryheap_allocate(num_spaces,
    3739                 :             :                                   ts_ckpt_progress_comparator,
    3740                 :             :                                   NULL);
    3741                 :             : 
    3742         [ +  + ]:        3057 :     for (i = 0; i < num_spaces; i++)
    3743                 :             :     {
    3744                 :        1840 :         CkptTsStatus *ts_stat = &per_ts_stat[i];
    3745                 :             : 
    3746                 :        1840 :         ts_stat->progress_slice = (float8) num_to_scan / ts_stat->num_to_scan;
    3747                 :             : 
    3748                 :        1840 :         binaryheap_add_unordered(ts_heap, PointerGetDatum(ts_stat));
    3749                 :             :     }
    3750                 :             : 
    3751                 :        1217 :     binaryheap_build(ts_heap);
    3752                 :             : 
    3753                 :             :     /*
    3754                 :             :      * Iterate through to-be-checkpointed buffers and write the ones (still)
    3755                 :             :      * marked with BM_CHECKPOINT_NEEDED. The writes are balanced between
    3756                 :             :      * tablespaces; otherwise the sorting would lead to only one tablespace
    3757                 :             :      * receiving writes at a time, making inefficient use of the hardware.
    3758                 :             :      */
    3759                 :        1217 :     num_processed = 0;
    3760                 :        1217 :     num_written = 0;
    3761         [ +  + ]:      336000 :     while (!binaryheap_empty(ts_heap))
    3762                 :             :     {
    3763                 :      334783 :         BufferDesc *bufHdr = NULL;
    3764                 :             :         CkptTsStatus *ts_stat = (CkptTsStatus *)
    3765                 :      334783 :             DatumGetPointer(binaryheap_first(ts_heap));
    3766                 :             : 
    3767                 :      334783 :         buf_id = CkptBufferIds[ts_stat->index].buf_id;
    3768                 :             :         Assert(buf_id != -1);
    3769                 :             : 
    3770                 :      334783 :         bufHdr = GetBufferDescriptor(buf_id);
    3771                 :             : 
    3772                 :      334783 :         num_processed++;
    3773                 :             : 
    3774                 :             :         /*
    3775                 :             :          * We don't need to acquire the lock here, because we're only looking
    3776                 :             :          * at a single bit. It's possible that someone else writes the buffer
    3777                 :             :          * and clears the flag right after we check, but that doesn't matter
    3778                 :             :          * since SyncOneBuffer will then do nothing.  However, there is a
    3779                 :             :          * further race condition: it's conceivable that between the time we
    3780                 :             :          * examine the bit here and the time SyncOneBuffer acquires the lock,
    3781                 :             :          * someone else not only wrote the buffer but replaced it with another
    3782                 :             :          * page and dirtied it.  In that improbable case, SyncOneBuffer will
    3783                 :             :          * write the buffer though we didn't need to.  It doesn't seem worth
    3784                 :             :          * guarding against this, though.
    3785                 :             :          */
    3786         [ +  + ]:      334783 :         if (pg_atomic_read_u64(&bufHdr->state) & BM_CHECKPOINT_NEEDED)
    3787                 :             :         {
    3788         [ +  - ]:      312654 :             if (SyncOneBuffer(buf_id, false, &wb_context) & BUF_WRITTEN)
    3789                 :             :             {
    3790                 :             :                 TRACE_POSTGRESQL_BUFFER_SYNC_WRITTEN(buf_id);
    3791                 :      312654 :                 PendingCheckpointerStats.buffers_written++;
    3792                 :      312654 :                 num_written++;
    3793                 :             :             }
    3794                 :             :         }
    3795                 :             : 
    3796                 :             :         /*
    3797                 :             :          * Measure progress independent of actually having to flush the buffer
    3798                 :             :          * - otherwise writing become unbalanced.
    3799                 :             :          */
    3800                 :      334783 :         ts_stat->progress += ts_stat->progress_slice;
    3801                 :      334783 :         ts_stat->num_scanned++;
    3802                 :      334783 :         ts_stat->index++;
    3803                 :             : 
    3804                 :             :         /* Have all the buffers from the tablespace been processed? */
    3805         [ +  + ]:      334783 :         if (ts_stat->num_scanned == ts_stat->num_to_scan)
    3806                 :             :         {
    3807                 :        1840 :             binaryheap_remove_first(ts_heap);
    3808                 :             :         }
    3809                 :             :         else
    3810                 :             :         {
    3811                 :             :             /* update heap with the new progress */
    3812                 :      332943 :             binaryheap_replace_first(ts_heap, PointerGetDatum(ts_stat));
    3813                 :             :         }
    3814                 :             : 
    3815                 :             :         /*
    3816                 :             :          * Sleep to throttle our I/O rate.
    3817                 :             :          *
    3818                 :             :          * (This will check for barrier events even if it doesn't sleep.)
    3819                 :             :          */
    3820                 :      334783 :         CheckpointWriteDelay(flags, (double) num_processed / num_to_scan);
    3821                 :             :     }
    3822                 :             : 
    3823                 :             :     /*
    3824                 :             :      * Issue all pending flushes. Only checkpointer calls BufferSync(), so
    3825                 :             :      * IOContext will always be IOCONTEXT_NORMAL.
    3826                 :             :      */
    3827                 :        1217 :     IssuePendingWritebacks(&wb_context, IOCONTEXT_NORMAL);
    3828                 :             : 
    3829                 :        1217 :     pfree(per_ts_stat);
    3830                 :        1217 :     per_ts_stat = NULL;
    3831                 :        1217 :     binaryheap_free(ts_heap);
    3832                 :             : 
    3833                 :             :     /*
    3834                 :             :      * Update checkpoint statistics. As noted above, this doesn't include
    3835                 :             :      * buffers written by other backends or bgwriter scan.
    3836                 :             :      */
    3837                 :        1217 :     CheckpointStats.ckpt_bufs_written += num_written;
    3838                 :             : 
    3839                 :             :     TRACE_POSTGRESQL_BUFFER_SYNC_DONE(NBuffers, num_written, num_to_scan);
    3840                 :             : }
    3841                 :             : 
    3842                 :             : /*
    3843                 :             :  * BgBufferSync -- Write out some dirty buffers in the pool.
    3844                 :             :  *
    3845                 :             :  * This is called periodically by the background writer process.
    3846                 :             :  *
    3847                 :             :  * Returns true if it's appropriate for the bgwriter process to go into
    3848                 :             :  * low-power hibernation mode.  (This happens if the strategy clock-sweep
    3849                 :             :  * has been "lapped" and no buffer allocations have occurred recently,
    3850                 :             :  * or if the bgwriter has been effectively disabled by setting
    3851                 :             :  * bgwriter_lru_maxpages to 0.)
    3852                 :             :  */
    3853                 :             : bool
    3854                 :       15110 : BgBufferSync(WritebackContext *wb_context)
    3855                 :             : {
    3856                 :             :     /* info obtained from freelist.c */
    3857                 :             :     int         strategy_buf_id;
    3858                 :             :     uint32      strategy_passes;
    3859                 :             :     uint32      recent_alloc;
    3860                 :             : 
    3861                 :             :     /*
    3862                 :             :      * Information saved between calls so we can determine the strategy
    3863                 :             :      * point's advance rate and avoid scanning already-cleaned buffers.
    3864                 :             :      */
    3865                 :             :     static bool saved_info_valid = false;
    3866                 :             :     static int  prev_strategy_buf_id;
    3867                 :             :     static uint32 prev_strategy_passes;
    3868                 :             :     static int  next_to_clean;
    3869                 :             :     static uint32 next_passes;
    3870                 :             : 
    3871                 :             :     /* Moving averages of allocation rate and clean-buffer density */
    3872                 :             :     static float smoothed_alloc = 0;
    3873                 :             :     static float smoothed_density = 10.0;
    3874                 :             : 
    3875                 :             :     /* Potentially these could be tunables, but for now, not */
    3876                 :       15110 :     float       smoothing_samples = 16;
    3877                 :       15110 :     float       scan_whole_pool_milliseconds = 120000.0;
    3878                 :             : 
    3879                 :             :     /* Used to compute how far we scan ahead */
    3880                 :             :     long        strategy_delta;
    3881                 :             :     int         bufs_to_lap;
    3882                 :             :     int         bufs_ahead;
    3883                 :             :     float       scans_per_alloc;
    3884                 :             :     int         reusable_buffers_est;
    3885                 :             :     int         upcoming_alloc_est;
    3886                 :             :     int         min_scan_buffers;
    3887                 :             : 
    3888                 :             :     /* Variables for the scanning loop proper */
    3889                 :             :     int         num_to_scan;
    3890                 :             :     int         num_written;
    3891                 :             :     int         reusable_buffers;
    3892                 :             : 
    3893                 :             :     /* Variables for final smoothed_density update */
    3894                 :             :     long        new_strategy_delta;
    3895                 :             :     uint32      new_recent_alloc;
    3896                 :             : 
    3897                 :             :     /*
    3898                 :             :      * Find out where the clock-sweep currently is, and how many buffer
    3899                 :             :      * allocations have happened since our last call.
    3900                 :             :      */
    3901                 :       15110 :     strategy_buf_id = StrategySyncStart(&strategy_passes, &recent_alloc);
    3902                 :             : 
    3903                 :             :     /* Report buffer alloc counts to pgstat */
    3904                 :       15110 :     PendingBgWriterStats.buf_alloc += recent_alloc;
    3905                 :             : 
    3906                 :             :     /*
    3907                 :             :      * If we're not running the LRU scan, just stop after doing the stats
    3908                 :             :      * stuff.  We mark the saved state invalid so that we can recover sanely
    3909                 :             :      * if LRU scan is turned back on later.
    3910                 :             :      */
    3911         [ +  + ]:       15110 :     if (bgwriter_lru_maxpages <= 0)
    3912                 :             :     {
    3913                 :          39 :         saved_info_valid = false;
    3914                 :          39 :         return true;
    3915                 :             :     }
    3916                 :             : 
    3917                 :             :     /*
    3918                 :             :      * Compute strategy_delta = how many buffers have been scanned by the
    3919                 :             :      * clock-sweep since last time.  If first time through, assume none. Then
    3920                 :             :      * see if we are still ahead of the clock-sweep, and if so, how many
    3921                 :             :      * buffers we could scan before we'd catch up with it and "lap" it. Note:
    3922                 :             :      * weird-looking coding of xxx_passes comparisons are to avoid bogus
    3923                 :             :      * behavior when the passes counts wrap around.
    3924                 :             :      */
    3925         [ +  + ]:       15071 :     if (saved_info_valid)
    3926                 :             :     {
    3927                 :       14436 :         int32       passes_delta = strategy_passes - prev_strategy_passes;
    3928                 :             : 
    3929                 :       14436 :         strategy_delta = strategy_buf_id - prev_strategy_buf_id;
    3930                 :       14436 :         strategy_delta += (long) passes_delta * NBuffers;
    3931                 :             : 
    3932                 :             :         Assert(strategy_delta >= 0);
    3933                 :             : 
    3934         [ +  + ]:       14436 :         if ((int32) (next_passes - strategy_passes) > 0)
    3935                 :             :         {
    3936                 :             :             /* we're one pass ahead of the strategy point */
    3937                 :        2459 :             bufs_to_lap = strategy_buf_id - next_to_clean;
    3938                 :             : #ifdef BGW_DEBUG
    3939                 :             :             elog(DEBUG2, "bgwriter ahead: bgw %u-%u strategy %u-%u delta=%ld lap=%d",
    3940                 :             :                  next_passes, next_to_clean,
    3941                 :             :                  strategy_passes, strategy_buf_id,
    3942                 :             :                  strategy_delta, bufs_to_lap);
    3943                 :             : #endif
    3944                 :             :         }
    3945         [ +  + ]:       11977 :         else if (next_passes == strategy_passes &&
    3946         [ +  + ]:        9380 :                  next_to_clean >= strategy_buf_id)
    3947                 :             :         {
    3948                 :             :             /* on same pass, but ahead or at least not behind */
    3949                 :        8415 :             bufs_to_lap = NBuffers - (next_to_clean - strategy_buf_id);
    3950                 :             : #ifdef BGW_DEBUG
    3951                 :             :             elog(DEBUG2, "bgwriter ahead: bgw %u-%u strategy %u-%u delta=%ld lap=%d",
    3952                 :             :                  next_passes, next_to_clean,
    3953                 :             :                  strategy_passes, strategy_buf_id,
    3954                 :             :                  strategy_delta, bufs_to_lap);
    3955                 :             : #endif
    3956                 :             :         }
    3957                 :             :         else
    3958                 :             :         {
    3959                 :             :             /*
    3960                 :             :              * We're behind, so skip forward to the strategy point and start
    3961                 :             :              * cleaning from there.
    3962                 :             :              */
    3963                 :             : #ifdef BGW_DEBUG
    3964                 :             :             elog(DEBUG2, "bgwriter behind: bgw %u-%u strategy %u-%u delta=%ld",
    3965                 :             :                  next_passes, next_to_clean,
    3966                 :             :                  strategy_passes, strategy_buf_id,
    3967                 :             :                  strategy_delta);
    3968                 :             : #endif
    3969                 :        3562 :             next_to_clean = strategy_buf_id;
    3970                 :        3562 :             next_passes = strategy_passes;
    3971                 :        3562 :             bufs_to_lap = NBuffers;
    3972                 :             :         }
    3973                 :             :     }
    3974                 :             :     else
    3975                 :             :     {
    3976                 :             :         /*
    3977                 :             :          * Initializing at startup or after LRU scanning had been off. Always
    3978                 :             :          * start at the strategy point.
    3979                 :             :          */
    3980                 :             : #ifdef BGW_DEBUG
    3981                 :             :         elog(DEBUG2, "bgwriter initializing: strategy %u-%u",
    3982                 :             :              strategy_passes, strategy_buf_id);
    3983                 :             : #endif
    3984                 :         635 :         strategy_delta = 0;
    3985                 :         635 :         next_to_clean = strategy_buf_id;
    3986                 :         635 :         next_passes = strategy_passes;
    3987                 :         635 :         bufs_to_lap = NBuffers;
    3988                 :             :     }
    3989                 :             : 
    3990                 :             :     /* Update saved info for next time */
    3991                 :       15071 :     prev_strategy_buf_id = strategy_buf_id;
    3992                 :       15071 :     prev_strategy_passes = strategy_passes;
    3993                 :       15071 :     saved_info_valid = true;
    3994                 :             : 
    3995                 :             :     /*
    3996                 :             :      * Compute how many buffers had to be scanned for each new allocation, ie,
    3997                 :             :      * 1/density of reusable buffers, and track a moving average of that.
    3998                 :             :      *
    3999                 :             :      * If the strategy point didn't move, we don't update the density estimate
    4000                 :             :      */
    4001   [ +  +  +  - ]:       15071 :     if (strategy_delta > 0 && recent_alloc > 0)
    4002                 :             :     {
    4003                 :        8175 :         scans_per_alloc = (float) strategy_delta / (float) recent_alloc;
    4004                 :        8175 :         smoothed_density += (scans_per_alloc - smoothed_density) /
    4005                 :             :             smoothing_samples;
    4006                 :             :     }
    4007                 :             : 
    4008                 :             :     /*
    4009                 :             :      * Estimate how many reusable buffers there are between the current
    4010                 :             :      * strategy point and where we've scanned ahead to, based on the smoothed
    4011                 :             :      * density estimate.
    4012                 :             :      */
    4013                 :       15071 :     bufs_ahead = NBuffers - bufs_to_lap;
    4014                 :       15071 :     reusable_buffers_est = (float) bufs_ahead / smoothed_density;
    4015                 :             : 
    4016                 :             :     /*
    4017                 :             :      * Track a moving average of recent buffer allocations.  Here, rather than
    4018                 :             :      * a true average we want a fast-attack, slow-decline behavior: we
    4019                 :             :      * immediately follow any increase.
    4020                 :             :      */
    4021         [ +  + ]:       15071 :     if (smoothed_alloc <= (float) recent_alloc)
    4022                 :        3939 :         smoothed_alloc = recent_alloc;
    4023                 :             :     else
    4024                 :       11132 :         smoothed_alloc += ((float) recent_alloc - smoothed_alloc) /
    4025                 :             :             smoothing_samples;
    4026                 :             : 
    4027                 :             :     /* Scale the estimate by a GUC to allow more aggressive tuning. */
    4028                 :       15071 :     upcoming_alloc_est = (int) (smoothed_alloc * bgwriter_lru_multiplier);
    4029                 :             : 
    4030                 :             :     /*
    4031                 :             :      * If recent_alloc remains at zero for many cycles, smoothed_alloc will
    4032                 :             :      * eventually underflow to zero, and the underflows produce annoying
    4033                 :             :      * kernel warnings on some platforms.  Once upcoming_alloc_est has gone to
    4034                 :             :      * zero, there's no point in tracking smaller and smaller values of
    4035                 :             :      * smoothed_alloc, so just reset it to exactly zero to avoid this
    4036                 :             :      * syndrome.  It will pop back up as soon as recent_alloc increases.
    4037                 :             :      */
    4038         [ +  + ]:       15071 :     if (upcoming_alloc_est == 0)
    4039                 :        2282 :         smoothed_alloc = 0;
    4040                 :             : 
    4041                 :             :     /*
    4042                 :             :      * Even in cases where there's been little or no buffer allocation
    4043                 :             :      * activity, we want to make a small amount of progress through the buffer
    4044                 :             :      * cache so that as many reusable buffers as possible are clean after an
    4045                 :             :      * idle period.
    4046                 :             :      *
    4047                 :             :      * (scan_whole_pool_milliseconds / BgWriterDelay) computes how many times
    4048                 :             :      * the BGW will be called during the scan_whole_pool time; slice the
    4049                 :             :      * buffer pool into that many sections.
    4050                 :             :      */
    4051                 :       15071 :     min_scan_buffers = (int) (NBuffers / (scan_whole_pool_milliseconds / BgWriterDelay));
    4052                 :             : 
    4053         [ +  + ]:       15071 :     if (upcoming_alloc_est < (min_scan_buffers + reusable_buffers_est))
    4054                 :             :     {
    4055                 :             : #ifdef BGW_DEBUG
    4056                 :             :         elog(DEBUG2, "bgwriter: alloc_est=%d too small, using min=%d + reusable_est=%d",
    4057                 :             :              upcoming_alloc_est, min_scan_buffers, reusable_buffers_est);
    4058                 :             : #endif
    4059                 :        7184 :         upcoming_alloc_est = min_scan_buffers + reusable_buffers_est;
    4060                 :             :     }
    4061                 :             : 
    4062                 :             :     /*
    4063                 :             :      * Now write out dirty reusable buffers, working forward from the
    4064                 :             :      * next_to_clean point, until we have lapped the strategy scan, or cleaned
    4065                 :             :      * enough buffers to match our estimate of the next cycle's allocation
    4066                 :             :      * requirements, or hit the bgwriter_lru_maxpages limit.
    4067                 :             :      */
    4068                 :             : 
    4069                 :       15071 :     num_to_scan = bufs_to_lap;
    4070                 :       15071 :     num_written = 0;
    4071                 :       15071 :     reusable_buffers = reusable_buffers_est;
    4072                 :             : 
    4073                 :             :     /* Execute the LRU scan */
    4074   [ +  +  +  + ]:     2058088 :     while (num_to_scan > 0 && reusable_buffers < upcoming_alloc_est)
    4075                 :             :     {
    4076                 :     2043020 :         int         sync_state = SyncOneBuffer(next_to_clean, true,
    4077                 :             :                                                wb_context);
    4078                 :             : 
    4079         [ +  + ]:     2043020 :         if (++next_to_clean >= NBuffers)
    4080                 :             :         {
    4081                 :        3417 :             next_to_clean = 0;
    4082                 :        3417 :             next_passes++;
    4083                 :             :         }
    4084                 :     2043020 :         num_to_scan--;
    4085                 :             : 
    4086         [ +  + ]:     2043020 :         if (sync_state & BUF_WRITTEN)
    4087                 :             :         {
    4088                 :       27982 :             reusable_buffers++;
    4089         [ +  + ]:       27982 :             if (++num_written >= bgwriter_lru_maxpages)
    4090                 :             :             {
    4091                 :           3 :                 PendingBgWriterStats.maxwritten_clean++;
    4092                 :           3 :                 break;
    4093                 :             :             }
    4094                 :             :         }
    4095         [ +  + ]:     2015038 :         else if (sync_state & BUF_REUSABLE)
    4096                 :     1579077 :             reusable_buffers++;
    4097                 :             :     }
    4098                 :             : 
    4099                 :       15071 :     PendingBgWriterStats.buf_written_clean += num_written;
    4100                 :             : 
    4101                 :             : #ifdef BGW_DEBUG
    4102                 :             :     elog(DEBUG1, "bgwriter: recent_alloc=%u smoothed=%.2f delta=%ld ahead=%d density=%.2f reusable_est=%d upcoming_est=%d scanned=%d wrote=%d reusable=%d",
    4103                 :             :          recent_alloc, smoothed_alloc, strategy_delta, bufs_ahead,
    4104                 :             :          smoothed_density, reusable_buffers_est, upcoming_alloc_est,
    4105                 :             :          bufs_to_lap - num_to_scan,
    4106                 :             :          num_written,
    4107                 :             :          reusable_buffers - reusable_buffers_est);
    4108                 :             : #endif
    4109                 :             : 
    4110                 :             :     /*
    4111                 :             :      * Consider the above scan as being like a new allocation scan.
    4112                 :             :      * Characterize its density and update the smoothed one based on it. This
    4113                 :             :      * effectively halves the moving average period in cases where both the
    4114                 :             :      * strategy and the background writer are doing some useful scanning,
    4115                 :             :      * which is helpful because a long memory isn't as desirable on the
    4116                 :             :      * density estimates.
    4117                 :             :      */
    4118                 :       15071 :     new_strategy_delta = bufs_to_lap - num_to_scan;
    4119                 :       15071 :     new_recent_alloc = reusable_buffers - reusable_buffers_est;
    4120   [ +  +  +  + ]:       15071 :     if (new_strategy_delta > 0 && new_recent_alloc > 0)
    4121                 :             :     {
    4122                 :       12872 :         scans_per_alloc = (float) new_strategy_delta / (float) new_recent_alloc;
    4123                 :       12872 :         smoothed_density += (scans_per_alloc - smoothed_density) /
    4124                 :             :             smoothing_samples;
    4125                 :             : 
    4126                 :             : #ifdef BGW_DEBUG
    4127                 :             :         elog(DEBUG2, "bgwriter: cleaner density alloc=%u scan=%ld density=%.2f new smoothed=%.2f",
    4128                 :             :              new_recent_alloc, new_strategy_delta,
    4129                 :             :              scans_per_alloc, smoothed_density);
    4130                 :             : #endif
    4131                 :             :     }
    4132                 :             : 
    4133                 :             :     /* Return true if OK to hibernate */
    4134   [ +  +  +  - ]:       15071 :     return (bufs_to_lap == 0 && recent_alloc == 0);
    4135                 :             : }
    4136                 :             : 
    4137                 :             : /*
    4138                 :             :  * SyncOneBuffer -- process a single buffer during syncing.
    4139                 :             :  *
    4140                 :             :  * If skip_recently_used is true, we don't write currently-pinned buffers, nor
    4141                 :             :  * buffers marked recently used, as these are not replacement candidates.
    4142                 :             :  *
    4143                 :             :  * Returns a bitmask containing the following flag bits:
    4144                 :             :  *  BUF_WRITTEN: we wrote the buffer.
    4145                 :             :  *  BUF_REUSABLE: buffer is available for replacement, ie, it has
    4146                 :             :  *      pin count 0 and usage count 0.
    4147                 :             :  *
    4148                 :             :  * (BUF_WRITTEN could be set in error if FlushBuffer finds the buffer clean
    4149                 :             :  * after locking it, but we don't care all that much.)
    4150                 :             :  */
    4151                 :             : static int
    4152                 :     2355674 : SyncOneBuffer(int buf_id, bool skip_recently_used, WritebackContext *wb_context)
    4153                 :             : {
    4154                 :     2355674 :     BufferDesc *bufHdr = GetBufferDescriptor(buf_id);
    4155                 :     2355674 :     int         result = 0;
    4156                 :             :     uint64      buf_state;
    4157                 :             :     BufferTag   tag;
    4158                 :             : 
    4159                 :             :     /* Make sure we can handle the pin */
    4160                 :     2355674 :     ReservePrivateRefCountEntry();
    4161                 :     2355674 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    4162                 :             : 
    4163                 :             :     /*
    4164                 :             :      * Check whether buffer needs writing.
    4165                 :             :      *
    4166                 :             :      * We can make this check without taking the buffer content lock so long
    4167                 :             :      * as we mark pages dirty in access methods *before* logging changes with
    4168                 :             :      * XLogInsert(): if someone marks the buffer dirty just after our check we
    4169                 :             :      * don't worry because our checkpoint.redo points before log record for
    4170                 :             :      * upcoming changes and so we are not required to write such dirty buffer.
    4171                 :             :      */
    4172                 :     2355674 :     buf_state = LockBufHdr(bufHdr);
    4173                 :             : 
    4174         [ +  + ]:     2355674 :     if (BUF_STATE_GET_REFCOUNT(buf_state) == 0 &&
    4175         [ +  + ]:     2352355 :         BUF_STATE_GET_USAGECOUNT(buf_state) == 0)
    4176                 :             :     {
    4177                 :     1609254 :         result |= BUF_REUSABLE;
    4178                 :             :     }
    4179         [ +  + ]:      746420 :     else if (skip_recently_used)
    4180                 :             :     {
    4181                 :             :         /* Caller told us not to write recently-used buffers */
    4182                 :      435961 :         UnlockBufHdr(bufHdr);
    4183                 :      435961 :         return result;
    4184                 :             :     }
    4185                 :             : 
    4186   [ +  +  +  + ]:     1919713 :     if (!(buf_state & BM_VALID) || !(buf_state & BM_DIRTY))
    4187                 :             :     {
    4188                 :             :         /* It's clean, so nothing to do */
    4189                 :     1579077 :         UnlockBufHdr(bufHdr);
    4190                 :     1579077 :         return result;
    4191                 :             :     }
    4192                 :             : 
    4193                 :             :     /*
    4194                 :             :      * Pin it, share-exclusive-lock it, write it.  (FlushBuffer will do
    4195                 :             :      * nothing if the buffer is clean by the time we've locked it.)
    4196                 :             :      */
    4197                 :      340636 :     PinBuffer_Locked(bufHdr);
    4198                 :             : 
    4199                 :      340636 :     FlushUnlockedBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    4200                 :             : 
    4201                 :      340636 :     tag = bufHdr->tag;
    4202                 :             : 
    4203                 :      340636 :     UnpinBuffer(bufHdr);
    4204                 :             : 
    4205                 :             :     /*
    4206                 :             :      * SyncOneBuffer() is only called by checkpointer and bgwriter, so
    4207                 :             :      * IOContext will always be IOCONTEXT_NORMAL.
    4208                 :             :      */
    4209                 :      340636 :     ScheduleBufferTagForWriteback(wb_context, IOCONTEXT_NORMAL, &tag);
    4210                 :             : 
    4211                 :      340636 :     return result | BUF_WRITTEN;
    4212                 :             : }
    4213                 :             : 
    4214                 :             : /*
    4215                 :             :  *      AtEOXact_Buffers - clean up at end of transaction.
    4216                 :             :  *
    4217                 :             :  *      As of PostgreSQL 8.0, buffer pins should get released by the
    4218                 :             :  *      ResourceOwner mechanism.  This routine is just a debugging
    4219                 :             :  *      cross-check that no pins remain.
    4220                 :             :  */
    4221                 :             : void
    4222                 :      654586 : AtEOXact_Buffers(bool isCommit)
    4223                 :             : {
    4224                 :      654586 :     CheckForBufferLeaks();
    4225                 :             : 
    4226                 :      654586 :     AtEOXact_LocalBuffers(isCommit);
    4227                 :             : 
    4228                 :             :     Assert(PrivateRefCountOverflowed == 0);
    4229                 :      654586 : }
    4230                 :             : 
    4231                 :             : /*
    4232                 :             :  * Initialize access to shared buffer pool
    4233                 :             :  *
    4234                 :             :  * This is called during backend startup (whether standalone or under the
    4235                 :             :  * postmaster).  It sets up for this backend's access to the already-existing
    4236                 :             :  * buffer pool.
    4237                 :             :  */
    4238                 :             : void
    4239                 :       24803 : InitBufferManagerAccess(void)
    4240                 :             : {
    4241                 :             :     /*
    4242                 :             :      * An advisory limit on the number of pins each backend should hold, based
    4243                 :             :      * on shared_buffers and the maximum number of connections possible.
    4244                 :             :      * That's very pessimistic, but outside toy-sized shared_buffers it should
    4245                 :             :      * allow plenty of pins.  LimitAdditionalPins() and
    4246                 :             :      * GetAdditionalPinLimit() can be used to check the remaining balance.
    4247                 :             :      */
    4248                 :       24803 :     MaxProportionalPins = NBuffers / (MaxBackends + NUM_AUXILIARY_PROCS);
    4249                 :             : 
    4250                 :       24803 :     memset(&PrivateRefCountArray, 0, sizeof(PrivateRefCountArray));
    4251                 :       24803 :     memset(&PrivateRefCountArrayKeys, 0, sizeof(PrivateRefCountArrayKeys));
    4252                 :             : 
    4253                 :       24803 :     PrivateRefCountHash = refcount_create(CurrentMemoryContext, 100, NULL);
    4254                 :             : 
    4255                 :             :     /*
    4256                 :             :      * AtProcExit_Buffers needs LWLock access, and thereby has to be called at
    4257                 :             :      * the corresponding phase of backend shutdown.
    4258                 :             :      */
    4259                 :             :     Assert(MyProc != NULL);
    4260                 :       24803 :     on_shmem_exit(AtProcExit_Buffers, 0);
    4261                 :       24803 : }
    4262                 :             : 
    4263                 :             : /*
    4264                 :             :  * During backend exit, ensure that we released all shared-buffer locks and
    4265                 :             :  * assert that we have no remaining pins.
    4266                 :             :  */
    4267                 :             : static void
    4268                 :       24803 : AtProcExit_Buffers(int code, Datum arg)
    4269                 :             : {
    4270                 :       24803 :     UnlockBuffers();
    4271                 :             : 
    4272                 :       24803 :     CheckForBufferLeaks();
    4273                 :             : 
    4274                 :             :     /* localbuf.c needs a chance too */
    4275                 :       24803 :     AtProcExit_LocalBuffers();
    4276                 :       24803 : }
    4277                 :             : 
    4278                 :             : /*
    4279                 :             :  *      CheckForBufferLeaks - ensure this backend holds no buffer pins
    4280                 :             :  *
    4281                 :             :  *      As of PostgreSQL 8.0, buffer pins should get released by the
    4282                 :             :  *      ResourceOwner mechanism.  This routine is just a debugging
    4283                 :             :  *      cross-check that no pins remain.
    4284                 :             :  */
    4285                 :             : static void
    4286                 :      679389 : CheckForBufferLeaks(void)
    4287                 :             : {
    4288                 :             : #ifdef USE_ASSERT_CHECKING
    4289                 :             :     int         RefCountErrors = 0;
    4290                 :             :     PrivateRefCountEntry *res;
    4291                 :             :     int         i;
    4292                 :             :     char       *s;
    4293                 :             : 
    4294                 :             :     /* check the array */
    4295                 :             :     for (i = 0; i < REFCOUNT_ARRAY_ENTRIES; i++)
    4296                 :             :     {
    4297                 :             :         if (PrivateRefCountArrayKeys[i] != InvalidBuffer)
    4298                 :             :         {
    4299                 :             :             res = &PrivateRefCountArray[i];
    4300                 :             : 
    4301                 :             :             s = DebugPrintBufferRefcount(res->buffer);
    4302                 :             :             elog(WARNING, "buffer refcount leak: %s", s);
    4303                 :             :             pfree(s);
    4304                 :             : 
    4305                 :             :             RefCountErrors++;
    4306                 :             :         }
    4307                 :             :     }
    4308                 :             : 
    4309                 :             :     /* if necessary search the hash */
    4310                 :             :     if (PrivateRefCountOverflowed)
    4311                 :             :     {
    4312                 :             :         refcount_iterator iter;
    4313                 :             : 
    4314                 :             :         refcount_start_iterate(PrivateRefCountHash, &iter);
    4315                 :             :         while ((res = refcount_iterate(PrivateRefCountHash, &iter)) != NULL)
    4316                 :             :         {
    4317                 :             :             s = DebugPrintBufferRefcount(res->buffer);
    4318                 :             :             elog(WARNING, "buffer refcount leak: %s", s);
    4319                 :             :             pfree(s);
    4320                 :             :             RefCountErrors++;
    4321                 :             :         }
    4322                 :             :     }
    4323                 :             : 
    4324                 :             :     Assert(RefCountErrors == 0);
    4325                 :             : #endif
    4326                 :      679389 : }
    4327                 :             : 
    4328                 :             : #ifdef USE_ASSERT_CHECKING
    4329                 :             : /*
    4330                 :             :  * Check for exclusive-locked catalog buffers.  This is the core of
    4331                 :             :  * AssertCouldGetRelation().
    4332                 :             :  *
    4333                 :             :  * A backend would self-deadlock on the content lock if the catalog scan read
    4334                 :             :  * the exclusive-locked buffer.  The main threat is exclusive-locked buffers
    4335                 :             :  * of catalogs used in relcache, because a catcache search on any catalog may
    4336                 :             :  * build that catalog's relcache entry.  We don't have an inventory of
    4337                 :             :  * catalogs relcache uses, so just check buffers of most catalogs.
    4338                 :             :  *
    4339                 :             :  * It's better to minimize waits while holding an exclusive buffer lock, so it
    4340                 :             :  * would be nice to broaden this check not to be catalog-specific.  However,
    4341                 :             :  * bttextcmp() accesses pg_collation, and non-core opclasses might similarly
    4342                 :             :  * read tables.  That is deadlock-free as long as there's no loop in the
    4343                 :             :  * dependency graph: modifying table A may cause an opclass to read table B,
    4344                 :             :  * but it must not cause a read of table A.
    4345                 :             :  */
    4346                 :             : void
    4347                 :             : AssertBufferLocksPermitCatalogRead(void)
    4348                 :             : {
    4349                 :             :     PrivateRefCountEntry *res;
    4350                 :             : 
    4351                 :             :     /* check the array */
    4352                 :             :     for (int i = 0; i < REFCOUNT_ARRAY_ENTRIES; i++)
    4353                 :             :     {
    4354                 :             :         if (PrivateRefCountArrayKeys[i] != InvalidBuffer)
    4355                 :             :         {
    4356                 :             :             res = &PrivateRefCountArray[i];
    4357                 :             : 
    4358                 :             :             if (res->buffer == InvalidBuffer)
    4359                 :             :                 continue;
    4360                 :             : 
    4361                 :             :             AssertNotCatalogBufferLock(res->buffer, res->data.lockmode);
    4362                 :             :         }
    4363                 :             :     }
    4364                 :             : 
    4365                 :             :     /* if necessary search the hash */
    4366                 :             :     if (PrivateRefCountOverflowed)
    4367                 :             :     {
    4368                 :             :         refcount_iterator iter;
    4369                 :             : 
    4370                 :             :         refcount_start_iterate(PrivateRefCountHash, &iter);
    4371                 :             :         while ((res = refcount_iterate(PrivateRefCountHash, &iter)) != NULL)
    4372                 :             :         {
    4373                 :             :             AssertNotCatalogBufferLock(res->buffer, res->data.lockmode);
    4374                 :             :         }
    4375                 :             :     }
    4376                 :             : }
    4377                 :             : 
    4378                 :             : static void
    4379                 :             : AssertNotCatalogBufferLock(Buffer buffer, BufferLockMode mode)
    4380                 :             : {
    4381                 :             :     BufferDesc *bufHdr = GetBufferDescriptor(buffer - 1);
    4382                 :             :     BufferTag   tag;
    4383                 :             :     Oid         relid;
    4384                 :             : 
    4385                 :             :     if (mode != BUFFER_LOCK_EXCLUSIVE)
    4386                 :             :         return;
    4387                 :             : 
    4388                 :             :     tag = bufHdr->tag;
    4389                 :             : 
    4390                 :             :     /*
    4391                 :             :      * This relNumber==relid assumption holds until a catalog experiences
    4392                 :             :      * VACUUM FULL or similar.  After a command like that, relNumber will be
    4393                 :             :      * in the normal (non-catalog) range, and we lose the ability to detect
    4394                 :             :      * hazardous access to that catalog.  Calling RelidByRelfilenumber() would
    4395                 :             :      * close that gap, but RelidByRelfilenumber() might then deadlock with a
    4396                 :             :      * held lock.
    4397                 :             :      */
    4398                 :             :     relid = tag.relNumber;
    4399                 :             : 
    4400                 :             :     if (IsCatalogTextUniqueIndexOid(relid)) /* see comments at the callee */
    4401                 :             :         return;
    4402                 :             : 
    4403                 :             :     Assert(!IsCatalogRelationOid(relid));
    4404                 :             : }
    4405                 :             : #endif
    4406                 :             : 
    4407                 :             : 
    4408                 :             : /*
    4409                 :             :  * Helper routine to issue warnings when a buffer is unexpectedly pinned
    4410                 :             :  */
    4411                 :             : char *
    4412                 :          46 : DebugPrintBufferRefcount(Buffer buffer)
    4413                 :             : {
    4414                 :             :     BufferDesc *buf;
    4415                 :             :     int32       loccount;
    4416                 :             :     char       *result;
    4417                 :             :     ProcNumber  backend;
    4418                 :             :     uint64      buf_state;
    4419                 :             : 
    4420                 :             :     Assert(BufferIsValid(buffer));
    4421         [ +  + ]:          46 :     if (BufferIsLocal(buffer))
    4422                 :             :     {
    4423                 :          16 :         buf = GetLocalBufferDescriptor(-buffer - 1);
    4424                 :          16 :         loccount = LocalRefCount[-buffer - 1];
    4425                 :          16 :         backend = MyProcNumber;
    4426                 :             :     }
    4427                 :             :     else
    4428                 :             :     {
    4429                 :          30 :         buf = GetBufferDescriptor(buffer - 1);
    4430                 :          30 :         loccount = GetPrivateRefCount(buffer);
    4431                 :          30 :         backend = INVALID_PROC_NUMBER;
    4432                 :             :     }
    4433                 :             : 
    4434                 :             :     /* theoretically we should lock the bufHdr here */
    4435                 :          46 :     buf_state = pg_atomic_read_u64(&buf->state);
    4436                 :             : 
    4437                 :          46 :     result = psprintf("[%03d] (rel=%s, blockNum=%u, flags=0x%" PRIx64 ", refcount=%u %d)",
    4438                 :             :                       buffer,
    4439                 :          46 :                       relpathbackend(BufTagGetRelFileLocator(&buf->tag), backend,
    4440                 :             :                                      BufTagGetForkNum(&buf->tag)).str,
    4441                 :             :                       buf->tag.blockNum, buf_state & BUF_FLAG_MASK,
    4442                 :             :                       BUF_STATE_GET_REFCOUNT(buf_state), loccount);
    4443                 :          46 :     return result;
    4444                 :             : }
    4445                 :             : 
    4446                 :             : /*
    4447                 :             :  * CheckPointBuffers
    4448                 :             :  *
    4449                 :             :  * Flush all dirty blocks in buffer pool to disk at checkpoint time.
    4450                 :             :  *
    4451                 :             :  * Note: temporary relations do not participate in checkpoints, so they don't
    4452                 :             :  * need to be flushed.
    4453                 :             :  */
    4454                 :             : void
    4455                 :        1950 : CheckPointBuffers(int flags)
    4456                 :             : {
    4457                 :        1950 :     BufferSync(flags);
    4458                 :        1950 : }
    4459                 :             : 
    4460                 :             : /*
    4461                 :             :  * BufferGetBlockNumber
    4462                 :             :  *      Returns the block number associated with a buffer.
    4463                 :             :  *
    4464                 :             :  * Note:
    4465                 :             :  *      Assumes that the buffer is valid and pinned, else the
    4466                 :             :  *      value may be obsolete immediately...
    4467                 :             :  */
    4468                 :             : BlockNumber
    4469                 :    80744448 : BufferGetBlockNumber(Buffer buffer)
    4470                 :             : {
    4471                 :             :     BufferDesc *bufHdr;
    4472                 :             : 
    4473                 :             :     Assert(BufferIsPinned(buffer));
    4474                 :             : 
    4475         [ +  + ]:    80744448 :     if (BufferIsLocal(buffer))
    4476                 :     2561323 :         bufHdr = GetLocalBufferDescriptor(-buffer - 1);
    4477                 :             :     else
    4478                 :    78183125 :         bufHdr = GetBufferDescriptor(buffer - 1);
    4479                 :             : 
    4480                 :             :     /* pinned, so OK to read tag without spinlock */
    4481                 :    80744448 :     return bufHdr->tag.blockNum;
    4482                 :             : }
    4483                 :             : 
    4484                 :             : /*
    4485                 :             :  * BufferGetTag
    4486                 :             :  *      Returns the relfilelocator, fork number and block number associated with
    4487                 :             :  *      a buffer.
    4488                 :             :  */
    4489                 :             : void
    4490                 :    27662970 : BufferGetTag(Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum,
    4491                 :             :              BlockNumber *blknum)
    4492                 :             : {
    4493                 :             :     BufferDesc *bufHdr;
    4494                 :             : 
    4495                 :             :     /* Do the same checks as BufferGetBlockNumber. */
    4496                 :             :     Assert(BufferIsPinned(buffer));
    4497                 :             : 
    4498         [ -  + ]:    27662970 :     if (BufferIsLocal(buffer))
    4499                 :           0 :         bufHdr = GetLocalBufferDescriptor(-buffer - 1);
    4500                 :             :     else
    4501                 :    27662970 :         bufHdr = GetBufferDescriptor(buffer - 1);
    4502                 :             : 
    4503                 :             :     /* pinned, so OK to read tag without spinlock */
    4504                 :    27662970 :     *rlocator = BufTagGetRelFileLocator(&bufHdr->tag);
    4505                 :    27662970 :     *forknum = BufTagGetForkNum(&bufHdr->tag);
    4506                 :    27662970 :     *blknum = bufHdr->tag.blockNum;
    4507                 :    27662970 : }
    4508                 :             : 
    4509                 :             : /*
    4510                 :             :  * FlushBuffer
    4511                 :             :  *      Physically write out a shared buffer.
    4512                 :             :  *
    4513                 :             :  * NOTE: this actually just passes the buffer contents to the kernel; the
    4514                 :             :  * real write to disk won't happen until the kernel feels like it.  This
    4515                 :             :  * is okay from our point of view since we can redo the changes from WAL.
    4516                 :             :  * However, we will need to force the changes to disk via fsync before
    4517                 :             :  * we can checkpoint WAL.
    4518                 :             :  *
    4519                 :             :  * The caller must hold a pin on the buffer and have
    4520                 :             :  * (share-)exclusively-locked the buffer contents.
    4521                 :             :  *
    4522                 :             :  * If the caller has an smgr reference for the buffer's relation, pass it
    4523                 :             :  * as the second parameter.  If not, pass NULL.
    4524                 :             :  */
    4525                 :             : static void
    4526                 :      700411 : FlushBuffer(BufferDesc *buf, SMgrRelation reln, IOObject io_object,
    4527                 :             :             IOContext io_context)
    4528                 :             : {
    4529                 :             :     XLogRecPtr  recptr;
    4530                 :             :     ErrorContextCallback errcallback;
    4531                 :             :     instr_time  io_start;
    4532                 :             :     Block       bufBlock;
    4533                 :             : 
    4534                 :             :     Assert(BufferLockHeldByMeInMode(buf, BUFFER_LOCK_EXCLUSIVE) ||
    4535                 :             :            BufferLockHeldByMeInMode(buf, BUFFER_LOCK_SHARE_EXCLUSIVE));
    4536                 :             : 
    4537                 :             :     /*
    4538                 :             :      * Try to start an I/O operation.  If StartBufferIO returns false, then
    4539                 :             :      * someone else flushed the buffer before we could, so we need not do
    4540                 :             :      * anything.
    4541                 :             :      */
    4542         [ +  + ]:      700411 :     if (StartSharedBufferIO(buf, false, true, NULL) == BUFFER_IO_ALREADY_DONE)
    4543                 :          10 :         return;
    4544                 :             : 
    4545                 :             :     /* Setup error traceback support for ereport() */
    4546                 :      700401 :     errcallback.callback = shared_buffer_write_error_callback;
    4547                 :      700401 :     errcallback.arg = buf;
    4548                 :      700401 :     errcallback.previous = error_context_stack;
    4549                 :      700401 :     error_context_stack = &errcallback;
    4550                 :             : 
    4551                 :             :     /* Find smgr relation for buffer */
    4552         [ +  + ]:      700401 :     if (reln == NULL)
    4553                 :      697802 :         reln = smgropen(BufTagGetRelFileLocator(&buf->tag), INVALID_PROC_NUMBER);
    4554                 :             : 
    4555                 :             :     TRACE_POSTGRESQL_BUFFER_FLUSH_START(BufTagGetForkNum(&buf->tag),
    4556                 :             :                                         buf->tag.blockNum,
    4557                 :             :                                         reln->smgr_rlocator.locator.spcOid,
    4558                 :             :                                         reln->smgr_rlocator.locator.dbOid,
    4559                 :             :                                         reln->smgr_rlocator.locator.relNumber);
    4560                 :             : 
    4561                 :             :     /*
    4562                 :             :      * As we hold at least a share-exclusive lock on the buffer, the LSN
    4563                 :             :      * cannot change during the flush (and thus can't be torn).
    4564                 :             :      */
    4565                 :      700401 :     recptr = BufferGetLSN(buf);
    4566                 :             : 
    4567                 :             :     /*
    4568                 :             :      * Force XLOG flush up to buffer's LSN.  This implements the basic WAL
    4569                 :             :      * rule that log updates must hit disk before any of the data-file changes
    4570                 :             :      * they describe do.
    4571                 :             :      *
    4572                 :             :      * However, this rule does not apply to unlogged relations, which will be
    4573                 :             :      * lost after a crash anyway.  Most unlogged relation pages do not bear
    4574                 :             :      * LSNs since we never emit WAL records for them, and therefore flushing
    4575                 :             :      * up through the buffer LSN would be useless, but harmless.  However,
    4576                 :             :      * some index AMs use LSNs internally to detect concurrent page
    4577                 :             :      * modifications, and therefore unlogged index pages bear "fake" LSNs
    4578                 :             :      * generated by XLogGetFakeLSN.  It is unlikely but possible that the fake
    4579                 :             :      * LSN counter could advance past the WAL insertion point; and if it did
    4580                 :             :      * happen, attempting to flush WAL through that location would fail, with
    4581                 :             :      * disastrous system-wide consequences.  To make sure that can't happen,
    4582                 :             :      * skip the flush if the buffer isn't permanent.
    4583                 :             :      */
    4584         [ +  + ]:      700401 :     if (pg_atomic_read_u64(&buf->state) & BM_PERMANENT)
    4585                 :      698513 :         XLogFlush(recptr);
    4586                 :             : 
    4587                 :             :     /*
    4588                 :             :      * Now it's safe to write the buffer to disk. Note that no one else should
    4589                 :             :      * have been able to write it, while we were busy with log flushing,
    4590                 :             :      * because we got the exclusive right to perform I/O by setting the
    4591                 :             :      * BM_IO_IN_PROGRESS bit.
    4592                 :             :      */
    4593                 :      700401 :     bufBlock = BufHdrGetBlock(buf);
    4594                 :             : 
    4595                 :             :     /* Update page checksum if desired. */
    4596                 :      700401 :     PageSetChecksum((Page) bufBlock, buf->tag.blockNum);
    4597                 :             : 
    4598                 :      700401 :     io_start = pgstat_prepare_io_time(track_io_timing);
    4599                 :             : 
    4600                 :      700401 :     smgrwrite(reln,
    4601                 :      700401 :               BufTagGetForkNum(&buf->tag),
    4602                 :             :               buf->tag.blockNum,
    4603                 :             :               bufBlock,
    4604                 :             :               false);
    4605                 :             : 
    4606                 :             :     /*
    4607                 :             :      * When a strategy is in use, only flushes of dirty buffers already in the
    4608                 :             :      * strategy ring are counted as strategy writes (IOCONTEXT
    4609                 :             :      * [BULKREAD|BULKWRITE|VACUUM] IOOP_WRITE) for the purpose of IO
    4610                 :             :      * statistics tracking.
    4611                 :             :      *
    4612                 :             :      * If a shared buffer initially added to the ring must be flushed before
    4613                 :             :      * being used, this is counted as an IOCONTEXT_NORMAL IOOP_WRITE.
    4614                 :             :      *
    4615                 :             :      * If a shared buffer which was added to the ring later because the
    4616                 :             :      * current strategy buffer is pinned or in use or because all strategy
    4617                 :             :      * buffers were dirty and rejected (for BAS_BULKREAD operations only)
    4618                 :             :      * requires flushing, this is counted as an IOCONTEXT_NORMAL IOOP_WRITE
    4619                 :             :      * (from_ring will be false).
    4620                 :             :      *
    4621                 :             :      * When a strategy is not in use, the write can only be a "regular" write
    4622                 :             :      * of a dirty shared buffer (IOCONTEXT_NORMAL IOOP_WRITE).
    4623                 :             :      */
    4624                 :      700401 :     pgstat_count_io_op_time(io_object, io_context,
    4625                 :             :                             IOOP_WRITE, io_start, 1, BLCKSZ);
    4626                 :             : 
    4627                 :      700401 :     pgBufferUsage.shared_blks_written++;
    4628                 :             : 
    4629                 :             :     /*
    4630                 :             :      * Mark the buffer as clean and end the BM_IO_IN_PROGRESS state.
    4631                 :             :      */
    4632                 :      700401 :     TerminateBufferIO(buf, true, 0, true, false);
    4633                 :             : 
    4634                 :             :     TRACE_POSTGRESQL_BUFFER_FLUSH_DONE(BufTagGetForkNum(&buf->tag),
    4635                 :             :                                        buf->tag.blockNum,
    4636                 :             :                                        reln->smgr_rlocator.locator.spcOid,
    4637                 :             :                                        reln->smgr_rlocator.locator.dbOid,
    4638                 :             :                                        reln->smgr_rlocator.locator.relNumber);
    4639                 :             : 
    4640                 :             :     /* Pop the error context stack */
    4641                 :      700401 :     error_context_stack = errcallback.previous;
    4642                 :             : }
    4643                 :             : 
    4644                 :             : /*
    4645                 :             :  * Convenience wrapper around FlushBuffer() that locks/unlocks the buffer
    4646                 :             :  * before/after calling FlushBuffer().
    4647                 :             :  */
    4648                 :             : static void
    4649                 :      344339 : FlushUnlockedBuffer(BufferDesc *buf, SMgrRelation reln,
    4650                 :             :                     IOObject io_object, IOContext io_context)
    4651                 :             : {
    4652                 :      344339 :     Buffer      buffer = BufferDescriptorGetBuffer(buf);
    4653                 :             : 
    4654                 :      344339 :     BufferLockAcquire(buffer, buf, BUFFER_LOCK_SHARE_EXCLUSIVE);
    4655                 :      344339 :     FlushBuffer(buf, reln, io_object, io_context);
    4656                 :      344339 :     BufferLockUnlock(buffer, buf);
    4657                 :      344339 : }
    4658                 :             : 
    4659                 :             : /*
    4660                 :             :  * RelationGetNumberOfBlocksInFork
    4661                 :             :  *      Determines the current number of pages in the specified relation fork.
    4662                 :             :  *
    4663                 :             :  * Note that the accuracy of the result will depend on the details of the
    4664                 :             :  * relation's storage. For builtin AMs it'll be accurate, but for external AMs
    4665                 :             :  * it might not be.
    4666                 :             :  */
    4667                 :             : BlockNumber
    4668                 :     2381931 : RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
    4669                 :             : {
    4670   [ +  +  +  +  :     2381931 :     if (RELKIND_HAS_TABLE_AM(relation->rd_rel->relkind))
                   +  + ]
    4671                 :             :     {
    4672                 :             :         /*
    4673                 :             :          * Not every table AM uses BLCKSZ wide fixed size blocks. Therefore
    4674                 :             :          * tableam returns the size in bytes - but for the purpose of this
    4675                 :             :          * routine, we want the number of blocks. Therefore divide, rounding
    4676                 :             :          * up.
    4677                 :             :          */
    4678                 :             :         uint64      szbytes;
    4679                 :             : 
    4680                 :     1703814 :         szbytes = table_relation_size(relation, forkNum);
    4681                 :             : 
    4682                 :     1703795 :         return (szbytes + (BLCKSZ - 1)) / BLCKSZ;
    4683                 :             :     }
    4684   [ +  -  +  +  :      678117 :     else if (RELKIND_HAS_STORAGE(relation->rd_rel->relkind))
          -  +  -  -  -  
                      - ]
    4685                 :             :     {
    4686                 :      678117 :         return smgrnblocks(RelationGetSmgr(relation), forkNum);
    4687                 :             :     }
    4688                 :             :     else
    4689                 :             :         Assert(false);
    4690                 :             : 
    4691                 :           0 :     return 0;                   /* keep compiler quiet */
    4692                 :             : }
    4693                 :             : 
    4694                 :             : /*
    4695                 :             :  * BufferIsPermanent
    4696                 :             :  *      Determines whether a buffer will potentially still be around after
    4697                 :             :  *      a crash.  Caller must hold a buffer pin.
    4698                 :             :  */
    4699                 :             : bool
    4700                 :    17305037 : BufferIsPermanent(Buffer buffer)
    4701                 :             : {
    4702                 :             :     BufferDesc *bufHdr;
    4703                 :             : 
    4704                 :             :     /* Local buffers are used only for temp relations. */
    4705         [ +  + ]:    17305037 :     if (BufferIsLocal(buffer))
    4706                 :      828431 :         return false;
    4707                 :             : 
    4708                 :             :     /* Make sure we've got a real buffer, and that we hold a pin on it. */
    4709                 :             :     Assert(BufferIsValid(buffer));
    4710                 :             :     Assert(BufferIsPinned(buffer));
    4711                 :             : 
    4712                 :             :     /*
    4713                 :             :      * BM_PERMANENT can't be changed while we hold a pin on the buffer, so we
    4714                 :             :      * need not bother with the buffer header spinlock.  Even if someone else
    4715                 :             :      * changes the buffer header state while we're doing this, the state is
    4716                 :             :      * changed atomically, so we'll read the old value or the new value, but
    4717                 :             :      * not random garbage.
    4718                 :             :      */
    4719                 :    16476606 :     bufHdr = GetBufferDescriptor(buffer - 1);
    4720                 :    16476606 :     return (pg_atomic_read_u64(&bufHdr->state) & BM_PERMANENT) != 0;
    4721                 :             : }
    4722                 :             : 
    4723                 :             : /*
    4724                 :             :  * BufferGetLSNAtomic
    4725                 :             :  *      Retrieves the LSN of the buffer atomically.
    4726                 :             :  *
    4727                 :             :  * This is necessary for some callers who may only hold a share lock on
    4728                 :             :  * the buffer. A share lock allows a concurrent backend to set hint bits
    4729                 :             :  * on the page, which in turn may require a WAL record to be emitted.
    4730                 :             :  *
    4731                 :             :  * On platforms with 8 byte atomic reads/writes, we don't need to do any
    4732                 :             :  * additional locking. On platforms not supporting such 8 byte atomic
    4733                 :             :  * reads/writes, we need to actually take the header lock.
    4734                 :             :  */
    4735                 :             : XLogRecPtr
    4736                 :     8930619 : BufferGetLSNAtomic(Buffer buffer)
    4737                 :             : {
    4738                 :             :     /* Make sure we've got a real buffer, and that we hold a pin on it. */
    4739                 :             :     Assert(BufferIsValid(buffer));
    4740                 :             :     Assert(BufferIsPinned(buffer));
    4741                 :             : 
    4742                 :             : #ifdef PG_HAVE_8BYTE_SINGLE_COPY_ATOMICITY
    4743                 :     8930619 :     return PageGetLSN(BufferGetPage(buffer));
    4744                 :             : #else
    4745                 :             :     {
    4746                 :             :         char       *page = BufferGetPage(buffer);
    4747                 :             :         BufferDesc *bufHdr;
    4748                 :             :         XLogRecPtr  lsn;
    4749                 :             : 
    4750                 :             :         /*
    4751                 :             :          * If we don't need locking for correctness, fastpath out.
    4752                 :             :          */
    4753                 :             :         if (!XLogHintBitIsNeeded() || BufferIsLocal(buffer))
    4754                 :             :             return PageGetLSN(page);
    4755                 :             : 
    4756                 :             :         bufHdr = GetBufferDescriptor(buffer - 1);
    4757                 :             :         LockBufHdr(bufHdr);
    4758                 :             :         lsn = PageGetLSN(page);
    4759                 :             :         UnlockBufHdr(bufHdr);
    4760                 :             : 
    4761                 :             :         return lsn;
    4762                 :             :     }
    4763                 :             : #endif
    4764                 :             : }
    4765                 :             : 
    4766                 :             : /* ---------------------------------------------------------------------
    4767                 :             :  *      DropRelationBuffers
    4768                 :             :  *
    4769                 :             :  *      This function removes from the buffer pool all the pages of the
    4770                 :             :  *      specified relation forks that have block numbers >= firstDelBlock.
    4771                 :             :  *      (In particular, with firstDelBlock = 0, all pages are removed.)
    4772                 :             :  *      Dirty pages are simply dropped, without bothering to write them
    4773                 :             :  *      out first.  Therefore, this is NOT rollback-able, and so should be
    4774                 :             :  *      used only with extreme caution!
    4775                 :             :  *
    4776                 :             :  *      Currently, this is called only from smgr.c when the underlying file
    4777                 :             :  *      is about to be deleted or truncated (firstDelBlock is needed for
    4778                 :             :  *      the truncation case).  The data in the affected pages would therefore
    4779                 :             :  *      be deleted momentarily anyway, and there is no point in writing it.
    4780                 :             :  *      It is the responsibility of higher-level code to ensure that the
    4781                 :             :  *      deletion or truncation does not lose any data that could be needed
    4782                 :             :  *      later.  It is also the responsibility of higher-level code to ensure
    4783                 :             :  *      that no other process could be trying to load more pages of the
    4784                 :             :  *      relation into buffers.
    4785                 :             :  * --------------------------------------------------------------------
    4786                 :             :  */
    4787                 :             : void
    4788                 :         788 : DropRelationBuffers(SMgrRelation smgr_reln, ForkNumber *forkNum,
    4789                 :             :                     int nforks, BlockNumber *firstDelBlock)
    4790                 :             : {
    4791                 :             :     int         i;
    4792                 :             :     int         j;
    4793                 :             :     RelFileLocatorBackend rlocator;
    4794                 :             :     BlockNumber nForkBlock[MAX_FORKNUM];
    4795                 :         788 :     uint64      nBlocksToInvalidate = 0;
    4796                 :             : 
    4797                 :         788 :     rlocator = smgr_reln->smgr_rlocator;
    4798                 :             : 
    4799                 :             :     /* If it's a local relation, it's localbuf.c's problem. */
    4800         [ +  + ]:         788 :     if (RelFileLocatorBackendIsTemp(rlocator))
    4801                 :             :     {
    4802         [ +  - ]:         498 :         if (rlocator.backend == MyProcNumber)
    4803                 :         498 :             DropRelationLocalBuffers(rlocator.locator, forkNum, nforks,
    4804                 :             :                                      firstDelBlock);
    4805                 :             : 
    4806                 :         537 :         return;
    4807                 :             :     }
    4808                 :             : 
    4809                 :             :     /*
    4810                 :             :      * To remove all the pages of the specified relation forks from the buffer
    4811                 :             :      * pool, we need to scan the entire buffer pool but we can optimize it by
    4812                 :             :      * finding the buffers from BufMapping table provided we know the exact
    4813                 :             :      * size of each fork of the relation. The exact size is required to ensure
    4814                 :             :      * that we don't leave any buffer for the relation being dropped as
    4815                 :             :      * otherwise the background writer or checkpointer can lead to a PANIC
    4816                 :             :      * error while flushing buffers corresponding to files that don't exist.
    4817                 :             :      *
    4818                 :             :      * To know the exact size, we rely on the size cached for each fork by us
    4819                 :             :      * during recovery which limits the optimization to recovery and on
    4820                 :             :      * standbys but we can easily extend it once we have shared cache for
    4821                 :             :      * relation size.
    4822                 :             :      *
    4823                 :             :      * In recovery, we cache the value returned by the first lseek(SEEK_END)
    4824                 :             :      * and the future writes keeps the cached value up-to-date. See
    4825                 :             :      * smgrextend. It is possible that the value of the first lseek is smaller
    4826                 :             :      * than the actual number of existing blocks in the file due to buggy
    4827                 :             :      * Linux kernels that might not have accounted for the recent write. But
    4828                 :             :      * that should be fine because there must not be any buffers after that
    4829                 :             :      * file size.
    4830                 :             :      */
    4831         [ +  + ]:         390 :     for (i = 0; i < nforks; i++)
    4832                 :             :     {
    4833                 :             :         /* Get the number of blocks for a relation's fork */
    4834                 :         338 :         nForkBlock[i] = smgrnblocks_cached(smgr_reln, forkNum[i]);
    4835                 :             : 
    4836         [ +  + ]:         338 :         if (nForkBlock[i] == InvalidBlockNumber)
    4837                 :             :         {
    4838                 :         238 :             nBlocksToInvalidate = InvalidBlockNumber;
    4839                 :         238 :             break;
    4840                 :             :         }
    4841                 :             : 
    4842                 :             :         /* calculate the number of blocks to be invalidated */
    4843                 :         100 :         nBlocksToInvalidate += (nForkBlock[i] - firstDelBlock[i]);
    4844                 :             :     }
    4845                 :             : 
    4846                 :             :     /*
    4847                 :             :      * We apply the optimization iff the total number of blocks to invalidate
    4848                 :             :      * is below the BUF_DROP_FULL_SCAN_THRESHOLD.
    4849                 :             :      */
    4850         [ +  + ]:         290 :     if (BlockNumberIsValid(nBlocksToInvalidate) &&
    4851         [ +  + ]:          52 :         nBlocksToInvalidate < BUF_DROP_FULL_SCAN_THRESHOLD)
    4852                 :             :     {
    4853         [ +  + ]:         107 :         for (j = 0; j < nforks; j++)
    4854                 :          68 :             FindAndDropRelationBuffers(rlocator.locator, forkNum[j],
    4855                 :          68 :                                        nForkBlock[j], firstDelBlock[j]);
    4856                 :          39 :         return;
    4857                 :             :     }
    4858                 :             : 
    4859         [ +  + ]:     3348603 :     for (i = 0; i < NBuffers; i++)
    4860                 :             :     {
    4861                 :     3348352 :         BufferDesc *bufHdr = GetBufferDescriptor(i);
    4862                 :             : 
    4863                 :             :         /*
    4864                 :             :          * We can make this a tad faster by prechecking the buffer tag before
    4865                 :             :          * we attempt to lock the buffer; this saves a lot of lock
    4866                 :             :          * acquisitions in typical cases.  It should be safe because the
    4867                 :             :          * caller must have AccessExclusiveLock on the relation, or some other
    4868                 :             :          * reason to be certain that no one is loading new pages of the rel
    4869                 :             :          * into the buffer pool.  (Otherwise we might well miss such pages
    4870                 :             :          * entirely.)  Therefore, while the tag might be changing while we
    4871                 :             :          * look at it, it can't be changing *to* a value we care about, only
    4872                 :             :          * *away* from such a value.  So false negatives are impossible, and
    4873                 :             :          * false positives are safe because we'll recheck after getting the
    4874                 :             :          * buffer lock.
    4875                 :             :          *
    4876                 :             :          * We could check forkNum and blockNum as well as the rlocator, but
    4877                 :             :          * the incremental win from doing so seems small.
    4878                 :             :          */
    4879         [ +  + ]:     3348352 :         if (!BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator.locator))
    4880                 :     3339999 :             continue;
    4881                 :             : 
    4882                 :        8353 :         LockBufHdr(bufHdr);
    4883                 :             : 
    4884         [ +  + ]:       21151 :         for (j = 0; j < nforks; j++)
    4885                 :             :         {
    4886         [ +  - ]:       14926 :             if (BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator.locator) &&
    4887         [ +  + ]:       14926 :                 BufTagGetForkNum(&bufHdr->tag) == forkNum[j] &&
    4888         [ +  + ]:        8255 :                 bufHdr->tag.blockNum >= firstDelBlock[j])
    4889                 :             :             {
    4890                 :        2128 :                 InvalidateBuffer(bufHdr);   /* releases spinlock */
    4891                 :        2128 :                 break;
    4892                 :             :             }
    4893                 :             :         }
    4894         [ +  + ]:        8353 :         if (j >= nforks)
    4895                 :        6225 :             UnlockBufHdr(bufHdr);
    4896                 :             :     }
    4897                 :             : }
    4898                 :             : 
    4899                 :             : /* ---------------------------------------------------------------------
    4900                 :             :  *      DropRelationsAllBuffers
    4901                 :             :  *
    4902                 :             :  *      This function removes from the buffer pool all the pages of all
    4903                 :             :  *      forks of the specified relations.  It's equivalent to calling
    4904                 :             :  *      DropRelationBuffers once per fork per relation with firstDelBlock = 0.
    4905                 :             :  *      --------------------------------------------------------------------
    4906                 :             :  */
    4907                 :             : void
    4908                 :       18297 : DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators)
    4909                 :             : {
    4910                 :             :     int         i;
    4911                 :       18297 :     int         n = 0;
    4912                 :             :     SMgrRelation *rels;
    4913                 :             :     BlockNumber (*block)[MAX_FORKNUM + 1];
    4914                 :       18297 :     uint64      nBlocksToInvalidate = 0;
    4915                 :             :     RelFileLocator *locators;
    4916                 :       18297 :     bool        cached = true;
    4917                 :             :     bool        use_bsearch;
    4918                 :             : 
    4919         [ -  + ]:       18297 :     if (nlocators == 0)
    4920                 :           0 :         return;
    4921                 :             : 
    4922                 :       18297 :     rels = palloc_array(SMgrRelation, nlocators);   /* non-local relations */
    4923                 :             : 
    4924                 :             :     /* If it's a local relation, it's localbuf.c's problem. */
    4925         [ +  + ]:       80304 :     for (i = 0; i < nlocators; i++)
    4926                 :             :     {
    4927         [ +  + ]:       62007 :         if (RelFileLocatorBackendIsTemp(smgr_reln[i]->smgr_rlocator))
    4928                 :             :         {
    4929         [ +  + ]:        4475 :             if (smgr_reln[i]->smgr_rlocator.backend == MyProcNumber)
    4930                 :        4473 :                 DropRelationAllLocalBuffers(smgr_reln[i]->smgr_rlocator.locator);
    4931                 :             :         }
    4932                 :             :         else
    4933                 :       57532 :             rels[n++] = smgr_reln[i];
    4934                 :             :     }
    4935                 :             : 
    4936                 :             :     /*
    4937                 :             :      * If there are no non-local relations, then we're done. Release the
    4938                 :             :      * memory and return.
    4939                 :             :      */
    4940         [ +  + ]:       18297 :     if (n == 0)
    4941                 :             :     {
    4942                 :        1197 :         pfree(rels);
    4943                 :        1197 :         return;
    4944                 :             :     }
    4945                 :             : 
    4946                 :             :     /*
    4947                 :             :      * This is used to remember the number of blocks for all the relations
    4948                 :             :      * forks.
    4949                 :             :      */
    4950                 :             :     block = (BlockNumber (*)[MAX_FORKNUM + 1])
    4951                 :       17100 :         palloc(sizeof(BlockNumber) * n * (MAX_FORKNUM + 1));
    4952                 :             : 
    4953                 :             :     /*
    4954                 :             :      * We can avoid scanning the entire buffer pool if we know the exact size
    4955                 :             :      * of each of the given relation forks. See DropRelationBuffers.
    4956                 :             :      */
    4957   [ +  +  +  + ]:       35647 :     for (i = 0; i < n && cached; i++)
    4958                 :             :     {
    4959         [ +  + ]:       27878 :         for (int j = 0; j <= MAX_FORKNUM; j++)
    4960                 :             :         {
    4961                 :             :             /* Get the number of blocks for a relation's fork. */
    4962                 :       25563 :             block[i][j] = smgrnblocks_cached(rels[i], j);
    4963                 :             : 
    4964                 :             :             /* We need to only consider the relation forks that exists. */
    4965         [ +  + ]:       25563 :             if (block[i][j] == InvalidBlockNumber)
    4966                 :             :             {
    4967         [ +  + ]:       23041 :                 if (!smgrexists(rels[i], j))
    4968                 :        6809 :                     continue;
    4969                 :       16232 :                 cached = false;
    4970                 :       16232 :                 break;
    4971                 :             :             }
    4972                 :             : 
    4973                 :             :             /* calculate the total number of blocks to be invalidated */
    4974                 :        2522 :             nBlocksToInvalidate += block[i][j];
    4975                 :             :         }
    4976                 :             :     }
    4977                 :             : 
    4978                 :             :     /*
    4979                 :             :      * We apply the optimization iff the total number of blocks to invalidate
    4980                 :             :      * is below the BUF_DROP_FULL_SCAN_THRESHOLD.
    4981                 :             :      */
    4982   [ +  +  +  + ]:       17100 :     if (cached && nBlocksToInvalidate < BUF_DROP_FULL_SCAN_THRESHOLD)
    4983                 :             :     {
    4984         [ +  + ]:        1447 :         for (i = 0; i < n; i++)
    4985                 :             :         {
    4986         [ +  + ]:        4005 :             for (int j = 0; j <= MAX_FORKNUM; j++)
    4987                 :             :             {
    4988                 :             :                 /* ignore relation forks that doesn't exist */
    4989         [ +  + ]:        3204 :                 if (!BlockNumberIsValid(block[i][j]))
    4990                 :        2392 :                     continue;
    4991                 :             : 
    4992                 :             :                 /* drop all the buffers for a particular relation fork */
    4993                 :         812 :                 FindAndDropRelationBuffers(rels[i]->smgr_rlocator.locator,
    4994                 :         812 :                                            j, block[i][j], 0);
    4995                 :             :             }
    4996                 :             :         }
    4997                 :             : 
    4998                 :         646 :         pfree(block);
    4999                 :         646 :         pfree(rels);
    5000                 :         646 :         return;
    5001                 :             :     }
    5002                 :             : 
    5003                 :       16454 :     pfree(block);
    5004                 :       16454 :     locators = palloc_array(RelFileLocator, n); /* non-local relations */
    5005         [ +  + ]:       73185 :     for (i = 0; i < n; i++)
    5006                 :       56731 :         locators[i] = rels[i]->smgr_rlocator.locator;
    5007                 :             : 
    5008                 :             :     /*
    5009                 :             :      * For low number of relations to drop just use a simple walk through, to
    5010                 :             :      * save the bsearch overhead. The threshold to use is rather a guess than
    5011                 :             :      * an exactly determined value, as it depends on many factors (CPU and RAM
    5012                 :             :      * speeds, amount of shared buffers etc.).
    5013                 :             :      */
    5014                 :       16454 :     use_bsearch = n > RELS_BSEARCH_THRESHOLD;
    5015                 :             : 
    5016                 :             :     /* sort the list of rlocators if necessary */
    5017         [ +  + ]:       16454 :     if (use_bsearch)
    5018                 :         218 :         qsort(locators, n, sizeof(RelFileLocator), rlocator_comparator);
    5019                 :             : 
    5020         [ +  + ]:   192008902 :     for (i = 0; i < NBuffers; i++)
    5021                 :             :     {
    5022                 :   191992448 :         RelFileLocator *rlocator = NULL;
    5023                 :   191992448 :         BufferDesc *bufHdr = GetBufferDescriptor(i);
    5024                 :             : 
    5025                 :             :         /*
    5026                 :             :          * As in DropRelationBuffers, an unlocked precheck should be safe and
    5027                 :             :          * saves some cycles.
    5028                 :             :          */
    5029                 :             : 
    5030         [ +  + ]:   191992448 :         if (!use_bsearch)
    5031                 :             :         {
    5032                 :             :             int         j;
    5033                 :             : 
    5034         [ +  + ]:   763598028 :             for (j = 0; j < n; j++)
    5035                 :             :             {
    5036         [ +  + ]:   574210967 :                 if (BufTagMatchesRelFileLocator(&bufHdr->tag, &locators[j]))
    5037                 :             :                 {
    5038                 :      106571 :                     rlocator = &locators[j];
    5039                 :      106571 :                     break;
    5040                 :             :                 }
    5041                 :             :             }
    5042                 :             :         }
    5043                 :             :         else
    5044                 :             :         {
    5045                 :             :             RelFileLocator locator;
    5046                 :             : 
    5047                 :     2498816 :             locator = BufTagGetRelFileLocator(&bufHdr->tag);
    5048                 :     2498816 :             rlocator = bsearch(&locator,
    5049                 :             :                                locators, n, sizeof(RelFileLocator),
    5050                 :             :                                rlocator_comparator);
    5051                 :             :         }
    5052                 :             : 
    5053                 :             :         /* buffer doesn't belong to any of the given relfilelocators; skip it */
    5054         [ +  + ]:   191992448 :         if (rlocator == NULL)
    5055                 :   191884059 :             continue;
    5056                 :             : 
    5057                 :      108389 :         LockBufHdr(bufHdr);
    5058         [ +  - ]:      108389 :         if (BufTagMatchesRelFileLocator(&bufHdr->tag, rlocator))
    5059                 :      108389 :             InvalidateBuffer(bufHdr);   /* releases spinlock */
    5060                 :             :         else
    5061                 :           0 :             UnlockBufHdr(bufHdr);
    5062                 :             :     }
    5063                 :             : 
    5064                 :       16454 :     pfree(locators);
    5065                 :       16454 :     pfree(rels);
    5066                 :             : }
    5067                 :             : 
    5068                 :             : /* ---------------------------------------------------------------------
    5069                 :             :  *      FindAndDropRelationBuffers
    5070                 :             :  *
    5071                 :             :  *      This function performs look up in BufMapping table and removes from the
    5072                 :             :  *      buffer pool all the pages of the specified relation fork that has block
    5073                 :             :  *      number >= firstDelBlock. (In particular, with firstDelBlock = 0, all
    5074                 :             :  *      pages are removed.)
    5075                 :             :  * --------------------------------------------------------------------
    5076                 :             :  */
    5077                 :             : static void
    5078                 :         880 : FindAndDropRelationBuffers(RelFileLocator rlocator, ForkNumber forkNum,
    5079                 :             :                            BlockNumber nForkBlock,
    5080                 :             :                            BlockNumber firstDelBlock)
    5081                 :             : {
    5082                 :             :     BlockNumber curBlock;
    5083                 :             : 
    5084         [ +  + ]:        2111 :     for (curBlock = firstDelBlock; curBlock < nForkBlock; curBlock++)
    5085                 :             :     {
    5086                 :             :         uint32      bufHash;    /* hash value for tag */
    5087                 :             :         BufferTag   bufTag;     /* identity of requested block */
    5088                 :             :         LWLock     *bufPartitionLock;   /* buffer partition lock for it */
    5089                 :             :         int         buf_id;
    5090                 :             :         BufferDesc *bufHdr;
    5091                 :             : 
    5092                 :             :         /* create a tag so we can lookup the buffer */
    5093                 :        1231 :         InitBufferTag(&bufTag, &rlocator, forkNum, curBlock);
    5094                 :             : 
    5095                 :             :         /* determine its hash code and partition lock ID */
    5096                 :        1231 :         bufHash = BufTableHashCode(&bufTag);
    5097                 :        1231 :         bufPartitionLock = BufMappingPartitionLock(bufHash);
    5098                 :             : 
    5099                 :             :         /* Check that it is in the buffer pool. If not, do nothing. */
    5100                 :        1231 :         LWLockAcquire(bufPartitionLock, LW_SHARED);
    5101                 :        1231 :         buf_id = BufTableLookup(&bufTag, bufHash);
    5102                 :        1231 :         LWLockRelease(bufPartitionLock);
    5103                 :             : 
    5104         [ +  + ]:        1231 :         if (buf_id < 0)
    5105                 :         133 :             continue;
    5106                 :             : 
    5107                 :        1098 :         bufHdr = GetBufferDescriptor(buf_id);
    5108                 :             : 
    5109                 :             :         /*
    5110                 :             :          * We need to lock the buffer header and recheck if the buffer is
    5111                 :             :          * still associated with the same block because the buffer could be
    5112                 :             :          * evicted by some other backend loading blocks for a different
    5113                 :             :          * relation after we release lock on the BufMapping table.
    5114                 :             :          */
    5115                 :        1098 :         LockBufHdr(bufHdr);
    5116                 :             : 
    5117   [ +  -  +  - ]:        2196 :         if (BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator) &&
    5118                 :        1098 :             BufTagGetForkNum(&bufHdr->tag) == forkNum &&
    5119         [ +  - ]:        1098 :             bufHdr->tag.blockNum >= firstDelBlock)
    5120                 :        1098 :             InvalidateBuffer(bufHdr);   /* releases spinlock */
    5121                 :             :         else
    5122                 :           0 :             UnlockBufHdr(bufHdr);
    5123                 :             :     }
    5124                 :         880 : }
    5125                 :             : 
    5126                 :             : /* ---------------------------------------------------------------------
    5127                 :             :  *      DropDatabaseBuffers
    5128                 :             :  *
    5129                 :             :  *      This function removes all the buffers in the buffer cache for a
    5130                 :             :  *      particular database.  Dirty pages are simply dropped, without
    5131                 :             :  *      bothering to write them out first.  This is used when we destroy a
    5132                 :             :  *      database, to avoid trying to flush data to disk when the directory
    5133                 :             :  *      tree no longer exists.  Implementation is pretty similar to
    5134                 :             :  *      DropRelationBuffers() which is for destroying just one relation.
    5135                 :             :  * --------------------------------------------------------------------
    5136                 :             :  */
    5137                 :             : void
    5138                 :          84 : DropDatabaseBuffers(Oid dbid)
    5139                 :             : {
    5140                 :             :     int         i;
    5141                 :             : 
    5142                 :             :     /*
    5143                 :             :      * We needn't consider local buffers, since by assumption the target
    5144                 :             :      * database isn't our own.
    5145                 :             :      */
    5146                 :             : 
    5147         [ +  + ]:      644820 :     for (i = 0; i < NBuffers; i++)
    5148                 :             :     {
    5149                 :      644736 :         BufferDesc *bufHdr = GetBufferDescriptor(i);
    5150                 :             : 
    5151                 :             :         /*
    5152                 :             :          * As in DropRelationBuffers, an unlocked precheck should be safe and
    5153                 :             :          * saves some cycles.
    5154                 :             :          */
    5155         [ +  + ]:      644736 :         if (bufHdr->tag.dbOid != dbid)
    5156                 :      628908 :             continue;
    5157                 :             : 
    5158                 :       15828 :         LockBufHdr(bufHdr);
    5159         [ +  - ]:       15828 :         if (bufHdr->tag.dbOid == dbid)
    5160                 :       15828 :             InvalidateBuffer(bufHdr);   /* releases spinlock */
    5161                 :             :         else
    5162                 :           0 :             UnlockBufHdr(bufHdr);
    5163                 :             :     }
    5164                 :          84 : }
    5165                 :             : 
    5166                 :             : /* ---------------------------------------------------------------------
    5167                 :             :  *      FlushRelationBuffers
    5168                 :             :  *
    5169                 :             :  *      This function writes all dirty pages of a relation out to disk
    5170                 :             :  *      (or more accurately, out to kernel disk buffers), ensuring that the
    5171                 :             :  *      kernel has an up-to-date view of the relation.
    5172                 :             :  *
    5173                 :             :  *      Generally, the caller should be holding AccessExclusiveLock on the
    5174                 :             :  *      target relation to ensure that no other backend is busy dirtying
    5175                 :             :  *      more blocks of the relation; the effects can't be expected to last
    5176                 :             :  *      after the lock is released.
    5177                 :             :  *
    5178                 :             :  *      XXX currently it sequentially searches the buffer pool, should be
    5179                 :             :  *      changed to more clever ways of searching.  This routine is not
    5180                 :             :  *      used in any performance-critical code paths, so it's not worth
    5181                 :             :  *      adding additional overhead to normal paths to make it go faster.
    5182                 :             :  * --------------------------------------------------------------------
    5183                 :             :  */
    5184                 :             : void
    5185                 :         186 : FlushRelationBuffers(Relation rel)
    5186                 :             : {
    5187                 :             :     int         i;
    5188                 :             :     BufferDesc *bufHdr;
    5189                 :         186 :     SMgrRelation srel = RelationGetSmgr(rel);
    5190                 :             : 
    5191         [ +  + ]:         186 :     if (RelationUsesLocalBuffers(rel))
    5192                 :             :     {
    5193         [ +  + ]:        1212 :         for (i = 0; i < NLocBuffer; i++)
    5194                 :             :         {
    5195                 :             :             uint64      buf_state;
    5196                 :             : 
    5197                 :        1200 :             bufHdr = GetLocalBufferDescriptor(i);
    5198         [ +  + ]:        1200 :             if (BufTagMatchesRelFileLocator(&bufHdr->tag, &rel->rd_locator) &&
    5199         [ +  + ]:         400 :                 ((buf_state = pg_atomic_read_u64(&bufHdr->state)) &
    5200                 :             :                  (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
    5201                 :             :             {
    5202                 :             :                 ErrorContextCallback errcallback;
    5203                 :             : 
    5204                 :             :                 /* Setup error traceback support for ereport() */
    5205                 :         392 :                 errcallback.callback = local_buffer_write_error_callback;
    5206                 :         392 :                 errcallback.arg = bufHdr;
    5207                 :         392 :                 errcallback.previous = error_context_stack;
    5208                 :         392 :                 error_context_stack = &errcallback;
    5209                 :             : 
    5210                 :             :                 /* Make sure we can handle the pin */
    5211                 :         392 :                 ReservePrivateRefCountEntry();
    5212                 :         392 :                 ResourceOwnerEnlarge(CurrentResourceOwner);
    5213                 :             : 
    5214                 :             :                 /*
    5215                 :             :                  * Pin/unpin mostly to make valgrind work, but it also seems
    5216                 :             :                  * like the right thing to do.
    5217                 :             :                  */
    5218                 :         392 :                 PinLocalBuffer(bufHdr, false);
    5219                 :             : 
    5220                 :             : 
    5221                 :         392 :                 FlushLocalBuffer(bufHdr, srel);
    5222                 :             : 
    5223                 :         392 :                 UnpinLocalBuffer(BufferDescriptorGetBuffer(bufHdr));
    5224                 :             : 
    5225                 :             :                 /* Pop the error context stack */
    5226                 :         392 :                 error_context_stack = errcallback.previous;
    5227                 :             :             }
    5228                 :             :         }
    5229                 :             : 
    5230                 :          12 :         return;
    5231                 :             :     }
    5232                 :             : 
    5233         [ +  + ]:     2021934 :     for (i = 0; i < NBuffers; i++)
    5234                 :             :     {
    5235                 :             :         uint64      buf_state;
    5236                 :             : 
    5237                 :     2021760 :         bufHdr = GetBufferDescriptor(i);
    5238                 :             : 
    5239                 :             :         /*
    5240                 :             :          * As in DropRelationBuffers, an unlocked precheck should be safe and
    5241                 :             :          * saves some cycles.
    5242                 :             :          */
    5243         [ +  + ]:     2021760 :         if (!BufTagMatchesRelFileLocator(&bufHdr->tag, &rel->rd_locator))
    5244                 :     2021504 :             continue;
    5245                 :             : 
    5246                 :             :         /* Make sure we can handle the pin */
    5247                 :         256 :         ReservePrivateRefCountEntry();
    5248                 :         256 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    5249                 :             : 
    5250                 :         256 :         buf_state = LockBufHdr(bufHdr);
    5251         [ +  - ]:         256 :         if (BufTagMatchesRelFileLocator(&bufHdr->tag, &rel->rd_locator) &&
    5252         [ +  + ]:         256 :             (buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
    5253                 :             :         {
    5254                 :         208 :             PinBuffer_Locked(bufHdr);
    5255                 :         208 :             FlushUnlockedBuffer(bufHdr, srel, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    5256                 :         208 :             UnpinBuffer(bufHdr);
    5257                 :             :         }
    5258                 :             :         else
    5259                 :          48 :             UnlockBufHdr(bufHdr);
    5260                 :             :     }
    5261                 :             : }
    5262                 :             : 
    5263                 :             : /* ---------------------------------------------------------------------
    5264                 :             :  *      FlushRelationsAllBuffers
    5265                 :             :  *
    5266                 :             :  *      This function flushes out of the buffer pool all the pages of all
    5267                 :             :  *      forks of the specified smgr relations.  It's equivalent to calling
    5268                 :             :  *      FlushRelationBuffers once per relation.  The relations are assumed not
    5269                 :             :  *      to use local buffers.
    5270                 :             :  * --------------------------------------------------------------------
    5271                 :             :  */
    5272                 :             : void
    5273                 :          10 : FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels)
    5274                 :             : {
    5275                 :             :     int         i;
    5276                 :             :     SMgrSortArray *srels;
    5277                 :             :     bool        use_bsearch;
    5278                 :             : 
    5279         [ -  + ]:          10 :     if (nrels == 0)
    5280                 :           0 :         return;
    5281                 :             : 
    5282                 :             :     /* fill-in array for qsort */
    5283                 :          10 :     srels = palloc_array(SMgrSortArray, nrels);
    5284                 :             : 
    5285         [ +  + ]:          31 :     for (i = 0; i < nrels; i++)
    5286                 :             :     {
    5287                 :             :         Assert(!RelFileLocatorBackendIsTemp(smgrs[i]->smgr_rlocator));
    5288                 :             : 
    5289                 :          21 :         srels[i].rlocator = smgrs[i]->smgr_rlocator.locator;
    5290                 :          21 :         srels[i].srel = smgrs[i];
    5291                 :             :     }
    5292                 :             : 
    5293                 :             :     /*
    5294                 :             :      * Save the bsearch overhead for low number of relations to sync. See
    5295                 :             :      * DropRelationsAllBuffers for details.
    5296                 :             :      */
    5297                 :          10 :     use_bsearch = nrels > RELS_BSEARCH_THRESHOLD;
    5298                 :             : 
    5299                 :             :     /* sort the list of SMgrRelations if necessary */
    5300         [ -  + ]:          10 :     if (use_bsearch)
    5301                 :           0 :         qsort(srels, nrels, sizeof(SMgrSortArray), rlocator_comparator);
    5302                 :             : 
    5303         [ +  + ]:      163850 :     for (i = 0; i < NBuffers; i++)
    5304                 :             :     {
    5305                 :      163840 :         SMgrSortArray *srelent = NULL;
    5306                 :      163840 :         BufferDesc *bufHdr = GetBufferDescriptor(i);
    5307                 :             :         uint64      buf_state;
    5308                 :             : 
    5309                 :             :         /*
    5310                 :             :          * As in DropRelationBuffers, an unlocked precheck should be safe and
    5311                 :             :          * saves some cycles.
    5312                 :             :          */
    5313                 :             : 
    5314         [ +  - ]:      163840 :         if (!use_bsearch)
    5315                 :             :         {
    5316                 :             :             int         j;
    5317                 :             : 
    5318         [ +  + ]:      505265 :             for (j = 0; j < nrels; j++)
    5319                 :             :             {
    5320         [ +  + ]:      343857 :                 if (BufTagMatchesRelFileLocator(&bufHdr->tag, &srels[j].rlocator))
    5321                 :             :                 {
    5322                 :        2432 :                     srelent = &srels[j];
    5323                 :        2432 :                     break;
    5324                 :             :                 }
    5325                 :             :             }
    5326                 :             :         }
    5327                 :             :         else
    5328                 :             :         {
    5329                 :             :             RelFileLocator rlocator;
    5330                 :             : 
    5331                 :           0 :             rlocator = BufTagGetRelFileLocator(&bufHdr->tag);
    5332                 :           0 :             srelent = bsearch(&rlocator,
    5333                 :             :                               srels, nrels, sizeof(SMgrSortArray),
    5334                 :             :                               rlocator_comparator);
    5335                 :             :         }
    5336                 :             : 
    5337                 :             :         /* buffer doesn't belong to any of the given relfilelocators; skip it */
    5338         [ +  + ]:      163840 :         if (srelent == NULL)
    5339                 :      161408 :             continue;
    5340                 :             : 
    5341                 :             :         /* Make sure we can handle the pin */
    5342                 :        2432 :         ReservePrivateRefCountEntry();
    5343                 :        2432 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    5344                 :             : 
    5345                 :        2432 :         buf_state = LockBufHdr(bufHdr);
    5346         [ +  - ]:        2432 :         if (BufTagMatchesRelFileLocator(&bufHdr->tag, &srelent->rlocator) &&
    5347         [ +  + ]:        2432 :             (buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
    5348                 :             :         {
    5349                 :        2391 :             PinBuffer_Locked(bufHdr);
    5350                 :        2391 :             FlushUnlockedBuffer(bufHdr, srelent->srel, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    5351                 :        2391 :             UnpinBuffer(bufHdr);
    5352                 :             :         }
    5353                 :             :         else
    5354                 :          41 :             UnlockBufHdr(bufHdr);
    5355                 :             :     }
    5356                 :             : 
    5357                 :          10 :     pfree(srels);
    5358                 :             : }
    5359                 :             : 
    5360                 :             : /* ---------------------------------------------------------------------
    5361                 :             :  *      RelationCopyStorageUsingBuffer
    5362                 :             :  *
    5363                 :             :  *      Copy fork's data using bufmgr.  Same as RelationCopyStorage but instead
    5364                 :             :  *      of using smgrread and smgrextend this will copy using bufmgr APIs.
    5365                 :             :  *
    5366                 :             :  *      Refer comments atop CreateAndCopyRelationData() for details about
    5367                 :             :  *      'permanent' parameter.
    5368                 :             :  * --------------------------------------------------------------------
    5369                 :             :  */
    5370                 :             : static void
    5371                 :       84620 : RelationCopyStorageUsingBuffer(RelFileLocator srclocator,
    5372                 :             :                                RelFileLocator dstlocator,
    5373                 :             :                                ForkNumber forkNum, bool permanent)
    5374                 :             : {
    5375                 :             :     Buffer      srcBuf;
    5376                 :             :     Buffer      dstBuf;
    5377                 :             :     Page        srcPage;
    5378                 :             :     Page        dstPage;
    5379                 :             :     bool        use_wal;
    5380                 :             :     BlockNumber nblocks;
    5381                 :             :     BlockNumber blkno;
    5382                 :             :     PGIOAlignedBlock buf;
    5383                 :             :     BufferAccessStrategy bstrategy_src;
    5384                 :             :     BufferAccessStrategy bstrategy_dst;
    5385                 :             :     BlockRangeReadStreamPrivate p;
    5386                 :             :     ReadStream *src_stream;
    5387                 :             :     SMgrRelation src_smgr;
    5388                 :             : 
    5389                 :             :     /*
    5390                 :             :      * In general, we want to write WAL whenever wal_level > 'minimal', but we
    5391                 :             :      * can skip it when copying any fork of an unlogged relation other than
    5392                 :             :      * the init fork.
    5393                 :             :      */
    5394   [ +  +  -  +  :       84620 :     use_wal = XLogIsNeeded() && (permanent || forkNum == INIT_FORKNUM);
                   -  - ]
    5395                 :             : 
    5396                 :             :     /* Get number of blocks in the source relation. */
    5397                 :       84620 :     nblocks = smgrnblocks(smgropen(srclocator, INVALID_PROC_NUMBER),
    5398                 :             :                           forkNum);
    5399                 :             : 
    5400                 :             :     /* Nothing to copy; just return. */
    5401         [ +  + ]:       84620 :     if (nblocks == 0)
    5402                 :       15697 :         return;
    5403                 :             : 
    5404                 :             :     /*
    5405                 :             :      * Bulk extend the destination relation of the same size as the source
    5406                 :             :      * relation before starting to copy block by block.
    5407                 :             :      */
    5408                 :       68923 :     memset(buf.data, 0, BLCKSZ);
    5409                 :       68923 :     smgrextend(smgropen(dstlocator, INVALID_PROC_NUMBER), forkNum, nblocks - 1,
    5410                 :             :                buf.data, true);
    5411                 :             : 
    5412                 :             :     /* This is a bulk operation, so use buffer access strategies. */
    5413                 :       68923 :     bstrategy_src = GetAccessStrategy(BAS_BULKREAD);
    5414                 :       68923 :     bstrategy_dst = GetAccessStrategy(BAS_BULKWRITE);
    5415                 :             : 
    5416                 :             :     /* Initialize streaming read */
    5417                 :       68923 :     p.current_blocknum = 0;
    5418                 :       68923 :     p.last_exclusive = nblocks;
    5419                 :       68923 :     src_smgr = smgropen(srclocator, INVALID_PROC_NUMBER);
    5420                 :             : 
    5421                 :             :     /*
    5422                 :             :      * It is safe to use batchmode as block_range_read_stream_cb takes no
    5423                 :             :      * locks.
    5424                 :             :      */
    5425         [ +  - ]:       68923 :     src_stream = read_stream_begin_smgr_relation(READ_STREAM_FULL |
    5426                 :             :                                                  READ_STREAM_USE_BATCHING,
    5427                 :             :                                                  bstrategy_src,
    5428                 :             :                                                  src_smgr,
    5429                 :             :                                                  permanent ? RELPERSISTENCE_PERMANENT : RELPERSISTENCE_UNLOGGED,
    5430                 :             :                                                  forkNum,
    5431                 :             :                                                  block_range_read_stream_cb,
    5432                 :             :                                                  &p,
    5433                 :             :                                                  0);
    5434                 :             : 
    5435                 :             :     /* Iterate over each block of the source relation file. */
    5436         [ +  + ]:      326269 :     for (blkno = 0; blkno < nblocks; blkno++)
    5437                 :             :     {
    5438         [ -  + ]:      257348 :         CHECK_FOR_INTERRUPTS();
    5439                 :             : 
    5440                 :             :         /* Read block from source relation. */
    5441                 :      257348 :         srcBuf = read_stream_next_buffer(src_stream, NULL);
    5442                 :      257346 :         LockBuffer(srcBuf, BUFFER_LOCK_SHARE);
    5443                 :      257346 :         srcPage = BufferGetPage(srcBuf);
    5444                 :             : 
    5445                 :      257346 :         dstBuf = ReadBufferWithoutRelcache(dstlocator, forkNum,
    5446                 :             :                                            BufferGetBlockNumber(srcBuf),
    5447                 :             :                                            RBM_ZERO_AND_LOCK, bstrategy_dst,
    5448                 :             :                                            permanent);
    5449                 :      257346 :         dstPage = BufferGetPage(dstBuf);
    5450                 :             : 
    5451                 :      257346 :         START_CRIT_SECTION();
    5452                 :             : 
    5453                 :             :         /* Copy page data from the source to the destination. */
    5454                 :      257346 :         memcpy(dstPage, srcPage, BLCKSZ);
    5455                 :      257346 :         MarkBufferDirty(dstBuf);
    5456                 :             : 
    5457                 :             :         /* WAL-log the copied page. */
    5458         [ +  + ]:      257346 :         if (use_wal)
    5459                 :      149010 :             log_newpage_buffer(dstBuf, true);
    5460                 :             : 
    5461                 :      257346 :         END_CRIT_SECTION();
    5462                 :             : 
    5463                 :      257346 :         UnlockReleaseBuffer(dstBuf);
    5464                 :      257346 :         UnlockReleaseBuffer(srcBuf);
    5465                 :             :     }
    5466                 :             :     Assert(read_stream_next_buffer(src_stream, NULL) == InvalidBuffer);
    5467                 :       68921 :     read_stream_end(src_stream);
    5468                 :             : 
    5469                 :       68921 :     FreeAccessStrategy(bstrategy_src);
    5470                 :       68921 :     FreeAccessStrategy(bstrategy_dst);
    5471                 :             : }
    5472                 :             : 
    5473                 :             : /* ---------------------------------------------------------------------
    5474                 :             :  *      CreateAndCopyRelationData
    5475                 :             :  *
    5476                 :             :  *      Create destination relation storage and copy all forks from the
    5477                 :             :  *      source relation to the destination.
    5478                 :             :  *
    5479                 :             :  *      Pass permanent as true for permanent relations and false for
    5480                 :             :  *      unlogged relations.  Currently this API is not supported for
    5481                 :             :  *      temporary relations.
    5482                 :             :  * --------------------------------------------------------------------
    5483                 :             :  */
    5484                 :             : void
    5485                 :       64932 : CreateAndCopyRelationData(RelFileLocator src_rlocator,
    5486                 :             :                           RelFileLocator dst_rlocator, bool permanent)
    5487                 :             : {
    5488                 :             :     char        relpersistence;
    5489                 :             :     SMgrRelation src_rel;
    5490                 :             :     SMgrRelation dst_rel;
    5491                 :             : 
    5492                 :             :     /* Set the relpersistence. */
    5493         [ +  - ]:       64932 :     relpersistence = permanent ?
    5494                 :             :         RELPERSISTENCE_PERMANENT : RELPERSISTENCE_UNLOGGED;
    5495                 :             : 
    5496                 :       64932 :     src_rel = smgropen(src_rlocator, INVALID_PROC_NUMBER);
    5497                 :       64932 :     dst_rel = smgropen(dst_rlocator, INVALID_PROC_NUMBER);
    5498                 :             : 
    5499                 :             :     /*
    5500                 :             :      * Create and copy all forks of the relation.  During create database we
    5501                 :             :      * have a separate cleanup mechanism which deletes complete database
    5502                 :             :      * directory.  Therefore, each individual relation doesn't need to be
    5503                 :             :      * registered for cleanup.
    5504                 :             :      */
    5505                 :       64932 :     RelationCreateStorage(dst_rlocator, relpersistence, false);
    5506                 :             : 
    5507                 :             :     /* copy main fork. */
    5508                 :       64932 :     RelationCopyStorageUsingBuffer(src_rlocator, dst_rlocator, MAIN_FORKNUM,
    5509                 :             :                                    permanent);
    5510                 :             : 
    5511                 :             :     /* copy those extra forks that exist */
    5512                 :       64930 :     for (ForkNumber forkNum = MAIN_FORKNUM + 1;
    5513         [ +  + ]:      259720 :          forkNum <= MAX_FORKNUM; forkNum++)
    5514                 :             :     {
    5515         [ +  + ]:      194790 :         if (smgrexists(src_rel, forkNum))
    5516                 :             :         {
    5517                 :       19688 :             smgrcreate(dst_rel, forkNum, false);
    5518                 :             : 
    5519                 :             :             /*
    5520                 :             :              * WAL log creation if the relation is persistent, or this is the
    5521                 :             :              * init fork of an unlogged relation.
    5522                 :             :              */
    5523   [ -  +  -  - ]:       19688 :             if (permanent || forkNum == INIT_FORKNUM)
    5524                 :       19688 :                 log_smgrcreate(&dst_rlocator, forkNum);
    5525                 :             : 
    5526                 :             :             /* Copy a fork's data, block by block. */
    5527                 :       19688 :             RelationCopyStorageUsingBuffer(src_rlocator, dst_rlocator, forkNum,
    5528                 :             :                                            permanent);
    5529                 :             :         }
    5530                 :             :     }
    5531                 :       64930 : }
    5532                 :             : 
    5533                 :             : /* ---------------------------------------------------------------------
    5534                 :             :  *      FlushDatabaseBuffers
    5535                 :             :  *
    5536                 :             :  *      This function writes all dirty pages of a database out to disk
    5537                 :             :  *      (or more accurately, out to kernel disk buffers), ensuring that the
    5538                 :             :  *      kernel has an up-to-date view of the database.
    5539                 :             :  *
    5540                 :             :  *      Generally, the caller should be holding an appropriate lock to ensure
    5541                 :             :  *      no other backend is active in the target database; otherwise more
    5542                 :             :  *      pages could get dirtied.
    5543                 :             :  *
    5544                 :             :  *      Note we don't worry about flushing any pages of temporary relations.
    5545                 :             :  *      It's assumed these wouldn't be interesting.
    5546                 :             :  * --------------------------------------------------------------------
    5547                 :             :  */
    5548                 :             : void
    5549                 :           5 : FlushDatabaseBuffers(Oid dbid)
    5550                 :             : {
    5551                 :             :     int         i;
    5552                 :             :     BufferDesc *bufHdr;
    5553                 :             : 
    5554         [ +  + ]:         645 :     for (i = 0; i < NBuffers; i++)
    5555                 :             :     {
    5556                 :             :         uint64      buf_state;
    5557                 :             : 
    5558                 :         640 :         bufHdr = GetBufferDescriptor(i);
    5559                 :             : 
    5560                 :             :         /*
    5561                 :             :          * As in DropRelationBuffers, an unlocked precheck should be safe and
    5562                 :             :          * saves some cycles.
    5563                 :             :          */
    5564         [ +  + ]:         640 :         if (bufHdr->tag.dbOid != dbid)
    5565                 :         477 :             continue;
    5566                 :             : 
    5567                 :             :         /* Make sure we can handle the pin */
    5568                 :         163 :         ReservePrivateRefCountEntry();
    5569                 :         163 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    5570                 :             : 
    5571                 :         163 :         buf_state = LockBufHdr(bufHdr);
    5572         [ +  - ]:         163 :         if (bufHdr->tag.dbOid == dbid &&
    5573         [ +  + ]:         163 :             (buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
    5574                 :             :         {
    5575                 :          27 :             PinBuffer_Locked(bufHdr);
    5576                 :          27 :             FlushUnlockedBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    5577                 :          27 :             UnpinBuffer(bufHdr);
    5578                 :             :         }
    5579                 :             :         else
    5580                 :         136 :             UnlockBufHdr(bufHdr);
    5581                 :             :     }
    5582                 :           5 : }
    5583                 :             : 
    5584                 :             : /*
    5585                 :             :  * Flush a previously, share-exclusively or exclusively, locked and pinned
    5586                 :             :  * buffer to the OS.
    5587                 :             :  */
    5588                 :             : void
    5589                 :         112 : FlushOneBuffer(Buffer buffer)
    5590                 :             : {
    5591                 :             :     BufferDesc *bufHdr;
    5592                 :             : 
    5593                 :             :     /* currently not needed, but no fundamental reason not to support */
    5594                 :             :     Assert(!BufferIsLocal(buffer));
    5595                 :             : 
    5596                 :             :     Assert(BufferIsPinned(buffer));
    5597                 :             : 
    5598                 :         112 :     bufHdr = GetBufferDescriptor(buffer - 1);
    5599                 :             : 
    5600                 :             :     Assert(BufferIsLockedByMe(buffer));
    5601                 :             : 
    5602                 :         112 :     FlushBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    5603                 :         112 : }
    5604                 :             : 
    5605                 :             : /*
    5606                 :             :  * ReleaseBuffer -- release the pin on a buffer
    5607                 :             :  */
    5608                 :             : void
    5609                 :    50025923 : ReleaseBuffer(Buffer buffer)
    5610                 :             : {
    5611         [ -  + ]:    50025923 :     if (!BufferIsValid(buffer))
    5612         [ #  # ]:           0 :         elog(ERROR, "bad buffer ID: %d", buffer);
    5613                 :             : 
    5614         [ +  + ]:    50025923 :     if (BufferIsLocal(buffer))
    5615                 :      752280 :         UnpinLocalBuffer(buffer);
    5616                 :             :     else
    5617                 :    49273643 :         UnpinBuffer(GetBufferDescriptor(buffer - 1));
    5618                 :    50025923 : }
    5619                 :             : 
    5620                 :             : /*
    5621                 :             :  * UnlockReleaseBuffer -- release the content lock and pin on a buffer
    5622                 :             :  *
    5623                 :             :  * This is just a, more efficient, shorthand for a common combination.
    5624                 :             :  */
    5625                 :             : void
    5626                 :    51118579 : UnlockReleaseBuffer(Buffer buffer)
    5627                 :             : {
    5628                 :             :     int         mode;
    5629                 :             :     BufferDesc *buf;
    5630                 :             :     PrivateRefCountEntry *ref;
    5631                 :             :     uint64      sub;
    5632                 :             :     uint64      lockstate;
    5633                 :             : 
    5634                 :             :     Assert(BufferIsPinned(buffer));
    5635                 :             : 
    5636         [ +  + ]:    51118579 :     if (BufferIsLocal(buffer))
    5637                 :             :     {
    5638                 :     1381960 :         UnpinLocalBuffer(buffer);
    5639                 :     1381960 :         return;
    5640                 :             :     }
    5641                 :             : 
    5642                 :    49736619 :     ResourceOwnerForgetBuffer(CurrentResourceOwner, buffer);
    5643                 :             : 
    5644                 :    49736619 :     buf = GetBufferDescriptor(buffer - 1);
    5645                 :             : 
    5646                 :    49736619 :     mode = BufferLockDisownInternal(buffer, buf);
    5647                 :             : 
    5648                 :             :     /* compute state modification for lock release */
    5649                 :    49736619 :     sub = BufferLockReleaseSub(mode);
    5650                 :             : 
    5651                 :             :     /* compute state modification for pin release */
    5652                 :    49736619 :     ref = GetPrivateRefCountEntry(buffer, false);
    5653                 :             :     Assert(ref != NULL);
    5654                 :             :     Assert(ref->data.refcount > 0);
    5655                 :    49736619 :     ref->data.refcount--;
    5656                 :             : 
    5657                 :             :     /* no more backend local pins, reduce shared pin count */
    5658         [ +  + ]:    49736619 :     if (likely(ref->data.refcount == 0))
    5659                 :             :     {
    5660                 :             :         /* See comment in UnpinBufferNoOwner() */
    5661                 :             :         VALGRIND_MAKE_MEM_NOACCESS(BufHdrGetBlock(buf), BLCKSZ);
    5662                 :             : 
    5663                 :    47355974 :         sub |= BUF_REFCOUNT_ONE;
    5664                 :    47355974 :         ForgetPrivateRefCountEntry(ref);
    5665                 :             :     }
    5666                 :             : 
    5667                 :             :     /* perform the lock and pin release in one atomic op */
    5668                 :    49736619 :     lockstate = pg_atomic_sub_fetch_u64(&buf->state, sub);
    5669                 :             : 
    5670                 :             :     /* wake up waiters for the lock */
    5671                 :    49736619 :     BufferLockProcessRelease(buf, mode, lockstate);
    5672                 :             : 
    5673                 :             :     /* wake up waiter for the pin release */
    5674         [ +  + ]:    49736619 :     if (lockstate & BM_PIN_COUNT_WAITER)
    5675                 :           1 :         WakePinCountWaiter(buf);
    5676                 :             : 
    5677                 :             :     /*
    5678                 :             :      * Now okay to allow cancel/die interrupts again, which were held when the
    5679                 :             :      * lock was acquired.
    5680                 :             :      */
    5681                 :    49736619 :     RESUME_INTERRUPTS();
    5682                 :             : }
    5683                 :             : 
    5684                 :             : /*
    5685                 :             :  * IncrBufferRefCount
    5686                 :             :  *      Increment the pin count on a buffer that we have *already* pinned
    5687                 :             :  *      at least once.
    5688                 :             :  *
    5689                 :             :  *      This function cannot be used on a buffer we do not have pinned,
    5690                 :             :  *      because it doesn't change the shared buffer state.
    5691                 :             :  */
    5692                 :             : void
    5693                 :    15043375 : IncrBufferRefCount(Buffer buffer)
    5694                 :             : {
    5695                 :             :     Assert(BufferIsPinned(buffer));
    5696                 :    15043375 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    5697         [ +  + ]:    15043375 :     if (BufferIsLocal(buffer))
    5698                 :      470018 :         LocalRefCount[-buffer - 1]++;
    5699                 :             :     else
    5700                 :             :     {
    5701                 :             :         PrivateRefCountEntry *ref;
    5702                 :             : 
    5703                 :    14573357 :         ref = GetPrivateRefCountEntry(buffer, true);
    5704                 :             :         Assert(ref != NULL);
    5705                 :    14573357 :         ref->data.refcount++;
    5706                 :             :     }
    5707                 :    15043375 :     ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer);
    5708                 :    15043375 : }
    5709                 :             : 
    5710                 :             : /*
    5711                 :             :  * Shared-buffer only helper for MarkBufferDirtyHint() and
    5712                 :             :  * BufferSetHintBits16().
    5713                 :             :  *
    5714                 :             :  * This is separated out because it turns out that the repeated checks for
    5715                 :             :  * local buffers, repeated GetBufferDescriptor() and repeated reading of the
    5716                 :             :  * buffer's state sufficiently hurts the performance of BufferSetHintBits16().
    5717                 :             :  */
    5718                 :             : static inline void
    5719                 :    15190100 : MarkSharedBufferDirtyHint(Buffer buffer, BufferDesc *bufHdr, uint64 lockstate,
    5720                 :             :                           bool buffer_std)
    5721                 :             : {
    5722                 :    15190100 :     Page        page = BufferGetPage(buffer);
    5723                 :             : 
    5724                 :             :     Assert(GetPrivateRefCount(buffer) > 0);
    5725                 :             : 
    5726                 :             :     /* here, either share-exclusive or exclusive lock is OK */
    5727                 :             :     Assert(BufferLockHeldByMeInMode(bufHdr, BUFFER_LOCK_EXCLUSIVE) ||
    5728                 :             :            BufferLockHeldByMeInMode(bufHdr, BUFFER_LOCK_SHARE_EXCLUSIVE));
    5729                 :             : 
    5730                 :             :     /*
    5731                 :             :      * This routine might get called many times on the same page, if we are
    5732                 :             :      * making the first scan after commit of an xact that added/deleted many
    5733                 :             :      * tuples. So, be as quick as we can if the buffer is already dirty.
    5734                 :             :      *
    5735                 :             :      * As we are holding (at least) a share-exclusive lock, nobody could have
    5736                 :             :      * cleaned or dirtied the page concurrently, so we can just rely on the
    5737                 :             :      * previously fetched value here without any danger of races.
    5738                 :             :      */
    5739         [ +  + ]:    15190100 :     if (unlikely(!(lockstate & BM_DIRTY)))
    5740                 :             :     {
    5741                 :      423199 :         XLogRecPtr  lsn = InvalidXLogRecPtr;
    5742                 :      423199 :         bool        wal_log = false;
    5743                 :             :         uint64      buf_state;
    5744                 :             : 
    5745                 :             :         /*
    5746                 :             :          * If we need to protect hint bit updates from torn writes, WAL-log a
    5747                 :             :          * full page image of the page. This full page image is only necessary
    5748                 :             :          * if the hint bit update is the first change to the page since the
    5749                 :             :          * last checkpoint.
    5750                 :             :          *
    5751                 :             :          * We don't check full_page_writes here because that logic is included
    5752                 :             :          * when we call XLogInsert() since the value changes dynamically.
    5753                 :             :          */
    5754   [ +  +  +  +  :      423199 :         if (XLogHintBitIsNeeded() && (lockstate & BM_PERMANENT))
                   +  + ]
    5755                 :             :         {
    5756                 :             :             /*
    5757                 :             :              * If we must not write WAL, due to a relfilelocator-specific
    5758                 :             :              * condition or being in recovery, don't dirty the page.  We can
    5759                 :             :              * set the hint, just not dirty the page as a result so the hint
    5760                 :             :              * is lost when we evict the page or shutdown.
    5761                 :             :              *
    5762                 :             :              * See src/backend/storage/page/README for longer discussion.
    5763                 :             :              */
    5764   [ +  +  +  + ]:      507182 :             if (RecoveryInProgress() ||
    5765                 :       85976 :                 RelFileLocatorSkippingWAL(BufTagGetRelFileLocator(&bufHdr->tag)))
    5766                 :      336478 :                 return;
    5767                 :             : 
    5768                 :       84728 :             wal_log = true;
    5769                 :             :         }
    5770                 :             : 
    5771                 :             :         /*
    5772                 :             :          * We must mark the page dirty before we emit the WAL record, as per
    5773                 :             :          * the usual rules, to ensure that BufferSync()/SyncOneBuffer() try to
    5774                 :             :          * flush the buffer, even if we haven't inserted the WAL record yet.
    5775                 :             :          * As we hold at least a share-exclusive lock, checkpoints will wait
    5776                 :             :          * for this backend to be done with the buffer before continuing. If
    5777                 :             :          * we did it the other way round, a checkpoint could start between
    5778                 :             :          * writing the WAL record and marking the buffer dirty.
    5779                 :             :          */
    5780                 :       86721 :         buf_state = LockBufHdr(bufHdr);
    5781                 :             : 
    5782                 :             :         /*
    5783                 :             :          * It should not be possible for the buffer to already be dirty, see
    5784                 :             :          * comment above.
    5785                 :             :          */
    5786                 :             :         Assert(!(buf_state & BM_DIRTY));
    5787                 :             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    5788                 :       86721 :         UnlockBufHdrExt(bufHdr, buf_state,
    5789                 :             :                         BM_DIRTY,
    5790                 :             :                         0, 0);
    5791                 :             : 
    5792                 :             :         /*
    5793                 :             :          * If the block is already dirty because we either made a change or
    5794                 :             :          * set a hint already, then we don't need to write a full page image.
    5795                 :             :          * Note that aggressive cleaning of blocks dirtied by hint bit setting
    5796                 :             :          * would increase the call rate. Bulk setting of hint bits would
    5797                 :             :          * reduce the call rate...
    5798                 :             :          */
    5799         [ +  + ]:       86721 :         if (wal_log)
    5800                 :       84728 :             lsn = XLogSaveBufferForHint(buffer, buffer_std);
    5801                 :             : 
    5802         [ +  + ]:       86721 :         if (XLogRecPtrIsValid(lsn))
    5803                 :             :         {
    5804                 :             :             /*
    5805                 :             :              * Set the page LSN if we wrote a backup block. To allow backends
    5806                 :             :              * that only hold a share lock on the buffer to read the LSN in a
    5807                 :             :              * tear-free manner, we set the page LSN while holding the buffer
    5808                 :             :              * header lock. This allows any reader of an LSN who holds only a
    5809                 :             :              * share lock to also obtain a buffer header lock before using
    5810                 :             :              * PageGetLSN() to read the LSN in a tear free way. This is done
    5811                 :             :              * in BufferGetLSNAtomic().
    5812                 :             :              *
    5813                 :             :              * If checksums are enabled, you might think we should reset the
    5814                 :             :              * checksum here. That will happen when the page is written
    5815                 :             :              * sometime later in this checkpoint cycle.
    5816                 :             :              */
    5817                 :       58422 :             buf_state = LockBufHdr(bufHdr);
    5818                 :       58422 :             PageSetLSN(page, lsn);
    5819                 :       58422 :             UnlockBufHdr(bufHdr);
    5820                 :             :         }
    5821                 :             : 
    5822                 :       86721 :         pgBufferUsage.shared_blks_dirtied++;
    5823         [ +  + ]:       86721 :         if (VacuumCostActive)
    5824                 :        1447 :             VacuumCostBalance += VacuumCostPageDirty;
    5825                 :             :     }
    5826                 :             : }
    5827                 :             : 
    5828                 :             : /*
    5829                 :             :  * MarkBufferDirtyHint
    5830                 :             :  *
    5831                 :             :  *  Mark a buffer dirty for non-critical changes.
    5832                 :             :  *
    5833                 :             :  * This is essentially the same as MarkBufferDirty, except:
    5834                 :             :  *
    5835                 :             :  * 1. The caller does not write WAL; so if checksums are enabled, we may need
    5836                 :             :  *    to write an XLOG_FPI_FOR_HINT WAL record to protect against torn pages.
    5837                 :             :  * 2. The caller might have only a share-exclusive-lock instead of an
    5838                 :             :  *    exclusive-lock on the buffer's content lock.
    5839                 :             :  * 3. This function does not guarantee that the buffer is always marked dirty
    5840                 :             :  *    (it e.g. can't always on a hot standby), so it cannot be used for
    5841                 :             :  *    important changes.
    5842                 :             :  */
    5843                 :             : inline void
    5844                 :      439934 : MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
    5845                 :             : {
    5846                 :             :     BufferDesc *bufHdr;
    5847                 :             : 
    5848         [ -  + ]:      439934 :     if (!BufferIsValid(buffer))
    5849         [ #  # ]:           0 :         elog(ERROR, "bad buffer ID: %d", buffer);
    5850                 :             : 
    5851         [ +  + ]:      439934 :     if (BufferIsLocal(buffer))
    5852                 :             :     {
    5853                 :       22052 :         MarkLocalBufferDirty(buffer);
    5854                 :       22052 :         return;
    5855                 :             :     }
    5856                 :             : 
    5857                 :      417882 :     bufHdr = GetBufferDescriptor(buffer - 1);
    5858                 :             : 
    5859                 :      417882 :     MarkSharedBufferDirtyHint(buffer, bufHdr,
    5860                 :      417882 :                               pg_atomic_read_u64(&bufHdr->state),
    5861                 :             :                               buffer_std);
    5862                 :             : }
    5863                 :             : 
    5864                 :             : /*
    5865                 :             :  * Release buffer content locks for shared buffers.
    5866                 :             :  *
    5867                 :             :  * Used to clean up after errors.
    5868                 :             :  *
    5869                 :             :  * Currently, we can expect that resource owner cleanup, via
    5870                 :             :  * ResOwnerReleaseBuffer(), took care of releasing buffer content locks per
    5871                 :             :  * se; the only thing we need to deal with here is clearing any PIN_COUNT
    5872                 :             :  * request that was in progress.
    5873                 :             :  */
    5874                 :             : void
    5875                 :       66181 : UnlockBuffers(void)
    5876                 :             : {
    5877                 :       66181 :     BufferDesc *buf = PinCountWaitBuf;
    5878                 :             : 
    5879         [ -  + ]:       66181 :     if (buf)
    5880                 :             :     {
    5881                 :             :         uint64      buf_state;
    5882                 :           0 :         uint64      unset_bits = 0;
    5883                 :             : 
    5884                 :           0 :         buf_state = LockBufHdr(buf);
    5885                 :             : 
    5886                 :             :         /*
    5887                 :             :          * Don't complain if flag bit not set; it could have been reset but we
    5888                 :             :          * got a cancel/die interrupt before getting the signal.
    5889                 :             :          */
    5890         [ #  # ]:           0 :         if ((buf_state & BM_PIN_COUNT_WAITER) != 0 &&
    5891         [ #  # ]:           0 :             buf->wait_backend_pgprocno == MyProcNumber)
    5892                 :           0 :             unset_bits = BM_PIN_COUNT_WAITER;
    5893                 :             : 
    5894                 :           0 :         UnlockBufHdrExt(buf, buf_state,
    5895                 :             :                         0, unset_bits,
    5896                 :             :                         0);
    5897                 :             : 
    5898                 :           0 :         PinCountWaitBuf = NULL;
    5899                 :             :     }
    5900                 :       66181 : }
    5901                 :             : 
    5902                 :             : /*
    5903                 :             :  * Acquire the buffer content lock in the specified mode
    5904                 :             :  *
    5905                 :             :  * If the lock is not available, sleep until it is.
    5906                 :             :  *
    5907                 :             :  * Side effect: cancel/die interrupts are held off until lock release.
    5908                 :             :  *
    5909                 :             :  * This uses almost the same locking approach as lwlock.c's
    5910                 :             :  * LWLockAcquire(). See documentation at the top of lwlock.c for a more
    5911                 :             :  * detailed discussion.
    5912                 :             :  *
    5913                 :             :  * The reason that this, and most of the other BufferLock* functions, get both
    5914                 :             :  * the Buffer and BufferDesc* as parameters, is that looking up one from the
    5915                 :             :  * other repeatedly shows up noticeably in profiles.
    5916                 :             :  *
    5917                 :             :  * Callers should provide a constant for mode, for more efficient code
    5918                 :             :  * generation.
    5919                 :             :  */
    5920                 :             : static inline void
    5921                 :   112116182 : BufferLockAcquire(Buffer buffer, BufferDesc *buf_hdr, BufferLockMode mode)
    5922                 :             : {
    5923                 :             :     PrivateRefCountEntry *entry;
    5924                 :   112116182 :     int         extraWaits = 0;
    5925                 :             : 
    5926                 :             :     /*
    5927                 :             :      * Get reference to the refcount entry before we hold the lock, it seems
    5928                 :             :      * better to do before holding the lock.
    5929                 :             :      */
    5930                 :   112116182 :     entry = GetPrivateRefCountEntry(buffer, true);
    5931                 :             : 
    5932                 :             :     /*
    5933                 :             :      * We better not already hold a lock on the buffer.
    5934                 :             :      */
    5935                 :             :     Assert(entry->data.lockmode == BUFFER_LOCK_UNLOCK);
    5936                 :             : 
    5937                 :             :     /*
    5938                 :             :      * Lock out cancel/die interrupts until we exit the code section protected
    5939                 :             :      * by the content lock.  This ensures that interrupts will not interfere
    5940                 :             :      * with manipulations of data structures in shared memory.
    5941                 :             :      */
    5942                 :   112116182 :     HOLD_INTERRUPTS();
    5943                 :             : 
    5944                 :             :     for (;;)
    5945                 :       22230 :     {
    5946                 :   112138412 :         uint32      wait_event = 0; /* initialized to avoid compiler warning */
    5947                 :             :         bool        mustwait;
    5948                 :             : 
    5949                 :             :         /*
    5950                 :             :          * Try to grab the lock the first time, we're not in the waitqueue
    5951                 :             :          * yet/anymore.
    5952                 :             :          */
    5953                 :   112138412 :         mustwait = BufferLockAttempt(buf_hdr, mode);
    5954                 :             : 
    5955         [ +  + ]:   112138412 :         if (likely(!mustwait))
    5956                 :             :         {
    5957                 :   112114544 :             break;
    5958                 :             :         }
    5959                 :             : 
    5960                 :             :         /*
    5961                 :             :          * Ok, at this point we couldn't grab the lock on the first try. We
    5962                 :             :          * cannot simply queue ourselves to the end of the list and wait to be
    5963                 :             :          * woken up because by now the lock could long have been released.
    5964                 :             :          * Instead add us to the queue and try to grab the lock again. If we
    5965                 :             :          * succeed we need to revert the queuing and be happy, otherwise we
    5966                 :             :          * recheck the lock. If we still couldn't grab it, we know that the
    5967                 :             :          * other locker will see our queue entries when releasing since they
    5968                 :             :          * existed before we checked for the lock.
    5969                 :             :          */
    5970                 :             : 
    5971                 :             :         /* add to the queue */
    5972                 :       23868 :         BufferLockQueueSelf(buf_hdr, mode);
    5973                 :             : 
    5974                 :             :         /* we're now guaranteed to be woken up if necessary */
    5975                 :       23868 :         mustwait = BufferLockAttempt(buf_hdr, mode);
    5976                 :             : 
    5977                 :             :         /* ok, grabbed the lock the second time round, need to undo queueing */
    5978         [ +  + ]:       23868 :         if (!mustwait)
    5979                 :             :         {
    5980                 :        1638 :             BufferLockDequeueSelf(buf_hdr);
    5981                 :        1638 :             break;
    5982                 :             :         }
    5983                 :             : 
    5984   [ +  +  +  - ]:       22230 :         switch (mode)
    5985                 :             :         {
    5986                 :       12752 :             case BUFFER_LOCK_EXCLUSIVE:
    5987                 :       12752 :                 wait_event = WAIT_EVENT_BUFFER_EXCLUSIVE;
    5988                 :       12752 :                 break;
    5989                 :         143 :             case BUFFER_LOCK_SHARE_EXCLUSIVE:
    5990                 :         143 :                 wait_event = WAIT_EVENT_BUFFER_SHARE_EXCLUSIVE;
    5991                 :         143 :                 break;
    5992                 :        9335 :             case BUFFER_LOCK_SHARE:
    5993                 :        9335 :                 wait_event = WAIT_EVENT_BUFFER_SHARED;
    5994                 :        9335 :                 break;
    5995                 :             :             case BUFFER_LOCK_UNLOCK:
    5996                 :             :                 pg_unreachable();
    5997                 :             : 
    5998                 :             :         }
    5999                 :       22230 :         pgstat_report_wait_start(wait_event);
    6000                 :             : 
    6001                 :             :         /*
    6002                 :             :          * Wait until awakened.
    6003                 :             :          *
    6004                 :             :          * It is possible that we get awakened for a reason other than being
    6005                 :             :          * signaled by BufferLockWakeup().  If so, loop back and wait again.
    6006                 :             :          * Once we've gotten the lock, re-increment the sema by the number of
    6007                 :             :          * additional signals received.
    6008                 :             :          */
    6009                 :             :         for (;;)
    6010                 :             :         {
    6011                 :       22230 :             PGSemaphoreLock(MyProc->sem);
    6012         [ +  - ]:       22230 :             if (MyProc->lwWaiting == LW_WS_NOT_WAITING)
    6013                 :       22230 :                 break;
    6014                 :           0 :             extraWaits++;
    6015                 :             :         }
    6016                 :             : 
    6017                 :       22230 :         pgstat_report_wait_end();
    6018                 :             : 
    6019                 :             :         /* Retrying, allow BufferLockReleaseSub to release waiters again. */
    6020                 :       22230 :         pg_atomic_fetch_and_u64(&buf_hdr->state, ~BM_LOCK_WAKE_IN_PROGRESS);
    6021                 :             :     }
    6022                 :             : 
    6023                 :             :     /* Remember that we now hold this lock */
    6024                 :   112116182 :     entry->data.lockmode = mode;
    6025                 :             : 
    6026                 :             :     /*
    6027                 :             :      * Fix the process wait semaphore's count for any absorbed wakeups.
    6028                 :             :      */
    6029         [ -  + ]:   112116182 :     while (unlikely(extraWaits-- > 0))
    6030                 :           0 :         PGSemaphoreUnlock(MyProc->sem);
    6031                 :   112116182 : }
    6032                 :             : 
    6033                 :             : /*
    6034                 :             :  * Release a previously acquired buffer content lock.
    6035                 :             :  */
    6036                 :             : static void
    6037                 :    64531462 : BufferLockUnlock(Buffer buffer, BufferDesc *buf_hdr)
    6038                 :             : {
    6039                 :             :     BufferLockMode mode;
    6040                 :             :     uint64      oldstate;
    6041                 :             :     uint64      sub;
    6042                 :             : 
    6043                 :    64531462 :     mode = BufferLockDisownInternal(buffer, buf_hdr);
    6044                 :             : 
    6045                 :             :     /*
    6046                 :             :      * Release my hold on lock, after that it can immediately be acquired by
    6047                 :             :      * others, even if we still have to wakeup other waiters.
    6048                 :             :      */
    6049                 :    64531462 :     sub = BufferLockReleaseSub(mode);
    6050                 :             : 
    6051                 :    64531462 :     oldstate = pg_atomic_sub_fetch_u64(&buf_hdr->state, sub);
    6052                 :             : 
    6053                 :    64531462 :     BufferLockProcessRelease(buf_hdr, mode, oldstate);
    6054                 :             : 
    6055                 :             :     /*
    6056                 :             :      * Now okay to allow cancel/die interrupts.
    6057                 :             :      */
    6058                 :    64531462 :     RESUME_INTERRUPTS();
    6059                 :    64531462 : }
    6060                 :             : 
    6061                 :             : 
    6062                 :             : /*
    6063                 :             :  * Acquire the content lock for the buffer, but only if we don't have to wait.
    6064                 :             :  *
    6065                 :             :  * It is allowed to try to conditionally acquire a lock on a buffer that this
    6066                 :             :  * backend has already locked, but the lock acquisition will always fail, even
    6067                 :             :  * if the new lock acquisition does not conflict with an already held lock
    6068                 :             :  * (e.g. two share locks). This is because we currently do not have space to
    6069                 :             :  * track multiple lock ownerships of the same buffer within one backend.  That
    6070                 :             :  * is ok for the current uses of BufferLockConditional().
    6071                 :             :  */
    6072                 :             : static bool
    6073                 :     2152417 : BufferLockConditional(Buffer buffer, BufferDesc *buf_hdr, BufferLockMode mode)
    6074                 :             : {
    6075                 :     2152417 :     PrivateRefCountEntry *entry = GetPrivateRefCountEntry(buffer, true);
    6076                 :             :     bool        mustwait;
    6077                 :             : 
    6078                 :             :     /*
    6079                 :             :      * As described above, if we're trying to lock a buffer this backend
    6080                 :             :      * already has locked, return false, independent of the existing and
    6081                 :             :      * desired lock level.
    6082                 :             :      */
    6083         [ -  + ]:     2152417 :     if (entry->data.lockmode != BUFFER_LOCK_UNLOCK)
    6084                 :           0 :         return false;
    6085                 :             : 
    6086                 :             :     /*
    6087                 :             :      * Lock out cancel/die interrupts until we exit the code section protected
    6088                 :             :      * by the content lock.  This ensures that interrupts will not interfere
    6089                 :             :      * with manipulations of data structures in shared memory.
    6090                 :             :      */
    6091                 :     2152417 :     HOLD_INTERRUPTS();
    6092                 :             : 
    6093                 :             :     /* Check for the lock */
    6094                 :     2152417 :     mustwait = BufferLockAttempt(buf_hdr, mode);
    6095                 :             : 
    6096         [ +  + ]:     2152417 :     if (mustwait)
    6097                 :             :     {
    6098                 :             :         /* Failed to get lock, so release interrupt holdoff */
    6099                 :         518 :         RESUME_INTERRUPTS();
    6100                 :             :     }
    6101                 :             :     else
    6102                 :             :     {
    6103                 :     2151899 :         entry->data.lockmode = mode;
    6104                 :             :     }
    6105                 :             : 
    6106                 :     2152417 :     return !mustwait;
    6107                 :             : }
    6108                 :             : 
    6109                 :             : /*
    6110                 :             :  * Internal function that tries to atomically acquire the content lock in the
    6111                 :             :  * passed in mode.
    6112                 :             :  *
    6113                 :             :  * This function will not block waiting for a lock to become free - that's the
    6114                 :             :  * caller's job.
    6115                 :             :  *
    6116                 :             :  * Similar to LWLockAttemptLock().
    6117                 :             :  */
    6118                 :             : static inline bool
    6119                 :   114314697 : BufferLockAttempt(BufferDesc *buf_hdr, BufferLockMode mode)
    6120                 :             : {
    6121                 :             :     uint64      old_state;
    6122                 :             : 
    6123                 :             :     /*
    6124                 :             :      * Read once outside the loop, later iterations will get the newer value
    6125                 :             :      * via compare & exchange.
    6126                 :             :      */
    6127                 :   114314697 :     old_state = pg_atomic_read_u64(&buf_hdr->state);
    6128                 :             : 
    6129                 :             :     /* loop until we've determined whether we could acquire the lock or not */
    6130                 :             :     while (true)
    6131                 :       16296 :     {
    6132                 :             :         uint64      desired_state;
    6133                 :             :         bool        lock_free;
    6134                 :             : 
    6135                 :   114330993 :         desired_state = old_state;
    6136                 :             : 
    6137         [ +  + ]:   114330993 :         if (mode == BUFFER_LOCK_EXCLUSIVE)
    6138                 :             :         {
    6139                 :    38982791 :             lock_free = (old_state & BM_LOCK_MASK) == 0;
    6140         [ +  + ]:    38982791 :             if (lock_free)
    6141                 :    38955364 :                 desired_state += BM_LOCK_VAL_EXCLUSIVE;
    6142                 :             :         }
    6143         [ +  + ]:    75348202 :         else if (mode == BUFFER_LOCK_SHARE_EXCLUSIVE)
    6144                 :             :         {
    6145                 :      722662 :             lock_free = (old_state & (BM_LOCK_VAL_EXCLUSIVE | BM_LOCK_VAL_SHARE_EXCLUSIVE)) == 0;
    6146         [ +  + ]:      722662 :             if (lock_free)
    6147                 :      722375 :                 desired_state += BM_LOCK_VAL_SHARE_EXCLUSIVE;
    6148                 :             :         }
    6149                 :             :         else
    6150                 :             :         {
    6151                 :    74625540 :             lock_free = (old_state & BM_LOCK_VAL_EXCLUSIVE) == 0;
    6152         [ +  + ]:    74625540 :             if (lock_free)
    6153                 :    74606078 :                 desired_state += BM_LOCK_VAL_SHARED;
    6154                 :             :         }
    6155                 :             : 
    6156                 :             :         /*
    6157                 :             :          * Attempt to swap in the state we are expecting. If we didn't see
    6158                 :             :          * lock to be free, that's just the old value. If we saw it as free,
    6159                 :             :          * we'll attempt to mark it acquired. The reason that we always swap
    6160                 :             :          * in the value is that this doubles as a memory barrier. We could try
    6161                 :             :          * to be smarter and only swap in values if we saw the lock as free,
    6162                 :             :          * but benchmark haven't shown it as beneficial so far.
    6163                 :             :          *
    6164                 :             :          * Retry if the value changed since we last looked at it.
    6165                 :             :          */
    6166         [ +  + ]:   114330993 :         if (likely(pg_atomic_compare_exchange_u64(&buf_hdr->state,
    6167                 :             :                                                   &old_state, desired_state)))
    6168                 :             :         {
    6169         [ +  + ]:   114314697 :             if (lock_free)
    6170                 :             :             {
    6171                 :             :                 /* Great! Got the lock. */
    6172                 :   114268081 :                 return false;
    6173                 :             :             }
    6174                 :             :             else
    6175                 :       46616 :                 return true;    /* somebody else has the lock */
    6176                 :             :         }
    6177                 :             :     }
    6178                 :             : 
    6179                 :             :     pg_unreachable();
    6180                 :             : }
    6181                 :             : 
    6182                 :             : /*
    6183                 :             :  * Add ourselves to the end of the content lock's wait queue.
    6184                 :             :  */
    6185                 :             : static void
    6186                 :       23868 : BufferLockQueueSelf(BufferDesc *buf_hdr, BufferLockMode mode)
    6187                 :             : {
    6188                 :             :     /*
    6189                 :             :      * If we don't have a PGPROC structure, there's no way to wait. This
    6190                 :             :      * should never occur, since MyProc should only be null during shared
    6191                 :             :      * memory initialization.
    6192                 :             :      */
    6193         [ -  + ]:       23868 :     if (MyProc == NULL)
    6194         [ #  # ]:           0 :         elog(PANIC, "cannot wait without a PGPROC structure");
    6195                 :             : 
    6196         [ -  + ]:       23868 :     if (MyProc->lwWaiting != LW_WS_NOT_WAITING)
    6197         [ #  # ]:           0 :         elog(PANIC, "queueing for lock while waiting on another one");
    6198                 :             : 
    6199                 :       23868 :     LockBufHdr(buf_hdr);
    6200                 :             : 
    6201                 :             :     /* setting the flag is protected by the spinlock */
    6202                 :       23868 :     pg_atomic_fetch_or_u64(&buf_hdr->state, BM_LOCK_HAS_WAITERS);
    6203                 :             : 
    6204                 :             :     /*
    6205                 :             :      * These are currently used both for lwlocks and buffer content locks,
    6206                 :             :      * which is acceptable, although not pretty, because a backend can't wait
    6207                 :             :      * for both types of locks at the same time.
    6208                 :             :      */
    6209                 :       23868 :     MyProc->lwWaiting = LW_WS_WAITING;
    6210                 :       23868 :     MyProc->lwWaitMode = mode;
    6211                 :             : 
    6212                 :       23868 :     proclist_push_tail(&buf_hdr->lock_waiters, MyProcNumber, lwWaitLink);
    6213                 :             : 
    6214                 :             :     /* Can release the mutex now */
    6215                 :       23868 :     UnlockBufHdr(buf_hdr);
    6216                 :       23868 : }
    6217                 :             : 
    6218                 :             : /*
    6219                 :             :  * Remove ourselves from the waitlist.
    6220                 :             :  *
    6221                 :             :  * This is used if we queued ourselves because we thought we needed to sleep
    6222                 :             :  * but, after further checking, we discovered that we don't actually need to
    6223                 :             :  * do so.
    6224                 :             :  */
    6225                 :             : static void
    6226                 :        1638 : BufferLockDequeueSelf(BufferDesc *buf_hdr)
    6227                 :             : {
    6228                 :             :     bool        on_waitlist;
    6229                 :             : 
    6230                 :        1638 :     LockBufHdr(buf_hdr);
    6231                 :             : 
    6232                 :        1638 :     on_waitlist = MyProc->lwWaiting == LW_WS_WAITING;
    6233         [ +  + ]:        1638 :     if (on_waitlist)
    6234                 :        1134 :         proclist_delete(&buf_hdr->lock_waiters, MyProcNumber, lwWaitLink);
    6235                 :             : 
    6236         [ +  + ]:        1638 :     if (proclist_is_empty(&buf_hdr->lock_waiters) &&
    6237         [ +  + ]:        1581 :         (pg_atomic_read_u64(&buf_hdr->state) & BM_LOCK_HAS_WAITERS) != 0)
    6238                 :             :     {
    6239                 :        1078 :         pg_atomic_fetch_and_u64(&buf_hdr->state, ~BM_LOCK_HAS_WAITERS);
    6240                 :             :     }
    6241                 :             : 
    6242                 :             :     /* XXX: combine with fetch_and above? */
    6243                 :        1638 :     UnlockBufHdr(buf_hdr);
    6244                 :             : 
    6245                 :             :     /* clear waiting state again, nice for debugging */
    6246         [ +  + ]:        1638 :     if (on_waitlist)
    6247                 :        1134 :         MyProc->lwWaiting = LW_WS_NOT_WAITING;
    6248                 :             :     else
    6249                 :             :     {
    6250                 :         504 :         int         extraWaits = 0;
    6251                 :             : 
    6252                 :             : 
    6253                 :             :         /*
    6254                 :             :          * Somebody else dequeued us and has or will wake us up. Deal with the
    6255                 :             :          * superfluous absorption of a wakeup.
    6256                 :             :          */
    6257                 :             : 
    6258                 :             :         /*
    6259                 :             :          * Clear BM_LOCK_WAKE_IN_PROGRESS if somebody woke us before we
    6260                 :             :          * removed ourselves - they'll have set it.
    6261                 :             :          */
    6262                 :         504 :         pg_atomic_fetch_and_u64(&buf_hdr->state, ~BM_LOCK_WAKE_IN_PROGRESS);
    6263                 :             : 
    6264                 :             :         /*
    6265                 :             :          * Now wait for the scheduled wakeup, otherwise our ->lwWaiting would
    6266                 :             :          * get reset at some inconvenient point later. Most of the time this
    6267                 :             :          * will immediately return.
    6268                 :             :          */
    6269                 :             :         for (;;)
    6270                 :             :         {
    6271                 :         504 :             PGSemaphoreLock(MyProc->sem);
    6272         [ +  - ]:         504 :             if (MyProc->lwWaiting == LW_WS_NOT_WAITING)
    6273                 :         504 :                 break;
    6274                 :           0 :             extraWaits++;
    6275                 :             :         }
    6276                 :             : 
    6277                 :             :         /*
    6278                 :             :          * Fix the process wait semaphore's count for any absorbed wakeups.
    6279                 :             :          */
    6280         [ -  + ]:         504 :         while (extraWaits-- > 0)
    6281                 :           0 :             PGSemaphoreUnlock(MyProc->sem);
    6282                 :             :     }
    6283                 :        1638 : }
    6284                 :             : 
    6285                 :             : /*
    6286                 :             :  * Stop treating lock as held by current backend.
    6287                 :             :  *
    6288                 :             :  * After calling this function it's the callers responsibility to ensure that
    6289                 :             :  * the lock gets released, even in case of an error. This only is desirable if
    6290                 :             :  * the lock is going to be released in a different process than the process
    6291                 :             :  * that acquired it.
    6292                 :             :  */
    6293                 :             : static inline void
    6294                 :           0 : BufferLockDisown(Buffer buffer, BufferDesc *buf_hdr)
    6295                 :             : {
    6296                 :           0 :     BufferLockDisownInternal(buffer, buf_hdr);
    6297                 :           0 :     RESUME_INTERRUPTS();
    6298                 :           0 : }
    6299                 :             : 
    6300                 :             : /*
    6301                 :             :  * Stop treating lock as held by current backend.
    6302                 :             :  *
    6303                 :             :  * This is the code that can be shared between actually releasing a lock
    6304                 :             :  * (BufferLockUnlock()) and just not tracking ownership of the lock anymore
    6305                 :             :  * without releasing the lock (BufferLockDisown()).
    6306                 :             :  */
    6307                 :             : static inline int
    6308                 :   114268081 : BufferLockDisownInternal(Buffer buffer, BufferDesc *buf_hdr)
    6309                 :             : {
    6310                 :             :     BufferLockMode mode;
    6311                 :             :     PrivateRefCountEntry *ref;
    6312                 :             : 
    6313                 :   114268081 :     ref = GetPrivateRefCountEntry(buffer, false);
    6314         [ -  + ]:   114268081 :     if (ref == NULL)
    6315         [ #  # ]:           0 :         elog(ERROR, "lock %d is not held", buffer);
    6316                 :   114268081 :     mode = ref->data.lockmode;
    6317                 :   114268081 :     ref->data.lockmode = BUFFER_LOCK_UNLOCK;
    6318                 :             : 
    6319                 :   114268081 :     return mode;
    6320                 :             : }
    6321                 :             : 
    6322                 :             : /*
    6323                 :             :  * Wakeup all the lockers that currently have a chance to acquire the lock.
    6324                 :             :  *
    6325                 :             :  * wake_exclusive indicates whether exclusive lock waiters should be woken up.
    6326                 :             :  */
    6327                 :             : static void
    6328                 :       22112 : BufferLockWakeup(BufferDesc *buf_hdr, bool wake_exclusive)
    6329                 :             : {
    6330                 :       22112 :     bool        new_wake_in_progress = false;
    6331                 :       22112 :     bool        wake_share_exclusive = true;
    6332                 :             :     proclist_head wakeup;
    6333                 :             :     proclist_mutable_iter iter;
    6334                 :             : 
    6335                 :       22112 :     proclist_init(&wakeup);
    6336                 :             : 
    6337                 :             :     /* lock wait list while collecting backends to wake up */
    6338                 :       22112 :     LockBufHdr(buf_hdr);
    6339                 :             : 
    6340   [ +  +  +  +  :       32296 :     proclist_foreach_modify(iter, &buf_hdr->lock_waiters, lwWaitLink)
                   +  + ]
    6341                 :             :     {
    6342                 :       23275 :         PGPROC     *waiter = GetPGProcByNumber(iter.cur);
    6343                 :             : 
    6344                 :             :         /*
    6345                 :             :          * Already woke up a conflicting lock, so skip over this wait list
    6346                 :             :          * entry.
    6347                 :             :          */
    6348   [ +  +  +  + ]:       23275 :         if (!wake_exclusive && waiter->lwWaitMode == BUFFER_LOCK_EXCLUSIVE)
    6349                 :         544 :             continue;
    6350   [ +  +  -  + ]:       22731 :         if (!wake_share_exclusive && waiter->lwWaitMode == BUFFER_LOCK_SHARE_EXCLUSIVE)
    6351                 :           0 :             continue;
    6352                 :             : 
    6353                 :       22731 :         proclist_delete(&buf_hdr->lock_waiters, iter.cur, lwWaitLink);
    6354                 :       22731 :         proclist_push_tail(&wakeup, iter.cur, lwWaitLink);
    6355                 :             : 
    6356                 :             :         /*
    6357                 :             :          * Prevent additional wakeups until retryer gets to run. Backends that
    6358                 :             :          * are just waiting for the lock to become free don't retry
    6359                 :             :          * automatically.
    6360                 :             :          */
    6361                 :       22731 :         new_wake_in_progress = true;
    6362                 :             : 
    6363                 :             :         /*
    6364                 :             :          * Signal that the process isn't on the wait list anymore. This allows
    6365                 :             :          * BufferLockDequeueSelf() to remove itself from the waitlist with a
    6366                 :             :          * proclist_delete(), rather than having to check if it has been
    6367                 :             :          * removed from the list.
    6368                 :             :          */
    6369                 :             :         Assert(waiter->lwWaiting == LW_WS_WAITING);
    6370                 :       22731 :         waiter->lwWaiting = LW_WS_PENDING_WAKEUP;
    6371                 :             : 
    6372                 :             :         /*
    6373                 :             :          * Don't wakeup further waiters after waking a conflicting waiter.
    6374                 :             :          */
    6375         [ +  + ]:       22731 :         if (waiter->lwWaitMode == BUFFER_LOCK_SHARE)
    6376                 :             :         {
    6377                 :             :             /*
    6378                 :             :              * Share locks conflict with exclusive locks.
    6379                 :             :              */
    6380                 :        9494 :             wake_exclusive = false;
    6381                 :             :         }
    6382         [ +  + ]:       13237 :         else if (waiter->lwWaitMode == BUFFER_LOCK_SHARE_EXCLUSIVE)
    6383                 :             :         {
    6384                 :             :             /*
    6385                 :             :              * Share-exclusive locks conflict with share-exclusive and
    6386                 :             :              * exclusive locks.
    6387                 :             :              */
    6388                 :         146 :             wake_exclusive = false;
    6389                 :         146 :             wake_share_exclusive = false;
    6390                 :             :         }
    6391         [ +  - ]:       13091 :         else if (waiter->lwWaitMode == BUFFER_LOCK_EXCLUSIVE)
    6392                 :             :         {
    6393                 :             :             /*
    6394                 :             :              * Exclusive locks conflict with all other locks, there's no point
    6395                 :             :              * in waking up anybody else.
    6396                 :             :              */
    6397                 :       13091 :             break;
    6398                 :             :         }
    6399                 :             :     }
    6400                 :             : 
    6401                 :             :     Assert(proclist_is_empty(&wakeup) || pg_atomic_read_u64(&buf_hdr->state) & BM_LOCK_HAS_WAITERS);
    6402                 :             : 
    6403                 :             :     /* unset required flags, and release lock, in one fell swoop */
    6404                 :             :     {
    6405                 :             :         uint64      old_state;
    6406                 :             :         uint64      desired_state;
    6407                 :             : 
    6408                 :       22112 :         old_state = pg_atomic_read_u64(&buf_hdr->state);
    6409                 :             :         while (true)
    6410                 :             :         {
    6411                 :       22146 :             desired_state = old_state;
    6412                 :             : 
    6413                 :             :             /* compute desired flags */
    6414                 :             : 
    6415         [ +  + ]:       22146 :             if (new_wake_in_progress)
    6416                 :       21847 :                 desired_state |= BM_LOCK_WAKE_IN_PROGRESS;
    6417                 :             :             else
    6418                 :         299 :                 desired_state &= ~BM_LOCK_WAKE_IN_PROGRESS;
    6419                 :             : 
    6420         [ +  + ]:       22146 :             if (proclist_is_empty(&buf_hdr->lock_waiters))
    6421                 :       20249 :                 desired_state &= ~BM_LOCK_HAS_WAITERS;
    6422                 :             : 
    6423                 :       22146 :             desired_state &= ~BM_LOCKED;    /* release lock */
    6424                 :             : 
    6425         [ +  + ]:       22146 :             if (pg_atomic_compare_exchange_u64(&buf_hdr->state, &old_state,
    6426                 :             :                                                desired_state))
    6427                 :       22112 :                 break;
    6428                 :             :         }
    6429                 :             :     }
    6430                 :             : 
    6431                 :             :     /* Awaken any waiters I removed from the queue. */
    6432   [ +  +  +  +  :       44843 :     proclist_foreach_modify(iter, &wakeup, lwWaitLink)
                   +  + ]
    6433                 :             :     {
    6434                 :       22731 :         PGPROC     *waiter = GetPGProcByNumber(iter.cur);
    6435                 :             : 
    6436                 :       22731 :         proclist_delete(&wakeup, iter.cur, lwWaitLink);
    6437                 :             : 
    6438                 :             :         /*
    6439                 :             :          * Guarantee that lwWaiting being unset only becomes visible once the
    6440                 :             :          * unlink from the link has completed. Otherwise the target backend
    6441                 :             :          * could be woken up for other reason and enqueue for a new lock - if
    6442                 :             :          * that happens before the list unlink happens, the list would end up
    6443                 :             :          * being corrupted.
    6444                 :             :          *
    6445                 :             :          * The barrier pairs with the LockBufHdr() when enqueuing for another
    6446                 :             :          * lock.
    6447                 :             :          */
    6448                 :       22731 :         pg_write_barrier();
    6449                 :       22731 :         waiter->lwWaiting = LW_WS_NOT_WAITING;
    6450                 :       22731 :         PGSemaphoreUnlock(waiter->sem);
    6451                 :             :     }
    6452                 :       22112 : }
    6453                 :             : 
    6454                 :             : /*
    6455                 :             :  * Compute subtraction from buffer state for a release of a held lock in
    6456                 :             :  * `mode`.
    6457                 :             :  *
    6458                 :             :  * This is separated from BufferLockUnlock() as we want to combine the lock
    6459                 :             :  * release with other atomic operations when possible, leading to the lock
    6460                 :             :  * release being done in multiple places, each needing to compute what to
    6461                 :             :  * subtract from the lock state.
    6462                 :             :  */
    6463                 :             : static inline uint64
    6464                 :   114268081 : BufferLockReleaseSub(BufferLockMode mode)
    6465                 :             : {
    6466                 :             :     /*
    6467                 :             :      * Turns out that a switch() leads gcc to generate sufficiently worse code
    6468                 :             :      * for this to show up in profiles...
    6469                 :             :      */
    6470         [ +  + ]:   114268081 :     if (mode == BUFFER_LOCK_EXCLUSIVE)
    6471                 :    38954843 :         return BM_LOCK_VAL_EXCLUSIVE;
    6472         [ +  + ]:    75313238 :     else if (mode == BUFFER_LOCK_SHARE_EXCLUSIVE)
    6473                 :     4130825 :         return BM_LOCK_VAL_SHARE_EXCLUSIVE;
    6474                 :             :     else
    6475                 :             :     {
    6476                 :             :         Assert(mode == BUFFER_LOCK_SHARE);
    6477                 :    71182413 :         return BM_LOCK_VAL_SHARED;
    6478                 :             :     }
    6479                 :             : 
    6480                 :             :     return 0;                   /* keep compiler quiet */
    6481                 :             : }
    6482                 :             : 
    6483                 :             : /*
    6484                 :             :  * Handle work that needs to be done after releasing a lock that was held in
    6485                 :             :  * `mode`, where `lockstate` is the result of the atomic operation modifying
    6486                 :             :  * the state variable.
    6487                 :             :  *
    6488                 :             :  * This is separated from BufferLockUnlock() as we want to combine the lock
    6489                 :             :  * release with other atomic operations when possible, leading to the lock
    6490                 :             :  * release being done in multiple places.
    6491                 :             :  */
    6492                 :             : static void
    6493                 :   114268081 : BufferLockProcessRelease(BufferDesc *buf_hdr, BufferLockMode mode, uint64 lockstate)
    6494                 :             : {
    6495                 :   114268081 :     bool        check_waiters = false;
    6496                 :   114268081 :     bool        wake_exclusive = false;
    6497                 :             : 
    6498                 :             :     /* nobody else can have that kind of lock */
    6499                 :             :     Assert(!(lockstate & BM_LOCK_VAL_EXCLUSIVE));
    6500                 :             : 
    6501                 :             :     /*
    6502                 :             :      * If we're still waiting for backends to get scheduled, don't wake them
    6503                 :             :      * up again. Otherwise check if we need to look through the waitqueue to
    6504                 :             :      * wake other backends.
    6505                 :             :      */
    6506         [ +  + ]:   114268081 :     if ((lockstate & BM_LOCK_HAS_WAITERS) &&
    6507         [ +  + ]:       80148 :         !(lockstate & BM_LOCK_WAKE_IN_PROGRESS))
    6508                 :             :     {
    6509         [ +  + ]:       41930 :         if ((lockstate & BM_LOCK_MASK) == 0)
    6510                 :             :         {
    6511                 :             :             /*
    6512                 :             :              * We released a lock and the lock was, in that moment, free. We
    6513                 :             :              * therefore can wake waiters for any kind of lock.
    6514                 :             :              */
    6515                 :       22112 :             check_waiters = true;
    6516                 :       22112 :             wake_exclusive = true;
    6517                 :             :         }
    6518         [ -  + ]:       19818 :         else if (mode == BUFFER_LOCK_SHARE_EXCLUSIVE)
    6519                 :             :         {
    6520                 :             :             /*
    6521                 :             :              * We released the lock, but another backend still holds a lock.
    6522                 :             :              * We can't have released an exclusive lock, as there couldn't
    6523                 :             :              * have been other lock holders. If we released a share lock, no
    6524                 :             :              * waiters need to be woken up, as there must be other share
    6525                 :             :              * lockers. However, if we held a share-exclusive lock, another
    6526                 :             :              * backend now could acquire a share-exclusive lock.
    6527                 :             :              */
    6528                 :           0 :             check_waiters = true;
    6529                 :           0 :             wake_exclusive = false;
    6530                 :             :         }
    6531                 :             :     }
    6532                 :             : 
    6533                 :             :     /*
    6534                 :             :      * As waking up waiters requires the spinlock to be acquired, only do so
    6535                 :             :      * if necessary.
    6536                 :             :      */
    6537         [ +  + ]:   114268081 :     if (check_waiters)
    6538                 :       22112 :         BufferLockWakeup(buf_hdr, wake_exclusive);
    6539                 :   114268081 : }
    6540                 :             : 
    6541                 :             : /*
    6542                 :             :  * BufferLockHeldByMeInMode - test whether my process holds the content lock
    6543                 :             :  * in the specified mode
    6544                 :             :  *
    6545                 :             :  * This is meant as debug support only.
    6546                 :             :  */
    6547                 :             : static bool
    6548                 :           0 : BufferLockHeldByMeInMode(BufferDesc *buf_hdr, BufferLockMode mode)
    6549                 :             : {
    6550                 :             :     PrivateRefCountEntry *entry =
    6551                 :           0 :         GetPrivateRefCountEntry(BufferDescriptorGetBuffer(buf_hdr), false);
    6552                 :             : 
    6553         [ #  # ]:           0 :     if (!entry)
    6554                 :           0 :         return false;
    6555                 :             :     else
    6556                 :           0 :         return entry->data.lockmode == mode;
    6557                 :             : }
    6558                 :             : 
    6559                 :             : /*
    6560                 :             :  * BufferLockHeldByMe - test whether my process holds the content lock in any
    6561                 :             :  * mode
    6562                 :             :  *
    6563                 :             :  * This is meant as debug support only.
    6564                 :             :  */
    6565                 :             : static bool
    6566                 :           0 : BufferLockHeldByMe(BufferDesc *buf_hdr)
    6567                 :             : {
    6568                 :             :     PrivateRefCountEntry *entry =
    6569                 :           0 :         GetPrivateRefCountEntry(BufferDescriptorGetBuffer(buf_hdr), false);
    6570                 :             : 
    6571         [ #  # ]:           0 :     if (!entry)
    6572                 :           0 :         return false;
    6573                 :             :     else
    6574                 :           0 :         return entry->data.lockmode != BUFFER_LOCK_UNLOCK;
    6575                 :             : }
    6576                 :             : 
    6577                 :             : /*
    6578                 :             :  * Release the content lock for the buffer.
    6579                 :             :  */
    6580                 :             : void
    6581                 :    69387636 : UnlockBuffer(Buffer buffer)
    6582                 :             : {
    6583                 :             :     BufferDesc *buf_hdr;
    6584                 :             : 
    6585                 :             :     Assert(BufferIsPinned(buffer));
    6586         [ +  + ]:    69387636 :     if (BufferIsLocal(buffer))
    6587                 :     5200643 :         return;                 /* local buffers need no lock */
    6588                 :             : 
    6589                 :    64186993 :     buf_hdr = GetBufferDescriptor(buffer - 1);
    6590                 :    64186993 :     BufferLockUnlock(buffer, buf_hdr);
    6591                 :             : }
    6592                 :             : 
    6593                 :             : /*
    6594                 :             :  * Acquire the content_lock for the buffer.
    6595                 :             :  */
    6596                 :             : void
    6597                 :   118242664 : LockBufferInternal(Buffer buffer, BufferLockMode mode)
    6598                 :             : {
    6599                 :             :     BufferDesc *buf_hdr;
    6600                 :             : 
    6601                 :             :     /*
    6602                 :             :      * We can't wait if we haven't got a PGPROC.  This should only occur
    6603                 :             :      * during bootstrap or shared memory initialization.  Put an Assert here
    6604                 :             :      * to catch unsafe coding practices.
    6605                 :             :      */
    6606                 :             :     Assert(!(MyProc == NULL && IsUnderPostmaster));
    6607                 :             : 
    6608                 :             :     /* handled in LockBuffer() wrapper */
    6609                 :             :     Assert(mode != BUFFER_LOCK_UNLOCK);
    6610                 :             : 
    6611                 :             :     Assert(BufferIsPinned(buffer));
    6612         [ +  + ]:   118242664 :     if (BufferIsLocal(buffer))
    6613                 :     6470837 :         return;                 /* local buffers need no lock */
    6614                 :             : 
    6615                 :   111771827 :     buf_hdr = GetBufferDescriptor(buffer - 1);
    6616                 :             : 
    6617                 :             :     /*
    6618                 :             :      * Test the most frequent lock modes first. While a switch (mode) would be
    6619                 :             :      * nice, at least gcc generates considerably worse code for it.
    6620                 :             :      *
    6621                 :             :      * Call BufferLockAcquire() with a constant argument for mode, to generate
    6622                 :             :      * more efficient code for the different lock modes.
    6623                 :             :      */
    6624         [ +  + ]:   111771827 :     if (mode == BUFFER_LOCK_SHARE)
    6625                 :    74590868 :         BufferLockAcquire(buffer, buf_hdr, BUFFER_LOCK_SHARE);
    6626         [ +  - ]:    37180959 :     else if (mode == BUFFER_LOCK_EXCLUSIVE)
    6627                 :    37180959 :         BufferLockAcquire(buffer, buf_hdr, BUFFER_LOCK_EXCLUSIVE);
    6628         [ #  # ]:           0 :     else if (mode == BUFFER_LOCK_SHARE_EXCLUSIVE)
    6629                 :           0 :         BufferLockAcquire(buffer, buf_hdr, BUFFER_LOCK_SHARE_EXCLUSIVE);
    6630                 :             :     else
    6631         [ #  # ]:           0 :         elog(ERROR, "unrecognized buffer lock mode: %d", mode);
    6632                 :             : }
    6633                 :             : 
    6634                 :             : /*
    6635                 :             :  * Acquire the content_lock for the buffer, but only if we don't have to wait.
    6636                 :             :  *
    6637                 :             :  * This assumes the caller wants BUFFER_LOCK_EXCLUSIVE mode.
    6638                 :             :  */
    6639                 :             : bool
    6640                 :     1860654 : ConditionalLockBuffer(Buffer buffer)
    6641                 :             : {
    6642                 :             :     BufferDesc *buf;
    6643                 :             : 
    6644                 :             :     Assert(BufferIsPinned(buffer));
    6645         [ +  + ]:     1860654 :     if (BufferIsLocal(buffer))
    6646                 :       86268 :         return true;            /* act as though we got it */
    6647                 :             : 
    6648                 :     1774386 :     buf = GetBufferDescriptor(buffer - 1);
    6649                 :             : 
    6650                 :     1774386 :     return BufferLockConditional(buffer, buf, BUFFER_LOCK_EXCLUSIVE);
    6651                 :             : }
    6652                 :             : 
    6653                 :             : /*
    6654                 :             :  * Verify that this backend is pinning the buffer exactly once.
    6655                 :             :  *
    6656                 :             :  * NOTE: Like in BufferIsPinned(), what we check here is that *this* backend
    6657                 :             :  * holds a pin on the buffer.  We do not care whether some other backend does.
    6658                 :             :  */
    6659                 :             : void
    6660                 :     2703195 : CheckBufferIsPinnedOnce(Buffer buffer)
    6661                 :             : {
    6662         [ +  + ]:     2703195 :     if (BufferIsLocal(buffer))
    6663                 :             :     {
    6664         [ -  + ]:        1049 :         if (LocalRefCount[-buffer - 1] != 1)
    6665         [ #  # ]:           0 :             elog(ERROR, "incorrect local pin count: %d",
    6666                 :             :                  LocalRefCount[-buffer - 1]);
    6667                 :             :     }
    6668                 :             :     else
    6669                 :             :     {
    6670         [ -  + ]:     2702146 :         if (GetPrivateRefCount(buffer) != 1)
    6671         [ #  # ]:           0 :             elog(ERROR, "incorrect local pin count: %d",
    6672                 :             :                  GetPrivateRefCount(buffer));
    6673                 :             :     }
    6674                 :     2703195 : }
    6675                 :             : 
    6676                 :             : /*
    6677                 :             :  * LockBufferForCleanup - lock a buffer in preparation for deleting items
    6678                 :             :  *
    6679                 :             :  * Items may be deleted from a disk page only when the caller (a) holds an
    6680                 :             :  * exclusive lock on the buffer and (b) has observed that no other backend
    6681                 :             :  * holds a pin on the buffer.  If there is a pin, then the other backend
    6682                 :             :  * might have a pointer into the buffer (for example, a heapscan reference
    6683                 :             :  * to an item --- see README for more details).  It's OK if a pin is added
    6684                 :             :  * after the cleanup starts, however; the newly-arrived backend will be
    6685                 :             :  * unable to look at the page until we release the exclusive lock.
    6686                 :             :  *
    6687                 :             :  * To implement this protocol, a would-be deleter must pin the buffer and
    6688                 :             :  * then call LockBufferForCleanup().  LockBufferForCleanup() is similar to
    6689                 :             :  * LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE), except that it loops until
    6690                 :             :  * it has successfully observed pin count = 1.
    6691                 :             :  */
    6692                 :             : void
    6693                 :       28560 : LockBufferForCleanup(Buffer buffer)
    6694                 :             : {
    6695                 :             :     BufferDesc *bufHdr;
    6696                 :       28560 :     TimestampTz waitStart = 0;
    6697                 :       28560 :     bool        waiting = false;
    6698                 :       28560 :     bool        logged_recovery_conflict = false;
    6699                 :             : 
    6700                 :             :     Assert(BufferIsPinned(buffer));
    6701                 :             :     Assert(PinCountWaitBuf == NULL);
    6702                 :             : 
    6703                 :       28560 :     CheckBufferIsPinnedOnce(buffer);
    6704                 :             : 
    6705                 :             :     /*
    6706                 :             :      * We do not yet need to be worried about in-progress AIOs holding a pin,
    6707                 :             :      * as we, so far, only support doing reads via AIO and this function can
    6708                 :             :      * only be called once the buffer is valid (i.e. no read can be in
    6709                 :             :      * flight).
    6710                 :             :      */
    6711                 :             : 
    6712                 :             :     /* Nobody else to wait for */
    6713         [ +  + ]:       28560 :     if (BufferIsLocal(buffer))
    6714                 :          18 :         return;
    6715                 :             : 
    6716                 :       28542 :     bufHdr = GetBufferDescriptor(buffer - 1);
    6717                 :             : 
    6718                 :             :     for (;;)
    6719                 :          11 :     {
    6720                 :             :         uint64      buf_state;
    6721                 :       28553 :         uint64      unset_bits = 0;
    6722                 :             : 
    6723                 :             :         /* Try to acquire lock */
    6724                 :       28553 :         LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
    6725                 :       28553 :         buf_state = LockBufHdr(bufHdr);
    6726                 :             : 
    6727                 :             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    6728         [ +  + ]:       28553 :         if (BUF_STATE_GET_REFCOUNT(buf_state) == 1)
    6729                 :             :         {
    6730                 :             :             /* Successfully acquired exclusive lock with pincount 1 */
    6731                 :       28542 :             UnlockBufHdr(bufHdr);
    6732                 :       28542 :             goto cleanup_lock_acquired;
    6733                 :             :         }
    6734                 :             :         /* Failed, so mark myself as waiting for pincount 1 */
    6735         [ -  + ]:          11 :         if (buf_state & BM_PIN_COUNT_WAITER)
    6736                 :             :         {
    6737                 :           0 :             UnlockBufHdr(bufHdr);
    6738                 :           0 :             LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
    6739         [ #  # ]:           0 :             elog(ERROR, "multiple backends attempting to wait for pincount 1");
    6740                 :             :         }
    6741                 :          11 :         bufHdr->wait_backend_pgprocno = MyProcNumber;
    6742                 :          11 :         PinCountWaitBuf = bufHdr;
    6743                 :             : 
    6744                 :             :         /*
    6745                 :             :          * Publish BM_PIN_COUNT_WAITER while retaining the buffer header lock.
    6746                 :             :          * The shared refcount can be decremented while BM_LOCKED is set, so
    6747                 :             :          * use an atomic operation that preserves concurrent refcount changes.
    6748                 :             :          */
    6749                 :          11 :         pg_atomic_fetch_or_u64(&bufHdr->state, BM_PIN_COUNT_WAITER);
    6750                 :             : 
    6751                 :             :         /*
    6752                 :             :          * Recheck the refcount after publishing the waiter flag, while shared
    6753                 :             :          * refcount increments are still prevented by BM_LOCKED.  If only our
    6754                 :             :          * pin remains, the cleanup-lock condition has already been satisfied,
    6755                 :             :          * so remove the waiter state and return without sleeping.
    6756                 :             :          */
    6757                 :          11 :         buf_state = pg_atomic_read_u64(&bufHdr->state);
    6758                 :             : 
    6759         [ -  + ]:          11 :         if (BUF_STATE_GET_REFCOUNT(buf_state) == 1)
    6760                 :             :         {
    6761                 :           0 :             UnlockBufHdrExt(bufHdr, buf_state,
    6762                 :             :                             0, BM_PIN_COUNT_WAITER,
    6763                 :             :                             0);
    6764                 :           0 :             PinCountWaitBuf = NULL;
    6765                 :           0 :             goto cleanup_lock_acquired;
    6766                 :             :         }
    6767                 :             : 
    6768                 :          11 :         UnlockBufHdr(bufHdr);
    6769                 :          11 :         LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
    6770                 :             : 
    6771                 :             :         /* Wait to be signaled by UnpinBuffer() */
    6772         [ +  + ]:          11 :         if (InHotStandby)
    6773                 :             :         {
    6774         [ +  + ]:           8 :             if (!waiting)
    6775                 :             :             {
    6776                 :             :                 /* adjust the process title to indicate that it's waiting */
    6777                 :           2 :                 set_ps_display_suffix("waiting");
    6778                 :           2 :                 waiting = true;
    6779                 :             :             }
    6780                 :             : 
    6781                 :             :             /*
    6782                 :             :              * Emit the log message if the startup process is waiting longer
    6783                 :             :              * than deadlock_timeout for recovery conflict on buffer pin.
    6784                 :             :              *
    6785                 :             :              * Skip this if first time through because the startup process has
    6786                 :             :              * not started waiting yet in this case. So, the wait start
    6787                 :             :              * timestamp is set after this logic.
    6788                 :             :              */
    6789   [ +  +  +  + ]:           8 :             if (waitStart != 0 && !logged_recovery_conflict)
    6790                 :             :             {
    6791                 :           3 :                 TimestampTz now = GetCurrentTimestamp();
    6792                 :             : 
    6793         [ +  + ]:           3 :                 if (TimestampDifferenceExceeds(waitStart, now,
    6794                 :             :                                                DeadlockTimeout))
    6795                 :             :                 {
    6796                 :           2 :                     LogRecoveryConflict(RECOVERY_CONFLICT_BUFFERPIN,
    6797                 :             :                                         waitStart, now, NULL, true);
    6798                 :           2 :                     logged_recovery_conflict = true;
    6799                 :             :                 }
    6800                 :             :             }
    6801                 :             : 
    6802                 :             :             /*
    6803                 :             :              * Set the wait start timestamp if logging is enabled and first
    6804                 :             :              * time through.
    6805                 :             :              */
    6806   [ +  -  +  + ]:           8 :             if (log_recovery_conflict_waits && waitStart == 0)
    6807                 :           2 :                 waitStart = GetCurrentTimestamp();
    6808                 :             : 
    6809                 :             :             /* Publish the bufid that Startup process waits on */
    6810                 :           8 :             SetStartupBufferPinWaitBufId(buffer - 1);
    6811                 :             :             /* Set alarm and then wait to be signaled by UnpinBuffer() */
    6812                 :           8 :             ResolveRecoveryConflictWithBufferPin();
    6813                 :             :             /* Reset the published bufid */
    6814                 :           8 :             SetStartupBufferPinWaitBufId(-1);
    6815                 :             :         }
    6816                 :             :         else
    6817                 :           3 :             ProcWaitForSignal(WAIT_EVENT_BUFFER_CLEANUP);
    6818                 :             : 
    6819                 :             :         /*
    6820                 :             :          * Remove flag marking us as waiter. Normally this will not be set
    6821                 :             :          * anymore, but ProcWaitForSignal() can return for other signals as
    6822                 :             :          * well.  We take care to only reset the flag if we're the waiter, as
    6823                 :             :          * theoretically another backend could have started waiting. That's
    6824                 :             :          * impossible with the current usages due to table level locking, but
    6825                 :             :          * better be safe.
    6826                 :             :          */
    6827                 :          11 :         buf_state = LockBufHdr(bufHdr);
    6828         [ +  + ]:          11 :         if ((buf_state & BM_PIN_COUNT_WAITER) != 0 &&
    6829         [ +  - ]:           6 :             bufHdr->wait_backend_pgprocno == MyProcNumber)
    6830                 :           6 :             unset_bits |= BM_PIN_COUNT_WAITER;
    6831                 :             : 
    6832                 :          11 :         UnlockBufHdrExt(bufHdr, buf_state,
    6833                 :             :                         0, unset_bits,
    6834                 :             :                         0);
    6835                 :             : 
    6836                 :          11 :         PinCountWaitBuf = NULL;
    6837                 :             :         /* Loop back and try again */
    6838                 :             :     }
    6839                 :             : 
    6840                 :       28542 : cleanup_lock_acquired:
    6841                 :             : 
    6842                 :             :     /*
    6843                 :             :      * Emit the log message if recovery conflict on buffer pin was resolved
    6844                 :             :      * but the startup process waited longer than deadlock_timeout for it.
    6845                 :             :      */
    6846         [ +  + ]:       28542 :     if (logged_recovery_conflict)
    6847                 :           2 :         LogRecoveryConflict(RECOVERY_CONFLICT_BUFFERPIN,
    6848                 :             :                             waitStart, GetCurrentTimestamp(),
    6849                 :             :                             NULL, false);
    6850                 :             : 
    6851         [ +  + ]:       28542 :     if (waiting)
    6852                 :             :     {
    6853                 :             :         /* reset ps display to remove the suffix if we added one */
    6854                 :           2 :         set_ps_display_remove_suffix();
    6855                 :           2 :         waiting = false;
    6856                 :             :     }
    6857                 :             : 
    6858                 :       28542 :     return;
    6859                 :             : }
    6860                 :             : 
    6861                 :             : /*
    6862                 :             :  * Check called from ProcessRecoveryConflictInterrupts() when Startup process
    6863                 :             :  * requests cancellation of all pin holders that are blocking it.
    6864                 :             :  */
    6865                 :             : bool
    6866                 :           3 : HoldingBufferPinThatDelaysRecovery(void)
    6867                 :             : {
    6868                 :           3 :     int         bufid = GetStartupBufferPinWaitBufId();
    6869                 :             : 
    6870                 :             :     /*
    6871                 :             :      * If we get woken slowly then it's possible that the Startup process was
    6872                 :             :      * already woken by other backends before we got here. Also possible that
    6873                 :             :      * we get here by multiple interrupts or interrupts at inappropriate
    6874                 :             :      * times, so make sure we do nothing if the bufid is not set.
    6875                 :             :      */
    6876         [ +  + ]:           3 :     if (bufid < 0)
    6877                 :           1 :         return false;
    6878                 :             : 
    6879         [ +  - ]:           2 :     if (GetPrivateRefCount(bufid + 1) > 0)
    6880                 :           2 :         return true;
    6881                 :             : 
    6882                 :           0 :     return false;
    6883                 :             : }
    6884                 :             : 
    6885                 :             : /*
    6886                 :             :  * ConditionalLockBufferForCleanup - as above, but don't wait to get the lock
    6887                 :             :  *
    6888                 :             :  * We won't loop, but just check once to see if the pin count is OK.  If
    6889                 :             :  * not, return false with no lock held.
    6890                 :             :  */
    6891                 :             : bool
    6892                 :      619930 : ConditionalLockBufferForCleanup(Buffer buffer)
    6893                 :             : {
    6894                 :             :     BufferDesc *bufHdr;
    6895                 :             :     uint64      buf_state,
    6896                 :             :                 refcount;
    6897                 :             : 
    6898                 :             :     Assert(BufferIsValid(buffer));
    6899                 :             : 
    6900                 :             :     /* see AIO related comment in LockBufferForCleanup() */
    6901                 :             : 
    6902         [ +  + ]:      619930 :     if (BufferIsLocal(buffer))
    6903                 :             :     {
    6904                 :       12373 :         refcount = LocalRefCount[-buffer - 1];
    6905                 :             :         /* There should be exactly one pin */
    6906                 :             :         Assert(refcount > 0);
    6907         [ +  + ]:       12373 :         if (refcount != 1)
    6908                 :        1540 :             return false;
    6909                 :             :         /* Nobody else to wait for */
    6910                 :       10833 :         return true;
    6911                 :             :     }
    6912                 :             : 
    6913                 :             :     /* There should be exactly one local pin */
    6914                 :      607557 :     refcount = GetPrivateRefCount(buffer);
    6915                 :             :     Assert(refcount);
    6916         [ +  + ]:      607557 :     if (refcount != 1)
    6917                 :         495 :         return false;
    6918                 :             : 
    6919                 :             :     /* Try to acquire lock */
    6920         [ +  + ]:      607062 :     if (!ConditionalLockBuffer(buffer))
    6921                 :          71 :         return false;
    6922                 :             : 
    6923                 :      606991 :     bufHdr = GetBufferDescriptor(buffer - 1);
    6924                 :      606991 :     buf_state = LockBufHdr(bufHdr);
    6925                 :      606991 :     refcount = BUF_STATE_GET_REFCOUNT(buf_state);
    6926                 :             : 
    6927                 :             :     Assert(refcount > 0);
    6928         [ +  + ]:      606991 :     if (refcount == 1)
    6929                 :             :     {
    6930                 :             :         /* Successfully acquired exclusive lock with pincount 1 */
    6931                 :      606835 :         UnlockBufHdr(bufHdr);
    6932                 :      606835 :         return true;
    6933                 :             :     }
    6934                 :             : 
    6935                 :             :     /* Failed, so release the lock */
    6936                 :         156 :     UnlockBufHdr(bufHdr);
    6937                 :         156 :     LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
    6938                 :         156 :     return false;
    6939                 :             : }
    6940                 :             : 
    6941                 :             : /*
    6942                 :             :  * IsBufferCleanupOK - as above, but we already have the lock
    6943                 :             :  *
    6944                 :             :  * Check whether it's OK to perform cleanup on a buffer we've already
    6945                 :             :  * locked.  If we observe that the pin count is 1, our exclusive lock
    6946                 :             :  * happens to be a cleanup lock, and we can proceed with anything that
    6947                 :             :  * would have been allowable had we sought a cleanup lock originally.
    6948                 :             :  */
    6949                 :             : bool
    6950                 :        2708 : IsBufferCleanupOK(Buffer buffer)
    6951                 :             : {
    6952                 :             :     BufferDesc *bufHdr;
    6953                 :             :     uint64      buf_state;
    6954                 :             : 
    6955                 :             :     Assert(BufferIsValid(buffer));
    6956                 :             : 
    6957                 :             :     /* see AIO related comment in LockBufferForCleanup() */
    6958                 :             : 
    6959         [ -  + ]:        2708 :     if (BufferIsLocal(buffer))
    6960                 :             :     {
    6961                 :             :         /* There should be exactly one pin */
    6962         [ #  # ]:           0 :         if (LocalRefCount[-buffer - 1] != 1)
    6963                 :           0 :             return false;
    6964                 :             :         /* Nobody else to wait for */
    6965                 :           0 :         return true;
    6966                 :             :     }
    6967                 :             : 
    6968                 :             :     /* There should be exactly one local pin */
    6969         [ -  + ]:        2708 :     if (GetPrivateRefCount(buffer) != 1)
    6970                 :           0 :         return false;
    6971                 :             : 
    6972                 :        2708 :     bufHdr = GetBufferDescriptor(buffer - 1);
    6973                 :             : 
    6974                 :             :     /* caller must hold exclusive lock on buffer */
    6975                 :             :     Assert(BufferIsLockedByMeInMode(buffer, BUFFER_LOCK_EXCLUSIVE));
    6976                 :             : 
    6977                 :        2708 :     buf_state = LockBufHdr(bufHdr);
    6978                 :             : 
    6979                 :             :     Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    6980         [ +  - ]:        2708 :     if (BUF_STATE_GET_REFCOUNT(buf_state) == 1)
    6981                 :             :     {
    6982                 :             :         /* pincount is OK. */
    6983                 :        2708 :         UnlockBufHdr(bufHdr);
    6984                 :        2708 :         return true;
    6985                 :             :     }
    6986                 :             : 
    6987                 :           0 :     UnlockBufHdr(bufHdr);
    6988                 :           0 :     return false;
    6989                 :             : }
    6990                 :             : 
    6991                 :             : /*
    6992                 :             :  * Helper for BufferBeginSetHintBits() and BufferSetHintBits16().
    6993                 :             :  *
    6994                 :             :  * This checks if the current lock mode already suffices to allow hint bits
    6995                 :             :  * being set and, if not, whether the current lock can be upgraded.
    6996                 :             :  *
    6997                 :             :  * Updates *lockstate when returning true.
    6998                 :             :  */
    6999                 :             : static inline bool
    7000                 :    15196141 : SharedBufferBeginSetHintBits(Buffer buffer, BufferDesc *buf_hdr, uint64 *lockstate)
    7001                 :             : {
    7002                 :             :     uint64      old_state;
    7003                 :             :     PrivateRefCountEntry *ref;
    7004                 :             :     BufferLockMode mode;
    7005                 :             : 
    7006                 :    15196141 :     ref = GetPrivateRefCountEntry(buffer, true);
    7007                 :             : 
    7008         [ -  + ]:    15196141 :     if (ref == NULL)
    7009         [ #  # ]:           0 :         elog(ERROR, "buffer is not pinned");
    7010                 :             : 
    7011                 :    15196141 :     mode = ref->data.lockmode;
    7012         [ -  + ]:    15196141 :     if (mode == BUFFER_LOCK_UNLOCK)
    7013         [ #  # ]:           0 :         elog(ERROR, "buffer is not locked");
    7014                 :             : 
    7015                 :             :     /* we're done if we are already holding a sufficient lock level */
    7016   [ +  +  +  + ]:    15196141 :     if (mode == BUFFER_LOCK_EXCLUSIVE || mode == BUFFER_LOCK_SHARE_EXCLUSIVE)
    7017                 :             :     {
    7018                 :    11787517 :         *lockstate = pg_atomic_read_u64(&buf_hdr->state);
    7019                 :    11787517 :         return true;
    7020                 :             :     }
    7021                 :             : 
    7022                 :             :     /*
    7023                 :             :      * We are only holding a share lock right now, try to upgrade it to
    7024                 :             :      * SHARE_EXCLUSIVE.
    7025                 :             :      */
    7026                 :             :     Assert(mode == BUFFER_LOCK_SHARE);
    7027                 :             : 
    7028                 :     3408624 :     old_state = pg_atomic_read_u64(&buf_hdr->state);
    7029                 :             :     while (true)
    7030                 :          12 :     {
    7031                 :             :         uint64      desired_state;
    7032                 :             : 
    7033                 :     3408636 :         desired_state = old_state;
    7034                 :             : 
    7035                 :             :         /*
    7036                 :             :          * Can't upgrade if somebody else holds the lock in exclusive or
    7037                 :             :          * share-exclusive mode.
    7038                 :             :          */
    7039         [ +  + ]:     3408636 :         if (unlikely((old_state & (BM_LOCK_VAL_EXCLUSIVE | BM_LOCK_VAL_SHARE_EXCLUSIVE)) != 0))
    7040                 :             :         {
    7041                 :         169 :             return false;
    7042                 :             :         }
    7043                 :             : 
    7044                 :             :         /* currently held lock state */
    7045                 :     3408467 :         desired_state -= BM_LOCK_VAL_SHARED;
    7046                 :             : 
    7047                 :             :         /* new lock level */
    7048                 :     3408467 :         desired_state += BM_LOCK_VAL_SHARE_EXCLUSIVE;
    7049                 :             : 
    7050         [ +  + ]:     3408467 :         if (likely(pg_atomic_compare_exchange_u64(&buf_hdr->state,
    7051                 :             :                                                   &old_state, desired_state)))
    7052                 :             :         {
    7053                 :     3408455 :             ref->data.lockmode = BUFFER_LOCK_SHARE_EXCLUSIVE;
    7054                 :     3408455 :             *lockstate = desired_state;
    7055                 :             : 
    7056                 :     3408455 :             return true;
    7057                 :             :         }
    7058                 :             :     }
    7059                 :             : }
    7060                 :             : 
    7061                 :             : /*
    7062                 :             :  * Try to acquire the right to set hint bits on the buffer.
    7063                 :             :  *
    7064                 :             :  * To be allowed to set hint bits, this backend needs to hold either a
    7065                 :             :  * share-exclusive or an exclusive lock. In case this backend only holds a
    7066                 :             :  * share lock, this function will try to upgrade the lock to
    7067                 :             :  * share-exclusive. The caller is only allowed to set hint bits if true is
    7068                 :             :  * returned.
    7069                 :             :  *
    7070                 :             :  * Once BufferBeginSetHintBits() has returned true, hint bits may be set
    7071                 :             :  * without further calls to BufferBeginSetHintBits(), until the buffer is
    7072                 :             :  * unlocked.
    7073                 :             :  *
    7074                 :             :  *
    7075                 :             :  * Requiring a share-exclusive lock to set hint bits prevents setting hint
    7076                 :             :  * bits on buffers that are currently being written out, which could corrupt
    7077                 :             :  * the checksum on the page. Flushing buffers also requires a share-exclusive
    7078                 :             :  * lock.
    7079                 :             :  *
    7080                 :             :  * Due to a lock >= share-exclusive being required to set hint bits, only one
    7081                 :             :  * backend can set hint bits at a time. Allowing multiple backends to set hint
    7082                 :             :  * bits would require more complicated locking: For setting hint bits we'd
    7083                 :             :  * need to store the count of backends currently setting hint bits, for I/O we
    7084                 :             :  * would need another lock-level conflicting with the hint-setting
    7085                 :             :  * lock-level. Given that the share-exclusive lock for setting hint bits is
    7086                 :             :  * only held for a short time, that backends often would just set the same
    7087                 :             :  * hint bits and that the cost of occasionally not setting hint bits in hotly
    7088                 :             :  * accessed pages is fairly low, this seems like an acceptable tradeoff.
    7089                 :             :  */
    7090                 :             : bool
    7091                 :      426169 : BufferBeginSetHintBits(Buffer buffer)
    7092                 :             : {
    7093                 :             :     BufferDesc *buf_hdr;
    7094                 :             :     uint64      lockstate;
    7095                 :             : 
    7096         [ +  + ]:      426169 :     if (BufferIsLocal(buffer))
    7097                 :             :     {
    7098                 :             :         /*
    7099                 :             :          * NB: Will need to check if there is a write in progress, once it is
    7100                 :             :          * possible for writes to be done asynchronously.
    7101                 :             :          */
    7102                 :        2405 :         return true;
    7103                 :             :     }
    7104                 :             : 
    7105                 :      423764 :     buf_hdr = GetBufferDescriptor(buffer - 1);
    7106                 :             : 
    7107                 :      423764 :     return SharedBufferBeginSetHintBits(buffer, buf_hdr, &lockstate);
    7108                 :             : }
    7109                 :             : 
    7110                 :             : /*
    7111                 :             :  * End a phase of setting hint bits on this buffer, started with
    7112                 :             :  * BufferBeginSetHintBits().
    7113                 :             :  *
    7114                 :             :  * This would strictly speaking not be required (i.e. the caller could do
    7115                 :             :  * MarkBufferDirtyHint() if so desired), but allows us to perform some sanity
    7116                 :             :  * checks.
    7117                 :             :  */
    7118                 :             : void
    7119                 :      426159 : BufferFinishSetHintBits(Buffer buffer, bool mark_dirty, bool buffer_std)
    7120                 :             : {
    7121                 :             :     if (!BufferIsLocal(buffer))
    7122                 :             :         Assert(BufferIsLockedByMeInMode(buffer, BUFFER_LOCK_SHARE_EXCLUSIVE) ||
    7123                 :             :                BufferIsLockedByMeInMode(buffer, BUFFER_LOCK_EXCLUSIVE));
    7124                 :             : 
    7125         [ +  + ]:      426159 :     if (mark_dirty)
    7126                 :      234307 :         MarkBufferDirtyHint(buffer, buffer_std);
    7127                 :      426159 : }
    7128                 :             : 
    7129                 :             : /*
    7130                 :             :  * Try to set hint bits on a single 16bit value in a buffer.
    7131                 :             :  *
    7132                 :             :  * If hint bits are allowed to be set, set *ptr = val, try to mark the buffer
    7133                 :             :  * dirty and return true. Otherwise false is returned.
    7134                 :             :  *
    7135                 :             :  * *ptr needs to be a pointer to memory within the buffer.
    7136                 :             :  *
    7137                 :             :  * This is a bit faster than BufferBeginSetHintBits() /
    7138                 :             :  * BufferFinishSetHintBits() when setting hints once in a buffer, but slower
    7139                 :             :  * than the former when setting hint bits multiple times in the same buffer.
    7140                 :             :  */
    7141                 :             : bool
    7142                 :    15576038 : BufferSetHintBits16(uint16 *ptr, uint16 val, Buffer buffer)
    7143                 :             : {
    7144                 :             :     BufferDesc *buf_hdr;
    7145                 :             :     uint64      lockstate;
    7146                 :             : #ifdef USE_ASSERT_CHECKING
    7147                 :             :     char       *page;
    7148                 :             : 
    7149                 :             :     /* verify that the address is on the page */
    7150                 :             :     page = BufferGetPage(buffer);
    7151                 :             :     Assert((char *) ptr >= page && (char *) ptr < (page + BLCKSZ));
    7152                 :             : #endif
    7153                 :             : 
    7154         [ +  + ]:    15576038 :     if (BufferIsLocal(buffer))
    7155                 :             :     {
    7156                 :      803661 :         *ptr = val;
    7157                 :             : 
    7158                 :      803661 :         MarkLocalBufferDirty(buffer);
    7159                 :             : 
    7160                 :      803661 :         return true;
    7161                 :             :     }
    7162                 :             : 
    7163                 :    14772377 :     buf_hdr = GetBufferDescriptor(buffer - 1);
    7164                 :             : 
    7165         [ +  + ]:    14772377 :     if (SharedBufferBeginSetHintBits(buffer, buf_hdr, &lockstate))
    7166                 :             :     {
    7167                 :    14772218 :         *ptr = val;
    7168                 :             : 
    7169                 :    14772218 :         MarkSharedBufferDirtyHint(buffer, buf_hdr, lockstate, true);
    7170                 :             : 
    7171                 :    14772218 :         return true;
    7172                 :             :     }
    7173                 :             : 
    7174                 :         159 :     return false;
    7175                 :             : }
    7176                 :             : 
    7177                 :             : 
    7178                 :             : /*
    7179                 :             :  *  Functions for buffer I/O handling
    7180                 :             :  *
    7181                 :             :  *  Also note that these are used only for shared buffers, not local ones.
    7182                 :             :  */
    7183                 :             : 
    7184                 :             : /*
    7185                 :             :  * WaitIO -- Block until the IO_IN_PROGRESS flag on 'buf' is cleared.
    7186                 :             :  */
    7187                 :             : static void
    7188                 :         182 : WaitIO(BufferDesc *buf)
    7189                 :             : {
    7190                 :         182 :     ConditionVariable *cv = BufferDescriptorGetIOCV(buf);
    7191                 :             : 
    7192                 :             :     /*
    7193                 :             :      * Should never end up here with unsubmitted IO, as no AIO unaware code
    7194                 :             :      * may be used while in batch mode and AIO aware code needs to have
    7195                 :             :      * submitted all staged IO to avoid deadlocks & slowness.
    7196                 :             :      */
    7197                 :             :     Assert(!pgaio_have_staged());
    7198                 :             : 
    7199                 :         182 :     ConditionVariablePrepareToSleep(cv);
    7200                 :             :     for (;;)
    7201                 :         182 :     {
    7202                 :             :         uint64      buf_state;
    7203                 :             :         PgAioWaitRef iow;
    7204                 :             : 
    7205                 :             :         /*
    7206                 :             :          * It may not be necessary to acquire the spinlock to check the flag
    7207                 :             :          * here, but since this test is essential for correctness, we'd better
    7208                 :             :          * play it safe.
    7209                 :             :          */
    7210                 :         364 :         buf_state = LockBufHdr(buf);
    7211                 :             : 
    7212                 :             :         /*
    7213                 :             :          * Copy the wait reference while holding the spinlock. This protects
    7214                 :             :          * against a concurrent TerminateBufferIO() in another backend from
    7215                 :             :          * clearing the wref while it's being read.
    7216                 :             :          */
    7217                 :         364 :         iow = buf->io_wref;
    7218                 :         364 :         UnlockBufHdr(buf);
    7219                 :             : 
    7220                 :             :         /* no IO in progress, we don't need to wait */
    7221         [ +  + ]:         364 :         if (!(buf_state & BM_IO_IN_PROGRESS))
    7222                 :         182 :             break;
    7223                 :             : 
    7224                 :             :         /*
    7225                 :             :          * The buffer has asynchronous IO in progress, wait for it to
    7226                 :             :          * complete.
    7227                 :             :          */
    7228         [ +  + ]:         182 :         if (pgaio_wref_valid(&iow))
    7229                 :             :         {
    7230                 :          25 :             pgaio_wref_wait(&iow);
    7231                 :             : 
    7232                 :             :             /*
    7233                 :             :              * The AIO subsystem internally uses condition variables and thus
    7234                 :             :              * might remove this backend from the BufferDesc's CV. While that
    7235                 :             :              * wouldn't cause a correctness issue (the first CV sleep just
    7236                 :             :              * immediately returns if not already registered), it seems worth
    7237                 :             :              * avoiding unnecessary loop iterations, given that we take care
    7238                 :             :              * to do so at the start of the function.
    7239                 :             :              */
    7240                 :          25 :             ConditionVariablePrepareToSleep(cv);
    7241                 :          25 :             continue;
    7242                 :             :         }
    7243                 :             : 
    7244                 :             :         /* wait on BufferDesc->cv, e.g. for concurrent synchronous IO */
    7245                 :         157 :         ConditionVariableSleep(cv, WAIT_EVENT_BUFFER_IO);
    7246                 :             :     }
    7247                 :         182 :     ConditionVariableCancelSleep();
    7248                 :         182 : }
    7249                 :             : 
    7250                 :             : /*
    7251                 :             :  * StartSharedBufferIO: begin I/O on this buffer
    7252                 :             :  *  (Assumptions)
    7253                 :             :  *  The buffer is Pinned
    7254                 :             :  *
    7255                 :             :  * In several scenarios the buffer may already be undergoing I/O in this or
    7256                 :             :  * another backend. How to best handle that depends on the caller's
    7257                 :             :  * situation. It might be appropriate to wait synchronously (e.g., because the
    7258                 :             :  * buffer is about to be invalidated); wait asynchronously, using the buffer's
    7259                 :             :  * IO wait reference (e.g., because the caller is doing readahead and doesn't
    7260                 :             :  * need the buffer to be ready immediately); or to not wait at all (e.g.,
    7261                 :             :  * because the caller is trying to combine IO for this buffer with another
    7262                 :             :  * buffer).
    7263                 :             :  *
    7264                 :             :  * How and whether to wait is controlled by the wait and io_wref
    7265                 :             :  * parameters. In detail:
    7266                 :             :  *
    7267                 :             :  * - If the caller passes a non-NULL io_wref and the buffer has an I/O wait
    7268                 :             :  *   reference, the *io_wref is set to the buffer's io_wref and
    7269                 :             :  *   BUFFER_IO_IN_PROGRESS is returned. This is done regardless of the wait
    7270                 :             :  *   parameter.
    7271                 :             :  *
    7272                 :             :  * - If the caller passes a NULL io_wref (i.e. the caller does not want to
    7273                 :             :  *   asynchronously wait for the completion of the IO), wait = false and the
    7274                 :             :  *   buffer is undergoing IO, BUFFER_IO_IN_PROGRESS is returned.
    7275                 :             :  *
    7276                 :             :  * - If wait = true and either the buffer does not have a wait reference,
    7277                 :             :  *   or the caller passes io_wref = NULL, WaitIO() is used to wait for the IO
    7278                 :             :  *   to complete.  To avoid the potential of deadlocks and unnecessary delays,
    7279                 :             :  *   all staged I/O is submitted before waiting.
    7280                 :             :  *
    7281                 :             :  * Input operations are only attempted on buffers that are not BM_VALID, and
    7282                 :             :  * output operations only on buffers that are BM_VALID and BM_DIRTY, so we can
    7283                 :             :  * always tell if the work is already done.  If no I/O is necessary,
    7284                 :             :  * BUFFER_IO_ALREADY_DONE is returned.
    7285                 :             :  *
    7286                 :             :  * If we successfully marked the buffer as BM_IO_IN_PROGRESS,
    7287                 :             :  * BUFFER_IO_READY_FOR_IO is returned.
    7288                 :             :  */
    7289                 :             : StartBufferIOResult
    7290                 :     2873260 : StartSharedBufferIO(BufferDesc *buf, bool forInput, bool wait, PgAioWaitRef *io_wref)
    7291                 :             : {
    7292                 :             :     uint64      buf_state;
    7293                 :             : 
    7294                 :     2873260 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    7295                 :             : 
    7296                 :             :     for (;;)
    7297                 :             :     {
    7298                 :     2873442 :         buf_state = LockBufHdr(buf);
    7299                 :             : 
    7300         [ +  + ]:     2873442 :         if (!(buf_state & BM_IO_IN_PROGRESS))
    7301                 :     2871242 :             break;
    7302                 :             : 
    7303                 :             :         /* Join the existing IO */
    7304   [ +  +  +  + ]:        2200 :         if (io_wref != NULL && pgaio_wref_valid(&buf->io_wref))
    7305                 :             :         {
    7306                 :        2010 :             *io_wref = buf->io_wref;
    7307                 :        2010 :             UnlockBufHdr(buf);
    7308                 :             : 
    7309                 :        2010 :             return BUFFER_IO_IN_PROGRESS;
    7310                 :             :         }
    7311         [ +  + ]:         190 :         else if (!wait)
    7312                 :             :         {
    7313                 :           8 :             UnlockBufHdr(buf);
    7314                 :           8 :             return BUFFER_IO_IN_PROGRESS;
    7315                 :             :         }
    7316                 :             :         else
    7317                 :             :         {
    7318                 :             :             /*
    7319                 :             :              * With wait = true, we always have to wait if the caller has
    7320                 :             :              * passed io_wref = NULL.
    7321                 :             :              *
    7322                 :             :              * Even with io_wref != NULL, we have to wait if the buffer's wait
    7323                 :             :              * ref is not valid but the IO is in progress, someone else
    7324                 :             :              * started IO but hasn't set the wait ref yet. We have no choice
    7325                 :             :              * but to wait until the IO completes.
    7326                 :             :              */
    7327                 :         182 :             UnlockBufHdr(buf);
    7328                 :             : 
    7329                 :             :             /*
    7330                 :             :              * If this backend currently has staged IO, submit it before
    7331                 :             :              * waiting for in-progress IO, to avoid potential deadlocks and
    7332                 :             :              * unnecessary delays.
    7333                 :             :              */
    7334                 :         182 :             pgaio_submit_staged();
    7335                 :             : 
    7336                 :         182 :             WaitIO(buf);
    7337                 :             :         }
    7338                 :             :     }
    7339                 :             : 
    7340                 :             :     /* Once we get here, there is definitely no I/O active on this buffer */
    7341                 :             : 
    7342                 :             :     /* Check if someone else already did the I/O */
    7343   [ +  +  +  + ]:     2871242 :     if (forInput ? (buf_state & BM_VALID) : !(buf_state & BM_DIRTY))
    7344                 :             :     {
    7345                 :         338 :         UnlockBufHdr(buf);
    7346                 :         338 :         return BUFFER_IO_ALREADY_DONE;
    7347                 :             :     }
    7348                 :             : 
    7349                 :             :     /*
    7350                 :             :      * No IO in progress and not already done; we will start IO. It's possible
    7351                 :             :      * that the IO was in progress but we're not done, because the IO errored
    7352                 :             :      * out. We'll do the IO ourselves.
    7353                 :             :      */
    7354                 :     2870904 :     UnlockBufHdrExt(buf, buf_state,
    7355                 :             :                     BM_IO_IN_PROGRESS, 0,
    7356                 :             :                     0);
    7357                 :             : 
    7358                 :     2870904 :     ResourceOwnerRememberBufferIO(CurrentResourceOwner,
    7359                 :             :                                   BufferDescriptorGetBuffer(buf));
    7360                 :             : 
    7361                 :     2870904 :     return BUFFER_IO_READY_FOR_IO;
    7362                 :             : }
    7363                 :             : 
    7364                 :             : /*
    7365                 :             :  * Wrapper around StartSharedBufferIO / StartLocalBufferIO. Only to be used
    7366                 :             :  * when the caller doesn't otherwise need to care about local vs shared. See
    7367                 :             :  * StartSharedBufferIO() for details.
    7368                 :             :  */
    7369                 :             : StartBufferIOResult
    7370                 :     1584660 : StartBufferIO(Buffer buffer, bool forInput, bool wait, PgAioWaitRef *io_wref)
    7371                 :             : {
    7372                 :             :     BufferDesc *buf_hdr;
    7373                 :             : 
    7374         [ +  + ]:     1584660 :     if (BufferIsLocal(buffer))
    7375                 :             :     {
    7376                 :       11017 :         buf_hdr = GetLocalBufferDescriptor(-buffer - 1);
    7377                 :             : 
    7378                 :       11017 :         return StartLocalBufferIO(buf_hdr, forInput, wait, io_wref);
    7379                 :             :     }
    7380                 :             :     else
    7381                 :             :     {
    7382                 :     1573643 :         buf_hdr = GetBufferDescriptor(buffer - 1);
    7383                 :             : 
    7384                 :     1573643 :         return StartSharedBufferIO(buf_hdr, forInput, wait, io_wref);
    7385                 :             :     }
    7386                 :             : }
    7387                 :             : 
    7388                 :             : /*
    7389                 :             :  * TerminateBufferIO: release a buffer we were doing I/O on
    7390                 :             :  *  (Assumptions)
    7391                 :             :  *  My process is executing IO for the buffer
    7392                 :             :  *  BM_IO_IN_PROGRESS bit is set for the buffer
    7393                 :             :  *  The buffer is Pinned
    7394                 :             :  *
    7395                 :             :  * If clear_dirty is true, we clear the buffer's BM_DIRTY flag.  This is
    7396                 :             :  * appropriate when terminating a successful write.
    7397                 :             :  *
    7398                 :             :  * set_flag_bits gets ORed into the buffer's flags.  It must include
    7399                 :             :  * BM_IO_ERROR in a failure case.  For successful completion it could
    7400                 :             :  * be 0, or BM_VALID if we just finished reading in the page.
    7401                 :             :  *
    7402                 :             :  * If forget_owner is true, we release the buffer I/O from the current
    7403                 :             :  * resource owner. (forget_owner=false is used when the resource owner itself
    7404                 :             :  * is being released)
    7405                 :             :  */
    7406                 :             : void
    7407                 :     2696719 : TerminateBufferIO(BufferDesc *buf, bool clear_dirty, uint64 set_flag_bits,
    7408                 :             :                   bool forget_owner, bool release_aio)
    7409                 :             : {
    7410                 :             :     uint64      buf_state;
    7411                 :     2696719 :     uint64      unset_flag_bits = 0;
    7412                 :     2696719 :     int         refcount_change = 0;
    7413                 :             : 
    7414                 :     2696719 :     buf_state = LockBufHdr(buf);
    7415                 :             : 
    7416                 :             :     Assert(buf_state & BM_IO_IN_PROGRESS);
    7417                 :     2696719 :     unset_flag_bits |= BM_IO_IN_PROGRESS;
    7418                 :             : 
    7419                 :             :     /* Clear earlier errors, if this IO failed, it'll be marked again */
    7420                 :     2696719 :     unset_flag_bits |= BM_IO_ERROR;
    7421                 :             : 
    7422         [ +  + ]:     2696719 :     if (clear_dirty)
    7423                 :      700401 :         unset_flag_bits |= BM_DIRTY | BM_CHECKPOINT_NEEDED;
    7424                 :             : 
    7425         [ +  + ]:     2696719 :     if (release_aio)
    7426                 :             :     {
    7427                 :             :         /* release ownership by the AIO subsystem */
    7428                 :             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    7429                 :     1397207 :         refcount_change = -1;
    7430                 :     1397207 :         pgaio_wref_clear(&buf->io_wref);
    7431                 :             :     }
    7432                 :             : 
    7433                 :     2696719 :     buf_state = UnlockBufHdrExt(buf, buf_state,
    7434                 :             :                                 set_flag_bits, unset_flag_bits,
    7435                 :             :                                 refcount_change);
    7436                 :             : 
    7437         [ +  + ]:     2696719 :     if (forget_owner)
    7438                 :     1299489 :         ResourceOwnerForgetBufferIO(CurrentResourceOwner,
    7439                 :             :                                     BufferDescriptorGetBuffer(buf));
    7440                 :             : 
    7441                 :     2696719 :     ConditionVariableBroadcast(BufferDescriptorGetIOCV(buf));
    7442                 :             : 
    7443                 :             :     /*
    7444                 :             :      * Support LockBufferForCleanup()
    7445                 :             :      *
    7446                 :             :      * We may have just released the last pin other than the waiter's. In most
    7447                 :             :      * cases, this backend holds another pin on the buffer. But, if, for
    7448                 :             :      * example, this backend is completing an IO issued by another backend, it
    7449                 :             :      * may be time to wake the waiter.
    7450                 :             :      */
    7451   [ +  +  -  + ]:     2696719 :     if (release_aio && (buf_state & BM_PIN_COUNT_WAITER))
    7452                 :           0 :         WakePinCountWaiter(buf);
    7453                 :     2696719 : }
    7454                 :             : 
    7455                 :             : /*
    7456                 :             :  * AbortBufferIO: Clean up active buffer I/O after an error.
    7457                 :             :  *
    7458                 :             :  *  All LWLocks & content locks we might have held have been released, but we
    7459                 :             :  *  haven't yet released buffer pins, so the buffer is still pinned.
    7460                 :             :  *
    7461                 :             :  *  If I/O was in progress, we always set BM_IO_ERROR, even though it's
    7462                 :             :  *  possible the error condition wasn't related to the I/O.
    7463                 :             :  *
    7464                 :             :  *  Note: this does not remove the buffer I/O from the resource owner.
    7465                 :             :  *  That's correct when we're releasing the whole resource owner, but
    7466                 :             :  *  beware if you use this in other contexts.
    7467                 :             :  */
    7468                 :             : static void
    7469                 :          15 : AbortBufferIO(Buffer buffer)
    7470                 :             : {
    7471                 :          15 :     BufferDesc *buf_hdr = GetBufferDescriptor(buffer - 1);
    7472                 :             :     uint64      buf_state;
    7473                 :             : 
    7474                 :          15 :     buf_state = LockBufHdr(buf_hdr);
    7475                 :             :     Assert(buf_state & (BM_IO_IN_PROGRESS | BM_TAG_VALID));
    7476                 :             : 
    7477         [ +  - ]:          15 :     if (!(buf_state & BM_VALID))
    7478                 :             :     {
    7479                 :             :         Assert(!(buf_state & BM_DIRTY));
    7480                 :          15 :         UnlockBufHdr(buf_hdr);
    7481                 :             :     }
    7482                 :             :     else
    7483                 :             :     {
    7484                 :             :         Assert(buf_state & BM_DIRTY);
    7485                 :           0 :         UnlockBufHdr(buf_hdr);
    7486                 :             : 
    7487                 :             :         /* Issue notice if this is not the first failure... */
    7488         [ #  # ]:           0 :         if (buf_state & BM_IO_ERROR)
    7489                 :             :         {
    7490                 :             :             /* Buffer is pinned, so we can read tag without spinlock */
    7491         [ #  # ]:           0 :             ereport(WARNING,
    7492                 :             :                     (errcode(ERRCODE_IO_ERROR),
    7493                 :             :                      errmsg("could not write block %u of %s",
    7494                 :             :                             buf_hdr->tag.blockNum,
    7495                 :             :                             relpathperm(BufTagGetRelFileLocator(&buf_hdr->tag),
    7496                 :             :                                         BufTagGetForkNum(&buf_hdr->tag)).str),
    7497                 :             :                      errdetail("Multiple failures --- write error might be permanent.")));
    7498                 :             :         }
    7499                 :             :     }
    7500                 :             : 
    7501                 :          15 :     TerminateBufferIO(buf_hdr, false, BM_IO_ERROR, false, false);
    7502                 :          15 : }
    7503                 :             : 
    7504                 :             : /*
    7505                 :             :  * Error context callback for errors occurring during shared buffer writes.
    7506                 :             :  */
    7507                 :             : static void
    7508                 :          44 : shared_buffer_write_error_callback(void *arg)
    7509                 :             : {
    7510                 :          44 :     BufferDesc *bufHdr = (BufferDesc *) arg;
    7511                 :             : 
    7512                 :             :     /* Buffer is pinned, so we can read the tag without locking the spinlock */
    7513         [ +  - ]:          44 :     if (bufHdr != NULL)
    7514                 :          88 :         errcontext("writing block %u of relation \"%s\"",
    7515                 :             :                    bufHdr->tag.blockNum,
    7516                 :          44 :                    relpathperm(BufTagGetRelFileLocator(&bufHdr->tag),
    7517                 :             :                                BufTagGetForkNum(&bufHdr->tag)).str);
    7518                 :          44 : }
    7519                 :             : 
    7520                 :             : /*
    7521                 :             :  * Error context callback for errors occurring during local buffer writes.
    7522                 :             :  */
    7523                 :             : static void
    7524                 :           0 : local_buffer_write_error_callback(void *arg)
    7525                 :             : {
    7526                 :           0 :     BufferDesc *bufHdr = (BufferDesc *) arg;
    7527                 :             : 
    7528         [ #  # ]:           0 :     if (bufHdr != NULL)
    7529                 :           0 :         errcontext("writing block %u of relation \"%s\"",
    7530                 :             :                    bufHdr->tag.blockNum,
    7531                 :           0 :                    relpathbackend(BufTagGetRelFileLocator(&bufHdr->tag),
    7532                 :             :                                   MyProcNumber,
    7533                 :             :                                   BufTagGetForkNum(&bufHdr->tag)).str);
    7534                 :           0 : }
    7535                 :             : 
    7536                 :             : /*
    7537                 :             :  * RelFileLocator qsort/bsearch comparator; see RelFileLocatorEquals.
    7538                 :             :  */
    7539                 :             : static int
    7540                 :    13255628 : rlocator_comparator(const void *p1, const void *p2)
    7541                 :             : {
    7542                 :    13255628 :     RelFileLocator n1 = *(const RelFileLocator *) p1;
    7543                 :    13255628 :     RelFileLocator n2 = *(const RelFileLocator *) p2;
    7544                 :             : 
    7545         [ +  + ]:    13255628 :     if (n1.relNumber < n2.relNumber)
    7546                 :    13212024 :         return -1;
    7547         [ +  + ]:       43604 :     else if (n1.relNumber > n2.relNumber)
    7548                 :       41786 :         return 1;
    7549                 :             : 
    7550         [ -  + ]:        1818 :     if (n1.dbOid < n2.dbOid)
    7551                 :           0 :         return -1;
    7552         [ -  + ]:        1818 :     else if (n1.dbOid > n2.dbOid)
    7553                 :           0 :         return 1;
    7554                 :             : 
    7555         [ -  + ]:        1818 :     if (n1.spcOid < n2.spcOid)
    7556                 :           0 :         return -1;
    7557         [ -  + ]:        1818 :     else if (n1.spcOid > n2.spcOid)
    7558                 :           0 :         return 1;
    7559                 :             :     else
    7560                 :        1818 :         return 0;
    7561                 :             : }
    7562                 :             : 
    7563                 :             : /*
    7564                 :             :  * Lock buffer header - set BM_LOCKED in buffer state.
    7565                 :             :  */
    7566                 :             : uint64
    7567                 :    28198130 : LockBufHdr(BufferDesc *desc)
    7568                 :             : {
    7569                 :             :     uint64      old_buf_state;
    7570                 :             : 
    7571                 :             :     Assert(!BufferIsLocal(BufferDescriptorGetBuffer(desc)));
    7572                 :             : 
    7573                 :             :     while (true)
    7574                 :             :     {
    7575                 :             :         /*
    7576                 :             :          * Always try once to acquire the lock directly, without setting up
    7577                 :             :          * the spin-delay infrastructure. The work necessary for that shows up
    7578                 :             :          * in profiles and is rarely necessary.
    7579                 :             :          */
    7580                 :    28199035 :         old_buf_state = pg_atomic_fetch_or_u64(&desc->state, BM_LOCKED);
    7581         [ +  + ]:    28199035 :         if (likely(!(old_buf_state & BM_LOCKED)))
    7582                 :    28198130 :             break;              /* got lock */
    7583                 :             : 
    7584                 :             :         /* and then spin without atomic operations until lock is released */
    7585                 :             :         {
    7586                 :             :             SpinDelayStatus delayStatus;
    7587                 :             : 
    7588                 :         905 :             init_local_spin_delay(&delayStatus);
    7589                 :             : 
    7590         [ +  + ]:        3974 :             while (old_buf_state & BM_LOCKED)
    7591                 :             :             {
    7592                 :        3069 :                 perform_spin_delay(&delayStatus);
    7593                 :        3069 :                 old_buf_state = pg_atomic_read_u64(&desc->state);
    7594                 :             :             }
    7595                 :         905 :             finish_spin_delay(&delayStatus);
    7596                 :             :         }
    7597                 :             : 
    7598                 :             :         /*
    7599                 :             :          * Retry. The lock might obviously already be re-acquired by the time
    7600                 :             :          * we're attempting to get it again.
    7601                 :             :          */
    7602                 :             :     }
    7603                 :             : 
    7604                 :    28198130 :     return old_buf_state | BM_LOCKED;
    7605                 :             : }
    7606                 :             : 
    7607                 :             : /*
    7608                 :             :  * Wait until the BM_LOCKED flag isn't set anymore and return the buffer's
    7609                 :             :  * state at that point.
    7610                 :             :  *
    7611                 :             :  * Obviously the buffer could be locked by the time the value is returned, so
    7612                 :             :  * this is primarily useful in CAS style loops.
    7613                 :             :  */
    7614                 :             : pg_noinline uint64
    7615                 :         648 : WaitBufHdrUnlocked(BufferDesc *buf)
    7616                 :             : {
    7617                 :             :     SpinDelayStatus delayStatus;
    7618                 :             :     uint64      buf_state;
    7619                 :             : 
    7620                 :         648 :     init_local_spin_delay(&delayStatus);
    7621                 :             : 
    7622                 :         648 :     buf_state = pg_atomic_read_u64(&buf->state);
    7623                 :             : 
    7624         [ +  + ]:         878 :     while (buf_state & BM_LOCKED)
    7625                 :             :     {
    7626                 :         230 :         perform_spin_delay(&delayStatus);
    7627                 :         230 :         buf_state = pg_atomic_read_u64(&buf->state);
    7628                 :             :     }
    7629                 :             : 
    7630                 :         648 :     finish_spin_delay(&delayStatus);
    7631                 :             : 
    7632                 :         648 :     return buf_state;
    7633                 :             : }
    7634                 :             : 
    7635                 :             : /*
    7636                 :             :  * BufferTag comparator.
    7637                 :             :  */
    7638                 :             : static inline int
    7639                 :           0 : buffertag_comparator(const BufferTag *ba, const BufferTag *bb)
    7640                 :             : {
    7641                 :             :     int         ret;
    7642                 :             :     RelFileLocator rlocatora;
    7643                 :             :     RelFileLocator rlocatorb;
    7644                 :             : 
    7645                 :           0 :     rlocatora = BufTagGetRelFileLocator(ba);
    7646                 :           0 :     rlocatorb = BufTagGetRelFileLocator(bb);
    7647                 :             : 
    7648                 :           0 :     ret = rlocator_comparator(&rlocatora, &rlocatorb);
    7649                 :             : 
    7650         [ #  # ]:           0 :     if (ret != 0)
    7651                 :           0 :         return ret;
    7652                 :             : 
    7653         [ #  # ]:           0 :     if (BufTagGetForkNum(ba) < BufTagGetForkNum(bb))
    7654                 :           0 :         return -1;
    7655         [ #  # ]:           0 :     if (BufTagGetForkNum(ba) > BufTagGetForkNum(bb))
    7656                 :           0 :         return 1;
    7657                 :             : 
    7658         [ #  # ]:           0 :     if (ba->blockNum < bb->blockNum)
    7659                 :           0 :         return -1;
    7660         [ #  # ]:           0 :     if (ba->blockNum > bb->blockNum)
    7661                 :           0 :         return 1;
    7662                 :             : 
    7663                 :           0 :     return 0;
    7664                 :             : }
    7665                 :             : 
    7666                 :             : /*
    7667                 :             :  * Comparator determining the writeout order in a checkpoint.
    7668                 :             :  *
    7669                 :             :  * It is important that tablespaces are compared first, the logic balancing
    7670                 :             :  * writes between tablespaces relies on it.
    7671                 :             :  */
    7672                 :             : static inline int
    7673                 :     3416812 : ckpt_buforder_comparator(const CkptSortItem *a, const CkptSortItem *b)
    7674                 :             : {
    7675                 :             :     /* compare tablespace */
    7676         [ +  + ]:     3416812 :     if (a->tsId < b->tsId)
    7677                 :        9476 :         return -1;
    7678         [ +  + ]:     3407336 :     else if (a->tsId > b->tsId)
    7679                 :       30084 :         return 1;
    7680                 :             :     /* compare relation */
    7681         [ +  + ]:     3377252 :     if (a->relNumber < b->relNumber)
    7682                 :      965396 :         return -1;
    7683         [ +  + ]:     2411856 :     else if (a->relNumber > b->relNumber)
    7684                 :      908471 :         return 1;
    7685                 :             :     /* compare fork */
    7686         [ +  + ]:     1503385 :     else if (a->forkNum < b->forkNum)
    7687                 :       69526 :         return -1;
    7688         [ +  + ]:     1433859 :     else if (a->forkNum > b->forkNum)
    7689                 :       69988 :         return 1;
    7690                 :             :     /* compare block number */
    7691         [ +  + ]:     1363871 :     else if (a->blockNum < b->blockNum)
    7692                 :      664123 :         return -1;
    7693         [ +  + ]:      699748 :     else if (a->blockNum > b->blockNum)
    7694                 :      653248 :         return 1;
    7695                 :             :     /* equal page IDs are unlikely, but not impossible */
    7696                 :       46500 :     return 0;
    7697                 :             : }
    7698                 :             : 
    7699                 :             : /*
    7700                 :             :  * Comparator for a Min-Heap over the per-tablespace checkpoint completion
    7701                 :             :  * progress.
    7702                 :             :  */
    7703                 :             : static int
    7704                 :      281609 : ts_ckpt_progress_comparator(Datum a, Datum b, void *arg)
    7705                 :             : {
    7706                 :      281609 :     CkptTsStatus *sa = (CkptTsStatus *) DatumGetPointer(a);
    7707                 :      281609 :     CkptTsStatus *sb = (CkptTsStatus *) DatumGetPointer(b);
    7708                 :             : 
    7709                 :             :     /* we want a min-heap, so return 1 for the a < b */
    7710         [ +  + ]:      281609 :     if (sa->progress < sb->progress)
    7711                 :      257909 :         return 1;
    7712         [ +  + ]:       23700 :     else if (sa->progress == sb->progress)
    7713                 :        1056 :         return 0;
    7714                 :             :     else
    7715                 :       22644 :         return -1;
    7716                 :             : }
    7717                 :             : 
    7718                 :             : /*
    7719                 :             :  * Initialize a writeback context, discarding potential previous state.
    7720                 :             :  *
    7721                 :             :  * *max_pending is a pointer instead of an immediate value, so the coalesce
    7722                 :             :  * limits can easily changed by the GUC mechanism, and so calling code does
    7723                 :             :  * not have to check the current configuration. A value of 0 means that no
    7724                 :             :  * writeback control will be performed.
    7725                 :             :  */
    7726                 :             : void
    7727                 :        3108 : WritebackContextInit(WritebackContext *context, int *max_pending)
    7728                 :             : {
    7729                 :             :     Assert(*max_pending <= WRITEBACK_MAX_PENDING_FLUSHES);
    7730                 :             : 
    7731                 :        3108 :     context->max_pending = max_pending;
    7732                 :        3108 :     context->nr_pending = 0;
    7733                 :        3108 : }
    7734                 :             : 
    7735                 :             : /*
    7736                 :             :  * Add buffer to list of pending writeback requests.
    7737                 :             :  */
    7738                 :             : void
    7739                 :      696596 : ScheduleBufferTagForWriteback(WritebackContext *wb_context, IOContext io_context,
    7740                 :             :                               BufferTag *tag)
    7741                 :             : {
    7742                 :             :     PendingWriteback *pending;
    7743                 :             : 
    7744                 :             :     /*
    7745                 :             :      * As pg_flush_data() doesn't do anything with fsync disabled, there's no
    7746                 :             :      * point in tracking in that case.
    7747                 :             :      */
    7748         [ +  + ]:      696596 :     if (io_direct_flags & IO_DIRECT_DATA ||
    7749         [ +  + ]:      696077 :         !enableFsync)
    7750                 :      696594 :         return;
    7751                 :             : 
    7752                 :             :     /*
    7753                 :             :      * Add buffer to the pending writeback array, unless writeback control is
    7754                 :             :      * disabled.
    7755                 :             :      */
    7756         [ -  + ]:           2 :     if (*wb_context->max_pending > 0)
    7757                 :             :     {
    7758                 :             :         Assert(*wb_context->max_pending <= WRITEBACK_MAX_PENDING_FLUSHES);
    7759                 :             : 
    7760                 :           0 :         pending = &wb_context->pending_writebacks[wb_context->nr_pending++];
    7761                 :             : 
    7762                 :           0 :         pending->tag = *tag;
    7763                 :             :     }
    7764                 :             : 
    7765                 :             :     /*
    7766                 :             :      * Perform pending flushes if the writeback limit is exceeded. This
    7767                 :             :      * includes the case where previously an item has been added, but control
    7768                 :             :      * is now disabled.
    7769                 :             :      */
    7770         [ +  - ]:           2 :     if (wb_context->nr_pending >= *wb_context->max_pending)
    7771                 :           2 :         IssuePendingWritebacks(wb_context, io_context);
    7772                 :             : }
    7773                 :             : 
    7774                 :             : #define ST_SORT sort_pending_writebacks
    7775                 :             : #define ST_ELEMENT_TYPE PendingWriteback
    7776                 :             : #define ST_COMPARE(a, b) buffertag_comparator(&a->tag, &b->tag)
    7777                 :             : #define ST_SCOPE static
    7778                 :             : #define ST_DEFINE
    7779                 :             : #include "lib/sort_template.h"
    7780                 :             : 
    7781                 :             : /*
    7782                 :             :  * Issue all pending writeback requests, previously scheduled with
    7783                 :             :  * ScheduleBufferTagForWriteback, to the OS.
    7784                 :             :  *
    7785                 :             :  * Because this is only used to improve the OSs IO scheduling we try to never
    7786                 :             :  * error out - it's just a hint.
    7787                 :             :  */
    7788                 :             : void
    7789                 :        1219 : IssuePendingWritebacks(WritebackContext *wb_context, IOContext io_context)
    7790                 :             : {
    7791                 :             :     instr_time  io_start;
    7792                 :             :     int         i;
    7793                 :             : 
    7794         [ +  - ]:        1219 :     if (wb_context->nr_pending == 0)
    7795                 :        1219 :         return;
    7796                 :             : 
    7797                 :             :     /*
    7798                 :             :      * Executing the writes in-order can make them a lot faster, and allows to
    7799                 :             :      * merge writeback requests to consecutive blocks into larger writebacks.
    7800                 :             :      */
    7801                 :           0 :     sort_pending_writebacks(wb_context->pending_writebacks,
    7802                 :           0 :                             wb_context->nr_pending);
    7803                 :             : 
    7804                 :           0 :     io_start = pgstat_prepare_io_time(track_io_timing);
    7805                 :             : 
    7806                 :             :     /*
    7807                 :             :      * Coalesce neighbouring writes, but nothing else. For that we iterate
    7808                 :             :      * through the, now sorted, array of pending flushes, and look forward to
    7809                 :             :      * find all neighbouring (or identical) writes.
    7810                 :             :      */
    7811         [ #  # ]:           0 :     for (i = 0; i < wb_context->nr_pending; i++)
    7812                 :             :     {
    7813                 :             :         PendingWriteback *cur;
    7814                 :             :         PendingWriteback *next;
    7815                 :             :         SMgrRelation reln;
    7816                 :             :         int         ahead;
    7817                 :             :         BufferTag   tag;
    7818                 :             :         RelFileLocator currlocator;
    7819                 :           0 :         Size        nblocks = 1;
    7820                 :             : 
    7821                 :           0 :         cur = &wb_context->pending_writebacks[i];
    7822                 :           0 :         tag = cur->tag;
    7823                 :           0 :         currlocator = BufTagGetRelFileLocator(&tag);
    7824                 :             : 
    7825                 :             :         /*
    7826                 :             :          * Peek ahead, into following writeback requests, to see if they can
    7827                 :             :          * be combined with the current one.
    7828                 :             :          */
    7829         [ #  # ]:           0 :         for (ahead = 0; i + ahead + 1 < wb_context->nr_pending; ahead++)
    7830                 :             :         {
    7831                 :             : 
    7832                 :           0 :             next = &wb_context->pending_writebacks[i + ahead + 1];
    7833                 :             : 
    7834                 :             :             /* different file, stop */
    7835   [ #  #  #  #  :           0 :             if (!RelFileLocatorEquals(currlocator,
                   #  # ]
    7836         [ #  # ]:           0 :                                       BufTagGetRelFileLocator(&next->tag)) ||
    7837                 :           0 :                 BufTagGetForkNum(&cur->tag) != BufTagGetForkNum(&next->tag))
    7838                 :             :                 break;
    7839                 :             : 
    7840                 :             :             /* ok, block queued twice, skip */
    7841         [ #  # ]:           0 :             if (cur->tag.blockNum == next->tag.blockNum)
    7842                 :           0 :                 continue;
    7843                 :             : 
    7844                 :             :             /* only merge consecutive writes */
    7845         [ #  # ]:           0 :             if (cur->tag.blockNum + 1 != next->tag.blockNum)
    7846                 :           0 :                 break;
    7847                 :             : 
    7848                 :           0 :             nblocks++;
    7849                 :           0 :             cur = next;
    7850                 :             :         }
    7851                 :             : 
    7852                 :           0 :         i += ahead;
    7853                 :             : 
    7854                 :             :         /* and finally tell the kernel to write the data to storage */
    7855                 :           0 :         reln = smgropen(currlocator, INVALID_PROC_NUMBER);
    7856                 :           0 :         smgrwriteback(reln, BufTagGetForkNum(&tag), tag.blockNum, nblocks);
    7857                 :             :     }
    7858                 :             : 
    7859                 :             :     /*
    7860                 :             :      * Assume that writeback requests are only issued for buffers containing
    7861                 :             :      * blocks of permanent relations.
    7862                 :             :      */
    7863                 :           0 :     pgstat_count_io_op_time(IOOBJECT_RELATION, io_context,
    7864                 :           0 :                             IOOP_WRITEBACK, io_start, wb_context->nr_pending, 0);
    7865                 :             : 
    7866                 :           0 :     wb_context->nr_pending = 0;
    7867                 :             : }
    7868                 :             : 
    7869                 :             : /* ResourceOwner callbacks */
    7870                 :             : 
    7871                 :             : static void
    7872                 :          15 : ResOwnerReleaseBufferIO(Datum res)
    7873                 :             : {
    7874                 :          15 :     Buffer      buffer = DatumGetInt32(res);
    7875                 :             : 
    7876                 :          15 :     AbortBufferIO(buffer);
    7877                 :          15 : }
    7878                 :             : 
    7879                 :             : static char *
    7880                 :           0 : ResOwnerPrintBufferIO(Datum res)
    7881                 :             : {
    7882                 :           0 :     Buffer      buffer = DatumGetInt32(res);
    7883                 :             : 
    7884                 :           0 :     return psprintf("lost track of buffer IO on buffer %d", buffer);
    7885                 :             : }
    7886                 :             : 
    7887                 :             : /*
    7888                 :             :  * Release buffer as part of resource owner cleanup. This will only be called
    7889                 :             :  * if the buffer is pinned. If this backend held the content lock at the time
    7890                 :             :  * of the error we also need to release that (note that it is not possible to
    7891                 :             :  * hold a content lock without a pin).
    7892                 :             :  */
    7893                 :             : static void
    7894                 :       10732 : ResOwnerReleaseBuffer(Datum res)
    7895                 :             : {
    7896                 :       10732 :     Buffer      buffer = DatumGetInt32(res);
    7897                 :             : 
    7898                 :             :     /* Like ReleaseBuffer, but don't call ResourceOwnerForgetBuffer */
    7899         [ -  + ]:       10732 :     if (!BufferIsValid(buffer))
    7900         [ #  # ]:           0 :         elog(ERROR, "bad buffer ID: %d", buffer);
    7901                 :             : 
    7902         [ +  + ]:       10732 :     if (BufferIsLocal(buffer))
    7903                 :        3995 :         UnpinLocalBufferNoOwner(buffer);
    7904                 :             :     else
    7905                 :             :     {
    7906                 :             :         PrivateRefCountEntry *ref;
    7907                 :             : 
    7908                 :        6737 :         ref = GetPrivateRefCountEntry(buffer, false);
    7909                 :             : 
    7910                 :             :         /* not having a private refcount would imply resowner corruption */
    7911                 :             :         Assert(ref != NULL);
    7912                 :             : 
    7913                 :             :         /*
    7914                 :             :          * If the buffer was locked at the time of the resowner release,
    7915                 :             :          * release the lock now. This should only happen after errors.
    7916                 :             :          */
    7917         [ +  + ]:        6737 :         if (ref->data.lockmode != BUFFER_LOCK_UNLOCK)
    7918                 :             :         {
    7919                 :         114 :             BufferDesc *buf = GetBufferDescriptor(buffer - 1);
    7920                 :             : 
    7921                 :         114 :             HOLD_INTERRUPTS();  /* match the upcoming RESUME_INTERRUPTS */
    7922                 :         114 :             BufferLockUnlock(buffer, buf);
    7923                 :             :         }
    7924                 :             : 
    7925                 :        6737 :         UnpinBufferNoOwner(GetBufferDescriptor(buffer - 1));
    7926                 :             :     }
    7927                 :       10732 : }
    7928                 :             : 
    7929                 :             : static char *
    7930                 :           0 : ResOwnerPrintBuffer(Datum res)
    7931                 :             : {
    7932                 :           0 :     return DebugPrintBufferRefcount(DatumGetInt32(res));
    7933                 :             : }
    7934                 :             : 
    7935                 :             : /*
    7936                 :             :  * Helper function to evict unpinned buffer whose buffer header lock is
    7937                 :             :  * already acquired.
    7938                 :             :  */
    7939                 :             : static bool
    7940                 :        2518 : EvictUnpinnedBufferInternal(BufferDesc *desc, bool *buffer_flushed)
    7941                 :             : {
    7942                 :             :     uint64      buf_state;
    7943                 :             :     bool        result;
    7944                 :             : 
    7945                 :        2518 :     *buffer_flushed = false;
    7946                 :             : 
    7947                 :        2518 :     buf_state = pg_atomic_read_u64(&(desc->state));
    7948                 :             :     Assert(buf_state & BM_LOCKED);
    7949                 :             : 
    7950         [ -  + ]:        2518 :     if ((buf_state & BM_VALID) == 0)
    7951                 :             :     {
    7952                 :           0 :         UnlockBufHdr(desc);
    7953                 :           0 :         return false;
    7954                 :             :     }
    7955                 :             : 
    7956                 :             :     /* Check that it's not pinned already. */
    7957         [ -  + ]:        2518 :     if (BUF_STATE_GET_REFCOUNT(buf_state) > 0)
    7958                 :             :     {
    7959                 :           0 :         UnlockBufHdr(desc);
    7960                 :           0 :         return false;
    7961                 :             :     }
    7962                 :             : 
    7963                 :        2518 :     PinBuffer_Locked(desc);     /* releases spinlock */
    7964                 :             : 
    7965                 :             :     /* If it was dirty, try to clean it once. */
    7966         [ +  + ]:        2518 :     if (buf_state & BM_DIRTY)
    7967                 :             :     {
    7968                 :        1077 :         FlushUnlockedBuffer(desc, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    7969                 :        1077 :         *buffer_flushed = true;
    7970                 :             :     }
    7971                 :             : 
    7972                 :             :     /* This will return false if it becomes dirty or someone else pins it. */
    7973                 :        2518 :     result = InvalidateVictimBuffer(desc);
    7974                 :             : 
    7975                 :        2518 :     UnpinBuffer(desc);
    7976                 :             : 
    7977                 :        2518 :     return result;
    7978                 :             : }
    7979                 :             : 
    7980                 :             : /*
    7981                 :             :  * Try to evict the current block in a shared buffer.
    7982                 :             :  *
    7983                 :             :  * This function is intended for testing/development use only!
    7984                 :             :  *
    7985                 :             :  * To succeed, the buffer must not be pinned on entry, so if the caller had a
    7986                 :             :  * particular block in mind, it might already have been replaced by some other
    7987                 :             :  * block by the time this function runs.  It's also unpinned on return, so the
    7988                 :             :  * buffer might be occupied again by the time control is returned, potentially
    7989                 :             :  * even by the same block.  This inherent raciness without other interlocking
    7990                 :             :  * makes the function unsuitable for non-testing usage.
    7991                 :             :  *
    7992                 :             :  * *buffer_flushed is set to true if the buffer was dirty and has been
    7993                 :             :  * flushed, false otherwise.  However, *buffer_flushed=true does not
    7994                 :             :  * necessarily mean that we flushed the buffer, it could have been flushed by
    7995                 :             :  * someone else.
    7996                 :             :  *
    7997                 :             :  * Returns true if the buffer was valid and it has now been made invalid.
    7998                 :             :  * Returns false if it wasn't valid, if it couldn't be evicted due to a pin,
    7999                 :             :  * or if the buffer becomes dirty again while we're trying to write it out.
    8000                 :             :  */
    8001                 :             : bool
    8002                 :         137 : EvictUnpinnedBuffer(Buffer buf, bool *buffer_flushed)
    8003                 :             : {
    8004                 :             :     BufferDesc *desc;
    8005                 :             : 
    8006                 :             :     Assert(BufferIsValid(buf) && !BufferIsLocal(buf));
    8007                 :             : 
    8008                 :             :     /* Make sure we can pin the buffer. */
    8009                 :         137 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    8010                 :         137 :     ReservePrivateRefCountEntry();
    8011                 :             : 
    8012                 :         137 :     desc = GetBufferDescriptor(buf - 1);
    8013                 :         137 :     LockBufHdr(desc);
    8014                 :             : 
    8015                 :         137 :     return EvictUnpinnedBufferInternal(desc, buffer_flushed);
    8016                 :             : }
    8017                 :             : 
    8018                 :             : /*
    8019                 :             :  * Try to evict all the shared buffers.
    8020                 :             :  *
    8021                 :             :  * This function is intended for testing/development use only! See
    8022                 :             :  * EvictUnpinnedBuffer().
    8023                 :             :  *
    8024                 :             :  * The buffers_* parameters are mandatory and indicate the total count of
    8025                 :             :  * buffers that:
    8026                 :             :  * - buffers_evicted - were evicted
    8027                 :             :  * - buffers_flushed - were flushed
    8028                 :             :  * - buffers_skipped - could not be evicted
    8029                 :             :  */
    8030                 :             : void
    8031                 :           1 : EvictAllUnpinnedBuffers(int32 *buffers_evicted, int32 *buffers_flushed,
    8032                 :             :                         int32 *buffers_skipped)
    8033                 :             : {
    8034                 :           1 :     *buffers_evicted = 0;
    8035                 :           1 :     *buffers_skipped = 0;
    8036                 :           1 :     *buffers_flushed = 0;
    8037                 :             : 
    8038         [ +  + ]:       16385 :     for (int buf = 1; buf <= NBuffers; buf++)
    8039                 :             :     {
    8040                 :       16384 :         BufferDesc *desc = GetBufferDescriptor(buf - 1);
    8041                 :             :         uint64      buf_state;
    8042                 :             :         bool        buffer_flushed;
    8043                 :             : 
    8044         [ -  + ]:       16384 :         CHECK_FOR_INTERRUPTS();
    8045                 :             : 
    8046                 :       16384 :         buf_state = pg_atomic_read_u64(&desc->state);
    8047         [ +  + ]:       16384 :         if (!(buf_state & BM_VALID))
    8048                 :       14351 :             continue;
    8049                 :             : 
    8050                 :        2033 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    8051                 :        2033 :         ReservePrivateRefCountEntry();
    8052                 :             : 
    8053                 :        2033 :         LockBufHdr(desc);
    8054                 :             : 
    8055         [ +  - ]:        2033 :         if (EvictUnpinnedBufferInternal(desc, &buffer_flushed))
    8056                 :        2033 :             (*buffers_evicted)++;
    8057                 :             :         else
    8058                 :           0 :             (*buffers_skipped)++;
    8059                 :             : 
    8060         [ +  + ]:        2033 :         if (buffer_flushed)
    8061                 :         980 :             (*buffers_flushed)++;
    8062                 :             :     }
    8063                 :           1 : }
    8064                 :             : 
    8065                 :             : /*
    8066                 :             :  * Try to evict all the shared buffers containing provided relation's pages.
    8067                 :             :  *
    8068                 :             :  * This function is intended for testing/development use only! See
    8069                 :             :  * EvictUnpinnedBuffer().
    8070                 :             :  *
    8071                 :             :  * The caller must hold at least AccessShareLock on the relation to prevent
    8072                 :             :  * the relation from being dropped.
    8073                 :             :  *
    8074                 :             :  * The buffers_* parameters are mandatory and indicate the total count of
    8075                 :             :  * buffers that:
    8076                 :             :  * - buffers_evicted - were evicted
    8077                 :             :  * - buffers_flushed - were flushed
    8078                 :             :  * - buffers_skipped - could not be evicted
    8079                 :             :  */
    8080                 :             : void
    8081                 :          29 : EvictRelUnpinnedBuffers(Relation rel, int32 *buffers_evicted,
    8082                 :             :                         int32 *buffers_flushed, int32 *buffers_skipped)
    8083                 :             : {
    8084                 :             :     Assert(!RelationUsesLocalBuffers(rel));
    8085                 :             : 
    8086                 :          29 :     *buffers_skipped = 0;
    8087                 :          29 :     *buffers_evicted = 0;
    8088                 :          29 :     *buffers_flushed = 0;
    8089                 :             : 
    8090         [ +  + ]:      475165 :     for (int buf = 1; buf <= NBuffers; buf++)
    8091                 :             :     {
    8092                 :      475136 :         BufferDesc *desc = GetBufferDescriptor(buf - 1);
    8093                 :      475136 :         uint64      buf_state = pg_atomic_read_u64(&(desc->state));
    8094                 :             :         bool        buffer_flushed;
    8095                 :             : 
    8096         [ -  + ]:      475136 :         CHECK_FOR_INTERRUPTS();
    8097                 :             : 
    8098                 :             :         /* An unlocked precheck should be safe and saves some cycles. */
    8099         [ +  + ]:      475136 :         if ((buf_state & BM_VALID) == 0 ||
    8100         [ +  + ]:       63071 :             !BufTagMatchesRelFileLocator(&desc->tag, &rel->rd_locator))
    8101                 :      474788 :             continue;
    8102                 :             : 
    8103                 :             :         /* Make sure we can pin the buffer. */
    8104                 :         348 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    8105                 :         348 :         ReservePrivateRefCountEntry();
    8106                 :             : 
    8107                 :         348 :         buf_state = LockBufHdr(desc);
    8108                 :             : 
    8109                 :             :         /* recheck, could have changed without the lock */
    8110         [ +  - ]:         348 :         if ((buf_state & BM_VALID) == 0 ||
    8111         [ -  + ]:         348 :             !BufTagMatchesRelFileLocator(&desc->tag, &rel->rd_locator))
    8112                 :             :         {
    8113                 :           0 :             UnlockBufHdr(desc);
    8114                 :           0 :             continue;
    8115                 :             :         }
    8116                 :             : 
    8117         [ +  - ]:         348 :         if (EvictUnpinnedBufferInternal(desc, &buffer_flushed))
    8118                 :         348 :             (*buffers_evicted)++;
    8119                 :             :         else
    8120                 :           0 :             (*buffers_skipped)++;
    8121                 :             : 
    8122         [ +  + ]:         348 :         if (buffer_flushed)
    8123                 :          78 :             (*buffers_flushed)++;
    8124                 :             :     }
    8125                 :          29 : }
    8126                 :             : 
    8127                 :             : /*
    8128                 :             :  * Helper function to mark unpinned buffer dirty whose buffer header lock is
    8129                 :             :  * already acquired.
    8130                 :             :  */
    8131                 :             : static bool
    8132                 :          36 : MarkDirtyUnpinnedBufferInternal(Buffer buf, BufferDesc *desc,
    8133                 :             :                                 bool *buffer_already_dirty)
    8134                 :             : {
    8135                 :             :     uint64      buf_state;
    8136                 :          36 :     bool        result = false;
    8137                 :             : 
    8138                 :          36 :     *buffer_already_dirty = false;
    8139                 :             : 
    8140                 :          36 :     buf_state = pg_atomic_read_u64(&(desc->state));
    8141                 :             :     Assert(buf_state & BM_LOCKED);
    8142                 :             : 
    8143         [ +  + ]:          36 :     if ((buf_state & BM_VALID) == 0)
    8144                 :             :     {
    8145                 :           1 :         UnlockBufHdr(desc);
    8146                 :           1 :         return false;
    8147                 :             :     }
    8148                 :             : 
    8149                 :             :     /* Check that it's not pinned already. */
    8150         [ -  + ]:          35 :     if (BUF_STATE_GET_REFCOUNT(buf_state) > 0)
    8151                 :             :     {
    8152                 :           0 :         UnlockBufHdr(desc);
    8153                 :           0 :         return false;
    8154                 :             :     }
    8155                 :             : 
    8156                 :             :     /* Pin the buffer and then release the buffer spinlock */
    8157                 :          35 :     PinBuffer_Locked(desc);
    8158                 :             : 
    8159                 :             :     /* If it was not already dirty, mark it as dirty. */
    8160         [ +  + ]:          35 :     if (!(buf_state & BM_DIRTY))
    8161                 :             :     {
    8162                 :          16 :         BufferLockAcquire(buf, desc, BUFFER_LOCK_EXCLUSIVE);
    8163                 :          16 :         MarkBufferDirty(buf);
    8164                 :          16 :         result = true;
    8165                 :          16 :         BufferLockUnlock(buf, desc);
    8166                 :             :     }
    8167                 :             :     else
    8168                 :          19 :         *buffer_already_dirty = true;
    8169                 :             : 
    8170                 :          35 :     UnpinBuffer(desc);
    8171                 :             : 
    8172                 :          35 :     return result;
    8173                 :             : }
    8174                 :             : 
    8175                 :             : /*
    8176                 :             :  * Try to mark the provided shared buffer as dirty.
    8177                 :             :  *
    8178                 :             :  * This function is intended for testing/development use only!
    8179                 :             :  *
    8180                 :             :  * Same as EvictUnpinnedBuffer() but with MarkBufferDirty() call inside.
    8181                 :             :  *
    8182                 :             :  * The buffer_already_dirty parameter is mandatory and indicate if the buffer
    8183                 :             :  * could not be dirtied because it is already dirty.
    8184                 :             :  *
    8185                 :             :  * Returns true if the buffer has successfully been marked as dirty.
    8186                 :             :  */
    8187                 :             : bool
    8188                 :           1 : MarkDirtyUnpinnedBuffer(Buffer buf, bool *buffer_already_dirty)
    8189                 :             : {
    8190                 :             :     BufferDesc *desc;
    8191                 :           1 :     bool        buffer_dirtied = false;
    8192                 :             : 
    8193                 :             :     Assert(!BufferIsLocal(buf));
    8194                 :             : 
    8195                 :             :     /* Make sure we can pin the buffer. */
    8196                 :           1 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    8197                 :           1 :     ReservePrivateRefCountEntry();
    8198                 :             : 
    8199                 :           1 :     desc = GetBufferDescriptor(buf - 1);
    8200                 :           1 :     LockBufHdr(desc);
    8201                 :             : 
    8202                 :           1 :     buffer_dirtied = MarkDirtyUnpinnedBufferInternal(buf, desc, buffer_already_dirty);
    8203                 :             :     /* Both can not be true at the same time */
    8204                 :             :     Assert(!(buffer_dirtied && *buffer_already_dirty));
    8205                 :             : 
    8206                 :           1 :     return buffer_dirtied;
    8207                 :             : }
    8208                 :             : 
    8209                 :             : /*
    8210                 :             :  * Try to mark all the shared buffers containing provided relation's pages as
    8211                 :             :  * dirty.
    8212                 :             :  *
    8213                 :             :  * This function is intended for testing/development use only! See
    8214                 :             :  * MarkDirtyUnpinnedBuffer().
    8215                 :             :  *
    8216                 :             :  * The buffers_* parameters are mandatory and indicate the total count of
    8217                 :             :  * buffers that:
    8218                 :             :  * - buffers_dirtied - were dirtied
    8219                 :             :  * - buffers_already_dirty - were already dirty
    8220                 :             :  * - buffers_skipped - could not be dirtied because of a reason different
    8221                 :             :  * than a buffer being already dirty.
    8222                 :             :  */
    8223                 :             : void
    8224                 :           1 : MarkDirtyRelUnpinnedBuffers(Relation rel,
    8225                 :             :                             int32 *buffers_dirtied,
    8226                 :             :                             int32 *buffers_already_dirty,
    8227                 :             :                             int32 *buffers_skipped)
    8228                 :             : {
    8229                 :             :     Assert(!RelationUsesLocalBuffers(rel));
    8230                 :             : 
    8231                 :           1 :     *buffers_dirtied = 0;
    8232                 :           1 :     *buffers_already_dirty = 0;
    8233                 :           1 :     *buffers_skipped = 0;
    8234                 :             : 
    8235         [ +  + ]:       16385 :     for (int buf = 1; buf <= NBuffers; buf++)
    8236                 :             :     {
    8237                 :       16384 :         BufferDesc *desc = GetBufferDescriptor(buf - 1);
    8238                 :       16384 :         uint64      buf_state = pg_atomic_read_u64(&(desc->state));
    8239                 :             :         bool        buffer_already_dirty;
    8240                 :             : 
    8241         [ -  + ]:       16384 :         CHECK_FOR_INTERRUPTS();
    8242                 :             : 
    8243                 :             :         /* An unlocked precheck should be safe and saves some cycles. */
    8244         [ +  + ]:       16384 :         if ((buf_state & BM_VALID) == 0 ||
    8245         [ +  - ]:          27 :             !BufTagMatchesRelFileLocator(&desc->tag, &rel->rd_locator))
    8246                 :       16384 :             continue;
    8247                 :             : 
    8248                 :             :         /* Make sure we can pin the buffer. */
    8249                 :           0 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    8250                 :           0 :         ReservePrivateRefCountEntry();
    8251                 :             : 
    8252                 :           0 :         buf_state = LockBufHdr(desc);
    8253                 :             : 
    8254                 :             :         /* recheck, could have changed without the lock */
    8255         [ #  # ]:           0 :         if ((buf_state & BM_VALID) == 0 ||
    8256         [ #  # ]:           0 :             !BufTagMatchesRelFileLocator(&desc->tag, &rel->rd_locator))
    8257                 :             :         {
    8258                 :           0 :             UnlockBufHdr(desc);
    8259                 :           0 :             continue;
    8260                 :             :         }
    8261                 :             : 
    8262         [ #  # ]:           0 :         if (MarkDirtyUnpinnedBufferInternal(buf, desc, &buffer_already_dirty))
    8263                 :           0 :             (*buffers_dirtied)++;
    8264         [ #  # ]:           0 :         else if (buffer_already_dirty)
    8265                 :           0 :             (*buffers_already_dirty)++;
    8266                 :             :         else
    8267                 :           0 :             (*buffers_skipped)++;
    8268                 :             :     }
    8269                 :           1 : }
    8270                 :             : 
    8271                 :             : /*
    8272                 :             :  * Try to mark all the shared buffers as dirty.
    8273                 :             :  *
    8274                 :             :  * This function is intended for testing/development use only! See
    8275                 :             :  * MarkDirtyUnpinnedBuffer().
    8276                 :             :  *
    8277                 :             :  * See MarkDirtyRelUnpinnedBuffers() above for details about the buffers_*
    8278                 :             :  * parameters.
    8279                 :             :  */
    8280                 :             : void
    8281                 :           1 : MarkDirtyAllUnpinnedBuffers(int32 *buffers_dirtied,
    8282                 :             :                             int32 *buffers_already_dirty,
    8283                 :             :                             int32 *buffers_skipped)
    8284                 :             : {
    8285                 :           1 :     *buffers_dirtied = 0;
    8286                 :           1 :     *buffers_already_dirty = 0;
    8287                 :           1 :     *buffers_skipped = 0;
    8288                 :             : 
    8289         [ +  + ]:       16385 :     for (int buf = 1; buf <= NBuffers; buf++)
    8290                 :             :     {
    8291                 :       16384 :         BufferDesc *desc = GetBufferDescriptor(buf - 1);
    8292                 :             :         uint64      buf_state;
    8293                 :             :         bool        buffer_already_dirty;
    8294                 :             : 
    8295         [ -  + ]:       16384 :         CHECK_FOR_INTERRUPTS();
    8296                 :             : 
    8297                 :       16384 :         buf_state = pg_atomic_read_u64(&desc->state);
    8298         [ +  + ]:       16384 :         if (!(buf_state & BM_VALID))
    8299                 :       16349 :             continue;
    8300                 :             : 
    8301                 :          35 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    8302                 :          35 :         ReservePrivateRefCountEntry();
    8303                 :             : 
    8304                 :          35 :         LockBufHdr(desc);
    8305                 :             : 
    8306         [ +  + ]:          35 :         if (MarkDirtyUnpinnedBufferInternal(buf, desc, &buffer_already_dirty))
    8307                 :          16 :             (*buffers_dirtied)++;
    8308         [ +  - ]:          19 :         else if (buffer_already_dirty)
    8309                 :          19 :             (*buffers_already_dirty)++;
    8310                 :             :         else
    8311                 :           0 :             (*buffers_skipped)++;
    8312                 :             :     }
    8313                 :           1 : }
    8314                 :             : 
    8315                 :             : /*
    8316                 :             :  * Generic implementation of the AIO handle staging callback for readv/writev
    8317                 :             :  * on local/shared buffers.
    8318                 :             :  *
    8319                 :             :  * Each readv/writev can target multiple buffers. The buffers have already
    8320                 :             :  * been registered with the IO handle.
    8321                 :             :  *
    8322                 :             :  * To make the IO ready for execution ("staging"), we need to ensure that the
    8323                 :             :  * targeted buffers are in an appropriate state while the IO is ongoing. For
    8324                 :             :  * that the AIO subsystem needs to have its own buffer pin, otherwise an error
    8325                 :             :  * in this backend could lead to this backend's buffer pin being released as
    8326                 :             :  * part of error handling, which in turn could lead to the buffer being
    8327                 :             :  * replaced while IO is ongoing.
    8328                 :             :  */
    8329                 :             : static pg_always_inline void
    8330                 :     1397631 : buffer_stage_common(PgAioHandle *ioh, bool is_write, bool is_temp)
    8331                 :             : {
    8332                 :             :     uint64     *io_data;
    8333                 :             :     uint8       handle_data_len;
    8334                 :             :     PgAioWaitRef io_ref;
    8335                 :     1397631 :     BufferTag   first PG_USED_FOR_ASSERTS_ONLY = {0};
    8336                 :             : 
    8337                 :     1397631 :     io_data = pgaio_io_get_handle_data(ioh, &handle_data_len);
    8338                 :             : 
    8339                 :     1397631 :     pgaio_io_get_wref(ioh, &io_ref);
    8340                 :             : 
    8341                 :             :     /* iterate over all buffers affected by the vectored readv/writev */
    8342         [ +  + ]:     2980086 :     for (int i = 0; i < handle_data_len; i++)
    8343                 :             :     {
    8344                 :     1582455 :         Buffer      buffer = (Buffer) io_data[i];
    8345                 :     1582455 :         BufferDesc *buf_hdr = is_temp ?
    8346                 :       11063 :             GetLocalBufferDescriptor(-buffer - 1)
    8347         [ +  + ]:     1582455 :             : GetBufferDescriptor(buffer - 1);
    8348                 :             :         uint64      buf_state;
    8349                 :             : 
    8350                 :             :         /*
    8351                 :             :          * Check that all the buffers are actually ones that could conceivably
    8352                 :             :          * be done in one IO, i.e. are sequential. This is the last
    8353                 :             :          * buffer-aware code before IO is actually executed and confusion
    8354                 :             :          * about which buffers are targeted by IO can be hard to debug, making
    8355                 :             :          * it worth doing extra-paranoid checks.
    8356                 :             :          */
    8357         [ +  + ]:     1582455 :         if (i == 0)
    8358                 :     1397631 :             first = buf_hdr->tag;
    8359                 :             :         else
    8360                 :             :         {
    8361                 :             :             Assert(buf_hdr->tag.relNumber == first.relNumber);
    8362                 :             :             Assert(buf_hdr->tag.blockNum == first.blockNum + i);
    8363                 :             :         }
    8364                 :             : 
    8365         [ +  + ]:     1582455 :         if (is_temp)
    8366                 :       11063 :             buf_state = pg_atomic_read_u64(&buf_hdr->state);
    8367                 :             :         else
    8368                 :     1571392 :             buf_state = LockBufHdr(buf_hdr);
    8369                 :             : 
    8370                 :             :         /* verify the buffer is in the expected state */
    8371                 :             :         Assert(buf_state & BM_TAG_VALID);
    8372                 :             :         if (is_write)
    8373                 :             :         {
    8374                 :             :             Assert(buf_state & BM_VALID);
    8375                 :             :             Assert(buf_state & BM_DIRTY);
    8376                 :             :         }
    8377                 :             :         else
    8378                 :             :         {
    8379                 :             :             Assert(!(buf_state & BM_VALID));
    8380                 :             :             Assert(!(buf_state & BM_DIRTY));
    8381                 :             :         }
    8382                 :             : 
    8383                 :             :         /* temp buffers don't use BM_IO_IN_PROGRESS */
    8384                 :     1582455 :         if (!is_temp)
    8385                 :             :             Assert(buf_state & BM_IO_IN_PROGRESS);
    8386                 :             : 
    8387                 :             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) >= 1);
    8388                 :             : 
    8389                 :             :         /*
    8390                 :             :          * Reflect that the buffer is now owned by the AIO subsystem.
    8391                 :             :          *
    8392                 :             :          * For local buffers: This can't be done just via LocalRefCount, as
    8393                 :             :          * one might initially think, as this backend could error out while
    8394                 :             :          * AIO is still in progress, releasing all the pins by the backend
    8395                 :             :          * itself.
    8396                 :             :          *
    8397                 :             :          * This pin is released again in TerminateBufferIO().
    8398                 :             :          */
    8399                 :     1582455 :         buf_hdr->io_wref = io_ref;
    8400                 :             : 
    8401         [ +  + ]:     1582455 :         if (is_temp)
    8402                 :             :         {
    8403                 :       11063 :             buf_state += BUF_REFCOUNT_ONE;
    8404                 :       11063 :             pg_atomic_unlocked_write_u64(&buf_hdr->state, buf_state);
    8405                 :             :         }
    8406                 :             :         else
    8407                 :     1571392 :             UnlockBufHdrExt(buf_hdr, buf_state, 0, 0, 1);
    8408                 :             : 
    8409                 :             :         /*
    8410                 :             :          * Ensure the content lock that prevents buffer modifications while
    8411                 :             :          * the buffer is being written out is not released early due to an
    8412                 :             :          * error.
    8413                 :             :          */
    8414   [ -  +  -  - ]:     1582455 :         if (is_write && !is_temp)
    8415                 :             :         {
    8416                 :             :             Assert(BufferLockHeldByMe(buf_hdr));
    8417                 :             : 
    8418                 :             :             /*
    8419                 :             :              * Lock is now owned by AIO subsystem.
    8420                 :             :              */
    8421                 :           0 :             BufferLockDisown(buffer, buf_hdr);
    8422                 :             :         }
    8423                 :             : 
    8424                 :             :         /*
    8425                 :             :          * Stop tracking this buffer via the resowner - the AIO system now
    8426                 :             :          * keeps track.
    8427                 :             :          */
    8428         [ +  + ]:     1582455 :         if (!is_temp)
    8429                 :     1571392 :             ResourceOwnerForgetBufferIO(CurrentResourceOwner, buffer);
    8430                 :             :     }
    8431                 :     1397631 : }
    8432                 :             : 
    8433                 :             : /*
    8434                 :             :  * Decode readv errors as encoded by buffer_readv_encode_error().
    8435                 :             :  */
    8436                 :             : static inline void
    8437                 :         454 : buffer_readv_decode_error(PgAioResult result,
    8438                 :             :                           bool *zeroed_any,
    8439                 :             :                           bool *ignored_any,
    8440                 :             :                           uint8 *zeroed_or_error_count,
    8441                 :             :                           uint8 *checkfail_count,
    8442                 :             :                           uint8 *first_off)
    8443                 :             : {
    8444                 :         454 :     uint32      rem_error = result.error_data;
    8445                 :             : 
    8446                 :             :     /* see static asserts in buffer_readv_encode_error */
    8447                 :             : #define READV_COUNT_BITS    7
    8448                 :             : #define READV_COUNT_MASK    ((1 << READV_COUNT_BITS) - 1)
    8449                 :             : 
    8450                 :         454 :     *zeroed_any = rem_error & 1;
    8451                 :         454 :     rem_error >>= 1;
    8452                 :             : 
    8453                 :         454 :     *ignored_any = rem_error & 1;
    8454                 :         454 :     rem_error >>= 1;
    8455                 :             : 
    8456                 :         454 :     *zeroed_or_error_count = rem_error & READV_COUNT_MASK;
    8457                 :         454 :     rem_error >>= READV_COUNT_BITS;
    8458                 :             : 
    8459                 :         454 :     *checkfail_count = rem_error & READV_COUNT_MASK;
    8460                 :         454 :     rem_error >>= READV_COUNT_BITS;
    8461                 :             : 
    8462                 :         454 :     *first_off = rem_error & READV_COUNT_MASK;
    8463                 :         454 :     rem_error >>= READV_COUNT_BITS;
    8464                 :         454 : }
    8465                 :             : 
    8466                 :             : /*
    8467                 :             :  * Helper to encode errors for buffer_readv_complete()
    8468                 :             :  *
    8469                 :             :  * Errors are encoded as follows:
    8470                 :             :  * - bit 0 indicates whether any page was zeroed (1) or not (0)
    8471                 :             :  * - bit 1 indicates whether any checksum failure was ignored (1) or not (0)
    8472                 :             :  * - next READV_COUNT_BITS bits indicate the number of errored or zeroed pages
    8473                 :             :  * - next READV_COUNT_BITS bits indicate the number of checksum failures
    8474                 :             :  * - next READV_COUNT_BITS bits indicate the first offset of the first page
    8475                 :             :  *   that was errored or zeroed or, if no errors/zeroes, the first ignored
    8476                 :             :  *   checksum
    8477                 :             :  */
    8478                 :             : static inline void
    8479                 :         194 : buffer_readv_encode_error(PgAioResult *result,
    8480                 :             :                           bool is_temp,
    8481                 :             :                           bool zeroed_any,
    8482                 :             :                           bool ignored_any,
    8483                 :             :                           uint8 error_count,
    8484                 :             :                           uint8 zeroed_count,
    8485                 :             :                           uint8 checkfail_count,
    8486                 :             :                           uint8 first_error_off,
    8487                 :             :                           uint8 first_zeroed_off,
    8488                 :             :                           uint8 first_ignored_off)
    8489                 :             : {
    8490                 :             : 
    8491                 :         194 :     uint8       shift = 0;
    8492         [ +  + ]:         194 :     uint8       zeroed_or_error_count =
    8493                 :             :         error_count > 0 ? error_count : zeroed_count;
    8494                 :             :     uint8       first_off;
    8495                 :             : 
    8496                 :             :     StaticAssertDecl(PG_IOV_MAX <= 1 << READV_COUNT_BITS,
    8497                 :             :                      "PG_IOV_MAX is bigger than reserved space for error data");
    8498                 :             :     StaticAssertDecl((1 + 1 + 3 * READV_COUNT_BITS) <= PGAIO_RESULT_ERROR_BITS,
    8499                 :             :                      "PGAIO_RESULT_ERROR_BITS is insufficient for buffer_readv");
    8500                 :             : 
    8501                 :             :     /*
    8502                 :             :      * We only have space to encode one offset - but luckily that's good
    8503                 :             :      * enough. If there is an error, the error is the interesting offset, same
    8504                 :             :      * with a zeroed buffer vs an ignored buffer.
    8505                 :             :      */
    8506         [ +  + ]:         194 :     if (error_count > 0)
    8507                 :          94 :         first_off = first_error_off;
    8508         [ +  + ]:         100 :     else if (zeroed_count > 0)
    8509                 :          82 :         first_off = first_zeroed_off;
    8510                 :             :     else
    8511                 :          18 :         first_off = first_ignored_off;
    8512                 :             : 
    8513                 :             :     Assert(!zeroed_any || error_count == 0);
    8514                 :             : 
    8515                 :         194 :     result->error_data = 0;
    8516                 :             : 
    8517                 :         194 :     result->error_data |= zeroed_any << shift;
    8518                 :         194 :     shift += 1;
    8519                 :             : 
    8520                 :         194 :     result->error_data |= ignored_any << shift;
    8521                 :         194 :     shift += 1;
    8522                 :             : 
    8523                 :         194 :     result->error_data |= ((uint32) zeroed_or_error_count) << shift;
    8524                 :         194 :     shift += READV_COUNT_BITS;
    8525                 :             : 
    8526                 :         194 :     result->error_data |= ((uint32) checkfail_count) << shift;
    8527                 :         194 :     shift += READV_COUNT_BITS;
    8528                 :             : 
    8529                 :         194 :     result->error_data |= ((uint32) first_off) << shift;
    8530                 :         194 :     shift += READV_COUNT_BITS;
    8531                 :             : 
    8532         [ +  + ]:         194 :     result->id = is_temp ? PGAIO_HCB_LOCAL_BUFFER_READV :
    8533                 :             :         PGAIO_HCB_SHARED_BUFFER_READV;
    8534                 :             : 
    8535         [ +  + ]:         194 :     if (error_count > 0)
    8536                 :          94 :         result->status = PGAIO_RS_ERROR;
    8537                 :             :     else
    8538                 :         100 :         result->status = PGAIO_RS_WARNING;
    8539                 :             : 
    8540                 :             :     /*
    8541                 :             :      * The encoding is complicated enough to warrant cross-checking it against
    8542                 :             :      * the decode function.
    8543                 :             :      */
    8544                 :             : #ifdef USE_ASSERT_CHECKING
    8545                 :             :     {
    8546                 :             :         bool        zeroed_any_2,
    8547                 :             :                     ignored_any_2;
    8548                 :             :         uint8       zeroed_or_error_count_2,
    8549                 :             :                     checkfail_count_2,
    8550                 :             :                     first_off_2;
    8551                 :             : 
    8552                 :             :         buffer_readv_decode_error(*result,
    8553                 :             :                                   &zeroed_any_2, &ignored_any_2,
    8554                 :             :                                   &zeroed_or_error_count_2,
    8555                 :             :                                   &checkfail_count_2,
    8556                 :             :                                   &first_off_2);
    8557                 :             :         Assert(zeroed_any == zeroed_any_2);
    8558                 :             :         Assert(ignored_any == ignored_any_2);
    8559                 :             :         Assert(zeroed_or_error_count == zeroed_or_error_count_2);
    8560                 :             :         Assert(checkfail_count == checkfail_count_2);
    8561                 :             :         Assert(first_off == first_off_2);
    8562                 :             :     }
    8563                 :             : #endif
    8564                 :             : 
    8565                 :             : #undef READV_COUNT_BITS
    8566                 :             : #undef READV_COUNT_MASK
    8567                 :         194 : }
    8568                 :             : 
    8569                 :             : /*
    8570                 :             :  * Helper for AIO readv completion callbacks, supporting both shared and temp
    8571                 :             :  * buffers. Gets called once for each buffer in a multi-page read.
    8572                 :             :  */
    8573                 :             : static pg_always_inline void
    8574                 :     1408270 : buffer_readv_complete_one(PgAioTargetData *td, uint8 buf_off, Buffer buffer,
    8575                 :             :                           uint8 flags, bool failed, bool is_temp,
    8576                 :             :                           bool *buffer_invalid,
    8577                 :             :                           bool *failed_checksum,
    8578                 :             :                           bool *ignored_checksum,
    8579                 :             :                           bool *zeroed_buffer)
    8580                 :             : {
    8581                 :     1408270 :     BufferDesc *buf_hdr = is_temp ?
    8582                 :       11063 :         GetLocalBufferDescriptor(-buffer - 1)
    8583         [ +  + ]:     1408270 :         : GetBufferDescriptor(buffer - 1);
    8584                 :     1408270 :     BufferTag   tag = buf_hdr->tag;
    8585                 :     1408270 :     char       *bufdata = BufferGetBlock(buffer);
    8586                 :             :     uint64      set_flag_bits;
    8587                 :             :     int         piv_flags;
    8588                 :             : 
    8589                 :             :     /* check that the buffer is in the expected state for a read */
    8590                 :             : #ifdef USE_ASSERT_CHECKING
    8591                 :             :     {
    8592                 :             :         uint64      buf_state = pg_atomic_read_u64(&buf_hdr->state);
    8593                 :             : 
    8594                 :             :         Assert(buf_state & BM_TAG_VALID);
    8595                 :             :         Assert(!(buf_state & BM_VALID));
    8596                 :             :         /* temp buffers don't use BM_IO_IN_PROGRESS */
    8597                 :             :         if (!is_temp)
    8598                 :             :             Assert(buf_state & BM_IO_IN_PROGRESS);
    8599                 :             :         Assert(!(buf_state & BM_DIRTY));
    8600                 :             :     }
    8601                 :             : #endif
    8602                 :             : 
    8603                 :     1408270 :     *buffer_invalid = false;
    8604                 :     1408270 :     *failed_checksum = false;
    8605                 :     1408270 :     *ignored_checksum = false;
    8606                 :     1408270 :     *zeroed_buffer = false;
    8607                 :             : 
    8608                 :             :     /*
    8609                 :             :      * We ask PageIsVerified() to only log the message about checksum errors,
    8610                 :             :      * as the completion might be run in any backend (or IO workers). We will
    8611                 :             :      * report checksum errors in buffer_readv_report().
    8612                 :             :      */
    8613                 :     1408270 :     piv_flags = PIV_LOG_LOG;
    8614                 :             : 
    8615                 :             :     /* the local zero_damaged_pages may differ from the definer's */
    8616         [ +  + ]:     1408270 :     if (flags & READ_BUFFERS_IGNORE_CHECKSUM_FAILURES)
    8617                 :          38 :         piv_flags |= PIV_IGNORE_CHECKSUM_FAILURE;
    8618                 :             : 
    8619                 :             :     /*
    8620                 :             :      * If the buffers are marked for zero on error, we want to log that in
    8621                 :             :      * case of a checksum failure.
    8622                 :             :      */
    8623         [ +  + ]:     1408270 :     if (flags & READ_BUFFERS_ZERO_ON_ERROR)
    8624                 :       46200 :         piv_flags |= PIV_ZERO_BUFFERS_ON_ERROR;
    8625                 :             : 
    8626                 :             :     /* Check for garbage data. */
    8627         [ +  + ]:     1408270 :     if (!failed)
    8628                 :             :     {
    8629                 :             :         /*
    8630                 :             :          * If the buffer is not currently pinned by this backend, e.g. because
    8631                 :             :          * we're completing this IO after an error, the buffer data will have
    8632                 :             :          * been marked as inaccessible when the buffer was unpinned. The AIO
    8633                 :             :          * subsystem holds a pin, but that doesn't prevent the buffer from
    8634                 :             :          * having been marked as inaccessible. The completion might also be
    8635                 :             :          * executed in a different process.
    8636                 :             :          */
    8637                 :             : #ifdef USE_VALGRIND
    8638                 :             :         if (!BufferIsPinned(buffer))
    8639                 :             :             VALGRIND_MAKE_MEM_DEFINED(bufdata, BLCKSZ);
    8640                 :             : #endif
    8641                 :             : 
    8642         [ +  + ]:     1407498 :         if (!PageIsVerified((Page) bufdata, tag.blockNum, piv_flags,
    8643                 :             :                             failed_checksum))
    8644                 :             :         {
    8645         [ +  + ]:          97 :             if (flags & READ_BUFFERS_ZERO_ON_ERROR)
    8646                 :             :             {
    8647                 :          47 :                 memset(bufdata, 0, BLCKSZ);
    8648                 :          47 :                 *zeroed_buffer = true;
    8649                 :             :             }
    8650                 :             :             else
    8651                 :             :             {
    8652                 :          50 :                 *buffer_invalid = true;
    8653                 :             :                 /* mark buffer as having failed */
    8654                 :          50 :                 failed = true;
    8655                 :             :             }
    8656                 :             :         }
    8657         [ +  + ]:     1407401 :         else if (*failed_checksum)
    8658                 :          12 :             *ignored_checksum = true;
    8659                 :             : 
    8660                 :             :         /* undo what we did above */
    8661                 :             : #ifdef USE_VALGRIND
    8662                 :             :         if (!BufferIsPinned(buffer))
    8663                 :             :             VALGRIND_MAKE_MEM_NOACCESS(bufdata, BLCKSZ);
    8664                 :             : #endif
    8665                 :             : 
    8666                 :             :         /*
    8667                 :             :          * Immediately log a message about the invalid page, but only to the
    8668                 :             :          * server log. The reason to do so immediately is that this may be
    8669                 :             :          * executed in a different backend than the one that originated the
    8670                 :             :          * request. The reason to do so immediately is that the originator
    8671                 :             :          * might not process the query result immediately (because it is busy
    8672                 :             :          * doing another part of query processing) or at all (e.g. if it was
    8673                 :             :          * cancelled or errored out due to another IO also failing). The
    8674                 :             :          * definer of the IO will emit an ERROR or WARNING when processing the
    8675                 :             :          * IO's results
    8676                 :             :          *
    8677                 :             :          * To avoid duplicating the code to emit these log messages, we reuse
    8678                 :             :          * buffer_readv_report().
    8679                 :             :          */
    8680   [ +  +  +  +  :     1407498 :         if (*buffer_invalid || *failed_checksum || *zeroed_buffer)
                   +  + ]
    8681                 :             :         {
    8682                 :         109 :             PgAioResult result_one = {0};
    8683                 :             : 
    8684                 :         109 :             buffer_readv_encode_error(&result_one, is_temp,
    8685                 :         109 :                                       *zeroed_buffer,
    8686                 :         109 :                                       *ignored_checksum,
    8687                 :         109 :                                       *buffer_invalid,
    8688                 :         109 :                                       *zeroed_buffer ? 1 : 0,
    8689                 :         109 :                                       *failed_checksum ? 1 : 0,
    8690                 :             :                                       buf_off, buf_off, buf_off);
    8691                 :         109 :             pgaio_result_report(result_one, td, LOG_SERVER_ONLY);
    8692                 :             :         }
    8693                 :             :     }
    8694                 :             : 
    8695                 :             :     /* Terminate I/O and set BM_VALID. */
    8696         [ +  + ]:     1408270 :     set_flag_bits = failed ? BM_IO_ERROR : BM_VALID;
    8697         [ +  + ]:     1408270 :     if (is_temp)
    8698                 :       11063 :         TerminateLocalBufferIO(buf_hdr, false, set_flag_bits, true);
    8699                 :             :     else
    8700                 :     1397207 :         TerminateBufferIO(buf_hdr, false, set_flag_bits, false, true);
    8701                 :             : 
    8702                 :             :     /*
    8703                 :             :      * Call the BUFFER_READ_DONE tracepoint in the callback, even though the
    8704                 :             :      * callback may not be executed in the same backend that called
    8705                 :             :      * BUFFER_READ_START. The alternative would be to defer calling the
    8706                 :             :      * tracepoint to a later point (e.g. the local completion callback for
    8707                 :             :      * shared buffer reads), which seems even less helpful.
    8708                 :             :      */
    8709                 :             :     TRACE_POSTGRESQL_BUFFER_READ_DONE(tag.forkNum,
    8710                 :             :                                       tag.blockNum,
    8711                 :             :                                       tag.spcOid,
    8712                 :             :                                       tag.dbOid,
    8713                 :             :                                       tag.relNumber,
    8714                 :             :                                       is_temp ? MyProcNumber : INVALID_PROC_NUMBER,
    8715                 :             :                                       false);
    8716                 :     1408270 : }
    8717                 :             : 
    8718                 :             : /*
    8719                 :             :  * Perform completion handling of a single AIO read. This read may cover
    8720                 :             :  * multiple blocks / buffers.
    8721                 :             :  *
    8722                 :             :  * Shared between shared and local buffers, to reduce code duplication.
    8723                 :             :  */
    8724                 :             : static pg_always_inline PgAioResult
    8725                 :     1259745 : buffer_readv_complete(PgAioHandle *ioh, PgAioResult prior_result,
    8726                 :             :                       uint8 cb_data, bool is_temp)
    8727                 :             : {
    8728                 :     1259745 :     PgAioResult result = prior_result;
    8729                 :     1259745 :     PgAioTargetData *td = pgaio_io_get_target_data(ioh);
    8730                 :     1259745 :     uint8       first_error_off = 0;
    8731                 :     1259745 :     uint8       first_zeroed_off = 0;
    8732                 :     1259745 :     uint8       first_ignored_off = 0;
    8733                 :     1259745 :     uint8       error_count = 0;
    8734                 :     1259745 :     uint8       zeroed_count = 0;
    8735                 :     1259745 :     uint8       ignored_count = 0;
    8736                 :     1259745 :     uint8       checkfail_count = 0;
    8737                 :             :     uint64     *io_data;
    8738                 :             :     uint8       handle_data_len;
    8739                 :             : 
    8740                 :             :     if (is_temp)
    8741                 :             :     {
    8742                 :             :         Assert(td->smgr.is_temp);
    8743                 :             :         Assert(pgaio_io_get_owner(ioh) == MyProcNumber);
    8744                 :             :     }
    8745                 :             :     else
    8746                 :             :         Assert(!td->smgr.is_temp);
    8747                 :             : 
    8748                 :             :     /*
    8749                 :             :      * Iterate over all the buffers affected by this IO and call the
    8750                 :             :      * per-buffer completion function for each buffer.
    8751                 :             :      */
    8752                 :     1259745 :     io_data = pgaio_io_get_handle_data(ioh, &handle_data_len);
    8753         [ +  + ]:     2668015 :     for (uint8 buf_off = 0; buf_off < handle_data_len; buf_off++)
    8754                 :             :     {
    8755                 :     1408270 :         Buffer      buf = io_data[buf_off];
    8756                 :             :         bool        failed;
    8757                 :     1408270 :         bool        failed_verification = false;
    8758                 :     1408270 :         bool        failed_checksum = false;
    8759                 :     1408270 :         bool        zeroed_buffer = false;
    8760                 :     1408270 :         bool        ignored_checksum = false;
    8761                 :             : 
    8762                 :             :         Assert(BufferIsValid(buf));
    8763                 :             : 
    8764                 :             :         /*
    8765                 :             :          * If the entire I/O failed on a lower-level, each buffer needs to be
    8766                 :             :          * marked as failed. In case of a partial read, the first few buffers
    8767                 :             :          * may be ok.
    8768                 :             :          */
    8769                 :     1408270 :         failed =
    8770                 :     1408270 :             prior_result.status == PGAIO_RS_ERROR
    8771   [ +  +  +  + ]:     1408270 :             || prior_result.result <= buf_off;
    8772                 :             : 
    8773                 :     1408270 :         buffer_readv_complete_one(td, buf_off, buf, cb_data, failed, is_temp,
    8774                 :             :                                   &failed_verification,
    8775                 :             :                                   &failed_checksum,
    8776                 :             :                                   &ignored_checksum,
    8777                 :             :                                   &zeroed_buffer);
    8778                 :             : 
    8779                 :             :         /*
    8780                 :             :          * Track information about the number of different kinds of error
    8781                 :             :          * conditions across all pages, as there can be multiple pages failing
    8782                 :             :          * verification as part of one IO.
    8783                 :             :          */
    8784   [ +  +  +  -  :     1408270 :         if (failed_verification && !zeroed_buffer && error_count++ == 0)
                   +  + ]
    8785                 :          44 :             first_error_off = buf_off;
    8786   [ +  +  +  + ]:     1408270 :         if (zeroed_buffer && zeroed_count++ == 0)
    8787                 :          35 :             first_zeroed_off = buf_off;
    8788   [ +  +  +  + ]:     1408270 :         if (ignored_checksum && ignored_count++ == 0)
    8789                 :          10 :             first_ignored_off = buf_off;
    8790         [ +  + ]:     1408270 :         if (failed_checksum)
    8791                 :          33 :             checkfail_count++;
    8792                 :             :     }
    8793                 :             : 
    8794                 :             :     /*
    8795                 :             :      * If the smgr read succeeded [partially] and page verification failed for
    8796                 :             :      * some of the pages, adjust the IO's result state appropriately.
    8797                 :             :      */
    8798   [ +  +  +  + ]:     1259745 :     if (prior_result.status != PGAIO_RS_ERROR &&
    8799   [ +  +  +  + ]:     1259690 :         (error_count > 0 || ignored_count > 0 || zeroed_count > 0))
    8800                 :             :     {
    8801                 :          85 :         buffer_readv_encode_error(&result, is_temp,
    8802                 :             :                                   zeroed_count > 0, ignored_count > 0,
    8803                 :             :                                   error_count, zeroed_count, checkfail_count,
    8804                 :             :                                   first_error_off, first_zeroed_off,
    8805                 :             :                                   first_ignored_off);
    8806                 :          85 :         pgaio_result_report(result, td, DEBUG1);
    8807                 :             :     }
    8808                 :             : 
    8809                 :             :     /*
    8810                 :             :      * For shared relations this reporting is done in
    8811                 :             :      * shared_buffer_readv_complete_local().
    8812                 :             :      */
    8813   [ +  +  +  + ]:     1259745 :     if (is_temp && checkfail_count > 0)
    8814                 :           2 :         pgstat_report_checksum_failures_in_db(td->smgr.rlocator.dbOid,
    8815                 :             :                                               checkfail_count);
    8816                 :             : 
    8817                 :     1259745 :     return result;
    8818                 :             : }
    8819                 :             : 
    8820                 :             : /*
    8821                 :             :  * AIO error reporting callback for aio_shared_buffer_readv_cb and
    8822                 :             :  * aio_local_buffer_readv_cb.
    8823                 :             :  *
    8824                 :             :  * The error is encoded / decoded in buffer_readv_encode_error() /
    8825                 :             :  * buffer_readv_decode_error().
    8826                 :             :  */
    8827                 :             : static void
    8828                 :         275 : buffer_readv_report(PgAioResult result, const PgAioTargetData *td,
    8829                 :             :                     int elevel)
    8830                 :             : {
    8831                 :         275 :     int         nblocks = td->smgr.nblocks;
    8832                 :         275 :     BlockNumber first = td->smgr.blockNum;
    8833                 :         275 :     BlockNumber last = first + nblocks - 1;
    8834                 :         275 :     ProcNumber  errProc =
    8835         [ +  + ]:         275 :         td->smgr.is_temp ? MyProcNumber : INVALID_PROC_NUMBER;
    8836                 :             :     RelPathStr  rpath =
    8837                 :         275 :         relpathbackend(td->smgr.rlocator, errProc, td->smgr.forkNum);
    8838                 :             :     bool        zeroed_any,
    8839                 :             :                 ignored_any;
    8840                 :             :     uint8       zeroed_or_error_count,
    8841                 :             :                 checkfail_count,
    8842                 :             :                 first_off;
    8843                 :             :     uint8       affected_count;
    8844                 :             :     const char *msg_one,
    8845                 :             :                *msg_mult,
    8846                 :             :                *det_mult,
    8847                 :             :                *hint_mult;
    8848                 :             : 
    8849                 :         275 :     buffer_readv_decode_error(result, &zeroed_any, &ignored_any,
    8850                 :             :                               &zeroed_or_error_count,
    8851                 :             :                               &checkfail_count,
    8852                 :             :                               &first_off);
    8853                 :             : 
    8854                 :             :     /*
    8855                 :             :      * Treat a read that had both zeroed buffers *and* ignored checksums as a
    8856                 :             :      * special case, it's too irregular to be emitted the same way as the
    8857                 :             :      * other cases.
    8858                 :             :      */
    8859   [ +  +  +  + ]:         275 :     if (zeroed_any && ignored_any)
    8860                 :             :     {
    8861                 :             :         Assert(zeroed_any && ignored_any);
    8862                 :             :         Assert(nblocks > 1); /* same block can't be both zeroed and ignored */
    8863                 :             :         Assert(result.status != PGAIO_RS_ERROR);
    8864                 :           4 :         affected_count = zeroed_or_error_count;
    8865                 :             : 
    8866   [ +  -  +  - ]:           4 :         ereport(elevel,
    8867                 :             :                 errcode(ERRCODE_DATA_CORRUPTED),
    8868                 :             :                 errmsg("zeroing %u page(s) and ignoring %u checksum failure(s) among blocks %u..%u of relation \"%s\"",
    8869                 :             :                        affected_count, checkfail_count, first, last, rpath.str),
    8870                 :             :                 affected_count > 1 ?
    8871                 :             :                 errdetail("Block %u held the first zeroed page.",
    8872                 :             :                           first + first_off) : 0,
    8873                 :             :                 errhint_plural("See server log for details about the other %d invalid block.",
    8874                 :             :                                "See server log for details about the other %d invalid blocks.",
    8875                 :             :                                affected_count + checkfail_count - 1,
    8876                 :             :                                affected_count + checkfail_count - 1));
    8877                 :           4 :         return;
    8878                 :             :     }
    8879                 :             : 
    8880                 :             :     /*
    8881                 :             :      * The other messages are highly repetitive. To avoid duplicating a long
    8882                 :             :      * and complicated ereport(), gather the translated format strings
    8883                 :             :      * separately and then do one common ereport.
    8884                 :             :      */
    8885         [ +  + ]:         271 :     if (result.status == PGAIO_RS_ERROR)
    8886                 :             :     {
    8887                 :             :         Assert(!zeroed_any);    /* can't have invalid pages when zeroing them */
    8888                 :         136 :         affected_count = zeroed_or_error_count;
    8889                 :         136 :         msg_one = _("invalid page in block %u of relation \"%s\"");
    8890                 :         136 :         msg_mult = _("%u invalid pages among blocks %u..%u of relation \"%s\"");
    8891                 :         136 :         det_mult = _("Block %u held the first invalid page.");
    8892                 :         136 :         hint_mult = _("See server log for the other %u invalid block(s).");
    8893                 :             :     }
    8894   [ +  +  +  - ]:         135 :     else if (zeroed_any && !ignored_any)
    8895                 :             :     {
    8896                 :         111 :         affected_count = zeroed_or_error_count;
    8897                 :         111 :         msg_one = _("invalid page in block %u of relation \"%s\"; zeroing out page");
    8898                 :         111 :         msg_mult = _("zeroing out %u invalid pages among blocks %u..%u of relation \"%s\"");
    8899                 :         111 :         det_mult = _("Block %u held the first zeroed page.");
    8900                 :         111 :         hint_mult = _("See server log for the other %u zeroed block(s).");
    8901                 :             :     }
    8902   [ +  -  +  - ]:          24 :     else if (!zeroed_any && ignored_any)
    8903                 :             :     {
    8904                 :          24 :         affected_count = checkfail_count;
    8905                 :          24 :         msg_one = _("ignoring checksum failure in block %u of relation \"%s\"");
    8906                 :          24 :         msg_mult = _("ignoring %u checksum failures among blocks %u..%u of relation \"%s\"");
    8907                 :          24 :         det_mult = _("Block %u held the first ignored page.");
    8908                 :          24 :         hint_mult = _("See server log for the other %u ignored block(s).");
    8909                 :             :     }
    8910                 :             :     else
    8911                 :           0 :         pg_unreachable();
    8912                 :             : 
    8913   [ +  +  +  +  :         271 :     ereport(elevel,
             +  +  +  + ]
    8914                 :             :             errcode(ERRCODE_DATA_CORRUPTED),
    8915                 :             :             affected_count == 1 ?
    8916                 :             :             errmsg_internal(msg_one, first + first_off, rpath.str) :
    8917                 :             :             errmsg_internal(msg_mult, affected_count, first, last, rpath.str),
    8918                 :             :             affected_count > 1 ? errdetail_internal(det_mult, first + first_off) : 0,
    8919                 :             :             affected_count > 1 ? errhint_internal(hint_mult, affected_count - 1) : 0);
    8920                 :             : }
    8921                 :             : 
    8922                 :             : static void
    8923                 :     1394784 : shared_buffer_readv_stage(PgAioHandle *ioh, uint8 cb_data)
    8924                 :             : {
    8925                 :     1394784 :     buffer_stage_common(ioh, false, false);
    8926                 :     1394784 : }
    8927                 :             : 
    8928                 :             : static PgAioResult
    8929                 :     1256898 : shared_buffer_readv_complete(PgAioHandle *ioh, PgAioResult prior_result,
    8930                 :             :                              uint8 cb_data)
    8931                 :             : {
    8932                 :     1256898 :     return buffer_readv_complete(ioh, prior_result, cb_data, false);
    8933                 :             : }
    8934                 :             : 
    8935                 :             : /*
    8936                 :             :  * We need a backend-local completion callback for shared buffers, to be able
    8937                 :             :  * to report checksum errors correctly. Unfortunately that can only safely
    8938                 :             :  * happen if the reporting backend has previously called
    8939                 :             :  * pgstat_prepare_report_checksum_failure(), which we can only guarantee in
    8940                 :             :  * the backend that started the IO. Hence this callback.
    8941                 :             :  */
    8942                 :             : static PgAioResult
    8943                 :     1394784 : shared_buffer_readv_complete_local(PgAioHandle *ioh, PgAioResult prior_result,
    8944                 :             :                                    uint8 cb_data)
    8945                 :             : {
    8946                 :             :     bool        zeroed_any,
    8947                 :             :                 ignored_any;
    8948                 :             :     uint8       zeroed_or_error_count,
    8949                 :             :                 checkfail_count,
    8950                 :             :                 first_off;
    8951                 :             : 
    8952         [ +  + ]:     1394784 :     if (prior_result.status == PGAIO_RS_OK)
    8953                 :     1394605 :         return prior_result;
    8954                 :             : 
    8955                 :         179 :     buffer_readv_decode_error(prior_result,
    8956                 :             :                               &zeroed_any,
    8957                 :             :                               &ignored_any,
    8958                 :             :                               &zeroed_or_error_count,
    8959                 :             :                               &checkfail_count,
    8960                 :             :                               &first_off);
    8961                 :             : 
    8962         [ +  + ]:         179 :     if (checkfail_count)
    8963                 :             :     {
    8964                 :          25 :         PgAioTargetData *td = pgaio_io_get_target_data(ioh);
    8965                 :             : 
    8966                 :          25 :         pgstat_report_checksum_failures_in_db(td->smgr.rlocator.dbOid,
    8967                 :             :                                               checkfail_count);
    8968                 :             :     }
    8969                 :             : 
    8970                 :         179 :     return prior_result;
    8971                 :             : }
    8972                 :             : 
    8973                 :             : static void
    8974                 :        2847 : local_buffer_readv_stage(PgAioHandle *ioh, uint8 cb_data)
    8975                 :             : {
    8976                 :        2847 :     buffer_stage_common(ioh, false, true);
    8977                 :        2847 : }
    8978                 :             : 
    8979                 :             : static PgAioResult
    8980                 :        2847 : local_buffer_readv_complete(PgAioHandle *ioh, PgAioResult prior_result,
    8981                 :             :                             uint8 cb_data)
    8982                 :             : {
    8983                 :        2847 :     return buffer_readv_complete(ioh, prior_result, cb_data, true);
    8984                 :             : }
    8985                 :             : 
    8986                 :             : /* readv callback is passed READ_BUFFERS_* flags as callback data */
    8987                 :             : const PgAioHandleCallbacks aio_shared_buffer_readv_cb = {
    8988                 :             :     .stage = shared_buffer_readv_stage,
    8989                 :             :     .complete_shared = shared_buffer_readv_complete,
    8990                 :             :     /* need a local callback to report checksum failures */
    8991                 :             :     .complete_local = shared_buffer_readv_complete_local,
    8992                 :             :     .report = buffer_readv_report,
    8993                 :             : };
    8994                 :             : 
    8995                 :             : /* readv callback is passed READ_BUFFERS_* flags as callback data */
    8996                 :             : const PgAioHandleCallbacks aio_local_buffer_readv_cb = {
    8997                 :             :     .stage = local_buffer_readv_stage,
    8998                 :             : 
    8999                 :             :     /*
    9000                 :             :      * Note that this, in contrast to the shared_buffers case, uses
    9001                 :             :      * complete_local, as only the issuing backend has access to the required
    9002                 :             :      * datastructures. This is important in case the IO completion may be
    9003                 :             :      * consumed incidentally by another backend.
    9004                 :             :      */
    9005                 :             :     .complete_local = local_buffer_readv_complete,
    9006                 :             :     .report = buffer_readv_report,
    9007                 :             : };
        

Generated by: LCOV version 2.0-1