PostgreSQL Source Code  git master
bufmgr.h File Reference
#include "port/pg_iovec.h"
#include "storage/block.h"
#include "storage/buf.h"
#include "storage/bufpage.h"
#include "storage/relfilelocator.h"
#include "utils/relcache.h"
#include "utils/snapmgr.h"
Include dependency graph for bufmgr.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PrefetchBufferResult
 
struct  BufferManagerRelation
 
struct  ReadBuffersOperation
 

Macros

#define BMR_REL(p_rel)   ((BufferManagerRelation){.rel = p_rel})
 
#define BMR_SMGR(p_smgr, p_relpersistence)   ((BufferManagerRelation){.smgr = p_smgr, .relpersistence = p_relpersistence})
 
#define READ_BUFFERS_ZERO_ON_ERROR   (1 << 0)
 
#define READ_BUFFERS_ISSUE_ADVICE   (1 << 1)
 
#define DEFAULT_EFFECTIVE_IO_CONCURRENCY   0
 
#define DEFAULT_MAINTENANCE_IO_CONCURRENCY   0
 
#define MAX_IO_COMBINE_LIMIT   PG_IOV_MAX
 
#define DEFAULT_IO_COMBINE_LIMIT   Min(MAX_IO_COMBINE_LIMIT, (128 * 1024) / BLCKSZ)
 
#define MAX_IO_CONCURRENCY   1000
 
#define P_NEW   InvalidBlockNumber /* grow the file to get a new page */
 
#define BUFFER_LOCK_UNLOCK   0
 
#define BUFFER_LOCK_SHARE   1
 
#define BUFFER_LOCK_EXCLUSIVE   2
 
#define RelationGetNumberOfBlocks(reln)    RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
 

Typedefs

typedef void * Block
 
typedef enum BufferAccessStrategyType BufferAccessStrategyType
 
typedef struct PrefetchBufferResult PrefetchBufferResult
 
typedef enum ExtendBufferedFlags ExtendBufferedFlags
 
typedef struct BufferManagerRelation BufferManagerRelation
 
typedef struct ReadBuffersOperation ReadBuffersOperation
 

Enumerations

enum  BufferAccessStrategyType { BAS_NORMAL , BAS_BULKREAD , BAS_BULKWRITE , BAS_VACUUM }
 
enum  ReadBufferMode {
  RBM_NORMAL , RBM_ZERO_AND_LOCK , RBM_ZERO_AND_CLEANUP_LOCK , RBM_ZERO_ON_ERROR ,
  RBM_NORMAL_NO_LOG
}
 
enum  ExtendBufferedFlags {
  EB_SKIP_EXTENSION_LOCK = (1 << 0) , EB_PERFORMING_RECOVERY = (1 << 1) , EB_CREATE_FORK_IF_NEEDED = (1 << 2) , EB_LOCK_FIRST = (1 << 3) ,
  EB_CLEAR_SIZE_CACHE = (1 << 4) , EB_LOCK_TARGET = (1 << 5)
}
 

Functions

PrefetchBufferResult PrefetchSharedBuffer (struct SMgrRelationData *smgr_reln, ForkNumber forkNum, BlockNumber blockNum)
 
PrefetchBufferResult PrefetchBuffer (Relation reln, ForkNumber forkNum, BlockNumber blockNum)
 
bool ReadRecentBuffer (RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, Buffer recent_buffer)
 
Buffer ReadBuffer (Relation reln, BlockNumber blockNum)
 
