LCOV - code coverage report
Current view: top level - src/backend/storage/buffer - bufmgr.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 1638 1788 91.6 %
Date: 2025-04-02 10:15:58 Functions: 106 112 94.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * bufmgr.c
       4             :  *    buffer manager interface routines
       5             :  *
       6             :  * Portions Copyright (c) 1996-2025, 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             : #include "catalog/storage.h"
      44             : #include "catalog/storage_xlog.h"
      45             : #include "executor/instrument.h"
      46             : #include "lib/binaryheap.h"
      47             : #include "miscadmin.h"
      48             : #include "pg_trace.h"
      49             : #include "pgstat.h"
      50             : #include "postmaster/bgwriter.h"
      51             : #include "storage/aio.h"
      52             : #include "storage/buf_internals.h"
      53             : #include "storage/bufmgr.h"
      54             : #include "storage/fd.h"
      55             : #include "storage/ipc.h"
      56             : #include "storage/lmgr.h"
      57             : #include "storage/proc.h"
      58             : #include "storage/read_stream.h"
      59             : #include "storage/smgr.h"
      60             : #include "storage/standby.h"
      61             : #include "utils/memdebug.h"
      62             : #include "utils/ps_status.h"
      63             : #include "utils/rel.h"
      64             : #include "utils/resowner.h"
      65             : #include "utils/timestamp.h"
      66             : 
      67             : 
      68             : /* Note: these two macros only work on shared buffers, not local ones! */
      69             : #define BufHdrGetBlock(bufHdr)  ((Block) (BufferBlocks + ((Size) (bufHdr)->buf_id) * BLCKSZ))
      70             : #define BufferGetLSN(bufHdr)    (PageGetLSN(BufHdrGetBlock(bufHdr)))
      71             : 
      72             : /* Note: this macro only works on local buffers, not shared ones! */
      73             : #define LocalBufHdrGetBlock(bufHdr) \
      74             :     LocalBufferBlockPointers[-((bufHdr)->buf_id + 2)]
      75             : 
      76             : /* Bits in SyncOneBuffer's return value */
      77             : #define BUF_WRITTEN             0x01
      78             : #define BUF_REUSABLE            0x02
      79             : 
      80             : #define RELS_BSEARCH_THRESHOLD      20
      81             : 
      82             : /*
      83             :  * This is the size (in the number of blocks) above which we scan the
      84             :  * entire buffer pool to remove the buffers for all the pages of relation
      85             :  * being dropped. For the relations with size below this threshold, we find
      86             :  * the buffers by doing lookups in BufMapping table.
      87             :  */
      88             : #define BUF_DROP_FULL_SCAN_THRESHOLD        (uint64) (NBuffers / 32)
      89             : 
      90             : typedef struct PrivateRefCountEntry
      91             : {
      92             :     Buffer      buffer;
      93             :     int32       refcount;
      94             : } PrivateRefCountEntry;
      95             : 
      96             : /* 64 bytes, about the size of a cache line on common systems */
      97             : #define REFCOUNT_ARRAY_ENTRIES 8
      98             : 
      99             : /*
     100             :  * Status of buffers to checkpoint for a particular tablespace, used
     101             :  * internally in BufferSync.
     102             :  */
     103             : typedef struct CkptTsStatus
     104             : {
     105             :     /* oid of the tablespace */
     106             :     Oid         tsId;
     107             : 
     108             :     /*
     109             :      * Checkpoint progress for this tablespace. To make progress comparable
     110             :      * between tablespaces the progress is, for each tablespace, measured as a
     111             :      * number between 0 and the total number of to-be-checkpointed pages. Each
     112             :      * page checkpointed in this tablespace increments this space's progress
     113             :      * by progress_slice.
     114             :      */
     115             :     float8      progress;
     116             :     float8      progress_slice;
     117             : 
     118             :     /* number of to-be checkpointed pages in this tablespace */
     119             :     int         num_to_scan;
     120             :     /* already processed pages in this tablespace */
     121             :     int         num_scanned;
     122             : 
     123             :     /* current offset in CkptBufferIds for this tablespace */
     124             :     int         index;
     125             : } CkptTsStatus;
     126             : 
     127             : /*
     128             :  * Type for array used to sort SMgrRelations
     129             :  *
     130             :  * FlushRelationsAllBuffers shares the same comparator function with
     131             :  * DropRelationsAllBuffers. Pointer to this struct and RelFileLocator must be
     132             :  * compatible.
     133             :  */
     134             : typedef struct SMgrSortArray
     135             : {
     136             :     RelFileLocator rlocator;    /* This must be the first member */
     137             :     SMgrRelation srel;
     138             : } SMgrSortArray;
     139             : 
     140             : /* GUC variables */
     141             : bool        zero_damaged_pages = false;
     142             : int         bgwriter_lru_maxpages = 100;
     143             : double      bgwriter_lru_multiplier = 2.0;
     144             : bool        track_io_timing = false;
     145             : 
     146             : /*
     147             :  * How many buffers PrefetchBuffer callers should try to stay ahead of their
     148             :  * ReadBuffer calls by.  Zero means "never prefetch".  This value is only used
     149             :  * for buffers not belonging to tablespaces that have their
     150             :  * effective_io_concurrency parameter set.
     151             :  */
     152             : int         effective_io_concurrency = DEFAULT_EFFECTIVE_IO_CONCURRENCY;
     153             : 
     154             : /*
     155             :  * Like effective_io_concurrency, but used by maintenance code paths that might
     156             :  * benefit from a higher setting because they work on behalf of many sessions.
     157             :  * Overridden by the tablespace setting of the same name.
     158             :  */
     159             : int         maintenance_io_concurrency = DEFAULT_MAINTENANCE_IO_CONCURRENCY;
     160             : 
     161             : /*
     162             :  * Limit on how many blocks should be handled in single I/O operations.
     163             :  * StartReadBuffers() callers should respect it, as should other operations
     164             :  * that call smgr APIs directly.  It is computed as the minimum of underlying
     165             :  * GUCs io_combine_limit_guc and io_max_combine_limit.
     166             :  */
     167             : int         io_combine_limit = DEFAULT_IO_COMBINE_LIMIT;
     168             : int         io_combine_limit_guc = DEFAULT_IO_COMBINE_LIMIT;
     169             : int         io_max_combine_limit = DEFAULT_IO_COMBINE_LIMIT;
     170             : 
     171             : /*
     172             :  * GUC variables about triggering kernel writeback for buffers written; OS
     173             :  * dependent defaults are set via the GUC mechanism.
     174             :  */
     175             : int         checkpoint_flush_after = DEFAULT_CHECKPOINT_FLUSH_AFTER;
     176             : int         bgwriter_flush_after = DEFAULT_BGWRITER_FLUSH_AFTER;
     177             : int         backend_flush_after = DEFAULT_BACKEND_FLUSH_AFTER;
     178             : 
     179             : /* local state for LockBufferForCleanup */
     180             : static BufferDesc *PinCountWaitBuf = NULL;
     181             : 
     182             : /*
     183             :  * Backend-Private refcount management:
     184             :  *
     185             :  * Each buffer also has a private refcount that keeps track of the number of
     186             :  * times the buffer is pinned in the current process.  This is so that the
     187             :  * shared refcount needs to be modified only once if a buffer is pinned more
     188             :  * than once by an individual backend.  It's also used to check that no buffers
     189             :  * are still pinned at the end of transactions and when exiting.
     190             :  *
     191             :  *
     192             :  * To avoid - as we used to - requiring an array with NBuffers entries to keep
     193             :  * track of local buffers, we use a small sequentially searched array
     194             :  * (PrivateRefCountArray) and an overflow hash table (PrivateRefCountHash) to
     195             :  * keep track of backend local pins.
     196             :  *
     197             :  * Until no more than REFCOUNT_ARRAY_ENTRIES buffers are pinned at once, all
     198             :  * refcounts are kept track of in the array; after that, new array entries
     199             :  * displace old ones into the hash table. That way a frequently used entry
     200             :  * can't get "stuck" in the hashtable while infrequent ones clog the array.
     201             :  *
     202             :  * Note that in most scenarios the number of pinned buffers will not exceed
     203             :  * REFCOUNT_ARRAY_ENTRIES.
     204             :  *
     205             :  *
     206             :  * To enter a buffer into the refcount tracking mechanism first reserve a free
     207             :  * entry using ReservePrivateRefCountEntry() and then later, if necessary,
     208             :  * fill it with NewPrivateRefCountEntry(). That split lets us avoid doing
     209             :  * memory allocations in NewPrivateRefCountEntry() which can be important
     210             :  * because in some scenarios it's called with a spinlock held...
     211             :  */
     212             : static struct PrivateRefCountEntry PrivateRefCountArray[REFCOUNT_ARRAY_ENTRIES];
     213             : static HTAB *PrivateRefCountHash = NULL;
     214             : static int32 PrivateRefCountOverflowed = 0;
     215             : static uint32 PrivateRefCountClock = 0;
     216             : static PrivateRefCountEntry *ReservedRefCountEntry = NULL;
     217             : 
     218             : static uint32 MaxProportionalPins;
     219             : 
     220             : static void ReservePrivateRefCountEntry(void);
     221             : static PrivateRefCountEntry *NewPrivateRefCountEntry(Buffer buffer);
     222             : static PrivateRefCountEntry *GetPrivateRefCountEntry(Buffer buffer, bool do_move);
     223             : static inline int32 GetPrivateRefCount(Buffer buffer);
     224             : static void ForgetPrivateRefCountEntry(PrivateRefCountEntry *ref);
     225             : 
     226             : /* ResourceOwner callbacks to hold in-progress I/Os and buffer pins */
     227             : static void ResOwnerReleaseBufferIO(Datum res);
     228             : static char *ResOwnerPrintBufferIO(Datum res);
     229             : static void ResOwnerReleaseBufferPin(Datum res);
     230             : static char *ResOwnerPrintBufferPin(Datum res);
     231             : 
     232             : const ResourceOwnerDesc buffer_io_resowner_desc =
     233             : {
     234             :     .name = "buffer io",
     235             :     .release_phase = RESOURCE_RELEASE_BEFORE_LOCKS,
     236             :     .release_priority = RELEASE_PRIO_BUFFER_IOS,
     237             :     .ReleaseResource = ResOwnerReleaseBufferIO,
     238             :     .DebugPrint = ResOwnerPrintBufferIO
     239             : };
     240             : 
     241             : const ResourceOwnerDesc buffer_pin_resowner_desc =
     242             : {
     243             :     .name = "buffer pin",
     244             :     .release_phase = RESOURCE_RELEASE_BEFORE_LOCKS,
     245             :     .release_priority = RELEASE_PRIO_BUFFER_PINS,
     246             :     .ReleaseResource = ResOwnerReleaseBufferPin,
     247             :     .DebugPrint = ResOwnerPrintBufferPin
     248             : };
     249             : 
     250             : /*
     251             :  * Ensure that the PrivateRefCountArray has sufficient space to store one more
     252             :  * entry. This has to be called before using NewPrivateRefCountEntry() to fill
     253             :  * a new entry - but it's perfectly fine to not use a reserved entry.
     254             :  */
     255             : static void
     256   124059052 : ReservePrivateRefCountEntry(void)
     257             : {
     258             :     /* Already reserved (or freed), nothing to do */
     259   124059052 :     if (ReservedRefCountEntry != NULL)
     260   115775282 :         return;
     261             : 
     262             :     /*
     263             :      * First search for a free entry the array, that'll be sufficient in the
     264             :      * majority of cases.
     265             :      */
     266             :     {
     267             :         int         i;
     268             : 
     269    21325912 :         for (i = 0; i < REFCOUNT_ARRAY_ENTRIES; i++)
     270             :         {
     271             :             PrivateRefCountEntry *res;
     272             : 
     273    20973420 :             res = &PrivateRefCountArray[i];
     274             : 
     275    20973420 :             if (res->buffer == InvalidBuffer)
     276             :             {
     277     7931278 :                 ReservedRefCountEntry = res;
     278     7931278 :                 return;
     279             :             }
     280             :         }
     281             :     }
     282             : 
     283             :     /*
     284             :      * No luck. All array entries are full. Move one array entry into the hash
     285             :      * table.
     286             :      */
     287             :     {
     288             :         /*
     289             :          * Move entry from the current clock position in the array into the
     290             :          * hashtable. Use that slot.
     291             :          */
     292             :         PrivateRefCountEntry *hashent;
     293             :         bool        found;
     294             : 
     295             :         /* select victim slot */
     296      352492 :         ReservedRefCountEntry =
     297      352492 :             &PrivateRefCountArray[PrivateRefCountClock++ % REFCOUNT_ARRAY_ENTRIES];
     298             : 
     299             :         /* Better be used, otherwise we shouldn't get here. */
     300             :         Assert(ReservedRefCountEntry->buffer != InvalidBuffer);
     301             : 
     302             :         /* enter victim array entry into hashtable */
     303      352492 :         hashent = hash_search(PrivateRefCountHash,
     304      352492 :                               &(ReservedRefCountEntry->buffer),
     305             :                               HASH_ENTER,
     306             :                               &found);
     307             :         Assert(!found);
     308      352492 :         hashent->refcount = ReservedRefCountEntry->refcount;
     309             : 
     310             :         /* clear the now free array slot */
     311      352492 :         ReservedRefCountEntry->buffer = InvalidBuffer;
     312      352492 :         ReservedRefCountEntry->refcount = 0;
     313             : 
     314      352492 :         PrivateRefCountOverflowed++;
     315             :     }
     316             : }
     317             : 
     318             : /*
     319             :  * Fill a previously reserved refcount entry.
     320             :  */
     321             : static PrivateRefCountEntry *
     322   112825636 : NewPrivateRefCountEntry(Buffer buffer)
     323             : {
     324             :     PrivateRefCountEntry *res;
     325             : 
     326             :     /* only allowed to be called when a reservation has been made */
     327             :     Assert(ReservedRefCountEntry != NULL);
     328             : 
     329             :     /* use up the reserved entry */
     330   112825636 :     res = ReservedRefCountEntry;
     331   112825636 :     ReservedRefCountEntry = NULL;
     332             : 
     333             :     /* and fill it */
     334   112825636 :     res->buffer = buffer;
     335   112825636 :     res->refcount = 0;
     336             : 
     337   112825636 :     return res;
     338             : }
     339             : 
     340             : /*
     341             :  * Return the PrivateRefCount entry for the passed buffer.
     342             :  *
     343             :  * Returns NULL if a buffer doesn't have a refcount entry. Otherwise, if
     344             :  * do_move is true, and the entry resides in the hashtable the entry is
     345             :  * optimized for frequent access by moving it to the array.
     346             :  */
     347             : static PrivateRefCountEntry *
     348   277769762 : GetPrivateRefCountEntry(Buffer buffer, bool do_move)
     349             : {
     350             :     PrivateRefCountEntry *res;
     351             :     int         i;
     352             : 
     353             :     Assert(BufferIsValid(buffer));
     354             :     Assert(!BufferIsLocal(buffer));
     355             : 
     356             :     /*
     357             :      * First search for references in the array, that'll be sufficient in the
     358             :      * majority of cases.
     359             :      */
     360  1310877332 :     for (i = 0; i < REFCOUNT_ARRAY_ENTRIES; i++)
     361             :     {
     362  1201970706 :         res = &PrivateRefCountArray[i];
     363             : 
     364  1201970706 :         if (res->buffer == buffer)
     365   168863136 :             return res;
     366             :     }
     367             : 
     368             :     /*
     369             :      * By here we know that the buffer, if already pinned, isn't residing in
     370             :      * the array.
     371             :      *
     372             :      * Only look up the buffer in the hashtable if we've previously overflowed
     373             :      * into it.
     374             :      */
     375   108906626 :     if (PrivateRefCountOverflowed == 0)
     376   108083998 :         return NULL;
     377             : 
     378      822628 :     res = hash_search(PrivateRefCountHash, &buffer, HASH_FIND, NULL);
     379             : 
     380      822628 :     if (res == NULL)
     381      467022 :         return NULL;
     382      355606 :     else if (!do_move)
     383             :     {
     384             :         /* caller doesn't want us to move the hash entry into the array */
     385      315848 :         return res;
     386             :     }
     387             :     else
     388             :     {
     389             :         /* move buffer from hashtable into the free array slot */
     390             :         bool        found;
     391             :         PrivateRefCountEntry *free;
     392             : 
     393             :         /* Ensure there's a free array slot */
     394       39758 :         ReservePrivateRefCountEntry();
     395             : 
     396             :         /* Use up the reserved slot */
     397             :         Assert(ReservedRefCountEntry != NULL);
     398       39758 :         free = ReservedRefCountEntry;
     399       39758 :         ReservedRefCountEntry = NULL;
     400             :         Assert(free->buffer == InvalidBuffer);
     401             : 
     402             :         /* and fill it */
     403       39758 :         free->buffer = buffer;
     404       39758 :         free->refcount = res->refcount;
     405             : 
     406             :         /* delete from hashtable */
     407       39758 :         hash_search(PrivateRefCountHash, &buffer, HASH_REMOVE, &found);
     408             :         Assert(found);
     409             :         Assert(PrivateRefCountOverflowed > 0);
     410       39758 :         PrivateRefCountOverflowed--;
     411             : 
     412       39758 :         return free;
     413             :     }
     414             : }
     415             : 
     416             : /*
     417             :  * Returns how many times the passed buffer is pinned by this backend.
     418             :  *
     419             :  * Only works for shared memory buffers!
     420             :  */
     421             : static inline int32
     422     5406714 : GetPrivateRefCount(Buffer buffer)
     423             : {
     424             :     PrivateRefCountEntry *ref;
     425             : 
     426             :     Assert(BufferIsValid(buffer));
     427             :     Assert(!BufferIsLocal(buffer));
     428             : 
     429             :     /*
     430             :      * Not moving the entry - that's ok for the current users, but we might
     431             :      * want to change this one day.
     432             :      */
     433     5406714 :     ref = GetPrivateRefCountEntry(buffer, false);
     434             : 
     435     5406714 :     if (ref == NULL)
     436        8084 :         return 0;
     437     5398630 :     return ref->refcount;
     438             : }
     439             : 
     440             : /*
     441             :  * Release resources used to track the reference count of a buffer which we no
     442             :  * longer have pinned and don't want to pin again immediately.
     443             :  */
     444             : static void
     445   112825636 : ForgetPrivateRefCountEntry(PrivateRefCountEntry *ref)
     446             : {
     447             :     Assert(ref->refcount == 0);
     448             : 
     449   112825636 :     if (ref >= &PrivateRefCountArray[0] &&
     450             :         ref < &PrivateRefCountArray[REFCOUNT_ARRAY_ENTRIES])
     451             :     {
     452   112512902 :         ref->buffer = InvalidBuffer;
     453             : 
     454             :         /*
     455             :          * Mark the just used entry as reserved - in many scenarios that
     456             :          * allows us to avoid ever having to search the array/hash for free
     457             :          * entries.
     458             :          */
     459   112512902 :         ReservedRefCountEntry = ref;
     460             :     }
     461             :     else
     462             :     {
     463             :         bool        found;
     464      312734 :         Buffer      buffer = ref->buffer;
     465             : 
     466      312734 :         hash_search(PrivateRefCountHash, &buffer, HASH_REMOVE, &found);
     467             :         Assert(found);
     468             :         Assert(PrivateRefCountOverflowed > 0);
     469      312734 :         PrivateRefCountOverflowed--;
     470             :     }
     471   112825636 : }
     472             : 
     473             : /*
     474             :  * BufferIsPinned
     475             :  *      True iff the buffer is pinned (also checks for valid buffer number).
     476             :  *
     477             :  *      NOTE: what we check here is that *this* backend holds a pin on
     478             :  *      the buffer.  We do not care whether some other backend does.
     479             :  */
     480             : #define BufferIsPinned(bufnum) \
     481             : ( \
     482             :     !BufferIsValid(bufnum) ? \
     483             :         false \
     484             :     : \
     485             :         BufferIsLocal(bufnum) ? \
     486             :             (LocalRefCount[-(bufnum) - 1] > 0) \
     487             :         : \
     488             :     (GetPrivateRefCount(bufnum) > 0) \
     489             : )
     490             : 
     491             : 
     492             : static Buffer ReadBuffer_common(Relation rel,
     493             :                                 SMgrRelation smgr, char smgr_persistence,
     494             :                                 ForkNumber forkNum, BlockNumber blockNum,
     495             :                                 ReadBufferMode mode, BufferAccessStrategy strategy);
     496             : static BlockNumber ExtendBufferedRelCommon(BufferManagerRelation bmr,
     497             :                                            ForkNumber fork,
     498             :                                            BufferAccessStrategy strategy,
     499             :                                            uint32 flags,
     500             :                                            uint32 extend_by,
     501             :                                            BlockNumber extend_upto,
     502             :                                            Buffer *buffers,
     503             :                                            uint32 *extended_by);
     504             : static BlockNumber ExtendBufferedRelShared(BufferManagerRelation bmr,
     505             :                                            ForkNumber fork,
     506             :                                            BufferAccessStrategy strategy,
     507             :                                            uint32 flags,
     508             :                                            uint32 extend_by,
     509             :                                            BlockNumber extend_upto,
     510             :                                            Buffer *buffers,
     511             :                                            uint32 *extended_by);
     512             : static bool PinBuffer(BufferDesc *buf, BufferAccessStrategy strategy);
     513             : static void PinBuffer_Locked(BufferDesc *buf);
     514             : static void UnpinBuffer(BufferDesc *buf);
     515             : static void UnpinBufferNoOwner(BufferDesc *buf);
     516             : static void BufferSync(int flags);
     517             : static uint32 WaitBufHdrUnlocked(BufferDesc *buf);
     518             : static int  SyncOneBuffer(int buf_id, bool skip_recently_used,
     519             :                           WritebackContext *wb_context);
     520             : static void WaitIO(BufferDesc *buf);
     521             : static void AbortBufferIO(Buffer buffer);
     522             : static void shared_buffer_write_error_callback(void *arg);
     523             : static void local_buffer_write_error_callback(void *arg);
     524             : static inline BufferDesc *BufferAlloc(SMgrRelation smgr,
     525             :                                       char relpersistence,
     526             :                                       ForkNumber forkNum,
     527             :                                       BlockNumber blockNum,
     528             :                                       BufferAccessStrategy strategy,
     529             :                                       bool *foundPtr, IOContext io_context);
     530             : static bool AsyncReadBuffers(ReadBuffersOperation *operation, int *nblocks);
     531             : static void CheckReadBuffersOperation(ReadBuffersOperation *operation, bool is_complete);
     532             : static Buffer GetVictimBuffer(BufferAccessStrategy strategy, IOContext io_context);
     533             : static void FlushBuffer(BufferDesc *buf, SMgrRelation reln,
     534             :                         IOObject io_object, IOContext io_context);
     535             : static void FindAndDropRelationBuffers(RelFileLocator rlocator,
     536             :                                        ForkNumber forkNum,
     537             :                                        BlockNumber nForkBlock,
     538             :                                        BlockNumber firstDelBlock);
     539             : static void RelationCopyStorageUsingBuffer(RelFileLocator srclocator,
     540             :                                            RelFileLocator dstlocator,
     541             :                                            ForkNumber forkNum, bool permanent);
     542             : static void AtProcExit_Buffers(int code, Datum arg);
     543             : static void CheckForBufferLeaks(void);
     544             : static int  rlocator_comparator(const void *p1, const void *p2);
     545             : static inline int buffertag_comparator(const BufferTag *ba, const BufferTag *bb);
     546             : static inline int ckpt_buforder_comparator(const CkptSortItem *a, const CkptSortItem *b);
     547             : static int  ts_ckpt_progress_comparator(Datum a, Datum b, void *arg);
     548             : 
     549             : 
     550             : /*
     551             :  * Implementation of PrefetchBuffer() for shared buffers.
     552             :  */
     553             : PrefetchBufferResult
     554       61566 : PrefetchSharedBuffer(SMgrRelation smgr_reln,
     555             :                      ForkNumber forkNum,
     556             :                      BlockNumber blockNum)
     557             : {
     558       61566 :     PrefetchBufferResult result = {InvalidBuffer, false};
     559             :     BufferTag   newTag;         /* identity of requested block */
     560             :     uint32      newHash;        /* hash value for newTag */
     561             :     LWLock     *newPartitionLock;   /* buffer partition lock for it */
     562             :     int         buf_id;
     563             : 
     564             :     Assert(BlockNumberIsValid(blockNum));
     565             : 
     566             :     /* create a tag so we can lookup the buffer */
     567       61566 :     InitBufferTag(&newTag, &smgr_reln->smgr_rlocator.locator,
     568             :                   forkNum, blockNum);
     569             : 
     570             :     /* determine its hash code and partition lock ID */
     571       61566 :     newHash = BufTableHashCode(&newTag);
     572       61566 :     newPartitionLock = BufMappingPartitionLock(newHash);
     573             : 
     574             :     /* see if the block is in the buffer pool already */
     575       61566 :     LWLockAcquire(newPartitionLock, LW_SHARED);
     576       61566 :     buf_id = BufTableLookup(&newTag, newHash);
     577       61566 :     LWLockRelease(newPartitionLock);
     578             : 
     579             :     /* If not in buffers, initiate prefetch */
     580       61566 :     if (buf_id < 0)
     581             :     {
     582             : #ifdef USE_PREFETCH
     583             :         /*
     584             :          * Try to initiate an asynchronous read.  This returns false in
     585             :          * recovery if the relation file doesn't exist.
     586             :          */
     587       34072 :         if ((io_direct_flags & IO_DIRECT_DATA) == 0 &&
     588       16812 :             smgrprefetch(smgr_reln, forkNum, blockNum, 1))
     589             :         {
     590       16812 :             result.initiated_io = true;
     591             :         }
     592             : #endif                          /* USE_PREFETCH */
     593             :     }
     594             :     else
     595             :     {
     596             :         /*
     597             :          * Report the buffer it was in at that time.  The caller may be able
     598             :          * to avoid a buffer table lookup, but it's not pinned and it must be
     599             :          * rechecked!
     600             :          */
     601       44306 :         result.recent_buffer = buf_id + 1;
     602             :     }
     603             : 
     604             :     /*
     605             :      * If the block *is* in buffers, we do nothing.  This is not really ideal:
     606             :      * the block might be just about to be evicted, which would be stupid
     607             :      * since we know we are going to need it soon.  But the only easy answer
     608             :      * is to bump the usage_count, which does not seem like a great solution:
     609             :      * when the caller does ultimately touch the block, usage_count would get
     610             :      * bumped again, resulting in too much favoritism for blocks that are
     611             :      * involved in a prefetch sequence. A real fix would involve some
     612             :      * additional per-buffer state, and it's not clear that there's enough of
     613             :      * a problem to justify that.
     614             :      */
     615             : 
     616       61566 :     return result;
     617             : }
     618             : 
     619             : /*
     620             :  * PrefetchBuffer -- initiate asynchronous read of a block of a relation
     621             :  *
     622             :  * This is named by analogy to ReadBuffer but doesn't actually allocate a
     623             :  * buffer.  Instead it tries to ensure that a future ReadBuffer for the given
     624             :  * block will not be delayed by the I/O.  Prefetching is optional.
     625             :  *
     626             :  * There are three possible outcomes:
     627             :  *
     628             :  * 1.  If the block is already cached, the result includes a valid buffer that
     629             :  * could be used by the caller to avoid the need for a later buffer lookup, but
     630             :  * it's not pinned, so the caller must recheck it.
     631             :  *
     632             :  * 2.  If the kernel has been asked to initiate I/O, the initiated_io member is
     633             :  * true.  Currently there is no way to know if the data was already cached by
     634             :  * the kernel and therefore didn't really initiate I/O, and no way to know when
     635             :  * the I/O completes other than using synchronous ReadBuffer().
     636             :  *
     637             :  * 3.  Otherwise, the buffer wasn't already cached by PostgreSQL, and
     638             :  * USE_PREFETCH is not defined (this build doesn't support prefetching due to
     639             :  * lack of a kernel facility), direct I/O is enabled, or the underlying
     640             :  * relation file wasn't found and we are in recovery.  (If the relation file
     641             :  * wasn't found and we are not in recovery, an error is raised).
     642             :  */
     643             : PrefetchBufferResult
     644       41506 : PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
     645             : {
     646             :     Assert(RelationIsValid(reln));
     647             :     Assert(BlockNumberIsValid(blockNum));
     648             : 
     649       41506 :     if (RelationUsesLocalBuffers(reln))
     650             :     {
     651             :         /* see comments in ReadBufferExtended */
     652        1566 :         if (RELATION_IS_OTHER_TEMP(reln))
     653           0 :             ereport(ERROR,
     654             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     655             :                      errmsg("cannot access temporary tables of other sessions")));
     656             : 
     657             :         /* pass it off to localbuf.c */
     658        1566 :         return PrefetchLocalBuffer(RelationGetSmgr(reln), forkNum, blockNum);
     659             :     }
     660             :     else
     661             :     {
     662             :         /* pass it to the shared buffer version */
     663       39940 :         return PrefetchSharedBuffer(RelationGetSmgr(reln), forkNum, blockNum);
     664             :     }
     665             : }
     666             : 
     667             : /*
     668             :  * ReadRecentBuffer -- try to pin a block in a recently observed buffer
     669             :  *
     670             :  * Compared to ReadBuffer(), this avoids a buffer mapping lookup when it's
     671             :  * successful.  Return true if the buffer is valid and still has the expected
     672             :  * tag.  In that case, the buffer is pinned and the usage count is bumped.
     673             :  */
     674             : bool
     675        8100 : ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum,
     676             :                  Buffer recent_buffer)
     677             : {
     678             :     BufferDesc *bufHdr;
     679             :     BufferTag   tag;
     680             :     uint32      buf_state;
     681             :     bool        have_private_ref;
     682             : 
     683             :     Assert(BufferIsValid(recent_buffer));
     684             : 
     685        8100 :     ResourceOwnerEnlarge(CurrentResourceOwner);
     686        8100 :     ReservePrivateRefCountEntry();
     687        8100 :     InitBufferTag(&tag, &rlocator, forkNum, blockNum);
     688             : 
     689        8100 :     if (BufferIsLocal(recent_buffer))
     690             :     {
     691          64 :         int         b = -recent_buffer - 1;
     692             : 
     693          64 :         bufHdr = GetLocalBufferDescriptor(b);
     694          64 :         buf_state = pg_atomic_read_u32(&bufHdr->state);
     695             : 
     696             :         /* Is it still valid and holding the right tag? */
     697          64 :         if ((buf_state & BM_VALID) && BufferTagsEqual(&tag, &bufHdr->tag))
     698             :         {
     699          64 :             PinLocalBuffer(bufHdr, true);
     700             : 
     701          64 :             pgBufferUsage.local_blks_hit++;
     702             : 
     703          64 :             return true;
     704             :         }
     705             :     }
     706             :     else
     707             :     {
     708        8036 :         bufHdr = GetBufferDescriptor(recent_buffer - 1);
     709        8036 :         have_private_ref = GetPrivateRefCount(recent_buffer) > 0;
     710             : 
     711             :         /*
     712             :          * Do we already have this buffer pinned with a private reference?  If
     713             :          * so, it must be valid and it is safe to check the tag without
     714             :          * locking.  If not, we have to lock the header first and then check.
     715             :          */
     716        8036 :         if (have_private_ref)
     717           0 :             buf_state = pg_atomic_read_u32(&bufHdr->state);
     718             :         else
     719        8036 :             buf_state = LockBufHdr(bufHdr);
     720             : 
     721        8036 :         if ((buf_state & BM_VALID) && BufferTagsEqual(&tag, &bufHdr->tag))
     722             :         {
     723             :             /*
     724             :              * It's now safe to pin the buffer.  We can't pin first and ask
     725             :              * questions later, because it might confuse code paths like
     726             :              * InvalidateBuffer() if we pinned a random non-matching buffer.
     727             :              */
     728        7964 :             if (have_private_ref)
     729           0 :                 PinBuffer(bufHdr, NULL);    /* bump pin count */
     730             :             else
     731        7964 :                 PinBuffer_Locked(bufHdr);   /* pin for first time */
     732             : 
     733        7964 :             pgBufferUsage.shared_blks_hit++;
     734             : 
     735        7964 :             return true;
     736             :         }
     737             : 
     738             :         /* If we locked the header above, now unlock. */
     739          72 :         if (!have_private_ref)
     740          72 :             UnlockBufHdr(bufHdr, buf_state);
     741             :     }
     742             : 
     743          72 :     return false;
     744             : }
     745             : 
     746             : /*
     747             :  * ReadBuffer -- a shorthand for ReadBufferExtended, for reading from main
     748             :  *      fork with RBM_NORMAL mode and default strategy.
     749             :  */
     750             : Buffer
     751    82782354 : ReadBuffer(Relation reln, BlockNumber blockNum)
     752             : {
     753    82782354 :     return ReadBufferExtended(reln, MAIN_FORKNUM, blockNum, RBM_NORMAL, NULL);
     754             : }
     755             : 
     756             : /*
     757             :  * ReadBufferExtended -- returns a buffer containing the requested
     758             :  *      block of the requested relation.  If the blknum
     759             :  *      requested is P_NEW, extend the relation file and
     760             :  *      allocate a new block.  (Caller is responsible for
     761             :  *      ensuring that only one backend tries to extend a
     762             :  *      relation at the same time!)
     763             :  *
     764             :  * Returns: the buffer number for the buffer containing
     765             :  *      the block read.  The returned buffer has been pinned.
     766             :  *      Does not return on error --- elog's instead.
     767             :  *
     768             :  * Assume when this function is called, that reln has been opened already.
     769             :  *
     770             :  * In RBM_NORMAL mode, the page is read from disk, and the page header is
     771             :  * validated.  An error is thrown if the page header is not valid.  (But
     772             :  * note that an all-zero page is considered "valid"; see
     773             :  * PageIsVerified().)
     774             :  *
     775             :  * RBM_ZERO_ON_ERROR is like the normal mode, but if the page header is not
     776             :  * valid, the page is zeroed instead of throwing an error. This is intended
     777             :  * for non-critical data, where the caller is prepared to repair errors.
     778             :  *
     779             :  * In RBM_ZERO_AND_LOCK mode, if the page isn't in buffer cache already, it's
     780             :  * filled with zeros instead of reading it from disk.  Useful when the caller
     781             :  * is going to fill the page from scratch, since this saves I/O and avoids
     782             :  * unnecessary failure if the page-on-disk has corrupt page headers.
     783             :  * The page is returned locked to ensure that the caller has a chance to
     784             :  * initialize the page before it's made visible to others.
     785             :  * Caution: do not use this mode to read a page that is beyond the relation's
     786             :  * current physical EOF; that is likely to cause problems in md.c when
     787             :  * the page is modified and written out. P_NEW is OK, though.
     788             :  *
     789             :  * RBM_ZERO_AND_CLEANUP_LOCK is the same as RBM_ZERO_AND_LOCK, but acquires
     790             :  * a cleanup-strength lock on the page.
     791             :  *
     792             :  * RBM_NORMAL_NO_LOG mode is treated the same as RBM_NORMAL here.
     793             :  *
     794             :  * If strategy is not NULL, a nondefault buffer access strategy is used.
     795             :  * See buffer/README for details.
     796             :  */
     797             : inline Buffer
     798    99876368 : ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum,
     799             :                    ReadBufferMode mode, BufferAccessStrategy strategy)
     800             : {
     801             :     Buffer      buf;
     802             : 
     803             :     /*
     804             :      * Reject attempts to read non-local temporary relations; we would be
     805             :      * likely to get wrong data since we have no visibility into the owning
     806             :      * session's local buffers.
     807             :      */
     808    99876368 :     if (RELATION_IS_OTHER_TEMP(reln))
     809           0 :         ereport(ERROR,
     810             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     811             :                  errmsg("cannot access temporary tables of other sessions")));
     812             : 
     813             :     /*
     814             :      * Read the buffer, and update pgstat counters to reflect a cache hit or
     815             :      * miss.
     816             :      */
     817    99876368 :     buf = ReadBuffer_common(reln, RelationGetSmgr(reln), 0,
     818             :                             forkNum, blockNum, mode, strategy);
     819             : 
     820    99876322 :     return buf;
     821             : }
     822             : 
     823             : 
     824             : /*
     825             :  * ReadBufferWithoutRelcache -- like ReadBufferExtended, but doesn't require
     826             :  *      a relcache entry for the relation.
     827             :  *
     828             :  * Pass permanent = true for a RELPERSISTENCE_PERMANENT relation, and
     829             :  * permanent = false for a RELPERSISTENCE_UNLOGGED relation. This function
     830             :  * cannot be used for temporary relations (and making that work might be
     831             :  * difficult, unless we only want to read temporary relations for our own
     832             :  * ProcNumber).
     833             :  */
     834             : Buffer
     835    11270968 : ReadBufferWithoutRelcache(RelFileLocator rlocator, ForkNumber forkNum,
     836             :                           BlockNumber blockNum, ReadBufferMode mode,
     837             :                           BufferAccessStrategy strategy, bool permanent)
     838             : {
     839    11270968 :     SMgrRelation smgr = smgropen(rlocator, INVALID_PROC_NUMBER);
     840             : 
     841    11270968 :     return ReadBuffer_common(NULL, smgr,
     842             :                              permanent ? RELPERSISTENCE_PERMANENT : RELPERSISTENCE_UNLOGGED,
     843             :                              forkNum, blockNum,
     844             :                              mode, strategy);
     845             : }
     846             : 
     847             : /*
     848             :  * Convenience wrapper around ExtendBufferedRelBy() extending by one block.
     849             :  */
     850             : Buffer
     851       93488 : ExtendBufferedRel(BufferManagerRelation bmr,
     852             :                   ForkNumber forkNum,
     853             :                   BufferAccessStrategy strategy,
     854             :                   uint32 flags)
     855             : {
     856             :     Buffer      buf;
     857       93488 :     uint32      extend_by = 1;
     858             : 
     859       93488 :     ExtendBufferedRelBy(bmr, forkNum, strategy, flags, extend_by,
     860             :                         &buf, &extend_by);
     861             : 
     862       93488 :     return buf;
     863             : }
     864             : 
     865             : /*
     866             :  * Extend relation by multiple blocks.
     867             :  *
     868             :  * Tries to extend the relation by extend_by blocks. Depending on the
     869             :  * availability of resources the relation may end up being extended by a
     870             :  * smaller number of pages (unless an error is thrown, always by at least one
     871             :  * page). *extended_by is updated to the number of pages the relation has been
     872             :  * extended to.
     873             :  *
     874             :  * buffers needs to be an array that is at least extend_by long. Upon
     875             :  * completion, the first extend_by array elements will point to a pinned
     876             :  * buffer.
     877             :  *
     878             :  * If EB_LOCK_FIRST is part of flags, the first returned buffer is
     879             :  * locked. This is useful for callers that want a buffer that is guaranteed to
     880             :  * be empty.
     881             :  */
     882             : BlockNumber
     883      318730 : ExtendBufferedRelBy(BufferManagerRelation bmr,
     884             :                     ForkNumber fork,
     885             :                     BufferAccessStrategy strategy,
     886             :                     uint32 flags,
     887             :                     uint32 extend_by,
     888             :                     Buffer *buffers,
     889             :                     uint32 *extended_by)
     890             : {
     891             :     Assert((bmr.rel != NULL) != (bmr.smgr != NULL));
     892             :     Assert(bmr.smgr == NULL || bmr.relpersistence != 0);
     893             :     Assert(extend_by > 0);
     894             : 
     895      318730 :     if (bmr.smgr == NULL)
     896             :     {
     897      318730 :         bmr.smgr = RelationGetSmgr(bmr.rel);
     898      318730 :         bmr.relpersistence = bmr.rel->rd_rel->relpersistence;
     899             :     }
     900             : 
     901      318730 :     return ExtendBufferedRelCommon(bmr, fork, strategy, flags,
     902             :                                    extend_by, InvalidBlockNumber,
     903             :                                    buffers, extended_by);
     904             : }
     905             : 
     906             : /*
     907             :  * Extend the relation so it is at least extend_to blocks large, return buffer
     908             :  * (extend_to - 1).
     909             :  *
     910             :  * This is useful for callers that want to write a specific page, regardless
     911             :  * of the current size of the relation (e.g. useful for visibilitymap and for
     912             :  * crash recovery).
     913             :  */
     914             : Buffer
     915      100316 : ExtendBufferedRelTo(BufferManagerRelation bmr,
     916             :                     ForkNumber fork,
     917             :                     BufferAccessStrategy strategy,
     918             :                     uint32 flags,
     919             :                     BlockNumber extend_to,
     920             :                     ReadBufferMode mode)
     921             : {
     922             :     BlockNumber current_size;
     923      100316 :     uint32      extended_by = 0;
     924      100316 :     Buffer      buffer = InvalidBuffer;
     925             :     Buffer      buffers[64];
     926             : 
     927             :     Assert((bmr.rel != NULL) != (bmr.smgr != NULL));
     928             :     Assert(bmr.smgr == NULL || bmr.relpersistence != 0);
     929             :     Assert(extend_to != InvalidBlockNumber && extend_to > 0);
     930             : 
     931      100316 :     if (bmr.smgr == NULL)
     932             :     {
     933       13690 :         bmr.smgr = RelationGetSmgr(bmr.rel);
     934       13690 :         bmr.relpersistence = bmr.rel->rd_rel->relpersistence;
     935             :     }
     936             : 
     937             :     /*
     938             :      * If desired, create the file if it doesn't exist.  If
     939             :      * smgr_cached_nblocks[fork] is positive then it must exist, no need for
     940             :      * an smgrexists call.
     941             :      */
     942      100316 :     if ((flags & EB_CREATE_FORK_IF_NEEDED) &&
     943       13690 :         (bmr.smgr->smgr_cached_nblocks[fork] == 0 ||
     944          42 :          bmr.smgr->smgr_cached_nblocks[fork] == InvalidBlockNumber) &&
     945       13648 :         !smgrexists(bmr.smgr, fork))
     946             :     {
     947       13610 :         LockRelationForExtension(bmr.rel, ExclusiveLock);
     948             : 
     949             :         /* recheck, fork might have been created concurrently */
     950       13610 :         if (!smgrexists(bmr.smgr, fork))
     951       13606 :             smgrcreate(bmr.smgr, fork, flags & EB_PERFORMING_RECOVERY);
     952             : 
     953       13610 :         UnlockRelationForExtension(bmr.rel, ExclusiveLock);
     954             :     }
     955             : 
     956             :     /*
     957             :      * If requested, invalidate size cache, so that smgrnblocks asks the
     958             :      * kernel.
     959             :      */
     960      100316 :     if (flags & EB_CLEAR_SIZE_CACHE)
     961       13690 :         bmr.smgr->smgr_cached_nblocks[fork] = InvalidBlockNumber;
     962             : 
     963             :     /*
     964             :      * Estimate how many pages we'll need to extend by. This avoids acquiring
     965             :      * unnecessarily many victim buffers.
     966             :      */
     967      100316 :     current_size = smgrnblocks(bmr.smgr, fork);
     968             : 
     969             :     /*
     970             :      * Since no-one else can be looking at the page contents yet, there is no
     971             :      * difference between an exclusive lock and a cleanup-strength lock. Note
     972             :      * that we pass the original mode to ReadBuffer_common() below, when
     973             :      * falling back to reading the buffer to a concurrent relation extension.
     974             :      */
     975      100316 :     if (mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK)
     976       85910 :         flags |= EB_LOCK_TARGET;
     977             : 
     978      204786 :     while (current_size < extend_to)
     979             :     {
     980      104470 :         uint32      num_pages = lengthof(buffers);
     981             :         BlockNumber first_block;
     982             : 
     983      104470 :         if ((uint64) current_size + num_pages > extend_to)
     984      104338 :             num_pages = extend_to - current_size;
     985             : 
     986      104470 :         first_block = ExtendBufferedRelCommon(bmr, fork, strategy, flags,
     987             :                                               num_pages, extend_to,
     988             :                                               buffers, &extended_by);
     989             : 
     990      104470 :         current_size = first_block + extended_by;
     991             :         Assert(num_pages != 0 || current_size >= extend_to);
     992             : 
     993      222886 :         for (uint32 i = 0; i < extended_by; i++)
     994             :         {
     995      118416 :             if (first_block + i != extend_to - 1)
     996       18126 :                 ReleaseBuffer(buffers[i]);
     997             :             else
     998      100290 :                 buffer = buffers[i];
     999             :         }
    1000             :     }
    1001             : 
    1002             :     /*
    1003             :      * It's possible that another backend concurrently extended the relation.
    1004             :      * In that case read the buffer.
    1005             :      *
    1006             :      * XXX: Should we control this via a flag?
    1007             :      */
    1008      100316 :     if (buffer == InvalidBuffer)
    1009             :     {
    1010             :         Assert(extended_by == 0);
    1011          26 :         buffer = ReadBuffer_common(bmr.rel, bmr.smgr, bmr.relpersistence,
    1012             :                                    fork, extend_to - 1, mode, strategy);
    1013             :     }
    1014             : 
    1015      100316 :     return buffer;
    1016             : }
    1017             : 
    1018             : /*
    1019             :  * Lock and optionally zero a buffer, as part of the implementation of
    1020             :  * RBM_ZERO_AND_LOCK or RBM_ZERO_AND_CLEANUP_LOCK.  The buffer must be already
    1021             :  * pinned.  If the buffer is not already valid, it is zeroed and made valid.
    1022             :  */
    1023             : static void
    1024      603874 : ZeroAndLockBuffer(Buffer buffer, ReadBufferMode mode, bool already_valid)
    1025             : {
    1026             :     BufferDesc *bufHdr;
    1027             :     bool        need_to_zero;
    1028      603874 :     bool        isLocalBuf = BufferIsLocal(buffer);
    1029             : 
    1030             :     Assert(mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK);
    1031             : 
    1032      603874 :     if (already_valid)
    1033             :     {
    1034             :         /*
    1035             :          * If the caller already knew the buffer was valid, we can skip some
    1036             :          * header interaction.  The caller just wants to lock the buffer.
    1037             :          */
    1038       74158 :         need_to_zero = false;
    1039             :     }
    1040      529716 :     else if (isLocalBuf)
    1041             :     {
    1042             :         /* Simple case for non-shared buffers. */
    1043          48 :         bufHdr = GetLocalBufferDescriptor(-buffer - 1);
    1044          48 :         need_to_zero = StartLocalBufferIO(bufHdr, true, false);
    1045             :     }
    1046             :     else
    1047             :     {
    1048             :         /*
    1049             :          * Take BM_IO_IN_PROGRESS, or discover that BM_VALID has been set
    1050             :          * concurrently.  Even though we aren't doing I/O, that ensures that
    1051             :          * we don't zero a page that someone else has pinned.  An exclusive
    1052             :          * content lock wouldn't be enough, because readers are allowed to
    1053             :          * drop the content lock after determining that a tuple is visible
    1054             :          * (see buffer access rules in README).
    1055             :          */
    1056      529668 :         bufHdr = GetBufferDescriptor(buffer - 1);
    1057      529668 :         need_to_zero = StartBufferIO(bufHdr, true, false);
    1058             :     }
    1059             : 
    1060      603874 :     if (need_to_zero)
    1061             :     {
    1062      529716 :         memset(BufferGetPage(buffer), 0, BLCKSZ);
    1063             : 
    1064             :         /*
    1065             :          * Grab the buffer content lock before marking the page as valid, to
    1066             :          * make sure that no other backend sees the zeroed page before the
    1067             :          * caller has had a chance to initialize it.
    1068             :          *
    1069             :          * Since no-one else can be looking at the page contents yet, there is
    1070             :          * no difference between an exclusive lock and a cleanup-strength
    1071             :          * lock. (Note that we cannot use LockBuffer() or
    1072             :          * LockBufferForCleanup() here, because they assert that the buffer is
    1073             :          * already valid.)
    1074             :          */
    1075      529716 :         if (!isLocalBuf)
    1076      529668 :             LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_EXCLUSIVE);
    1077             : 
    1078             :         /* Set BM_VALID, terminate IO, and wake up any waiters */
    1079      529716 :         if (isLocalBuf)
    1080          48 :             TerminateLocalBufferIO(bufHdr, false, BM_VALID, false);
    1081             :         else
    1082      529668 :             TerminateBufferIO(bufHdr, false, BM_VALID, true, false);
    1083             :     }
    1084       74158 :     else if (!isLocalBuf)
    1085             :     {
    1086             :         /*
    1087             :          * The buffer is valid, so we can't zero it.  The caller still expects
    1088             :          * the page to be locked on return.
    1089             :          */
    1090       74118 :         if (mode == RBM_ZERO_AND_LOCK)
    1091       74036 :             LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
    1092             :         else
    1093          82 :             LockBufferForCleanup(buffer);
    1094             :     }
    1095      603874 : }
    1096             : 
    1097             : /*
    1098             :  * Pin a buffer for a given block.  *foundPtr is set to true if the block was
    1099             :  * already present, or false if more work is required to either read it in or
    1100             :  * zero it.
    1101             :  */
    1102             : static pg_attribute_always_inline Buffer
    1103   118651076 : PinBufferForBlock(Relation rel,
    1104             :                   SMgrRelation smgr,
    1105             :                   char persistence,
    1106             :                   ForkNumber forkNum,
    1107             :                   BlockNumber blockNum,
    1108             :                   BufferAccessStrategy strategy,
    1109             :                   bool *foundPtr)
    1110             : {
    1111             :     BufferDesc *bufHdr;
    1112             :     IOContext   io_context;
    1113             :     IOObject    io_object;
    1114             : 
    1115             :     Assert(blockNum != P_NEW);
    1116             : 
    1117             :     /* Persistence should be set before */
    1118             :     Assert((persistence == RELPERSISTENCE_TEMP ||
    1119             :             persistence == RELPERSISTENCE_PERMANENT ||
    1120             :             persistence == RELPERSISTENCE_UNLOGGED));
    1121             : 
    1122   118651076 :     if (persistence == RELPERSISTENCE_TEMP)
    1123             :     {
    1124     2545690 :         io_context = IOCONTEXT_NORMAL;
    1125     2545690 :         io_object = IOOBJECT_TEMP_RELATION;
    1126             :     }
    1127             :     else
    1128             :     {
    1129   116105386 :         io_context = IOContextForStrategy(strategy);
    1130   116105386 :         io_object = IOOBJECT_RELATION;
    1131             :     }
    1132             : 
    1133             :     TRACE_POSTGRESQL_BUFFER_READ_START(forkNum, blockNum,
    1134             :                                        smgr->smgr_rlocator.locator.spcOid,
    1135             :                                        smgr->smgr_rlocator.locator.dbOid,
    1136             :                                        smgr->smgr_rlocator.locator.relNumber,
    1137             :                                        smgr->smgr_rlocator.backend);
    1138             : 
    1139   118651076 :     if (persistence == RELPERSISTENCE_TEMP)
    1140             :     {
    1141     2545690 :         bufHdr = LocalBufferAlloc(smgr, forkNum, blockNum, foundPtr);
    1142     2545678 :         if (*foundPtr)
    1143     2528936 :             pgBufferUsage.local_blks_hit++;
    1144             :     }
    1145             :     else
    1146             :     {
    1147   116105386 :         bufHdr = BufferAlloc(smgr, persistence, forkNum, blockNum,
    1148             :                              strategy, foundPtr, io_context);
    1149   116105386 :         if (*foundPtr)
    1150   112849726 :             pgBufferUsage.shared_blks_hit++;
    1151             :     }
    1152   118651064 :     if (rel)
    1153             :     {
    1154             :         /*
    1155             :          * While pgBufferUsage's "read" counter isn't bumped unless we reach
    1156             :          * WaitReadBuffers() (so, not for hits, and not for buffers that are
    1157             :          * zeroed instead), the per-relation stats always count them.
    1158             :          */
    1159   106938064 :         pgstat_count_buffer_read(rel);
    1160   106938064 :         if (*foundPtr)
    1161   104546900 :             pgstat_count_buffer_hit(rel);
    1162             :     }
    1163   118651064 :     if (*foundPtr)
    1164             :     {
    1165   115378662 :         pgstat_count_io_op(io_object, io_context, IOOP_HIT, 1, 0);
    1166   115378662 :         if (VacuumCostActive)
    1167     3451198 :             VacuumCostBalance += VacuumCostPageHit;
    1168             : 
    1169             :         TRACE_POSTGRESQL_BUFFER_READ_DONE(forkNum, blockNum,
    1170             :                                           smgr->smgr_rlocator.locator.spcOid,
    1171             :                                           smgr->smgr_rlocator.locator.dbOid,
    1172             :                                           smgr->smgr_rlocator.locator.relNumber,
    1173             :                                           smgr->smgr_rlocator.backend,
    1174             :                                           true);
    1175             :     }
    1176             : 
    1177   118651064 :     return BufferDescriptorGetBuffer(bufHdr);
    1178             : }
    1179             : 
    1180             : /*
    1181             :  * ReadBuffer_common -- common logic for all ReadBuffer variants
    1182             :  *
    1183             :  * smgr is required, rel is optional unless using P_NEW.
    1184             :  */
    1185             : static pg_attribute_always_inline Buffer
    1186   111147362 : ReadBuffer_common(Relation rel, SMgrRelation smgr, char smgr_persistence,
    1187             :                   ForkNumber forkNum,
    1188             :                   BlockNumber blockNum, ReadBufferMode mode,
    1189             :                   BufferAccessStrategy strategy)
    1190             : {
    1191             :     ReadBuffersOperation operation;
    1192             :     Buffer      buffer;
    1193             :     int         flags;
    1194             :     char        persistence;
    1195             : 
    1196             :     /*
    1197             :      * Backward compatibility path, most code should use ExtendBufferedRel()
    1198             :      * instead, as acquiring the extension lock inside ExtendBufferedRel()
    1199             :      * scales a lot better.
    1200             :      */
    1201   111147362 :     if (unlikely(blockNum == P_NEW))
    1202             :     {
    1203         574 :         uint32      flags = EB_SKIP_EXTENSION_LOCK;
    1204             : 
    1205             :         /*
    1206             :          * Since no-one else can be looking at the page contents yet, there is
    1207             :          * no difference between an exclusive lock and a cleanup-strength
    1208             :          * lock.
    1209             :          */
    1210         574 :         if (mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK)
    1211           0 :             flags |= EB_LOCK_FIRST;
    1212             : 
    1213         574 :         return ExtendBufferedRel(BMR_REL(rel), forkNum, strategy, flags);
    1214             :     }
    1215             : 
    1216   111146788 :     if (rel)
    1217    99875820 :         persistence = rel->rd_rel->relpersistence;
    1218             :     else
    1219    11270968 :         persistence = smgr_persistence;
    1220             : 
    1221   111146788 :     if (unlikely(mode == RBM_ZERO_AND_CLEANUP_LOCK ||
    1222             :                  mode == RBM_ZERO_AND_LOCK))
    1223             :     {
    1224             :         bool        found;
    1225             : 
    1226      603874 :         buffer = PinBufferForBlock(rel, smgr, persistence,
    1227             :                                    forkNum, blockNum, strategy, &found);
    1228      603874 :         ZeroAndLockBuffer(buffer, mode, found);
    1229      603874 :         return buffer;
    1230             :     }
    1231             : 
    1232             :     /*
    1233             :      * Signal that we are going to immediately wait. If we're immediately
    1234             :      * waiting, there is no benefit in actually executing the IO
    1235             :      * asynchronously, it would just add dispatch overhead.
    1236             :      */
    1237   110542914 :     flags = READ_BUFFERS_SYNCHRONOUSLY;
    1238   110542914 :     if (mode == RBM_ZERO_ON_ERROR)
    1239     2392320 :         flags |= READ_BUFFERS_ZERO_ON_ERROR;
    1240   110542914 :     operation.smgr = smgr;
    1241   110542914 :     operation.rel = rel;
    1242   110542914 :     operation.persistence = persistence;
    1243   110542914 :     operation.forknum = forkNum;
    1244   110542914 :     operation.strategy = strategy;
    1245   110542914 :     if (StartReadBuffer(&operation,
    1246             :                         &buffer,
    1247             :                         blockNum,
    1248             :                         flags))
    1249     1259824 :         WaitReadBuffers(&operation);
    1250             : 
    1251   110542868 :     return buffer;
    1252             : }
    1253             : 
    1254             : static pg_attribute_always_inline bool
    1255   117719194 : StartReadBuffersImpl(ReadBuffersOperation *operation,
    1256             :                      Buffer *buffers,
    1257             :                      BlockNumber blockNum,
    1258             :                      int *nblocks,
    1259             :                      int flags,
    1260             :                      bool allow_forwarding)
    1261             : {
    1262   117719194 :     int         actual_nblocks = *nblocks;
    1263   117719194 :     int         maxcombine = 0;
    1264             :     bool        did_start_io;
    1265             : 
    1266             :     Assert(*nblocks == 1 || allow_forwarding);
    1267             :     Assert(*nblocks > 0);
    1268             :     Assert(*nblocks <= MAX_IO_COMBINE_LIMIT);
    1269             : 
    1270   120461880 :     for (int i = 0; i < actual_nblocks; ++i)
    1271             :     {
    1272             :         bool        found;
    1273             : 
    1274   118050450 :         if (allow_forwarding && buffers[i] != InvalidBuffer)
    1275        3248 :         {
    1276             :             BufferDesc *bufHdr;
    1277             : 
    1278             :             /*
    1279             :              * This is a buffer that was pinned by an earlier call to
    1280             :              * StartReadBuffers(), but couldn't be handled in one operation at
    1281             :              * that time.  The operation was split, and the caller has passed
    1282             :              * an already pinned buffer back to us to handle the rest of the
    1283             :              * operation.  It must continue at the expected block number.
    1284             :              */
    1285             :             Assert(BufferGetBlockNumber(buffers[i]) == blockNum + i);
    1286             : 
    1287             :             /*
    1288             :              * It might be an already valid buffer (a hit) that followed the
    1289             :              * final contiguous block of an earlier I/O (a miss) marking the
    1290             :              * end of it, or a buffer that some other backend has since made
    1291             :              * valid by performing the I/O for us, in which case we can handle
    1292             :              * it as a hit now.  It is safe to check for a BM_VALID flag with
    1293             :              * a relaxed load, because we got a fresh view of it while pinning
    1294             :              * it in the previous call.
    1295             :              *
    1296             :              * On the other hand if we don't see BM_VALID yet, it must be an
    1297             :              * I/O that was split by the previous call and we need to try to
    1298             :              * start a new I/O from this block.  We're also racing against any
    1299             :              * other backend that might start the I/O or even manage to mark
    1300             :              * it BM_VALID after this check, but StartBufferIO() will handle
    1301             :              * those cases.
    1302             :              */
    1303        3248 :             if (BufferIsLocal(buffers[i]))
    1304           4 :                 bufHdr = GetLocalBufferDescriptor(-buffers[i] - 1);
    1305             :             else
    1306        3244 :                 bufHdr = GetBufferDescriptor(buffers[i] - 1);
    1307             :             Assert(pg_atomic_read_u32(&bufHdr->state) & BM_TAG_VALID);
    1308        3248 :             found = pg_atomic_read_u32(&bufHdr->state) & BM_VALID;
    1309             :         }
    1310             :         else
    1311             :         {
    1312   118047190 :             buffers[i] = PinBufferForBlock(operation->rel,
    1313   118047202 :                                            operation->smgr,
    1314   118047202 :                                            operation->persistence,
    1315             :                                            operation->forknum,
    1316             :                                            blockNum + i,
    1317             :                                            operation->strategy,
    1318             :                                            &found);
    1319             :         }
    1320             : 
    1321   118050438 :         if (found)
    1322             :         {
    1323             :             /*
    1324             :              * We have a hit.  If it's the first block in the requested range,
    1325             :              * we can return it immediately and report that WaitReadBuffers()
    1326             :              * does not need to be called.  If the initial value of *nblocks
    1327             :              * was larger, the caller will have to call again for the rest.
    1328             :              */
    1329   115307752 :             if (i == 0)
    1330             :             {
    1331   115304500 :                 *nblocks = 1;
    1332             : 
    1333             : #ifdef USE_ASSERT_CHECKING
    1334             : 
    1335             :                 /*
    1336             :                  * Initialize enough of ReadBuffersOperation to make
    1337             :                  * CheckReadBuffersOperation() work. Outside of assertions
    1338             :                  * that's not necessary when no IO is issued.
    1339             :                  */
    1340             :                 operation->buffers = buffers;
    1341             :                 operation->blocknum = blockNum;
    1342             :                 operation->nblocks = 1;
    1343             :                 operation->nblocks_done = 1;
    1344             :                 CheckReadBuffersOperation(operation, true);
    1345             : #endif
    1346   115304500 :                 return false;
    1347             :             }
    1348             : 
    1349             :             /*
    1350             :              * Otherwise we already have an I/O to perform, but this block
    1351             :              * can't be included as it is already valid.  Split the I/O here.
    1352             :              * There may or may not be more blocks requiring I/O after this
    1353             :              * one, we haven't checked, but they can't be contiguous with this
    1354             :              * one in the way.  We'll leave this buffer pinned, forwarding it
    1355             :              * to the next call, avoiding the need to unpin it here and re-pin
    1356             :              * it in the next call.
    1357             :              */
    1358        3252 :             actual_nblocks = i;
    1359        3252 :             break;
    1360             :         }
    1361             :         else
    1362             :         {
    1363             :             /*
    1364             :              * Check how many blocks we can cover with the same IO. The smgr
    1365             :              * implementation might e.g. be limited due to a segment boundary.
    1366             :              */
    1367     2742686 :             if (i == 0 && actual_nblocks > 1)
    1368             :             {
    1369       66232 :                 maxcombine = smgrmaxcombine(operation->smgr,
    1370             :                                             operation->forknum,
    1371             :                                             blockNum);
    1372       66232 :                 if (unlikely(maxcombine < actual_nblocks))
    1373             :                 {
    1374           0 :                     elog(DEBUG2, "limiting nblocks at %u from %u to %u",
    1375             :                          blockNum, actual_nblocks, maxcombine);
    1376           0 :                     actual_nblocks = maxcombine;
    1377             :                 }
    1378             :             }
    1379             :         }
    1380             :     }
    1381     2414682 :     *nblocks = actual_nblocks;
    1382             : 
    1383             :     /* Populate information needed for I/O. */
    1384     2414682 :     operation->buffers = buffers;
    1385     2414682 :     operation->blocknum = blockNum;
    1386     2414682 :     operation->flags = flags;
    1387     2414682 :     operation->nblocks = actual_nblocks;
    1388     2414682 :     operation->nblocks_done = 0;
    1389     2414682 :     pgaio_wref_clear(&operation->io_wref);
    1390             : 
    1391             :     /*
    1392             :      * When using AIO, start the IO in the background. If not, issue prefetch
    1393             :      * requests if desired by the caller.
    1394             :      *
    1395             :      * The reason we have a dedicated path for IOMETHOD_SYNC here is to
    1396             :      * de-risk the introduction of AIO somewhat. It's a large architectural
    1397             :      * change, with lots of chances for unanticipated performance effects.
    1398             :      *
    1399             :      * Use of IOMETHOD_SYNC already leads to not actually performing IO
    1400             :      * asynchronously, but without the check here we'd execute IO earlier than
    1401             :      * we used to. Eventually this IOMETHOD_SYNC specific path should go away.
    1402             :      */
    1403     2414682 :     if (io_method != IOMETHOD_SYNC)
    1404             :     {
    1405             :         /*
    1406             :          * Try to start IO asynchronously. It's possible that no IO needs to
    1407             :          * be started, if another backend already performed the IO.
    1408             :          *
    1409             :          * Note that if an IO is started, it might not cover the entire
    1410             :          * requested range, e.g. because an intermediary block has been read
    1411             :          * in by another backend.  In that case any "trailing" buffers we
    1412             :          * already pinned above will be "forwarded" by read_stream.c to the
    1413             :          * next call to StartReadBuffers().
    1414             :          *
    1415             :          * This is signalled to the caller by decrementing *nblocks *and*
    1416             :          * reducing operation->nblocks. The latter is done here, but not below
    1417             :          * WaitReadBuffers(), as in WaitReadBuffers() we can't "shorten" the
    1418             :          * overall read size anymore, we need to retry until done in its
    1419             :          * entirety or until failed.
    1420             :          */
    1421     2412514 :         did_start_io = AsyncReadBuffers(operation, nblocks);
    1422             : 
    1423     2412484 :         operation->nblocks = *nblocks;
    1424             :     }
    1425             :     else
    1426             :     {
    1427        2168 :         operation->flags |= READ_BUFFERS_SYNCHRONOUSLY;
    1428             : 
    1429        2168 :         if (flags & READ_BUFFERS_ISSUE_ADVICE)
    1430             :         {
    1431             :             /*
    1432             :              * In theory we should only do this if PinBufferForBlock() had to
    1433             :              * allocate new buffers above.  That way, if two calls to
    1434             :              * StartReadBuffers() were made for the same blocks before
    1435             :              * WaitReadBuffers(), only the first would issue the advice.
    1436             :              * That'd be a better simulation of true asynchronous I/O, which
    1437             :              * would only start the I/O once, but isn't done here for
    1438             :              * simplicity.
    1439             :              */
    1440           4 :             smgrprefetch(operation->smgr,
    1441             :                          operation->forknum,
    1442             :                          blockNum,
    1443             :                          actual_nblocks);
    1444             :         }
    1445             : 
    1446             :         /*
    1447             :          * Indicate that WaitReadBuffers() should be called. WaitReadBuffers()
    1448             :          * will initiate the necessary IO.
    1449             :          */
    1450        2168 :         did_start_io = true;
    1451             :     }
    1452             : 
    1453     2414652 :     CheckReadBuffersOperation(operation, !did_start_io);
    1454             : 
    1455     2414652 :     return did_start_io;
    1456             : }
    1457             : 
    1458             : /*
    1459             :  * Begin reading a range of blocks beginning at blockNum and extending for
    1460             :  * *nblocks.  *nblocks and the buffers array are in/out parameters.  On entry,
    1461             :  * the buffers elements covered by *nblocks must hold either InvalidBuffer or
    1462             :  * buffers forwarded by an earlier call to StartReadBuffers() that was split
    1463             :  * and is now being continued.  On return, *nblocks holds the number of blocks
    1464             :  * accepted by this operation.  If it is less than the original number then
    1465             :  * this operation has been split, but buffer elements up to the original
    1466             :  * requested size may hold forwarded buffers to be used for a continuing
    1467             :  * operation.  The caller must either start a new I/O beginning at the block
    1468             :  * immediately following the blocks accepted by this call and pass those
    1469             :  * buffers back in, or release them if it chooses not to.  It shouldn't make
    1470             :  * any other use of or assumptions about forwarded buffers.
    1471             :  *
    1472             :  * If false is returned, no I/O is necessary and the buffers covered by
    1473             :  * *nblocks on exit are valid and ready to be accessed.  If true is returned,
    1474             :  * an I/O has been started, and WaitReadBuffers() must be called with the same
    1475             :  * operation object before the buffers covered by *nblocks on exit can be
    1476             :  * accessed.  Along with the operation object, the caller-supplied array of
    1477             :  * buffers must remain valid until WaitReadBuffers() is called, and any
    1478             :  * forwarded buffers must also be preserved for a continuing call unless
    1479             :  * they are explicitly released.
    1480             :  *
    1481             :  * Currently the I/O is only started with optional operating system advice if
    1482             :  * requested by the caller with READ_BUFFERS_ISSUE_ADVICE, and the real I/O
    1483             :  * happens synchronously in WaitReadBuffers().  In future work, true I/O could
    1484             :  * be initiated here.
    1485             :  */
    1486             : bool
    1487     3559028 : StartReadBuffers(ReadBuffersOperation *operation,
    1488             :                  Buffer *buffers,
    1489             :                  BlockNumber blockNum,
    1490             :                  int *nblocks,
    1491             :                  int flags)
    1492             : {
    1493     3559028 :     return StartReadBuffersImpl(operation, buffers, blockNum, nblocks, flags,
    1494             :                                 true /* expect forwarded buffers */ );
    1495             : }
    1496             : 
    1497             : /*
    1498             :  * Single block version of the StartReadBuffers().  This might save a few
    1499             :  * instructions when called from another translation unit, because it is
    1500             :  * specialized for nblocks == 1.
    1501             :  *
    1502             :  * This version does not support "forwarded" buffers: they cannot be created
    1503             :  * by reading only one block and *buffer is ignored on entry.
    1504             :  */
    1505             : bool
    1506   114160166 : StartReadBuffer(ReadBuffersOperation *operation,
    1507             :                 Buffer *buffer,
    1508             :                 BlockNumber blocknum,
    1509             :                 int flags)
    1510             : {
    1511   114160166 :     int         nblocks = 1;
    1512             :     bool        result;
    1513             : 
    1514   114160166 :     result = StartReadBuffersImpl(operation, buffer, blocknum, &nblocks, flags,
    1515             :                                   false /* single block, no forwarding */ );
    1516             :     Assert(nblocks == 1);       /* single block can't be short */
    1517             : 
    1518   114160136 :     return result;
    1519             : }
    1520             : 
    1521             : /*
    1522             :  * Perform sanity checks on the ReadBuffersOperation.
    1523             :  */
    1524             : static void
    1525     7234934 : CheckReadBuffersOperation(ReadBuffersOperation *operation, bool is_complete)
    1526             : {
    1527             : #ifdef USE_ASSERT_CHECKING
    1528             :     Assert(operation->nblocks_done <= operation->nblocks);
    1529             :     Assert(!is_complete || operation->nblocks == operation->nblocks_done);
    1530             : 
    1531             :     for (int i = 0; i < operation->nblocks; i++)
    1532             :     {
    1533             :         Buffer      buffer = operation->buffers[i];
    1534             :         BufferDesc *buf_hdr = BufferIsLocal(buffer) ?
    1535             :             GetLocalBufferDescriptor(-buffer - 1) :
    1536             :             GetBufferDescriptor(buffer - 1);
    1537             : 
    1538             :         Assert(BufferGetBlockNumber(buffer) == operation->blocknum + i);
    1539             :         Assert(pg_atomic_read_u32(&buf_hdr->state) & BM_TAG_VALID);
    1540             : 
    1541             :         if (i < operation->nblocks_done)
    1542             :             Assert(pg_atomic_read_u32(&buf_hdr->state) & BM_VALID);
    1543             :     }
    1544             : #endif
    1545     7234934 : }
    1546             : 
    1547             : /* helper for ReadBuffersCanStartIO(), to avoid repetition */
    1548             : static inline bool
    1549     2742722 : ReadBuffersCanStartIOOnce(Buffer buffer, bool nowait)
    1550             : {
    1551     2742722 :     if (BufferIsLocal(buffer))
    1552       16694 :         return StartLocalBufferIO(GetLocalBufferDescriptor(-buffer - 1),
    1553             :                                   true, nowait);
    1554             :     else
    1555     2726028 :         return StartBufferIO(GetBufferDescriptor(buffer - 1), true, nowait);
    1556             : }
    1557             : 
    1558             : /*
    1559             :  * Helper for AsyncReadBuffers that tries to get the buffer ready for IO.
    1560             :  */
    1561             : static inline bool
    1562     2742722 : ReadBuffersCanStartIO(Buffer buffer, bool nowait)
    1563             : {
    1564             :     /*
    1565             :      * If this backend currently has staged IO, we need to submit the pending
    1566             :      * IO before waiting for the right to issue IO, to avoid the potential for
    1567             :      * deadlocks (and, more commonly, unnecessary delays for other backends).
    1568             :      */
    1569     2742722 :     if (!nowait && pgaio_have_staged())
    1570             :     {
    1571         754 :         if (ReadBuffersCanStartIOOnce(buffer, true))
    1572         754 :             return true;
    1573             : 
    1574             :         /*
    1575             :          * Unfortunately StartBufferIO() returning false doesn't allow to
    1576             :          * distinguish between the buffer already being valid and IO already
    1577             :          * being in progress. Since IO already being in progress is quite
    1578             :          * rare, this approach seems fine.
    1579             :          */
    1580           0 :         pgaio_submit_staged();
    1581             :     }
    1582             : 
    1583     2741968 :     return ReadBuffersCanStartIOOnce(buffer, nowait);
    1584             : }
    1585             : 
    1586             : /*
    1587             :  * Helper for WaitReadBuffers() that processes the results of a readv
    1588             :  * operation, raising an error if necessary.
    1589             :  */
    1590             : static void
    1591     2409096 : ProcessReadBuffersResult(ReadBuffersOperation *operation)
    1592             : {
    1593     2409096 :     PgAioReturn *aio_ret = &operation->io_return;
    1594     2409096 :     PgAioResultStatus rs = aio_ret->result.status;
    1595     2409096 :     int         newly_read_blocks = 0;
    1596             : 
    1597             :     Assert(pgaio_wref_valid(&operation->io_wref));
    1598             :     Assert(aio_ret->result.status != PGAIO_RS_UNKNOWN);
    1599             : 
    1600             :     /*
    1601             :      * SMGR reports the number of blocks successfully read as the result of
    1602             :      * the IO operation. Thus we can simply add that to ->nblocks_done.
    1603             :      */
    1604             : 
    1605     2409096 :     if (likely(rs != PGAIO_RS_ERROR))
    1606     2409038 :         newly_read_blocks = aio_ret->result.result;
    1607             : 
    1608     2409096 :     if (rs == PGAIO_RS_ERROR || rs == PGAIO_RS_WARNING)
    1609          90 :         pgaio_result_report(aio_ret->result, &aio_ret->target_data,
    1610             :                             rs == PGAIO_RS_ERROR ? ERROR : WARNING);
    1611     2409006 :     else if (aio_ret->result.status == PGAIO_RS_PARTIAL)
    1612             :     {
    1613             :         /*
    1614             :          * We'll retry, so we just emit a debug message to the server log (or
    1615             :          * not even that in prod scenarios).
    1616             :          */
    1617          20 :         pgaio_result_report(aio_ret->result, &aio_ret->target_data, DEBUG1);
    1618          20 :         elog(DEBUG3, "partial read, will retry");
    1619             :     }
    1620             : 
    1621             :     Assert(newly_read_blocks > 0);
    1622             :     Assert(newly_read_blocks <= MAX_IO_COMBINE_LIMIT);
    1623             : 
    1624     2409038 :     operation->nblocks_done += newly_read_blocks;
    1625             : 
    1626             :     Assert(operation->nblocks_done <= operation->nblocks);
    1627     2409038 : }
    1628             : 
    1629             : void
    1630     2409076 : WaitReadBuffers(ReadBuffersOperation *operation)
    1631             : {
    1632     2409076 :     PgAioReturn *aio_ret = &operation->io_return;
    1633             :     IOContext   io_context;
    1634             :     IOObject    io_object;
    1635             : 
    1636     2409076 :     if (operation->persistence == RELPERSISTENCE_TEMP)
    1637             :     {
    1638        2940 :         io_context = IOCONTEXT_NORMAL;
    1639        2940 :         io_object = IOOBJECT_TEMP_RELATION;
    1640             :     }
    1641             :     else
    1642             :     {
    1643     2406136 :         io_context = IOContextForStrategy(operation->strategy);
    1644     2406136 :         io_object = IOOBJECT_RELATION;
    1645             :     }
    1646             : 
    1647             :     /*
    1648             :      * If we get here without an IO operation having been issued, the
    1649             :      * io_method == IOMETHOD_SYNC path must have been used. Otherwise the
    1650             :      * caller should not have called WaitReadBuffers().
    1651             :      *
    1652             :      * In the case of IOMETHOD_SYNC, we start - as we used to before the
    1653             :      * introducing of AIO - the IO in WaitReadBuffers(). This is done as part
    1654             :      * of the retry logic below, no extra code is required.
    1655             :      *
    1656             :      * This path is expected to eventually go away.
    1657             :      */
    1658     2409076 :     if (!pgaio_wref_valid(&operation->io_wref) && io_method != IOMETHOD_SYNC)
    1659           0 :         elog(ERROR, "waiting for read operation that didn't read");
    1660             : 
    1661             :     /*
    1662             :      * To handle partial reads, and IOMETHOD_SYNC, we re-issue IO until we're
    1663             :      * done. We may need multiple retries, not just because we could get
    1664             :      * multiple partial reads, but also because some of the remaining
    1665             :      * to-be-read buffers may have been read in by other backends, limiting
    1666             :      * the IO size.
    1667             :      */
    1668             :     while (true)
    1669        2188 :     {
    1670             :         int         ignored_nblocks_progress;
    1671             : 
    1672     2411264 :         CheckReadBuffersOperation(operation, false);
    1673             : 
    1674             :         /*
    1675             :          * If there is an IO associated with the operation, we may need to
    1676             :          * wait for it.
    1677             :          */
    1678     2411264 :         if (pgaio_wref_valid(&operation->io_wref))
    1679             :         {
    1680             :             /*
    1681             :              * Track the time spent waiting for the IO to complete. As
    1682             :              * tracking a wait even if we don't actually need to wait
    1683             :              *
    1684             :              * a) is not cheap, due to the timestamping overhead
    1685             :              *
    1686             :              * b) reports some time as waiting, even if we never waited
    1687             :              *
    1688             :              * we first check if we already know the IO is complete.
    1689             :              */
    1690     2409096 :             if (aio_ret->result.status == PGAIO_RS_UNKNOWN &&
    1691     1135672 :                 !pgaio_wref_check_done(&operation->io_wref))
    1692             :             {
    1693      485164 :                 instr_time  io_start = pgstat_prepare_io_time(track_io_timing);
    1694             : 
    1695      485164 :                 pgaio_wref_wait(&operation->io_wref);
    1696             : 
    1697             :                 /*
    1698             :                  * The IO operation itself was already counted earlier, in
    1699             :                  * AsyncReadBuffers(), this just accounts for the wait time.
    1700             :                  */
    1701      485164 :                 pgstat_count_io_op_time(io_object, io_context, IOOP_READ,
    1702             :                                         io_start, 0, 0);
    1703             :             }
    1704             :             else
    1705             :             {
    1706             :                 Assert(pgaio_wref_check_done(&operation->io_wref));
    1707             :             }
    1708             : 
    1709             :             /*
    1710             :              * We now are sure the IO completed. Check the results. This
    1711             :              * includes reporting on errors if there were any.
    1712             :              */
    1713     2409096 :             ProcessReadBuffersResult(operation);
    1714             :         }
    1715             : 
    1716             :         /*
    1717             :          * Most of the time, the one IO we already started, will read in
    1718             :          * everything.  But we need to deal with partial reads and buffers not
    1719             :          * needing IO anymore.
    1720             :          */
    1721     2411206 :         if (operation->nblocks_done == operation->nblocks)
    1722     2409018 :             break;
    1723             : 
    1724        2188 :         CHECK_FOR_INTERRUPTS();
    1725             : 
    1726             :         /*
    1727             :          * This may only complete the IO partially, either because some
    1728             :          * buffers were already valid, or because of a partial read.
    1729             :          *
    1730             :          * NB: In contrast to after the AsyncReadBuffers() call in
    1731             :          * StartReadBuffers(), we do *not* reduce
    1732             :          * ReadBuffersOperation->nblocks here, callers expect the full
    1733             :          * operation to be completed at this point (as more operations may
    1734             :          * have been queued).
    1735             :          */
    1736        2188 :         AsyncReadBuffers(operation, &ignored_nblocks_progress);
    1737             :     }
    1738             : 
    1739     2409018 :     CheckReadBuffersOperation(operation, true);
    1740             : 
    1741             :     /* NB: READ_DONE tracepoint was already executed in completion callback */
    1742     2409018 : }
    1743             : 
    1744             : /*
    1745             :  * Initiate IO for the ReadBuffersOperation
    1746             :  *
    1747             :  * This function only starts a single IO at a time. The size of the IO may be
    1748             :  * limited to below the to-be-read blocks, if one of the buffers has
    1749             :  * concurrently been read in. If the first to-be-read buffer is already valid,
    1750             :  * no IO will be issued.
    1751             :  *
    1752             :  * To support retries after partial reads, the first operation->nblocks_done
    1753             :  * buffers are skipped.
    1754             :  *
    1755             :  * On return *nblocks_progress is updated to reflect the number of buffers
    1756             :  * affected by the call. If the first buffer is valid, *nblocks_progress is
    1757             :  * set to 1 and operation->nblocks_done is incremented.
    1758             :  *
    1759             :  * Returns true if IO was initiated, false if no IO was necessary.
    1760             :  */
    1761             : static bool
    1762     2414702 : AsyncReadBuffers(ReadBuffersOperation *operation, int *nblocks_progress)
    1763             : {
    1764     2414702 :     Buffer     *buffers = &operation->buffers[0];
    1765     2414702 :     int         flags = operation->flags;
    1766     2414702 :     BlockNumber blocknum = operation->blocknum;
    1767     2414702 :     ForkNumber  forknum = operation->forknum;
    1768     2414702 :     char        persistence = operation->persistence;
    1769     2414702 :     int16       nblocks_done = operation->nblocks_done;
    1770     2414702 :     Buffer     *io_buffers = &operation->buffers[nblocks_done];
    1771     2414702 :     int         io_buffers_len = 0;
    1772             :     PgAioHandle *ioh;
    1773     2414702 :     uint32      ioh_flags = 0;
    1774             :     void       *io_pages[MAX_IO_COMBINE_LIMIT];
    1775             :     IOContext   io_context;
    1776             :     IOObject    io_object;
    1777             :     bool        did_start_io;
    1778             : 
    1779             :     /*
    1780             :      * When this IO is executed synchronously, either because the caller will
    1781             :      * immediately block waiting for the IO or because IOMETHOD_SYNC is used,
    1782             :      * the AIO subsystem needs to know.
    1783             :      */
    1784     2414702 :     if (flags & READ_BUFFERS_SYNCHRONOUSLY)
    1785     1262146 :         ioh_flags |= PGAIO_HF_SYNCHRONOUS;
    1786             : 
    1787     2414702 :     if (persistence == RELPERSISTENCE_TEMP)
    1788             :     {
    1789        3528 :         io_context = IOCONTEXT_NORMAL;
    1790        3528 :         io_object = IOOBJECT_TEMP_RELATION;
    1791        3528 :         ioh_flags |= PGAIO_HF_REFERENCES_LOCAL;
    1792             :     }
    1793             :     else
    1794             :     {
    1795     2411174 :         io_context = IOContextForStrategy(operation->strategy);
    1796     2411174 :         io_object = IOOBJECT_RELATION;
    1797             :     }
    1798             : 
    1799             :     /*
    1800             :      * If zero_damaged_pages is enabled, add the READ_BUFFERS_ZERO_ON_ERROR
    1801             :      * flag. The reason for that is that, hopefully, zero_damaged_pages isn't
    1802             :      * set globally, but on a per-session basis. The completion callback,
    1803             :      * which may be run in other processes, e.g. in IO workers, may have a
    1804             :      * different value of the zero_damaged_pages GUC.
    1805             :      *
    1806             :      * XXX: We probably should eventually use a different flag for
    1807             :      * zero_damaged_pages, so we can report different log levels / error codes
    1808             :      * for zero_damaged_pages and ZERO_ON_ERROR.
    1809             :      */
    1810     2414702 :     if (zero_damaged_pages)
    1811          32 :         flags |= READ_BUFFERS_ZERO_ON_ERROR;
    1812             : 
    1813             :     /*
    1814             :      * For the same reason as with zero_damaged_pages we need to use this
    1815             :      * backend's ignore_checksum_failure value.
    1816             :      */
    1817     2414702 :     if (ignore_checksum_failure)
    1818          16 :         flags |= READ_BUFFERS_IGNORE_CHECKSUM_FAILURES;
    1819             : 
    1820             : 
    1821             :     /*
    1822             :      * To be allowed to report stats in the local completion callback we need
    1823             :      * to prepare to report stats now. This ensures we can safely report the
    1824             :      * checksum failure even in a critical section.
    1825             :      */
    1826     2414702 :     pgstat_prepare_report_checksum_failure(operation->smgr->smgr_rlocator.locator.dbOid);
    1827             : 
    1828             :     /*
    1829             :      * Get IO handle before ReadBuffersCanStartIO(), as pgaio_io_acquire()
    1830             :      * might block, which we don't want after setting IO_IN_PROGRESS.
    1831             :      *
    1832             :      * If we need to wait for IO before we can get a handle, submit
    1833             :      * already-staged IO first, so that other backends don't need to wait.
    1834             :      * There wouldn't be a deadlock risk, as pgaio_io_acquire() just needs to
    1835             :      * wait for already submitted IO, which doesn't require additional locks,
    1836             :      * but it could still cause undesirable waits.
    1837             :      *
    1838             :      * A secondary benefit is that this would allow us to measure the time in
    1839             :      * pgaio_io_acquire() without causing undue timer overhead in the common,
    1840             :      * non-blocking, case.  However, currently the pgstats infrastructure
    1841             :      * doesn't really allow that, as it a) asserts that an operation can't
    1842             :      * have time without operations b) doesn't have an API to report
    1843             :      * "accumulated" time.
    1844             :      */
    1845     2414702 :     ioh = pgaio_io_acquire_nb(CurrentResourceOwner, &operation->io_return);
    1846     2414702 :     if (unlikely(!ioh))
    1847             :     {
    1848        5408 :         pgaio_submit_staged();
    1849             : 
    1850        5408 :         ioh = pgaio_io_acquire(CurrentResourceOwner, &operation->io_return);
    1851             :     }
    1852             : 
    1853             :     /*
    1854             :      * Check if we can start IO on the first to-be-read buffer.
    1855             :      *
    1856             :      * If an I/O is already in progress in another backend, we want to wait
    1857             :      * for the outcome: either done, or something went wrong and we will
    1858             :      * retry.
    1859             :      */
    1860     2414702 :     if (!ReadBuffersCanStartIO(buffers[nblocks_done], false))
    1861             :     {
    1862             :         /*
    1863             :          * Someone else has already completed this block, we're done.
    1864             :          *
    1865             :          * When IO is necessary, ->nblocks_done is updated in
    1866             :          * ProcessReadBuffersResult(), but that is not called if no IO is
    1867             :          * necessary. Thus update here.
    1868             :          */
    1869        4984 :         operation->nblocks_done += 1;
    1870        4984 :         *nblocks_progress = 1;
    1871             : 
    1872        4984 :         pgaio_io_release(ioh);
    1873        4984 :         pgaio_wref_clear(&operation->io_wref);
    1874        4984 :         did_start_io = false;
    1875             : 
    1876             :         /*
    1877             :          * Report and track this as a 'hit' for this backend, even though it
    1878             :          * must have started out as a miss in PinBufferForBlock(). The other
    1879             :          * backend will track this as a 'read'.
    1880             :          */
    1881             :         TRACE_POSTGRESQL_BUFFER_READ_DONE(forknum, blocknum + operation->nblocks_done,
    1882             :                                           operation->smgr->smgr_rlocator.locator.spcOid,
    1883             :                                           operation->smgr->smgr_rlocator.locator.dbOid,
    1884             :                                           operation->smgr->smgr_rlocator.locator.relNumber,
    1885             :                                           operation->smgr->smgr_rlocator.backend,
    1886             :                                           true);
    1887             : 
    1888        4984 :         if (persistence == RELPERSISTENCE_TEMP)
    1889           0 :             pgBufferUsage.local_blks_hit += 1;
    1890             :         else
    1891        4984 :             pgBufferUsage.shared_blks_hit += 1;
    1892             : 
    1893        4984 :         if (operation->rel)
    1894        4984 :             pgstat_count_buffer_hit(operation->rel);
    1895             : 
    1896        4984 :         pgstat_count_io_op(io_object, io_context, IOOP_HIT, 1, 0);
    1897             : 
    1898        4984 :         if (VacuumCostActive)
    1899           4 :             VacuumCostBalance += VacuumCostPageHit;
    1900             :     }
    1901             :     else
    1902             :     {
    1903             :         instr_time  io_start;
    1904             : 
    1905             :         /* We found a buffer that we need to read in. */
    1906             :         Assert(io_buffers[0] == buffers[nblocks_done]);
    1907     2409718 :         io_pages[0] = BufferGetBlock(buffers[nblocks_done]);
    1908     2409718 :         io_buffers_len = 1;
    1909             : 
    1910             :         /*
    1911             :          * How many neighboring-on-disk blocks can we scatter-read into other
    1912             :          * buffers at the same time?  In this case we don't wait if we see an
    1913             :          * I/O already in progress.  We already set BM_IO_IN_PROGRESS for the
    1914             :          * head block, so we should get on with that I/O as soon as possible.
    1915             :          */
    1916     2737738 :         for (int i = nblocks_done + 1; i < operation->nblocks; i++)
    1917             :         {
    1918      328020 :             if (!ReadBuffersCanStartIO(buffers[i], true))
    1919           0 :                 break;
    1920             :             /* Must be consecutive block numbers. */
    1921             :             Assert(BufferGetBlockNumber(buffers[i - 1]) ==
    1922             :                    BufferGetBlockNumber(buffers[i]) - 1);
    1923             :             Assert(io_buffers[io_buffers_len] == buffers[i]);
    1924             : 
    1925      328020 :             io_pages[io_buffers_len++] = BufferGetBlock(buffers[i]);
    1926             :         }
    1927             : 
    1928             :         /* get a reference to wait for in WaitReadBuffers() */
    1929     2409718 :         pgaio_io_get_wref(ioh, &operation->io_wref);
    1930             : 
    1931             :         /* provide the list of buffers to the completion callbacks */
    1932     2409718 :         pgaio_io_set_handle_data_32(ioh, (uint32 *) io_buffers, io_buffers_len);
    1933             : 
    1934     2409718 :         pgaio_io_register_callbacks(ioh,
    1935             :                                     persistence == RELPERSISTENCE_TEMP ?
    1936             :                                     PGAIO_HCB_LOCAL_BUFFER_READV :
    1937             :                                     PGAIO_HCB_SHARED_BUFFER_READV,
    1938             :                                     flags);
    1939             : 
    1940     2409718 :         pgaio_io_set_flag(ioh, ioh_flags);
    1941             : 
    1942             :         /* ---
    1943             :          * Even though we're trying to issue IO asynchronously, track the time
    1944             :          * in smgrstartreadv():
    1945             :          * - if io_method == IOMETHOD_SYNC, we will always perform the IO
    1946             :          *   immediately
    1947             :          * - the io method might not support the IO (e.g. worker IO for a temp
    1948             :          *   table)
    1949             :          * ---
    1950             :          */
    1951     2409718 :         io_start = pgstat_prepare_io_time(track_io_timing);
    1952     2409718 :         smgrstartreadv(ioh, operation->smgr, forknum,
    1953             :                        blocknum + nblocks_done,
    1954             :                        io_pages, io_buffers_len);
    1955     2409688 :         pgstat_count_io_op_time(io_object, io_context, IOOP_READ,
    1956     2409688 :                                 io_start, 1, io_buffers_len * BLCKSZ);
    1957             : 
    1958     2409688 :         if (persistence == RELPERSISTENCE_TEMP)
    1959        3528 :             pgBufferUsage.local_blks_read += io_buffers_len;
    1960             :         else
    1961     2406160 :             pgBufferUsage.shared_blks_read += io_buffers_len;
    1962             : 
    1963             :         /*
    1964             :          * Track vacuum cost when issuing IO, not after waiting for it.
    1965             :          * Otherwise we could end up issuing a lot of IO in a short timespan,
    1966             :          * despite a low cost limit.
    1967             :          */
    1968     2409688 :         if (VacuumCostActive)
    1969       35602 :             VacuumCostBalance += VacuumCostPageMiss * io_buffers_len;
    1970             : 
    1971     2409688 :         *nblocks_progress = io_buffers_len;
    1972     2409688 :         did_start_io = true;
    1973             :     }
    1974             : 
    1975     2414672 :     return did_start_io;
    1976             : }
    1977             : 
    1978             : /*
    1979             :  * BufferAlloc -- subroutine for PinBufferForBlock.  Handles lookup of a shared
    1980             :  *      buffer.  If no buffer exists already, selects a replacement victim and
    1981             :  *      evicts the old page, but does NOT read in new page.
    1982             :  *
    1983             :  * "strategy" can be a buffer replacement strategy object, or NULL for
    1984             :  * the default strategy.  The selected buffer's usage_count is advanced when
    1985             :  * using the default strategy, but otherwise possibly not (see PinBuffer).
    1986             :  *
    1987             :  * The returned buffer is pinned and is already marked as holding the
    1988             :  * desired page.  If it already did have the desired page, *foundPtr is
    1989             :  * set true.  Otherwise, *foundPtr is set false.
    1990             :  *
    1991             :  * io_context is passed as an output parameter to avoid calling
    1992             :  * IOContextForStrategy() when there is a shared buffers hit and no IO
    1993             :  * statistics need be captured.
    1994             :  *
    1995             :  * No locks are held either at entry or exit.
    1996             :  */
    1997             : static pg_attribute_always_inline BufferDesc *
    1998   116105386 : BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
    1999             :             BlockNumber blockNum,
    2000             :             BufferAccessStrategy strategy,
    2001             :             bool *foundPtr, IOContext io_context)
    2002             : {
    2003             :     BufferTag   newTag;         /* identity of requested block */
    2004             :     uint32      newHash;        /* hash value for newTag */
    2005             :     LWLock     *newPartitionLock;   /* buffer partition lock for it */
    2006             :     int         existing_buf_id;
    2007             :     Buffer      victim_buffer;
    2008             :     BufferDesc *victim_buf_hdr;
    2009             :     uint32      victim_buf_state;
    2010             : 
    2011             :     /* Make sure we will have room to remember the buffer pin */
    2012   116105386 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    2013   116105386 :     ReservePrivateRefCountEntry();
    2014             : 
    2015             :     /* create a tag so we can lookup the buffer */
    2016   116105386 :     InitBufferTag(&newTag, &smgr->smgr_rlocator.locator, forkNum, blockNum);
    2017             : 
    2018             :     /* determine its hash code and partition lock ID */
    2019   116105386 :     newHash = BufTableHashCode(&newTag);
    2020   116105386 :     newPartitionLock = BufMappingPartitionLock(newHash);
    2021             : 
    2022             :     /* see if the block is in the buffer pool already */
    2023   116105386 :     LWLockAcquire(newPartitionLock, LW_SHARED);
    2024   116105386 :     existing_buf_id = BufTableLookup(&newTag, newHash);
    2025   116105386 :     if (existing_buf_id >= 0)
    2026             :     {
    2027             :         BufferDesc *buf;
    2028             :         bool        valid;
    2029             : 
    2030             :         /*
    2031             :          * Found it.  Now, pin the buffer so no one can steal it from the
    2032             :          * buffer pool, and check to see if the correct data has been loaded
    2033             :          * into the buffer.
    2034             :          */
    2035   112853766 :         buf = GetBufferDescriptor(existing_buf_id);
    2036             : 
    2037   112853766 :         valid = PinBuffer(buf, strategy);
    2038             : 
    2039             :         /* Can release the mapping lock as soon as we've pinned it */
    2040   112853766 :         LWLockRelease(newPartitionLock);
    2041             : 
    2042   112853766 :         *foundPtr = true;
    2043             : 
    2044   112853766 :         if (!valid)
    2045             :         {
    2046             :             /*
    2047             :              * We can only get here if (a) someone else is still reading in
    2048             :              * the page, (b) a previous read attempt failed, or (c) someone
    2049             :              * called StartReadBuffers() but not yet WaitReadBuffers().
    2050             :              */
    2051        4362 :             *foundPtr = false;
    2052             :         }
    2053             : 
    2054   112853766 :         return buf;
    2055             :     }
    2056             : 
    2057             :     /*
    2058             :      * Didn't find it in the buffer pool.  We'll have to initialize a new
    2059             :      * buffer.  Remember to unlock the mapping lock while doing the work.
    2060             :      */
    2061     3251620 :     LWLockRelease(newPartitionLock);
    2062             : 
    2063             :     /*
    2064             :      * Acquire a victim buffer. Somebody else might try to do the same, we
    2065             :      * don't hold any conflicting locks. If so we'll have to undo our work
    2066             :      * later.
    2067             :      */
    2068     3251620 :     victim_buffer = GetVictimBuffer(strategy, io_context);
    2069     3251620 :     victim_buf_hdr = GetBufferDescriptor(victim_buffer - 1);
    2070             : 
    2071             :     /*
    2072             :      * Try to make a hashtable entry for the buffer under its new tag. If
    2073             :      * somebody else inserted another buffer for the tag, we'll release the
    2074             :      * victim buffer we acquired and use the already inserted one.
    2075             :      */
    2076     3251620 :     LWLockAcquire(newPartitionLock, LW_EXCLUSIVE);
    2077     3251620 :     existing_buf_id = BufTableInsert(&newTag, newHash, victim_buf_hdr->buf_id);
    2078     3251620 :     if (existing_buf_id >= 0)
    2079             :     {
    2080             :         BufferDesc *existing_buf_hdr;
    2081             :         bool        valid;
    2082             : 
    2083             :         /*
    2084             :          * Got a collision. Someone has already done what we were about to do.
    2085             :          * We'll just handle this as if it were found in the buffer pool in
    2086             :          * the first place.  First, give up the buffer we were planning to
    2087             :          * use.
    2088             :          *
    2089             :          * We could do this after releasing the partition lock, but then we'd
    2090             :          * have to call ResourceOwnerEnlarge() & ReservePrivateRefCountEntry()
    2091             :          * before acquiring the lock, for the rare case of such a collision.
    2092             :          */
    2093        1040 :         UnpinBuffer(victim_buf_hdr);
    2094             : 
    2095             :         /*
    2096             :          * The victim buffer we acquired previously is clean and unused, let
    2097             :          * it be found again quickly
    2098             :          */
    2099        1040 :         StrategyFreeBuffer(victim_buf_hdr);
    2100             : 
    2101             :         /* remaining code should match code at top of routine */
    2102             : 
    2103        1040 :         existing_buf_hdr = GetBufferDescriptor(existing_buf_id);
    2104             : 
    2105        1040 :         valid = PinBuffer(existing_buf_hdr, strategy);
    2106             : 
    2107             :         /* Can release the mapping lock as soon as we've pinned it */
    2108        1040 :         LWLockRelease(newPartitionLock);
    2109             : 
    2110        1040 :         *foundPtr = true;
    2111             : 
    2112        1040 :         if (!valid)
    2113             :         {
    2114             :             /*
    2115             :              * We can only get here if (a) someone else is still reading in
    2116             :              * the page, (b) a previous read attempt failed, or (c) someone
    2117             :              * called StartReadBuffers() but not yet WaitReadBuffers().
    2118             :              */
    2119         718 :             *foundPtr = false;
    2120             :         }
    2121             : 
    2122        1040 :         return existing_buf_hdr;
    2123             :     }
    2124             : 
    2125             :     /*
    2126             :      * Need to lock the buffer header too in order to change its tag.
    2127             :      */
    2128     3250580 :     victim_buf_state = LockBufHdr(victim_buf_hdr);
    2129             : 
    2130             :     /* some sanity checks while we hold the buffer header lock */
    2131             :     Assert(BUF_STATE_GET_REFCOUNT(victim_buf_state) == 1);
    2132             :     Assert(!(victim_buf_state & (BM_TAG_VALID | BM_VALID | BM_DIRTY | BM_IO_IN_PROGRESS)));
    2133             : 
    2134     3250580 :     victim_buf_hdr->tag = newTag;
    2135             : 
    2136             :     /*
    2137             :      * Make sure BM_PERMANENT is set for buffers that must be written at every
    2138             :      * checkpoint.  Unlogged buffers only need to be written at shutdown
    2139             :      * checkpoints, except for their "init" forks, which need to be treated
    2140             :      * just like permanent relations.
    2141             :      */
    2142     3250580 :     victim_buf_state |= BM_TAG_VALID | BUF_USAGECOUNT_ONE;
    2143     3250580 :     if (relpersistence == RELPERSISTENCE_PERMANENT || forkNum == INIT_FORKNUM)
    2144     3249898 :         victim_buf_state |= BM_PERMANENT;
    2145             : 
    2146     3250580 :     UnlockBufHdr(victim_buf_hdr, victim_buf_state);
    2147             : 
    2148     3250580 :     LWLockRelease(newPartitionLock);
    2149             : 
    2150             :     /*
    2151             :      * Buffer contents are currently invalid.
    2152             :      */
    2153     3250580 :     *foundPtr = false;
    2154             : 
    2155     3250580 :     return victim_buf_hdr;
    2156             : }
    2157             : 
    2158             : /*
    2159             :  * InvalidateBuffer -- mark a shared buffer invalid and return it to the
    2160             :  * freelist.
    2161             :  *
    2162             :  * The buffer header spinlock must be held at entry.  We drop it before
    2163             :  * returning.  (This is sane because the caller must have locked the
    2164             :  * buffer in order to be sure it should be dropped.)
    2165             :  *
    2166             :  * This is used only in contexts such as dropping a relation.  We assume
    2167             :  * that no other backend could possibly be interested in using the page,
    2168             :  * so the only reason the buffer might be pinned is if someone else is
    2169             :  * trying to write it out.  We have to let them finish before we can
    2170             :  * reclaim the buffer.
    2171             :  *
    2172             :  * The buffer could get reclaimed by someone else while we are waiting
    2173             :  * to acquire the necessary locks; if so, don't mess it up.
    2174             :  */
    2175             : static void
    2176      213316 : InvalidateBuffer(BufferDesc *buf)
    2177             : {
    2178             :     BufferTag   oldTag;
    2179             :     uint32      oldHash;        /* hash value for oldTag */
    2180             :     LWLock     *oldPartitionLock;   /* buffer partition lock for it */
    2181             :     uint32      oldFlags;
    2182             :     uint32      buf_state;
    2183             : 
    2184             :     /* Save the original buffer tag before dropping the spinlock */
    2185      213316 :     oldTag = buf->tag;
    2186             : 
    2187      213316 :     buf_state = pg_atomic_read_u32(&buf->state);
    2188             :     Assert(buf_state & BM_LOCKED);
    2189      213316 :     UnlockBufHdr(buf, buf_state);
    2190             : 
    2191             :     /*
    2192             :      * Need to compute the old tag's hashcode and partition lock ID. XXX is it
    2193             :      * worth storing the hashcode in BufferDesc so we need not recompute it
    2194             :      * here?  Probably not.
    2195             :      */
    2196      213316 :     oldHash = BufTableHashCode(&oldTag);
    2197      213316 :     oldPartitionLock = BufMappingPartitionLock(oldHash);
    2198             : 
    2199      213316 : retry:
    2200             : 
    2201             :     /*
    2202             :      * Acquire exclusive mapping lock in preparation for changing the buffer's
    2203             :      * association.
    2204             :      */
    2205      213316 :     LWLockAcquire(oldPartitionLock, LW_EXCLUSIVE);
    2206             : 
    2207             :     /* Re-lock the buffer header */
    2208      213316 :     buf_state = LockBufHdr(buf);
    2209             : 
    2210             :     /* If it's changed while we were waiting for lock, do nothing */
    2211      213316 :     if (!BufferTagsEqual(&buf->tag, &oldTag))
    2212             :     {
    2213           2 :         UnlockBufHdr(buf, buf_state);
    2214           2 :         LWLockRelease(oldPartitionLock);
    2215           2 :         return;
    2216             :     }
    2217             : 
    2218             :     /*
    2219             :      * We assume the reason for it to be pinned is that either we were
    2220             :      * asynchronously reading the page in before erroring out or someone else
    2221             :      * is flushing the page out.  Wait for the IO to finish.  (This could be
    2222             :      * an infinite loop if the refcount is messed up... it would be nice to
    2223             :      * time out after awhile, but there seems no way to be sure how many loops
    2224             :      * may be needed.  Note that if the other guy has pinned the buffer but
    2225             :      * not yet done StartBufferIO, WaitIO will fall through and we'll
    2226             :      * effectively be busy-looping here.)
    2227             :      */
    2228      213314 :     if (BUF_STATE_GET_REFCOUNT(buf_state) != 0)
    2229             :     {
    2230           0 :         UnlockBufHdr(buf, buf_state);
    2231           0 :         LWLockRelease(oldPartitionLock);
    2232             :         /* safety check: should definitely not be our *own* pin */
    2233           0 :         if (GetPrivateRefCount(BufferDescriptorGetBuffer(buf)) > 0)
    2234           0 :             elog(ERROR, "buffer is pinned in InvalidateBuffer");
    2235           0 :         WaitIO(buf);
    2236           0 :         goto retry;
    2237             :     }
    2238             : 
    2239             :     /*
    2240             :      * Clear out the buffer's tag and flags.  We must do this to ensure that
    2241             :      * linear scans of the buffer array don't think the buffer is valid.
    2242             :      */
    2243      213314 :     oldFlags = buf_state & BUF_FLAG_MASK;
    2244      213314 :     ClearBufferTag(&buf->tag);
    2245      213314 :     buf_state &= ~(BUF_FLAG_MASK | BUF_USAGECOUNT_MASK);
    2246      213314 :     UnlockBufHdr(buf, buf_state);
    2247             : 
    2248             :     /*
    2249             :      * Remove the buffer from the lookup hashtable, if it was in there.
    2250             :      */
    2251      213314 :     if (oldFlags & BM_TAG_VALID)
    2252      213314 :         BufTableDelete(&oldTag, oldHash);
    2253             : 
    2254             :     /*
    2255             :      * Done with mapping lock.
    2256             :      */
    2257      213314 :     LWLockRelease(oldPartitionLock);
    2258             : 
    2259             :     /*
    2260             :      * Insert the buffer at the head of the list of free buffers.
    2261             :      */
    2262      213314 :     StrategyFreeBuffer(buf);
    2263             : }
    2264             : 
    2265             : /*
    2266             :  * Helper routine for GetVictimBuffer()
    2267             :  *
    2268             :  * Needs to be called on a buffer with a valid tag, pinned, but without the
    2269             :  * buffer header spinlock held.
    2270             :  *
    2271             :  * Returns true if the buffer can be reused, in which case the buffer is only
    2272             :  * pinned by this backend and marked as invalid, false otherwise.
    2273             :  */
    2274             : static bool
    2275     2318072 : InvalidateVictimBuffer(BufferDesc *buf_hdr)
    2276             : {
    2277             :     uint32      buf_state;
    2278             :     uint32      hash;
    2279             :     LWLock     *partition_lock;
    2280             :     BufferTag   tag;
    2281             : 
    2282             :     Assert(GetPrivateRefCount(BufferDescriptorGetBuffer(buf_hdr)) == 1);
    2283             : 
    2284             :     /* have buffer pinned, so it's safe to read tag without lock */
    2285     2318072 :     tag = buf_hdr->tag;
    2286             : 
    2287     2318072 :     hash = BufTableHashCode(&tag);
    2288     2318072 :     partition_lock = BufMappingPartitionLock(hash);
    2289             : 
    2290     2318072 :     LWLockAcquire(partition_lock, LW_EXCLUSIVE);
    2291             : 
    2292             :     /* lock the buffer header */
    2293     2318072 :     buf_state = LockBufHdr(buf_hdr);
    2294             : 
    2295             :     /*
    2296             :      * We have the buffer pinned nobody else should have been able to unset
    2297             :      * this concurrently.
    2298             :      */
    2299             :     Assert(buf_state & BM_TAG_VALID);
    2300             :     Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    2301             :     Assert(BufferTagsEqual(&buf_hdr->tag, &tag));
    2302             : 
    2303             :     /*
    2304             :      * If somebody else pinned the buffer since, or even worse, dirtied it,
    2305             :      * give up on this buffer: It's clearly in use.
    2306             :      */
    2307     2318072 :     if (BUF_STATE_GET_REFCOUNT(buf_state) != 1 || (buf_state & BM_DIRTY))
    2308             :     {
    2309             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    2310             : 
    2311         568 :         UnlockBufHdr(buf_hdr, buf_state);
    2312         568 :         LWLockRelease(partition_lock);
    2313             : 
    2314         568 :         return false;
    2315             :     }
    2316             : 
    2317             :     /*
    2318             :      * Clear out the buffer's tag and flags and usagecount.  This is not
    2319             :      * strictly required, as BM_TAG_VALID/BM_VALID needs to be checked before
    2320             :      * doing anything with the buffer. But currently it's beneficial, as the
    2321             :      * cheaper pre-check for several linear scans of shared buffers use the
    2322             :      * tag (see e.g. FlushDatabaseBuffers()).
    2323             :      */
    2324     2317504 :     ClearBufferTag(&buf_hdr->tag);
    2325     2317504 :     buf_state &= ~(BUF_FLAG_MASK | BUF_USAGECOUNT_MASK);
    2326     2317504 :     UnlockBufHdr(buf_hdr, buf_state);
    2327             : 
    2328             :     Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    2329             : 
    2330             :     /* finally delete buffer from the buffer mapping table */
    2331     2317504 :     BufTableDelete(&tag, hash);
    2332             : 
    2333     2317504 :     LWLockRelease(partition_lock);
    2334             : 
    2335             :     Assert(!(buf_state & (BM_DIRTY | BM_VALID | BM_TAG_VALID)));
    2336             :     Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    2337             :     Assert(BUF_STATE_GET_REFCOUNT(pg_atomic_read_u32(&buf_hdr->state)) > 0);
    2338             : 
    2339     2317504 :     return true;
    2340             : }
    2341             : 
    2342             : static Buffer
    2343     3703812 : GetVictimBuffer(BufferAccessStrategy strategy, IOContext io_context)
    2344             : {
    2345             :     BufferDesc *buf_hdr;
    2346             :     Buffer      buf;
    2347             :     uint32      buf_state;
    2348             :     bool        from_ring;
    2349             : 
    2350             :     /*
    2351             :      * Ensure, while the spinlock's not yet held, that there's a free refcount
    2352             :      * entry, and a resource owner slot for the pin.
    2353             :      */
    2354     3703812 :     ReservePrivateRefCountEntry();
    2355     3703812 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    2356             : 
    2357             :     /* we return here if a prospective victim buffer gets used concurrently */
    2358     3715934 : again:
    2359             : 
    2360             :     /*
    2361             :      * Select a victim buffer.  The buffer is returned with its header
    2362             :      * spinlock still held!
    2363             :      */
    2364     3715934 :     buf_hdr = StrategyGetBuffer(strategy, &buf_state, &from_ring);
    2365     3715934 :     buf = BufferDescriptorGetBuffer(buf_hdr);
    2366             : 
    2367             :     Assert(BUF_STATE_GET_REFCOUNT(buf_state) == 0);
    2368             : 
    2369             :     /* Pin the buffer and then release the buffer spinlock */
    2370     3715934 :     PinBuffer_Locked(buf_hdr);
    2371             : 
    2372             :     /*
    2373             :      * We shouldn't have any other pins for this buffer.
    2374             :      */
    2375     3715934 :     CheckBufferIsPinnedOnce(buf);
    2376             : 
    2377             :     /*
    2378             :      * If the buffer was dirty, try to write it out.  There is a race
    2379             :      * condition here, in that someone might dirty it after we released the
    2380             :      * buffer header lock above, or even while we are writing it out (since
    2381             :      * our share-lock won't prevent hint-bit updates).  We will recheck the
    2382             :      * dirty bit after re-locking the buffer header.
    2383             :      */
    2384     3715934 :     if (buf_state & BM_DIRTY)
    2385             :     {
    2386             :         LWLock     *content_lock;
    2387             : 
    2388             :         Assert(buf_state & BM_TAG_VALID);
    2389             :         Assert(buf_state & BM_VALID);
    2390             : 
    2391             :         /*
    2392             :          * We need a share-lock on the buffer contents to write it out (else
    2393             :          * we might write invalid data, eg because someone else is compacting
    2394             :          * the page contents while we write).  We must use a conditional lock
    2395             :          * acquisition here to avoid deadlock.  Even though the buffer was not
    2396             :          * pinned (and therefore surely not locked) when StrategyGetBuffer
    2397             :          * returned it, someone else could have pinned and exclusive-locked it
    2398             :          * by the time we get here. If we try to get the lock unconditionally,
    2399             :          * we'd block waiting for them; if they later block waiting for us,
    2400             :          * deadlock ensues. (This has been observed to happen when two
    2401             :          * backends are both trying to split btree index pages, and the second
    2402             :          * one just happens to be trying to split the page the first one got
    2403             :          * from StrategyGetBuffer.)
    2404             :          */
    2405      506638 :         content_lock = BufferDescriptorGetContentLock(buf_hdr);
    2406      506638 :         if (!LWLockConditionalAcquire(content_lock, LW_SHARED))
    2407             :         {
    2408             :             /*
    2409             :              * Someone else has locked the buffer, so give it up and loop back
    2410             :              * to get another one.
    2411             :              */
    2412           0 :             UnpinBuffer(buf_hdr);
    2413           0 :             goto again;
    2414             :         }
    2415             : 
    2416             :         /*
    2417             :          * If using a nondefault strategy, and writing the buffer would
    2418             :          * require a WAL flush, let the strategy decide whether to go ahead
    2419             :          * and write/reuse the buffer or to choose another victim.  We need a
    2420             :          * lock to inspect the page LSN, so this can't be done inside
    2421             :          * StrategyGetBuffer.
    2422             :          */
    2423      506638 :         if (strategy != NULL)
    2424             :         {
    2425             :             XLogRecPtr  lsn;
    2426             : 
    2427             :             /* Read the LSN while holding buffer header lock */
    2428      149680 :             buf_state = LockBufHdr(buf_hdr);
    2429      149680 :             lsn = BufferGetLSN(buf_hdr);
    2430      149680 :             UnlockBufHdr(buf_hdr, buf_state);
    2431             : 
    2432      149680 :             if (XLogNeedsFlush(lsn)
    2433       17434 :                 && StrategyRejectBuffer(strategy, buf_hdr, from_ring))
    2434             :             {
    2435       11554 :                 LWLockRelease(content_lock);
    2436       11554 :                 UnpinBuffer(buf_hdr);
    2437       11554 :                 goto again;
    2438             :             }
    2439             :         }
    2440             : 
    2441             :         /* OK, do the I/O */
    2442      495084 :         FlushBuffer(buf_hdr, NULL, IOOBJECT_RELATION, io_context);
    2443      495084 :         LWLockRelease(content_lock);
    2444             : 
    2445      495084 :         ScheduleBufferTagForWriteback(&BackendWritebackContext, io_context,
    2446             :                                       &buf_hdr->tag);
    2447             :     }
    2448             : 
    2449             : 
    2450     3704380 :     if (buf_state & BM_VALID)
    2451             :     {
    2452             :         /*
    2453             :          * When a BufferAccessStrategy is in use, blocks evicted from shared
    2454             :          * buffers are counted as IOOP_EVICT in the corresponding context
    2455             :          * (e.g. IOCONTEXT_BULKWRITE). Shared buffers are evicted by a
    2456             :          * strategy in two cases: 1) while initially claiming buffers for the
    2457             :          * strategy ring 2) to replace an existing strategy ring buffer
    2458             :          * because it is pinned or in use and cannot be reused.
    2459             :          *
    2460             :          * Blocks evicted from buffers already in the strategy ring are
    2461             :          * counted as IOOP_REUSE in the corresponding strategy context.
    2462             :          *
    2463             :          * At this point, we can accurately count evictions and reuses,
    2464             :          * because we have successfully claimed the valid buffer. Previously,
    2465             :          * we may have been forced to release the buffer due to concurrent
    2466             :          * pinners or erroring out.
    2467             :          */
    2468     2317794 :         pgstat_count_io_op(IOOBJECT_RELATION, io_context,
    2469     2317794 :                            from_ring ? IOOP_REUSE : IOOP_EVICT, 1, 0);
    2470             :     }
    2471             : 
    2472             :     /*
    2473             :      * If the buffer has an entry in the buffer mapping table, delete it. This
    2474             :      * can fail because another backend could have pinned or dirtied the
    2475             :      * buffer.
    2476             :      */
    2477     3704380 :     if ((buf_state & BM_TAG_VALID) && !InvalidateVictimBuffer(buf_hdr))
    2478             :     {
    2479         568 :         UnpinBuffer(buf_hdr);
    2480         568 :         goto again;
    2481             :     }
    2482             : 
    2483             :     /* a final set of sanity checks */
    2484             : #ifdef USE_ASSERT_CHECKING
    2485             :     buf_state = pg_atomic_read_u32(&buf_hdr->state);
    2486             : 
    2487             :     Assert(BUF_STATE_GET_REFCOUNT(buf_state) == 1);
    2488             :     Assert(!(buf_state & (BM_TAG_VALID | BM_VALID | BM_DIRTY)));
    2489             : 
    2490             :     CheckBufferIsPinnedOnce(buf);
    2491             : #endif
    2492             : 
    2493     3703812 :     return buf;
    2494             : }
    2495             : 
    2496             : /*
    2497             :  * Return the maximum number of buffers that a backend should try to pin once,
    2498             :  * to avoid exceeding its fair share.  This is the highest value that
    2499             :  * GetAdditionalPinLimit() could ever return.  Note that it may be zero on a
    2500             :  * system with a very small buffer pool relative to max_connections.
    2501             :  */
    2502             : uint32
    2503      985178 : GetPinLimit(void)
    2504             : {
    2505      985178 :     return MaxProportionalPins;
    2506             : }
    2507             : 
    2508             : /*
    2509             :  * Return the maximum number of additional buffers that this backend should
    2510             :  * pin if it wants to stay under the per-backend limit, considering the number
    2511             :  * of buffers it has already pinned.  Unlike LimitAdditionalPins(), the limit
    2512             :  * return by this function can be zero.
    2513             :  */
    2514             : uint32
    2515     7095920 : GetAdditionalPinLimit(void)
    2516             : {
    2517             :     uint32      estimated_pins_held;
    2518             : 
    2519             :     /*
    2520             :      * We get the number of "overflowed" pins for free, but don't know the
    2521             :      * number of pins in PrivateRefCountArray.  The cost of calculating that
    2522             :      * exactly doesn't seem worth it, so just assume the max.
    2523             :      */
    2524     7095920 :     estimated_pins_held = PrivateRefCountOverflowed + REFCOUNT_ARRAY_ENTRIES;
    2525             : 
    2526             :     /* Is this backend already holding more than its fair share? */
    2527     7095920 :     if (estimated_pins_held > MaxProportionalPins)
    2528     2449822 :         return 0;
    2529             : 
    2530     4646098 :     return MaxProportionalPins - estimated_pins_held;
    2531             : }
    2532             : 
    2533             : /*
    2534             :  * Limit the number of pins a batch operation may additionally acquire, to
    2535             :  * avoid running out of pinnable buffers.
    2536             :  *
    2537             :  * One additional pin is always allowed, on the assumption that the operation
    2538             :  * requires at least one to make progress.
    2539             :  */
    2540             : void
    2541      400496 : LimitAdditionalPins(uint32 *additional_pins)
    2542             : {
    2543             :     uint32      limit;
    2544             : 
    2545      400496 :     if (*additional_pins <= 1)
    2546      380404 :         return;
    2547             : 
    2548       20092 :     limit = GetAdditionalPinLimit();
    2549       20092 :     limit = Max(limit, 1);
    2550       20092 :     if (limit < *additional_pins)
    2551       10902 :         *additional_pins = limit;
    2552             : }
    2553             : 
    2554             : /*
    2555             :  * Logic shared between ExtendBufferedRelBy(), ExtendBufferedRelTo(). Just to
    2556             :  * avoid duplicating the tracing and relpersistence related logic.
    2557             :  */
    2558             : static BlockNumber
    2559      423200 : ExtendBufferedRelCommon(BufferManagerRelation bmr,
    2560             :                         ForkNumber fork,
    2561             :                         BufferAccessStrategy strategy,
    2562             :                         uint32 flags,
    2563             :                         uint32 extend_by,
    2564             :                         BlockNumber extend_upto,
    2565             :                         Buffer *buffers,
    2566             :                         uint32 *extended_by)
    2567             : {
    2568             :     BlockNumber first_block;
    2569             : 
    2570             :     TRACE_POSTGRESQL_BUFFER_EXTEND_START(fork,
    2571             :                                          bmr.smgr->smgr_rlocator.locator.spcOid,
    2572             :                                          bmr.smgr->smgr_rlocator.locator.dbOid,
    2573             :                                          bmr.smgr->smgr_rlocator.locator.relNumber,
    2574             :                                          bmr.smgr->smgr_rlocator.backend,
    2575             :                                          extend_by);
    2576             : 
    2577      423200 :     if (bmr.relpersistence == RELPERSISTENCE_TEMP)
    2578       22704 :         first_block = ExtendBufferedRelLocal(bmr, fork, flags,
    2579             :                                              extend_by, extend_upto,
    2580             :                                              buffers, &extend_by);
    2581             :     else
    2582      400496 :         first_block = ExtendBufferedRelShared(bmr, fork, strategy, flags,
    2583             :                                               extend_by, extend_upto,
    2584             :                                               buffers, &extend_by);
    2585      423200 :     *extended_by = extend_by;
    2586             : 
    2587             :     TRACE_POSTGRESQL_BUFFER_EXTEND_DONE(fork,
    2588             :                                         bmr.smgr->smgr_rlocator.locator.spcOid,
    2589             :                                         bmr.smgr->smgr_rlocator.locator.dbOid,
    2590             :                                         bmr.smgr->smgr_rlocator.locator.relNumber,
    2591             :                                         bmr.smgr->smgr_rlocator.backend,
    2592             :                                         *extended_by,
    2593             :                                         first_block);
    2594             : 
    2595      423200 :     return first_block;
    2596             : }
    2597             : 
    2598             : /*
    2599             :  * Implementation of ExtendBufferedRelBy() and ExtendBufferedRelTo() for
    2600             :  * shared buffers.
    2601             :  */
    2602             : static BlockNumber
    2603      400496 : ExtendBufferedRelShared(BufferManagerRelation bmr,
    2604             :                         ForkNumber fork,
    2605             :                         BufferAccessStrategy strategy,
    2606             :                         uint32 flags,
    2607             :                         uint32 extend_by,
    2608             :                         BlockNumber extend_upto,
    2609             :                         Buffer *buffers,
    2610             :                         uint32 *extended_by)
    2611             : {
    2612             :     BlockNumber first_block;
    2613      400496 :     IOContext   io_context = IOContextForStrategy(strategy);
    2614             :     instr_time  io_start;
    2615             : 
    2616      400496 :     LimitAdditionalPins(&extend_by);
    2617             : 
    2618             :     /*
    2619             :      * Acquire victim buffers for extension without holding extension lock.
    2620             :      * Writing out victim buffers is the most expensive part of extending the
    2621             :      * relation, particularly when doing so requires WAL flushes. Zeroing out
    2622             :      * the buffers is also quite expensive, so do that before holding the
    2623             :      * extension lock as well.
    2624             :      *
    2625             :      * These pages are pinned by us and not valid. While we hold the pin they
    2626             :      * can't be acquired as victim buffers by another backend.
    2627             :      */
    2628      852688 :     for (uint32 i = 0; i < extend_by; i++)
    2629             :     {
    2630             :         Block       buf_block;
    2631             : 
    2632      452192 :         buffers[i] = GetVictimBuffer(strategy, io_context);
    2633      452192 :         buf_block = BufHdrGetBlock(GetBufferDescriptor(buffers[i] - 1));
    2634             : 
    2635             :         /* new buffers are zero-filled */
    2636      452192 :         MemSet(buf_block, 0, BLCKSZ);
    2637             :     }
    2638             : 
    2639             :     /*
    2640             :      * Lock relation against concurrent extensions, unless requested not to.
    2641             :      *
    2642             :      * We use the same extension lock for all forks. That's unnecessarily
    2643             :      * restrictive, but currently extensions for forks don't happen often
    2644             :      * enough to make it worth locking more granularly.
    2645             :      *
    2646             :      * Note that another backend might have extended the relation by the time
    2647             :      * we get the lock.
    2648             :      */
    2649      400496 :     if (!(flags & EB_SKIP_EXTENSION_LOCK))
    2650      298696 :         LockRelationForExtension(bmr.rel, ExclusiveLock);
    2651             : 
    2652             :     /*
    2653             :      * If requested, invalidate size cache, so that smgrnblocks asks the
    2654             :      * kernel.
    2655             :      */
    2656      400496 :     if (flags & EB_CLEAR_SIZE_CACHE)
    2657       14998 :         bmr.smgr->smgr_cached_nblocks[fork] = InvalidBlockNumber;
    2658             : 
    2659      400496 :     first_block = smgrnblocks(bmr.smgr, fork);
    2660             : 
    2661             :     /*
    2662             :      * Now that we have the accurate relation size, check if the caller wants
    2663             :      * us to extend to only up to a specific size. If there were concurrent
    2664             :      * extensions, we might have acquired too many buffers and need to release
    2665             :      * them.
    2666             :      */
    2667      400496 :     if (extend_upto != InvalidBlockNumber)
    2668             :     {
    2669      104146 :         uint32      orig_extend_by = extend_by;
    2670             : 
    2671      104146 :         if (first_block > extend_upto)
    2672           0 :             extend_by = 0;
    2673      104146 :         else if ((uint64) first_block + extend_by > extend_upto)
    2674          26 :             extend_by = extend_upto - first_block;
    2675             : 
    2676      104206 :         for (uint32 i = extend_by; i < orig_extend_by; i++)
    2677             :         {
    2678          60 :             BufferDesc *buf_hdr = GetBufferDescriptor(buffers[i] - 1);
    2679             : 
    2680             :             /*
    2681             :              * The victim buffer we acquired previously is clean and unused,
    2682             :              * let it be found again quickly
    2683             :              */
    2684          60 :             StrategyFreeBuffer(buf_hdr);
    2685          60 :             UnpinBuffer(buf_hdr);
    2686             :         }
    2687             : 
    2688      104146 :         if (extend_by == 0)
    2689             :         {
    2690          26 :             if (!(flags & EB_SKIP_EXTENSION_LOCK))
    2691          26 :                 UnlockRelationForExtension(bmr.rel, ExclusiveLock);
    2692          26 :             *extended_by = extend_by;
    2693          26 :             return first_block;
    2694             :         }
    2695             :     }
    2696             : 
    2697             :     /* Fail if relation is already at maximum possible length */
    2698      400470 :     if ((uint64) first_block + extend_by >= MaxBlockNumber)
    2699           0 :         ereport(ERROR,
    2700             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    2701             :                  errmsg("cannot extend relation %s beyond %u blocks",
    2702             :                         relpath(bmr.smgr->smgr_rlocator, fork).str,
    2703             :                         MaxBlockNumber)));
    2704             : 
    2705             :     /*
    2706             :      * Insert buffers into buffer table, mark as IO_IN_PROGRESS.
    2707             :      *
    2708             :      * This needs to happen before we extend the relation, because as soon as
    2709             :      * we do, other backends can start to read in those pages.
    2710             :      */
    2711      852602 :     for (uint32 i = 0; i < extend_by; i++)
    2712             :     {
    2713      452132 :         Buffer      victim_buf = buffers[i];
    2714      452132 :         BufferDesc *victim_buf_hdr = GetBufferDescriptor(victim_buf - 1);
    2715             :         BufferTag   tag;
    2716             :         uint32      hash;
    2717             :         LWLock     *partition_lock;
    2718             :         int         existing_id;
    2719             : 
    2720             :         /* in case we need to pin an existing buffer below */
    2721      452132 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    2722      452132 :         ReservePrivateRefCountEntry();
    2723             : 
    2724      452132 :         InitBufferTag(&tag, &bmr.smgr->smgr_rlocator.locator, fork, first_block + i);
    2725      452132 :         hash = BufTableHashCode(&tag);
    2726      452132 :         partition_lock = BufMappingPartitionLock(hash);
    2727             : 
    2728      452132 :         LWLockAcquire(partition_lock, LW_EXCLUSIVE);
    2729             : 
    2730      452132 :         existing_id = BufTableInsert(&tag, hash, victim_buf_hdr->buf_id);
    2731             : 
    2732             :         /*
    2733             :          * We get here only in the corner case where we are trying to extend
    2734             :          * the relation but we found a pre-existing buffer. This can happen
    2735             :          * because a prior attempt at extending the relation failed, and
    2736             :          * because mdread doesn't complain about reads beyond EOF (when
    2737             :          * zero_damaged_pages is ON) and so a previous attempt to read a block
    2738             :          * beyond EOF could have left a "valid" zero-filled buffer.
    2739             :          * Unfortunately, we have also seen this case occurring because of
    2740             :          * buggy Linux kernels that sometimes return an lseek(SEEK_END) result
    2741             :          * that doesn't account for a recent write. In that situation, the
    2742             :          * pre-existing buffer would contain valid data that we don't want to
    2743             :          * overwrite.  Since the legitimate cases should always have left a
    2744             :          * zero-filled buffer, complain if not PageIsNew.
    2745             :          */
    2746      452132 :         if (existing_id >= 0)
    2747             :         {
    2748           0 :             BufferDesc *existing_hdr = GetBufferDescriptor(existing_id);
    2749             :             Block       buf_block;
    2750             :             bool        valid;
    2751             : 
    2752             :             /*
    2753             :              * Pin the existing buffer before releasing the partition lock,
    2754             :              * preventing it from being evicted.
    2755             :              */
    2756           0 :             valid = PinBuffer(existing_hdr, strategy);
    2757             : 
    2758           0 :             LWLockRelease(partition_lock);
    2759             : 
    2760             :             /*
    2761             :              * The victim buffer we acquired previously is clean and unused,
    2762             :              * let it be found again quickly
    2763             :              */
    2764           0 :             StrategyFreeBuffer(victim_buf_hdr);
    2765           0 :             UnpinBuffer(victim_buf_hdr);
    2766             : 
    2767           0 :             buffers[i] = BufferDescriptorGetBuffer(existing_hdr);
    2768           0 :             buf_block = BufHdrGetBlock(existing_hdr);
    2769             : 
    2770           0 :             if (valid && !PageIsNew((Page) buf_block))
    2771           0 :                 ereport(ERROR,
    2772             :                         (errmsg("unexpected data beyond EOF in block %u of relation %s",
    2773             :                                 existing_hdr->tag.blockNum,
    2774             :                                 relpath(bmr.smgr->smgr_rlocator, fork).str),
    2775             :                          errhint("This has been seen to occur with buggy kernels; consider updating your system.")));
    2776             : 
    2777             :             /*
    2778             :              * We *must* do smgr[zero]extend before succeeding, else the page
    2779             :              * will not be reserved by the kernel, and the next P_NEW call
    2780             :              * will decide to return the same page.  Clear the BM_VALID bit,
    2781             :              * do StartBufferIO() and proceed.
    2782             :              *
    2783             :              * Loop to handle the very small possibility that someone re-sets
    2784             :              * BM_VALID between our clearing it and StartBufferIO inspecting
    2785             :              * it.
    2786             :              */
    2787             :             do
    2788             :             {
    2789           0 :                 uint32      buf_state = LockBufHdr(existing_hdr);
    2790             : 
    2791           0 :                 buf_state &= ~BM_VALID;
    2792           0 :                 UnlockBufHdr(existing_hdr, buf_state);
    2793           0 :             } while (!StartBufferIO(existing_hdr, true, false));
    2794             :         }
    2795             :         else
    2796             :         {
    2797             :             uint32      buf_state;
    2798             : 
    2799      452132 :             buf_state = LockBufHdr(victim_buf_hdr);
    2800             : 
    2801             :             /* some sanity checks while we hold the buffer header lock */
    2802             :             Assert(!(buf_state & (BM_VALID | BM_TAG_VALID | BM_DIRTY | BM_JUST_DIRTIED)));
    2803             :             Assert(BUF_STATE_GET_REFCOUNT(buf_state) == 1);
    2804             : 
    2805      452132 :             victim_buf_hdr->tag = tag;
    2806             : 
    2807      452132 :             buf_state |= BM_TAG_VALID | BUF_USAGECOUNT_ONE;
    2808      452132 :             if (bmr.relpersistence == RELPERSISTENCE_PERMANENT || fork == INIT_FORKNUM)
    2809      441558 :                 buf_state |= BM_PERMANENT;
    2810             : 
    2811      452132 :             UnlockBufHdr(victim_buf_hdr, buf_state);
    2812             : 
    2813      452132 :             LWLockRelease(partition_lock);
    2814             : 
    2815             :             /* XXX: could combine the locked operations in it with the above */
    2816      452132 :             StartBufferIO(victim_buf_hdr, true, false);
    2817             :         }
    2818             :     }
    2819             : 
    2820      400470 :     io_start = pgstat_prepare_io_time(track_io_timing);
    2821             : 
    2822             :     /*
    2823             :      * Note: if smgrzeroextend fails, we will end up with buffers that are
    2824             :      * allocated but not marked BM_VALID.  The next relation extension will
    2825             :      * still select the same block number (because the relation didn't get any
    2826             :      * longer on disk) and so future attempts to extend the relation will find
    2827             :      * the same buffers (if they have not been recycled) but come right back
    2828             :      * here to try smgrzeroextend again.
    2829             :      *
    2830             :      * We don't need to set checksum for all-zero pages.
    2831             :      */
    2832      400470 :     smgrzeroextend(bmr.smgr, fork, first_block, extend_by, false);
    2833             : 
    2834             :     /*
    2835             :      * Release the file-extension lock; it's now OK for someone else to extend
    2836             :      * the relation some more.
    2837             :      *
    2838             :      * We remove IO_IN_PROGRESS after this, as waking up waiting backends can
    2839             :      * take noticeable time.
    2840             :      */
    2841      400470 :     if (!(flags & EB_SKIP_EXTENSION_LOCK))
    2842      298670 :         UnlockRelationForExtension(bmr.rel, ExclusiveLock);
    2843             : 
    2844      400470 :     pgstat_count_io_op_time(IOOBJECT_RELATION, io_context, IOOP_EXTEND,
    2845      400470 :                             io_start, 1, extend_by * BLCKSZ);
    2846             : 
    2847             :     /* Set BM_VALID, terminate IO, and wake up any waiters */
    2848      852602 :     for (uint32 i = 0; i < extend_by; i++)
    2849             :     {
    2850      452132 :         Buffer      buf = buffers[i];
    2851      452132 :         BufferDesc *buf_hdr = GetBufferDescriptor(buf - 1);
    2852      452132 :         bool        lock = false;
    2853             : 
    2854      452132 :         if (flags & EB_LOCK_FIRST && i == 0)
    2855      295764 :             lock = true;
    2856      156368 :         else if (flags & EB_LOCK_TARGET)
    2857             :         {
    2858             :             Assert(extend_upto != InvalidBlockNumber);
    2859       87068 :             if (first_block + i + 1 == extend_upto)
    2860       85910 :                 lock = true;
    2861             :         }
    2862             : 
    2863      452132 :         if (lock)
    2864      381674 :             LWLockAcquire(BufferDescriptorGetContentLock(buf_hdr), LW_EXCLUSIVE);
    2865             : 
    2866      452132 :         TerminateBufferIO(buf_hdr, false, BM_VALID, true, false);
    2867             :     }
    2868             : 
    2869      400470 :     pgBufferUsage.shared_blks_written += extend_by;
    2870             : 
    2871      400470 :     *extended_by = extend_by;
    2872             : 
    2873      400470 :     return first_block;
    2874             : }
    2875             : 
    2876             : /*
    2877             :  * BufferIsExclusiveLocked
    2878             :  *
    2879             :  *      Checks if buffer is exclusive-locked.
    2880             :  *
    2881             :  * Buffer must be pinned.
    2882             :  */
    2883             : bool
    2884           0 : BufferIsExclusiveLocked(Buffer buffer)
    2885             : {
    2886             :     BufferDesc *bufHdr;
    2887             : 
    2888             :     Assert(BufferIsPinned(buffer));
    2889             : 
    2890           0 :     if (BufferIsLocal(buffer))
    2891             :     {
    2892             :         /* Content locks are not maintained for local buffers. */
    2893           0 :         return true;
    2894             :     }
    2895             :     else
    2896             :     {
    2897           0 :         bufHdr = GetBufferDescriptor(buffer - 1);
    2898           0 :         return LWLockHeldByMeInMode(BufferDescriptorGetContentLock(bufHdr),
    2899             :                                     LW_EXCLUSIVE);
    2900             :     }
    2901             : }
    2902             : 
    2903             : /*
    2904             :  * BufferIsDirty
    2905             :  *
    2906             :  *      Checks if buffer is already dirty.
    2907             :  *
    2908             :  * Buffer must be pinned and exclusive-locked.  (Without an exclusive lock,
    2909             :  * the result may be stale before it's returned.)
    2910             :  */
    2911             : bool
    2912           0 : BufferIsDirty(Buffer buffer)
    2913             : {
    2914             :     BufferDesc *bufHdr;
    2915             : 
    2916             :     Assert(BufferIsPinned(buffer));
    2917             : 
    2918           0 :     if (BufferIsLocal(buffer))
    2919             :     {
    2920           0 :         int         bufid = -buffer - 1;
    2921             : 
    2922           0 :         bufHdr = GetLocalBufferDescriptor(bufid);
    2923             :         /* Content locks are not maintained for local buffers. */
    2924             :     }
    2925             :     else
    2926             :     {
    2927           0 :         bufHdr = GetBufferDescriptor(buffer - 1);
    2928             :         Assert(LWLockHeldByMeInMode(BufferDescriptorGetContentLock(bufHdr),
    2929             :                                     LW_EXCLUSIVE));
    2930             :     }
    2931             : 
    2932           0 :     return pg_atomic_read_u32(&bufHdr->state) & BM_DIRTY;
    2933             : }
    2934             : 
    2935             : /*
    2936             :  * MarkBufferDirty
    2937             :  *
    2938             :  *      Marks buffer contents as dirty (actual write happens later).
    2939             :  *
    2940             :  * Buffer must be pinned and exclusive-locked.  (If caller does not hold
    2941             :  * exclusive lock, then somebody could be in process of writing the buffer,
    2942             :  * leading to risk of bad data written to disk.)
    2943             :  */
    2944             : void
    2945    43079152 : MarkBufferDirty(Buffer buffer)
    2946             : {
    2947             :     BufferDesc *bufHdr;
    2948             :     uint32      buf_state;
    2949             :     uint32      old_buf_state;
    2950             : 
    2951    43079152 :     if (!BufferIsValid(buffer))
    2952           0 :         elog(ERROR, "bad buffer ID: %d", buffer);
    2953             : 
    2954    43079152 :     if (BufferIsLocal(buffer))
    2955             :     {
    2956     2436568 :         MarkLocalBufferDirty(buffer);
    2957     2436568 :         return;
    2958             :     }
    2959             : 
    2960    40642584 :     bufHdr = GetBufferDescriptor(buffer - 1);
    2961             : 
    2962             :     Assert(BufferIsPinned(buffer));
    2963             :     Assert(LWLockHeldByMeInMode(BufferDescriptorGetContentLock(bufHdr),
    2964             :                                 LW_EXCLUSIVE));
    2965             : 
    2966    40642584 :     old_buf_state = pg_atomic_read_u32(&bufHdr->state);
    2967             :     for (;;)
    2968             :     {
    2969    40642774 :         if (old_buf_state & BM_LOCKED)
    2970          52 :             old_buf_state = WaitBufHdrUnlocked(bufHdr);
    2971             : 
    2972    40642774 :         buf_state = old_buf_state;
    2973             : 
    2974             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    2975    40642774 :         buf_state |= BM_DIRTY | BM_JUST_DIRTIED;
    2976             : 
    2977    40642774 :         if (pg_atomic_compare_exchange_u32(&bufHdr->state, &old_buf_state,
    2978             :                                            buf_state))
    2979    40642584 :             break;
    2980             :     }
    2981             : 
    2982             :     /*
    2983             :      * If the buffer was not dirty already, do vacuum accounting.
    2984             :      */
    2985    40642584 :     if (!(old_buf_state & BM_DIRTY))
    2986             :     {
    2987     1252844 :         pgBufferUsage.shared_blks_dirtied++;
    2988     1252844 :         if (VacuumCostActive)
    2989       12090 :             VacuumCostBalance += VacuumCostPageDirty;
    2990             :     }
    2991             : }
    2992             : 
    2993             : /*
    2994             :  * ReleaseAndReadBuffer -- combine ReleaseBuffer() and ReadBuffer()
    2995             :  *
    2996             :  * Formerly, this saved one cycle of acquiring/releasing the BufMgrLock
    2997             :  * compared to calling the two routines separately.  Now it's mainly just
    2998             :  * a convenience function.  However, if the passed buffer is valid and
    2999             :  * already contains the desired block, we just return it as-is; and that
    3000             :  * does save considerable work compared to a full release and reacquire.
    3001             :  *
    3002             :  * Note: it is OK to pass buffer == InvalidBuffer, indicating that no old
    3003             :  * buffer actually needs to be released.  This case is the same as ReadBuffer,
    3004             :  * but can save some tests in the caller.
    3005             :  */
    3006             : Buffer
    3007    54441966 : ReleaseAndReadBuffer(Buffer buffer,
    3008             :                      Relation relation,
    3009             :                      BlockNumber blockNum)
    3010             : {
    3011    54441966 :     ForkNumber  forkNum = MAIN_FORKNUM;
    3012             :     BufferDesc *bufHdr;
    3013             : 
    3014    54441966 :     if (BufferIsValid(buffer))
    3015             :     {
    3016             :         Assert(BufferIsPinned(buffer));
    3017    32618296 :         if (BufferIsLocal(buffer))
    3018             :         {
    3019       73716 :             bufHdr = GetLocalBufferDescriptor(-buffer - 1);
    3020       80760 :             if (bufHdr->tag.blockNum == blockNum &&
    3021       14088 :                 BufTagMatchesRelFileLocator(&bufHdr->tag, &relation->rd_locator) &&
    3022        7044 :                 BufTagGetForkNum(&bufHdr->tag) == forkNum)
    3023        7044 :                 return buffer;
    3024       66672 :             UnpinLocalBuffer(buffer);
    3025             :         }
    3026             :         else
    3027             :         {
    3028    32544580 :             bufHdr = GetBufferDescriptor(buffer - 1);
    3029             :             /* we have pin, so it's ok to examine tag without spinlock */
    3030    43845860 :             if (bufHdr->tag.blockNum == blockNum &&
    3031    22602560 :                 BufTagMatchesRelFileLocator(&bufHdr->tag, &relation->rd_locator) &&
    3032    11301280 :                 BufTagGetForkNum(&bufHdr->tag) == forkNum)
    3033    11301280 :                 return buffer;
    3034    21243300 :             UnpinBuffer(bufHdr);
    3035             :         }
    3036             :     }
    3037             : 
    3038    43133642 :     return ReadBuffer(relation, blockNum);
    3039             : }
    3040             : 
    3041             : /*
    3042             :  * PinBuffer -- make buffer unavailable for replacement.
    3043             :  *
    3044             :  * For the default access strategy, the buffer's usage_count is incremented
    3045             :  * when we first pin it; for other strategies we just make sure the usage_count
    3046             :  * isn't zero.  (The idea of the latter is that we don't want synchronized
    3047             :  * heap scans to inflate the count, but we need it to not be zero to discourage
    3048             :  * other backends from stealing buffers from our ring.  As long as we cycle
    3049             :  * through the ring faster than the global clock-sweep cycles, buffers in
    3050             :  * our ring won't be chosen as victims for replacement by other backends.)
    3051             :  *
    3052             :  * This should be applied only to shared buffers, never local ones.
    3053             :  *
    3054             :  * Since buffers are pinned/unpinned very frequently, pin buffers without
    3055             :  * taking the buffer header lock; instead update the state variable in loop of
    3056             :  * CAS operations. Hopefully it's just a single CAS.
    3057             :  *
    3058             :  * Note that ResourceOwnerEnlarge() and ReservePrivateRefCountEntry()
    3059             :  * must have been done already.
    3060             :  *
    3061             :  * Returns true if buffer is BM_VALID, else false.  This provision allows
    3062             :  * some callers to avoid an extra spinlock cycle.
    3063             :  */
    3064             : static bool
    3065   112854806 : PinBuffer(BufferDesc *buf, BufferAccessStrategy strategy)
    3066             : {
    3067   112854806 :     Buffer      b = BufferDescriptorGetBuffer(buf);
    3068             :     bool        result;
    3069             :     PrivateRefCountEntry *ref;
    3070             : 
    3071             :     Assert(!BufferIsLocal(b));
    3072             :     Assert(ReservedRefCountEntry != NULL);
    3073             : 
    3074   112854806 :     ref = GetPrivateRefCountEntry(b, true);
    3075             : 
    3076   112854806 :     if (ref == NULL)
    3077             :     {
    3078             :         uint32      buf_state;
    3079             :         uint32      old_buf_state;
    3080             : 
    3081   108542936 :         ref = NewPrivateRefCountEntry(b);
    3082             : 
    3083   108542936 :         old_buf_state = pg_atomic_read_u32(&buf->state);
    3084             :         for (;;)
    3085             :         {
    3086   108569718 :             if (old_buf_state & BM_LOCKED)
    3087        1964 :                 old_buf_state = WaitBufHdrUnlocked(buf);
    3088             : 
    3089   108569718 :             buf_state = old_buf_state;
    3090             : 
    3091             :             /* increase refcount */
    3092   108569718 :             buf_state += BUF_REFCOUNT_ONE;
    3093             : 
    3094   108569718 :             if (strategy == NULL)
    3095             :             {
    3096             :                 /* Default case: increase usagecount unless already max. */
    3097   107263424 :                 if (BUF_STATE_GET_USAGECOUNT(buf_state) < BM_MAX_USAGE_COUNT)
    3098     6137744 :                     buf_state += BUF_USAGECOUNT_ONE;
    3099             :             }
    3100             :             else
    3101             :             {
    3102             :                 /*
    3103             :                  * Ring buffers shouldn't evict others from pool.  Thus we
    3104             :                  * don't make usagecount more than 1.
    3105             :                  */
    3106     1306294 :                 if (BUF_STATE_GET_USAGECOUNT(buf_state) == 0)
    3107       68830 :                     buf_state += BUF_USAGECOUNT_ONE;
    3108             :             }
    3109             : 
    3110   108569718 :             if (pg_atomic_compare_exchange_u32(&buf->state, &old_buf_state,
    3111             :                                                buf_state))
    3112             :             {
    3113   108542936 :                 result = (buf_state & BM_VALID) != 0;
    3114             : 
    3115             :                 /*
    3116             :                  * Assume that we acquired a buffer pin for the purposes of
    3117             :                  * Valgrind buffer client checks (even in !result case) to
    3118             :                  * keep things simple.  Buffers that are unsafe to access are
    3119             :                  * not generally guaranteed to be marked undefined or
    3120             :                  * non-accessible in any case.
    3121             :                  */
    3122             :                 VALGRIND_MAKE_MEM_DEFINED(BufHdrGetBlock(buf), BLCKSZ);
    3123   108542936 :                 break;
    3124             :             }
    3125             :         }
    3126             :     }
    3127             :     else
    3128             :     {
    3129             :         /*
    3130             :          * If we previously pinned the buffer, it is likely to be valid, but
    3131             :          * it may not be if StartReadBuffers() was called and
    3132             :          * WaitReadBuffers() hasn't been called yet.  We'll check by loading
    3133             :          * the flags without locking.  This is racy, but it's OK to return
    3134             :          * false spuriously: when WaitReadBuffers() calls StartBufferIO(),
    3135             :          * it'll see that it's now valid.
    3136             :          *
    3137             :          * Note: We deliberately avoid a Valgrind client request here.
    3138             :          * Individual access methods can optionally superimpose buffer page
    3139             :          * client requests on top of our client requests to enforce that
    3140             :          * buffers are only accessed while locked (and pinned).  It's possible
    3141             :          * that the buffer page is legitimately non-accessible here.  We
    3142             :          * cannot meddle with that.
    3143             :          */
    3144     4311870 :         result = (pg_atomic_read_u32(&buf->state) & BM_VALID) != 0;
    3145             :     }
    3146             : 
    3147   112854806 :     ref->refcount++;
    3148             :     Assert(ref->refcount > 0);
    3149   112854806 :     ResourceOwnerRememberBuffer(CurrentResourceOwner, b);
    3150   112854806 :     return result;
    3151             : }
    3152             : 
    3153             : /*
    3154             :  * PinBuffer_Locked -- as above, but caller already locked the buffer header.
    3155             :  * The spinlock is released before return.
    3156             :  *
    3157             :  * As this function is called with the spinlock held, the caller has to
    3158             :  * previously call ReservePrivateRefCountEntry() and
    3159             :  * ResourceOwnerEnlarge(CurrentResourceOwner);
    3160             :  *
    3161             :  * Currently, no callers of this function want to modify the buffer's
    3162             :  * usage_count at all, so there's no need for a strategy parameter.
    3163             :  * Also we don't bother with a BM_VALID test (the caller could check that for
    3164             :  * itself).
    3165             :  *
    3166             :  * Also all callers only ever use this function when it's known that the
    3167             :  * buffer can't have a preexisting pin by this backend. That allows us to skip
    3168             :  * searching the private refcount array & hash, which is a boon, because the
    3169             :  * spinlock is still held.
    3170             :  *
    3171             :  * Note: use of this routine is frequently mandatory, not just an optimization
    3172             :  * to save a spin lock/unlock cycle, because we need to pin a buffer before
    3173             :  * its state can change under us.
    3174             :  */
    3175             : static void
    3176     4282700 : PinBuffer_Locked(BufferDesc *buf)
    3177             : {
    3178             :     Buffer      b;
    3179             :     PrivateRefCountEntry *ref;
    3180             :     uint32      buf_state;
    3181             : 
    3182             :     /*
    3183             :      * As explained, We don't expect any preexisting pins. That allows us to
    3184             :      * manipulate the PrivateRefCount after releasing the spinlock
    3185             :      */
    3186             :     Assert(GetPrivateRefCountEntry(BufferDescriptorGetBuffer(buf), false) == NULL);
    3187             : 
    3188             :     /*
    3189             :      * Buffer can't have a preexisting pin, so mark its page as defined to
    3190             :      * Valgrind (this is similar to the PinBuffer() case where the backend
    3191             :      * doesn't already have a buffer pin)
    3192             :      */
    3193             :     VALGRIND_MAKE_MEM_DEFINED(BufHdrGetBlock(buf), BLCKSZ);
    3194             : 
    3195             :     /*
    3196             :      * Since we hold the buffer spinlock, we can update the buffer state and
    3197             :      * release the lock in one operation.
    3198             :      */
    3199     4282700 :     buf_state = pg_atomic_read_u32(&buf->state);
    3200             :     Assert(buf_state & BM_LOCKED);
    3201     4282700 :     buf_state += BUF_REFCOUNT_ONE;
    3202     4282700 :     UnlockBufHdr(buf, buf_state);
    3203             : 
    3204     4282700 :     b = BufferDescriptorGetBuffer(buf);
    3205             : 
    3206     4282700 :     ref = NewPrivateRefCountEntry(b);
    3207     4282700 :     ref->refcount++;
    3208             : 
    3209     4282700 :     ResourceOwnerRememberBuffer(CurrentResourceOwner, b);
    3210     4282700 : }
    3211             : 
    3212             : /*
    3213             :  * Support for waking up another backend that is waiting for the cleanup lock
    3214             :  * to be released using BM_PIN_COUNT_WAITER.
    3215             :  *
    3216             :  * See LockBufferForCleanup().
    3217             :  *
    3218             :  * Expected to be called just after releasing a buffer pin (in a BufferDesc,
    3219             :  * not just reducing the backend-local pincount for the buffer).
    3220             :  */
    3221             : static void
    3222           4 : WakePinCountWaiter(BufferDesc *buf)
    3223             : {
    3224             :     /*
    3225             :      * Acquire the buffer header lock, re-check that there's a waiter. Another
    3226             :      * backend could have unpinned this buffer, and already woken up the
    3227             :      * waiter.
    3228             :      *
    3229             :      * There's no danger of the buffer being replaced after we unpinned it
    3230             :      * above, as it's pinned by the waiter. The waiter removes
    3231             :      * BM_PIN_COUNT_WAITER if it stops waiting for a reason other than this
    3232             :      * backend waking it up.
    3233             :      */
    3234           4 :     uint32      buf_state = LockBufHdr(buf);
    3235             : 
    3236           4 :     if ((buf_state & BM_PIN_COUNT_WAITER) &&
    3237           4 :         BUF_STATE_GET_REFCOUNT(buf_state) == 1)
    3238           4 :     {
    3239             :         /* we just released the last pin other than the waiter's */
    3240           4 :         int         wait_backend_pgprocno = buf->wait_backend_pgprocno;
    3241             : 
    3242           4 :         buf_state &= ~BM_PIN_COUNT_WAITER;
    3243           4 :         UnlockBufHdr(buf, buf_state);
    3244           4 :         ProcSendSignal(wait_backend_pgprocno);
    3245             :     }
    3246             :     else
    3247           0 :         UnlockBufHdr(buf, buf_state);
    3248           4 : }
    3249             : 
    3250             : /*
    3251             :  * UnpinBuffer -- make buffer available for replacement.
    3252             :  *
    3253             :  * This should be applied only to shared buffers, never local ones.  This
    3254             :  * always adjusts CurrentResourceOwner.
    3255             :  */
    3256             : static void
    3257   138314340 : UnpinBuffer(BufferDesc *buf)
    3258             : {
    3259   138314340 :     Buffer      b = BufferDescriptorGetBuffer(buf);
    3260             : 
    3261   138314340 :     ResourceOwnerForgetBuffer(CurrentResourceOwner, b);
    3262   138314340 :     UnpinBufferNoOwner(buf);
    3263   138314340 : }
    3264             : 
    3265             : static void
    3266   138322874 : UnpinBufferNoOwner(BufferDesc *buf)
    3267             : {
    3268             :     PrivateRefCountEntry *ref;
    3269   138322874 :     Buffer      b = BufferDescriptorGetBuffer(buf);
    3270             : 
    3271             :     Assert(!BufferIsLocal(b));
    3272             : 
    3273             :     /* not moving as we're likely deleting it soon anyway */
    3274   138322874 :     ref = GetPrivateRefCountEntry(b, false);
    3275             :     Assert(ref != NULL);
    3276             :     Assert(ref->refcount > 0);
    3277   138322874 :     ref->refcount--;
    3278   138322874 :     if (ref->refcount == 0)
    3279             :     {
    3280             :         uint32      buf_state;
    3281             :         uint32      old_buf_state;
    3282             : 
    3283             :         /*
    3284             :          * Mark buffer non-accessible to Valgrind.
    3285             :          *
    3286             :          * Note that the buffer may have already been marked non-accessible
    3287             :          * within access method code that enforces that buffers are only
    3288             :          * accessed while a buffer lock is held.
    3289             :          */
    3290             :         VALGRIND_MAKE_MEM_NOACCESS(BufHdrGetBlock(buf), BLCKSZ);
    3291             : 
    3292             :         /* I'd better not still hold the buffer content lock */
    3293             :         Assert(!LWLockHeldByMe(BufferDescriptorGetContentLock(buf)));
    3294             : 
    3295             :         /*
    3296             :          * Decrement the shared reference count.
    3297             :          *
    3298             :          * Since buffer spinlock holder can update status using just write,
    3299             :          * it's not safe to use atomic decrement here; thus use a CAS loop.
    3300             :          */
    3301   112825636 :         old_buf_state = pg_atomic_read_u32(&buf->state);
    3302             :         for (;;)
    3303             :         {
    3304   112850900 :             if (old_buf_state & BM_LOCKED)
    3305        1800 :                 old_buf_state = WaitBufHdrUnlocked(buf);
    3306             : 
    3307   112850900 :             buf_state = old_buf_state;
    3308             : 
    3309   112850900 :             buf_state -= BUF_REFCOUNT_ONE;
    3310             : 
    3311   112850900 :             if (pg_atomic_compare_exchange_u32(&buf->state, &old_buf_state,
    3312             :                                                buf_state))
    3313   112825636 :                 break;
    3314             :         }
    3315             : 
    3316             :         /* Support LockBufferForCleanup() */
    3317   112825636 :         if (buf_state & BM_PIN_COUNT_WAITER)
    3318           4 :             WakePinCountWaiter(buf);
    3319             : 
    3320   112825636 :         ForgetPrivateRefCountEntry(ref);
    3321             :     }
    3322   138322874 : }
    3323             : 
    3324             : #define ST_SORT sort_checkpoint_bufferids
    3325             : #define ST_ELEMENT_TYPE CkptSortItem
    3326             : #define ST_COMPARE(a, b) ckpt_buforder_comparator(a, b)
    3327             : #define ST_SCOPE static
    3328             : #define ST_DEFINE
    3329             : #include <lib/sort_template.h>
    3330             : 
    3331             : /*
    3332             :  * BufferSync -- Write out all dirty buffers in the pool.
    3333             :  *
    3334             :  * This is called at checkpoint time to write out all dirty shared buffers.
    3335             :  * The checkpoint request flags should be passed in.  If CHECKPOINT_IMMEDIATE
    3336             :  * is set, we disable delays between writes; if CHECKPOINT_IS_SHUTDOWN,
    3337             :  * CHECKPOINT_END_OF_RECOVERY or CHECKPOINT_FLUSH_ALL is set, we write even
    3338             :  * unlogged buffers, which are otherwise skipped.  The remaining flags
    3339             :  * currently have no effect here.
    3340             :  */
    3341             : static void
    3342        2642 : BufferSync(int flags)
    3343             : {
    3344             :     uint32      buf_state;
    3345             :     int         buf_id;
    3346             :     int         num_to_scan;
    3347             :     int         num_spaces;
    3348             :     int         num_processed;
    3349             :     int         num_written;
    3350        2642 :     CkptTsStatus *per_ts_stat = NULL;
    3351             :     Oid         last_tsid;
    3352             :     binaryheap *ts_heap;
    3353             :     int         i;
    3354        2642 :     int         mask = BM_DIRTY;
    3355             :     WritebackContext wb_context;
    3356             : 
    3357             :     /*
    3358             :      * Unless this is a shutdown checkpoint or we have been explicitly told,
    3359             :      * we write only permanent, dirty buffers.  But at shutdown or end of
    3360             :      * recovery, we write all dirty buffers.
    3361             :      */
    3362        2642 :     if (!((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY |
    3363             :                     CHECKPOINT_FLUSH_ALL))))
    3364        1260 :         mask |= BM_PERMANENT;
    3365             : 
    3366             :     /*
    3367             :      * Loop over all buffers, and mark the ones that need to be written with
    3368             :      * BM_CHECKPOINT_NEEDED.  Count them as we go (num_to_scan), so that we
    3369             :      * can estimate how much work needs to be done.
    3370             :      *
    3371             :      * This allows us to write only those pages that were dirty when the
    3372             :      * checkpoint began, and not those that get dirtied while it proceeds.
    3373             :      * Whenever a page with BM_CHECKPOINT_NEEDED is written out, either by us
    3374             :      * later in this function, or by normal backends or the bgwriter cleaning
    3375             :      * scan, the flag is cleared.  Any buffer dirtied after this point won't
    3376             :      * have the flag set.
    3377             :      *
    3378             :      * Note that if we fail to write some buffer, we may leave buffers with
    3379             :      * BM_CHECKPOINT_NEEDED still set.  This is OK since any such buffer would
    3380             :      * certainly need to be written for the next checkpoint attempt, too.
    3381             :      */
    3382        2642 :     num_to_scan = 0;
    3383    23001074 :     for (buf_id = 0; buf_id < NBuffers; buf_id++)
    3384             :     {
    3385    22998432 :         BufferDesc *bufHdr = GetBufferDescriptor(buf_id);
    3386             : 
    3387             :         /*
    3388             :          * Header spinlock is enough to examine BM_DIRTY, see comment in
    3389             :          * SyncOneBuffer.
    3390             :          */
    3391    22998432 :         buf_state = LockBufHdr(bufHdr);
    3392             : 
    3393    22998432 :         if ((buf_state & mask) == mask)
    3394             :         {
    3395             :             CkptSortItem *item;
    3396             : 
    3397      553674 :             buf_state |= BM_CHECKPOINT_NEEDED;
    3398             : 
    3399      553674 :             item = &CkptBufferIds[num_to_scan++];
    3400      553674 :             item->buf_id = buf_id;
    3401      553674 :             item->tsId = bufHdr->tag.spcOid;
    3402      553674 :             item->relNumber = BufTagGetRelNumber(&bufHdr->tag);
    3403      553674 :             item->forkNum = BufTagGetForkNum(&bufHdr->tag);
    3404      553674 :             item->blockNum = bufHdr->tag.blockNum;
    3405             :         }
    3406             : 
    3407    22998432 :         UnlockBufHdr(bufHdr, buf_state);
    3408             : 
    3409             :         /* Check for barrier events in case NBuffers is large. */
    3410    22998432 :         if (ProcSignalBarrierPending)
    3411           0 :             ProcessProcSignalBarrier();
    3412             :     }
    3413             : 
    3414        2642 :     if (num_to_scan == 0)
    3415         610 :         return;                 /* nothing to do */
    3416             : 
    3417        2032 :     WritebackContextInit(&wb_context, &checkpoint_flush_after);
    3418             : 
    3419             :     TRACE_POSTGRESQL_BUFFER_SYNC_START(NBuffers, num_to_scan);
    3420             : 
    3421             :     /*
    3422             :      * Sort buffers that need to be written to reduce the likelihood of random
    3423             :      * IO. The sorting is also important for the implementation of balancing
    3424             :      * writes between tablespaces. Without balancing writes we'd potentially
    3425             :      * end up writing to the tablespaces one-by-one; possibly overloading the
    3426             :      * underlying system.
    3427             :      */
    3428        2032 :     sort_checkpoint_bufferids(CkptBufferIds, num_to_scan);
    3429             : 
    3430        2032 :     num_spaces = 0;
    3431             : 
    3432             :     /*
    3433             :      * Allocate progress status for each tablespace with buffers that need to
    3434             :      * be flushed. This requires the to-be-flushed array to be sorted.
    3435             :      */
    3436        2032 :     last_tsid = InvalidOid;
    3437      555706 :     for (i = 0; i < num_to_scan; i++)
    3438             :     {
    3439             :         CkptTsStatus *s;
    3440             :         Oid         cur_tsid;
    3441             : 
    3442      553674 :         cur_tsid = CkptBufferIds[i].tsId;
    3443             : 
    3444             :         /*
    3445             :          * Grow array of per-tablespace status structs, every time a new
    3446             :          * tablespace is found.
    3447             :          */
    3448      553674 :         if (last_tsid == InvalidOid || last_tsid != cur_tsid)
    3449        3012 :         {
    3450             :             Size        sz;
    3451             : 
    3452        3012 :             num_spaces++;
    3453             : 
    3454             :             /*
    3455             :              * Not worth adding grow-by-power-of-2 logic here - even with a
    3456             :              * few hundred tablespaces this should be fine.
    3457             :              */
    3458        3012 :             sz = sizeof(CkptTsStatus) * num_spaces;
    3459             : 
    3460        3012 :             if (per_ts_stat == NULL)
    3461        2032 :                 per_ts_stat = (CkptTsStatus *) palloc(sz);
    3462             :             else
    3463         980 :                 per_ts_stat = (CkptTsStatus *) repalloc(per_ts_stat, sz);
    3464             : 
    3465        3012 :             s = &per_ts_stat[num_spaces - 1];
    3466        3012 :             memset(s, 0, sizeof(*s));
    3467        3012 :             s->tsId = cur_tsid;
    3468             : 
    3469             :             /*
    3470             :              * The first buffer in this tablespace. As CkptBufferIds is sorted
    3471             :              * by tablespace all (s->num_to_scan) buffers in this tablespace
    3472             :              * will follow afterwards.
    3473             :              */
    3474        3012 :             s->index = i;
    3475             : 
    3476             :             /*
    3477             :              * progress_slice will be determined once we know how many buffers
    3478             :              * are in each tablespace, i.e. after this loop.
    3479             :              */
    3480             : 
    3481        3012 :             last_tsid = cur_tsid;
    3482             :         }
    3483             :         else
    3484             :         {
    3485      550662 :             s = &per_ts_stat[num_spaces - 1];
    3486             :         }
    3487             : 
    3488      553674 :         s->num_to_scan++;
    3489             : 
    3490             :         /* Check for barrier events. */
    3491      553674 :         if (ProcSignalBarrierPending)
    3492           0 :             ProcessProcSignalBarrier();
    3493             :     }
    3494             : 
    3495             :     Assert(num_spaces > 0);
    3496             : 
    3497             :     /*
    3498             :      * Build a min-heap over the write-progress in the individual tablespaces,
    3499             :      * and compute how large a portion of the total progress a single
    3500             :      * processed buffer is.
    3501             :      */
    3502        2032 :     ts_heap = binaryheap_allocate(num_spaces,
    3503             :                                   ts_ckpt_progress_comparator,
    3504             :                                   NULL);
    3505             : 
    3506        5044 :     for (i = 0; i < num_spaces; i++)
    3507             :     {
    3508        3012 :         CkptTsStatus *ts_stat = &per_ts_stat[i];
    3509             : 
    3510        3012 :         ts_stat->progress_slice = (float8) num_to_scan / ts_stat->num_to_scan;
    3511             : 
    3512        3012 :         binaryheap_add_unordered(ts_heap, PointerGetDatum(ts_stat));
    3513             :     }
    3514             : 
    3515        2032 :     binaryheap_build(ts_heap);
    3516             : 
    3517             :     /*
    3518             :      * Iterate through to-be-checkpointed buffers and write the ones (still)
    3519             :      * marked with BM_CHECKPOINT_NEEDED. The writes are balanced between
    3520             :      * tablespaces; otherwise the sorting would lead to only one tablespace
    3521             :      * receiving writes at a time, making inefficient use of the hardware.
    3522             :      */
    3523        2032 :     num_processed = 0;
    3524        2032 :     num_written = 0;
    3525      555706 :     while (!binaryheap_empty(ts_heap))
    3526             :     {
    3527      553674 :         BufferDesc *bufHdr = NULL;
    3528             :         CkptTsStatus *ts_stat = (CkptTsStatus *)
    3529      553674 :             DatumGetPointer(binaryheap_first(ts_heap));
    3530             : 
    3531      553674 :         buf_id = CkptBufferIds[ts_stat->index].buf_id;
    3532             :         Assert(buf_id != -1);
    3533             : 
    3534      553674 :         bufHdr = GetBufferDescriptor(buf_id);
    3535             : 
    3536      553674 :         num_processed++;
    3537             : 
    3538             :         /*
    3539             :          * We don't need to acquire the lock here, because we're only looking
    3540             :          * at a single bit. It's possible that someone else writes the buffer
    3541             :          * and clears the flag right after we check, but that doesn't matter
    3542             :          * since SyncOneBuffer will then do nothing.  However, there is a
    3543             :          * further race condition: it's conceivable that between the time we
    3544             :          * examine the bit here and the time SyncOneBuffer acquires the lock,
    3545             :          * someone else not only wrote the buffer but replaced it with another
    3546             :          * page and dirtied it.  In that improbable case, SyncOneBuffer will
    3547             :          * write the buffer though we didn't need to.  It doesn't seem worth
    3548             :          * guarding against this, though.
    3549             :          */
    3550      553674 :         if (pg_atomic_read_u32(&bufHdr->state) & BM_CHECKPOINT_NEEDED)
    3551             :         {
    3552      515004 :             if (SyncOneBuffer(buf_id, false, &wb_context) & BUF_WRITTEN)
    3553             :             {
    3554             :                 TRACE_POSTGRESQL_BUFFER_SYNC_WRITTEN(buf_id);
    3555      515004 :                 PendingCheckpointerStats.buffers_written++;
    3556      515004 :                 num_written++;
    3557             :             }
    3558             :         }
    3559             : 
    3560             :         /*
    3561             :          * Measure progress independent of actually having to flush the buffer
    3562             :          * - otherwise writing become unbalanced.
    3563             :          */
    3564      553674 :         ts_stat->progress += ts_stat->progress_slice;
    3565      553674 :         ts_stat->num_scanned++;
    3566      553674 :         ts_stat->index++;
    3567             : 
    3568             :         /* Have all the buffers from the tablespace been processed? */
    3569      553674 :         if (ts_stat->num_scanned == ts_stat->num_to_scan)
    3570             :         {
    3571        3012 :             binaryheap_remove_first(ts_heap);
    3572             :         }
    3573             :         else
    3574             :         {
    3575             :             /* update heap with the new progress */
    3576      550662 :             binaryheap_replace_first(ts_heap, PointerGetDatum(ts_stat));
    3577             :         }
    3578             : 
    3579             :         /*
    3580             :          * Sleep to throttle our I/O rate.
    3581             :          *
    3582             :          * (This will check for barrier events even if it doesn't sleep.)
    3583             :          */
    3584      553674 :         CheckpointWriteDelay(flags, (double) num_processed / num_to_scan);
    3585             :     }
    3586             : 
    3587             :     /*
    3588             :      * Issue all pending flushes. Only checkpointer calls BufferSync(), so
    3589             :      * IOContext will always be IOCONTEXT_NORMAL.
    3590             :      */
    3591        2032 :     IssuePendingWritebacks(&wb_context, IOCONTEXT_NORMAL);
    3592             : 
    3593        2032 :     pfree(per_ts_stat);
    3594        2032 :     per_ts_stat = NULL;
    3595        2032 :     binaryheap_free(ts_heap);
    3596             : 
    3597             :     /*
    3598             :      * Update checkpoint statistics. As noted above, this doesn't include
    3599             :      * buffers written by other backends or bgwriter scan.
    3600             :      */
    3601        2032 :     CheckpointStats.ckpt_bufs_written += num_written;
    3602             : 
    3603             :     TRACE_POSTGRESQL_BUFFER_SYNC_DONE(NBuffers, num_written, num_to_scan);
    3604             : }
    3605             : 
    3606             : /*
    3607             :  * BgBufferSync -- Write out some dirty buffers in the pool.
    3608             :  *
    3609             :  * This is called periodically by the background writer process.
    3610             :  *
    3611             :  * Returns true if it's appropriate for the bgwriter process to go into
    3612             :  * low-power hibernation mode.  (This happens if the strategy clock sweep
    3613             :  * has been "lapped" and no buffer allocations have occurred recently,
    3614             :  * or if the bgwriter has been effectively disabled by setting
    3615             :  * bgwriter_lru_maxpages to 0.)
    3616             :  */
    3617             : bool
    3618       20058 : BgBufferSync(WritebackContext *wb_context)
    3619             : {
    3620             :     /* info obtained from freelist.c */
    3621             :     int         strategy_buf_id;
    3622             :     uint32      strategy_passes;
    3623             :     uint32      recent_alloc;
    3624             : 
    3625             :     /*
    3626             :      * Information saved between calls so we can determine the strategy
    3627             :      * point's advance rate and avoid scanning already-cleaned buffers.
    3628             :      */
    3629             :     static bool saved_info_valid = false;
    3630             :     static int  prev_strategy_buf_id;
    3631             :     static uint32 prev_strategy_passes;
    3632             :     static int  next_to_clean;
    3633             :     static uint32 next_passes;
    3634             : 
    3635             :     /* Moving averages of allocation rate and clean-buffer density */
    3636             :     static float smoothed_alloc = 0;
    3637             :     static float smoothed_density = 10.0;
    3638             : 
    3639             :     /* Potentially these could be tunables, but for now, not */
    3640       20058 :     float       smoothing_samples = 16;
    3641       20058 :     float       scan_whole_pool_milliseconds = 120000.0;
    3642             : 
    3643             :     /* Used to compute how far we scan ahead */
    3644             :     long        strategy_delta;
    3645             :     int         bufs_to_lap;
    3646             :     int         bufs_ahead;
    3647             :     float       scans_per_alloc;
    3648             :     int         reusable_buffers_est;
    3649             :     int         upcoming_alloc_est;
    3650             :     int         min_scan_buffers;
    3651             : 
    3652             :     /* Variables for the scanning loop proper */
    3653             :     int         num_to_scan;
    3654             :     int         num_written;
    3655             :     int         reusable_buffers;
    3656             : 
    3657             :     /* Variables for final smoothed_density update */
    3658             :     long        new_strategy_delta;
    3659             :     uint32      new_recent_alloc;
    3660             : 
    3661             :     /*
    3662             :      * Find out where the freelist clock sweep currently is, and how many
    3663             :      * buffer allocations have happened since our last call.
    3664             :      */
    3665       20058 :     strategy_buf_id = StrategySyncStart(&strategy_passes, &recent_alloc);
    3666             : 
    3667             :     /* Report buffer alloc counts to pgstat */
    3668       20058 :     PendingBgWriterStats.buf_alloc += recent_alloc;
    3669             : 
    3670             :     /*
    3671             :      * If we're not running the LRU scan, just stop after doing the stats
    3672             :      * stuff.  We mark the saved state invalid so that we can recover sanely
    3673             :      * if LRU scan is turned back on later.
    3674             :      */
    3675       20058 :     if (bgwriter_lru_maxpages <= 0)
    3676             :     {
    3677          38 :         saved_info_valid = false;
    3678          38 :         return true;
    3679             :     }
    3680             : 
    3681             :     /*
    3682             :      * Compute strategy_delta = how many buffers have been scanned by the
    3683             :      * clock sweep since last time.  If first time through, assume none. Then
    3684             :      * see if we are still ahead of the clock sweep, and if so, how many
    3685             :      * buffers we could scan before we'd catch up with it and "lap" it. Note:
    3686             :      * weird-looking coding of xxx_passes comparisons are to avoid bogus
    3687             :      * behavior when the passes counts wrap around.
    3688             :      */
    3689       20020 :     if (saved_info_valid)
    3690             :     {
    3691       19038 :         int32       passes_delta = strategy_passes - prev_strategy_passes;
    3692             : 
    3693       19038 :         strategy_delta = strategy_buf_id - prev_strategy_buf_id;
    3694       19038 :         strategy_delta += (long) passes_delta * NBuffers;
    3695             : 
    3696             :         Assert(strategy_delta >= 0);
    3697             : 
    3698       19038 :         if ((int32) (next_passes - strategy_passes) > 0)
    3699             :         {
    3700             :             /* we're one pass ahead of the strategy point */
    3701        3382 :             bufs_to_lap = strategy_buf_id - next_to_clean;
    3702             : #ifdef BGW_DEBUG
    3703             :             elog(DEBUG2, "bgwriter ahead: bgw %u-%u strategy %u-%u delta=%ld lap=%d",
    3704             :                  next_passes, next_to_clean,
    3705             :                  strategy_passes, strategy_buf_id,
    3706             :                  strategy_delta, bufs_to_lap);
    3707             : #endif
    3708             :         }
    3709       15656 :         else if (next_passes == strategy_passes &&
    3710       12470 :                  next_to_clean >= strategy_buf_id)
    3711             :         {
    3712             :             /* on same pass, but ahead or at least not behind */
    3713       12160 :             bufs_to_lap = NBuffers - (next_to_clean - strategy_buf_id);
    3714             : #ifdef BGW_DEBUG
    3715             :             elog(DEBUG2, "bgwriter ahead: bgw %u-%u strategy %u-%u delta=%ld lap=%d",
    3716             :                  next_passes, next_to_clean,
    3717             :                  strategy_passes, strategy_buf_id,
    3718             :                  strategy_delta, bufs_to_lap);
    3719             : #endif
    3720             :         }
    3721             :         else
    3722             :         {
    3723             :             /*
    3724             :              * We're behind, so skip forward to the strategy point and start
    3725             :              * cleaning from there.
    3726             :              */
    3727             : #ifdef BGW_DEBUG
    3728             :             elog(DEBUG2, "bgwriter behind: bgw %u-%u strategy %u-%u delta=%ld",
    3729             :                  next_passes, next_to_clean,
    3730             :                  strategy_passes, strategy_buf_id,
    3731             :                  strategy_delta);
    3732             : #endif
    3733        3496 :             next_to_clean = strategy_buf_id;
    3734        3496 :             next_passes = strategy_passes;
    3735        3496 :             bufs_to_lap = NBuffers;
    3736             :         }
    3737             :     }
    3738             :     else
    3739             :     {
    3740             :         /*
    3741             :          * Initializing at startup or after LRU scanning had been off. Always
    3742             :          * start at the strategy point.
    3743             :          */
    3744             : #ifdef BGW_DEBUG
    3745             :         elog(DEBUG2, "bgwriter initializing: strategy %u-%u",
    3746             :              strategy_passes, strategy_buf_id);
    3747             : #endif
    3748         982 :         strategy_delta = 0;
    3749         982 :         next_to_clean = strategy_buf_id;
    3750         982 :         next_passes = strategy_passes;
    3751         982 :         bufs_to_lap = NBuffers;
    3752             :     }
    3753             : 
    3754             :     /* Update saved info for next time */
    3755       20020 :     prev_strategy_buf_id = strategy_buf_id;
    3756       20020 :     prev_strategy_passes = strategy_passes;
    3757       20020 :     saved_info_valid = true;
    3758             : 
    3759             :     /*
    3760             :      * Compute how many buffers had to be scanned for each new allocation, ie,
    3761             :      * 1/density of reusable buffers, and track a moving average of that.
    3762             :      *
    3763             :      * If the strategy point didn't move, we don't update the density estimate
    3764             :      */
    3765       20020 :     if (strategy_delta > 0 && recent_alloc > 0)
    3766             :     {
    3767        4374 :         scans_per_alloc = (float) strategy_delta / (float) recent_alloc;
    3768        4374 :         smoothed_density += (scans_per_alloc - smoothed_density) /
    3769             :             smoothing_samples;
    3770             :     }
    3771             : 
    3772             :     /*
    3773             :      * Estimate how many reusable buffers there are between the current
    3774             :      * strategy point and where we've scanned ahead to, based on the smoothed
    3775             :      * density estimate.
    3776             :      */
    3777       20020 :     bufs_ahead = NBuffers - bufs_to_lap;
    3778       20020 :     reusable_buffers_est = (float) bufs_ahead / smoothed_density;
    3779             : 
    3780             :     /*
    3781             :      * Track a moving average of recent buffer allocations.  Here, rather than
    3782             :      * a true average we want a fast-attack, slow-decline behavior: we
    3783             :      * immediately follow any increase.
    3784             :      */
    3785       20020 :     if (smoothed_alloc <= (float) recent_alloc)
    3786        5650 :         smoothed_alloc = recent_alloc;
    3787             :     else
    3788       14370 :         smoothed_alloc += ((float) recent_alloc - smoothed_alloc) /
    3789             :             smoothing_samples;
    3790             : 
    3791             :     /* Scale the estimate by a GUC to allow more aggressive tuning. */
    3792       20020 :     upcoming_alloc_est = (int) (smoothed_alloc * bgwriter_lru_multiplier);
    3793             : 
    3794             :     /*
    3795             :      * If recent_alloc remains at zero for many cycles, smoothed_alloc will
    3796             :      * eventually underflow to zero, and the underflows produce annoying
    3797             :      * kernel warnings on some platforms.  Once upcoming_alloc_est has gone to
    3798             :      * zero, there's no point in tracking smaller and smaller values of
    3799             :      * smoothed_alloc, so just reset it to exactly zero to avoid this
    3800             :      * syndrome.  It will pop back up as soon as recent_alloc increases.
    3801             :      */
    3802       20020 :     if (upcoming_alloc_est == 0)
    3803        3336 :         smoothed_alloc = 0;
    3804             : 
    3805             :     /*
    3806             :      * Even in cases where there's been little or no buffer allocation
    3807             :      * activity, we want to make a small amount of progress through the buffer
    3808             :      * cache so that as many reusable buffers as possible are clean after an
    3809             :      * idle period.
    3810             :      *
    3811             :      * (scan_whole_pool_milliseconds / BgWriterDelay) computes how many times
    3812             :      * the BGW will be called during the scan_whole_pool time; slice the
    3813             :      * buffer pool into that many sections.
    3814             :      */
    3815       20020 :     min_scan_buffers = (int) (NBuffers / (scan_whole_pool_milliseconds / BgWriterDelay));
    3816             : 
    3817       20020 :     if (upcoming_alloc_est < (min_scan_buffers + reusable_buffers_est))
    3818             :     {
    3819             : #ifdef BGW_DEBUG
    3820             :         elog(DEBUG2, "bgwriter: alloc_est=%d too small, using min=%d + reusable_est=%d",
    3821             :              upcoming_alloc_est, min_scan_buffers, reusable_buffers_est);
    3822             : #endif
    3823       10790 :         upcoming_alloc_est = min_scan_buffers + reusable_buffers_est;
    3824             :     }
    3825             : 
    3826             :     /*
    3827             :      * Now write out dirty reusable buffers, working forward from the
    3828             :      * next_to_clean point, until we have lapped the strategy scan, or cleaned
    3829             :      * enough buffers to match our estimate of the next cycle's allocation
    3830             :      * requirements, or hit the bgwriter_lru_maxpages limit.
    3831             :      */
    3832             : 
    3833       20020 :     num_to_scan = bufs_to_lap;
    3834       20020 :     num_written = 0;
    3835       20020 :     reusable_buffers = reusable_buffers_est;
    3836             : 
    3837             :     /* Execute the LRU scan */
    3838     3242632 :     while (num_to_scan > 0 && reusable_buffers < upcoming_alloc_est)
    3839             :     {
    3840     3222614 :         int         sync_state = SyncOneBuffer(next_to_clean, true,
    3841             :                                                wb_context);
    3842             : 
    3843     3222614 :         if (++next_to_clean >= NBuffers)
    3844             :         {
    3845        3940 :             next_to_clean = 0;
    3846        3940 :             next_passes++;
    3847             :         }
    3848     3222614 :         num_to_scan--;
    3849             : 
    3850     3222614 :         if (sync_state & BUF_WRITTEN)
    3851             :         {
    3852       33038 :             reusable_buffers++;
    3853       33038 :             if (++num_written >= bgwriter_lru_maxpages)
    3854             :             {
    3855           2 :                 PendingBgWriterStats.maxwritten_clean++;
    3856           2 :                 break;
    3857             :             }
    3858             :         }
    3859     3189576 :         else if (sync_state & BUF_REUSABLE)
    3860     2406096 :             reusable_buffers++;
    3861             :     }
    3862             : 
    3863       20020 :     PendingBgWriterStats.buf_written_clean += num_written;
    3864             : 
    3865             : #ifdef BGW_DEBUG
    3866             :     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",
    3867             :          recent_alloc, smoothed_alloc, strategy_delta, bufs_ahead,
    3868             :          smoothed_density, reusable_buffers_est, upcoming_alloc_est,
    3869             :          bufs_to_lap - num_to_scan,
    3870             :          num_written,
    3871             :          reusable_buffers - reusable_buffers_est);
    3872             : #endif
    3873             : 
    3874             :     /*
    3875             :      * Consider the above scan as being like a new allocation scan.
    3876             :      * Characterize its density and update the smoothed one based on it. This
    3877             :      * effectively halves the moving average period in cases where both the
    3878             :      * strategy and the background writer are doing some useful scanning,
    3879             :      * which is helpful because a long memory isn't as desirable on the
    3880             :      * density estimates.
    3881             :      */
    3882       20020 :     new_strategy_delta = bufs_to_lap - num_to_scan;
    3883       20020 :     new_recent_alloc = reusable_buffers - reusable_buffers_est;
    3884       20020 :     if (new_strategy_delta > 0 && new_recent_alloc > 0)
    3885             :     {
    3886       16446 :         scans_per_alloc = (float) new_strategy_delta / (float) new_recent_alloc;
    3887       16446 :         smoothed_density += (scans_per_alloc - smoothed_density) /
    3888             :             smoothing_samples;
    3889             : 
    3890             : #ifdef BGW_DEBUG
    3891             :         elog(DEBUG2, "bgwriter: cleaner density alloc=%u scan=%ld density=%.2f new smoothed=%.2f",
    3892             :              new_recent_alloc, new_strategy_delta,
    3893             :              scans_per_alloc, smoothed_density);
    3894             : #endif
    3895             :     }
    3896             : 
    3897             :     /* Return true if OK to hibernate */
    3898       20020 :     return (bufs_to_lap == 0 && recent_alloc == 0);
    3899             : }
    3900             : 
    3901             : /*
    3902             :  * SyncOneBuffer -- process a single buffer during syncing.
    3903             :  *
    3904             :  * If skip_recently_used is true, we don't write currently-pinned buffers, nor
    3905             :  * buffers marked recently used, as these are not replacement candidates.
    3906             :  *
    3907             :  * Returns a bitmask containing the following flag bits:
    3908             :  *  BUF_WRITTEN: we wrote the buffer.
    3909             :  *  BUF_REUSABLE: buffer is available for replacement, ie, it has
    3910             :  *      pin count 0 and usage count 0.
    3911             :  *
    3912             :  * (BUF_WRITTEN could be set in error if FlushBuffer finds the buffer clean
    3913             :  * after locking it, but we don't care all that much.)
    3914             :  */
    3915             : static int
    3916     3737618 : SyncOneBuffer(int buf_id, bool skip_recently_used, WritebackContext *wb_context)
    3917             : {
    3918     3737618 :     BufferDesc *bufHdr = GetBufferDescriptor(buf_id);
    3919     3737618 :     int         result = 0;
    3920             :     uint32      buf_state;
    3921             :     BufferTag   tag;
    3922             : 
    3923             :     /* Make sure we can handle the pin */
    3924     3737618 :     ReservePrivateRefCountEntry();
    3925     3737618 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    3926             : 
    3927             :     /*
    3928             :      * Check whether buffer needs writing.
    3929             :      *
    3930             :      * We can make this check without taking the buffer content lock so long
    3931             :      * as we mark pages dirty in access methods *before* logging changes with
    3932             :      * XLogInsert(): if someone marks the buffer dirty just after our check we
    3933             :      * don't worry because our checkpoint.redo points before log record for
    3934             :      * upcoming changes and so we are not required to write such dirty buffer.
    3935             :      */
    3936     3737618 :     buf_state = LockBufHdr(bufHdr);
    3937             : 
    3938     3737618 :     if (BUF_STATE_GET_REFCOUNT(buf_state) == 0 &&
    3939     3732072 :         BUF_STATE_GET_USAGECOUNT(buf_state) == 0)
    3940             :     {
    3941     2442682 :         result |= BUF_REUSABLE;
    3942             :     }
    3943     1294936 :     else if (skip_recently_used)
    3944             :     {
    3945             :         /* Caller told us not to write recently-used buffers */
    3946      783480 :         UnlockBufHdr(bufHdr, buf_state);
    3947      783480 :         return result;
    3948             :     }
    3949             : 
    3950     2954138 :     if (!(buf_state & BM_VALID) || !(buf_state & BM_DIRTY))
    3951             :     {
    3952             :         /* It's clean, so nothing to do */
    3953     2406096 :         UnlockBufHdr(bufHdr, buf_state);
    3954     2406096 :         return result;
    3955             :     }
    3956             : 
    3957             :     /*
    3958             :      * Pin it, share-lock it, write it.  (FlushBuffer will do nothing if the
    3959             :      * buffer is clean by the time we've locked it.)
    3960             :      */
    3961      548042 :     PinBuffer_Locked(bufHdr);
    3962      548042 :     LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED);
    3963             : 
    3964      548042 :     FlushBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    3965             : 
    3966      548042 :     LWLockRelease(BufferDescriptorGetContentLock(bufHdr));
    3967             : 
    3968      548042 :     tag = bufHdr->tag;
    3969             : 
    3970      548042 :     UnpinBuffer(bufHdr);
    3971             : 
    3972             :     /*
    3973             :      * SyncOneBuffer() is only called by checkpointer and bgwriter, so
    3974             :      * IOContext will always be IOCONTEXT_NORMAL.
    3975             :      */
    3976      548042 :     ScheduleBufferTagForWriteback(wb_context, IOCONTEXT_NORMAL, &tag);
    3977             : 
    3978      548042 :     return result | BUF_WRITTEN;
    3979             : }
    3980             : 
    3981             : /*
    3982             :  *      AtEOXact_Buffers - clean up at end of transaction.
    3983             :  *
    3984             :  *      As of PostgreSQL 8.0, buffer pins should get released by the
    3985             :  *      ResourceOwner mechanism.  This routine is just a debugging
    3986             :  *      cross-check that no pins remain.
    3987             :  */
    3988             : void
    3989      852940 : AtEOXact_Buffers(bool isCommit)
    3990             : {
    3991      852940 :     CheckForBufferLeaks();
    3992             : 
    3993      852940 :     AtEOXact_LocalBuffers(isCommit);
    3994             : 
    3995             :     Assert(PrivateRefCountOverflowed == 0);
    3996      852940 : }
    3997             : 
    3998             : /*
    3999             :  * Initialize access to shared buffer pool
    4000             :  *
    4001             :  * This is called during backend startup (whether standalone or under the
    4002             :  * postmaster).  It sets up for this backend's access to the already-existing
    4003             :  * buffer pool.
    4004             :  */
    4005             : void
    4006       42770 : InitBufferManagerAccess(void)
    4007             : {
    4008             :     HASHCTL     hash_ctl;
    4009             : 
    4010             :     /*
    4011             :      * An advisory limit on the number of pins each backend should hold, based
    4012             :      * on shared_buffers and the maximum number of connections possible.
    4013             :      * That's very pessimistic, but outside toy-sized shared_buffers it should
    4014             :      * allow plenty of pins.  LimitAdditionalPins() and
    4015             :      * GetAdditionalPinLimit() can be used to check the remaining balance.
    4016             :      */
    4017       42770 :     MaxProportionalPins = NBuffers / (MaxBackends + NUM_AUXILIARY_PROCS);
    4018             : 
    4019       42770 :     memset(&PrivateRefCountArray, 0, sizeof(PrivateRefCountArray));
    4020             : 
    4021       42770 :     hash_ctl.keysize = sizeof(int32);
    4022       42770 :     hash_ctl.entrysize = sizeof(PrivateRefCountEntry);
    4023             : 
    4024       42770 :     PrivateRefCountHash = hash_create("PrivateRefCount", 100, &hash_ctl,
    4025             :                                       HASH_ELEM | HASH_BLOBS);
    4026             : 
    4027             :     /*
    4028             :      * AtProcExit_Buffers needs LWLock access, and thereby has to be called at
    4029             :      * the corresponding phase of backend shutdown.
    4030             :      */
    4031             :     Assert(MyProc != NULL);
    4032       42770 :     on_shmem_exit(AtProcExit_Buffers, 0);
    4033       42770 : }
    4034             : 
    4035             : /*
    4036             :  * During backend exit, ensure that we released all shared-buffer locks and
    4037             :  * assert that we have no remaining pins.
    4038             :  */
    4039             : static void
    4040       42770 : AtProcExit_Buffers(int code, Datum arg)
    4041             : {
    4042       42770 :     UnlockBuffers();
    4043             : 
    4044       42770 :     CheckForBufferLeaks();
    4045             : 
    4046             :     /* localbuf.c needs a chance too */
    4047       42770 :     AtProcExit_LocalBuffers();
    4048       42770 : }
    4049             : 
    4050             : /*
    4051             :  *      CheckForBufferLeaks - ensure this backend holds no buffer pins
    4052             :  *
    4053             :  *      As of PostgreSQL 8.0, buffer pins should get released by the
    4054             :  *      ResourceOwner mechanism.  This routine is just a debugging
    4055             :  *      cross-check that no pins remain.
    4056             :  */
    4057             : static void
    4058      895710 : CheckForBufferLeaks(void)
    4059             : {
    4060             : #ifdef USE_ASSERT_CHECKING
    4061             :     int         RefCountErrors = 0;
    4062             :     PrivateRefCountEntry *res;
    4063             :     int         i;
    4064             :     char       *s;
    4065             : 
    4066             :     /* check the array */
    4067             :     for (i = 0; i < REFCOUNT_ARRAY_ENTRIES; i++)
    4068             :     {
    4069             :         res = &PrivateRefCountArray[i];
    4070             : 
    4071             :         if (res->buffer != InvalidBuffer)
    4072             :         {
    4073             :             s = DebugPrintBufferRefcount(res->buffer);
    4074             :             elog(WARNING, "buffer refcount leak: %s", s);
    4075             :             pfree(s);
    4076             : 
    4077             :             RefCountErrors++;
    4078             :         }
    4079             :     }
    4080             : 
    4081             :     /* if necessary search the hash */
    4082             :     if (PrivateRefCountOverflowed)
    4083             :     {
    4084             :         HASH_SEQ_STATUS hstat;
    4085             : 
    4086             :         hash_seq_init(&hstat, PrivateRefCountHash);
    4087             :         while ((res = (PrivateRefCountEntry *) hash_seq_search(&hstat)) != NULL)
    4088             :         {
    4089             :             s = DebugPrintBufferRefcount(res->buffer);
    4090             :             elog(WARNING, "buffer refcount leak: %s", s);
    4091             :             pfree(s);
    4092             :             RefCountErrors++;
    4093             :         }
    4094             :     }
    4095             : 
    4096             :     Assert(RefCountErrors == 0);
    4097             : #endif
    4098      895710 : }
    4099             : 
    4100             : /*
    4101             :  * Helper routine to issue warnings when a buffer is unexpectedly pinned
    4102             :  */
    4103             : char *
    4104          80 : DebugPrintBufferRefcount(Buffer buffer)
    4105             : {
    4106             :     BufferDesc *buf;
    4107             :     int32       loccount;
    4108             :     char       *result;
    4109             :     ProcNumber  backend;
    4110             :     uint32      buf_state;
    4111             : 
    4112             :     Assert(BufferIsValid(buffer));
    4113          80 :     if (BufferIsLocal(buffer))
    4114             :     {
    4115          32 :         buf = GetLocalBufferDescriptor(-buffer - 1);
    4116          32 :         loccount = LocalRefCount[-buffer - 1];
    4117          32 :         backend = MyProcNumber;
    4118             :     }
    4119             :     else
    4120             :     {
    4121          48 :         buf = GetBufferDescriptor(buffer - 1);
    4122          48 :         loccount = GetPrivateRefCount(buffer);
    4123          48 :         backend = INVALID_PROC_NUMBER;
    4124             :     }
    4125             : 
    4126             :     /* theoretically we should lock the bufhdr here */
    4127          80 :     buf_state = pg_atomic_read_u32(&buf->state);
    4128             : 
    4129          80 :     result = psprintf("[%03d] (rel=%s, blockNum=%u, flags=0x%x, refcount=%u %d)",
    4130             :                       buffer,
    4131          80 :                       relpathbackend(BufTagGetRelFileLocator(&buf->tag), backend,
    4132             :                                      BufTagGetForkNum(&buf->tag)).str,
    4133             :                       buf->tag.blockNum, buf_state & BUF_FLAG_MASK,
    4134             :                       BUF_STATE_GET_REFCOUNT(buf_state), loccount);
    4135          80 :     return result;
    4136             : }
    4137             : 
    4138             : /*
    4139             :  * CheckPointBuffers
    4140             :  *
    4141             :  * Flush all dirty blocks in buffer pool to disk at checkpoint time.
    4142             :  *
    4143             :  * Note: temporary relations do not participate in checkpoints, so they don't
    4144             :  * need to be flushed.
    4145             :  */
    4146             : void
    4147        2642 : CheckPointBuffers(int flags)
    4148             : {
    4149        2642 :     BufferSync(flags);
    4150        2642 : }
    4151             : 
    4152             : /*
    4153             :  * BufferGetBlockNumber
    4154             :  *      Returns the block number associated with a buffer.
    4155             :  *
    4156             :  * Note:
    4157             :  *      Assumes that the buffer is valid and pinned, else the
    4158             :  *      value may be obsolete immediately...
    4159             :  */
    4160             : BlockNumber
    4161    97451078 : BufferGetBlockNumber(Buffer buffer)
    4162             : {
    4163             :     BufferDesc *bufHdr;
    4164             : 
    4165             :     Assert(BufferIsPinned(buffer));
    4166             : 
    4167    97451078 :     if (BufferIsLocal(buffer))
    4168     3803854 :         bufHdr = GetLocalBufferDescriptor(-buffer - 1);
    4169             :     else
    4170    93647224 :         bufHdr = GetBufferDescriptor(buffer - 1);
    4171             : 
    4172             :     /* pinned, so OK to read tag without spinlock */
    4173    97451078 :     return bufHdr->tag.blockNum;
    4174             : }
    4175             : 
    4176             : /*
    4177             :  * BufferGetTag
    4178             :  *      Returns the relfilelocator, fork number and block number associated with
    4179             :  *      a buffer.
    4180             :  */
    4181             : void
    4182    29817958 : BufferGetTag(Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum,
    4183             :              BlockNumber *blknum)
    4184             : {
    4185             :     BufferDesc *bufHdr;
    4186             : 
    4187             :     /* Do the same checks as BufferGetBlockNumber. */
    4188             :     Assert(BufferIsPinned(buffer));
    4189             : 
    4190    29817958 :     if (BufferIsLocal(buffer))
    4191           0 :         bufHdr = GetLocalBufferDescriptor(-buffer - 1);
    4192             :     else
    4193    29817958 :         bufHdr = GetBufferDescriptor(buffer - 1);
    4194             : 
    4195             :     /* pinned, so OK to read tag without spinlock */
    4196    29817958 :     *rlocator = BufTagGetRelFileLocator(&bufHdr->tag);
    4197    29817958 :     *forknum = BufTagGetForkNum(&bufHdr->tag);
    4198    29817958 :     *blknum = bufHdr->tag.blockNum;
    4199    29817958 : }
    4200             : 
    4201             : /*
    4202             :  * FlushBuffer
    4203             :  *      Physically write out a shared buffer.
    4204             :  *
    4205             :  * NOTE: this actually just passes the buffer contents to the kernel; the
    4206             :  * real write to disk won't happen until the kernel feels like it.  This
    4207             :  * is okay from our point of view since we can redo the changes from WAL.
    4208             :  * However, we will need to force the changes to disk via fsync before
    4209             :  * we can checkpoint WAL.
    4210             :  *
    4211             :  * The caller must hold a pin on the buffer and have share-locked the
    4212             :  * buffer contents.  (Note: a share-lock does not prevent updates of
    4213             :  * hint bits in the buffer, so the page could change while the write
    4214             :  * is in progress, but we assume that that will not invalidate the data
    4215             :  * written.)
    4216             :  *
    4217             :  * If the caller has an smgr reference for the buffer's relation, pass it
    4218             :  * as the second parameter.  If not, pass NULL.
    4219             :  */
    4220             : static void
    4221     1053802 : FlushBuffer(BufferDesc *buf, SMgrRelation reln, IOObject io_object,
    4222             :             IOContext io_context)
    4223             : {
    4224             :     XLogRecPtr  recptr;
    4225             :     ErrorContextCallback errcallback;
    4226             :     instr_time  io_start;
    4227             :     Block       bufBlock;
    4228             :     char       *bufToWrite;
    4229             :     uint32      buf_state;
    4230             : 
    4231             :     /*
    4232             :      * Try to start an I/O operation.  If StartBufferIO returns false, then
    4233             :      * someone else flushed the buffer before we could, so we need not do
    4234             :      * anything.
    4235             :      */
    4236     1053802 :     if (!StartBufferIO(buf, false, false))
    4237          40 :         return;
    4238             : 
    4239             :     /* Setup error traceback support for ereport() */
    4240     1053762 :     errcallback.callback = shared_buffer_write_error_callback;
    4241     1053762 :     errcallback.arg = buf;
    4242     1053762 :     errcallback.previous = error_context_stack;
    4243     1053762 :     error_context_stack = &errcallback;
    4244             : 
    4245             :     /* Find smgr relation for buffer */
    4246     1053762 :     if (reln == NULL)
    4247     1043380 :         reln = smgropen(BufTagGetRelFileLocator(&buf->tag), INVALID_PROC_NUMBER);
    4248             : 
    4249             :     TRACE_POSTGRESQL_BUFFER_FLUSH_START(BufTagGetForkNum(&buf->tag),
    4250             :                                         buf->tag.blockNum,
    4251             :                                         reln->smgr_rlocator.locator.spcOid,
    4252             :                                         reln->smgr_rlocator.locator.dbOid,
    4253             :                                         reln->smgr_rlocator.locator.relNumber);
    4254             : 
    4255     1053762 :     buf_state = LockBufHdr(buf);
    4256             : 
    4257             :     /*
    4258             :      * Run PageGetLSN while holding header lock, since we don't have the
    4259             :      * buffer locked exclusively in all cases.
    4260             :      */
    4261     1053762 :     recptr = BufferGetLSN(buf);
    4262             : 
    4263             :     /* To check if block content changes while flushing. - vadim 01/17/97 */
    4264     1053762 :     buf_state &= ~BM_JUST_DIRTIED;
    4265     1053762 :     UnlockBufHdr(buf, buf_state);
    4266             : 
    4267             :     /*
    4268             :      * Force XLOG flush up to buffer's LSN.  This implements the basic WAL
    4269             :      * rule that log updates must hit disk before any of the data-file changes
    4270             :      * they describe do.
    4271             :      *
    4272             :      * However, this rule does not apply to unlogged relations, which will be
    4273             :      * lost after a crash anyway.  Most unlogged relation pages do not bear
    4274             :      * LSNs since we never emit WAL records for them, and therefore flushing
    4275             :      * up through the buffer LSN would be useless, but harmless.  However,
    4276             :      * GiST indexes use LSNs internally to track page-splits, and therefore
    4277             :      * unlogged GiST pages bear "fake" LSNs generated by
    4278             :      * GetFakeLSNForUnloggedRel.  It is unlikely but possible that the fake
    4279             :      * LSN counter could advance past the WAL insertion point; and if it did
    4280             :      * happen, attempting to flush WAL through that location would fail, with
    4281             :      * disastrous system-wide consequences.  To make sure that can't happen,
    4282             :      * skip the flush if the buffer isn't permanent.
    4283             :      */
    4284     1053762 :     if (buf_state & BM_PERMANENT)
    4285     1050200 :         XLogFlush(recptr);
    4286             : 
    4287             :     /*
    4288             :      * Now it's safe to write the buffer to disk. Note that no one else should
    4289             :      * have been able to write it, while we were busy with log flushing,
    4290             :      * because we got the exclusive right to perform I/O by setting the
    4291             :      * BM_IO_IN_PROGRESS bit.
    4292             :      */
    4293     1053762 :     bufBlock = BufHdrGetBlock(buf);
    4294             : 
    4295             :     /*
    4296             :      * Update page checksum if desired.  Since we have only shared lock on the
    4297             :      * buffer, other processes might be updating hint bits in it, so we must
    4298             :      * copy the page to private storage if we do checksumming.
    4299             :      */
    4300     1053762 :     bufToWrite = PageSetChecksumCopy((Page) bufBlock, buf->tag.blockNum);
    4301             : 
    4302     1053762 :     io_start = pgstat_prepare_io_time(track_io_timing);
    4303             : 
    4304             :     /*
    4305             :      * bufToWrite is either the shared buffer or a copy, as appropriate.
    4306             :      */
    4307     1053762 :     smgrwrite(reln,
    4308     1053762 :               BufTagGetForkNum(&buf->tag),
    4309             :               buf->tag.blockNum,
    4310             :               bufToWrite,
    4311             :               false);
    4312             : 
    4313             :     /*
    4314             :      * When a strategy is in use, only flushes of dirty buffers already in the
    4315             :      * strategy ring are counted as strategy writes (IOCONTEXT
    4316             :      * [BULKREAD|BULKWRITE|VACUUM] IOOP_WRITE) for the purpose of IO
    4317             :      * statistics tracking.
    4318             :      *
    4319             :      * If a shared buffer initially added to the ring must be flushed before
    4320             :      * being used, this is counted as an IOCONTEXT_NORMAL IOOP_WRITE.
    4321             :      *
    4322             :      * If a shared buffer which was added to the ring later because the
    4323             :      * current strategy buffer is pinned or in use or because all strategy
    4324             :      * buffers were dirty and rejected (for BAS_BULKREAD operations only)
    4325             :      * requires flushing, this is counted as an IOCONTEXT_NORMAL IOOP_WRITE
    4326             :      * (from_ring will be false).
    4327             :      *
    4328             :      * When a strategy is not in use, the write can only be a "regular" write
    4329             :      * of a dirty shared buffer (IOCONTEXT_NORMAL IOOP_WRITE).
    4330             :      */
    4331     1053762 :     pgstat_count_io_op_time(IOOBJECT_RELATION, io_context,
    4332             :                             IOOP_WRITE, io_start, 1, BLCKSZ);
    4333             : 
    4334     1053762 :     pgBufferUsage.shared_blks_written++;
    4335             : 
    4336             :     /*
    4337             :      * Mark the buffer as clean (unless BM_JUST_DIRTIED has become set) and
    4338             :      * end the BM_IO_IN_PROGRESS state.
    4339             :      */
    4340     1053762 :     TerminateBufferIO(buf, true, 0, true, false);
    4341             : 
    4342             :     TRACE_POSTGRESQL_BUFFER_FLUSH_DONE(BufTagGetForkNum(&buf->tag),
    4343             :                                        buf->tag.blockNum,
    4344             :                                        reln->smgr_rlocator.locator.spcOid,
    4345             :                                        reln->smgr_rlocator.locator.dbOid,
    4346             :                                        reln->smgr_rlocator.locator.relNumber);
    4347             : 
    4348             :     /* Pop the error context stack */
    4349     1053762 :     error_context_stack = errcallback.previous;
    4350             : }
    4351             : 
    4352             : /*
    4353             :  * RelationGetNumberOfBlocksInFork
    4354             :  *      Determines the current number of pages in the specified relation fork.
    4355             :  *
    4356             :  * Note that the accuracy of the result will depend on the details of the
    4357             :  * relation's storage. For builtin AMs it'll be accurate, but for external AMs
    4358             :  * it might not be.
    4359             :  */
    4360             : BlockNumber
    4361     3502288 : RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
    4362             : {
    4363     3502288 :     if (RELKIND_HAS_TABLE_AM(relation->rd_rel->relkind))
    4364             :     {
    4365             :         /*
    4366             :          * Not every table AM uses BLCKSZ wide fixed size blocks. Therefore
    4367             :          * tableam returns the size in bytes - but for the purpose of this
    4368             :          * routine, we want the number of blocks. Therefore divide, rounding
    4369             :          * up.
    4370             :          */
    4371             :         uint64      szbytes;
    4372             : 
    4373     2648804 :         szbytes = table_relation_size(relation, forkNum);
    4374             : 
    4375     2648766 :         return (szbytes + (BLCKSZ - 1)) / BLCKSZ;
    4376             :     }
    4377      853484 :     else if (RELKIND_HAS_STORAGE(relation->rd_rel->relkind))
    4378             :     {
    4379      853484 :         return smgrnblocks(RelationGetSmgr(relation), forkNum);
    4380             :     }
    4381             :     else
    4382             :         Assert(false);
    4383             : 
    4384           0 :     return 0;                   /* keep compiler quiet */
    4385             : }
    4386             : 
    4387             : /*
    4388             :  * BufferIsPermanent
    4389             :  *      Determines whether a buffer will potentially still be around after
    4390             :  *      a crash.  Caller must hold a buffer pin.
    4391             :  */
    4392             : bool
    4393    19742234 : BufferIsPermanent(Buffer buffer)
    4394             : {
    4395             :     BufferDesc *bufHdr;
    4396             : 
    4397             :     /* Local buffers are used only for temp relations. */
    4398    19742234 :     if (BufferIsLocal(buffer))
    4399     1248026 :         return false;
    4400             : 
    4401             :     /* Make sure we've got a real buffer, and that we hold a pin on it. */
    4402             :     Assert(BufferIsValid(buffer));
    4403             :     Assert(BufferIsPinned(buffer));
    4404             : 
    4405             :     /*
    4406             :      * BM_PERMANENT can't be changed while we hold a pin on the buffer, so we
    4407             :      * need not bother with the buffer header spinlock.  Even if someone else
    4408             :      * changes the buffer header state while we're doing this, the state is
    4409             :      * changed atomically, so we'll read the old value or the new value, but
    4410             :      * not random garbage.
    4411             :      */
    4412    18494208 :     bufHdr = GetBufferDescriptor(buffer - 1);
    4413    18494208 :     return (pg_atomic_read_u32(&bufHdr->state) & BM_PERMANENT) != 0;
    4414             : }
    4415             : 
    4416             : /*
    4417             :  * BufferGetLSNAtomic
    4418             :  *      Retrieves the LSN of the buffer atomically using a buffer header lock.
    4419             :  *      This is necessary for some callers who may not have an exclusive lock
    4420             :  *      on the buffer.
    4421             :  */
    4422             : XLogRecPtr
    4423    17411662 : BufferGetLSNAtomic(Buffer buffer)
    4424             : {
    4425    17411662 :     char       *page = BufferGetPage(buffer);
    4426             :     BufferDesc *bufHdr;
    4427             :     XLogRecPtr  lsn;
    4428             :     uint32      buf_state;
    4429             : 
    4430             :     /*
    4431             :      * If we don't need locking for correctness, fastpath out.
    4432             :      */
    4433    17411662 :     if (!XLogHintBitIsNeeded() || BufferIsLocal(buffer))
    4434      571974 :         return PageGetLSN(page);
    4435             : 
    4436             :     /* Make sure we've got a real buffer, and that we hold a pin on it. */
    4437             :     Assert(BufferIsValid(buffer));
    4438             :     Assert(BufferIsPinned(buffer));
    4439             : 
    4440    16839688 :     bufHdr = GetBufferDescriptor(buffer - 1);
    4441    16839688 :     buf_state = LockBufHdr(bufHdr);
    4442    16839688 :     lsn = PageGetLSN(page);
    4443    16839688 :     UnlockBufHdr(bufHdr, buf_state);
    4444             : 
    4445    16839688 :     return lsn;
    4446             : }
    4447             : 
    4448             : /* ---------------------------------------------------------------------
    4449             :  *      DropRelationBuffers
    4450             :  *
    4451             :  *      This function removes from the buffer pool all the pages of the
    4452             :  *      specified relation forks that have block numbers >= firstDelBlock.
    4453             :  *      (In particular, with firstDelBlock = 0, all pages are removed.)
    4454             :  *      Dirty pages are simply dropped, without bothering to write them
    4455             :  *      out first.  Therefore, this is NOT rollback-able, and so should be
    4456             :  *      used only with extreme caution!
    4457             :  *
    4458             :  *      Currently, this is called only from smgr.c when the underlying file
    4459             :  *      is about to be deleted or truncated (firstDelBlock is needed for
    4460             :  *      the truncation case).  The data in the affected pages would therefore
    4461             :  *      be deleted momentarily anyway, and there is no point in writing it.
    4462             :  *      It is the responsibility of higher-level code to ensure that the
    4463             :  *      deletion or truncation does not lose any data that could be needed
    4464             :  *      later.  It is also the responsibility of higher-level code to ensure
    4465             :  *      that no other process could be trying to load more pages of the
    4466             :  *      relation into buffers.
    4467             :  * --------------------------------------------------------------------
    4468             :  */
    4469             : void
    4470        1228 : DropRelationBuffers(SMgrRelation smgr_reln, ForkNumber *forkNum,
    4471             :                     int nforks, BlockNumber *firstDelBlock)
    4472             : {
    4473             :     int         i;
    4474             :     int         j;
    4475             :     RelFileLocatorBackend rlocator;
    4476             :     BlockNumber nForkBlock[MAX_FORKNUM];
    4477        1228 :     uint64      nBlocksToInvalidate = 0;
    4478             : 
    4479        1228 :     rlocator = smgr_reln->smgr_rlocator;
    4480             : 
    4481             :     /* If it's a local relation, it's localbuf.c's problem. */
    4482        1228 :     if (RelFileLocatorBackendIsTemp(rlocator))
    4483             :     {
    4484         664 :         if (rlocator.backend == MyProcNumber)
    4485             :         {
    4486        1374 :             for (j = 0; j < nforks; j++)
    4487         710 :                 DropRelationLocalBuffers(rlocator.locator, forkNum[j],
    4488         710 :                                          firstDelBlock[j]);
    4489             :         }
    4490         752 :         return;
    4491             :     }
    4492             : 
    4493             :     /*
    4494             :      * To remove all the pages of the specified relation forks from the buffer
    4495             :      * pool, we need to scan the entire buffer pool but we can optimize it by
    4496             :      * finding the buffers from BufMapping table provided we know the exact
    4497             :      * size of each fork of the relation. The exact size is required to ensure
    4498             :      * that we don't leave any buffer for the relation being dropped as
    4499             :      * otherwise the background writer or checkpointer can lead to a PANIC
    4500             :      * error while flushing buffers corresponding to files that don't exist.
    4501             :      *
    4502             :      * To know the exact size, we rely on the size cached for each fork by us
    4503             :      * during recovery which limits the optimization to recovery and on
    4504             :      * standbys but we can easily extend it once we have shared cache for
    4505             :      * relation size.
    4506             :      *
    4507             :      * In recovery, we cache the value returned by the first lseek(SEEK_END)
    4508             :      * and the future writes keeps the cached value up-to-date. See
    4509             :      * smgrextend. It is possible that the value of the first lseek is smaller
    4510             :      * than the actual number of existing blocks in the file due to buggy
    4511             :      * Linux kernels that might not have accounted for the recent write. But
    4512             :      * that should be fine because there must not be any buffers after that
    4513             :      * file size.
    4514             :      */
    4515         780 :     for (i = 0; i < nforks; i++)
    4516             :     {
    4517             :         /* Get the number of blocks for a relation's fork */
    4518         668 :         nForkBlock[i] = smgrnblocks_cached(smgr_reln, forkNum[i]);
    4519             : 
    4520         668 :         if (nForkBlock[i] == InvalidBlockNumber)
    4521             :         {
    4522         452 :             nBlocksToInvalidate = InvalidBlockNumber;
    4523         452 :             break;
    4524             :         }
    4525             : 
    4526             :         /* calculate the number of blocks to be invalidated */
    4527         216 :         nBlocksToInvalidate += (nForkBlock[i] - firstDelBlock[i]);
    4528             :     }
    4529             : 
    4530             :     /*
    4531             :      * We apply the optimization iff the total number of blocks to invalidate
    4532             :      * is below the BUF_DROP_FULL_SCAN_THRESHOLD.
    4533             :      */
    4534         564 :     if (BlockNumberIsValid(nBlocksToInvalidate) &&
    4535         112 :         nBlocksToInvalidate < BUF_DROP_FULL_SCAN_THRESHOLD)
    4536             :     {
    4537         250 :         for (j = 0; j < nforks; j++)
    4538         162 :             FindAndDropRelationBuffers(rlocator.locator, forkNum[j],
    4539         162 :                                        nForkBlock[j], firstDelBlock[j]);
    4540          88 :         return;
    4541             :     }
    4542             : 
    4543     6206172 :     for (i = 0; i < NBuffers; i++)
    4544             :     {
    4545     6205696 :         BufferDesc *bufHdr = GetBufferDescriptor(i);
    4546             :         uint32      buf_state;
    4547             : 
    4548             :         /*
    4549             :          * We can make this a tad faster by prechecking the buffer tag before
    4550             :          * we attempt to lock the buffer; this saves a lot of lock
    4551             :          * acquisitions in typical cases.  It should be safe because the
    4552             :          * caller must have AccessExclusiveLock on the relation, or some other
    4553             :          * reason to be certain that no one is loading new pages of the rel
    4554             :          * into the buffer pool.  (Otherwise we might well miss such pages
    4555             :          * entirely.)  Therefore, while the tag might be changing while we
    4556             :          * look at it, it can't be changing *to* a value we care about, only
    4557             :          * *away* from such a value.  So false negatives are impossible, and
    4558             :          * false positives are safe because we'll recheck after getting the
    4559             :          * buffer lock.
    4560             :          *
    4561             :          * We could check forkNum and blockNum as well as the rlocator, but
    4562             :          * the incremental win from doing so seems small.
    4563             :          */
    4564     6205696 :         if (!BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator.locator))
    4565     6189606 :             continue;
    4566             : 
    4567       16090 :         buf_state = LockBufHdr(bufHdr);
    4568             : 
    4569       40780 :         for (j = 0; j < nforks; j++)
    4570             :         {
    4571       28724 :             if (BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator.locator) &&
    4572       28724 :                 BufTagGetForkNum(&bufHdr->tag) == forkNum[j] &&
    4573       15872 :                 bufHdr->tag.blockNum >= firstDelBlock[j])
    4574             :             {
    4575        4034 :                 InvalidateBuffer(bufHdr);   /* releases spinlock */
    4576        4034 :                 break;
    4577             :             }
    4578             :         }
    4579       16090 :         if (j >= nforks)
    4580       12056 :             UnlockBufHdr(bufHdr, buf_state);
    4581             :     }
    4582             : }
    4583             : 
    4584             : /* ---------------------------------------------------------------------
    4585             :  *      DropRelationsAllBuffers
    4586             :  *
    4587             :  *      This function removes from the buffer pool all the pages of all
    4588             :  *      forks of the specified relations.  It's equivalent to calling
    4589             :  *      DropRelationBuffers once per fork per relation with firstDelBlock = 0.
    4590             :  *      --------------------------------------------------------------------
    4591             :  */
    4592             : void
    4593       27638 : DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators)
    4594             : {
    4595             :     int         i;
    4596       27638 :     int         n = 0;
    4597             :     SMgrRelation *rels;
    4598             :     BlockNumber (*block)[MAX_FORKNUM + 1];
    4599       27638 :     uint64      nBlocksToInvalidate = 0;
    4600             :     RelFileLocator *locators;
    4601       27638 :     bool        cached = true;
    4602             :     bool        use_bsearch;
    4603             : 
    4604       27638 :     if (nlocators == 0)
    4605           0 :         return;
    4606             : 
    4607       27638 :     rels = palloc(sizeof(SMgrRelation) * nlocators);    /* non-local relations */
    4608             : 
    4609             :     /* If it's a local relation, it's localbuf.c's problem. */
    4610      119610 :     for (i = 0; i < nlocators; i++)
    4611             :     {
    4612       91972 :         if (RelFileLocatorBackendIsTemp(smgr_reln[i]->smgr_rlocator))
    4613             :         {
    4614        6168 :             if (smgr_reln[i]->smgr_rlocator.backend == MyProcNumber)
    4615        6168 :                 DropRelationAllLocalBuffers(smgr_reln[i]->smgr_rlocator.locator);
    4616             :         }
    4617             :         else
    4618       85804 :             rels[n++] = smgr_reln[i];
    4619             :     }
    4620             : 
    4621             :     /*
    4622             :      * If there are no non-local relations, then we're done. Release the
    4623             :      * memory and return.
    4624             :      */
    4625       27638 :     if (n == 0)
    4626             :     {
    4627        1564 :         pfree(rels);
    4628        1564 :         return;
    4629             :     }
    4630             : 
    4631             :     /*
    4632             :      * This is used to remember the number of blocks for all the relations
    4633             :      * forks.
    4634             :      */
    4635             :     block = (BlockNumber (*)[MAX_FORKNUM + 1])
    4636       26074 :         palloc(sizeof(BlockNumber) * n * (MAX_FORKNUM + 1));
    4637             : 
    4638             :     /*
    4639             :      * We can avoid scanning the entire buffer pool if we know the exact size
    4640             :      * of each of the given relation forks. See DropRelationBuffers.
    4641             :      */
    4642       54794 :     for (i = 0; i < n && cached; i++)
    4643             :     {
    4644       45970 :         for (int j = 0; j <= MAX_FORKNUM; j++)
    4645             :         {
    4646             :             /* Get the number of blocks for a relation's fork. */
    4647       41680 :             block[i][j] = smgrnblocks_cached(rels[i], j);
    4648             : 
    4649             :             /* We need to only consider the relation forks that exists. */
    4650       41680 :             if (block[i][j] == InvalidBlockNumber)
    4651             :             {
    4652       37006 :                 if (!smgrexists(rels[i], j))
    4653       12576 :                     continue;
    4654       24430 :                 cached = false;
    4655       24430 :                 break;
    4656             :             }
    4657             : 
    4658             :             /* calculate the total number of blocks to be invalidated */
    4659        4674 :             nBlocksToInvalidate += block[i][j];
    4660             :         }
    4661             :     }
    4662             : 
    4663             :     /*
    4664             :      * We apply the optimization iff the total number of blocks to invalidate
    4665             :      * is below the BUF_DROP_FULL_SCAN_THRESHOLD.
    4666             :      */
    4667       26074 :     if (cached && nBlocksToInvalidate < BUF_DROP_FULL_SCAN_THRESHOLD)
    4668             :     {
    4669        2710 :         for (i = 0; i < n; i++)
    4670             :         {
    4671        7450 :             for (int j = 0; j <= MAX_FORKNUM; j++)
    4672             :             {
    4673             :                 /* ignore relation forks that doesn't exist */
    4674        5960 :                 if (!BlockNumberIsValid(block[i][j]))
    4675        4448 :                     continue;
    4676             : 
    4677             :                 /* drop all the buffers for a particular relation fork */
    4678        1512 :                 FindAndDropRelationBuffers(rels[i]->smgr_rlocator.locator,
    4679        1512 :                                            j, block[i][j], 0);
    4680             :             }
    4681             :         }
    4682             : 
    4683        1220 :         pfree(block);
    4684        1220 :         pfree(rels);
    4685        1220 :         return;
    4686             :     }
    4687             : 
    4688       24854 :     pfree(block);
    4689       24854 :     locators = palloc(sizeof(RelFileLocator) * n);  /* non-local relations */
    4690      109168 :     for (i = 0; i < n; i++)
    4691       84314 :         locators[i] = rels[i]->smgr_rlocator.locator;
    4692             : 
    4693             :     /*
    4694             :      * For low number of relations to drop just use a simple walk through, to
    4695             :      * save the bsearch overhead. The threshold to use is rather a guess than
    4696             :      * an exactly determined value, as it depends on many factors (CPU and RAM
    4697             :      * speeds, amount of shared buffers etc.).
    4698             :      */
    4699       24854 :     use_bsearch = n > RELS_BSEARCH_THRESHOLD;
    4700             : 
    4701             :     /* sort the list of rlocators if necessary */
    4702       24854 :     if (use_bsearch)
    4703         334 :         qsort(locators, n, sizeof(RelFileLocator), rlocator_comparator);
    4704             : 
    4705   277119766 :     for (i = 0; i < NBuffers; i++)
    4706             :     {
    4707   277094912 :         RelFileLocator *rlocator = NULL;
    4708   277094912 :         BufferDesc *bufHdr = GetBufferDescriptor(i);
    4709             :         uint32      buf_state;
    4710             : 
    4711             :         /*
    4712             :          * As in DropRelationBuffers, an unlocked precheck should be safe and
    4713             :          * saves some cycles.
    4714             :          */
    4715             : 
    4716   277094912 :         if (!use_bsearch)
    4717             :         {
    4718             :             int         j;
    4719             : 
    4720  1084268978 :             for (j = 0; j < n; j++)
    4721             :             {
    4722   810871850 :                 if (BufTagMatchesRelFileLocator(&bufHdr->tag, &locators[j]))
    4723             :                 {
    4724      176248 :                     rlocator = &locators[j];
    4725      176248 :                     break;
    4726             :                 }
    4727             :             }
    4728             :         }
    4729             :         else
    4730             :         {
    4731             :             RelFileLocator locator;
    4732             : 
    4733     3521536 :             locator = BufTagGetRelFileLocator(&bufHdr->tag);
    4734     3521536 :             rlocator = bsearch(&locator,
    4735             :                                locators, n, sizeof(RelFileLocator),
    4736             :                                rlocator_comparator);
    4737             :         }
    4738             : 
    4739             :         /* buffer doesn't belong to any of the given relfilelocators; skip it */
    4740   277094912 :         if (rlocator == NULL)
    4741   276915208 :             continue;
    4742             : 
    4743      179704 :         buf_state = LockBufHdr(bufHdr);
    4744      179704 :         if (BufTagMatchesRelFileLocator(&bufHdr->tag, rlocator))
    4745      179704 :             InvalidateBuffer(bufHdr);   /* releases spinlock */
    4746             :         else
    4747           0 :             UnlockBufHdr(bufHdr, buf_state);
    4748             :     }
    4749             : 
    4750       24854 :     pfree(locators);
    4751       24854 :     pfree(rels);
    4752             : }
    4753             : 
    4754             : /* ---------------------------------------------------------------------
    4755             :  *      FindAndDropRelationBuffers
    4756             :  *
    4757             :  *      This function performs look up in BufMapping table and removes from the
    4758             :  *      buffer pool all the pages of the specified relation fork that has block
    4759             :  *      number >= firstDelBlock. (In particular, with firstDelBlock = 0, all
    4760             :  *      pages are removed.)
    4761             :  * --------------------------------------------------------------------
    4762             :  */
    4763             : static void
    4764        1674 : FindAndDropRelationBuffers(RelFileLocator rlocator, ForkNumber forkNum,
    4765             :                            BlockNumber nForkBlock,
    4766             :                            BlockNumber firstDelBlock)
    4767             : {
    4768             :     BlockNumber curBlock;
    4769             : 
    4770        4026 :     for (curBlock = firstDelBlock; curBlock < nForkBlock; curBlock++)
    4771             :     {
    4772             :         uint32      bufHash;    /* hash value for tag */
    4773             :         BufferTag   bufTag;     /* identity of requested block */
    4774             :         LWLock     *bufPartitionLock;   /* buffer partition lock for it */
    4775             :         int         buf_id;
    4776             :         BufferDesc *bufHdr;
    4777             :         uint32      buf_state;
    4778             : 
    4779             :         /* create a tag so we can lookup the buffer */
    4780        2352 :         InitBufferTag(&bufTag, &rlocator, forkNum, curBlock);
    4781             : 
    4782             :         /* determine its hash code and partition lock ID */
    4783        2352 :         bufHash = BufTableHashCode(&bufTag);
    4784        2352 :         bufPartitionLock = BufMappingPartitionLock(bufHash);
    4785             : 
    4786             :         /* Check that it is in the buffer pool. If not, do nothing. */
    4787        2352 :         LWLockAcquire(bufPartitionLock, LW_SHARED);
    4788        2352 :         buf_id = BufTableLookup(&bufTag, bufHash);
    4789        2352 :         LWLockRelease(bufPartitionLock);
    4790             : 
    4791        2352 :         if (buf_id < 0)
    4792         242 :             continue;
    4793             : 
    4794        2110 :         bufHdr = GetBufferDescriptor(buf_id);
    4795             : 
    4796             :         /*
    4797             :          * We need to lock the buffer header and recheck if the buffer is
    4798             :          * still associated with the same block because the buffer could be
    4799             :          * evicted by some other backend loading blocks for a different
    4800             :          * relation after we release lock on the BufMapping table.
    4801             :          */
    4802        2110 :         buf_state = LockBufHdr(bufHdr);
    4803             : 
    4804        4220 :         if (BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator) &&
    4805        2110 :             BufTagGetForkNum(&bufHdr->tag) == forkNum &&
    4806        2110 :             bufHdr->tag.blockNum >= firstDelBlock)
    4807        2110 :             InvalidateBuffer(bufHdr);   /* releases spinlock */
    4808             :         else
    4809           0 :             UnlockBufHdr(bufHdr, buf_state);
    4810             :     }
    4811        1674 : }
    4812             : 
    4813             : /* ---------------------------------------------------------------------
    4814             :  *      DropDatabaseBuffers
    4815             :  *
    4816             :  *      This function removes all the buffers in the buffer cache for a
    4817             :  *      particular database.  Dirty pages are simply dropped, without
    4818             :  *      bothering to write them out first.  This is used when we destroy a
    4819             :  *      database, to avoid trying to flush data to disk when the directory
    4820             :  *      tree no longer exists.  Implementation is pretty similar to
    4821             :  *      DropRelationBuffers() which is for destroying just one relation.
    4822             :  * --------------------------------------------------------------------
    4823             :  */
    4824             : void
    4825         136 : DropDatabaseBuffers(Oid dbid)
    4826             : {
    4827             :     int         i;
    4828             : 
    4829             :     /*
    4830             :      * We needn't consider local buffers, since by assumption the target
    4831             :      * database isn't our own.
    4832             :      */
    4833             : 
    4834      992904 :     for (i = 0; i < NBuffers; i++)
    4835             :     {
    4836      992768 :         BufferDesc *bufHdr = GetBufferDescriptor(i);
    4837             :         uint32      buf_state;
    4838             : 
    4839             :         /*
    4840             :          * As in DropRelationBuffers, an unlocked precheck should be safe and
    4841             :          * saves some cycles.
    4842             :          */
    4843      992768 :         if (bufHdr->tag.dbOid != dbid)
    4844      965300 :             continue;
    4845             : 
    4846       27468 :         buf_state = LockBufHdr(bufHdr);
    4847       27468 :         if (bufHdr->tag.dbOid == dbid)
    4848       27468 :             InvalidateBuffer(bufHdr);   /* releases spinlock */
    4849             :         else
    4850           0 :             UnlockBufHdr(bufHdr, buf_state);
    4851             :     }
    4852         136 : }
    4853             : 
    4854             : /* ---------------------------------------------------------------------
    4855             :  *      FlushRelationBuffers
    4856             :  *
    4857             :  *      This function writes all dirty pages of a relation out to disk
    4858             :  *      (or more accurately, out to kernel disk buffers), ensuring that the
    4859             :  *      kernel has an up-to-date view of the relation.
    4860             :  *
    4861             :  *      Generally, the caller should be holding AccessExclusiveLock on the
    4862             :  *      target relation to ensure that no other backend is busy dirtying
    4863             :  *      more blocks of the relation; the effects can't be expected to last
    4864             :  *      after the lock is released.
    4865             :  *
    4866             :  *      XXX currently it sequentially searches the buffer pool, should be
    4867             :  *      changed to more clever ways of searching.  This routine is not
    4868             :  *      used in any performance-critical code paths, so it's not worth
    4869             :  *      adding additional overhead to normal paths to make it go faster.
    4870             :  * --------------------------------------------------------------------
    4871             :  */
    4872             : void
    4873         288 : FlushRelationBuffers(Relation rel)
    4874             : {
    4875             :     int         i;
    4876             :     BufferDesc *bufHdr;
    4877         288 :     SMgrRelation srel = RelationGetSmgr(rel);
    4878             : 
    4879         288 :     if (RelationUsesLocalBuffers(rel))
    4880             :     {
    4881        1818 :         for (i = 0; i < NLocBuffer; i++)
    4882             :         {
    4883             :             uint32      buf_state;
    4884             : 
    4885        1800 :             bufHdr = GetLocalBufferDescriptor(i);
    4886        1800 :             if (BufTagMatchesRelFileLocator(&bufHdr->tag, &rel->rd_locator) &&
    4887         600 :                 ((buf_state = pg_atomic_read_u32(&bufHdr->state)) &
    4888             :                  (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
    4889             :             {
    4890             :                 ErrorContextCallback errcallback;
    4891             : 
    4892             :                 /* Setup error traceback support for ereport() */
    4893         600 :                 errcallback.callback = local_buffer_write_error_callback;
    4894         600 :                 errcallback.arg = bufHdr;
    4895         600 :                 errcallback.previous = error_context_stack;
    4896         600 :                 error_context_stack = &errcallback;
    4897             : 
    4898         600 :                 FlushLocalBuffer(bufHdr, srel);
    4899             : 
    4900             :                 /* Pop the error context stack */
    4901         600 :                 error_context_stack = errcallback.previous;
    4902             :             }
    4903             :         }
    4904             : 
    4905          18 :         return;
    4906             :     }
    4907             : 
    4908     3221006 :     for (i = 0; i < NBuffers; i++)
    4909             :     {
    4910             :         uint32      buf_state;
    4911             : 
    4912     3220736 :         bufHdr = GetBufferDescriptor(i);
    4913             : 
    4914             :         /*
    4915             :          * As in DropRelationBuffers, an unlocked precheck should be safe and
    4916             :          * saves some cycles.
    4917             :          */
    4918     3220736 :         if (!BufTagMatchesRelFileLocator(&bufHdr->tag, &rel->rd_locator))
    4919     3220268 :             continue;
    4920             : 
    4921             :         /* Make sure we can handle the pin */
    4922         468 :         ReservePrivateRefCountEntry();
    4923         468 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    4924             : 
    4925         468 :         buf_state = LockBufHdr(bufHdr);
    4926         468 :         if (BufTagMatchesRelFileLocator(&bufHdr->tag, &rel->rd_locator) &&
    4927         468 :             (buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
    4928             :         {
    4929         382 :             PinBuffer_Locked(bufHdr);
    4930         382 :             LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED);
    4931         382 :             FlushBuffer(bufHdr, srel, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    4932         382 :             LWLockRelease(BufferDescriptorGetContentLock(bufHdr));
    4933         382 :             UnpinBuffer(bufHdr);
    4934             :         }
    4935             :         else
    4936          86 :             UnlockBufHdr(bufHdr, buf_state);
    4937             :     }
    4938             : }
    4939             : 
    4940             : /* ---------------------------------------------------------------------
    4941             :  *      FlushRelationsAllBuffers
    4942             :  *
    4943             :  *      This function flushes out of the buffer pool all the pages of all
    4944             :  *      forks of the specified smgr relations.  It's equivalent to calling
    4945             :  *      FlushRelationBuffers once per relation.  The relations are assumed not
    4946             :  *      to use local buffers.
    4947             :  * --------------------------------------------------------------------
    4948             :  */
    4949             : void
    4950          38 : FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels)
    4951             : {
    4952             :     int         i;
    4953             :     SMgrSortArray *srels;
    4954             :     bool        use_bsearch;
    4955             : 
    4956          38 :     if (nrels == 0)
    4957           0 :         return;
    4958             : 
    4959             :     /* fill-in array for qsort */
    4960          38 :     srels = palloc(sizeof(SMgrSortArray) * nrels);
    4961             : 
    4962          86 :     for (i = 0; i < nrels; i++)
    4963             :     {
    4964             :         Assert(!RelFileLocatorBackendIsTemp(smgrs[i]->smgr_rlocator));
    4965             : 
    4966          48 :         srels[i].rlocator = smgrs[i]->smgr_rlocator.locator;
    4967          48 :         srels[i].srel = smgrs[i];
    4968             :     }
    4969             : 
    4970             :     /*
    4971             :      * Save the bsearch overhead for low number of relations to sync. See
    4972             :      * DropRelationsAllBuffers for details.
    4973             :      */
    4974          38 :     use_bsearch = nrels > RELS_BSEARCH_THRESHOLD;
    4975             : 
    4976             :     /* sort the list of SMgrRelations if necessary */
    4977          38 :     if (use_bsearch)
    4978           0 :         qsort(srels, nrels, sizeof(SMgrSortArray), rlocator_comparator);
    4979             : 
    4980      622630 :     for (i = 0; i < NBuffers; i++)
    4981             :     {
    4982      622592 :         SMgrSortArray *srelent = NULL;
    4983      622592 :         BufferDesc *bufHdr = GetBufferDescriptor(i);
    4984             :         uint32      buf_state;
    4985             : 
    4986             :         /*
    4987             :          * As in DropRelationBuffers, an unlocked precheck should be safe and
    4988             :          * saves some cycles.
    4989             :          */
    4990             : 
    4991      622592 :         if (!use_bsearch)
    4992             :         {
    4993             :             int         j;
    4994             : 
    4995     1397834 :             for (j = 0; j < nrels; j++)
    4996             :             {
    4997      786424 :                 if (BufTagMatchesRelFileLocator(&bufHdr->tag, &srels[j].rlocator))
    4998             :                 {
    4999       11182 :                     srelent = &srels[j];
    5000       11182 :                     break;
    5001             :                 }
    5002             :             }
    5003             :         }
    5004             :         else
    5005             :         {
    5006             :             RelFileLocator rlocator;
    5007             : 
    5008           0 :             rlocator = BufTagGetRelFileLocator(&bufHdr->tag);
    5009           0 :             srelent = bsearch(&rlocator,
    5010             :                               srels, nrels, sizeof(SMgrSortArray),
    5011             :                               rlocator_comparator);
    5012             :         }
    5013             : 
    5014             :         /* buffer doesn't belong to any of the given relfilelocators; skip it */
    5015      622592 :         if (srelent == NULL)
    5016      611410 :             continue;
    5017             : 
    5018             :         /* Make sure we can handle the pin */
    5019       11182 :         ReservePrivateRefCountEntry();
    5020       11182 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    5021             : 
    5022       11182 :         buf_state = LockBufHdr(bufHdr);
    5023       11182 :         if (BufTagMatchesRelFileLocator(&bufHdr->tag, &srelent->rlocator) &&
    5024       11182 :             (buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
    5025             :         {
    5026       10000 :             PinBuffer_Locked(bufHdr);
    5027       10000 :             LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED);
    5028       10000 :             FlushBuffer(bufHdr, srelent->srel, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    5029       10000 :             LWLockRelease(BufferDescriptorGetContentLock(bufHdr));
    5030       10000 :             UnpinBuffer(bufHdr);
    5031             :         }
    5032             :         else
    5033        1182 :             UnlockBufHdr(bufHdr, buf_state);
    5034             :     }
    5035             : 
    5036          38 :     pfree(srels);
    5037             : }
    5038             : 
    5039             : /* ---------------------------------------------------------------------
    5040             :  *      RelationCopyStorageUsingBuffer
    5041             :  *
    5042             :  *      Copy fork's data using bufmgr.  Same as RelationCopyStorage but instead
    5043             :  *      of using smgrread and smgrextend this will copy using bufmgr APIs.
    5044             :  *
    5045             :  *      Refer comments atop CreateAndCopyRelationData() for details about
    5046             :  *      'permanent' parameter.
    5047             :  * --------------------------------------------------------------------
    5048             :  */
    5049             : static void
    5050      140704 : RelationCopyStorageUsingBuffer(RelFileLocator srclocator,
    5051             :                                RelFileLocator dstlocator,
    5052             :                                ForkNumber forkNum, bool permanent)
    5053             : {
    5054             :     Buffer      srcBuf;
    5055             :     Buffer      dstBuf;
    5056             :     Page        srcPage;
    5057             :     Page        dstPage;
    5058             :     bool        use_wal;
    5059             :     BlockNumber nblocks;
    5060             :     BlockNumber blkno;
    5061             :     PGIOAlignedBlock buf;
    5062             :     BufferAccessStrategy bstrategy_src;
    5063             :     BufferAccessStrategy bstrategy_dst;
    5064             :     BlockRangeReadStreamPrivate p;
    5065             :     ReadStream *src_stream;
    5066             :     SMgrRelation src_smgr;
    5067             : 
    5068             :     /*
    5069             :      * In general, we want to write WAL whenever wal_level > 'minimal', but we
    5070             :      * can skip it when copying any fork of an unlogged relation other than
    5071             :      * the init fork.
    5072             :      */
    5073      140704 :     use_wal = XLogIsNeeded() && (permanent || forkNum == INIT_FORKNUM);
    5074             : 
    5075             :     /* Get number of blocks in the source relation. */
    5076      140704 :     nblocks = smgrnblocks(smgropen(srclocator, INVALID_PROC_NUMBER),
    5077             :                           forkNum);
    5078             : 
    5079             :     /* Nothing to copy; just return. */
    5080      140704 :     if (nblocks == 0)
    5081       24542 :         return;
    5082             : 
    5083             :     /*
    5084             :      * Bulk extend the destination relation of the same size as the source
    5085             :      * relation before starting to copy block by block.
    5086             :      */
    5087      116162 :     memset(buf.data, 0, BLCKSZ);
    5088      116162 :     smgrextend(smgropen(dstlocator, INVALID_PROC_NUMBER), forkNum, nblocks - 1,
    5089             :                buf.data, true);
    5090             : 
    5091             :     /* This is a bulk operation, so use buffer access strategies. */
    5092      116162 :     bstrategy_src = GetAccessStrategy(BAS_BULKREAD);
    5093      116162 :     bstrategy_dst = GetAccessStrategy(BAS_BULKWRITE);
    5094             : 
    5095             :     /* Initialize streaming read */
    5096      116162 :     p.current_blocknum = 0;
    5097      116162 :     p.last_exclusive = nblocks;
    5098      116162 :     src_smgr = smgropen(srclocator, INVALID_PROC_NUMBER);
    5099             : 
    5100             :     /*
    5101             :      * It is safe to use batchmode as block_range_read_stream_cb takes no
    5102             :      * locks.
    5103             :      */
    5104      116162 :     src_stream = read_stream_begin_smgr_relation(READ_STREAM_FULL |
    5105             :                                                  READ_STREAM_USE_BATCHING,
    5106             :                                                  bstrategy_src,
    5107             :                                                  src_smgr,
    5108             :                                                  permanent ? RELPERSISTENCE_PERMANENT : RELPERSISTENCE_UNLOGGED,
    5109             :                                                  forkNum,
    5110             :                                                  block_range_read_stream_cb,
    5111             :                                                  &p,
    5112             :                                                  0);
    5113             : 
    5114             :     /* Iterate over each block of the source relation file. */
    5115      558186 :     for (blkno = 0; blkno < nblocks; blkno++)
    5116             :     {
    5117      442028 :         CHECK_FOR_INTERRUPTS();
    5118             : 
    5119             :         /* Read block from source relation. */
    5120      442028 :         srcBuf = read_stream_next_buffer(src_stream, NULL);
    5121      442024 :         LockBuffer(srcBuf, BUFFER_LOCK_SHARE);
    5122      442024 :         srcPage = BufferGetPage(srcBuf);
    5123             : 
    5124      442024 :         dstBuf = ReadBufferWithoutRelcache(dstlocator, forkNum,
    5125             :                                            BufferGetBlockNumber(srcBuf),
    5126             :                                            RBM_ZERO_AND_LOCK, bstrategy_dst,
    5127             :                                            permanent);
    5128      442024 :         dstPage = BufferGetPage(dstBuf);
    5129             : 
    5130      442024 :         START_CRIT_SECTION();
    5131             : 
    5132             :         /* Copy page data from the source to the destination. */
    5133      442024 :         memcpy(dstPage, srcPage, BLCKSZ);
    5134      442024 :         MarkBufferDirty(dstBuf);
    5135             : 
    5136             :         /* WAL-log the copied page. */
    5137      442024 :         if (use_wal)
    5138      241672 :             log_newpage_buffer(dstBuf, true);
    5139             : 
    5140      442024 :         END_CRIT_SECTION();
    5141             : 
    5142      442024 :         UnlockReleaseBuffer(dstBuf);
    5143      442024 :         UnlockReleaseBuffer(srcBuf);
    5144             :     }
    5145             :     Assert(read_stream_next_buffer(src_stream, NULL) == InvalidBuffer);
    5146      116158 :     read_stream_end(src_stream);
    5147             : 
    5148      116158 :     FreeAccessStrategy(bstrategy_src);
    5149      116158 :     FreeAccessStrategy(bstrategy_dst);
    5150             : }
    5151             : 
    5152             : /* ---------------------------------------------------------------------
    5153             :  *      CreateAndCopyRelationData
    5154             :  *
    5155             :  *      Create destination relation storage and copy all forks from the
    5156             :  *      source relation to the destination.
    5157             :  *
    5158             :  *      Pass permanent as true for permanent relations and false for
    5159             :  *      unlogged relations.  Currently this API is not supported for
    5160             :  *      temporary relations.
    5161             :  * --------------------------------------------------------------------
    5162             :  */
    5163             : void
    5164      105768 : CreateAndCopyRelationData(RelFileLocator src_rlocator,
    5165             :                           RelFileLocator dst_rlocator, bool permanent)
    5166             : {
    5167             :     char        relpersistence;
    5168             :     SMgrRelation src_rel;
    5169             :     SMgrRelation dst_rel;
    5170             : 
    5171             :     /* Set the relpersistence. */
    5172      105768 :     relpersistence = permanent ?
    5173             :         RELPERSISTENCE_PERMANENT : RELPERSISTENCE_UNLOGGED;
    5174             : 
    5175      105768 :     src_rel = smgropen(src_rlocator, INVALID_PROC_NUMBER);
    5176      105768 :     dst_rel = smgropen(dst_rlocator, INVALID_PROC_NUMBER);
    5177             : 
    5178             :     /*
    5179             :      * Create and copy all forks of the relation.  During create database we
    5180             :      * have a separate cleanup mechanism which deletes complete database
    5181             :      * directory.  Therefore, each individual relation doesn't need to be
    5182             :      * registered for cleanup.
    5183             :      */
    5184      105768 :     RelationCreateStorage(dst_rlocator, relpersistence, false);
    5185             : 
    5186             :     /* copy main fork. */
    5187      105768 :     RelationCopyStorageUsingBuffer(src_rlocator, dst_rlocator, MAIN_FORKNUM,
    5188             :                                    permanent);
    5189             : 
    5190             :     /* copy those extra forks that exist */
    5191      423056 :     for (ForkNumber forkNum = MAIN_FORKNUM + 1;
    5192      317292 :          forkNum <= MAX_FORKNUM; forkNum++)
    5193             :     {
    5194      317292 :         if (smgrexists(src_rel, forkNum))
    5195             :         {
    5196       34936 :             smgrcreate(dst_rel, forkNum, false);
    5197             : 
    5198             :             /*
    5199             :              * WAL log creation if the relation is persistent, or this is the
    5200             :              * init fork of an unlogged relation.
    5201             :              */
    5202       34936 :             if (permanent || forkNum == INIT_FORKNUM)
    5203       34936 :                 log_smgrcreate(&dst_rlocator, forkNum);
    5204             : 
    5205             :             /* Copy a fork's data, block by block. */
    5206       34936 :             RelationCopyStorageUsingBuffer(src_rlocator, dst_rlocator, forkNum,
    5207             :                                            permanent);
    5208             :         }
    5209             :     }
    5210      105764 : }
    5211             : 
    5212             : /* ---------------------------------------------------------------------
    5213             :  *      FlushDatabaseBuffers
    5214             :  *
    5215             :  *      This function writes all dirty pages of a database out to disk
    5216             :  *      (or more accurately, out to kernel disk buffers), ensuring that the
    5217             :  *      kernel has an up-to-date view of the database.
    5218             :  *
    5219             :  *      Generally, the caller should be holding an appropriate lock to ensure
    5220             :  *      no other backend is active in the target database; otherwise more
    5221             :  *      pages could get dirtied.
    5222             :  *
    5223             :  *      Note we don't worry about flushing any pages of temporary relations.
    5224             :  *      It's assumed these wouldn't be interesting.
    5225             :  * --------------------------------------------------------------------
    5226             :  */
    5227             : void
    5228           8 : FlushDatabaseBuffers(Oid dbid)
    5229             : {
    5230             :     int         i;
    5231             :     BufferDesc *bufHdr;
    5232             : 
    5233        1032 :     for (i = 0; i < NBuffers; i++)
    5234             :     {
    5235             :         uint32      buf_state;
    5236             : 
    5237        1024 :         bufHdr = GetBufferDescriptor(i);
    5238             : 
    5239             :         /*
    5240             :          * As in DropRelationBuffers, an unlocked precheck should be safe and
    5241             :          * saves some cycles.
    5242             :          */
    5243        1024 :         if (bufHdr->tag.dbOid != dbid)
    5244         706 :             continue;
    5245             : 
    5246             :         /* Make sure we can handle the pin */
    5247         318 :         ReservePrivateRefCountEntry();
    5248         318 :         ResourceOwnerEnlarge(CurrentResourceOwner);
    5249             : 
    5250         318 :         buf_state = LockBufHdr(bufHdr);
    5251         318 :         if (bufHdr->tag.dbOid == dbid &&
    5252         318 :             (buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
    5253             :         {
    5254         100 :             PinBuffer_Locked(bufHdr);
    5255         100 :             LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED);
    5256         100 :             FlushBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    5257         100 :             LWLockRelease(BufferDescriptorGetContentLock(bufHdr));
    5258         100 :             UnpinBuffer(bufHdr);
    5259             :         }
    5260             :         else
    5261         218 :             UnlockBufHdr(bufHdr, buf_state);
    5262             :     }
    5263           8 : }
    5264             : 
    5265             : /*
    5266             :  * Flush a previously, shared or exclusively, locked and pinned buffer to the
    5267             :  * OS.
    5268             :  */
    5269             : void
    5270         158 : FlushOneBuffer(Buffer buffer)
    5271             : {
    5272             :     BufferDesc *bufHdr;
    5273             : 
    5274             :     /* currently not needed, but no fundamental reason not to support */
    5275             :     Assert(!BufferIsLocal(buffer));
    5276             : 
    5277             :     Assert(BufferIsPinned(buffer));
    5278             : 
    5279         158 :     bufHdr = GetBufferDescriptor(buffer - 1);
    5280             : 
    5281             :     Assert(LWLockHeldByMe(BufferDescriptorGetContentLock(bufHdr)));
    5282             : 
    5283         158 :     FlushBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    5284         158 : }
    5285             : 
    5286             : /*
    5287             :  * ReleaseBuffer -- release the pin on a buffer
    5288             :  */
    5289             : void
    5290   119710410 : ReleaseBuffer(Buffer buffer)
    5291             : {
    5292   119710410 :     if (!BufferIsValid(buffer))
    5293           0 :         elog(ERROR, "bad buffer ID: %d", buffer);
    5294             : 
    5295   119710410 :     if (BufferIsLocal(buffer))
    5296     3211394 :         UnpinLocalBuffer(buffer);
    5297             :     else
    5298   116499016 :         UnpinBuffer(GetBufferDescriptor(buffer - 1));
    5299   119710410 : }
    5300             : 
    5301             : /*
    5302             :  * UnlockReleaseBuffer -- release the content lock and pin on a buffer
    5303             :  *
    5304             :  * This is just a shorthand for a common combination.
    5305             :  */
    5306             : void
    5307    37494288 : UnlockReleaseBuffer(Buffer buffer)
    5308             : {
    5309    37494288 :     LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
    5310    37494288 :     ReleaseBuffer(buffer);
    5311    37494288 : }
    5312             : 
    5313             : /*
    5314             :  * IncrBufferRefCount
    5315             :  *      Increment the pin count on a buffer that we have *already* pinned
    5316             :  *      at least once.
    5317             :  *
    5318             :  *      This function cannot be used on a buffer we do not have pinned,
    5319             :  *      because it doesn't change the shared buffer state.
    5320             :  */
    5321             : void
    5322    21894394 : IncrBufferRefCount(Buffer buffer)
    5323             : {
    5324             :     Assert(BufferIsPinned(buffer));
    5325    21894394 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    5326    21894394 :     if (BufferIsLocal(buffer))
    5327      709026 :         LocalRefCount[-buffer - 1]++;
    5328             :     else
    5329             :     {
    5330             :         PrivateRefCountEntry *ref;
    5331             : 
    5332    21185368 :         ref = GetPrivateRefCountEntry(buffer, true);
    5333             :         Assert(ref != NULL);
    5334    21185368 :         ref->refcount++;
    5335             :     }
    5336    21894394 :     ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer);
    5337    21894394 : }
    5338             : 
    5339             : /*
    5340             :  * MarkBufferDirtyHint
    5341             :  *
    5342             :  *  Mark a buffer dirty for non-critical changes.
    5343             :  *
    5344             :  * This is essentially the same as MarkBufferDirty, except:
    5345             :  *
    5346             :  * 1. The caller does not write WAL; so if checksums are enabled, we may need
    5347             :  *    to write an XLOG_FPI_FOR_HINT WAL record to protect against torn pages.
    5348             :  * 2. The caller might have only share-lock instead of exclusive-lock on the
    5349             :  *    buffer's content lock.
    5350             :  * 3. This function does not guarantee that the buffer is always marked dirty
    5351             :  *    (due to a race condition), so it cannot be used for important changes.
    5352             :  */
    5353             : void
    5354    20638838 : MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
    5355             : {
    5356             :     BufferDesc *bufHdr;
    5357    20638838 :     Page        page = BufferGetPage(buffer);
    5358             : 
    5359    20638838 :     if (!BufferIsValid(buffer))
    5360           0 :         elog(ERROR, "bad buffer ID: %d", buffer);
    5361             : 
    5362    20638838 :     if (BufferIsLocal(buffer))
    5363             :     {
    5364     1264340 :         MarkLocalBufferDirty(buffer);
    5365     1264340 :         return;
    5366             :     }
    5367             : 
    5368    19374498 :     bufHdr = GetBufferDescriptor(buffer - 1);
    5369             : 
    5370             :     Assert(GetPrivateRefCount(buffer) > 0);
    5371             :     /* here, either share or exclusive lock is OK */
    5372             :     Assert(LWLockHeldByMe(BufferDescriptorGetContentLock(bufHdr)));
    5373             : 
    5374             :     /*
    5375             :      * This routine might get called many times on the same page, if we are
    5376             :      * making the first scan after commit of an xact that added/deleted many
    5377             :      * tuples. So, be as quick as we can if the buffer is already dirty.  We
    5378             :      * do this by not acquiring spinlock if it looks like the status bits are
    5379             :      * already set.  Since we make this test unlocked, there's a chance we
    5380             :      * might fail to notice that the flags have just been cleared, and failed
    5381             :      * to reset them, due to memory-ordering issues.  But since this function
    5382             :      * is only intended to be used in cases where failing to write out the
    5383             :      * data would be harmless anyway, it doesn't really matter.
    5384             :      */
    5385    19374498 :     if ((pg_atomic_read_u32(&bufHdr->state) & (BM_DIRTY | BM_JUST_DIRTIED)) !=
    5386             :         (BM_DIRTY | BM_JUST_DIRTIED))
    5387             :     {
    5388     1823400 :         XLogRecPtr  lsn = InvalidXLogRecPtr;
    5389     1823400 :         bool        dirtied = false;
    5390     1823400 :         bool        delayChkptFlags = false;
    5391             :         uint32      buf_state;
    5392             : 
    5393             :         /*
    5394             :          * If we need to protect hint bit updates from torn writes, WAL-log a
    5395             :          * full page image of the page. This full page image is only necessary
    5396             :          * if the hint bit update is the first change to the page since the
    5397             :          * last checkpoint.
    5398             :          *
    5399             :          * We don't check full_page_writes here because that logic is included
    5400             :          * when we call XLogInsert() since the value changes dynamically.
    5401             :          */
    5402     3645010 :         if (XLogHintBitIsNeeded() &&
    5403     1821610 :             (pg_atomic_read_u32(&bufHdr->state) & BM_PERMANENT))
    5404             :         {
    5405             :             /*
    5406             :              * If we must not write WAL, due to a relfilelocator-specific
    5407             :              * condition or being in recovery, don't dirty the page.  We can
    5408             :              * set the hint, just not dirty the page as a result so the hint
    5409             :              * is lost when we evict the page or shutdown.
    5410             :              *
    5411             :              * See src/backend/storage/page/README for longer discussion.
    5412             :              */
    5413     1942152 :             if (RecoveryInProgress() ||
    5414      120604 :                 RelFileLocatorSkippingWAL(BufTagGetRelFileLocator(&bufHdr->tag)))
    5415     1706756 :                 return;
    5416             : 
    5417             :             /*
    5418             :              * If the block is already dirty because we either made a change
    5419             :              * or set a hint already, then we don't need to write a full page
    5420             :              * image.  Note that aggressive cleaning of blocks dirtied by hint
    5421             :              * bit setting would increase the call rate. Bulk setting of hint
    5422             :              * bits would reduce the call rate...
    5423             :              *
    5424             :              * We must issue the WAL record before we mark the buffer dirty.
    5425             :              * Otherwise we might write the page before we write the WAL. That
    5426             :              * causes a race condition, since a checkpoint might occur between
    5427             :              * writing the WAL record and marking the buffer dirty. We solve
    5428             :              * that with a kluge, but one that is already in use during
    5429             :              * transaction commit to prevent race conditions. Basically, we
    5430             :              * simply prevent the checkpoint WAL record from being written
    5431             :              * until we have marked the buffer dirty. We don't start the
    5432             :              * checkpoint flush until we have marked dirty, so our checkpoint
    5433             :              * must flush the change to disk successfully or the checkpoint
    5434             :              * never gets written, so crash recovery will fix.
    5435             :              *
    5436             :              * It's possible we may enter here without an xid, so it is
    5437             :              * essential that CreateCheckPoint waits for virtual transactions
    5438             :              * rather than full transactionids.
    5439             :              */
    5440             :             Assert((MyProc->delayChkptFlags & DELAY_CHKPT_START) == 0);
    5441      114792 :             MyProc->delayChkptFlags |= DELAY_CHKPT_START;
    5442      114792 :             delayChkptFlags = true;
    5443      114792 :             lsn = XLogSaveBufferForHint(buffer, buffer_std);
    5444             :         }
    5445             : 
    5446      116644 :         buf_state = LockBufHdr(bufHdr);
    5447             : 
    5448             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    5449             : 
    5450      116644 :         if (!(buf_state & BM_DIRTY))
    5451             :         {
    5452      116598 :             dirtied = true;     /* Means "will be dirtied by this action" */
    5453             : 
    5454             :             /*
    5455             :              * Set the page LSN if we wrote a backup block. We aren't supposed
    5456             :              * to set this when only holding a share lock but as long as we
    5457             :              * serialise it somehow we're OK. We choose to set LSN while
    5458             :              * holding the buffer header lock, which causes any reader of an
    5459             :              * LSN who holds only a share lock to also obtain a buffer header
    5460             :              * lock before using PageGetLSN(), which is enforced in
    5461             :              * BufferGetLSNAtomic().
    5462             :              *
    5463             :              * If checksums are enabled, you might think we should reset the
    5464             :              * checksum here. That will happen when the page is written
    5465             :              * sometime later in this checkpoint cycle.
    5466             :              */
    5467      116598 :             if (!XLogRecPtrIsInvalid(lsn))
    5468       62284 :                 PageSetLSN(page, lsn);
    5469             :         }
    5470             : 
    5471      116644 :         buf_state |= BM_DIRTY | BM_JUST_DIRTIED;
    5472      116644 :         UnlockBufHdr(bufHdr, buf_state);
    5473             : 
    5474      116644 :         if (delayChkptFlags)
    5475      114792 :             MyProc->delayChkptFlags &= ~DELAY_CHKPT_START;
    5476             : 
    5477      116644 :         if (dirtied)
    5478             :         {
    5479      116598 :             pgBufferUsage.shared_blks_dirtied++;
    5480      116598 :             if (VacuumCostActive)
    5481        2388 :                 VacuumCostBalance += VacuumCostPageDirty;
    5482             :         }
    5483             :     }
    5484             : }
    5485             : 
    5486             : /*
    5487             :  * Release buffer content locks for shared buffers.
    5488             :  *
    5489             :  * Used to clean up after errors.
    5490             :  *
    5491             :  * Currently, we can expect that lwlock.c's LWLockReleaseAll() took care
    5492             :  * of releasing buffer content locks per se; the only thing we need to deal
    5493             :  * with here is clearing any PIN_COUNT request that was in progress.
    5494             :  */
    5495             : void
    5496      100598 : UnlockBuffers(void)
    5497             : {
    5498      100598 :     BufferDesc *buf = PinCountWaitBuf;
    5499             : 
    5500      100598 :     if (buf)
    5501             :     {
    5502             :         uint32      buf_state;
    5503             : 
    5504           0 :         buf_state = LockBufHdr(buf);
    5505             : 
    5506             :         /*
    5507             :          * Don't complain if flag bit not set; it could have been reset but we
    5508             :          * got a cancel/die interrupt before getting the signal.
    5509             :          */
    5510           0 :         if ((buf_state & BM_PIN_COUNT_WAITER) != 0 &&
    5511           0 :             buf->wait_backend_pgprocno == MyProcNumber)
    5512           0 :             buf_state &= ~BM_PIN_COUNT_WAITER;
    5513             : 
    5514           0 :         UnlockBufHdr(buf, buf_state);
    5515             : 
    5516           0 :         PinCountWaitBuf = NULL;
    5517             :     }
    5518      100598 : }
    5519             : 
    5520             : /*
    5521             :  * Acquire or release the content_lock for the buffer.
    5522             :  */
    5523             : void
    5524   337208610 : LockBuffer(Buffer buffer, int mode)
    5525             : {
    5526             :     BufferDesc *buf;
    5527             : 
    5528             :     Assert(BufferIsPinned(buffer));
    5529   337208610 :     if (BufferIsLocal(buffer))
    5530    19770838 :         return;                 /* local buffers need no lock */
    5531             : 
    5532   317437772 :     buf = GetBufferDescriptor(buffer - 1);
    5533             : 
    5534   317437772 :     if (mode == BUFFER_LOCK_UNLOCK)
    5535   160637730 :         LWLockRelease(BufferDescriptorGetContentLock(buf));
    5536   156800042 :     else if (mode == BUFFER_LOCK_SHARE)
    5537   110781228 :         LWLockAcquire(BufferDescriptorGetContentLock(buf), LW_SHARED);
    5538    46018814 :     else if (mode == BUFFER_LOCK_EXCLUSIVE)
    5539    46018814 :         LWLockAcquire(BufferDescriptorGetContentLock(buf), LW_EXCLUSIVE);
    5540             :     else
    5541           0 :         elog(ERROR, "unrecognized buffer lock mode: %d", mode);
    5542             : }
    5543             : 
    5544             : /*
    5545             :  * Acquire the content_lock for the buffer, but only if we don't have to wait.
    5546             :  *
    5547             :  * This assumes the caller wants BUFFER_LOCK_EXCLUSIVE mode.
    5548             :  */
    5549             : bool
    5550     3057016 : ConditionalLockBuffer(Buffer buffer)
    5551             : {
    5552             :     BufferDesc *buf;
    5553             : 
    5554             :     Assert(BufferIsPinned(buffer));
    5555     3057016 :     if (BufferIsLocal(buffer))
    5556      129548 :         return true;            /* act as though we got it */
    5557             : 
    5558     2927468 :     buf = GetBufferDescriptor(buffer - 1);
    5559             : 
    5560     2927468 :     return LWLockConditionalAcquire(BufferDescriptorGetContentLock(buf),
    5561             :                                     LW_EXCLUSIVE);
    5562             : }
    5563             : 
    5564             : /*
    5565             :  * Verify that this backend is pinning the buffer exactly once.
    5566             :  *
    5567             :  * NOTE: Like in BufferIsPinned(), what we check here is that *this* backend
    5568             :  * holds a pin on the buffer.  We do not care whether some other backend does.
    5569             :  */
    5570             : void
    5571     4534216 : CheckBufferIsPinnedOnce(Buffer buffer)
    5572             : {
    5573     4534216 :     if (BufferIsLocal(buffer))
    5574             :     {
    5575        1578 :         if (LocalRefCount[-buffer - 1] != 1)
    5576           0 :             elog(ERROR, "incorrect local pin count: %d",
    5577             :                  LocalRefCount[-buffer - 1]);
    5578             :     }
    5579             :     else
    5580             :     {
    5581     4532638 :         if (GetPrivateRefCount(buffer) != 1)
    5582           0 :             elog(ERROR, "incorrect local pin count: %d",
    5583             :                  GetPrivateRefCount(buffer));
    5584             :     }
    5585     4534216 : }
    5586             : 
    5587             : /*
    5588             :  * LockBufferForCleanup - lock a buffer in preparation for deleting items
    5589             :  *
    5590             :  * Items may be deleted from a disk page only when the caller (a) holds an
    5591             :  * exclusive lock on the buffer and (b) has observed that no other backend
    5592             :  * holds a pin on the buffer.  If there is a pin, then the other backend
    5593             :  * might have a pointer into the buffer (for example, a heapscan reference
    5594             :  * to an item --- see README for more details).  It's OK if a pin is added
    5595             :  * after the cleanup starts, however; the newly-arrived backend will be
    5596             :  * unable to look at the page until we release the exclusive lock.
    5597             :  *
    5598             :  * To implement this protocol, a would-be deleter must pin the buffer and
    5599             :  * then call LockBufferForCleanup().  LockBufferForCleanup() is similar to
    5600             :  * LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE), except that it loops until
    5601             :  * it has successfully observed pin count = 1.
    5602             :  */
    5603             : void
    5604       41388 : LockBufferForCleanup(Buffer buffer)
    5605             : {
    5606             :     BufferDesc *bufHdr;
    5607       41388 :     TimestampTz waitStart = 0;
    5608       41388 :     bool        waiting = false;
    5609       41388 :     bool        logged_recovery_conflict = false;
    5610             : 
    5611             :     Assert(BufferIsPinned(buffer));
    5612             :     Assert(PinCountWaitBuf == NULL);
    5613             : 
    5614       41388 :     CheckBufferIsPinnedOnce(buffer);
    5615             : 
    5616             :     /*
    5617             :      * We do not yet need to be worried about in-progress AIOs holding a pin,
    5618             :      * as we, so far, only support doing reads via AIO and this function can
    5619             :      * only be called once the buffer is valid (i.e. no read can be in
    5620             :      * flight).
    5621             :      */
    5622             : 
    5623             :     /* Nobody else to wait for */
    5624       41388 :     if (BufferIsLocal(buffer))
    5625          32 :         return;
    5626             : 
    5627       41356 :     bufHdr = GetBufferDescriptor(buffer - 1);
    5628             : 
    5629             :     for (;;)
    5630          20 :     {
    5631             :         uint32      buf_state;
    5632             : 
    5633             :         /* Try to acquire lock */
    5634       41376 :         LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
    5635       41376 :         buf_state = LockBufHdr(bufHdr);
    5636             : 
    5637             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    5638       41376 :         if (BUF_STATE_GET_REFCOUNT(buf_state) == 1)
    5639             :         {
    5640             :             /* Successfully acquired exclusive lock with pincount 1 */
    5641       41356 :             UnlockBufHdr(bufHdr, buf_state);
    5642             : 
    5643             :             /*
    5644             :              * Emit the log message if recovery conflict on buffer pin was
    5645             :              * resolved but the startup process waited longer than
    5646             :              * deadlock_timeout for it.
    5647             :              */
    5648       41356 :             if (logged_recovery_conflict)
    5649           4 :                 LogRecoveryConflict(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN,
    5650             :                                     waitStart, GetCurrentTimestamp(),
    5651             :                                     NULL, false);
    5652             : 
    5653       41356 :             if (waiting)
    5654             :             {
    5655             :                 /* reset ps display to remove the suffix if we added one */
    5656           4 :                 set_ps_display_remove_suffix();
    5657           4 :                 waiting = false;
    5658             :             }
    5659       41356 :             return;
    5660             :         }
    5661             :         /* Failed, so mark myself as waiting for pincount 1 */
    5662          20 :         if (buf_state & BM_PIN_COUNT_WAITER)
    5663             :         {
    5664           0 :             UnlockBufHdr(bufHdr, buf_state);
    5665           0 :             LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
    5666           0 :             elog(ERROR, "multiple backends attempting to wait for pincount 1");
    5667             :         }
    5668          20 :         bufHdr->wait_backend_pgprocno = MyProcNumber;
    5669          20 :         PinCountWaitBuf = bufHdr;
    5670          20 :         buf_state |= BM_PIN_COUNT_WAITER;
    5671          20 :         UnlockBufHdr(bufHdr, buf_state);
    5672          20 :         LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
    5673             : 
    5674             :         /* Wait to be signaled by UnpinBuffer() */
    5675          20 :         if (InHotStandby)
    5676             :         {
    5677          20 :             if (!waiting)
    5678             :             {
    5679             :                 /* adjust the process title to indicate that it's waiting */
    5680           4 :                 set_ps_display_suffix("waiting");
    5681           4 :                 waiting = true;
    5682             :             }
    5683             : 
    5684             :             /*
    5685             :              * Emit the log message if the startup process is waiting longer
    5686             :              * than deadlock_timeout for recovery conflict on buffer pin.
    5687             :              *
    5688             :              * Skip this if first time through because the startup process has
    5689             :              * not started waiting yet in this case. So, the wait start
    5690             :              * timestamp is set after this logic.
    5691             :              */
    5692          20 :             if (waitStart != 0 && !logged_recovery_conflict)
    5693             :             {
    5694           6 :                 TimestampTz now = GetCurrentTimestamp();
    5695             : 
    5696           6 :                 if (TimestampDifferenceExceeds(waitStart, now,
    5697             :                                                DeadlockTimeout))
    5698             :                 {
    5699           4 :                     LogRecoveryConflict(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN,
    5700             :                                         waitStart, now, NULL, true);
    5701           4 :                     logged_recovery_conflict = true;
    5702             :                 }
    5703             :             }
    5704             : 
    5705             :             /*
    5706             :              * Set the wait start timestamp if logging is enabled and first
    5707             :              * time through.
    5708             :              */
    5709          20 :             if (log_recovery_conflict_waits && waitStart == 0)
    5710           4 :                 waitStart = GetCurrentTimestamp();
    5711             : 
    5712             :             /* Publish the bufid that Startup process waits on */
    5713          20 :             SetStartupBufferPinWaitBufId(buffer - 1);
    5714             :             /* Set alarm and then wait to be signaled by UnpinBuffer() */
    5715          20 :             ResolveRecoveryConflictWithBufferPin();
    5716             :             /* Reset the published bufid */
    5717          20 :             SetStartupBufferPinWaitBufId(-1);
    5718             :         }
    5719             :         else
    5720           0 :             ProcWaitForSignal(WAIT_EVENT_BUFFER_PIN);
    5721             : 
    5722             :         /*
    5723             :          * Remove flag marking us as waiter. Normally this will not be set
    5724             :          * anymore, but ProcWaitForSignal() can return for other signals as
    5725             :          * well.  We take care to only reset the flag if we're the waiter, as
    5726             :          * theoretically another backend could have started waiting. That's
    5727             :          * impossible with the current usages due to table level locking, but
    5728             :          * better be safe.
    5729             :          */
    5730          20 :         buf_state = LockBufHdr(bufHdr);
    5731          20 :         if ((buf_state & BM_PIN_COUNT_WAITER) != 0 &&
    5732          16 :             bufHdr->wait_backend_pgprocno == MyProcNumber)
    5733          16 :             buf_state &= ~BM_PIN_COUNT_WAITER;
    5734          20 :         UnlockBufHdr(bufHdr, buf_state);
    5735             : 
    5736          20 :         PinCountWaitBuf = NULL;
    5737             :         /* Loop back and try again */
    5738             :     }
    5739             : }
    5740             : 
    5741             : /*
    5742             :  * Check called from ProcessRecoveryConflictInterrupts() when Startup process
    5743             :  * requests cancellation of all pin holders that are blocking it.
    5744             :  */
    5745             : bool
    5746           8 : HoldingBufferPinThatDelaysRecovery(void)
    5747             : {
    5748           8 :     int         bufid = GetStartupBufferPinWaitBufId();
    5749             : 
    5750             :     /*
    5751             :      * If we get woken slowly then it's possible that the Startup process was
    5752             :      * already woken by other backends before we got here. Also possible that
    5753             :      * we get here by multiple interrupts or interrupts at inappropriate
    5754             :      * times, so make sure we do nothing if the bufid is not set.
    5755             :      */
    5756           8 :     if (bufid < 0)
    5757           4 :         return false;
    5758             : 
    5759           4 :     if (GetPrivateRefCount(bufid + 1) > 0)
    5760           4 :         return true;
    5761             : 
    5762           0 :     return false;
    5763             : }
    5764             : 
    5765             : /*
    5766             :  * ConditionalLockBufferForCleanup - as above, but don't wait to get the lock
    5767             :  *
    5768             :  * We won't loop, but just check once to see if the pin count is OK.  If
    5769             :  * not, return false with no lock held.
    5770             :  */
    5771             : bool
    5772      863526 : ConditionalLockBufferForCleanup(Buffer buffer)
    5773             : {
    5774             :     BufferDesc *bufHdr;
    5775             :     uint32      buf_state,
    5776             :                 refcount;
    5777             : 
    5778             :     Assert(BufferIsValid(buffer));
    5779             : 
    5780             :     /* see AIO related comment in LockBufferForCleanup() */
    5781             : 
    5782      863526 :     if (BufferIsLocal(buffer))
    5783             :     {
    5784        1600 :         refcount = LocalRefCount[-buffer - 1];
    5785             :         /* There should be exactly one pin */
    5786             :         Assert(refcount > 0);
    5787        1600 :         if (refcount != 1)
    5788          42 :             return false;
    5789             :         /* Nobody else to wait for */
    5790        1558 :         return true;
    5791             :     }
    5792             : 
    5793             :     /* There should be exactly one local pin */
    5794      861926 :     refcount = GetPrivateRefCount(buffer);
    5795             :     Assert(refcount);
    5796      861926 :     if (refcount != 1)
    5797         382 :         return false;
    5798             : 
    5799             :     /* Try to acquire lock */
    5800      861544 :     if (!ConditionalLockBuffer(buffer))
    5801          58 :         return false;
    5802             : 
    5803      861486 :     bufHdr = GetBufferDescriptor(buffer - 1);
    5804      861486 :     buf_state = LockBufHdr(bufHdr);
    5805      861486 :     refcount = BUF_STATE_GET_REFCOUNT(buf_state);
    5806             : 
    5807             :     Assert(refcount > 0);
    5808      861486 :     if (refcount == 1)
    5809             :     {
    5810             :         /* Successfully acquired exclusive lock with pincount 1 */
    5811      861264 :         UnlockBufHdr(bufHdr, buf_state);
    5812      861264 :         return true;
    5813             :     }
    5814             : 
    5815             :     /* Failed, so release the lock */
    5816         222 :     UnlockBufHdr(bufHdr, buf_state);
    5817         222 :     LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
    5818         222 :     return false;
    5819             : }
    5820             : 
    5821             : /*
    5822             :  * IsBufferCleanupOK - as above, but we already have the lock
    5823             :  *
    5824             :  * Check whether it's OK to perform cleanup on a buffer we've already
    5825             :  * locked.  If we observe that the pin count is 1, our exclusive lock
    5826             :  * happens to be a cleanup lock, and we can proceed with anything that
    5827             :  * would have been allowable had we sought a cleanup lock originally.
    5828             :  */
    5829             : bool
    5830        4062 : IsBufferCleanupOK(Buffer buffer)
    5831             : {
    5832             :     BufferDesc *bufHdr;
    5833             :     uint32      buf_state;
    5834             : 
    5835             :     Assert(BufferIsValid(buffer));
    5836             : 
    5837             :     /* see AIO related comment in LockBufferForCleanup() */
    5838             : 
    5839        4062 :     if (BufferIsLocal(buffer))
    5840             :     {
    5841             :         /* There should be exactly one pin */
    5842           0 :         if (LocalRefCount[-buffer - 1] != 1)
    5843           0 :             return false;
    5844             :         /* Nobody else to wait for */
    5845           0 :         return true;
    5846             :     }
    5847             : 
    5848             :     /* There should be exactly one local pin */
    5849        4062 :     if (GetPrivateRefCount(buffer) != 1)
    5850           0 :         return false;
    5851             : 
    5852        4062 :     bufHdr = GetBufferDescriptor(buffer - 1);
    5853             : 
    5854             :     /* caller must hold exclusive lock on buffer */
    5855             :     Assert(LWLockHeldByMeInMode(BufferDescriptorGetContentLock(bufHdr),
    5856             :                                 LW_EXCLUSIVE));
    5857             : 
    5858        4062 :     buf_state = LockBufHdr(bufHdr);
    5859             : 
    5860             :     Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    5861        4062 :     if (BUF_STATE_GET_REFCOUNT(buf_state) == 1)
    5862             :     {
    5863             :         /* pincount is OK. */
    5864        4062 :         UnlockBufHdr(bufHdr, buf_state);
    5865        4062 :         return true;
    5866             :     }
    5867             : 
    5868           0 :     UnlockBufHdr(bufHdr, buf_state);
    5869           0 :     return false;
    5870             : }
    5871             : 
    5872             : 
    5873             : /*
    5874             :  *  Functions for buffer I/O handling
    5875             :  *
    5876             :  *  Also note that these are used only for shared buffers, not local ones.
    5877             :  */
    5878             : 
    5879             : /*
    5880             :  * WaitIO -- Block until the IO_IN_PROGRESS flag on 'buf' is cleared.
    5881             :  */
    5882             : static void
    5883        4670 : WaitIO(BufferDesc *buf)
    5884             : {
    5885        4670 :     ConditionVariable *cv = BufferDescriptorGetIOCV(buf);
    5886             : 
    5887        4670 :     ConditionVariablePrepareToSleep(cv);
    5888             :     for (;;)
    5889        4642 :     {
    5890             :         uint32      buf_state;
    5891             :         PgAioWaitRef iow;
    5892             : 
    5893             :         /*
    5894             :          * It may not be necessary to acquire the spinlock to check the flag
    5895             :          * here, but since this test is essential for correctness, we'd better
    5896             :          * play it safe.
    5897             :          */
    5898        9312 :         buf_state = LockBufHdr(buf);
    5899             : 
    5900             :         /*
    5901             :          * Copy the wait reference while holding the spinlock. This protects
    5902             :          * against a concurrent TerminateBufferIO() in another backend from
    5903             :          * clearing the wref while it's being read.
    5904             :          */
    5905        9312 :         iow = buf->io_wref;
    5906        9312 :         UnlockBufHdr(buf, buf_state);
    5907             : 
    5908             :         /* no IO in progress, we don't need to wait */
    5909        9312 :         if (!(buf_state & BM_IO_IN_PROGRESS))
    5910        4670 :             break;
    5911             : 
    5912             :         /*
    5913             :          * The buffer has asynchronous IO in progress, wait for it to
    5914             :          * complete.
    5915             :          */
    5916        4642 :         if (pgaio_wref_valid(&iow))
    5917             :         {
    5918        4256 :             pgaio_wref_wait(&iow);
    5919             : 
    5920             :             /*
    5921             :              * The AIO subsystem internally uses condition variables and thus
    5922             :              * might remove this backend from the BufferDesc's CV. While that
    5923             :              * wouldn't cause a correctness issue (the first CV sleep just
    5924             :              * immediately returns if not already registered), it seems worth
    5925             :              * avoiding unnecessary loop iterations, given that we take care
    5926             :              * to do so at the start of the function.
    5927             :              */
    5928        4256 :             ConditionVariablePrepareToSleep(cv);
    5929        4256 :             continue;
    5930             :         }
    5931             : 
    5932             :         /* wait on BufferDesc->cv, e.g. for concurrent synchronous IO */
    5933         386 :         ConditionVariableSleep(cv, WAIT_EVENT_BUFFER_IO);
    5934             :     }
    5935        4670 :     ConditionVariableCancelSleep();
    5936        4670 : }
    5937             : 
    5938             : /*
    5939             :  * StartBufferIO: begin I/O on this buffer
    5940             :  *  (Assumptions)
    5941             :  *  My process is executing no IO on this buffer
    5942             :  *  The buffer is Pinned
    5943             :  *
    5944             :  * In some scenarios multiple backends could attempt the same I/O operation
    5945             :  * concurrently.  If someone else has already started I/O on this buffer then
    5946             :  * we will wait for completion of the IO using WaitIO().
    5947             :  *
    5948             :  * Input operations are only attempted on buffers that are not BM_VALID,
    5949             :  * and output operations only on buffers that are BM_VALID and BM_DIRTY,
    5950             :  * so we can always tell if the work is already done.
    5951             :  *
    5952             :  * Returns true if we successfully marked the buffer as I/O busy,
    5953             :  * false if someone else already did the work.
    5954             :  *
    5955             :  * If nowait is true, then we don't wait for an I/O to be finished by another
    5956             :  * backend.  In that case, false indicates either that the I/O was already
    5957             :  * finished, or is still in progress.  This is useful for callers that want to
    5958             :  * find out if they can perform the I/O as part of a larger operation, without
    5959             :  * waiting for the answer or distinguishing the reasons why not.
    5960             :  */
    5961             : bool
    5962     4761818 : StartBufferIO(BufferDesc *buf, bool forInput, bool nowait)
    5963             : {
    5964             :     uint32      buf_state;
    5965             : 
    5966     4761818 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    5967             : 
    5968             :     for (;;)
    5969             :     {
    5970     4766488 :         buf_state = LockBufHdr(buf);
    5971             : 
    5972     4766488 :         if (!(buf_state & BM_IO_IN_PROGRESS))
    5973     4761810 :             break;
    5974        4678 :         UnlockBufHdr(buf, buf_state);
    5975        4678 :         if (nowait)
    5976           8 :             return false;
    5977        4670 :         WaitIO(buf);
    5978             :     }
    5979             : 
    5980             :     /* Once we get here, there is definitely no I/O active on this buffer */
    5981             : 
    5982             :     /* Check if someone else already did the I/O */
    5983     4761810 :     if (forInput ? (buf_state & BM_VALID) : !(buf_state & BM_DIRTY))
    5984             :     {
    5985        5028 :         UnlockBufHdr(buf, buf_state);
    5986        5028 :         return false;
    5987             :     }
    5988             : 
    5989     4756782 :     buf_state |= BM_IO_IN_PROGRESS;
    5990     4756782 :     UnlockBufHdr(buf, buf_state);
    5991             : 
    5992     4756782 :     ResourceOwnerRememberBufferIO(CurrentResourceOwner,
    5993             :                                   BufferDescriptorGetBuffer(buf));
    5994             : 
    5995     4756782 :     return true;
    5996             : }
    5997             : 
    5998             : /*
    5999             :  * TerminateBufferIO: release a buffer we were doing I/O on
    6000             :  *  (Assumptions)
    6001             :  *  My process is executing IO for the buffer
    6002             :  *  BM_IO_IN_PROGRESS bit is set for the buffer
    6003             :  *  The buffer is Pinned
    6004             :  *
    6005             :  * If clear_dirty is true and BM_JUST_DIRTIED is not set, we clear the
    6006             :  * buffer's BM_DIRTY flag.  This is appropriate when terminating a
    6007             :  * successful write.  The check on BM_JUST_DIRTIED is necessary to avoid
    6008             :  * marking the buffer clean if it was re-dirtied while we were writing.
    6009             :  *
    6010             :  * set_flag_bits gets ORed into the buffer's flags.  It must include
    6011             :  * BM_IO_ERROR in a failure case.  For successful completion it could
    6012             :  * be 0, or BM_VALID if we just finished reading in the page.
    6013             :  *
    6014             :  * If forget_owner is true, we release the buffer I/O from the current
    6015             :  * resource owner. (forget_owner=false is used when the resource owner itself
    6016             :  * is being released)
    6017             :  */
    6018             : void
    6019     4475630 : TerminateBufferIO(BufferDesc *buf, bool clear_dirty, uint32 set_flag_bits,
    6020             :                   bool forget_owner, bool release_aio)
    6021             : {
    6022             :     uint32      buf_state;
    6023             : 
    6024     4475630 :     buf_state = LockBufHdr(buf);
    6025             : 
    6026             :     Assert(buf_state & BM_IO_IN_PROGRESS);
    6027     4475630 :     buf_state &= ~BM_IO_IN_PROGRESS;
    6028             : 
    6029             :     /* Clear earlier errors, if this IO failed, it'll be marked again */
    6030     4475630 :     buf_state &= ~BM_IO_ERROR;
    6031             : 
    6032     4475630 :     if (clear_dirty && !(buf_state & BM_JUST_DIRTIED))
    6033     1053730 :         buf_state &= ~(BM_DIRTY | BM_CHECKPOINT_NEEDED);
    6034             : 
    6035     4475630 :     if (release_aio)
    6036             :     {
    6037             :         /* release ownership by the AIO subsystem */
    6038             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
    6039     2440026 :         buf_state -= BUF_REFCOUNT_ONE;
    6040     2440026 :         pgaio_wref_clear(&buf->io_wref);
    6041             :     }
    6042             : 
    6043     4475630 :     buf_state |= set_flag_bits;
    6044     4475630 :     UnlockBufHdr(buf, buf_state);
    6045             : 
    6046     4475630 :     if (forget_owner)
    6047     2035562 :         ResourceOwnerForgetBufferIO(CurrentResourceOwner,
    6048             :                                     BufferDescriptorGetBuffer(buf));
    6049             : 
    6050     4475630 :     ConditionVariableBroadcast(BufferDescriptorGetIOCV(buf));
    6051             : 
    6052             :     /*
    6053             :      * Support LockBufferForCleanup()
    6054             :      *
    6055             :      * We may have just released the last pin other than the waiter's. In most
    6056             :      * cases, this backend holds another pin on the buffer. But, if, for
    6057             :      * example, this backend is completing an IO issued by another backend, it
    6058             :      * may be time to wake the waiter.
    6059             :      */
    6060     4475630 :     if (release_aio && (buf_state & BM_PIN_COUNT_WAITER))
    6061           0 :         WakePinCountWaiter(buf);
    6062     4475630 : }
    6063             : 
    6064             : /*
    6065             :  * AbortBufferIO: Clean up active buffer I/O after an error.
    6066             :  *
    6067             :  *  All LWLocks we might have held have been released,
    6068             :  *  but we haven't yet released buffer pins, so the buffer is still pinned.
    6069             :  *
    6070             :  *  If I/O was in progress, we always set BM_IO_ERROR, even though it's
    6071             :  *  possible the error condition wasn't related to the I/O.
    6072             :  *
    6073             :  *  Note: this does not remove the buffer I/O from the resource owner.
    6074             :  *  That's correct when we're releasing the whole resource owner, but
    6075             :  *  beware if you use this in other contexts.
    6076             :  */
    6077             : static void
    6078          30 : AbortBufferIO(Buffer buffer)
    6079             : {
    6080          30 :     BufferDesc *buf_hdr = GetBufferDescriptor(buffer - 1);
    6081             :     uint32      buf_state;
    6082             : 
    6083          30 :     buf_state = LockBufHdr(buf_hdr);
    6084             :     Assert(buf_state & (BM_IO_IN_PROGRESS | BM_TAG_VALID));
    6085             : 
    6086          30 :     if (!(buf_state & BM_VALID))
    6087             :     {
    6088             :         Assert(!(buf_state & BM_DIRTY));
    6089          30 :         UnlockBufHdr(buf_hdr, buf_state);
    6090             :     }
    6091             :     else
    6092             :     {
    6093             :         Assert(buf_state & BM_DIRTY);
    6094           0 :         UnlockBufHdr(buf_hdr, buf_state);
    6095             : 
    6096             :         /* Issue notice if this is not the first failure... */
    6097           0 :         if (buf_state & BM_IO_ERROR)
    6098             :         {
    6099             :             /* Buffer is pinned, so we can read tag without spinlock */
    6100           0 :             ereport(WARNING,
    6101             :                     (errcode(ERRCODE_IO_ERROR),
    6102             :                      errmsg("could not write block %u of %s",
    6103             :                             buf_hdr->tag.blockNum,
    6104             :                             relpathperm(BufTagGetRelFileLocator(&buf_hdr->tag),
    6105             :                                         BufTagGetForkNum(&buf_hdr->tag)).str),
    6106             :                      errdetail("Multiple failures --- write error might be permanent.")));
    6107             :         }
    6108             :     }
    6109             : 
    6110          30 :     TerminateBufferIO(buf_hdr, false, BM_IO_ERROR, false, false);
    6111          30 : }
    6112             : 
    6113             : /*
    6114             :  * Error context callback for errors occurring during shared buffer writes.
    6115             :  */
    6116             : static void
    6117          70 : shared_buffer_write_error_callback(void *arg)
    6118             : {
    6119          70 :     BufferDesc *bufHdr = (BufferDesc *) arg;
    6120             : 
    6121             :     /* Buffer is pinned, so we can read the tag without locking the spinlock */
    6122          70 :     if (bufHdr != NULL)
    6123         140 :         errcontext("writing block %u of relation %s",
    6124             :                    bufHdr->tag.blockNum,
    6125          70 :                    relpathperm(BufTagGetRelFileLocator(&bufHdr->tag),
    6126             :                                BufTagGetForkNum(&bufHdr->tag)).str);
    6127          70 : }
    6128             : 
    6129             : /*
    6130             :  * Error context callback for errors occurring during local buffer writes.
    6131             :  */
    6132             : static void
    6133           0 : local_buffer_write_error_callback(void *arg)
    6134             : {
    6135           0 :     BufferDesc *bufHdr = (BufferDesc *) arg;
    6136             : 
    6137           0 :     if (bufHdr != NULL)
    6138           0 :         errcontext("writing block %u of relation %s",
    6139             :                    bufHdr->tag.blockNum,
    6140           0 :                    relpathbackend(BufTagGetRelFileLocator(&bufHdr->tag),
    6141             :                                   MyProcNumber,
    6142             :                                   BufTagGetForkNum(&bufHdr->tag)).str);
    6143           0 : }
    6144             : 
    6145             : /*
    6146             :  * RelFileLocator qsort/bsearch comparator; see RelFileLocatorEquals.
    6147             :  */
    6148             : static int
    6149    18431232 : rlocator_comparator(const void *p1, const void *p2)
    6150             : {
    6151    18431232 :     RelFileLocator n1 = *(const RelFileLocator *) p1;
    6152    18431232 :     RelFileLocator n2 = *(const RelFileLocator *) p2;
    6153             : 
    6154    18431232 :     if (n1.relNumber < n2.relNumber)
    6155    18355048 :         return -1;
    6156       76184 :     else if (n1.relNumber > n2.relNumber)
    6157       72728 :         return 1;
    6158             : 
    6159        3456 :     if (n1.dbOid < n2.dbOid)
    6160           0 :         return -1;
    6161        3456 :     else if (n1.dbOid > n2.dbOid)
    6162           0 :         return 1;
    6163             : 
    6164        3456 :     if (n1.spcOid < n2.spcOid)
    6165           0 :         return -1;
    6166        3456 :     else if (n1.spcOid > n2.spcOid)
    6167           0 :         return 1;
    6168             :     else
    6169        3456 :         return 0;
    6170             : }
    6171             : 
    6172             : /*
    6173             :  * Lock buffer header - set BM_LOCKED in buffer state.
    6174             :  */
    6175             : uint32
    6176    73790178 : LockBufHdr(BufferDesc *desc)
    6177             : {
    6178             :     SpinDelayStatus delayStatus;
    6179             :     uint32      old_buf_state;
    6180             : 
    6181             :     Assert(!BufferIsLocal(BufferDescriptorGetBuffer(desc)));
    6182             : 
    6183    73790178 :     init_local_spin_delay(&delayStatus);
    6184             : 
    6185             :     while (true)
    6186             :     {
    6187             :         /* set BM_LOCKED flag */
    6188    73868658 :         old_buf_state = pg_atomic_fetch_or_u32(&desc->state, BM_LOCKED);
    6189             :         /* if it wasn't set before we're OK */
    6190    73868658 :         if (!(old_buf_state & BM_LOCKED))
    6191    73790178 :             break;
    6192       78480 :         perform_spin_delay(&delayStatus);
    6193             :     }
    6194    73790178 :     finish_spin_delay(&delayStatus);
    6195    73790178 :     return old_buf_state | BM_LOCKED;
    6196             : }
    6197             : 
    6198             : /*
    6199             :  * Wait until the BM_LOCKED flag isn't set anymore and return the buffer's
    6200             :  * state at that point.
    6201             :  *
    6202             :  * Obviously the buffer could be locked by the time the value is returned, so
    6203             :  * this is primarily useful in CAS style loops.
    6204             :  */
    6205             : static uint32
    6206        3816 : WaitBufHdrUnlocked(BufferDesc *buf)
    6207             : {
    6208             :     SpinDelayStatus delayStatus;
    6209             :     uint32      buf_state;
    6210             : 
    6211        3816 :     init_local_spin_delay(&delayStatus);
    6212             : 
    6213        3816 :     buf_state = pg_atomic_read_u32(&buf->state);
    6214             : 
    6215       29540 :     while (buf_state & BM_LOCKED)
    6216             :     {
    6217       25724 :         perform_spin_delay(&delayStatus);
    6218       25724 :         buf_state = pg_atomic_read_u32(&buf->state);
    6219             :     }
    6220             : 
    6221        3816 :     finish_spin_delay(&delayStatus);
    6222             : 
    6223        3816 :     return buf_state;
    6224             : }
    6225             : 
    6226             : /*
    6227             :  * BufferTag comparator.
    6228             :  */
    6229             : static inline int
    6230           0 : buffertag_comparator(const BufferTag *ba, const BufferTag *bb)
    6231             : {
    6232             :     int         ret;
    6233             :     RelFileLocator rlocatora;
    6234             :     RelFileLocator rlocatorb;
    6235             : 
    6236           0 :     rlocatora = BufTagGetRelFileLocator(ba);
    6237           0 :     rlocatorb = BufTagGetRelFileLocator(bb);
    6238             : 
    6239           0 :     ret = rlocator_comparator(&rlocatora, &rlocatorb);
    6240             : 
    6241           0 :     if (ret != 0)
    6242           0 :         return ret;
    6243             : 
    6244           0 :     if (BufTagGetForkNum(ba) < BufTagGetForkNum(bb))
    6245           0 :         return -1;
    6246           0 :     if (BufTagGetForkNum(ba) > BufTagGetForkNum(bb))
    6247           0 :         return 1;
    6248             : 
    6249           0 :     if (ba->blockNum < bb->blockNum)
    6250           0 :         return -1;
    6251           0 :     if (ba->blockNum > bb->blockNum)
    6252           0 :         return 1;
    6253             : 
    6254           0 :     return 0;
    6255             : }
    6256             : 
    6257             : /*
    6258             :  * Comparator determining the writeout order in a checkpoint.
    6259             :  *
    6260             :  * It is important that tablespaces are compared first, the logic balancing
    6261             :  * writes between tablespaces relies on it.
    6262             :  */
    6263             : static inline int
    6264     5670228 : ckpt_buforder_comparator(const CkptSortItem *a, const CkptSortItem *b)
    6265             : {
    6266             :     /* compare tablespace */
    6267     5670228 :     if (a->tsId < b->tsId)
    6268       11984 :         return -1;
    6269     5658244 :     else if (a->tsId > b->tsId)
    6270       36682 :         return 1;
    6271             :     /* compare relation */
    6272     5621562 :     if (a->relNumber < b->relNumber)
    6273     1579154 :         return -1;
    6274     4042408 :     else if (a->relNumber > b->relNumber)
    6275     1517432 :         return 1;
    6276             :     /* compare fork */
    6277     2524976 :     else if (a->forkNum < b->forkNum)
    6278      107546 :         return -1;
    6279     2417430 :     else if (a->forkNum > b->forkNum)
    6280      112556 :         return 1;
    6281             :     /* compare block number */
    6282     2304874 :     else if (a->blockNum < b->blockNum)
    6283     1128032 :         return -1;
    6284     1176842 :     else if (a->blockNum > b->blockNum)
    6285     1104134 :         return 1;
    6286             :     /* equal page IDs are unlikely, but not impossible */
    6287       72708 :     return 0;
    6288             : }
    6289             : 
    6290             : /*
    6291             :  * Comparator for a Min-Heap over the per-tablespace checkpoint completion
    6292             :  * progress.
    6293             :  */
    6294             : static int
    6295      437928 : ts_ckpt_progress_comparator(Datum a, Datum b, void *arg)
    6296             : {
    6297      437928 :     CkptTsStatus *sa = (CkptTsStatus *) a;
    6298      437928 :     CkptTsStatus *sb = (CkptTsStatus *) b;
    6299             : 
    6300             :     /* we want a min-heap, so return 1 for the a < b */
    6301      437928 :     if (sa->progress < sb->progress)
    6302      417174 :         return 1;
    6303       20754 :     else if (sa->progress == sb->progress)
    6304        1448 :         return 0;
    6305             :     else
    6306       19306 :         return -1;
    6307             : }
    6308             : 
    6309             : /*
    6310             :  * Initialize a writeback context, discarding potential previous state.
    6311             :  *
    6312             :  * *max_pending is a pointer instead of an immediate value, so the coalesce
    6313             :  * limits can easily changed by the GUC mechanism, and so calling code does
    6314             :  * not have to check the current configuration. A value of 0 means that no
    6315             :  * writeback control will be performed.
    6316             :  */
    6317             : void
    6318        5090 : WritebackContextInit(WritebackContext *context, int *max_pending)
    6319             : {
    6320             :     Assert(*max_pending <= WRITEBACK_MAX_PENDING_FLUSHES);
    6321             : 
    6322        5090 :     context->max_pending = max_pending;
    6323        5090 :     context->nr_pending = 0;
    6324        5090 : }
    6325             : 
    6326             : /*
    6327             :  * Add buffer to list of pending writeback requests.
    6328             :  */
    6329             : void
    6330     1043126 : ScheduleBufferTagForWriteback(WritebackContext *wb_context, IOContext io_context,
    6331             :                               BufferTag *tag)
    6332             : {
    6333             :     PendingWriteback *pending;
    6334             : 
    6335             :     /*
    6336             :      * As pg_flush_data() doesn't do anything with fsync disabled, there's no
    6337             :      * point in tracking in that case.
    6338             :      */
    6339     1043126 :     if (io_direct_flags & IO_DIRECT_DATA ||
    6340     1042072 :         !enableFsync)
    6341     1043124 :         return;
    6342             : 
    6343             :     /*
    6344             :      * Add buffer to the pending writeback array, unless writeback control is
    6345             :      * disabled.
    6346             :      */
    6347           2 :     if (*wb_context->max_pending > 0)
    6348             :     {
    6349             :         Assert(*wb_context->max_pending <= WRITEBACK_MAX_PENDING_FLUSHES);
    6350             : 
    6351           0 :         pending = &wb_context->pending_writebacks[wb_context->nr_pending++];
    6352             : 
    6353           0 :         pending->tag = *tag;
    6354             :     }
    6355             : 
    6356             :     /*
    6357             :      * Perform pending flushes if the writeback limit is exceeded. This
    6358             :      * includes the case where previously an item has been added, but control
    6359             :      * is now disabled.
    6360             :      */
    6361           2 :     if (wb_context->nr_pending >= *wb_context->max_pending)
    6362           2 :         IssuePendingWritebacks(wb_context, io_context);
    6363             : }
    6364             : 
    6365             : #define ST_SORT sort_pending_writebacks
    6366             : #define ST_ELEMENT_TYPE PendingWriteback
    6367             : #define ST_COMPARE(a, b) buffertag_comparator(&a->tag, &b->tag)
    6368             : #define ST_SCOPE static
    6369             : #define ST_DEFINE
    6370             : #include <lib/sort_template.h>
    6371             : 
    6372             : /*
    6373             :  * Issue all pending writeback requests, previously scheduled with
    6374             :  * ScheduleBufferTagForWriteback, to the OS.
    6375             :  *
    6376             :  * Because this is only used to improve the OSs IO scheduling we try to never
    6377             :  * error out - it's just a hint.
    6378             :  */
    6379             : void
    6380        2034 : IssuePendingWritebacks(WritebackContext *wb_context, IOContext io_context)
    6381             : {
    6382             :     instr_time  io_start;
    6383             :     int         i;
    6384             : 
    6385        2034 :     if (wb_context->nr_pending == 0)
    6386        2034 :         return;
    6387             : 
    6388             :     /*
    6389             :      * Executing the writes in-order can make them a lot faster, and allows to
    6390             :      * merge writeback requests to consecutive blocks into larger writebacks.
    6391             :      */
    6392           0 :     sort_pending_writebacks(wb_context->pending_writebacks,
    6393           0 :                             wb_context->nr_pending);
    6394             : 
    6395           0 :     io_start = pgstat_prepare_io_time(track_io_timing);
    6396             : 
    6397             :     /*
    6398             :      * Coalesce neighbouring writes, but nothing else. For that we iterate
    6399             :      * through the, now sorted, array of pending flushes, and look forward to
    6400             :      * find all neighbouring (or identical) writes.
    6401             :      */
    6402           0 :     for (i = 0; i < wb_context->nr_pending; i++)
    6403             :     {
    6404             :         PendingWriteback *cur;
    6405             :         PendingWriteback *next;
    6406             :         SMgrRelation reln;
    6407             :         int         ahead;
    6408             :         BufferTag   tag;
    6409             :         RelFileLocator currlocator;
    6410           0 :         Size        nblocks = 1;
    6411             : 
    6412           0 :         cur = &wb_context->pending_writebacks[i];
    6413           0 :         tag = cur->tag;
    6414           0 :         currlocator = BufTagGetRelFileLocator(&tag);
    6415             : 
    6416             :         /*
    6417             :          * Peek ahead, into following writeback requests, to see if they can
    6418             :          * be combined with the current one.
    6419             :          */
    6420           0 :         for (ahead = 0; i + ahead + 1 < wb_context->nr_pending; ahead++)
    6421             :         {
    6422             : 
    6423           0 :             next = &wb_context->pending_writebacks[i + ahead + 1];
    6424             : 
    6425             :             /* different file, stop */
    6426           0 :             if (!RelFileLocatorEquals(currlocator,
    6427           0 :                                       BufTagGetRelFileLocator(&next->tag)) ||
    6428           0 :                 BufTagGetForkNum(&cur->tag) != BufTagGetForkNum(&next->tag))
    6429             :                 break;
    6430             : 
    6431             :             /* ok, block queued twice, skip */
    6432           0 :             if (cur->tag.blockNum == next->tag.blockNum)
    6433           0 :                 continue;
    6434             : 
    6435             :             /* only merge consecutive writes */
    6436           0 :             if (cur->tag.blockNum + 1 != next->tag.blockNum)
    6437           0 :                 break;
    6438             : 
    6439           0 :             nblocks++;
    6440           0 :             cur = next;
    6441             :         }
    6442             : 
    6443           0 :         i += ahead;
    6444             : 
    6445             :         /* and finally tell the kernel to write the data to storage */
    6446           0 :         reln = smgropen(currlocator, INVALID_PROC_NUMBER);
    6447           0 :         smgrwriteback(reln, BufTagGetForkNum(&tag), tag.blockNum, nblocks);
    6448             :     }
    6449             : 
    6450             :     /*
    6451             :      * Assume that writeback requests are only issued for buffers containing
    6452             :      * blocks of permanent relations.
    6453             :      */
    6454           0 :     pgstat_count_io_op_time(IOOBJECT_RELATION, io_context,
    6455           0 :                             IOOP_WRITEBACK, io_start, wb_context->nr_pending, 0);
    6456             : 
    6457           0 :     wb_context->nr_pending = 0;
    6458             : }
    6459             : 
    6460             : /* ResourceOwner callbacks */
    6461             : 
    6462             : static void
    6463          30 : ResOwnerReleaseBufferIO(Datum res)
    6464             : {
    6465          30 :     Buffer      buffer = DatumGetInt32(res);
    6466             : 
    6467          30 :     AbortBufferIO(buffer);
    6468          30 : }
    6469             : 
    6470             : static char *
    6471           0 : ResOwnerPrintBufferIO(Datum res)
    6472             : {
    6473           0 :     Buffer      buffer = DatumGetInt32(res);
    6474             : 
    6475           0 :     return psprintf("lost track of buffer IO on buffer %d", buffer);
    6476             : }
    6477             : 
    6478             : static void
    6479       14504 : ResOwnerReleaseBufferPin(Datum res)
    6480             : {
    6481       14504 :     Buffer      buffer = DatumGetInt32(res);
    6482             : 
    6483             :     /* Like ReleaseBuffer, but don't call ResourceOwnerForgetBuffer */
    6484       14504 :     if (!BufferIsValid(buffer))
    6485           0 :         elog(ERROR, "bad buffer ID: %d", buffer);
    6486             : 
    6487       14504 :     if (BufferIsLocal(buffer))
    6488        5970 :         UnpinLocalBufferNoOwner(buffer);
    6489             :     else
    6490        8534 :         UnpinBufferNoOwner(GetBufferDescriptor(buffer - 1));
    6491       14504 : }
    6492             : 
    6493             : static char *
    6494           0 : ResOwnerPrintBufferPin(Datum res)
    6495             : {
    6496           0 :     return DebugPrintBufferRefcount(DatumGetInt32(res));
    6497             : }
    6498             : 
    6499             : /*
    6500             :  * Try to evict the current block in a shared buffer.
    6501             :  *
    6502             :  * This function is intended for testing/development use only!
    6503             :  *
    6504             :  * To succeed, the buffer must not be pinned on entry, so if the caller had a
    6505             :  * particular block in mind, it might already have been replaced by some other
    6506             :  * block by the time this function runs.  It's also unpinned on return, so the
    6507             :  * buffer might be occupied again by the time control is returned, potentially
    6508             :  * even by the same block.  This inherent raciness without other interlocking
    6509             :  * makes the function unsuitable for non-testing usage.
    6510             :  *
    6511             :  * Returns true if the buffer was valid and it has now been made invalid.
    6512             :  * Returns false if it wasn't valid, if it couldn't be evicted due to a pin,
    6513             :  * or if the buffer becomes dirty again while we're trying to write it out.
    6514             :  */
    6515             : bool
    6516         278 : EvictUnpinnedBuffer(Buffer buf)
    6517             : {
    6518             :     BufferDesc *desc;
    6519             :     uint32      buf_state;
    6520             :     bool        result;
    6521             : 
    6522             :     /* Make sure we can pin the buffer. */
    6523         278 :     ResourceOwnerEnlarge(CurrentResourceOwner);
    6524         278 :     ReservePrivateRefCountEntry();
    6525             : 
    6526             :     Assert(!BufferIsLocal(buf));
    6527         278 :     desc = GetBufferDescriptor(buf - 1);
    6528             : 
    6529             :     /* Lock the header and check if it's valid. */
    6530         278 :     buf_state = LockBufHdr(desc);
    6531         278 :     if ((buf_state & BM_VALID) == 0)
    6532             :     {
    6533           0 :         UnlockBufHdr(desc, buf_state);
    6534           0 :         return false;
    6535             :     }
    6536             : 
    6537             :     /* Check that it's not pinned already. */
    6538         278 :     if (BUF_STATE_GET_REFCOUNT(buf_state) > 0)
    6539             :     {
    6540           0 :         UnlockBufHdr(desc, buf_state);
    6541           0 :         return false;
    6542             :     }
    6543             : 
    6544         278 :     PinBuffer_Locked(desc);     /* releases spinlock */
    6545             : 
    6546             :     /* If it was dirty, try to clean it once. */
    6547         278 :     if (buf_state & BM_DIRTY)
    6548             :     {
    6549          36 :         LWLockAcquire(BufferDescriptorGetContentLock(desc), LW_SHARED);
    6550          36 :         FlushBuffer(desc, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
    6551          36 :         LWLockRelease(BufferDescriptorGetContentLock(desc));
    6552             :     }
    6553             : 
    6554             :     /* This will return false if it becomes dirty or someone else pins it. */
    6555         278 :     result = InvalidateVictimBuffer(desc);
    6556             : 
    6557         278 :     UnpinBuffer(desc);
    6558             : 
    6559         278 :     return result;
    6560             : }
    6561             : 
    6562             : /*
    6563             :  * Generic implementation of the AIO handle staging callback for readv/writev
    6564             :  * on local/shared buffers.
    6565             :  *
    6566             :  * Each readv/writev can target multiple buffers. The buffers have already
    6567             :  * been registered with the IO handle.
    6568             :  *
    6569             :  * To make the IO ready for execution ("staging"), we need to ensure that the
    6570             :  * targeted buffers are in an appropriate state while the IO is ongoing. For
    6571             :  * that the AIO subsystem needs to have its own buffer pin, otherwise an error
    6572             :  * in this backend could lead to this backend's buffer pin being released as
    6573             :  * part of error handling, which in turn could lead to the buffer being
    6574             :  * replaced while IO is ongoing.
    6575             :  */
    6576             : static pg_attribute_always_inline void
    6577     2409832 : buffer_stage_common(PgAioHandle *ioh, bool is_write, bool is_temp)
    6578             : {
    6579             :     uint64     *io_data;
    6580             :     uint8       handle_data_len;
    6581             :     PgAioWaitRef io_ref;
    6582     2409832 :     BufferTag   first PG_USED_FOR_ASSERTS_ONLY = {0};
    6583             : 
    6584     2409832 :     io_data = pgaio_io_get_handle_data(ioh, &handle_data_len);
    6585             : 
    6586     2409832 :     pgaio_io_get_wref(ioh, &io_ref);
    6587             : 
    6588             :     /* iterate over all buffers affected by the vectored readv/writev */
    6589     5147788 :     for (int i = 0; i < handle_data_len; i++)
    6590             :     {
    6591     2737956 :         Buffer      buffer = (Buffer) io_data[i];
    6592     2737956 :         BufferDesc *buf_hdr = is_temp ?
    6593       16778 :             GetLocalBufferDescriptor(-buffer - 1)
    6594     2737956 :             : GetBufferDescriptor(buffer - 1);
    6595             :         uint32      buf_state;
    6596             : 
    6597             :         /*
    6598             :          * Check that all the buffers are actually ones that could conceivably
    6599             :          * be done in one IO, i.e. are sequential. This is the last
    6600             :          * buffer-aware code before IO is actually executed and confusion
    6601             :          * about which buffers are targeted by IO can be hard to debug, making
    6602             :          * it worth doing extra-paranoid checks.
    6603             :          */
    6604     2737956 :         if (i == 0)
    6605     2409832 :             first = buf_hdr->tag;
    6606             :         else
    6607             :         {
    6608             :             Assert(buf_hdr->tag.relNumber == first.relNumber);
    6609             :             Assert(buf_hdr->tag.blockNum == first.blockNum + i);
    6610             :         }
    6611             : 
    6612     2737956 :         if (is_temp)
    6613       16778 :             buf_state = pg_atomic_read_u32(&buf_hdr->state);
    6614             :         else
    6615     2721178 :             buf_state = LockBufHdr(buf_hdr);
    6616             : 
    6617             :         /* verify the buffer is in the expected state */
    6618             :         Assert(buf_state & BM_TAG_VALID);
    6619             :         if (is_write)
    6620             :         {
    6621             :             Assert(buf_state & BM_VALID);
    6622             :             Assert(buf_state & BM_DIRTY);
    6623             :         }
    6624             :         else
    6625             :         {
    6626             :             Assert(!(buf_state & BM_VALID));
    6627             :             Assert(!(buf_state & BM_DIRTY));
    6628             :         }
    6629             : 
    6630             :         /* temp buffers don't use BM_IO_IN_PROGRESS */
    6631     2737956 :         if (!is_temp)
    6632             :             Assert(buf_state & BM_IO_IN_PROGRESS);
    6633             : 
    6634             :         Assert(BUF_STATE_GET_REFCOUNT(buf_state) >= 1);
    6635             : 
    6636             :         /*
    6637             :          * Reflect that the buffer is now owned by the AIO subsystem.
    6638             :          *
    6639             :          * For local buffers: This can't be done just via LocalRefCount, as
    6640             :          * one might initially think, as this backend could error out while
    6641             :          * AIO is still in progress, releasing all the pins by the backend
    6642             :          * itself.
    6643             :          *
    6644             :          * This pin is released again in TerminateBufferIO().
    6645             :          */
    6646     2737956 :         buf_state += BUF_REFCOUNT_ONE;
    6647     2737956 :         buf_hdr->io_wref = io_ref;
    6648             : 
    6649     2737956 :         if (is_temp)
    6650       16778 :             pg_atomic_unlocked_write_u32(&buf_hdr->state, buf_state);
    6651             :         else
    6652     2721178 :             UnlockBufHdr(buf_hdr, buf_state);
    6653             : 
    6654             :         /*
    6655             :          * Ensure the content lock that prevents buffer modifications while
    6656             :          * the buffer is being written out is not released early due to an
    6657             :          * error.
    6658             :          */
    6659     2737956 :         if (is_write && !is_temp)
    6660             :         {
    6661             :             LWLock     *content_lock;
    6662             : 
    6663           0 :             content_lock = BufferDescriptorGetContentLock(buf_hdr);
    6664             : 
    6665             :             Assert(LWLockHeldByMe(content_lock));
    6666             : 
    6667             :             /*
    6668             :              * Lock is now owned by AIO subsystem.
    6669             :              */
    6670           0 :             LWLockDisown(content_lock);
    6671             :         }
    6672             : 
    6673             :         /*
    6674             :          * Stop tracking this buffer via the resowner - the AIO system now
    6675             :          * keeps track.
    6676             :          */
    6677     2737956 :         if (!is_temp)
    6678     2721178 :             ResourceOwnerForgetBufferIO(CurrentResourceOwner, buffer);
    6679             :     }
    6680     2409832 : }
    6681             : 
    6682             : /*
    6683             :  * Decode readv errors as encoded by buffer_readv_encode_error().
    6684             :  */
    6685             : static inline void
    6686         698 : buffer_readv_decode_error(PgAioResult result,
    6687             :                           bool *zeroed_any,
    6688             :                           bool *ignored_any,
    6689             :                           uint8 *zeroed_or_error_count,
    6690             :                           uint8 *checkfail_count,
    6691             :                           uint8 *first_off)
    6692             : {
    6693         698 :     uint32      rem_error = result.error_data;
    6694             : 
    6695             :     /* see static asserts in buffer_readv_encode_error */
    6696             : #define READV_COUNT_BITS    7
    6697             : #define READV_COUNT_MASK    ((1 << READV_COUNT_BITS) - 1)
    6698             : 
    6699         698 :     *zeroed_any = rem_error & 1;
    6700         698 :     rem_error >>= 1;
    6701             : 
    6702         698 :     *ignored_any = rem_error & 1;
    6703         698 :     rem_error >>= 1;
    6704             : 
    6705         698 :     *zeroed_or_error_count = rem_error & READV_COUNT_MASK;
    6706         698 :     rem_error >>= READV_COUNT_BITS;
    6707             : 
    6708         698 :     *checkfail_count = rem_error & READV_COUNT_MASK;
    6709         698 :     rem_error >>= READV_COUNT_BITS;
    6710             : 
    6711         698 :     *first_off = rem_error & READV_COUNT_MASK;
    6712         698 :     rem_error >>= READV_COUNT_BITS;
    6713         698 : }
    6714             : 
    6715             : /*
    6716             :  * Helper to encode errors for buffer_readv_complete()
    6717             :  *
    6718             :  * Errors are encoded as follows:
    6719             :  * - bit 0 indicates whether any page was zeroed (1) or not (0)
    6720             :  * - bit 1 indicates whether any checksum failure was ignored (1) or not (0)
    6721             :  * - next READV_COUNT_BITS bits indicate the number of errored or zeroed pages
    6722             :  * - next READV_COUNT_BITS bits indicate the number of checksum failures
    6723             :  * - next READV_COUNT_BITS bits indicate the first offset of the first page
    6724             :  *   that was errored or zeroed or, if no errors/zeroes, the first ignored
    6725             :  *   checksum
    6726             :  */
    6727             : static inline void
    6728         384 : buffer_readv_encode_error(PgAioResult *result,
    6729             :                           bool is_temp,
    6730             :                           bool zeroed_any,
    6731             :                           bool ignored_any,
    6732             :                           uint8 error_count,
    6733             :                           uint8 zeroed_count,
    6734             :                           uint8 checkfail_count,
    6735             :                           uint8 first_error_off,
    6736             :                           uint8 first_zeroed_off,
    6737             :                           uint8 first_ignored_off)
    6738             : {
    6739             : 
    6740         384 :     uint8       shift = 0;
    6741         384 :     uint8       zeroed_or_error_count =
    6742             :         error_count > 0 ? error_count : zeroed_count;
    6743             :     uint8       first_off;
    6744             : 
    6745             :     StaticAssertStmt(PG_IOV_MAX <= 1 << READV_COUNT_BITS,
    6746             :                      "PG_IOV_MAX is bigger than reserved space for error data");
    6747             :     StaticAssertStmt((1 + 1 + 3 * READV_COUNT_BITS) <= PGAIO_RESULT_ERROR_BITS,
    6748             :                      "PGAIO_RESULT_ERROR_BITS is insufficient for buffer_readv");
    6749             : 
    6750             :     /*
    6751             :      * We only have space to encode one offset - but luckily that's good
    6752             :      * enough. If there is an error, the error is the interesting offset, same
    6753             :      * with a zeroed buffer vs an ignored buffer.
    6754             :      */
    6755         384 :     if (error_count > 0)
    6756         188 :         first_off = first_error_off;
    6757         196 :     else if (zeroed_count > 0)
    6758         160 :         first_off = first_zeroed_off;
    6759             :     else
    6760          36 :         first_off = first_ignored_off;
    6761             : 
    6762             :     Assert(!zeroed_any || error_count == 0);
    6763             : 
    6764         384 :     result->error_data = 0;
    6765             : 
    6766         384 :     result->error_data |= zeroed_any << shift;
    6767         384 :     shift += 1;
    6768             : 
    6769         384 :     result->error_data |= ignored_any << shift;
    6770         384 :     shift += 1;
    6771             : 
    6772         384 :     result->error_data |= ((uint32) zeroed_or_error_count) << shift;
    6773         384 :     shift += READV_COUNT_BITS;
    6774             : 
    6775         384 :     result->error_data |= ((uint32) checkfail_count) << shift;
    6776         384 :     shift += READV_COUNT_BITS;
    6777             : 
    6778         384 :     result->error_data |= ((uint32) first_off) << shift;
    6779         384 :     shift += READV_COUNT_BITS;
    6780             : 
    6781         384 :     result->id = is_temp ? PGAIO_HCB_LOCAL_BUFFER_READV :
    6782             :         PGAIO_HCB_SHARED_BUFFER_READV;
    6783             : 
    6784         384 :     if (error_count > 0)
    6785         188 :         result->status = PGAIO_RS_ERROR;
    6786             :     else
    6787         196 :         result->status = PGAIO_RS_WARNING;
    6788             : 
    6789             :     /*
    6790             :      * The encoding is complicated enough to warrant cross-checking it against
    6791             :      * the decode function.
    6792             :      */
    6793             : #ifdef USE_ASSERT_CHECKING
    6794             :     {
    6795             :         bool        zeroed_any_2,
    6796             :                     ignored_any_2;
    6797             :         uint8       zeroed_or_error_count_2,
    6798             :                     checkfail_count_2,
    6799             :                     first_off_2;
    6800             : 
    6801             :         buffer_readv_decode_error(*result,
    6802             :                                   &zeroed_any_2, &ignored_any_2,
    6803             :                                   &zeroed_or_error_count_2,
    6804             :                                   &checkfail_count_2,
    6805             :                                   &first_off_2);
    6806             :         Assert(zeroed_any == zeroed_any_2);
    6807             :         Assert(ignored_any == ignored_any_2);
    6808             :         Assert(zeroed_or_error_count == zeroed_or_error_count_2);
    6809             :         Assert(checkfail_count == checkfail_count_2);
    6810             :         Assert(first_off == first_off_2);
    6811             :     }
    6812             : #endif
    6813             : 
    6814             : #undef READV_COUNT_BITS
    6815             : #undef READV_COUNT_MASK
    6816         384 : }
    6817             : 
    6818             : /*
    6819             :  * Helper for AIO readv completion callbacks, supporting both shared and temp
    6820             :  * buffers. Gets called once for each buffer in a multi-page read.
    6821             :  */
    6822             : static pg_attribute_always_inline void
    6823     2456804 : buffer_readv_complete_one(PgAioTargetData *td, uint8 buf_off, Buffer buffer,
    6824             :                           uint8 flags, bool failed, bool is_temp,
    6825             :                           bool *buffer_invalid,
    6826             :                           bool *failed_checksum,
    6827             :                           bool *ignored_checksum,
    6828             :                           bool *zeroed_buffer)
    6829             : {
    6830     2456804 :     BufferDesc *buf_hdr = is_temp ?
    6831       16778 :         GetLocalBufferDescriptor(-buffer - 1)
    6832     2456804 :         : GetBufferDescriptor(buffer - 1);
    6833     2456804 :     BufferTag   tag = buf_hdr->tag;
    6834     2456804 :     char       *bufdata = BufferGetBlock(buffer);
    6835             :     uint32      set_flag_bits;
    6836             :     int         piv_flags;
    6837             : 
    6838             :     /* check that the buffer is in the expected state for a read */
    6839             : #ifdef USE_ASSERT_CHECKING
    6840             :     {
    6841             :         uint32      buf_state = pg_atomic_read_u32(&buf_hdr->state);
    6842             : 
    6843             :         Assert(buf_state & BM_TAG_VALID);
    6844             :         Assert(!(buf_state & BM_VALID));
    6845             :         /* temp buffers don't use BM_IO_IN_PROGRESS */
    6846             :         if (!is_temp)
    6847             :             Assert(buf_state & BM_IO_IN_PROGRESS);
    6848             :         Assert(!(buf_state & BM_DIRTY));
    6849             :     }
    6850             : #endif
    6851             : 
    6852     2456804 :     *buffer_invalid = false;
    6853     2456804 :     *failed_checksum = false;
    6854     2456804 :     *ignored_checksum = false;
    6855     2456804 :     *zeroed_buffer = false;
    6856             : 
    6857             :     /*
    6858             :      * We ask PageIsVerified() to only log the message about checksum errors,
    6859             :      * as the completion might be run in any backend (or IO workers). We will
    6860             :      * report checksum errors in buffer_readv_report().
    6861             :      */
    6862     2456804 :     piv_flags = PIV_LOG_LOG;
    6863             : 
    6864             :     /* the local zero_damaged_pages may differ from the definer's */
    6865     2456804 :     if (flags & READ_BUFFERS_IGNORE_CHECKSUM_FAILURES)
    6866          76 :         piv_flags |= PIV_IGNORE_CHECKSUM_FAILURE;
    6867             : 
    6868             :     /* Check for garbage data. */
    6869     2456804 :     if (!failed)
    6870             :     {
    6871             :         PgAioResult result_one;
    6872             : 
    6873     2456746 :         if (!PageIsVerified((Page) bufdata, tag.blockNum, piv_flags,
    6874             :                             failed_checksum))
    6875             :         {
    6876         192 :             if (flags & READ_BUFFERS_ZERO_ON_ERROR)
    6877             :             {
    6878          92 :                 memset(bufdata, 0, BLCKSZ);
    6879          92 :                 *zeroed_buffer = true;
    6880             :             }
    6881             :             else
    6882             :             {
    6883         100 :                 *buffer_invalid = true;
    6884             :                 /* mark buffer as having failed */
    6885         100 :                 failed = true;
    6886             :             }
    6887             :         }
    6888     2456554 :         else if (*failed_checksum)
    6889          24 :             *ignored_checksum = true;
    6890             : 
    6891             :         /*
    6892             :          * Immediately log a message about the invalid page, but only to the
    6893             :          * server log. The reason to do so immediately is that this may be
    6894             :          * executed in a different backend than the one that originated the
    6895             :          * request. The reason to do so immediately is that the originator
    6896             :          * might not process the query result immediately (because it is busy
    6897             :          * doing another part of query processing) or at all (e.g. if it was
    6898             :          * cancelled or errored out due to another IO also failing). The
    6899             :          * definer of the IO will emit an ERROR or WARNING when processing the
    6900             :          * IO's results
    6901             :          *
    6902             :          * To avoid duplicating the code to emit these log messages, we reuse
    6903             :          * buffer_readv_report().
    6904             :          */
    6905     2456746 :         if (*buffer_invalid || *failed_checksum || *zeroed_buffer)
    6906             :         {
    6907         216 :             buffer_readv_encode_error(&result_one, is_temp,
    6908         216 :                                       *zeroed_buffer,
    6909         216 :                                       *ignored_checksum,
    6910         216 :                                       *buffer_invalid,
    6911         216 :                                       *zeroed_buffer ? 1 : 0,
    6912         216 :                                       *failed_checksum ? 1 : 0,
    6913             :                                       buf_off, buf_off, buf_off);
    6914         216 :             pgaio_result_report(result_one, td, LOG_SERVER_ONLY);
    6915             :         }
    6916             :     }
    6917             : 
    6918             :     /* Terminate I/O and set BM_VALID. */
    6919     2456804 :     set_flag_bits = failed ? BM_IO_ERROR : BM_VALID;
    6920     2456804 :     if (is_temp)
    6921       16778 :         TerminateLocalBufferIO(buf_hdr, false, set_flag_bits, true);
    6922             :     else
    6923     2440026 :         TerminateBufferIO(buf_hdr, false, set_flag_bits, false, true);
    6924             : 
    6925             :     /*
    6926             :      * Call the BUFFER_READ_DONE tracepoint in the callback, even though the
    6927             :      * callback may not be executed in the same backend that called
    6928             :      * BUFFER_READ_START. The alternative would be to defer calling the
    6929             :      * tracepoint to a later point (e.g. the local completion callback for
    6930             :      * shared buffer reads), which seems even less helpful.
    6931             :      */
    6932             :     TRACE_POSTGRESQL_BUFFER_READ_DONE(tag.forkNum,
    6933             :                                       tag.blockNum,
    6934             :                                       tag.spcOid,
    6935             :                                       tag.dbOid,
    6936             :                                       tag.relNumber,
    6937             :                                       is_temp ? MyProcNumber : INVALID_PROC_NUMBER,
    6938             :                                       false);
    6939     2456804 : }
    6940             : 
    6941             : /*
    6942             :  * Perform completion handling of a single AIO read. This read may cover
    6943             :  * multiple blocks / buffers.
    6944             :  *
    6945             :  * Shared between shared and local buffers, to reduce code duplication.
    6946             :  */
    6947             : static pg_attribute_always_inline PgAioResult
    6948     2199768 : buffer_readv_complete(PgAioHandle *ioh, PgAioResult prior_result,
    6949             :                       uint8 cb_data, bool is_temp)
    6950             : {
    6951     2199768 :     PgAioResult result = prior_result;
    6952     2199768 :     PgAioTargetData *td = pgaio_io_get_target_data(ioh);
    6953     2199768 :     uint8       first_error_off = 0;
    6954     2199768 :     uint8       first_zeroed_off = 0;
    6955     2199768 :     uint8       first_ignored_off = 0;
    6956     2199768 :     uint8       error_count = 0;
    6957     2199768 :     uint8       zeroed_count = 0;
    6958     2199768 :     uint8       ignored_count = 0;
    6959     2199768 :     uint8       checkfail_count = 0;
    6960             :     uint64     *io_data;
    6961             :     uint8       handle_data_len;
    6962             : 
    6963             :     if (is_temp)
    6964             :     {
    6965             :         Assert(td->smgr.is_temp);
    6966             :         Assert(pgaio_io_get_owner(ioh) == MyProcNumber);
    6967             :     }
    6968             :     else
    6969             :         Assert(!td->smgr.is_temp);
    6970             : 
    6971             :     /*
    6972             :      * Iterate over all the buffers affected by this IO and call the
    6973             :      * per-buffer completion function for each buffer.
    6974             :      */
    6975     2199768 :     io_data = pgaio_io_get_handle_data(ioh, &handle_data_len);
    6976     4656572 :     for (uint8 buf_off = 0; buf_off < handle_data_len; buf_off++)
    6977             :     {
    6978     2456804 :         Buffer      buf = io_data[buf_off];
    6979             :         bool        failed;
    6980     2456804 :         bool        failed_verification = false;
    6981     2456804 :         bool        failed_checksum = false;
    6982     2456804 :         bool        zeroed_buffer = false;
    6983     2456804 :         bool        ignored_checksum = false;
    6984             : 
    6985             :         Assert(BufferIsValid(buf));
    6986             : 
    6987             :         /*
    6988             :          * If the entire I/O failed on a lower-level, each buffer needs to be
    6989             :          * marked as failed. In case of a partial read, the first few buffers
    6990             :          * may be ok.
    6991             :          */
    6992     2456804 :         failed =
    6993     2456804 :             prior_result.status == PGAIO_RS_ERROR
    6994     2456804 :             || prior_result.result <= buf_off;
    6995             : 
    6996     2456804 :         buffer_readv_complete_one(td, buf_off, buf, cb_data, failed, is_temp,
    6997             :                                   &failed_verification,
    6998             :                                   &failed_checksum,
    6999             :                                   &ignored_checksum,
    7000             :                                   &zeroed_buffer);
    7001             : 
    7002             :         /*
    7003             :          * Track information about the number of different kinds of error
    7004             :          * conditions across all pages, as there can be multiple pages failing
    7005             :          * verification as part of one IO.
    7006             :          */
    7007     2456804 :         if (failed_verification && !zeroed_buffer && error_count++ == 0)
    7008          88 :             first_error_off = buf_off;
    7009     2456804 :         if (zeroed_buffer && zeroed_count++ == 0)
    7010          68 :             first_zeroed_off = buf_off;
    7011     2456804 :         if (ignored_checksum && ignored_count++ == 0)
    7012          20 :             first_ignored_off = buf_off;
    7013     2456804 :         if (failed_checksum)
    7014          64 :             checkfail_count++;
    7015             :     }
    7016             : 
    7017             :     /*
    7018             :      * If the smgr read succeeded [partially] and page verification failed for
    7019             :      * some of the pages, adjust the IO's result state appropriately.
    7020             :      */
    7021     2199768 :     if (prior_result.status != PGAIO_RS_ERROR &&
    7022     2199662 :         (error_count > 0 || ignored_count > 0 || zeroed_count > 0))
    7023             :     {
    7024         168 :         buffer_readv_encode_error(&result, is_temp,
    7025             :                                   zeroed_count > 0, ignored_count > 0,
    7026             :                                   error_count, zeroed_count, checkfail_count,
    7027             :                                   first_error_off, first_zeroed_off,
    7028             :                                   first_ignored_off);
    7029         168 :         pgaio_result_report(result, td, DEBUG1);
    7030             :     }
    7031             : 
    7032             :     /*
    7033             :      * For shared relations this reporting is done in
    7034             :      * shared_buffer_readv_complete_local().
    7035             :      */
    7036     2199768 :     if (is_temp && checkfail_count > 0)
    7037           4 :         pgstat_report_checksum_failures_in_db(td->smgr.rlocator.dbOid,
    7038             :                                               checkfail_count);
    7039             : 
    7040     2199768 :     return result;
    7041             : }
    7042             : 
    7043             : /*
    7044             :  * AIO error reporting callback for aio_shared_buffer_readv_cb and
    7045             :  * aio_local_buffer_readv_cb.
    7046             :  *
    7047             :  * The error is encoded / decoded in buffer_readv_encode_error() /
    7048             :  * buffer_readv_decode_error().
    7049             :  */
    7050             : static void
    7051         544 : buffer_readv_report(PgAioResult result, const PgAioTargetData *td,
    7052             :                     int elevel)
    7053             : {
    7054         544 :     int         nblocks = td->smgr.nblocks;
    7055         544 :     BlockNumber first = td->smgr.blockNum;
    7056         544 :     BlockNumber last = first + nblocks - 1;
    7057         544 :     ProcNumber  errProc =
    7058         544 :         td->smgr.is_temp ? MyProcNumber : INVALID_PROC_NUMBER;
    7059             :     RelPathStr  rpath =
    7060         544 :         relpathbackend(td->smgr.rlocator, errProc, td->smgr.forkNum);
    7061             :     bool        zeroed_any,
    7062             :                 ignored_any;
    7063             :     uint8       zeroed_or_error_count,
    7064             :                 checkfail_count,
    7065             :                 first_off;
    7066             :     uint8       affected_count;
    7067             :     const char *msg_one,
    7068             :                *msg_mult,
    7069             :                *det_mult,
    7070             :                *hint_mult;
    7071             : 
    7072         544 :     buffer_readv_decode_error(result, &zeroed_any, &ignored_any,
    7073             :                               &zeroed_or_error_count,
    7074             :                               &checkfail_count,
    7075             :                               &first_off);
    7076             : 
    7077             :     /*
    7078             :      * Treat a read that had both zeroed buffers *and* ignored checksums as a
    7079             :      * special case, it's too irregular to be emitted the same way as the
    7080             :      * other cases.
    7081             :      */
    7082         544 :     if (zeroed_any && ignored_any)
    7083             :     {
    7084             :         Assert(zeroed_any && ignored_any);
    7085             :         Assert(nblocks > 1); /* same block can't be both zeroed and ignored */
    7086             :         Assert(result.status != PGAIO_RS_ERROR);
    7087           8 :         affected_count = zeroed_or_error_count;
    7088             : 
    7089           8 :         ereport(elevel,
    7090             :                 errcode(ERRCODE_DATA_CORRUPTED),
    7091             :                 errmsg("zeroing %u page(s) and ignoring %u checksum failure(s) among blocks %u..%u of relation %s",
    7092             :                        affected_count, checkfail_count, first, last, rpath.str),
    7093             :                 affected_count > 1 ?
    7094             :                 errdetail("Block %u held first zeroed page.",
    7095             :                           first + first_off) : 0,
    7096             :                 errhint("See server log for details about the other %u invalid block(s).",
    7097             :                         affected_count + checkfail_count - 1));
    7098           8 :         return;
    7099             :     }
    7100             : 
    7101             :     /*
    7102             :      * The other messages are highly repetitive. To avoid duplicating a long
    7103             :      * and complicated ereport(), gather the translated format strings
    7104             :      * separately and then do one common ereport.
    7105             :      */
    7106         536 :     if (result.status == PGAIO_RS_ERROR)
    7107             :     {
    7108             :         Assert(!zeroed_any);    /* can't have invalid pages when zeroing them */
    7109         272 :         affected_count = zeroed_or_error_count;
    7110         272 :         msg_one = _("invalid page in block %u of relation %s");
    7111         272 :         msg_mult = _("%u invalid pages among blocks %u..%u of relation %s");
    7112         272 :         det_mult = _("Block %u held first invalid page.");
    7113         272 :         hint_mult = _("See server log for the other %u invalid block(s).");
    7114             :     }
    7115         264 :     else if (zeroed_any && !ignored_any)
    7116             :     {
    7117         216 :         affected_count = zeroed_or_error_count;
    7118         216 :         msg_one = _("invalid page in block %u of relation %s; zeroing out page");
    7119         216 :         msg_mult = _("zeroing out %u invalid pages among blocks %u..%u of relation %s");
    7120         216 :         det_mult = _("Block %u held first zeroed page.");
    7121         216 :         hint_mult = _("See server log for the other %u zeroed block(s).");
    7122             :     }
    7123          48 :     else if (!zeroed_any && ignored_any)
    7124             :     {
    7125          48 :         affected_count = checkfail_count;
    7126          48 :         msg_one = _("ignoring checksum failure in block %u of relation %s");
    7127          48 :         msg_mult = _("ignoring %u checksum failures among blocks %u..%u of relation %s");
    7128          48 :         det_mult = _("Block %u held first ignored page.");
    7129          48 :         hint_mult = _("See server log for the other %u ignored block(s).");
    7130             :     }
    7131             :     else
    7132           0 :         pg_unreachable();
    7133             : 
    7134         536 :     ereport(elevel,
    7135             :             errcode(ERRCODE_DATA_CORRUPTED),
    7136             :             affected_count == 1 ?
    7137             :             errmsg_internal(msg_one, first + first_off, rpath.str) :
    7138             :             errmsg_internal(msg_mult, affected_count, first, last, rpath.str),
    7139             :             affected_count > 1 ? errdetail_internal(det_mult, first + first_off) : 0,
    7140             :             affected_count > 1 ? errhint_internal(hint_mult, affected_count - 1) : 0);
    7141             : }
    7142             : 
    7143             : static void
    7144     2406260 : shared_buffer_readv_stage(PgAioHandle *ioh, uint8 cb_data)
    7145             : {
    7146     2406260 :     buffer_stage_common(ioh, false, false);
    7147     2406260 : }
    7148             : 
    7149             : static PgAioResult
    7150     2196196 : shared_buffer_readv_complete(PgAioHandle *ioh, PgAioResult prior_result,
    7151             :                              uint8 cb_data)
    7152             : {
    7153     2196196 :     return buffer_readv_complete(ioh, prior_result, cb_data, false);
    7154             : }
    7155             : 
    7156             : /*
    7157             :  * We need a backend-local completion callback for shared buffers, to be able
    7158             :  * to report checksum errors correctly. Unfortunately that can only safely
    7159             :  * happen if the reporting backend has previously called
    7160             :  * pgstat_prepare_report_checksum_failure(), which we can only guarantee in
    7161             :  * the backend that started the IO. Hence this callback.
    7162             :  */
    7163             : static PgAioResult
    7164     2406260 : shared_buffer_readv_complete_local(PgAioHandle *ioh, PgAioResult prior_result,
    7165             :                                    uint8 cb_data)
    7166             : {
    7167             :     bool        zeroed_any,
    7168             :                 ignored_any;
    7169             :     uint8       zeroed_or_error_count,
    7170             :                 checkfail_count,
    7171             :                 first_off;
    7172             : 
    7173     2406260 :     if (prior_result.status == PGAIO_RS_OK)
    7174     2406106 :         return prior_result;
    7175             : 
    7176         154 :     buffer_readv_decode_error(prior_result,
    7177             :                               &zeroed_any,
    7178             :                               &ignored_any,
    7179             :                               &zeroed_or_error_count,
    7180             :                               &checkfail_count,
    7181             :                               &first_off);
    7182             : 
    7183         154 :     if (checkfail_count)
    7184             :     {
    7185          48 :         PgAioTargetData *td = pgaio_io_get_target_data(ioh);
    7186             : 
    7187          48 :         pgstat_report_checksum_failures_in_db(td->smgr.rlocator.dbOid,
    7188             :                                               checkfail_count);
    7189             :     }
    7190             : 
    7191         154 :     return prior_result;
    7192             : }
    7193             : 
    7194             : static void
    7195        3572 : local_buffer_readv_stage(PgAioHandle *ioh, uint8 cb_data)
    7196             : {
    7197        3572 :     buffer_stage_common(ioh, false, true);
    7198        3572 : }
    7199             : 
    7200             : static PgAioResult
    7201        3572 : local_buffer_readv_complete(PgAioHandle *ioh, PgAioResult prior_result,
    7202             :                             uint8 cb_data)
    7203             : {
    7204        3572 :     return buffer_readv_complete(ioh, prior_result, cb_data, true);
    7205             : }
    7206             : 
    7207             : /* readv callback is passed READ_BUFFERS_* flags as callback data */
    7208             : const PgAioHandleCallbacks aio_shared_buffer_readv_cb = {
    7209             :     .stage = shared_buffer_readv_stage,
    7210             :     .complete_shared = shared_buffer_readv_complete,
    7211             :     /* need a local callback to report checksum failures */
    7212             :     .complete_local = shared_buffer_readv_complete_local,
    7213             :     .report = buffer_readv_report,
    7214             : };
    7215             : 
    7216             : /* readv callback is passed READ_BUFFERS_* flags as callback data */
    7217             : const PgAioHandleCallbacks aio_local_buffer_readv_cb = {
    7218             :     .stage = local_buffer_readv_stage,
    7219             : 
    7220             :     /*
    7221             :      * Note that this, in contrast to the shared_buffers case, uses
    7222             :      * complete_local, as only the issuing backend has access to the required
    7223             :      * datastructures. This is important in case the IO completion may be
    7224             :      * consumed incidentally by another backend.
    7225             :      */
    7226             :     .complete_local = local_buffer_readv_complete,
    7227             :     .report = buffer_readv_report,
    7228             : };

Generated by: LCOV version 1.14