Buffer ReadBufferExtended (Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
 
Buffer ReadBufferWithoutRelcache (RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy, bool permanent)
 
bool StartReadBuffer (ReadBuffersOperation *operation, Buffer *buffer, BlockNumber blocknum, int flags)
 
bool StartReadBuffers (ReadBuffersOperation *operation, Buffer *buffers, BlockNumber blockNum, int *nblocks, int flags)
 
void WaitReadBuffers (ReadBuffersOperation *operation)
 
void ReleaseBuffer (Buffer buffer)
 
void UnlockReleaseBuffer (Buffer buffer)
 
bool BufferIsExclusiveLocked (Buffer buffer)
 
bool BufferIsDirty (Buffer buffer)
 
void MarkBufferDirty (Buffer buffer)
 
void IncrBufferRefCount (Buffer buffer)
 
void CheckBufferIsPinnedOnce (Buffer buffer)
 
Buffer ReleaseAndReadBuffer (Buffer buffer, Relation relation, BlockNumber blockNum)
 
Buffer ExtendBufferedRel (BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
 
BlockNumber ExtendBufferedRelBy (BufferManagerRelation bmr, ForkNumber fork, BufferAccessStrategy strategy, uint32 flags, uint32 extend_by, Buffer *buffers, uint32 *extended_by)
 
Buffer ExtendBufferedRelTo (BufferManagerRelation bmr, ForkNumber fork, BufferAccessStrategy strategy, uint32 flags, BlockNumber extend_to, ReadBufferMode mode)
 
void InitBufferPoolAccess (void)
 
void AtEOXact_Buffers (bool isCommit)
 
char * DebugPrintBufferRefcount (Buffer buffer)
 
void CheckPointBuffers (int flags)
 
BlockNumber BufferGetBlockNumber (Buffer buffer)
 
BlockNumber RelationGetNumberOfBlocksInFork (Relation relation, ForkNumber forkNum)
 
void FlushOneBuffer (Buffer buffer)
 
void FlushRelationBuffers (Relation rel)
 
void FlushRelationsAllBuffers (struct SMgrRelationData **smgrs, int nrels)
 
void CreateAndCopyRelationData (RelFileLocator src_rlocator, RelFileLocator dst_rlocator, bool permanent)
 
void FlushDatabaseBuffers (Oid dbid)
 
void DropRelationBuffers (struct SMgrRelationData *smgr_reln, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
 
void DropRelationsAllBuffers (struct SMgrRelationData **smgr_reln, int nlocators)
 
void DropDatabaseBuffers (Oid dbid)
 
bool BufferIsPermanent (Buffer buffer)
 
XLogRecPtr BufferGetLSNAtomic (Buffer buffer)
 
void BufferGetTag (Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
 
void MarkBufferDirtyHint (Buffer buffer, bool buffer_std)
 
void UnlockBuffers (void)
 
void LockBuffer (Buffer buffer, int mode)
 
bool ConditionalLockBuffer (Buffer buffer)
 
void LockBufferForCleanup (Buffer buffer)
 
bool ConditionalLockBufferForCleanup (Buffer buffer)
 
bool IsBufferCleanupOK (Buffer buffer)
 
bool HoldingBufferPinThatDelaysRecovery (void)
 
bool BgBufferSync (struct WritebackContext *wb_context)
 
void LimitAdditionalPins (uint32 *additional_pins)
 
void LimitAdditionalLocalPins (uint32 *additional_pins)
 
bool EvictUnpinnedBuffer (Buffer buf)
 
void InitBufferPool (void)
 
Size BufferShmemSize (void)
 
void AtProcExit_LocalBuffers (void)
 
BufferAccessStrategy GetAccessStrategy (BufferAccessStrategyType btype)
 
BufferAccessStrategy GetAccessStrategyWithSize (BufferAccessStrategyType btype, int ring_size_kb)
 
int GetAccessStrategyBufferCount (BufferAccessStrategy strategy)
 
int GetAccessStrategyPinLimit (BufferAccessStrategy strategy)
 
void FreeAccessStrategy (BufferAccessStrategy strategy)
 
static bool BufferIsValid (Buffer bufnum)
 
static Block BufferGetBlock (Buffer buffer)
 
static Size BufferGetPageSize (Buffer buffer)
 
static Page BufferGetPage (Buffer buffer)
 

Variables

PGDLLIMPORT int NBuffers
 
PGDLLIMPORT bool zero_damaged_pages
 
PGDLLIMPORT int bgwriter_lru_maxpages
 
PGDLLIMPORT double bgwriter_lru_multiplier
 
PGDLLIMPORT bool track_io_timing
 
PGDLLIMPORT int effective_io_concurrency
 
PGDLLIMPORT int maintenance_io_concurrency
 
PGDLLIMPORT int io_combine_limit
 
PGDLLIMPORT int checkpoint_flush_after
 
PGDLLIMPORT int backend_flush_after
 
PGDLLIMPORT int bgwriter_flush_after
 
PGDLLIMPORT char * BufferBlocks
 
PGDLLIMPORT int NLocBuffer
 
PGDLLIMPORT BlockLocalBufferBlockPointers
 
PGDLLIMPORT int32LocalRefCount
 

Macro Definition Documentation

◆ BMR_REL

#define BMR_REL (   p_rel)    ((BufferManagerRelation){.rel = p_rel})

Definition at line 107 of file bufmgr.h.

◆ BMR_SMGR

#define BMR_SMGR (   p_smgr,
  p_relpersistence 
)    ((BufferManagerRelation){.smgr = p_smgr, .relpersistence = p_relpersistence})

Definition at line 108 of file bufmgr.h.

◆ BUFFER_LOCK_EXCLUSIVE

#define BUFFER_LOCK_EXCLUSIVE   2

Definition at line 195 of file bufmgr.h.

◆ BUFFER_LOCK_SHARE

#define BUFFER_LOCK_SHARE   1

Definition at line 194 of file bufmgr.h.

◆ BUFFER_LOCK_UNLOCK

#define BUFFER_LOCK_UNLOCK   0

Definition at line 193 of file bufmgr.h.

◆ DEFAULT_EFFECTIVE_IO_CONCURRENCY

#define DEFAULT_EFFECTIVE_IO_CONCURRENCY   0

Definition at line 162 of file bufmgr.h.

◆ DEFAULT_IO_COMBINE_LIMIT

#define DEFAULT_IO_COMBINE_LIMIT   Min(MAX_IO_COMBINE_LIMIT, (128 * 1024) / BLCKSZ)

Definition at line 169 of file bufmgr.h.

◆ DEFAULT_MAINTENANCE_IO_CONCURRENCY

#define DEFAULT_MAINTENANCE_IO_CONCURRENCY   0

Definition at line 163 of file bufmgr.h.

◆ MAX_IO_COMBINE_LIMIT

#define MAX_IO_COMBINE_LIMIT   PG_IOV_MAX

Definition at line 168 of file bufmgr.h.

◆ MAX_IO_CONCURRENCY

#define MAX_IO_CONCURRENCY   1000

Definition at line 185 of file bufmgr.h.

◆ P_NEW

#define P_NEW   InvalidBlockNumber /* grow the file to get a new page */

Definition at line 188 of file bufmgr.h.

◆ READ_BUFFERS_ISSUE_ADVICE

#define READ_BUFFERS_ISSUE_ADVICE   (1 << 1)

Definition at line 113 of file bufmgr.h.

◆ READ_BUFFERS_ZERO_ON_ERROR

#define READ_BUFFERS_ZERO_ON_ERROR   (1 << 0)

Definition at line 111 of file bufmgr.h.

◆ RelationGetNumberOfBlocks

#define RelationGetNumberOfBlocks (   reln)     RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)

Definition at line 277 of file bufmgr.h.

Typedef Documentation

◆ Block

typedef void* Block

Definition at line 25 of file bufmgr.h.

◆ BufferAccessStrategyType

◆ BufferManagerRelation

◆ ExtendBufferedFlags

◆ PrefetchBufferResult

◆ ReadBuffersOperation

Definition at line 25 of file bufmgr.h.

Enumeration Type Documentation

◆ BufferAccessStrategyType

Enumerator
BAS_NORMAL 
BAS_BULKREAD 
BAS_BULKWRITE 
BAS_VACUUM 

Definition at line 33 of file bufmgr.h.

34 {
35  BAS_NORMAL, /* Normal random access */
36  BAS_BULKREAD, /* Large read-only scan (hint bit updates are
37  * ok) */
38  BAS_BULKWRITE, /* Large multi-block write (e.g. COPY IN) */
39  BAS_VACUUM, /* VACUUM */
BufferAccessStrategyType
Definition: bufmgr.h:34
@ BAS_BULKREAD
Definition: bufmgr.h:36
@ BAS_NORMAL
Definition: bufmgr.h:35
@ BAS_VACUUM
Definition: bufmgr.h:39
@ BAS_BULKWRITE
Definition: bufmgr.h:38

◆ ExtendBufferedFlags

Enumerator
EB_SKIP_EXTENSION_LOCK 
EB_PERFORMING_RECOVERY 
EB_CREATE_FORK_IF_NEEDED 
EB_LOCK_FIRST 
EB_CLEAR_SIZE_CACHE 
EB_LOCK_TARGET 

Definition at line 67 of file bufmgr.h.

68 {
69  /*
70  * Don't acquire extension lock. This is safe only if the relation isn't
71  * shared, an access exclusive lock is held or if this is the startup
72  * process.
73  */
74  EB_SKIP_EXTENSION_LOCK = (1 << 0),
75 
76  /* Is this extension part of recovery? */
77  EB_PERFORMING_RECOVERY = (1 << 1),
78 
79  /*
80  * Should the fork be created if it does not currently exist? This likely
81  * only ever makes sense for relation forks.
82  */
83  EB_CREATE_FORK_IF_NEEDED = (1 << 2),
84 
85  /* Should the first (possibly only) return buffer be returned locked? */
86  EB_LOCK_FIRST = (1 << 3),
87 
88  /* Should the smgr size cache be cleared? */
89  EB_CLEAR_SIZE_CACHE = (1 << 4),
90 
91  /* internal flags follow */
92  EB_LOCK_TARGET = (1 << 5),
ExtendBufferedFlags
Definition: bufmgr.h:68
@ EB_LOCK_TARGET
Definition: bufmgr.h:92
@ EB_CLEAR_SIZE_CACHE
Definition: bufmgr.h:89
@ EB_PERFORMING_RECOVERY
Definition: bufmgr.h:77
@ EB_CREATE_FORK_IF_NEEDED
Definition: bufmgr.h:83
@ EB_SKIP_EXTENSION_LOCK
Definition: bufmgr.h:74
@ EB_LOCK_FIRST
Definition: bufmgr.h:86

◆ ReadBufferMode

Enumerator
RBM_NORMAL 
RBM_ZERO_AND_LOCK 
RBM_ZERO_AND_CLEANUP_LOCK 
RBM_ZERO_ON_ERROR 
RBM_NORMAL_NO_LOG 

Definition at line 43 of file bufmgr.h.

44 {
45  RBM_NORMAL, /* Normal read */
46  RBM_ZERO_AND_LOCK, /* Don't read from disk, caller will
47  * initialize. Also locks the page. */
48  RBM_ZERO_AND_CLEANUP_LOCK, /* Like RBM_ZERO_AND_LOCK, but locks the page
49  * in "cleanup" mode */
50  RBM_ZERO_ON_ERROR, /* Read, but return an all-zeros page on error */
51  RBM_NORMAL_NO_LOG, /* Don't log page as invalid during WAL
52  * replay; otherwise same as RBM_NORMAL */
ReadBufferMode
Definition: bufmgr.h:44
@ RBM_ZERO_ON_ERROR
Definition: bufmgr.h:50
@ RBM_ZERO_AND_CLEANUP_LOCK
Definition: bufmgr.h:48
@ RBM_ZERO_AND_LOCK
Definition: bufmgr.h:46
@ RBM_NORMAL
Definition: bufmgr.h:45
@ RBM_NORMAL_NO_LOG
Definition: bufmgr.h:51

Function Documentation

◆ AtEOXact_Buffers()

void AtEOXact_Buffers ( bool  isCommit)

Definition at line 3548 of file bufmgr.c.

3549 {
3551 
3552  AtEOXact_LocalBuffers(isCommit);
3553 
3555 }
static void CheckForBufferLeaks(void)
Definition: bufmgr.c:3608
static int32 PrivateRefCountOverflowed
Definition: bufmgr.c:209
#define Assert(condition)
Definition: c.h:858
void AtEOXact_LocalBuffers(bool isCommit)
Definition: localbuf.c:819

References Assert, AtEOXact_LocalBuffers(), CheckForBufferLeaks(), and PrivateRefCountOverflowed.

Referenced by AbortTransaction(), BackgroundWriterMain(), CheckpointerMain(), CommitTransaction(), PrepareTransaction(), and WalWriterMain().

◆ AtProcExit_LocalBuffers()

void AtProcExit_LocalBuffers ( void  )

Definition at line 830 of file localbuf.c.

831 {
832  /*
833  * We shouldn't be holding any remaining pins; if we are, and assertions
834  * aren't enabled, we'll fail later in DropRelationBuffers while trying to
835  * drop the temp rels.
836  */
838 }
static void CheckForLocalBufferLeaks(void)
Definition: localbuf.c:786

References CheckForLocalBufferLeaks().

Referenced by AtProcExit_Buffers().

◆ BgBufferSync()

bool BgBufferSync ( struct WritebackContext wb_context)

Definition at line 3177 of file bufmgr.c.

3178 {
3179  /* info obtained from freelist.c */
3180  int strategy_buf_id;
3181  uint32 strategy_passes;
3182  uint32 recent_alloc;
3183 
3184  /*
3185  * Information saved between calls so we can determine the strategy
3186  * point's advance rate and avoid scanning already-cleaned buffers.
3187  */
3188  static bool saved_info_valid = false;
3189  static int prev_strategy_buf_id;
3190  static uint32 prev_strategy_passes;
3191  static int next_to_clean;
3192  static uint32 next_passes;
3193 
3194  /* Moving averages of allocation rate and clean-buffer density */
3195  static float smoothed_alloc = 0;
3196  static float smoothed_density = 10.0;
3197 
3198  /* Potentially these could be tunables, but for now, not */
3199  float smoothing_samples = 16;
3200  float scan_whole_pool_milliseconds = 120000.0;
3201 
3202  /* Used to compute how far we scan ahead */
3203  long strategy_delta;
3204  int bufs_to_lap;
3205  int bufs_ahead;
3206  float scans_per_alloc;
3207  int reusable_buffers_est;
3208  int upcoming_alloc_est;
3209  int min_scan_buffers;
3210 
3211  /* Variables for the scanning loop proper */
3212  int num_to_scan;
3213  int num_written;
3214  int reusable_buffers;
3215 
3216  /* Variables for final smoothed_density update */
3217  long new_strategy_delta;
3218  uint32 new_recent_alloc;
3219 
3220  /*
3221  * Find out where the freelist clock sweep currently is, and how many
3222  * buffer allocations have happened since our last call.
3223  */
3224  strategy_buf_id = StrategySyncStart(&strategy_passes, &recent_alloc);
3225 
3226  /* Report buffer alloc counts to pgstat */
3227  PendingBgWriterStats.buf_alloc += recent_alloc;
3228 
3229  /*
3230  * If we're not running the LRU scan, just stop after doing the stats
3231  * stuff. We mark the saved state invalid so that we can recover sanely
3232  * if LRU scan is turned back on later.
3233  */
3234  if (bgwriter_lru_maxpages <= 0)
3235  {
3236  saved_info_valid = false;
3237  return true;
3238  }
3239 
3240  /*
3241  * Compute strategy_delta = how many buffers have been scanned by the
3242  * clock sweep since last time. If first time through, assume none. Then
3243  * see if we are still ahead of the clock sweep, and if so, how many
3244  * buffers we could scan before we'd catch up with it and "lap" it. Note:
3245  * weird-looking coding of xxx_passes comparisons are to avoid bogus
3246  * behavior when the passes counts wrap around.
3247  */
3248  if (saved_info_valid)
3249  {
3250  int32 passes_delta = strategy_passes - prev_strategy_passes;
3251 
3252  strategy_delta = strategy_buf_id - prev_strategy_buf_id;
3253  strategy_delta += (long) passes_delta * NBuffers;
3254 
3255  Assert(strategy_delta >= 0);
3256 
3257  if ((int32) (next_passes - strategy_passes) > 0)
3258  {
3259  /* we're one pass ahead of the strategy point */
3260  bufs_to_lap = strategy_buf_id - next_to_clean;
3261 #ifdef BGW_DEBUG
3262  elog(DEBUG2, "bgwriter ahead: bgw %u-%u strategy %u-%u delta=%ld lap=%d",
3263  next_passes, next_to_clean,
3264  strategy_passes, strategy_buf_id,
3265  strategy_delta, bufs_to_lap);
3266 #endif
3267  }
3268  else if (next_passes == strategy_passes &&
3269  next_to_clean >= strategy_buf_id)
3270  {
3271  /* on same pass, but ahead or at least not behind */
3272  bufs_to_lap = NBuffers - (next_to_clean - strategy_buf_id);
3273 #ifdef BGW_DEBUG
3274  elog(DEBUG2, "bgwriter ahead: bgw %u-%u strategy %u-%u delta=%ld lap=%d",
3275  next_passes, next_to_clean,
3276  strategy_passes, strategy_buf_id,
3277  strategy_delta, bufs_to_lap);
3278 #endif
3279  }
3280  else
3281  {
3282  /*
3283  * We're behind, so skip forward to the strategy point and start
3284  * cleaning from there.
3285  */
3286 #ifdef BGW_DEBUG
3287  elog(DEBUG2, "bgwriter behind: bgw %u-%u strategy %u-%u delta=%ld",
3288  next_passes, next_to_clean,
3289  strategy_passes, strategy_buf_id,
3290  strategy_delta);
3291 #endif
3292  next_to_clean = strategy_buf_id;
3293  next_passes = strategy_passes;
3294  bufs_to_lap = NBuffers;
3295  }
3296  }
3297  else
3298  {
3299  /*
3300  * Initializing at startup or after LRU scanning had been off. Always
3301  * start at the strategy point.
3302  */
3303 #ifdef BGW_DEBUG
3304  elog(DEBUG2, "bgwriter initializing: strategy %u-%u",
3305  strategy_passes, strategy_buf_id);
3306 #endif
3307  strategy_delta = 0;
3308  next_to_clean = strategy_buf_id;
3309  next_passes = strategy_passes;
3310  bufs_to_lap = NBuffers;
3311  }
3312 
3313  /* Update saved info for next time */
3314  prev_strategy_buf_id = strategy_buf_id;
3315  prev_strategy_passes = strategy_passes;
3316  saved_info_valid = true;
3317 
3318  /*
3319  * Compute how many buffers had to be scanned for each new allocation, ie,
3320  * 1/density of reusable buffers, and track a moving average of that.
3321  *
3322  * If the strategy point didn't move, we don't update the density estimate
3323  */
3324  if (strategy_delta > 0 && recent_alloc > 0)
3325  {
3326  scans_per_alloc = (float) strategy_delta / (float) recent_alloc;
3327  smoothed_density += (scans_per_alloc - smoothed_density) /
3328  smoothing_samples;
3329  }
3330 
3331  /*
3332  * Estimate how many reusable buffers there are between the current
3333  * strategy point and where we've scanned ahead to, based on the smoothed
3334  * density estimate.
3335  */
3336  bufs_ahead = NBuffers - bufs_to_lap;
3337  reusable_buffers_est = (float) bufs_ahead / smoothed_density;
3338 
3339  /*
3340  * Track a moving average of recent buffer allocations. Here, rather than
3341  * a true average we want a fast-attack, slow-decline behavior: we
3342  * immediately follow any increase.
3343  */
3344  if (smoothed_alloc <= (float) recent_alloc)
3345  smoothed_alloc = recent_alloc;
3346  else
3347  smoothed_alloc += ((float) recent_alloc - smoothed_alloc) /
3348  smoothing_samples;
3349 
3350  /* Scale the estimate by a GUC to allow more aggressive tuning. */
3351  upcoming_alloc_est = (int) (smoothed_alloc * bgwriter_lru_multiplier);
3352 
3353  /*
3354  * If recent_alloc remains at zero for many cycles, smoothed_alloc will
3355  * eventually underflow to zero, and the underflows produce annoying
3356  * kernel warnings on some platforms. Once upcoming_alloc_est has gone to
3357  * zero, there's no point in tracking smaller and smaller values of
3358  * smoothed_alloc, so just reset it to exactly zero to avoid this
3359  * syndrome. It will pop back up as soon as recent_alloc increases.
3360  */
3361  if (upcoming_alloc_est == 0)
3362  smoothed_alloc = 0;
3363 
3364  /*
3365  * Even in cases where there's been little or no buffer allocation
3366  * activity, we want to make a small amount of progress through the buffer
3367  * cache so that as many reusable buffers as possible are clean after an
3368  * idle period.
3369  *
3370  * (scan_whole_pool_milliseconds / BgWriterDelay) computes how many times
3371  * the BGW will be called during the scan_whole_pool time; slice the
3372  * buffer pool into that many sections.
3373  */
3374  min_scan_buffers = (int) (NBuffers / (scan_whole_pool_milliseconds / BgWriterDelay));
3375 
3376  if (upcoming_alloc_est < (min_scan_buffers + reusable_buffers_est))
3377  {
3378 #ifdef BGW_DEBUG
3379  elog(DEBUG2, "bgwriter: alloc_est=%d too small, using min=%d + reusable_est=%d",
3380  upcoming_alloc_est, min_scan_buffers, reusable_buffers_est);
3381 #endif
3382  upcoming_alloc_est = min_scan_buffers + reusable_buffers_est;
3383  }
3384 
3385  /*
3386  * Now write out dirty reusable buffers, working forward from the
3387  * next_to_clean point, until we have lapped the strategy scan, or cleaned
3388  * enough buffers to match our estimate of the next cycle's allocation
3389  * requirements, or hit the bgwriter_lru_maxpages limit.
3390  */
3391 
3392  num_to_scan = bufs_to_lap;
3393  num_written = 0;
3394  reusable_buffers = reusable_buffers_est;
3395 
3396  /* Execute the LRU scan */
3397  while (num_to_scan > 0 && reusable_buffers < upcoming_alloc_est)
3398  {
3399  int sync_state = SyncOneBuffer(next_to_clean, true,
3400  wb_context);
3401 
3402  if (++next_to_clean >= NBuffers)
3403  {
3404  next_to_clean = 0;
3405  next_passes++;
3406  }
3407  num_to_scan--;
3408 
3409  if (sync_state & BUF_WRITTEN)
3410  {
3411  reusable_buffers++;
3412  if (++num_written >= bgwriter_lru_maxpages)
3413  {
3415  break;
3416  }
3417  }
3418  else if (sync_state & BUF_REUSABLE)
3419  reusable_buffers++;
3420  }
3421 
3422  PendingBgWriterStats.buf_written_clean += num_written;
3423 
3424 #ifdef BGW_DEBUG
3425  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",
3426  recent_alloc, smoothed_alloc, strategy_delta, bufs_ahead,
3427  smoothed_density, reusable_buffers_est, upcoming_alloc_est,
3428  bufs_to_lap - num_to_scan,
3429  num_written,
3430  reusable_buffers - reusable_buffers_est);
3431 #endif
3432 
3433  /*
3434  * Consider the above scan as being like a new allocation scan.
3435  * Characterize its density and update the smoothed one based on it. This
3436  * effectively halves the moving average period in cases where both the
3437  * strategy and the background writer are doing some useful scanning,
3438  * which is helpful because a long memory isn't as desirable on the
3439  * density estimates.
3440  */
3441  new_strategy_delta = bufs_to_lap - num_to_scan;
3442  new_recent_alloc = reusable_buffers - reusable_buffers_est;
3443  if (new_strategy_delta > 0 && new_recent_alloc > 0)
3444  {
3445  scans_per_alloc = (float) new_strategy_delta / (float) new_recent_alloc;
3446  smoothed_density += (scans_per_alloc - smoothed_density) /
3447  smoothing_samples;
3448 
3449 #ifdef BGW_DEBUG
3450  elog(DEBUG2, "bgwriter: cleaner density alloc=%u scan=%ld density=%.2f new smoothed=%.2f",
3451  new_recent_alloc, new_strategy_delta,
3452  scans_per_alloc, smoothed_density);
3453 #endif
3454  }
3455 
3456  /* Return true if OK to hibernate */
3457  return (bufs_to_lap == 0 && recent_alloc == 0);
3458 }
int BgWriterDelay
Definition: bgwriter.c:57
#define BUF_REUSABLE
Definition: bufmgr.c:76
double bgwriter_lru_multiplier
Definition: bufmgr.c:141
static int SyncOneBuffer(int buf_id, bool skip_recently_used, WritebackContext *wb_context)
Definition: bufmgr.c:3475
int bgwriter_lru_maxpages
Definition: bufmgr.c:140
#define BUF_WRITTEN
Definition: bufmgr.c:75
unsigned int uint32
Definition: c.h:506
signed int int32
Definition: c.h:494
#define DEBUG2
Definition: elog.h:29
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:224
int StrategySyncStart(uint32 *complete_passes, uint32 *num_buf_alloc)
Definition: freelist.c:394
int NBuffers
Definition: globals.c:139
PgStat_BgWriterStats PendingBgWriterStats
PgStat_Counter buf_written_clean
Definition: pgstat.h:255
PgStat_Counter maxwritten_clean
Definition: pgstat.h:256
PgStat_Counter buf_alloc
Definition: pgstat.h:257

References Assert, bgwriter_lru_maxpages, bgwriter_lru_multiplier, BgWriterDelay, PgStat_BgWriterStats::buf_alloc, BUF_REUSABLE, BUF_WRITTEN, PgStat_BgWriterStats::buf_written_clean, DEBUG1, DEBUG2, elog, PgStat_BgWriterStats::maxwritten_clean, NBuffers, PendingBgWriterStats, StrategySyncStart(), and SyncOneBuffer().

Referenced by BackgroundWriterMain().

◆ BufferGetBlock()

static Block BufferGetBlock ( Buffer  buffer)
inlinestatic

Definition at line 371 of file bufmgr.h.

372 {
373  Assert(BufferIsValid(buffer));
374 
375  if (BufferIsLocal(buffer))
376  return LocalBufferBlockPointers[-buffer - 1];
377  else
378  return (Block) (BufferBlocks + ((Size) (buffer - 1)) * BLCKSZ);
379 }
#define BufferIsLocal(buffer)
Definition: buf.h:37
PGDLLIMPORT Block * LocalBufferBlockPointers
Definition: localbuf.c:45
void * Block
Definition: bufmgr.h:25
PGDLLIMPORT char * BufferBlocks
Definition: buf_init.c:22
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:355
size_t Size
Definition: c.h:605

References Assert, BufferBlocks, BufferIsLocal, BufferIsValid(), and LocalBufferBlockPointers.

Referenced by BufferGetPage(), WaitReadBuffers(), and XLogSaveBufferForHint().

◆ BufferGetBlockNumber()

BlockNumber BufferGetBlockNumber ( Buffer  buffer)

Definition at line 3713 of file bufmgr.c.

3714 {
3715  BufferDesc *bufHdr;
3716 
3717  Assert(BufferIsPinned(buffer));
3718 
3719  if (BufferIsLocal(buffer))
3720  bufHdr = GetLocalBufferDescriptor(-buffer - 1);
3721  else
3722  bufHdr = GetBufferDescriptor(buffer - 1);
3723 
3724  /* pinned, so OK to read tag without spinlock */
3725  return bufHdr->tag.blockNum;
3726 }
static BufferDesc * GetLocalBufferDescriptor(uint32 id)
static BufferDesc * GetBufferDescriptor(uint32 id)
#define BufferIsPinned(bufnum)
Definition: bufmgr.c:473
BufferTag tag
BlockNumber blockNum
Definition: buf_internals.h:98

References Assert, buftag::blockNum, PrivateRefCountEntry::buffer, BufferIsLocal, BufferIsPinned, GetBufferDescriptor(), GetLocalBufferDescriptor(), and BufferDesc::tag.

Referenced by _bt_binsrch_insert(), _bt_bottomupdel_pass(), _bt_check_unique(), _bt_checkpage(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_doinsert(), _bt_endpoint(), _bt_finish_split(), _bt_first(), _bt_getroot(), _bt_insert_parent(), _bt_insertonpg(), _bt_mark_page_halfdead(), _bt_moveright(), _bt_newlevel(), _bt_pagedel(), _bt_readnextpage(), _bt_readpage(), _bt_restore_meta(), _bt_search(), _bt_simpledel_pass(), _bt_split(), _bt_unlink_halfdead_page(), _bt_walk_left(), _hash_addovflpage(), _hash_checkpage(), _hash_doinsert(), _hash_first(), _hash_freeovflpage(), _hash_getnewbuf(), _hash_readnext(), _hash_readpage(), _hash_splitbucket(), allocNewBuffer(), blinsert(), BloomInitMetapage(), brin_doinsert(), brin_doupdate(), brin_getinsertbuffer(), brin_initialize_empty_new_buffer(), brin_page_cleanup(), brin_xlog_insert_update(), brinbuild(), brinGetTupleForHeapBlock(), collectMatchBitmap(), createPostingTree(), dataBeginPlaceToPageLeaf(), dataPrepareDownlink(), doPickSplit(), entryPrepareDownlink(), fill_seq_fork_with_data(), ginEntryInsert(), ginFindParents(), ginFinishSplit(), ginPlaceToPage(), ginRedoDeleteListPages(), ginRedoUpdateMetapage(), ginScanToDelete(), gistbufferinginserttuples(), gistbuild(), gistcheckpage(), gistdeletepage(), gistformdownlink(), gistinserttuples(), gistMemorizeAllDownlinks(), gistplacetopage(), gistRelocateBuildBuffersOnSplit(), gistScanPage(), hash_xlog_add_ovfl_page(), heap_delete(), heap_fetch_next_buffer(), heap_hot_search_buffer(), heap_insert(), heap_multi_insert(), heap_page_is_all_visible(), heap_page_prune_and_freeze(), heap_prepare_pagescan(), heap_update(), heap_xlog_confirm(), heap_xlog_lock(), heapam_scan_analyze_next_block(), heapgettup(), heapgettup_pagemode(), index_compute_xid_horizon_for_tuples(), lazy_scan_noprune(), lazy_scan_prune(), makeSublist(), moveLeafs(), moveRightIfItNeeded(), pgstathashindex(), ReadBufferBI(), RelationAddBlocks(), RelationGetBufferForTuple(), RelationPutHeapTuple(), revmap_get_buffer(), revmap_physical_extend(), ScanSourceDatabasePgClassPage(), spgAddNodeAction(), spgbuild(), spgdoinsert(), SpGistSetLastUsedPage(), spgSplitNodeAction(), spgWalk(), startScanEntry(), terminate_brin_buildstate(), vacuumLeafPage(), visibilitymap_clear(), visibilitymap_get_status(), visibilitymap_pin(), visibilitymap_pin_ok(), visibilitymap_set(), and WaitReadBuffers().

◆ BufferGetLSNAtomic()

XLogRecPtr BufferGetLSNAtomic ( Buffer  buffer)

Definition at line 3974 of file bufmgr.c.

3975 {
3976  BufferDesc *bufHdr = GetBufferDescriptor(buffer - 1);
3977  char *page = BufferGetPage(buffer);
3978  XLogRecPtr lsn;
3979  uint32 buf_state;
3980 
3981  /*
3982  * If we don't need locking for correctness, fastpath out.
3983  */
3984  if (!XLogHintBitIsNeeded() || BufferIsLocal(buffer))
3985  return PageGetLSN(page);
3986 
3987  /* Make sure we've got a real buffer, and that we hold a pin on it. */
3988  Assert(BufferIsValid(buffer));
3989  Assert(BufferIsPinned(buffer));
3990 
3991  buf_state = LockBufHdr(bufHdr);
3992  lsn = PageGetLSN(page);
3993  UnlockBufHdr(bufHdr, buf_state);
3994 
3995  return lsn;
3996 }
static void UnlockBufHdr(BufferDesc *desc, uint32 buf_state)
uint32 LockBufHdr(BufferDesc *desc)
Definition: bufmgr.c:5734
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:404
static XLogRecPtr PageGetLSN(Page page)
Definition: bufpage.h:383
#define XLogHintBitIsNeeded()
Definition: xlog.h:118
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References Assert, PrivateRefCountEntry::buffer, BufferGetPage(), BufferIsLocal, BufferIsPinned, BufferIsValid(), GetBufferDescriptor(), LockBufHdr(), PageGetLSN(), UnlockBufHdr(), and XLogHintBitIsNeeded.

Referenced by _bt_killitems(), _bt_readpage(), gistdoinsert(), gistFindPath(), gistkillitems(), gistScanPage(), SetHintBits(), and XLogSaveBufferForHint().

◆ BufferGetPage()

static Page BufferGetPage ( Buffer  buffer)
inlinestatic

Definition at line 404 of file bufmgr.h.

405 {
406  return (Page) BufferGetBlock(buffer);
407 }
static Block BufferGetBlock(Buffer buffer)
Definition: bufmgr.h:371
Pointer Page
Definition: bufpage.h:78

References BufferGetBlock().

Referenced by _bt_allocbuf(), _bt_binsrch(), _bt_binsrch_insert(), _bt_bottomupdel_pass(), _bt_check_unique(), _bt_checkpage(), _bt_clear_incomplete_split(), _bt_conditionallockbuf(), _bt_dedup_pass(), _bt_delete_or_dedup_one_page(), _bt_delitems_delete(), _bt_delitems_delete_check(), _bt_delitems_vacuum(), _bt_endpoint(), _bt_findinsertloc(), _bt_finish_split(), _bt_get_endpoint(), _bt_getmeta(), _bt_getroot(), _bt_getstackbuf(), _bt_gettrueroot(), _bt_insert_parent(), _bt_insertonpg(), _bt_killitems(), _bt_leftsib_splitflag(), _bt_lock_subtree_parent(), _bt_lockbuf(), _bt_mark_page_halfdead(), _bt_moveright(), _bt_newlevel(), _bt_pagedel(), _bt_readnextpage(), _bt_readpage(), _bt_restore_meta(), _bt_rightsib_halfdeadflag(), _bt_search(), _bt_search_insert(), _bt_set_cleanup_info(), _bt_simpledel_pass(), _bt_split(), _bt_stepright(), _bt_unlink_halfdead_page(), _bt_unlockbuf(), _bt_upgradelockbufcleanup(), _bt_vacuum_needs_cleanup(), _bt_walk_left(), _hash_addovflpage(), _hash_checkpage(), _hash_doinsert(), _hash_expandtable(), _hash_finish_split(), _hash_first(), _hash_freeovflpage(), _hash_get_newblock_from_oldbucket(), _hash_get_oldblock_from_newbucket(), _hash_getbucketbuf_from_hashkey(), _hash_getcachedmetap(), _hash_getinitbuf(), _hash_getnewbuf(), _hash_init(), _hash_init_metabuffer(), _hash_initbitmapbuffer(), _hash_initbuf(), _hash_kill_items(), _hash_pgaddmultitup(), _hash_pgaddtup(), _hash_readnext(), _hash_readpage(), _hash_readprev(), _hash_splitbucket(), _hash_squeezebucket(), _hash_vacuum_one_page(), allocNewBuffer(), blgetbitmap(), blinsert(), BloomNewBuffer(), blvacuumcleanup(), brin_can_do_samepage_update(), brin_doinsert(), brin_doupdate(), brin_evacuate_page(), brin_getinsertbuffer(), brin_initialize_empty_new_buffer(), brin_page_cleanup(), brin_start_evacuating_page(), brin_xlog_createidx(), brin_xlog_desummarize_page(), brin_xlog_insert_update(), brin_xlog_revmap_extend(), brin_xlog_samepage_update(), brin_xlog_update(), brinbuild(), brinbuildempty(), brinGetStats(), brinGetTupleForHeapBlock(), brininsert(), brinRevmapDesummarizeRange(), brinRevmapInitialize(), brinSetHeapBlockItemptr(), bt_metap(), bt_page_items_internal(), bt_recheck_sibling_links(), bt_rootdescend(), btree_xlog_dedup(), btree_xlog_delete(), btree_xlog_insert(), btree_xlog_mark_page_halfdead(), btree_xlog_newroot(), btree_xlog_split(), btree_xlog_unlink_page(), btree_xlog_vacuum(), btvacuumpage(), BufferGetLSNAtomic(), collect_corrupt_items(), collect_visibility_data(), collectMatchBitmap(), collectMatchesForHeapRow(), count_nondeletable_pages(), createPostingTree(), dataBeginPlaceToPage(), dataBeginPlaceToPageInternal(), dataBeginPlaceToPageLeaf(), dataExecPlaceToPage(), dataExecPlaceToPageInternal(), dataLocateItem(), dataPlaceToPageLeafRecompress(), dataPrepareDownlink(), dataSplitPageInternal(), do_setval(), doPickSplit(), entryExecPlaceToPage(), entryIsEnoughSpace(), entryLoadMoreItems(), entryLocateEntry(), entryLocateLeafEntry(), entryPrepareDownlink(), entrySplitPage(), fill_seq_fork_with_data(), FreeSpaceMapPrepareTruncateRel(), fsm_readbuf(), fsm_search(), fsm_search_avail(), fsm_set_and_search(), fsm_vacuum_page(), generic_redo(), GenericXLogFinish(), GenericXLogRegisterBuffer(), get_raw_page_internal(), GetBTPageStatistics(), GetRecordedFreeSpace(), GetVisibilityMapPins(), ginbulkdelete(), ginDeletePage(), ginEntryInsert(), ginFindLeafPage(), ginFindParents(), ginFinishOldSplit(), ginFinishSplit(), ginGetStats(), ginHeapTupleFastInsert(), GinInitBuffer(), GinInitMetabuffer(), ginInsertCleanup(), ginInsertValue(), GinNewBuffer(), ginPlaceToPage(), ginRedoClearIncompleteSplit(), ginRedoCreatePTree(), ginRedoDeleteListPages(), ginRedoDeletePage(), ginRedoInsert(), ginRedoInsertData(), ginRedoInsertEntry(), ginRedoInsertListPage(), ginRedoUpdateMetapage(), ginRedoVacuumDataLeafPage(), ginScanToDelete(), ginStepRight(), ginTraverseLock(), ginUpdateStats(), ginvacuumcleanup(), ginVacuumEntryPage(), ginVacuumPostingTreeLeaf(), ginVacuumPostingTreeLeaves(), gistBufferingFindCorrectParent(), gistbufferinginserttuples(), gistbuild(), gistcheckpage(), gistdeletepage(), gistdoinsert(), gistFindCorrectParent(), gistFindPath(), gistfixsplit(), gistformdownlink(), gistGetMaxLevel(), GISTInitBuffer(), gistkillitems(), gistMemorizeAllDownlinks(), gistNewBuffer(), gistplacetopage(), gistProcessItup(), gistRedoClearFollowRight(), gistRedoDeleteRecord(), gistRedoPageDelete(), gistRedoPageSplitRecord(), gistRedoPageUpdateRecord(), gistScanPage(), gistvacuum_delete_empty_pages(), gistvacuumpage(), hash_bitmap_info(), hash_xlog_add_ovfl_page(), hash_xlog_delete(), hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), hash_xlog_insert(), hash_xlog_move_page_contents(), hash_xlog_split_allocate_page(), hash_xlog_split_cleanup(), hash_xlog_split_complete(), hash_xlog_squeeze_page(), hash_xlog_update_meta_page(), hash_xlog_vacuum_one_page(), hashbucketcleanup(), hashbulkdelete(), heap_abort_speculative(), heap_delete(), heap_fetch(), heap_finish_speculative(), heap_force_common(), heap_freeze_prepared_tuples(), heap_get_latest_tid(), heap_hot_search_buffer(), heap_index_delete_tuples(), heap_inplace_update(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_page_is_all_visible(), heap_page_prune_and_freeze(), heap_page_prune_execute(), heap_page_prune_opt(), heap_pre_freeze_checks(), heap_prepare_pagescan(), heap_update(), heap_xlog_confirm(), heap_xlog_delete(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_prune_freeze(), heap_xlog_update(), heap_xlog_visible(), heapam_index_build_range_scan(), heapam_index_validate_scan(), heapam_scan_analyze_next_tuple(), heapam_scan_bitmap_next_block(), heapam_scan_bitmap_next_tuple(), heapam_scan_sample_next_tuple(), heapgettup_continue_page(), heapgettup_pagemode(), heapgettup_start_page(), index_compute_xid_horizon_for_tuples(), initBloomState(), lazy_scan_heap(), lazy_vacuum_heap_page(), lazy_vacuum_heap_rel(), log_heap_prune_and_freeze(), log_heap_update(), log_newpage_buffer(), log_newpage_range(), log_split_page(), MarkBufferDirtyHint(), moveLeafs(), moveRightIfItNeeded(), nextval_internal(), palloc_btree_page(), pg_visibility(), pgstat_btree_page(), pgstat_gist_page(), pgstat_hash_page(), pgstat_heap(), pgstatginindex_internal(), pgstathashindex(), pgstatindex_impl(), read_seq_tuple(), RelationAddBlocks(), RelationCopyStorageUsingBuffer(), RelationGetBufferForTuple(), RelationPutHeapTuple(), revmap_physical_extend(), scanGetCandidate(), scanPendingInsert(), scanPostingTree(), ScanSourceDatabasePgClass(), seq_redo(), shiftList(), spgAddNodeAction(), spgbuild(), spgdoinsert(), spgGetCache(), SpGistGetBuffer(), SpGistInitBuffer(), SpGistNewBuffer(), SpGistSetLastUsedPage(), SpGistUpdateMetaPage(), spgprocesspending(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), spgRedoSplitTuple(), spgRedoVacuumLeaf(), spgRedoVacuumRedirect(), spgRedoVacuumRoot(), spgSplitNodeAction(), spgvacuumpage(), spgWalk(), startScanEntry(), statapprox_heap(), terminate_brin_buildstate(), vacuumLeafPage(), vacuumLeafRoot(), vacuumRedirectAndPlaceholder(), verify_heapam(), verifyBackupPageConsistency(), visibilitymap_clear(), visibilitymap_count(), visibilitymap_get_status(), visibilitymap_prepare_truncate(), visibilitymap_set(), vm_readbuf(), writeListPage(), XLogCheckBufferNeedsBackup(), XLogReadBufferExtended(), XLogReadBufferForRedoExtended(), XLogRecordPageWithFreeSpace(), XLogRegisterBuffer(), XLogSaveBufferForHint(), xlogVacuumPage(), and ZeroAndLockBuffer().

◆ BufferGetPageSize()

◆ BufferGetTag()

void BufferGetTag ( Buffer  buffer,
RelFileLocator rlocator,
ForkNumber forknum,
BlockNumber blknum 
)

Definition at line 3734 of file bufmgr.c.

3736 {
3737  BufferDesc *bufHdr;
3738 
3739  /* Do the same checks as BufferGetBlockNumber. */
3740  Assert(BufferIsPinned(buffer));
3741 
3742  if (BufferIsLocal(buffer))
3743  bufHdr = GetLocalBufferDescriptor(-buffer - 1);
3744  else
3745  bufHdr = GetBufferDescriptor(buffer - 1);
3746 
3747  /* pinned, so OK to read tag without spinlock */
3748  *rlocator = BufTagGetRelFileLocator(&bufHdr->tag);
3749  *forknum = BufTagGetForkNum(&bufHdr->tag);
3750  *blknum = bufHdr->tag.blockNum;
3751 }
static ForkNumber BufTagGetForkNum(const BufferTag *tag)
static RelFileLocator BufTagGetRelFileLocator(const BufferTag *tag)

References Assert, buftag::blockNum, PrivateRefCountEntry::buffer, BufferIsLocal, BufferIsPinned, BufTagGetForkNum(), BufTagGetRelFileLocator(), GetBufferDescriptor(), GetLocalBufferDescriptor(), and BufferDesc::tag.

Referenced by fsm_search_avail(), ginRedoInsertEntry(), log_newpage_buffer(), ResolveCminCmaxDuringDecoding(), XLogRegisterBuffer(), and XLogSaveBufferForHint().

◆ BufferIsDirty()

bool BufferIsDirty ( Buffer  buffer)

Definition at line 2488 of file bufmgr.c.

2489 {
2490  BufferDesc *bufHdr;
2491 
2492  if (BufferIsLocal(buffer))
2493  {
2494  int bufid = -buffer - 1;
2495 
2496  bufHdr = GetLocalBufferDescriptor(bufid);
2497  }
2498  else
2499  {
2500  bufHdr = GetBufferDescriptor(buffer - 1);
2501  }
2502 
2503  Assert(BufferIsPinned(buffer));
2505  LW_EXCLUSIVE));
2506 
2507  return pg_atomic_read_u32(&bufHdr->state) & BM_DIRTY;
2508 }
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:234
#define BM_DIRTY
Definition: buf_internals.h:61
static LWLock * BufferDescriptorGetContentLock(const BufferDesc *bdesc)
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1939
@ LW_EXCLUSIVE
Definition: lwlock.h:114
pg_atomic_uint32 state

References Assert, BM_DIRTY, PrivateRefCountEntry::buffer, BufferDescriptorGetContentLock(), BufferIsLocal, BufferIsPinned, GetBufferDescriptor(), GetLocalBufferDescriptor(), LW_EXCLUSIVE, LWLockHeldByMeInMode(), pg_atomic_read_u32(), and BufferDesc::state.

Referenced by XLogRegisterBuffer().

◆ BufferIsExclusiveLocked()

bool BufferIsExclusiveLocked ( Buffer  buffer)

Definition at line 2459 of file bufmgr.c.

2460 {
2461  BufferDesc *bufHdr;
2462 
2463  if (BufferIsLocal(buffer))
2464  {
2465  int bufid = -buffer - 1;
2466 
2467  bufHdr = GetLocalBufferDescriptor(bufid);
2468  }
2469  else
2470  {
2471  bufHdr = GetBufferDescriptor(buffer - 1);
2472  }
2473 
2474  Assert(BufferIsPinned(buffer));
2476  LW_EXCLUSIVE);
2477 }

References Assert, PrivateRefCountEntry::buffer, BufferDescriptorGetContentLock(), BufferIsLocal, BufferIsPinned, GetBufferDescriptor(), GetLocalBufferDescriptor(), LW_EXCLUSIVE, and LWLockHeldByMeInMode().

Referenced by XLogRegisterBuffer().

◆ BufferIsPermanent()

bool BufferIsPermanent ( Buffer  buffer)

Definition at line 3944 of file bufmgr.c.

3945 {
3946  BufferDesc *bufHdr;
3947 
3948  /* Local buffers are used only for temp relations. */
3949  if (BufferIsLocal(buffer))
3950  return false;
3951 
3952  /* Make sure we've got a real buffer, and that we hold a pin on it. */
3953  Assert(BufferIsValid(buffer));
3954  Assert(BufferIsPinned(buffer));
3955 
3956  /*
3957  * BM_PERMANENT can't be changed while we hold a pin on the buffer, so we
3958  * need not bother with the buffer header spinlock. Even if someone else
3959  * changes the buffer header state while we're doing this, the state is
3960  * changed atomically, so we'll read the old value or the new value, but
3961  * not random garbage.
3962  */
3963  bufHdr = GetBufferDescriptor(buffer - 1);
3964  return (pg_atomic_read_u32(&bufHdr->state) & BM_PERMANENT) != 0;
3965 }
#define BM_PERMANENT
Definition: buf_internals.h:69

References Assert, BM_PERMANENT, PrivateRefCountEntry::buffer, BufferIsLocal, BufferIsPinned, BufferIsValid(), GetBufferDescriptor(), pg_atomic_read_u32(), and BufferDesc::state.

Referenced by SetHintBits().

◆ BufferIsValid()

static bool BufferIsValid ( Buffer  bufnum)
inlinestatic

Definition at line 355 of file bufmgr.h.

356 {
357  Assert(bufnum <= NBuffers);
358  Assert(bufnum >= -NLocBuffer);
359 
360  return bufnum != InvalidBuffer;
361 }
#define InvalidBuffer
Definition: buf.h:25
PGDLLIMPORT int NBuffers
Definition: globals.c:139
PGDLLIMPORT int NLocBuffer
Definition: localbuf.c:42

References Assert, InvalidBuffer, NBuffers, and NLocBuffer.

Referenced by _bt_clear_incomplete_split(), _bt_endpoint(), _bt_first(), _bt_get_endpoint(), _bt_insertonpg(), _bt_readpage(), _bt_relandgetbuf(), _bt_search(), _bt_unlink_halfdead_page(), _hash_addovflpage(), _hash_dropscanbuf(), _hash_freeovflpage(), _hash_getbucketbuf_from_hashkey(), _hash_getcachedmetap(), _hash_readnext(), _hash_readpage(), _hash_readprev(), autoprewarm_database_main(), brin_doinsert(), brin_doupdate(), brin_getinsertbuffer(), brin_xlog_createidx(), brin_xlog_desummarize_page(), brin_xlog_insert_update(), brin_xlog_revmap_extend(), brin_xlog_samepage_update(), brin_xlog_update(), brinGetTupleForHeapBlock(), brininsert(), brinsummarize(), bt_recheck_sibling_links(), bt_rootdescend(), btree_xlog_dedup(), btree_xlog_delete(), btree_xlog_insert(), btree_xlog_mark_page_halfdead(), btree_xlog_split(), btree_xlog_unlink_page(), btree_xlog_vacuum(), BufferGetBlock(), BufferGetLSNAtomic(), BufferGetPageSize(), BufferIsPermanent(), ConditionalLockBufferForCleanup(), DebugPrintBufferRefcount(), doPickSplit(), entryGetItem(), entryLoadMoreItems(), ExecStoreBufferHeapTuple(), ExecStorePinnedBufferHeapTuple(), FreeSpaceMapPrepareTruncateRel(), fsm_search(), fsm_vacuum_page(), generic_redo(), GetPrivateRefCount(), GetPrivateRefCountEntry(), GetRecordedFreeSpace(), GetVisibilityMapPins(), ginPlaceToPage(), ginRedoClearIncompleteSplit(), ginRedoDeletePage(), ginRedoInsert(), ginRedoUpdateMetapage(), ginRedoVacuumDataLeafPage(), ginScanToDelete(), gistinserttuples(), gistkillitems(), gistplacetopage(), gistRedoClearFollowRight(), gistRedoDeleteRecord(), gistRedoPageDelete(), gistRedoPageUpdateRecord(), gistXLogSplit(), gistXLogUpdate(), hash_xlog_add_ovfl_page(), hash_xlog_delete(), hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), hash_xlog_insert(), hash_xlog_move_page_contents(), hash_xlog_split_allocate_page(), hash_xlog_split_cleanup(), hash_xlog_split_complete(), hash_xlog_squeeze_page(), hash_xlog_update_meta_page(), hash_xlog_vacuum_one_page(), heap_endscan(), heap_fetch_next_buffer(), heap_index_delete_tuples(), heap_lock_tuple(), heap_rescan(), heap_update(), heap_vac_scan_next_block(), heap_xlog_confirm(), heap_xlog_delete(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_prune_freeze(), heap_xlog_update(), heap_xlog_visible(), heapam_index_fetch_reset(), heapam_scan_analyze_next_block(), heapam_scan_sample_next_block(), heapam_tuple_lock(), heapam_tuple_satisfies_snapshot(), heapgettup(), heapgettup_continue_page(), heapgettup_pagemode(), heapgettup_start_page(), IsBufferCleanupOK(), lazy_scan_heap(), lazy_vacuum_heap_rel(), log_heap_visible(), MarkBufferDirty(), MarkBufferDirtyHint(), read_stream_next_buffer(), ReadRecentBuffer(), ReleaseAndReadBuffer(), ReleaseBuffer(), ResOwnerReleaseBufferPin(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), spgRedoSplitTuple(), spgRedoVacuumLeaf(), spgRedoVacuumRedirect(), spgRedoVacuumRoot(), statapprox_heap(), tts_buffer_heap_clear(), tts_buffer_heap_copyslot(), tts_buffer_heap_materialize(), tts_buffer_heap_store_tuple(), verifyBackupPageConsistency(), visibilitymap_clear(), visibilitymap_count(), visibilitymap_get_status(), visibilitymap_pin(), visibilitymap_pin_ok(), visibilitymap_prepare_truncate(), visibilitymap_set(), XLogPrefetcherNextBlock(), XLogReadBufferExtended(), and XLogReadBufferForRedoExtended().

◆ BufferShmemSize()

Size BufferShmemSize ( void  )

Definition at line 160 of file buf_init.c.

161 {
162  Size size = 0;
163 
164  /* size of buffer descriptors */
166  /* to allow aligning buffer descriptors */
168 
169  /* size of data pages, plus alignment padding */
171  size = add_size(size, mul_size(NBuffers, BLCKSZ));
172 
173  /* size of stuff controlled by freelist.c */
175 
176  /* size of I/O condition variables */
179  /* to allow aligning the above */
181 
182  /* size of checkpoint sort array in bufmgr.c */
184 
185  return size;
186 }
Size StrategyShmemSize(void)
Definition: freelist.c:453
#define PG_IO_ALIGN_SIZE
#define PG_CACHE_LINE_SIZE
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510
static pg_noinline void Size size
Definition: slab.c:607

References add_size(), mul_size(), NBuffers, PG_CACHE_LINE_SIZE, PG_IO_ALIGN_SIZE, size, and StrategyShmemSize().

Referenced by CalculateShmemSize().

◆ CheckBufferIsPinnedOnce()

void CheckBufferIsPinnedOnce ( Buffer  buffer)

Definition at line 5178 of file bufmgr.c.

5179 {
5180  if (BufferIsLocal(buffer))
5181  {
5182  if (LocalRefCount[-buffer - 1] != 1)
5183  elog(ERROR, "incorrect local pin count: %d",
5184  LocalRefCount[-buffer - 1]);
5185  }
5186  else
5187  {
5188  if (GetPrivateRefCount(buffer) != 1)
5189  elog(ERROR, "incorrect local pin count: %d",
5190  GetPrivateRefCount(buffer));
5191  }
5192 }
static int32 GetPrivateRefCount(Buffer buffer)
Definition: bufmgr.c:415
#define ERROR
Definition: elog.h:39
int32 * LocalRefCount
Definition: localbuf.c:46

References PrivateRefCountEntry::buffer, BufferIsLocal, elog, ERROR, GetPrivateRefCount(), and LocalRefCount.

Referenced by GetVictimBuffer(), and LockBufferForCleanup().

◆ CheckPointBuffers()

void CheckPointBuffers ( int  flags)

Definition at line 3699 of file bufmgr.c.

3700 {
3701  BufferSync(flags);
3702 }
static void BufferSync(int flags)
Definition: bufmgr.c:2901

References BufferSync().

Referenced by CheckPointGuts().

◆ ConditionalLockBuffer()

bool ConditionalLockBuffer ( Buffer  buffer)

Definition at line 5157 of file bufmgr.c.

5158 {
5159  BufferDesc *buf;
5160 
5161  Assert(BufferIsPinned(buffer));
5162  if (BufferIsLocal(buffer))
5163  return true; /* act as though we got it */
5164 
5165  buf = GetBufferDescriptor(buffer - 1);
5166 
5168  LW_EXCLUSIVE);
5169 }
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1341
static char * buf
Definition: pg_test_fsync.c:73

References Assert, buf, PrivateRefCountEntry::buffer, BufferDescriptorGetContentLock(), BufferIsLocal, BufferIsPinned, GetBufferDescriptor(), LW_EXCLUSIVE, and LWLockConditionalAcquire().

Referenced by _bt_conditionallockbuf(), BloomNewBuffer(), ConditionalLockBufferForCleanup(), GinNewBuffer(), gistNewBuffer(), RelationGetBufferForTuple(), spgdoinsert(), SpGistGetBuffer(), SpGistNewBuffer(), and SpGistUpdateMetaPage().

◆ ConditionalLockBufferForCleanup()

bool ConditionalLockBufferForCleanup ( Buffer  buffer)

Definition at line 5372 of file bufmgr.c.

5373 {
5374  BufferDesc *bufHdr;
5375  uint32 buf_state,
5376  refcount;
5377 
5378  Assert(BufferIsValid(buffer));
5379 
5380  if (BufferIsLocal(buffer))
5381  {
5382  refcount = LocalRefCount[-buffer - 1];
5383  /* There should be exactly one pin */
5384  Assert(refcount > 0);
5385  if (refcount != 1)
5386  return false;
5387  /* Nobody else to wait for */
5388  return true;
5389  }
5390 
5391  /* There should be exactly one local pin */
5392  refcount = GetPrivateRefCount(buffer);
5393  Assert(refcount);
5394  if (refcount != 1)
5395  return false;
5396 
5397  /* Try to acquire lock */
5398  if (!ConditionalLockBuffer(buffer))
5399  return false;
5400 
5401  bufHdr = GetBufferDescriptor(buffer - 1);
5402  buf_state = LockBufHdr(bufHdr);
5403  refcount = BUF_STATE_GET_REFCOUNT(buf_state);
5404 
5405  Assert(refcount > 0);
5406  if (refcount == 1)
5407  {
5408  /* Successfully acquired exclusive lock with pincount 1 */
5409  UnlockBufHdr(bufHdr, buf_state);
5410  return true;
5411  }
5412 
5413  /* Failed, so release the lock */
5414  UnlockBufHdr(bufHdr, buf_state);
5415  LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
5416  return false;
5417 }
#define BUF_STATE_GET_REFCOUNT(state)
Definition: buf_internals.h:51
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:5157
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5131
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:193

References Assert, BUF_STATE_GET_REFCOUNT, PrivateRefCountEntry::buffer, BUFFER_LOCK_UNLOCK, BufferIsLocal, BufferIsValid(), ConditionalLockBuffer(), GetBufferDescriptor(), GetPrivateRefCount(), LocalRefCount, LockBuffer(), LockBufHdr(), PrivateRefCountEntry::refcount, and UnlockBufHdr().

Referenced by _hash_finish_split(), _hash_getbuf_with_condlock_cleanup(), heap_page_prune_opt(), and lazy_scan_heap().

◆ CreateAndCopyRelationData()

void CreateAndCopyRelationData ( RelFileLocator  src_rlocator,
RelFileLocator  dst_rlocator,
bool  permanent 
)

Definition at line 4770 of file bufmgr.c.

4772 {
4773  char relpersistence;
4774  SMgrRelation src_rel;
4775  SMgrRelation dst_rel;
4776 
4777  /* Set the relpersistence. */
4778  relpersistence = permanent ?
4779  RELPERSISTENCE_PERMANENT : RELPERSISTENCE_UNLOGGED;
4780 
4781  src_rel = smgropen(src_rlocator, INVALID_PROC_NUMBER);
4782  dst_rel = smgropen(dst_rlocator, INVALID_PROC_NUMBER);
4783 
4784  /*
4785  * Create and copy all forks of the relation. During create database we
4786  * have a separate cleanup mechanism which deletes complete database
4787  * directory. Therefore, each individual relation doesn't need to be
4788  * registered for cleanup.
4789  */
4790  RelationCreateStorage(dst_rlocator, relpersistence, false);
4791 
4792  /* copy main fork. */
4793  RelationCopyStorageUsingBuffer(src_rlocator, dst_rlocator, MAIN_FORKNUM,
4794  permanent);
4795 
4796  /* copy those extra forks that exist */
4797  for (ForkNumber forkNum = MAIN_FORKNUM + 1;
4798  forkNum <= MAX_FORKNUM; forkNum++)
4799  {
4800  if (smgrexists(src_rel, forkNum))
4801  {
4802  smgrcreate(dst_rel, forkNum, false);
4803 
4804  /*
4805  * WAL log creation if the relation is persistent, or this is the
4806  * init fork of an unlogged relation.
4807  */
4808  if (permanent || forkNum == INIT_FORKNUM)
4809  log_smgrcreate(&dst_rlocator, forkNum);
4810 
4811  /* Copy a fork's data, block by block. */
4812  RelationCopyStorageUsingBuffer(src_rlocator, dst_rlocator, forkNum,
4813  permanent);
4814  }
4815  }
4816 }
static void RelationCopyStorageUsingBuffer(RelFileLocator srclocator, RelFileLocator dstlocator, ForkNumber forkNum, bool permanent)
Definition: bufmgr.c:4679
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
ForkNumber
Definition: relpath.h:48
@ MAIN_FORKNUM
Definition: relpath.h:50
@ INIT_FORKNUM
Definition: relpath.h:53
#define MAX_FORKNUM
Definition: relpath.h:62
SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend)
Definition: smgr.c:198
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:411
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:398
SMgrRelation RelationCreateStorage(RelFileLocator rlocator, char relpersistence, bool register_delete)
Definition: storage.c:121
void log_smgrcreate(const RelFileLocator *rlocator, ForkNumber forkNum)
Definition: storage.c:186

References INIT_FORKNUM, INVALID_PROC_NUMBER, log_smgrcreate(), MAIN_FORKNUM, MAX_FORKNUM, RelationCopyStorageUsingBuffer(), RelationCreateStorage(), smgrcreate(), smgrexists(), and smgropen().

Referenced by CreateDatabaseUsingWalLog().

◆ DebugPrintBufferRefcount()

char* DebugPrintBufferRefcount ( Buffer  buffer)

Definition at line 3654 of file bufmgr.c.

3655 {
3656  BufferDesc *buf;
3657  int32 loccount;
3658  char *path;
3659  char *result;
3660  ProcNumber backend;
3661  uint32 buf_state;
3662 
3663  Assert(BufferIsValid(buffer));
3664  if (BufferIsLocal(buffer))
3665  {
3666  buf = GetLocalBufferDescriptor(-buffer - 1);
3667  loccount = LocalRefCount[-buffer - 1];
3668  backend = MyProcNumber;
3669  }
3670  else
3671  {
3672  buf = GetBufferDescriptor(buffer - 1);
3673  loccount = GetPrivateRefCount(buffer);
3674  backend = INVALID_PROC_NUMBER;
3675  }
3676 
3677  /* theoretically we should lock the bufhdr here */
3678  path = relpathbackend(BufTagGetRelFileLocator(&buf->tag), backend,
3679  BufTagGetForkNum(&buf->tag));
3680  buf_state = pg_atomic_read_u32(&buf->state);
3681 
3682  result = psprintf("[%03d] (rel=%s, blockNum=%u, flags=0x%x, refcount=%u %d)",
3683  buffer, path,
3684  buf->tag.blockNum, buf_state & BUF_FLAG_MASK,
3685  BUF_STATE_GET_REFCOUNT(buf_state), loccount);
3686  pfree(path);
3687  return result;
3688 }
#define BUF_FLAG_MASK
Definition: buf_internals.h:48
ProcNumber MyProcNumber
Definition: globals.c:87
void pfree(void *pointer)
Definition: mcxt.c:1520
int ProcNumber
Definition: procnumber.h:24
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define relpathbackend(rlocator, backend, forknum)
Definition: relpath.h:85

References Assert, buf, BUF_FLAG_MASK, BUF_STATE_GET_REFCOUNT, PrivateRefCountEntry::buffer, BufferIsLocal, BufferIsValid(), BufTagGetForkNum(), BufTagGetRelFileLocator(), GetBufferDescriptor(), GetLocalBufferDescriptor(), GetPrivateRefCount(), INVALID_PROC_NUMBER, LocalRefCount, MyProcNumber, pfree(), pg_atomic_read_u32(), psprintf(), and relpathbackend.

Referenced by CheckForBufferLeaks(), CheckForLocalBufferLeaks(), and ResOwnerPrintBufferPin().

◆ DropDatabaseBuffers()

void DropDatabaseBuffers ( Oid  dbid)

Definition at line 4375 of file bufmgr.c.

4376 {
4377  int i;
4378 
4379  /*
4380  * We needn't consider local buffers, since by assumption the target
4381  * database isn't our own.
4382  */
4383 
4384  for (i = 0; i < NBuffers; i++)
4385  {
4386  BufferDesc *bufHdr = GetBufferDescriptor(i);
4387  uint32 buf_state;
4388 
4389  /*
4390  * As in DropRelationBuffers, an unlocked precheck should be safe and
4391  * saves some cycles.
4392  */
4393  if (bufHdr->tag.dbOid != dbid)
4394  continue;
4395 
4396  buf_state = LockBufHdr(bufHdr);
4397  if (bufHdr->tag.dbOid == dbid)
4398  InvalidateBuffer(bufHdr); /* releases spinlock */
4399  else
4400  UnlockBufHdr(bufHdr, buf_state);
4401  }
4402 }
static void InvalidateBuffer(BufferDesc *buf)
Definition: bufmgr.c:1772
int i
Definition: isn.c:73
Oid dbOid
Definition: buf_internals.h:95

References buftag::dbOid, GetBufferDescriptor(), i, InvalidateBuffer(), LockBufHdr(), NBuffers, BufferDesc::tag, and UnlockBufHdr().

Referenced by createdb_failure_callback(), dbase_redo(), dropdb(), and movedb().

◆ DropRelationBuffers()

void DropRelationBuffers ( struct SMgrRelationData smgr_reln,
ForkNumber forkNum,
int  nforks,
BlockNumber firstDelBlock 
)

◆ DropRelationsAllBuffers()

void DropRelationsAllBuffers ( struct SMgrRelationData **  smgr_reln,
int  nlocators 
)

◆ EvictUnpinnedBuffer()

bool EvictUnpinnedBuffer ( Buffer  buf)

Definition at line 6069 of file bufmgr.c.

6070 {
6071  BufferDesc *desc;
6072  uint32 buf_state;
6073  bool result;
6074 
6075  /* Make sure we can pin the buffer. */
6078 
6080  desc = GetBufferDescriptor(buf - 1);
6081 
6082  /* Lock the header and check if it's valid. */
6083  buf_state = LockBufHdr(desc);
6084  if ((buf_state & BM_VALID) == 0)
6085  {
6086  UnlockBufHdr(desc, buf_state);
6087  return false;
6088  }
6089 
6090  /* Check that it's not pinned already. */
6091  if (BUF_STATE_GET_REFCOUNT(buf_state) > 0)
6092  {
6093  UnlockBufHdr(desc, buf_state);
6094  return false;
6095  }
6096 
6097  PinBuffer_Locked(desc); /* releases spinlock */
6098 
6099  /* If it was dirty, try to clean it once. */
6100  if (buf_state & BM_DIRTY)
6101  {
6105  }
6106 
6107  /* This will return false if it becomes dirty or someone else pins it. */
6108  result = InvalidateVictimBuffer(desc);
6109 
6110  UnpinBuffer(desc);
6111 
6112  return result;
6113 }
#define BM_VALID
Definition: buf_internals.h:62
static void FlushBuffer(BufferDesc *buf, SMgrRelation reln, IOObject io_object, IOContext io_context)
Definition: bufmgr.c:3773
static void PinBuffer_Locked(BufferDesc *buf)
Definition: bufmgr.c:2752
static bool InvalidateVictimBuffer(BufferDesc *buf_hdr)
Definition: bufmgr.c:1870
static void ReservePrivateRefCountEntry(void)
Definition: bufmgr.c:249
static void UnpinBuffer(BufferDesc *buf)
Definition: bufmgr.c:2795
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1170
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1783
@ LW_SHARED
Definition: lwlock.h:115
@ IOOBJECT_RELATION
Definition: pgstat.h:280
@ IOCONTEXT_NORMAL
Definition: pgstat.h:290
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
void ResourceOwnerEnlarge(ResourceOwner owner)
Definition: resowner.c:442

References Assert, BM_DIRTY, BM_VALID, buf, BUF_STATE_GET_REFCOUNT, BufferDescriptorGetContentLock(), BufferIsLocal, CurrentResourceOwner, FlushBuffer(), GetBufferDescriptor(), InvalidateVictimBuffer(), IOCONTEXT_NORMAL, IOOBJECT_RELATION, LockBufHdr(), LW_SHARED, LWLockAcquire(), LWLockRelease(), PinBuffer_Locked(), ReservePrivateRefCountEntry(), ResourceOwnerEnlarge(), UnlockBufHdr(), and UnpinBuffer().

Referenced by pg_buffercache_evict().

◆ ExtendBufferedRel()

Buffer ExtendBufferedRel ( BufferManagerRelation  bmr,
ForkNumber  forkNum,
BufferAccessStrategy  strategy,
uint32  flags 
)

Definition at line 845 of file bufmgr.c.

849 {
850  Buffer buf;
851  uint32 extend_by = 1;
852 
853  ExtendBufferedRelBy(bmr, forkNum, strategy, flags, extend_by,
854  &buf, &extend_by);
855 
856  return buf;
857 }
int Buffer
Definition: buf.h:23
BlockNumber ExtendBufferedRelBy(BufferManagerRelation bmr, ForkNumber fork, BufferAccessStrategy strategy, uint32 flags, uint32 extend_by, Buffer *buffers, uint32 *extended_by)
Definition: bufmgr.c:877

References buf, and ExtendBufferedRelBy().

Referenced by _bt_allocbuf(), _hash_getnewbuf(), BloomNewBuffer(), brinbuild(), brinbuildempty(), fill_seq_fork_with_data(), ginbuildempty(), GinNewBuffer(), gistbuildempty(), gistNewBuffer(), ReadBuffer_common(), revmap_physical_extend(), and SpGistNewBuffer().

◆ ExtendBufferedRelBy()

BlockNumber ExtendBufferedRelBy ( BufferManagerRelation  bmr,
ForkNumber  fork,
BufferAccessStrategy  strategy,
uint32  flags,
uint32  extend_by,
Buffer buffers,
uint32 extended_by 
)

Definition at line 877 of file bufmgr.c.

884 {
885  Assert((bmr.rel != NULL) != (bmr.smgr != NULL));
886  Assert(bmr.smgr == NULL || bmr.relpersistence != 0);
887  Assert(extend_by > 0);
888 
889  if (bmr.smgr == NULL)
890  {
891  bmr.smgr = RelationGetSmgr(bmr.rel);
892  bmr.relpersistence = bmr.rel->rd_rel->relpersistence;
893  }
894 
895  return ExtendBufferedRelCommon(bmr, fork, strategy, flags,
896  extend_by, InvalidBlockNumber,
897  buffers, extended_by);
898 }
#define InvalidBlockNumber
Definition: block.h:33
static BlockNumber ExtendBufferedRelCommon(BufferManagerRelation bmr, ForkNumber fork, BufferAccessStrategy strategy, uint32 flags, uint32 extend_by, BlockNumber extend_upto, Buffer *buffers, uint32 *extended_by)
Definition: bufmgr.c:2135
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:567
struct SMgrRelationData * smgr
Definition: bufmgr.h:103
Form_pg_class rd_rel
Definition: rel.h:111

References Assert, ExtendBufferedRelCommon(), InvalidBlockNumber, RelationData::rd_rel, BufferManagerRelation::rel, RelationGetSmgr(), BufferManagerRelation::relpersistence, and BufferManagerRelation::smgr.

Referenced by ExtendBufferedRel(), and RelationAddBlocks().

◆ ExtendBufferedRelTo()

Buffer ExtendBufferedRelTo ( BufferManagerRelation  bmr,
ForkNumber  fork,
BufferAccessStrategy  strategy,
uint32  flags,
BlockNumber  extend_to,
ReadBufferMode  mode 
)

Definition at line 909 of file bufmgr.c.

915 {
917  uint32 extended_by = 0;
918  Buffer buffer = InvalidBuffer;
919  Buffer buffers[64];
920 
921  Assert((bmr.rel != NULL) != (bmr.smgr != NULL));
922  Assert(bmr.smgr == NULL || bmr.relpersistence != 0);
923  Assert(extend_to != InvalidBlockNumber && extend_to > 0);
924 
925  if (bmr.smgr == NULL)
926  {
927  bmr.smgr = RelationGetSmgr(bmr.rel);
928  bmr.relpersistence = bmr.rel->rd_rel->relpersistence;
929  }
930 
931  /*
932  * If desired, create the file if it doesn't exist. If
933  * smgr_cached_nblocks[fork] is positive then it must exist, no need for
934  * an smgrexists call.
935  */
936  if ((flags & EB_CREATE_FORK_IF_NEEDED) &&
937  (bmr.smgr->smgr_cached_nblocks[fork] == 0 ||
939  !smgrexists(bmr.smgr, fork))
940  {
942 
943  /* recheck, fork might have been created concurrently */
944  if (!smgrexists(bmr.smgr, fork))
945  smgrcreate(bmr.smgr, fork, flags & EB_PERFORMING_RECOVERY);
946 
948  }
949 
950  /*
951  * If requested, invalidate size cache, so that smgrnblocks asks the
952  * kernel.
953  */
954  if (flags & EB_CLEAR_SIZE_CACHE)
956 
957  /*
958  * Estimate how many pages we'll need to extend by. This avoids acquiring
959  * unnecessarily many victim buffers.
960  */
961  current_size = smgrnblocks(bmr.smgr, fork);
962 
963  /*
964  * Since no-one else can be looking at the page contents yet, there is no
965  * difference between an exclusive lock and a cleanup-strength lock. Note
966  * that we pass the original mode to ReadBuffer_common() below, when
967  * falling back to reading the buffer to a concurrent relation extension.
968  */
970  flags |= EB_LOCK_TARGET;
971 
972  while (current_size < extend_to)
973  {
974  uint32 num_pages = lengthof(buffers);
975  BlockNumber first_block;
976 
977  if ((uint64) current_size + num_pages > extend_to)
978  num_pages = extend_to - current_size;
979 
980  first_block = ExtendBufferedRelCommon(bmr, fork, strategy, flags,
981  num_pages, extend_to,
982  buffers, &extended_by);
983 
984  current_size = first_block + extended_by;
985  Assert(num_pages != 0 || current_size >= extend_to);
986 
987  for (uint32 i = 0; i < extended_by; i++)
988  {
989  if (first_block + i != extend_to - 1)
990  ReleaseBuffer(buffers[i]);
991  else
992  buffer = buffers[i];
993  }
994  }
995 
996  /*
997  * It's possible that another backend concurrently extended the relation.
998  * In that case read the buffer.
999  *
1000  * XXX: Should we control this via a flag?
1001  */
1002  if (buffer == InvalidBuffer)
1003  {
1004  Assert(extended_by == 0);
1005  buffer = ReadBuffer_common(bmr.rel, bmr.smgr, 0,
1006  fork, extend_to - 1, mode, strategy);
1007  }
1008 
1009  return buffer;
1010 }
uint32 BlockNumber
Definition: block.h:31
static Buffer ReadBuffer_common(Relation rel, SMgrRelation smgr, char smgr_persistence, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:1198
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4896
#define lengthof(array)
Definition: c.h:788
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:430
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:480
#define ExclusiveLock
Definition: lockdefs.h:42
static PgChecksumMode mode
Definition: pg_checksums.c:56
int64 current_size
Definition: pg_checksums.c:64
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:655
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:46

References Assert, PrivateRefCountEntry::buffer, current_size, EB_CLEAR_SIZE_CACHE, EB_CREATE_FORK_IF_NEEDED, EB_LOCK_TARGET, EB_PERFORMING_RECOVERY, ExclusiveLock, ExtendBufferedRelCommon(), i, InvalidBlockNumber, InvalidBuffer, lengthof, LockRelationForExtension(), mode, RBM_ZERO_AND_CLEANUP_LOCK, RBM_ZERO_AND_LOCK, RelationData::rd_rel, ReadBuffer_common(), BufferManagerRelation::rel, RelationGetSmgr(), ReleaseBuffer(), BufferManagerRelation::relpersistence, BufferManagerRelation::smgr, SMgrRelationData::smgr_cached_nblocks, smgrcreate(), smgrexists(), smgrnblocks(), and UnlockRelationForExtension().

Referenced by fsm_extend(), vm_extend(), and XLogReadBufferExtended().

◆ FlushDatabaseBuffers()

void FlushDatabaseBuffers ( Oid  dbid)

Definition at line 4834 of file bufmgr.c.

4835 {
4836  int i;
4837  BufferDesc *bufHdr;
4838 
4839  for (i = 0; i < NBuffers; i++)
4840  {
4841  uint32 buf_state;
4842 
4843  bufHdr = GetBufferDescriptor(i);
4844 
4845  /*
4846  * As in DropRelationBuffers, an unlocked precheck should be safe and
4847  * saves some cycles.
4848  */
4849  if (bufHdr->tag.dbOid != dbid)
4850  continue;
4851 
4852  /* Make sure we can handle the pin */
4855 
4856  buf_state = LockBufHdr(bufHdr);
4857  if (bufHdr->tag.dbOid == dbid &&
4858  (buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
4859  {
4860  PinBuffer_Locked(bufHdr);
4864  UnpinBuffer(bufHdr);
4865  }
4866  else
4867  UnlockBufHdr(bufHdr, buf_state);
4868  }
4869 }

References BM_DIRTY, BM_VALID, BufferDescriptorGetContentLock(), CurrentResourceOwner, buftag::dbOid, FlushBuffer(), GetBufferDescriptor(), i, IOCONTEXT_NORMAL, IOOBJECT_RELATION, LockBufHdr(), LW_SHARED, LWLockAcquire(), LWLockRelease(), NBuffers, PinBuffer_Locked(), ReservePrivateRefCountEntry(), ResourceOwnerEnlarge(), BufferDesc::tag, UnlockBufHdr(), and UnpinBuffer().

Referenced by dbase_redo().

◆ FlushOneBuffer()

void FlushOneBuffer ( Buffer  buffer)

Definition at line 4876 of file bufmgr.c.

4877 {
4878  BufferDesc *bufHdr;
4879 
4880  /* currently not needed, but no fundamental reason not to support */
4881  Assert(!BufferIsLocal(buffer));
4882 
4883  Assert(BufferIsPinned(buffer));
4884 
4885  bufHdr = GetBufferDescriptor(buffer - 1);
4886 
4888 
4890 }
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1895

References Assert, PrivateRefCountEntry::buffer, BufferDescriptorGetContentLock(), BufferIsLocal, BufferIsPinned, FlushBuffer(), GetBufferDescriptor(), IOCONTEXT_NORMAL, IOOBJECT_RELATION, and LWLockHeldByMe().

Referenced by hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), and XLogReadBufferForRedoExtended().

◆ FlushRelationBuffers()

void FlushRelationBuffers ( Relation  rel)

Definition at line 4481 of file bufmgr.c.

4482 {
4483  int i;
4484  BufferDesc *bufHdr;
4485  SMgrRelation srel = RelationGetSmgr(rel);
4486 
4487  if (RelationUsesLocalBuffers(rel))
4488  {
4489  for (i = 0; i < NLocBuffer; i++)
4490  {
4491  uint32 buf_state;
4492  instr_time io_start;
4493 
4494  bufHdr = GetLocalBufferDescriptor(i);
4495  if (BufTagMatchesRelFileLocator(&bufHdr->tag, &rel->rd_locator) &&
4496  ((buf_state = pg_atomic_read_u32(&bufHdr->state)) &
4497  (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
4498  {
4499  ErrorContextCallback errcallback;
4500  Page localpage;
4501 
4502  localpage = (char *) LocalBufHdrGetBlock(bufHdr);
4503 
4504  /* Setup error traceback support for ereport() */
4506  errcallback.arg = (void *) bufHdr;
4507  errcallback.previous = error_context_stack;
4508  error_context_stack = &errcallback;
4509 
4510  PageSetChecksumInplace(localpage, bufHdr->tag.blockNum);
4511 
4513 
4514  smgrwrite(srel,
4515  BufTagGetForkNum(&bufHdr->tag),
4516  bufHdr->tag.blockNum,
4517  localpage,
4518  false);
4519 
4522  io_start, 1);
4523 
4524  buf_state &= ~(BM_DIRTY | BM_JUST_DIRTIED);
4525  pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
4526 
4528 
4529  /* Pop the error context stack */
4530  error_context_stack = errcallback.previous;
4531  }
4532  }
4533 
4534  return;
4535  }
4536 
4537  for (i = 0; i < NBuffers; i++)
4538  {
4539  uint32 buf_state;
4540 
4541  bufHdr = GetBufferDescriptor(i);
4542 
4543  /*
4544  * As in DropRelationBuffers, an unlocked precheck should be safe and
4545  * saves some cycles.
4546  */
4547  if (!BufTagMatchesRelFileLocator(&bufHdr->tag, &rel->rd_locator))
4548  continue;
4549 
4550  /* Make sure we can handle the pin */
4553 
4554  buf_state = LockBufHdr(bufHdr);
4555  if (BufTagMatchesRelFileLocator(&bufHdr->tag, &rel->rd_locator) &&
4556  (buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
4557  {
4558  PinBuffer_Locked(bufHdr);
4562  UnpinBuffer(bufHdr);
4563  }
4564  else
4565  UnlockBufHdr(bufHdr, buf_state);
4566  }
4567 }
static void pg_atomic_unlocked_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:290
static bool BufTagMatchesRelFileLocator(const BufferTag *tag, const RelFileLocator *rlocator)
#define BM_JUST_DIRTIED
Definition: buf_internals.h:66
bool track_io_timing
Definition: bufmgr.c:142
#define LocalBufHdrGetBlock(bufHdr)
Definition: bufmgr.c:71
static void local_buffer_write_error_callback(void *arg)
Definition: bufmgr.c:5687
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1542
ErrorContextCallback * error_context_stack
Definition: elog.c:94
BufferUsage pgBufferUsage
Definition: instrument.c:20
int NLocBuffer
Definition: localbuf.c:42
@ IOOBJECT_TEMP_RELATION
Definition: pgstat.h:281
@ IOOP_WRITE
Definition: pgstat.h:304
instr_time pgstat_prepare_io_time(bool track_io_guc)
Definition: pgstat_io.c:100
void pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op, instr_time start_time, uint32 cnt)
Definition: pgstat_io.c:122
#define RelationUsesLocalBuffers(relation)
Definition: rel.h:637
static void smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: smgr.h:121
int64 local_blks_written
Definition: instrument.h:33
struct ErrorContextCallback * previous
Definition: elog.h:295
void(* callback)(void *arg)
Definition: elog.h:296
RelFileLocator rd_locator
Definition: rel.h:57

References ErrorContextCallback::arg, buftag::blockNum, BM_DIRTY, BM_JUST_DIRTIED, BM_VALID, BufferDescriptorGetContentLock(), BufTagGetForkNum(), BufTagMatchesRelFileLocator(), ErrorContextCallback::callback, CurrentResourceOwner, error_context_stack, FlushBuffer(), GetBufferDescriptor(), GetLocalBufferDescriptor(), i, IOCONTEXT_NORMAL, IOOBJECT_RELATION, IOOBJECT_TEMP_RELATION, IOOP_WRITE, BufferUsage::local_blks_written, local_buffer_write_error_callback(), LocalBufHdrGetBlock, LockBufHdr(), LW_SHARED, LWLockAcquire(), LWLockRelease(), NBuffers, NLocBuffer, PageSetChecksumInplace(), pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), pgBufferUsage, pgstat_count_io_op_time(), pgstat_prepare_io_time(), PinBuffer_Locked(), ErrorContextCallback::previous, RelationData::rd_locator, RelationGetSmgr(), RelationUsesLocalBuffers, ReservePrivateRefCountEntry(), ResourceOwnerEnlarge(), smgrwrite(), BufferDesc::state, BufferDesc::tag, track_io_timing, UnlockBufHdr(), and UnpinBuffer().

Referenced by fill_seq_with_data(), heapam_relation_copy_data(), and index_copy_data().

◆ FlushRelationsAllBuffers()

void FlushRelationsAllBuffers ( struct SMgrRelationData **  smgrs,
int  nrels 
)

◆ FreeAccessStrategy()

void FreeAccessStrategy ( BufferAccessStrategy  strategy)

Definition at line 681 of file freelist.c.

682 {
683  /* don't crash if called on a "default" strategy */
684  if (strategy != NULL)
685  pfree(strategy);
686 }

References pfree().

Referenced by blgetbitmap(), FreeBulkInsertState(), heap_endscan(), initscan(), parallel_vacuum_main(), and RelationCopyStorageUsingBuffer().

◆ GetAccessStrategy()

BufferAccessStrategy GetAccessStrategy ( BufferAccessStrategyType  btype)

Definition at line 541 of file freelist.c.

542 {
543  int ring_size_kb;
544 
545  /*
546  * Select ring size to use. See buffer/README for rationales.
547  *
548  * Note: if you change the ring size for BAS_BULKREAD, see also
549  * SYNC_SCAN_REPORT_INTERVAL in access/heap/syncscan.c.
550  */
551  switch (btype)
552  {
553  case BAS_NORMAL:
554  /* if someone asks for NORMAL, just give 'em a "default" object */
555  return NULL;
556 
557  case BAS_BULKREAD:
558  ring_size_kb = 256;
559  break;
560  case BAS_BULKWRITE:
561  ring_size_kb = 16 * 1024;
562  break;
563  case BAS_VACUUM:
564  ring_size_kb = 2048;
565  break;
566 
567  default:
568  elog(ERROR, "unrecognized buffer access strategy: %d",
569  (int) btype);
570  return NULL; /* keep compiler quiet */
571  }
572 
573  return GetAccessStrategyWithSize(btype, ring_size_kb);
574 }
BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype, int ring_size_kb)
Definition: freelist.c:584

References BAS_BULKREAD, BAS_BULKWRITE, BAS_NORMAL, BAS_VACUUM, elog, ERROR, and GetAccessStrategyWithSize().

Referenced by blgetbitmap(), bt_check_every_level(), collect_corrupt_items(), collect_visibility_data(), GetBulkInsertState(), initscan(), pgstat_index(), pgstathashindex(), pgstatindex_impl(), RelationCopyStorageUsingBuffer(), ScanSourceDatabasePgClass(), statapprox_heap(), and verify_heapam().

◆ GetAccessStrategyBufferCount()

int GetAccessStrategyBufferCount ( BufferAccessStrategy  strategy)

Definition at line 624 of file freelist.c.

625 {
626  if (strategy == NULL)
627  return 0;
628 
629  return strategy->nbuffers;
630 }

References BufferAccessStrategyData::nbuffers.

Referenced by parallel_vacuum_init().

◆ GetAccessStrategyPinLimit()

int GetAccessStrategyPinLimit ( BufferAccessStrategy  strategy)

Definition at line 647 of file freelist.c.

648 {
649  if (strategy == NULL)
650  return NBuffers;
651 
652  switch (strategy->btype)
653  {
654  case BAS_BULKREAD:
655 
656  /*
657  * Since BAS_BULKREAD uses StrategyRejectBuffer(), dirty buffers
658  * shouldn't be a problem and the caller is free to pin up to the
659  * entire ring at once.
660  */
661  return strategy->nbuffers;
662 
663  default:
664 
665  /*
666  * Tell caller not to pin more than half the buffers in the ring.
667  * This is a trade-off between look ahead distance and deferring
668  * writeback and associated WAL traffic.
669  */
670  return strategy->nbuffers / 2;
671  }
672 }
BufferAccessStrategyType btype
Definition: freelist.c:75

References BAS_BULKREAD, BufferAccessStrategyData::btype, BufferAccessStrategyData::nbuffers, and NBuffers.

Referenced by read_stream_begin_relation().

◆ GetAccessStrategyWithSize()

BufferAccessStrategy GetAccessStrategyWithSize ( BufferAccessStrategyType  btype,
int  ring_size_kb 
)

Definition at line 584 of file freelist.c.

585 {
586  int ring_buffers;
587  BufferAccessStrategy strategy;
588 
589  Assert(ring_size_kb >= 0);
590 
591  /* Figure out how many buffers ring_size_kb is */
592  ring_buffers = ring_size_kb / (BLCKSZ / 1024);
593 
594  /* 0 means unlimited, so no BufferAccessStrategy required */
595  if (ring_buffers == 0)
596  return NULL;
597 
598  /* Cap to 1/8th of shared_buffers */
599  ring_buffers = Min(NBuffers / 8, ring_buffers);
600 
601  /* NBuffers should never be less than 16, so this shouldn't happen */
602  Assert(ring_buffers > 0);
603 
604  /* Allocate the object and initialize all elements to zeroes */
605  strategy = (BufferAccessStrategy)
606  palloc0(offsetof(BufferAccessStrategyData, buffers) +
607  ring_buffers * sizeof(Buffer));
608 
609  /* Set fields that don't start out zero */
610  strategy->btype = btype;
611  strategy->nbuffers = ring_buffers;
612 
613  return strategy;
614 }
struct BufferAccessStrategyData * BufferAccessStrategy
Definition: buf.h:44
#define Min(x, y)
Definition: c.h:1004
void * palloc0(Size size)
Definition: mcxt.c:1346

References Assert, BufferAccessStrategyData::btype, Min, BufferAccessStrategyData::nbuffers, NBuffers, and palloc0().

Referenced by do_autovacuum(), ExecVacuum(), GetAccessStrategy(), and parallel_vacuum_main().

◆ HoldingBufferPinThatDelaysRecovery()

bool HoldingBufferPinThatDelaysRecovery ( void  )

Definition at line 5346 of file bufmgr.c.

5347 {
5348  int bufid = GetStartupBufferPinWaitBufId();
5349 
5350  /*
5351  * If we get woken slowly then it's possible that the Startup process was
5352  * already woken by other backends before we got here. Also possible that
5353  * we get here by multiple interrupts or interrupts at inappropriate
5354  * times, so make sure we do nothing if the bufid is not set.
5355  */
5356  if (bufid < 0)
5357  return false;
5358 
5359  if (GetPrivateRefCount(bufid + 1) > 0)
5360  return true;
5361 
5362  return false;
5363 }
int GetStartupBufferPinWaitBufId(void)
Definition: proc.c:671

References GetPrivateRefCount(), and GetStartupBufferPinWaitBufId().

Referenced by CheckRecoveryConflictDeadlock(), and ProcessRecoveryConflictInterrupt().

◆ IncrBufferRefCount()

void IncrBufferRefCount ( Buffer  buffer)

Definition at line 4928 of file bufmgr.c.

4929 {
4930  Assert(BufferIsPinned(buffer));
4932  if (BufferIsLocal(buffer))
4933  LocalRefCount[-buffer - 1]++;
4934  else
4935  {
4936  PrivateRefCountEntry *ref;
4937 
4938  ref = GetPrivateRefCountEntry(buffer, true);
4939  Assert(ref != NULL);
4940  ref->refcount++;
4941  }
4943 }
static void ResourceOwnerRememberBuffer(ResourceOwner owner, Buffer buffer)
static PrivateRefCountEntry * GetPrivateRefCountEntry(Buffer buffer, bool do_move)
Definition: bufmgr.c:341

References Assert, PrivateRefCountEntry::buffer, BufferIsLocal, BufferIsPinned, CurrentResourceOwner, GetPrivateRefCountEntry(), LocalRefCount, PrivateRefCountEntry::refcount, ResourceOwnerEnlarge(), and ResourceOwnerRememberBuffer().

Referenced by _bt_steppage(), btrestrpos(), entryLoadMoreItems(), ReadBufferBI(), RelationAddBlocks(), scanPostingTree(), startScanEntry(), and tts_buffer_heap_store_tuple().

◆ InitBufferPool()

void InitBufferPool ( void  )

Definition at line 68 of file buf_init.c.

69 {
70  bool foundBufs,
71  foundDescs,
72  foundIOCV,
73  foundBufCkpt;
74 
75  /* Align descriptors to a cacheline boundary. */
77  ShmemInitStruct("Buffer Descriptors",
78  NBuffers * sizeof(BufferDescPadded),
79  &foundDescs);
80 
81  /* Align buffer pool on IO page size boundary. */
82  BufferBlocks = (char *)
84  ShmemInitStruct("Buffer Blocks",
85  NBuffers * (Size) BLCKSZ + PG_IO_ALIGN_SIZE,
86  &foundBufs));
87 
88  /* Align condition variables to cacheline boundary. */
90  ShmemInitStruct("Buffer IO Condition Variables",
92  &foundIOCV);
93 
94  /*
95  * The array used to sort to-be-checkpointed buffer ids is located in
96  * shared memory, to avoid having to allocate significant amounts of
97  * memory at runtime. As that'd be in the middle of a checkpoint, or when
98  * the checkpointer is restarted, memory allocation failures would be
99  * painful.
100  */
102  ShmemInitStruct("Checkpoint BufferIds",
103  NBuffers * sizeof(CkptSortItem), &foundBufCkpt);
104 
105  if (foundDescs || foundBufs || foundIOCV || foundBufCkpt)
106  {
107  /* should find all of these, or none of them */
108  Assert(foundDescs && foundBufs && foundIOCV && foundBufCkpt);
109  /* note: this path is only taken in EXEC_BACKEND case */
110  }
111  else
112  {
113  int i;
114 
115  /*
116  * Initialize all the buffer headers.
117  */
118  for (i = 0; i < NBuffers; i++)
119  {
121 
122  ClearBufferTag(&buf->tag);
123 
124  pg_atomic_init_u32(&buf->state, 0);
125  buf->wait_backend_pgprocno = INVALID_PROC_NUMBER;
126 
127  buf->buf_id = i;
128 
129  /*
130  * Initially link all the buffers together as unused. Subsequent
131  * management of this list is done by freelist.c.
132  */
133  buf->freeNext = i + 1;
134 
137 
139  }
140 
141  /* Correct last entry of linked list */
143  }
144 
145  /* Init other shared buffer-management stuff */
146  StrategyInitialize(!foundDescs);
147 
148  /* Initialize per-backend file flush context */
151 }
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:216
CkptSortItem * CkptBufferIds
Definition: buf_init.c:25
char * BufferBlocks
Definition: buf_init.c:22
WritebackContext BackendWritebackContext
Definition: buf_init.c:24
ConditionVariableMinimallyPadded * BufferIOCVArray
Definition: buf_init.c:23
BufferDescPadded * BufferDescriptors
Definition: buf_init.c:21
#define FREENEXT_END_OF_LIST
static void ClearBufferTag(BufferTag *tag)
static ConditionVariable * BufferDescriptorGetIOCV(const BufferDesc *bdesc)
void WritebackContextInit(WritebackContext *context, int *max_pending)
Definition: bufmgr.c:5876
int backend_flush_after
Definition: bufmgr.c:172
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:804
void ConditionVariableInit(ConditionVariable *cv)
void StrategyInitialize(bool init)
Definition: freelist.c:474
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:709
@ LWTRANCHE_BUFFER_CONTENT
Definition: lwlock.h:187
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387

References Assert, backend_flush_after, BackendWritebackContext, buf, BufferBlocks, BufferDescriptorGetContentLock(), BufferDescriptorGetIOCV(), BufferDescriptors, BufferIOCVArray, CkptBufferIds, ClearBufferTag(), ConditionVariableInit(), BufferDesc::freeNext, FREENEXT_END_OF_LIST, GetBufferDescriptor(), i, INVALID_PROC_NUMBER, LWLockInitialize(), LWTRANCHE_BUFFER_CONTENT, NBuffers, pg_atomic_init_u32(), PG_IO_ALIGN_SIZE, ShmemInitStruct(), StrategyInitialize(), TYPEALIGN, and WritebackContextInit().

Referenced by CreateOrAttachShmemStructs().

◆ InitBufferPoolAccess()

void InitBufferPoolAccess ( void  )

Definition at line 3565 of file bufmgr.c.

3566 {
3567  HASHCTL hash_ctl;
3568 
3569  memset(&PrivateRefCountArray, 0, sizeof(PrivateRefCountArray));
3570 
3571  hash_ctl.keysize = sizeof(int32);
3572  hash_ctl.entrysize = sizeof(PrivateRefCountEntry);
3573 
3574  PrivateRefCountHash = hash_create("PrivateRefCount", 100, &hash_ctl,
3575  HASH_ELEM | HASH_BLOBS);
3576 
3577  /*
3578  * AtProcExit_Buffers needs LWLock access, and thereby has to be called at
3579  * the corresponding phase of backend shutdown.
3580  */
3581  Assert(MyProc != NULL);
3583 }
static void AtProcExit_Buffers(int code, Datum arg)
Definition: bufmgr.c:3590
struct PrivateRefCountEntry PrivateRefCountEntry
static struct PrivateRefCountEntry PrivateRefCountArray[REFCOUNT_ARRAY_ENTRIES]
Definition: bufmgr.c:207
static HTAB * PrivateRefCountHash
Definition: bufmgr.c:208
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
PGPROC * MyProc
Definition: proc.c:66
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76

References Assert, AtProcExit_Buffers(), HASHCTL::entrysize, HASH_BLOBS, hash_create(), HASH_ELEM, HASHCTL::keysize, MyProc, on_shmem_exit(), PrivateRefCountArray, and PrivateRefCountHash.

Referenced by BaseInit().

◆ IsBufferCleanupOK()

bool IsBufferCleanupOK ( Buffer  buffer)

Definition at line 5428 of file bufmgr.c.

5429 {
5430  BufferDesc *bufHdr;
5431  uint32 buf_state;
5432 
5433  Assert(BufferIsValid(buffer));
5434 
5435  if (BufferIsLocal(buffer))
5436  {
5437  /* There should be exactly one pin */
5438  if (LocalRefCount[-buffer - 1] != 1)
5439  return false;
5440  /* Nobody else to wait for */
5441  return true;
5442  }
5443 
5444  /* There should be exactly one local pin */
5445  if (GetPrivateRefCount(buffer) != 1)
5446  return false;
5447 
5448  bufHdr = GetBufferDescriptor(buffer - 1);
5449 
5450  /* caller must hold exclusive lock on buffer */
5452  LW_EXCLUSIVE));
5453 
5454  buf_state = LockBufHdr(bufHdr);
5455 
5456  Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
5457  if (BUF_STATE_GET_REFCOUNT(buf_state) == 1)
5458  {
5459  /* pincount is OK. */
5460  UnlockBufHdr(bufHdr, buf_state);
5461  return true;
5462  }
5463 
5464  UnlockBufHdr(bufHdr, buf_state);
5465  return false;
5466 }

References Assert, BUF_STATE_GET_REFCOUNT, PrivateRefCountEntry::buffer, BufferDescriptorGetContentLock(), BufferIsLocal, BufferIsValid(), GetBufferDescriptor(), GetPrivateRefCount(), LocalRefCount, LockBufHdr(), LW_EXCLUSIVE, LWLockHeldByMeInMode(), and UnlockBufHdr().

Referenced by _hash_doinsert(), _hash_expandtable(), _hash_splitbucket(), and hashbucketcleanup().

◆ LimitAdditionalLocalPins()

void LimitAdditionalLocalPins ( uint32 additional_pins)

Definition at line 290 of file localbuf.c.

291 {
292  uint32 max_pins;
293 
294  if (*additional_pins <= 1)
295  return;
296 
297  /*
298  * In contrast to LimitAdditionalPins() other backends don't play a role
299  * here. We can allow up to NLocBuffer pins in total, but it might not be
300  * initialized yet so read num_temp_buffers.
301  */
302  max_pins = (num_temp_buffers - NLocalPinnedBuffers);
303 
304  if (*additional_pins >= max_pins)
305  *additional_pins = max_pins;
306 }
int num_temp_buffers
Definition: guc_tables.c:537
static int NLocalPinnedBuffers
Definition: localbuf.c:53

References NLocalPinnedBuffers, and num_temp_buffers.

Referenced by ExtendBufferedRelLocal(), and read_stream_begin_relation().

◆ LimitAdditionalPins()

void LimitAdditionalPins ( uint32 additional_pins)

Definition at line 2104 of file bufmgr.c.

2105 {
2106  uint32 max_backends;
2107  int max_proportional_pins;
2108 
2109  if (*additional_pins <= 1)
2110  return;
2111 
2112  max_backends = MaxBackends + NUM_AUXILIARY_PROCS;
2113  max_proportional_pins = NBuffers / max_backends;
2114 
2115  /*
2116  * Subtract the approximate number of buffers already pinned by this
2117  * backend. We get the number of "overflowed" pins for free, but don't
2118  * know the number of pins in PrivateRefCountArray. The cost of
2119  * calculating that exactly doesn't seem worth it, so just assume the max.
2120  */
2121  max_proportional_pins -= PrivateRefCountOverflowed + REFCOUNT_ARRAY_ENTRIES;
2122 
2123  if (max_proportional_pins <= 0)
2124  max_proportional_pins = 1;
2125 
2126  if (*additional_pins > max_proportional_pins)
2127  *additional_pins = max_proportional_pins;
2128 }
#define REFCOUNT_ARRAY_ENTRIES
Definition: bufmgr.c:95
int MaxBackends
Definition: globals.c:143
#define NUM_AUXILIARY_PROCS
Definition: proc.h:440

References MaxBackends, NBuffers, NUM_AUXILIARY_PROCS, PrivateRefCountOverflowed, and REFCOUNT_ARRAY_ENTRIES.

Referenced by ExtendBufferedRelShared(), and read_stream_begin_relation().

◆ LockBuffer()

void LockBuffer ( Buffer  buffer,
int  mode 
)

Definition at line 5131 of file bufmgr.c.

5132 {
5133  BufferDesc *buf;
5134 
5135  Assert(BufferIsPinned(buffer));
5136  if (BufferIsLocal(buffer))
5137  return; /* local buffers need no lock */
5138 
5139  buf = GetBufferDescriptor(buffer - 1);
5140 
5141  if (mode == BUFFER_LOCK_UNLOCK)
5143  else if (mode == BUFFER_LOCK_SHARE)
5145  else if (mode == BUFFER_LOCK_EXCLUSIVE)
5147  else
5148  elog(ERROR, "unrecognized buffer lock mode: %d", mode);
5149 }
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:194
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:195

References Assert, buf, PrivateRefCountEntry::buffer, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferDescriptorGetContentLock(), BufferIsLocal, BufferIsPinned, elog, ERROR, GetBufferDescriptor(), LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), and mode.

Referenced by _bt_lockbuf(), _bt_unlockbuf(), _bt_upgradelockbufcleanup(), _hash_addovflpage(), _hash_doinsert(), _hash_expandtable(), _hash_finish_split(), _hash_first(), _hash_freeovflpage(), _hash_getbuf(), _hash_getbuf_with_strategy(), _hash_getcachedmetap(), _hash_init(), _hash_kill_items(), _hash_readnext(), _hash_readpage(), _hash_readprev(), _hash_splitbucket(), _hash_squeezebucket(), _hash_vacuum_one_page(), blbulkdelete(), blgetbitmap(), blinsert(), BloomInitMetapage(), BloomNewBuffer(), blvacuumcleanup(), brin_doinsert(), brin_doupdate(), brin_evacuate_page(), brin_getinsertbuffer(), brin_page_cleanup(), bringetbitmap(), brinGetStats(), brinGetTupleForHeapBlock(), brininsert(), brinLockRevmapPageForUpdate(), brinRevmapDesummarizeRange(), brinRevmapInitialize(), brinsummarize(), bt_metap(), bt_multi_page_stats(), bt_page_items_internal(), bt_page_stats_internal(), bt_recheck_sibling_links(), collect_corrupt_items(), collect_visibility_data(), collectMatchBitmap(), ConditionalLockBufferForCleanup(), count_nondeletable_pages(), entryLoadMoreItems(), FreeSpaceMapPrepareTruncateRel(), fsm_readbuf(), fsm_search(), fsm_search_avail(), fsm_set_and_search(), fsm_vacuum_page(), get_raw_page_internal(), GetVisibilityMapPins(), ginbulkdelete(), ginEntryInsert(), ginFindLeafPage(), ginFindParents(), ginFinishOldSplit(), ginFinishSplit(), ginGetStats(), ginHeapTupleFastInsert(), ginInsertCleanup(), ginInsertValue(), GinNewBuffer(), ginScanToDelete(), ginStepRight(), ginTraverseLock(), ginUpdateStats(), ginvacuumcleanup(), ginVacuumPostingTreeLeaves(), gistBufferingFindCorrectParent(), gistbufferinginserttuples(), gistdoinsert(), gistFindCorrectParent(), gistFindPath(), gistfinishsplit(), gistfixsplit(), gistformdownlink(), gistGetMaxLevel(), gistinserttuples(), gistkillitems(), gistNewBuffer(), gistProcessItup(), gistScanPage(), gistvacuum_delete_empty_pages(), gistvacuumpage(), hashbucketcleanup(), hashbulkdelete(), heap_abort_speculative(), heap_delete(), heap_fetch(), heap_finish_speculative(), heap_get_latest_tid(), heap_index_delete_tuples(), heap_inplace_update(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_page_prune_opt(), heap_prepare_pagescan(), heap_update(), heap_xlog_visible(), heapam_index_build_range_scan(), heapam_index_fetch_tuple(), heapam_index_validate_scan(), heapam_relation_copy_for_cluster(), heapam_scan_analyze_next_block(), heapam_scan_bitmap_next_block(), heapam_scan_sample_next_tuple(), heapam_tuple_satisfies_snapshot(), heapgettup(), initBloomState(), lazy_scan_heap(), lazy_scan_new_or_empty(), lazy_vacuum_heap_rel(), LockBufferForCleanup(), log_newpage_range(), palloc_btree_page(), pg_visibility(), pgrowlocks(), pgstat_btree_page(), pgstat_gist_page(), pgstat_heap(), pgstatginindex_internal(), pgstathashindex(), pgstatindex_impl(), read_seq_tuple(), RelationAddBlocks(), RelationCopyStorageUsingBuffer(), RelationGetBufferForTuple(), revmap_physical_extend(), scanGetCandidate(), scanPendingInsert(), ScanSourceDatabasePgClass(), shiftList(), spgdoinsert(), spgGetCache(), SpGistNewBuffer(), spgprocesspending(), spgvacuumpage(), spgWalk(), startScanEntry(), statapprox_heap(), summarize_range(), UnlockReleaseBuffer(), verify_heapam(), verifyBackupPageConsistency(), visibilitymap_clear(), visibilitymap_prepare_truncate(), visibilitymap_set(), vm_readbuf(), XLogReadBufferForRedoExtended(), XLogRecordPageWithFreeSpace(), and ZeroAndLockBuffer().

◆ LockBufferForCleanup()

void LockBufferForCleanup ( Buffer  buffer)

Definition at line 5211 of file bufmgr.c.

5212 {
5213  BufferDesc *bufHdr;
5214  TimestampTz waitStart = 0;
5215  bool waiting = false;
5216  bool logged_recovery_conflict = false;
5217 
5218  Assert(BufferIsPinned(buffer));
5219  Assert(PinCountWaitBuf == NULL);
5220 
5221  CheckBufferIsPinnedOnce(buffer);
5222 
5223  /* Nobody else to wait for */
5224  if (BufferIsLocal(buffer))
5225  return;
5226 
5227  bufHdr = GetBufferDescriptor(buffer - 1);
5228 
5229  for (;;)
5230  {
5231  uint32 buf_state;
5232 
5233  /* Try to acquire lock */
5235  buf_state = LockBufHdr(bufHdr);
5236 
5237  Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
5238  if (BUF_STATE_GET_REFCOUNT(buf_state) == 1)
5239  {
5240  /* Successfully acquired exclusive lock with pincount 1 */
5241  UnlockBufHdr(bufHdr, buf_state);
5242 
5243  /*
5244  * Emit the log message if recovery conflict on buffer pin was
5245  * resolved but the startup process waited longer than
5246  * deadlock_timeout for it.
5247  */
5248  if (logged_recovery_conflict)
5250  waitStart, GetCurrentTimestamp(),
5251  NULL, false);
5252 
5253  if (waiting)
5254  {
5255  /* reset ps display to remove the suffix if we added one */
5257  waiting = false;
5258  }
5259  return;
5260  }
5261  /* Failed, so mark myself as waiting for pincount 1 */
5262  if (buf_state & BM_PIN_COUNT_WAITER)
5263  {
5264  UnlockBufHdr(bufHdr, buf_state);
5265  LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
5266  elog(ERROR, "multiple backends attempting to wait for pincount 1");
5267  }
5269  PinCountWaitBuf = bufHdr;
5270  buf_state |= BM_PIN_COUNT_WAITER;
5271  UnlockBufHdr(bufHdr, buf_state);
5272  LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
5273 
5274  /* Wait to be signaled by UnpinBuffer() */
5275  if (InHotStandby)
5276  {
5277  if (!waiting)
5278  {
5279  /* adjust the process title to indicate that it's waiting */
5280  set_ps_display_suffix("waiting");
5281  waiting = true;
5282  }
5283 
5284  /*
5285  * Emit the log message if the startup process is waiting longer
5286  * than deadlock_timeout for recovery conflict on buffer pin.
5287  *
5288  * Skip this if first time through because the startup process has
5289  * not started waiting yet in this case. So, the wait start
5290  * timestamp is set after this logic.
5291  */
5292  if (waitStart != 0 && !logged_recovery_conflict)
5293  {
5295 
5296  if (TimestampDifferenceExceeds(waitStart, now,
5297  DeadlockTimeout))
5298  {
5300  waitStart, now, NULL, true);
5301  logged_recovery_conflict = true;
5302  }
5303  }
5304 
5305  /*
5306  * Set the wait start timestamp if logging is enabled and first
5307  * time through.
5308  */
5309  if (log_recovery_conflict_waits && waitStart == 0)
5310  waitStart = GetCurrentTimestamp();
5311 
5312  /* Publish the bufid that Startup process waits on */
5313  SetStartupBufferPinWaitBufId(buffer - 1);
5314  /* Set alarm and then wait to be signaled by UnpinBuffer() */
5316  /* Reset the published bufid */
5318  }
5319  else
5320  ProcWaitForSignal(WAIT_EVENT_BUFFER_PIN);
5321 
5322  /*
5323  * Remove flag marking us as waiter. Normally this will not be set
5324  * anymore, but ProcWaitForSignal() can return for other signals as
5325  * well. We take care to only reset the flag if we're the waiter, as
5326  * theoretically another backend could have started waiting. That's
5327  * impossible with the current usages due to table level locking, but
5328  * better be safe.
5329  */
5330  buf_state = LockBufHdr(bufHdr);
5331  if ((buf_state & BM_PIN_COUNT_WAITER) != 0 &&
5333  buf_state &= ~BM_PIN_COUNT_WAITER;
5334  UnlockBufHdr(bufHdr, buf_state);
5335 
5336  PinCountWaitBuf = NULL;
5337  /* Loop back and try again */
5338  }
5339 }
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1790
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1654
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1618
#define BM_PIN_COUNT_WAITER
Definition: buf_internals.h:67
void CheckBufferIsPinnedOnce(Buffer buffer)
Definition: bufmgr.c:5178
static BufferDesc * PinCountWaitBuf
Definition: bufmgr.c:175
int64 TimestampTz
Definition: timestamp.h:39
static volatile sig_atomic_t waiting
Definition: latch.c:162
@ PROCSIG_RECOVERY_CONFLICT_BUFFERPIN
Definition: procsignal.h:47
void set_ps_display_remove_suffix(void)
Definition: ps_status.c:421
void set_ps_display_suffix(const char *suffix)
Definition: ps_status.c:369
int DeadlockTimeout
Definition: proc.c:57
void SetStartupBufferPinWaitBufId(int bufid)
Definition: proc.c:659
void ProcWaitForSignal(uint32 wait_event_info)
Definition: proc.c:1866
void ResolveRecoveryConflictWithBufferPin(void)
Definition: standby.c:792
bool log_recovery_conflict_waits
Definition: standby.c:41
void LogRecoveryConflict(ProcSignalReason reason, TimestampTz wait_start, TimestampTz now, VirtualTransactionId *wait_list, bool still_waiting)
Definition: standby.c:273
int wait_backend_pgprocno
#define InHotStandby
Definition: xlogutils.h:57

References Assert, BM_PIN_COUNT_WAITER, BUF_STATE_GET_REFCOUNT, PrivateRefCountEntry::buffer, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferIsLocal, BufferIsPinned, CheckBufferIsPinnedOnce(), DeadlockTimeout, elog, ERROR, GetBufferDescriptor(), GetCurrentTimestamp(), InHotStandby, LockBuffer(), LockBufHdr(), log_recovery_conflict_waits, LogRecoveryConflict(), MyProcNumber, now(), PinCountWaitBuf, PROCSIG_RECOVERY_CONFLICT_BUFFERPIN, ProcWaitForSignal(), ResolveRecoveryConflictWithBufferPin(), set_ps_display_remove_suffix(), set_ps_display_suffix(), SetStartupBufferPinWaitBufId(), TimestampDifferenceExceeds(), UnlockBufHdr(), BufferDesc::wait_backend_pgprocno, and waiting.

Referenced by _bt_upgradelockbufcleanup(), ginVacuumPostingTree(), hashbulkdelete(), heap_force_common(), lazy_scan_heap(), XLogReadBufferForRedoExtended(), and ZeroAndLockBuffer().

◆ MarkBufferDirty()

void MarkBufferDirty ( Buffer  buffer)

Definition at line 2520 of file bufmgr.c.

2521 {
2522  BufferDesc *bufHdr;
2523  uint32 buf_state;
2524  uint32 old_buf_state;
2525 
2526  if (!BufferIsValid(buffer))
2527  elog(ERROR, "bad buffer ID: %d", buffer);
2528 
2529  if (BufferIsLocal(buffer))
2530  {
2531  MarkLocalBufferDirty(buffer);
2532  return;
2533  }
2534 
2535  bufHdr = GetBufferDescriptor(buffer - 1);
2536 
2537  Assert(BufferIsPinned(buffer));
2539  LW_EXCLUSIVE));
2540 
2541  old_buf_state = pg_atomic_read_u32(&bufHdr->state);
2542  for (;;)
2543  {
2544  if (old_buf_state & BM_LOCKED)
2545  old_buf_state = WaitBufHdrUnlocked(bufHdr);
2546 
2547  buf_state = old_buf_state;
2548 
2549  Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
2550  buf_state |= BM_DIRTY | BM_JUST_DIRTIED;
2551 
2552  if (pg_atomic_compare_exchange_u32(&bufHdr->state, &old_buf_state,
2553  buf_state))
2554  break;
2555  }
2556 
2557  /*
2558  * If the buffer was not dirty already, do vacuum accounting.
2559  */
2560  if (!(old_buf_state & BM_DIRTY))
2561  {
2562  VacuumPageDirty++;
2564  if (VacuumCostActive)
2566  }
2567 }
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
Definition: atomics.h:344
#define BM_LOCKED
Definition: buf_internals.h:60
static uint32 WaitBufHdrUnlocked(BufferDesc *buf)
Definition: bufmgr.c:5764
bool VacuumCostActive
Definition: globals.c:159
int64 VacuumPageDirty
Definition: globals.c:156
int VacuumCostBalance
Definition: globals.c:158
int VacuumCostPageDirty
Definition: globals.c:150
void MarkLocalBufferDirty(Buffer buffer)
Definition: localbuf.c:449
int64 shared_blks_dirtied
Definition: instrument.h:28

References Assert, BM_DIRTY, BM_JUST_DIRTIED, BM_LOCKED, BUF_STATE_GET_REFCOUNT, PrivateRefCountEntry::buffer, BufferDescriptorGetContentLock(), BufferIsLocal, BufferIsPinned, BufferIsValid(), elog, ERROR, GetBufferDescriptor(), LW_EXCLUSIVE, LWLockHeldByMeInMode(), MarkLocalBufferDirty(), pg_atomic_compare_exchange_u32(), pg_atomic_read_u32(), pgBufferUsage, BufferUsage::shared_blks_dirtied, BufferDesc::state, VacuumCostActive, VacuumCostBalance, VacuumCostPageDirty, VacuumPageDirty, and WaitBufHdrUnlocked().

Referenced by _bt_clear_incomplete_split(), _bt_dedup_pass(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_getroot(), _bt_insertonpg(), _bt_mark_page_halfdead(), _bt_newlevel(), _bt_restore_meta(), _bt_set_cleanup_info(), _bt_split(), _bt_unlink_halfdead_page(), _hash_addovflpage(), _hash_doinsert(), _hash_expandtable(), _hash_freeovflpage(), _hash_init(), _hash_splitbucket(), _hash_squeezebucket(), _hash_vacuum_one_page(), addLeafTuple(), brin_doinsert(), brin_doupdate(), brin_initialize_empty_new_buffer(), brin_xlog_createidx(), brin_xlog_desummarize_page(), brin_xlog_insert_update(), brin_xlog_revmap_extend(), brin_xlog_samepage_update(), brin_xlog_update(), brinbuild(), brinbuildempty(), brinRevmapDesummarizeRange(), btree_xlog_dedup(), btree_xlog_delete(), btree_xlog_insert(), btree_xlog_mark_page_halfdead(), btree_xlog_newroot(), btree_xlog_split(), btree_xlog_unlink_page(), btree_xlog_vacuum(), createPostingTree(), dataExecPlaceToPageInternal(), dataExecPlaceToPageLeaf(), do_setval(), doPickSplit(), entryExecPlaceToPage(), fill_seq_fork_with_data(), FreeSpaceMapPrepareTruncateRel(), generic_redo(), GenericXLogFinish(), ginbuild(), ginbuildempty(), ginbulkdelete(), ginDeletePage(), ginHeapTupleFastInsert(), ginPlaceToPage(), ginRedoClearIncompleteSplit(), ginRedoCreatePTree(), ginRedoDeleteListPages(), ginRedoDeletePage(), ginRedoInsert(), ginRedoInsertListPage(), ginRedoUpdateMetapage(), ginRedoVacuumDataLeafPage(), ginUpdateStats(), ginVacuumPostingTreeLeaf(), gistbuild(), gistbuildempty(), gistdeletepage(), gistplacetopage(), gistprunepage(), gistRedoClearFollowRight(), gistRedoDeleteRecord(), gistRedoPageDelete(), gistRedoPageSplitRecord(), gistRedoPageUpdateRecord(), gistvacuumpage(), hash_xlog_add_ovfl_page(), hash_xlog_delete(), hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), hash_xlog_insert(), hash_xlog_move_page_contents(), hash_xlog_split_allocate_page(), hash_xlog_split_cleanup(), hash_xlog_split_complete(), hash_xlog_squeeze_page(), hash_xlog_update_meta_page(), hash_xlog_vacuum_one_page(), hashbucketcleanup(), hashbulkdelete(), heap_abort_speculative(), heap_delete(), heap_finish_speculative(), heap_force_common(), heap_inplace_update(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_page_prune_and_freeze(), heap_update(), heap_xlog_confirm(), heap_xlog_delete(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_prune_freeze(), heap_xlog_update(), heap_xlog_visible(), lazy_scan_new_or_empty(), lazy_scan_prune(), lazy_vacuum_heap_page(), log_newpage_range(), moveLeafs(), nextval_internal(), RelationAddBlocks(), RelationCopyStorageUsingBuffer(), RelationGetBufferForTuple(), revmap_physical_extend(), saveNodeLink(), seq_redo(), shiftList(), spgAddNodeAction(), spgbuild(), SpGistUpdateMetaPage(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), spgRedoSplitTuple(), spgRedoVacuumLeaf(), spgRedoVacuumRedirect(), spgRedoVacuumRoot(), spgSplitNodeAction(), vacuumLeafPage(), vacuumLeafRoot(), vacuumRedirectAndPlaceholder(), visibilitymap_clear(), visibilitymap_prepare_truncate(), visibilitymap_set(), writeListPage(), and XLogReadBufferForRedoExtended().

◆ MarkBufferDirtyHint()

void MarkBufferDirtyHint ( Buffer  buffer,
bool  buffer_std 
)

Definition at line 4960 of file bufmgr.c.

4961 {
4962  BufferDesc *bufHdr;
4963  Page page = BufferGetPage(buffer);
4964 
4965  if (!BufferIsValid(buffer))
4966  elog(ERROR, "bad buffer ID: %d", buffer);
4967 
4968  if (BufferIsLocal(buffer))
4969  {
4970  MarkLocalBufferDirty(buffer);
4971  return;
4972  }
4973 
4974  bufHdr = GetBufferDescriptor(buffer - 1);
4975 
4976  Assert(GetPrivateRefCount(buffer) > 0);
4977  /* here, either share or exclusive lock is OK */
4979 
4980  /*
4981  * This routine might get called many times on the same page, if we are
4982  * making the first scan after commit of an xact that added/deleted many
4983  * tuples. So, be as quick as we can if the buffer is already dirty. We
4984  * do this by not acquiring spinlock if it looks like the status bits are
4985  * already set. Since we make this test unlocked, there's a chance we
4986  * might fail to notice that the flags have just been cleared, and failed
4987  * to reset them, due to memory-ordering issues. But since this function
4988  * is only intended to be used in cases where failing to write out the
4989  * data would be harmless anyway, it doesn't really matter.
4990  */
4991  if ((pg_atomic_read_u32(&bufHdr->state) & (BM_DIRTY | BM_JUST_DIRTIED)) !=
4993  {
4995  bool dirtied = false;
4996  bool delayChkptFlags = false;
4997  uint32 buf_state;
4998 
4999  /*
5000  * If we need to protect hint bit updates from torn writes, WAL-log a
5001  * full page image of the page. This full page image is only necessary
5002  * if the hint bit update is the first change to the page since the
5003  * last checkpoint.
5004  *
5005  * We don't check full_page_writes here because that logic is included
5006  * when we call XLogInsert() since the value changes dynamically.
5007  */
5008  if (XLogHintBitIsNeeded() &&
5009  (pg_atomic_read_u32(&bufHdr->state) & BM_PERMANENT))
5010  {
5011  /*
5012  * If we must not write WAL, due to a relfilelocator-specific
5013  * condition or being in recovery, don't dirty the page. We can
5014  * set the hint, just not dirty the page as a result so the hint
5015  * is lost when we evict the page or shutdown.
5016  *
5017  * See src/backend/storage/page/README for longer discussion.
5018  */
5019  if (RecoveryInProgress() ||
5021  return;
5022 
5023  /*
5024  * If the block is already dirty because we either made a change
5025  * or set a hint already, then we don't need to write a full page
5026  * image. Note that aggressive cleaning of blocks dirtied by hint
5027  * bit setting would increase the call rate. Bulk setting of hint
5028  * bits would reduce the call rate...
5029  *
5030  * We must issue the WAL record before we mark the buffer dirty.
5031  * Otherwise we might write the page before we write the WAL. That
5032  * causes a race condition, since a checkpoint might occur between
5033  * writing the WAL record and marking the buffer dirty. We solve
5034  * that with a kluge, but one that is already in use during
5035  * transaction commit to prevent race conditions. Basically, we
5036  * simply prevent the checkpoint WAL record from being written
5037  * until we have marked the buffer dirty. We don't start the
5038  * checkpoint flush until we have marked dirty, so our checkpoint
5039  * must flush the change to disk successfully or the checkpoint
5040  * never gets written, so crash recovery will fix.
5041  *
5042  * It's possible we may enter here without an xid, so it is
5043  * essential that CreateCheckPoint waits for virtual transactions
5044  * rather than full transactionids.
5045  */
5048  delayChkptFlags = true;
5049  lsn = XLogSaveBufferForHint(buffer, buffer_std);
5050  }
5051 
5052  buf_state = LockBufHdr(bufHdr);
5053 
5054  Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
5055 
5056  if (!(buf_state & BM_DIRTY))
5057  {
5058  dirtied = true; /* Means "will be dirtied by this action" */
5059 
5060  /*
5061  * Set the page LSN if we wrote a backup block. We aren't supposed
5062  * to set this when only holding a share lock but as long as we
5063  * serialise it somehow we're OK. We choose to set LSN while
5064  * holding the buffer header lock, which causes any reader of an
5065  * LSN who holds only a share lock to also obtain a buffer header
5066  * lock before using PageGetLSN(), which is enforced in
5067  * BufferGetLSNAtomic().
5068  *
5069  * If checksums are enabled, you might think we should reset the
5070  * checksum here. That will happen when the page is written
5071  * sometime later in this checkpoint cycle.
5072  */
5073  if (!XLogRecPtrIsInvalid(lsn))
5074  PageSetLSN(page, lsn);
5075  }
5076 
5077  buf_state |= BM_DIRTY | BM_JUST_DIRTIED;
5078  UnlockBufHdr(bufHdr, buf_state);
5079 
5080  if (delayChkptFlags)
5082 
5083  if (dirtied)
5084  {
5085  VacuumPageDirty++;
5087  if (VacuumCostActive)
5089  }
5090  }
5091 }
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:388
#define DELAY_CHKPT_START
Definition: proc.h:114
bool RelFileLocatorSkippingWAL(RelFileLocator rlocator)
Definition: storage.c:532
int delayChkptFlags
Definition: proc.h:236
bool RecoveryInProgress(void)
Definition: xlog.c:6290
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr XLogSaveBufferForHint(Buffer buffer, bool buffer_std)
Definition: xloginsert.c:1065

References Assert, BM_DIRTY, BM_JUST_DIRTIED, BM_PERMANENT, BUF_STATE_GET_REFCOUNT, PrivateRefCountEntry::buffer, BufferDescriptorGetContentLock(), BufferGetPage(), BufferIsLocal, BufferIsValid(), BufTagGetRelFileLocator(), DELAY_CHKPT_START, PGPROC::delayChkptFlags, elog, ERROR, GetBufferDescriptor(), GetPrivateRefCount(), InvalidXLogRecPtr, LockBufHdr(), LWLockHeldByMe(), MarkLocalBufferDirty(), MyProc, PageSetLSN(), pg_atomic_read_u32(), pgBufferUsage, RecoveryInProgress(), RelFileLocatorSkippingWAL(), BufferUsage::shared_blks_dirtied, BufferDesc::state, BufferDesc::tag, UnlockBufHdr(), VacuumCostActive, VacuumCostBalance, VacuumCostPageDirty, VacuumPageDirty, XLogHintBitIsNeeded, XLogRecPtrIsInvalid, and XLogSaveBufferForHint().

Referenced by _bt_check_unique(), _bt_killitems(), _hash_kill_items(), brin_start_evacuating_page(), btvacuumpage(), fsm_search(), fsm_search_avail(), fsm_set_and_search(), fsm_vacuum_page(), gistkillitems(), heap_page_prune_and_freeze(), read_seq_tuple(), SetHintBits(), and XLogRecordPageWithFreeSpace().

◆ PrefetchBuffer()

PrefetchBufferResult PrefetchBuffer ( Relation  reln,
ForkNumber  forkNum,
BlockNumber  blockNum 
)

Definition at line 638 of file bufmgr.c.

639 {
640  Assert(RelationIsValid(reln));
641  Assert(BlockNumberIsValid(blockNum));
642 
643  if (RelationUsesLocalBuffers(reln))
644  {
645  /* see comments in ReadBufferExtended */
646  if (RELATION_IS_OTHER_TEMP(reln))
647  ereport(ERROR,
648  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
649  errmsg("cannot access temporary tables of other sessions")));
650 
651  /* pass it off to localbuf.c */
652  return PrefetchLocalBuffer(RelationGetSmgr(reln), forkNum, blockNum);
653  }
654  else
655  {
656  /* pass it to the shared buffer version */
657  return PrefetchSharedBuffer(RelationGetSmgr(reln), forkNum, blockNum);
658  }
659 }
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
PrefetchBufferResult PrefetchSharedBuffer(SMgrRelation smgr_reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:548
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
PrefetchBufferResult PrefetchLocalBuffer(SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum)
Definition: localbuf.c:69
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:658
#define RelationIsValid(relation)
Definition: rel.h:478

References Assert, BlockNumberIsValid(), ereport, errcode(), errmsg(), ERROR, PrefetchLocalBuffer(), PrefetchSharedBuffer(), RELATION_IS_OTHER_TEMP, RelationGetSmgr(), RelationIsValid, and RelationUsesLocalBuffers.

Referenced by BitmapPrefetch(), count_nondeletable_pages(), and pg_prewarm().

◆ PrefetchSharedBuffer()

PrefetchBufferResult PrefetchSharedBuffer ( struct SMgrRelationData smgr_reln,
ForkNumber  forkNum,
BlockNumber  blockNum 
)

◆ ReadBuffer()

Buffer ReadBuffer ( Relation  reln,
BlockNumber  blockNum 
)

Definition at line 745 of file bufmgr.c.

746 {
747  return ReadBufferExtended(reln, MAIN_FORKNUM, blockNum, RBM_NORMAL, NULL);
748 }
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:792

References MAIN_FORKNUM, RBM_NORMAL, and ReadBufferExtended().

Referenced by _bt_allocbuf(), _bt_getbuf(), _bt_search_insert(), _hash_getbuf(), _hash_getbuf_with_condlock_cleanup(), blbulkdelete(), blinsert(), BloomNewBuffer(), brin_getinsertbuffer(), brinGetStats(), brinGetTupleForHeapBlock(), brinRevmapDesummarizeRange(), brinRevmapInitialize(), bt_metap(), bt_multi_page_stats(), bt_page_items_internal(), bt_page_stats_internal(), ginFindLeafPage(), ginFindParents(), ginGetStats(), ginHeapTupleFastInsert(), ginInsertCleanup(), GinNewBuffer(), ginStepRight(), ginUpdateStats(), gistBufferingFindCorrectParent(), gistbufferinginserttuples(), gistdoinsert(), gistFindCorrectParent(), gistFindPath(), gistfixsplit(), gistGetMaxLevel(), gistkillitems(), gistNewBuffer(), gistProcessItup(), gistScanPage(), heap_abort_speculative(), heap_delete(), heap_fetch(), heap_finish_speculative(), heap_force_common(), heap_get_latest_tid(), heap_index_delete_tuples(), heap_inplace_update(), heap_lock_tuple(), heap_update(), initBloomState(), pg_visibility(), pgstatginindex_internal(), read_seq_tuple(), RelationGetBufferForTuple(), ReleaseAndReadBuffer(), revmap_get_buffer(), revmap_physical_extend(), scanGetCandidate(), scanPendingInsert(), shiftList(), spgdoinsert(), spgGetCache(), SpGistGetBuffer(), SpGistNewBuffer(), SpGistUpdateMetaPage(), and spgWalk().

◆ ReadBufferExtended()

Buffer ReadBufferExtended ( Relation  reln,
ForkNumber  forkNum,
BlockNumber  blockNum,
ReadBufferMode  mode,
BufferAccessStrategy  strategy 
)
inline

Definition at line 792 of file bufmgr.c.

794 {
795  Buffer buf;
796 
797  /*
798  * Reject attempts to read non-local temporary relations; we would be
799  * likely to get wrong data since we have no visibility into the owning
800  * session's local buffers.
801  */
802  if (RELATION_IS_OTHER_TEMP(reln))
803  ereport(ERROR,
804  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
805  errmsg("cannot access temporary tables of other sessions")));
806 
807  /*
808  * Read the buffer, and update pgstat counters to reflect a cache hit or
809  * miss.
810  */
811  buf = ReadBuffer_common(reln, RelationGetSmgr(reln), 0,
812  forkNum, blockNum, mode, strategy);
813 
814  return buf;
815 }

References buf, ereport, errcode(), errmsg(), ERROR, mode, ReadBuffer_common(), RELATION_IS_OTHER_TEMP, and RelationGetSmgr().

Referenced by _hash_getbuf_with_strategy(), _hash_getinitbuf(), _hash_getnewbuf(), autoprewarm_database_main(), blbulkdelete(), blgetbitmap(), BloomInitMetapage(), blvacuumcleanup(), brin_vacuum_scan(), bt_recheck_sibling_links(), btvacuumpage(), collect_corrupt_items(), collect_visibility_data(), count_nondeletable_pages(), fsm_readbuf(), get_raw_page_internal(), ginbulkdelete(), ginDeletePage(), ginScanToDelete(), ginvacuumcleanup(), ginVacuumPostingTree(), ginVacuumPostingTreeLeaves(), gistvacuum_delete_empty_pages(), gistvacuumpage(), hashbulkdelete(), heapam_scan_sample_next_block(), lazy_scan_heap(), lazy_vacuum_heap_rel(), log_newpage_range(), palloc_btree_page(), pgstat_btree_page(), pgstat_gist_page(), pgstat_heap(), pgstathashindex(), pgstatindex_impl(), ReadBuffer(), ReadBufferBI(), spgprocesspending(), spgvacuumpage(), statapprox_heap(), verify_heapam(), and vm_readbuf().

◆ ReadBufferWithoutRelcache()

Buffer ReadBufferWithoutRelcache ( RelFileLocator  rlocator,
ForkNumber  forkNum,
BlockNumber  blockNum,
ReadBufferMode  mode,
BufferAccessStrategy  strategy,
bool  permanent 
)

Definition at line 829 of file bufmgr.c.

832 {
833  SMgrRelation smgr = smgropen(rlocator, INVALID_PROC_NUMBER);
834 
835  return ReadBuffer_common(NULL, smgr,
836  permanent ? RELPERSISTENCE_PERMANENT : RELPERSISTENCE_UNLOGGED,
837  forkNum, blockNum,
838  mode, strategy);
839 }

References INVALID_PROC_NUMBER, mode, ReadBuffer_common(), and smgropen().

Referenced by RelationCopyStorageUsingBuffer(), ScanSourceDatabasePgClass(), and XLogReadBufferExtended().

◆ ReadRecentBuffer()

bool ReadRecentBuffer ( RelFileLocator  rlocator,
ForkNumber  forkNum,
BlockNumber  blockNum,
Buffer  recent_buffer 
)

Definition at line 669 of file bufmgr.c.

671 {
672  BufferDesc *bufHdr;
673  BufferTag tag;
674  uint32 buf_state;
675  bool have_private_ref;
676 
677  Assert(BufferIsValid(recent_buffer));
678 
681  InitBufferTag(&tag, &rlocator, forkNum, blockNum);
682 
683  if (BufferIsLocal(recent_buffer))
684  {
685  int b = -recent_buffer - 1;
686 
687  bufHdr = GetLocalBufferDescriptor(b);
688  buf_state = pg_atomic_read_u32(&bufHdr->state);
689 
690  /* Is it still valid and holding the right tag? */
691  if ((buf_state & BM_VALID) && BufferTagsEqual(&tag, &bufHdr->tag))
692  {
693  PinLocalBuffer(bufHdr, true);
694 
696 
697  return true;
698  }
699  }
700  else
701  {
702  bufHdr = GetBufferDescriptor(recent_buffer - 1);
703  have_private_ref = GetPrivateRefCount(recent_buffer) > 0;
704 
705  /*
706  * Do we already have this buffer pinned with a private reference? If
707  * so, it must be valid and it is safe to check the tag without
708  * locking. If not, we have to lock the header first and then check.
709  */
710  if (have_private_ref)
711  buf_state = pg_atomic_read_u32(&bufHdr->state);
712  else
713  buf_state = LockBufHdr(bufHdr);
714 
715  if ((buf_state & BM_VALID) && BufferTagsEqual(&tag, &bufHdr->tag))
716  {
717  /*
718  * It's now safe to pin the buffer. We can't pin first and ask
719  * questions later, because it might confuse code paths like
720  * InvalidateBuffer() if we pinned a random non-matching buffer.
721  */
722  if (have_private_ref)
723  PinBuffer(bufHdr, NULL); /* bump pin count */
724  else
725  PinBuffer_Locked(bufHdr); /* pin for first time */
726 
728 
729  return true;
730  }
731 
732  /* If we locked the header above, now unlock. */
733  if (!have_private_ref)
734  UnlockBufHdr(bufHdr, buf_state);
735  }
736 
737  return false;
738 }
static void InitBufferTag(BufferTag *tag, const RelFileLocator *rlocator, ForkNumber forkNum, BlockNumber blockNum)
static bool BufferTagsEqual(const BufferTag *tag1, const BufferTag *tag2)
static bool PinBuffer(BufferDesc *buf, BufferAccessStrategy strategy)
Definition: bufmgr.c:2641
int b
Definition: isn.c:70
bool PinLocalBuffer(BufferDesc *buf_hdr, bool adjust_usagecount)
Definition: localbuf.c:655
int64 local_blks_hit
Definition: instrument.h:30
int64 shared_blks_hit
Definition: instrument.h:26

References Assert, b, BM_VALID, BufferIsLocal, BufferIsValid(), BufferTagsEqual(), CurrentResourceOwner, GetBufferDescriptor(), GetLocalBufferDescriptor(), GetPrivateRefCount(), InitBufferTag(), BufferUsage::local_blks_hit, LockBufHdr(), pg_atomic_read_u32(), pgBufferUsage, PinBuffer(), PinBuffer_Locked(), PinLocalBuffer(), ReservePrivateRefCountEntry(), ResourceOwnerEnlarge(), BufferUsage::shared_blks_hit, BufferDesc::state, BufferDesc::tag, and UnlockBufHdr().

Referenced by XLogReadBufferExtended().

◆ RelationGetNumberOfBlocksInFork()

BlockNumber RelationGetNumberOfBlocksInFork ( Relation  relation,
ForkNumber  forkNum 
)

Definition at line 3912 of file bufmgr.c.

3913 {
3914  if (RELKIND_HAS_TABLE_AM(relation->rd_rel->relkind))
3915  {
3916  /*
3917  * Not every table AM uses BLCKSZ wide fixed size blocks. Therefore
3918  * tableam returns the size in bytes - but for the purpose of this
3919  * routine, we want the number of blocks. Therefore divide, rounding
3920  * up.
3921  */
3922  uint64 szbytes;
3923 
3924  szbytes = table_relation_size(relation, forkNum);
3925 
3926  return (szbytes + (BLCKSZ - 1)) / BLCKSZ;
3927  }
3928  else if (RELKIND_HAS_STORAGE(relation->rd_rel->relkind))
3929  {
3930  return smgrnblocks(RelationGetSmgr(relation), forkNum);
3931  }
3932  else
3933  Assert(false);
3934 
3935  return 0; /* keep compiler quiet */
3936 }
static uint64 table_relation_size(Relation rel, ForkNumber forkNumber)
Definition: tableam.h:1878

References Assert, RelationData::rd_rel, RelationGetSmgr(), smgrnblocks(), and table_relation_size().

Referenced by _hash_getnewbuf(), _hash_init(), autoprewarm_database_main(), get_raw_page_internal(), and pg_prewarm().

◆ ReleaseAndReadBuffer()

Buffer ReleaseAndReadBuffer ( Buffer  buffer,
Relation  relation,
BlockNumber  blockNum 
)

Definition at line 2583 of file bufmgr.c.

2586 {
2587  ForkNumber forkNum = MAIN_FORKNUM;
2588  BufferDesc *bufHdr;
2589 
2590  if (BufferIsValid(buffer))
2591  {
2592  Assert(BufferIsPinned(buffer));
2593  if (BufferIsLocal(buffer))
2594  {
2595  bufHdr = GetLocalBufferDescriptor(-buffer - 1);
2596  if (bufHdr->tag.blockNum == blockNum &&
2597  BufTagMatchesRelFileLocator(&bufHdr->tag, &relation->rd_locator) &&
2598  BufTagGetForkNum(&bufHdr->tag) == forkNum)
2599  return buffer;
2600  UnpinLocalBuffer(buffer);
2601  }
2602  else
2603  {
2604  bufHdr = GetBufferDescriptor(buffer - 1);
2605  /* we have pin, so it's ok to examine tag without spinlock */
2606  if (bufHdr->tag.blockNum == blockNum &&
2607  BufTagMatchesRelFileLocator(&bufHdr->tag, &relation->rd_locator) &&
2608  BufTagGetForkNum(&bufHdr->tag) == forkNum)
2609  return buffer;
2610  UnpinBuffer(bufHdr);
2611  }
2612  }
2613 
2614  return ReadBuffer(relation, blockNum);
2615 }
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:745
void UnpinLocalBuffer(Buffer buffer)
Definition: localbuf.c:681

References Assert, buftag::blockNum, PrivateRefCountEntry::buffer, BufferIsLocal, BufferIsPinned, BufferIsValid(), BufTagGetForkNum(), BufTagMatchesRelFileLocator(), GetBufferDescriptor(), GetLocalBufferDescriptor(), MAIN_FORKNUM, RelationData::rd_locator, ReadBuffer(), BufferDesc::tag, UnpinBuffer(), and UnpinLocalBuffer().

Referenced by _bt_relandgetbuf(), ginFindLeafPage(), heapam_index_fetch_tuple(), and heapam_scan_bitmap_next_block().

◆ ReleaseBuffer()

void ReleaseBuffer ( Buffer  buffer)

Definition at line 4896 of file bufmgr.c.

4897 {
4898  if (!BufferIsValid(buffer))
4899  elog(ERROR, "bad buffer ID: %d", buffer);
4900 
4901  if (BufferIsLocal(buffer))
4902  UnpinLocalBuffer(buffer);
4903  else
4904  UnpinBuffer(GetBufferDescriptor(buffer - 1));
4905 }

References PrivateRefCountEntry::buffer, BufferIsLocal, BufferIsValid(), elog, ERROR, GetBufferDescriptor(), UnpinBuffer(), and UnpinLocalBuffer().

Referenced by _bt_allocbuf(), _bt_drop_lock_and_maybe_pin(), _bt_pagedel(), _bt_relbuf(), _bt_search_insert(), _bt_unlink_halfdead_page(), _hash_dropbuf(), _hash_getbuf_with_condlock_cleanup(), autoprewarm_database_main(), blinsert(), BloomNewBuffer(), brin_getinsertbuffer(), brin_vacuum_scan(), bringetbitmap(), brinGetTupleForHeapBlock(), brininsert(), brinRevmapTerminate(), brinsummarize(), collect_corrupt_items(), collect_visibility_data(), entryLoadMoreItems(), ExecEndBitmapHeapScan(), ExecEndIndexOnlyScan(), ExecReScanBitmapHeapScan(), ExtendBufferedRelTo(), FreeBulkInsertState(), freeGinBtreeStack(), fsm_search(), fsm_vacuum_page(), get_actual_variable_endpoint(), get_raw_page_internal(), GetRecordedFreeSpace(), ginDeletePage(), ginFindParents(), ginFinishSplit(), ginFreeScanKeys(), ginInsertCleanup(), GinNewBuffer(), ginScanToDelete(), gistdoinsert(), gistFindCorrectParent(), gistNewBuffer(), gistvacuum_delete_empty_pages(), heap_abort_speculative(), heap_delete(), heap_endscan(), heap_fetch(), heap_fetch_next_buffer(), heap_force_common(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_rescan(), heap_update(), heap_vac_scan_next_block(), heap_xlog_delete(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_update(), heap_xlog_visible(), heapam_index_fetch_reset(), heapam_scan_sample_next_block(), heapam_tuple_lock(), heapgettup(), heapgettup_pagemode(), lazy_scan_heap(), lazy_vacuum_heap_rel(), pg_prewarm(), pg_visibility(), pg_visibility_map(), pg_visibility_map_summary(), pgstatindex_impl(), read_stream_reset(), ReadBufferBI(), RelationAddBlocks(), RelationGetBufferForTuple(), ReleaseBulkInsertStatePin(), revmap_get_buffer(), spgdoinsert(), SpGistGetBuffer(), SpGistNewBuffer(), SpGistUpdateMetaPage(), statapprox_heap(), summarize_range(), terminate_brin_buildstate(), tts_buffer_heap_clear(), tts_buffer_heap_materialize(), tts_buffer_heap_store_tuple(), UnlockReleaseBuffer(), verify_heapam(), visibilitymap_count(), visibilitymap_get_status(), visibilitymap_pin(), and XLogReadBufferExtended().

◆ StartReadBuffer()

bool StartReadBuffer ( ReadBuffersOperation operation,
Buffer buffer,
BlockNumber  blocknum,
int  flags 
)

Definition at line 1367 of file bufmgr.c.

1371 {
1372  int nblocks = 1;
1373  bool result;
1374 
1375  result = StartReadBuffersImpl(operation, buffer, blocknum, &nblocks, flags);
1376  Assert(nblocks == 1); /* single block can't be short */
1377 
1378  return result;
1379 }
static pg_attribute_always_inline bool StartReadBuffersImpl(ReadBuffersOperation *operation, Buffer *buffers, BlockNumber blockNum, int *nblocks, int flags)
Definition: bufmgr.c:1257

References Assert, PrivateRefCountEntry::buffer, and StartReadBuffersImpl().

Referenced by read_stream_next_buffer(), and ReadBuffer_common().

◆ StartReadBuffers()

bool StartReadBuffers ( ReadBuffersOperation operation,
Buffer buffers,
BlockNumber  blockNum,
int *  nblocks,
int  flags 
)

Definition at line 1352 of file bufmgr.c.

1357 {
1358  return StartReadBuffersImpl(operation, buffers, blockNum, nblocks, flags);
1359 }

References StartReadBuffersImpl().

Referenced by read_stream_start_pending_read().

◆ UnlockBuffers()

void UnlockBuffers ( void  )

Definition at line 5103 of file bufmgr.c.

5104 {
5106 
5107  if (buf)
5108  {
5109  uint32 buf_state;
5110 
5111  buf_state = LockBufHdr(buf);
5112 
5113  /*
5114  * Don't complain if flag bit not set; it could have been reset but we
5115  * got a cancel/die interrupt before getting the signal.
5116  */
5117  if ((buf_state & BM_PIN_COUNT_WAITER) != 0 &&
5118  buf->wait_backend_pgprocno == MyProcNumber)
5119  buf_state &= ~BM_PIN_COUNT_WAITER;
5120 
5121  UnlockBufHdr(buf, buf_state);
5122 
5123  PinCountWaitBuf = NULL;
5124  }
5125 }

References BM_PIN_COUNT_WAITER, buf, LockBufHdr(), MyProcNumber, PinCountWaitBuf, and UnlockBufHdr().

Referenced by AbortSubTransaction(), AbortTransaction(), AtProcExit_Buffers(), BackgroundWriterMain(), CheckpointerMain(), and WalWriterMain().

◆ UnlockReleaseBuffer()

void UnlockReleaseBuffer ( Buffer  buffer)

Definition at line 4913 of file bufmgr.c.

4914 {
4915  LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
4916  ReleaseBuffer(buffer);
4917 }

References PrivateRefCountEntry::buffer, BUFFER_LOCK_UNLOCK, LockBuffer(), and ReleaseBuffer().

Referenced by _bt_clear_incomplete_split(), _bt_restore_meta(), _hash_relbuf(), allocNewBuffer(), AlterSequence(), blbulkdelete(), blgetbitmap(), blinsert(), BloomInitMetapage(), blvacuumcleanup(), brin_doinsert(), brin_doupdate(), brin_evacuate_page(), brin_getinsertbuffer(), brin_xlog_createidx(), brin_xlog_desummarize_page(), brin_xlog_insert_update(), brin_xlog_revmap_extend(), brin_xlog_samepage_update(), brin_xlog_update(), brinbuild(), brinbuildempty(), brinGetStats(), brinRevmapDesummarizeRange(), bt_metap(), bt_multi_page_stats(), bt_page_items_internal(), bt_page_stats_internal(), bt_recheck_sibling_links(), btree_xlog_dedup(), btree_xlog_delete(), btree_xlog_insert(), btree_xlog_mark_page_halfdead(), btree_xlog_newroot(), btree_xlog_split(), btree_xlog_unlink_page(), btree_xlog_vacuum(), collect_corrupt_items(), collect_visibility_data(), count_nondeletable_pages(), createPostingTree(), do_setval(), doPickSplit(), entryLoadMoreItems(), fill_seq_fork_with_data(), flushCachedPage(), FreeSpaceMapPrepareTruncateRel(), fsm_search(), fsm_set_and_search(), generic_redo(), ginbuild(), ginbuildempty(), ginbulkdelete(), ginGetStats(), ginHeapTupleFastInsert(), ginInsertCleanup(), ginPlaceToPage(), ginRedoClearIncompleteSplit(), ginRedoCreatePTree(), ginRedoDeleteListPages(), ginRedoDeletePage(), ginRedoInsert(), ginRedoInsertListPage(), ginRedoSplit(), ginRedoUpdateMetapage(), ginRedoVacuumDataLeafPage(), ginRedoVacuumPage(), ginScanToDelete(), ginStepRight(), ginUpdateStats(), ginvacuumcleanup(), ginVacuumPostingTree(), ginVacuumPostingTreeLeaves(), gistbufferinginserttuples(), gistbuild(), gistbuildempty(), gistdoinsert(), gistFindCorrectParent(), gistFindPath(), gistGetMaxLevel(), gistinserttuples(), gistkillitems(), gistplacetopage(), gistProcessItup(), gistRedoClearFollowRight(), gistRedoDeleteRecord(), gistRedoPageDelete(), gistRedoPageSplitRecord(), gistRedoPageUpdateRecord(), gistScanPage(), gistvacuum_delete_empty_pages(), gistvacuumpage(), hash_xlog_add_ovfl_page(), hash_xlog_delete(), hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), hash_xlog_insert(), hash_xlog_move_page_contents(), hash_xlog_split_allocate_page(), hash_xlog_split_cleanup(), hash_xlog_split_complete(), hash_xlog_split_page(), hash_xlog_squeeze_page(), hash_xlog_update_meta_page(), hash_xlog_vacuum_one_page(), heap_delete(), heap_finish_speculative(), heap_force_common(), heap_get_latest_tid(), heap_index_delete_tuples(), heap_inplace_update(), heap_insert(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_update(), heap_xlog_confirm(), heap_xlog_delete(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_prune_freeze(), heap_xlog_update(), heap_xlog_visible(), heapam_scan_analyze_next_tuple(), initBloomState(), lazy_scan_heap(), lazy_scan_new_or_empty(), lazy_vacuum_heap_rel(), log_newpage_range(), moveLeafs(), nextval_internal(), palloc_btree_page(), pg_sequence_last_value(), pg_visibility(), pgstat_gist_page(), pgstat_heap(), pgstatginindex_internal(), pgstathashindex(), RelationCopyStorageUsingBuffer(), RelationGetBufferForTuple(), ResetSequence(), revmap_physical_extend(), scanGetCandidate(), scanPendingInsert(), scanPostingTree(), ScanSourceDatabasePgClass(), seq_redo(), SequenceChangePersistence(), shiftList(), spgAddNodeAction(), spgbuild(), spgdoinsert(), spgGetCache(), SpGistGetBuffer(), SpGistUpdateMetaPage(), spgMatchNodeAction(), spgprocesspending(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), spgRedoSplitTuple(), spgRedoVacuumLeaf(), spgRedoVacuumRedirect(), spgRedoVacuumRoot(), spgSplitNodeAction(), spgvacuumpage(), spgWalk(), statapprox_heap(), verify_heapam(), verifyBackupPageConsistency(), visibilitymap_prepare_truncate(), writeListPage(), xlog_redo(), and XLogRecordPageWithFreeSpace().

◆ WaitReadBuffers()

void WaitReadBuffers ( ReadBuffersOperation operation)

Definition at line 1395 of file bufmgr.c.

1396 {
1397  Buffer *buffers;
1398  int nblocks;
1399  BlockNumber blocknum;
1400  ForkNumber forknum;
1401  IOContext io_context;
1402  IOObject io_object;
1403  char persistence;
1404 
1405  /*
1406  * Currently operations are only allowed to include a read of some range,
1407  * with an optional extra buffer that is already pinned at the end. So
1408  * nblocks can be at most one more than io_buffers_len.
1409  */
1410  Assert((operation->nblocks == operation->io_buffers_len) ||
1411  (operation->nblocks == operation->io_buffers_len + 1));
1412 
1413  /* Find the range of the physical read we need to perform. */
1414  nblocks = operation->io_buffers_len;
1415  if (nblocks == 0)
1416  return; /* nothing to do */
1417 
1418  buffers = &operation->buffers[0];
1419  blocknum = operation->blocknum;
1420  forknum = operation->forknum;
1421 
1422  persistence = operation->rel
1423  ? operation->rel->rd_rel->relpersistence
1424  : RELPERSISTENCE_PERMANENT;
1425  if (persistence == RELPERSISTENCE_TEMP)
1426  {
1427  io_context = IOCONTEXT_NORMAL;
1428  io_object = IOOBJECT_TEMP_RELATION;
1429  }
1430  else
1431  {
1432  io_context = IOContextForStrategy(operation->strategy);
1433  io_object = IOOBJECT_RELATION;
1434  }
1435 
1436  /*
1437  * We count all these blocks as read by this backend. This is traditional
1438  * behavior, but might turn out to be not true if we find that someone
1439  * else has beaten us and completed the read of some of these blocks. In
1440  * that case the system globally double-counts, but we traditionally don't
1441  * count this as a "hit", and we don't have a separate counter for "miss,
1442  * but another backend completed the read".
1443  */
1444  if (persistence == RELPERSISTENCE_TEMP)
1445  pgBufferUsage.local_blks_read += nblocks;
1446  else
1447  pgBufferUsage.shared_blks_read += nblocks;
1448 
1449  for (int i = 0; i < nblocks; ++i)
1450  {
1451  int io_buffers_len;
1452  Buffer io_buffers[MAX_IO_COMBINE_LIMIT];
1453  void *io_pages[MAX_IO_COMBINE_LIMIT];
1454  instr_time io_start;
1455  BlockNumber io_first_block;
1456 
1457  /*
1458  * Skip this block if someone else has already completed it. If an
1459  * I/O is already in progress in another backend, this will wait for
1460  * the outcome: either done, or something went wrong and we will
1461  * retry.
1462  */
1463  if (!WaitReadBuffersCanStartIO(buffers[i], false))
1464  {
1465  /*
1466  * Report this as a 'hit' for this backend, even though it must
1467  * have started out as a miss in PinBufferForBlock().
1468  */
1469  TRACE_POSTGRESQL_BUFFER_READ_DONE(forknum, blocknum + i,
1470  operation->smgr->smgr_rlocator.locator.spcOid,
1471  operation->smgr->smgr_rlocator.locator.dbOid,
1472  operation->smgr->smgr_rlocator.locator.relNumber,
1473  operation->smgr->smgr_rlocator.backend,
1474  true);
1475  continue;
1476  }
1477 
1478  /* We found a buffer that we need to read in. */
1479  io_buffers[0] = buffers[i];
1480  io_pages[0] = BufferGetBlock(buffers[i]);
1481  io_first_block = blocknum + i;
1482  io_buffers_len = 1;
1483 
1484  /*
1485  * How many neighboring-on-disk blocks can we can scatter-read into
1486  * other buffers at the same time? In this case we don't wait if we
1487  * see an I/O already in progress. We already hold BM_IO_IN_PROGRESS
1488  * for the head block, so we should get on with that I/O as soon as
1489  * possible. We'll come back to this block again, above.
1490  */
1491  while ((i + 1) < nblocks &&
1492  WaitReadBuffersCanStartIO(buffers[i + 1], true))
1493  {
1494  /* Must be consecutive block numbers. */
1495  Assert(BufferGetBlockNumber(buffers[i + 1]) ==
1496  BufferGetBlockNumber(buffers[i]) + 1);
1497 
1498  io_buffers[io_buffers_len] = buffers[++i];
1499  io_pages[io_buffers_len++] = BufferGetBlock(buffers[i]);
1500  }
1501 
1503  smgrreadv(operation->smgr, forknum, io_first_block, io_pages, io_buffers_len);
1504  pgstat_count_io_op_time(io_object, io_context, IOOP_READ, io_start,
1505  io_buffers_len);
1506 
1507  /* Verify each block we read, and terminate the I/O. */
1508  for (int j = 0; j < io_buffers_len; ++j)
1509  {
1510  BufferDesc *bufHdr;
1511  Block bufBlock;
1512 
1513  if (persistence == RELPERSISTENCE_TEMP)
1514  {
1515  bufHdr = GetLocalBufferDescriptor(-io_buffers[j] - 1);
1516  bufBlock = LocalBufHdrGetBlock(bufHdr);
1517  }
1518  else
1519  {
1520  bufHdr = GetBufferDescriptor(io_buffers[j] - 1);
1521  bufBlock = BufHdrGetBlock(bufHdr);
1522  }
1523 
1524  /* check for garbage data */
1525  if (!PageIsVerifiedExtended((Page) bufBlock, io_first_block + j,
1527  {
1528  if ((operation->flags & READ_BUFFERS_ZERO_ON_ERROR) || zero_damaged_pages)
1529  {
1530  ereport(WARNING,
1532  errmsg("invalid page in block %u of relation %s; zeroing out page",
1533  io_first_block + j,
1534  relpath(operation->smgr->smgr_rlocator, forknum))));
1535  memset(bufBlock, 0, BLCKSZ);
1536  }
1537  else
1538  ereport(ERROR,
1540  errmsg("invalid page in block %u of relation %s",
1541  io_first_block + j,
1542  relpath(operation->smgr->smgr_rlocator, forknum))));
1543  }
1544 
1545  /* Terminate I/O and set BM_VALID. */
1546  if (persistence == RELPERSISTENCE_TEMP)
1547  {
1548  uint32 buf_state = pg_atomic_read_u32(&bufHdr->state);
1549 
1550  buf_state |= BM_VALID;
1551  pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
1552  }
1553  else
1554  {
1555  /* Set BM_VALID, terminate IO, and wake up any waiters */
1556  TerminateBufferIO(bufHdr, false, BM_VALID, true);
1557  }
1558 
1559  /* Report I/Os as completing individually. */
1560  TRACE_POSTGRESQL_BUFFER_READ_DONE(forknum, io_first_block + j,
1561  operation->smgr->smgr_rlocator.locator.spcOid,
1562  operation->smgr->smgr_rlocator.locator.dbOid,
1563  operation->smgr->smgr_rlocator.locator.relNumber,
1564  operation->smgr->smgr_rlocator.backend,
1565  false);
1566  }
1567 
1568  VacuumPageMiss += io_buffers_len;
1569  if (VacuumCostActive)
1570  VacuumCostBalance += VacuumCostPageMiss * io_buffers_len;
1571  }
1572 }
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3713
static bool WaitReadBuffersCanStartIO(Buffer buffer, bool nowait)
Definition: bufmgr.c:1382
bool zero_damaged_pages
Definition: bufmgr.c:139
static void TerminateBufferIO(BufferDesc *buf, bool clear_dirty, uint32 set_flag_bits, bool forget_owner)
Definition: bufmgr.c:5588
#define BufHdrGetBlock(bufHdr)
Definition: bufmgr.c:67
#define READ_BUFFERS_ZERO_ON_ERROR
Definition: bufmgr.h:111
#define MAX_IO_COMBINE_LIMIT
Definition: bufmgr.h:168
bool PageIsVerifiedExtended(Page page, BlockNumber blkno, int flags)
Definition: bufpage.c:88
#define PIV_LOG_WARNING
Definition: bufpage.h:465
#define PIV_REPORT_STAT
Definition: bufpage.h:466
#define WARNING
Definition: elog.h:36
IOContext IOContextForStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:758
int VacuumCostPageMiss
Definition: globals.c:149
int64 VacuumPageMiss
Definition: globals.c:155
int j
Definition: isn.c:74
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
IOObject
Definition: pgstat.h:279
IOContext
Definition: pgstat.h:287
@ IOOP_READ
Definition: pgstat.h:302
#define relpath(rlocator, forknum)
Definition: relpath.h:94
void smgrreadv(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
Definition: smgr.c:600
int64 shared_blks_read
Definition: instrument.h:27
int64 local_blks_read
Definition: instrument.h:31
ForkNumber forknum
Definition: bufmgr.h:125
int16 io_buffers_len
Definition: bufmgr.h:137
Buffer * buffers
Definition: bufmgr.h:133
BufferAccessStrategy strategy
Definition: bufmgr.h:126
BlockNumber blocknum
Definition: bufmgr.h:134
struct SMgrRelationData * smgr
Definition: bufmgr.h:123
RelFileLocator locator
RelFileNumber relNumber
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:37

References Assert, RelFileLocatorBackend::backend, ReadBuffersOperation::blocknum, BM_VALID, BufferGetBlock(), BufferGetBlockNumber(), ReadBuffersOperation::buffers, BufHdrGetBlock, RelFileLocator::dbOid, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg(), ERROR, ReadBuffersOperation::flags, ReadBuffersOperation::forknum, GetBufferDescriptor(), GetLocalBufferDescriptor(), i, ReadBuffersOperation::io_buffers_len, IOCONTEXT_NORMAL, IOContextForStrategy(), IOOBJECT_RELATION, IOOBJECT_TEMP_RELATION, IOOP_READ, j, BufferUsage::local_blks_read, LocalBufHdrGetBlock, RelFileLocatorBackend::locator, MAX_IO_COMBINE_LIMIT, ReadBuffersOperation::nblocks, PageIsVerifiedExtended(), pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), pgBufferUsage, pgstat_count_io_op_time(), pgstat_prepare_io_time(), PIV_LOG_WARNING, PIV_REPORT_STAT, RelationData::rd_rel, READ_BUFFERS_ZERO_ON_ERROR, ReadBuffersOperation::rel, RelFileLocator::relNumber, relpath, BufferUsage::shared_blks_read, ReadBuffersOperation::smgr, SMgrRelationData::smgr_rlocator, smgrreadv(), RelFileLocator::spcOid, BufferDesc::state, ReadBuffersOperation::strategy, TerminateBufferIO(), track_io_timing, VacuumCostActive, VacuumCostBalance, VacuumCostPageMiss, VacuumPageMiss, WaitReadBuffersCanStartIO(), WARNING, and zero_damaged_pages.

Referenced by read_stream_next_buffer(), and ReadBuffer_common().

Variable Documentation

◆ backend_flush_after

PGDLLIMPORT int backend_flush_after
extern

Definition at line 172 of file bufmgr.c.

Referenced by InitBufferPool().

◆ bgwriter_flush_after

PGDLLIMPORT int bgwriter_flush_after
extern

Definition at line 171 of file bufmgr.c.

Referenced by BackgroundWriterMain().

◆ bgwriter_lru_maxpages

PGDLLIMPORT int bgwriter_lru_maxpages
extern

Definition at line 140 of file bufmgr.c.

Referenced by BgBufferSync().

◆ bgwriter_lru_multiplier

PGDLLIMPORT double bgwriter_lru_multiplier
extern

Definition at line 141 of file bufmgr.c.

Referenced by BgBufferSync().

◆ BufferBlocks

PGDLLIMPORT char* BufferBlocks
extern

Definition at line 22 of file buf_init.c.

Referenced by BufferGetBlock(), and InitBufferPool().

◆ checkpoint_flush_after

PGDLLIMPORT int checkpoint_flush_after
extern

Definition at line 170 of file bufmgr.c.

Referenced by BufferSync().

◆ effective_io_concurrency

PGDLLIMPORT int effective_io_concurrency
extern

◆ io_combine_limit

◆ LocalBufferBlockPointers

PGDLLIMPORT Block* LocalBufferBlockPointers
extern

Definition at line 45 of file localbuf.c.

Referenced by BufferGetBlock(), and InitLocalBuffers().

◆ LocalRefCount

◆ maintenance_io_concurrency

◆ NBuffers

◆ NLocBuffer

◆ track_io_timing

◆ zero_damaged_pages

PGDLLIMPORT bool zero_damaged_pages
extern

Definition at line 139 of file bufmgr.c.

Referenced by mdreadv(), and WaitReadBuffers().