PostgreSQL Source Code  git master
buf_internals.h File Reference
#include "port/atomics.h"
#include "storage/buf.h"
#include "storage/bufmgr.h"
#include "storage/condition_variable.h"
#include "storage/latch.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "storage/smgr.h"
#include "storage/spin.h"
#include "utils/relcache.h"
Include dependency graph for buf_internals.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  buftag
 
struct  BufferDesc
 
union  BufferDescPadded
 
struct  PendingWriteback
 
struct  WritebackContext
 
struct  CkptSortItem
 

Macros

#define BUF_REFCOUNT_ONE   1
 
#define BUF_REFCOUNT_MASK   ((1U << 18) - 1)
 
#define BUF_USAGECOUNT_MASK   0x003C0000U
 
#define BUF_USAGECOUNT_ONE   (1U << 18)
 
#define BUF_USAGECOUNT_SHIFT   18
 
#define BUF_FLAG_MASK   0xFFC00000U
 
#define BUF_STATE_GET_REFCOUNT(state)   ((state) & BUF_REFCOUNT_MASK)
 
#define BUF_STATE_GET_USAGECOUNT(state)   (((state) & BUF_USAGECOUNT_MASK) >> BUF_USAGECOUNT_SHIFT)
 
#define BM_LOCKED   (1U << 22) /* buffer header is locked */
 
#define BM_DIRTY   (1U << 23) /* data needs writing */
 
#define BM_VALID   (1U << 24) /* data is valid */
 
#define BM_TAG_VALID   (1U << 25) /* tag is assigned */
 
#define BM_IO_IN_PROGRESS   (1U << 26) /* read or write in progress */
 
#define BM_IO_ERROR   (1U << 27) /* previous I/O failed */
 
#define BM_JUST_DIRTIED   (1U << 28) /* dirtied since write started */
 
#define BM_PIN_COUNT_WAITER   (1U << 29) /* have waiter for sole pin */
 
#define BM_CHECKPOINT_NEEDED   (1U << 30) /* must write for checkpoint */
 
#define BM_PERMANENT
 
#define BM_MAX_USAGE_COUNT   5
 
#define BUFFERDESC_PAD_TO_SIZE   (SIZEOF_VOID_P == 8 ? 64 : 1)
 
#define FREENEXT_END_OF_LIST   (-1)
 
#define FREENEXT_NOT_IN_LIST   (-2)
 

Typedefs

typedef struct buftag BufferTag
 
typedef struct BufferDesc BufferDesc
 
typedef union BufferDescPadded BufferDescPadded
 
typedef struct PendingWriteback PendingWriteback
 
typedef struct WritebackContext WritebackContext
 
typedef struct CkptSortItem CkptSortItem
 

Functions

static RelFileNumber BufTagGetRelNumber (const BufferTag *tag)
 
static ForkNumber BufTagGetForkNum (const BufferTag *tag)
 
static void BufTagSetRelForkDetails (BufferTag *tag, RelFileNumber relnumber, ForkNumber forknum)
 
static RelFileLocator BufTagGetRelFileLocator (const BufferTag *tag)
 
static void ClearBufferTag (BufferTag *tag)
 
static void InitBufferTag (BufferTag *tag, const RelFileLocator *rlocator, ForkNumber forkNum, BlockNumber blockNum)
 
static bool BufferTagsEqual (const BufferTag *tag1, const BufferTag *tag2)
 
static bool BufTagMatchesRelFileLocator (const BufferTag *tag, const RelFileLocator *rlocator)
 
static uint32 BufTableHashPartition (uint32 hashcode)
 
static LWLockBufMappingPartitionLock (uint32 hashcode)
 
static LWLockBufMappingPartitionLockByIndex (uint32 index)
 
static BufferDescGetBufferDescriptor (uint32 id)
 
static BufferDescGetLocalBufferDescriptor (uint32 id)
 
static Buffer BufferDescriptorGetBuffer (const BufferDesc *bdesc)
 
static ConditionVariableBufferDescriptorGetIOCV (const BufferDesc *bdesc)
 
static LWLockBufferDescriptorGetContentLock (const BufferDesc *bdesc)
 
uint32 LockBufHdr (BufferDesc *desc)
 
static void UnlockBufHdr (BufferDesc *desc, uint32 buf_state)
 
void WritebackContextInit (WritebackContext *context, int *max_pending)
 
void IssuePendingWritebacks (WritebackContext *context)
 
void ScheduleBufferTagForWriteback (WritebackContext *context, BufferTag *tag)
 
BufferDescStrategyGetBuffer (BufferAccessStrategy strategy, uint32 *buf_state)
 
void StrategyFreeBuffer (BufferDesc *buf)
 
bool StrategyRejectBuffer (BufferAccessStrategy strategy, BufferDesc *buf)
 
int StrategySyncStart (uint32 *complete_passes, uint32 *num_buf_alloc)
 
void StrategyNotifyBgWriter (int bgwprocno)
 
Size StrategyShmemSize (void)
 
void StrategyInitialize (bool init)
 
bool have_free_buffer (void)
 
Size BufTableShmemSize (int size)
 
void InitBufTable (int size)
 
uint32 BufTableHashCode (BufferTag *tagPtr)
 
int BufTableLookup (BufferTag *tagPtr, uint32 hashcode)
 
int BufTableInsert (BufferTag *tagPtr, uint32 hashcode, int buf_id)
 
void BufTableDelete (BufferTag *tagPtr, uint32 hashcode)
 
PrefetchBufferResult PrefetchLocalBuffer (SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum)
 
BufferDescLocalBufferAlloc (SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum, bool *foundPtr)
 
void MarkLocalBufferDirty (Buffer buffer)
 
void DropRelationLocalBuffers (RelFileLocator rlocator, ForkNumber forkNum, BlockNumber firstDelBlock)
 
void DropRelationAllLocalBuffers (RelFileLocator rlocator)
 
void AtEOXact_LocalBuffers (bool isCommit)
 

Variables

PGDLLIMPORT BufferDescPaddedBufferDescriptors
 
PGDLLIMPORT ConditionVariableMinimallyPaddedBufferIOCVArray
 
PGDLLIMPORT WritebackContext BackendWritebackContext
 
PGDLLIMPORT BufferDescLocalBufferDescriptors
 
PGDLLIMPORT CkptSortItemCkptBufferIds
 

Macro Definition Documentation

◆ BM_CHECKPOINT_NEEDED

#define BM_CHECKPOINT_NEEDED   (1U << 30) /* must write for checkpoint */

Definition at line 66 of file buf_internals.h.

◆ BM_DIRTY

#define BM_DIRTY   (1U << 23) /* data needs writing */

Definition at line 59 of file buf_internals.h.

◆ BM_IO_ERROR

#define BM_IO_ERROR   (1U << 27) /* previous I/O failed */

Definition at line 63 of file buf_internals.h.

◆ BM_IO_IN_PROGRESS

#define BM_IO_IN_PROGRESS   (1U << 26) /* read or write in progress */

Definition at line 62 of file buf_internals.h.

◆ BM_JUST_DIRTIED

#define BM_JUST_DIRTIED   (1U << 28) /* dirtied since write started */

Definition at line 64 of file buf_internals.h.

◆ BM_LOCKED

#define BM_LOCKED   (1U << 22) /* buffer header is locked */

Definition at line 58 of file buf_internals.h.

◆ BM_MAX_USAGE_COUNT

#define BM_MAX_USAGE_COUNT   5

Definition at line 76 of file buf_internals.h.

◆ BM_PERMANENT

#define BM_PERMANENT
Value:
(1U << 31) /* permanent buffer (not unlogged,
* or init fork) */

Definition at line 67 of file buf_internals.h.

◆ BM_PIN_COUNT_WAITER

#define BM_PIN_COUNT_WAITER   (1U << 29) /* have waiter for sole pin */

Definition at line 65 of file buf_internals.h.

◆ BM_TAG_VALID

#define BM_TAG_VALID   (1U << 25) /* tag is assigned */

Definition at line 61 of file buf_internals.h.

◆ BM_VALID

#define BM_VALID   (1U << 24) /* data is valid */

Definition at line 60 of file buf_internals.h.

◆ BUF_FLAG_MASK

#define BUF_FLAG_MASK   0xFFC00000U

Definition at line 46 of file buf_internals.h.

◆ BUF_REFCOUNT_MASK

#define BUF_REFCOUNT_MASK   ((1U << 18) - 1)

Definition at line 42 of file buf_internals.h.

◆ BUF_REFCOUNT_ONE

#define BUF_REFCOUNT_ONE   1

Definition at line 41 of file buf_internals.h.

◆ BUF_STATE_GET_REFCOUNT

#define BUF_STATE_GET_REFCOUNT (   state)    ((state) & BUF_REFCOUNT_MASK)

Definition at line 49 of file buf_internals.h.

◆ BUF_STATE_GET_USAGECOUNT

#define BUF_STATE_GET_USAGECOUNT (   state)    (((state) & BUF_USAGECOUNT_MASK) >> BUF_USAGECOUNT_SHIFT)

Definition at line 50 of file buf_internals.h.

◆ BUF_USAGECOUNT_MASK

#define BUF_USAGECOUNT_MASK   0x003C0000U

Definition at line 43 of file buf_internals.h.

◆ BUF_USAGECOUNT_ONE

#define BUF_USAGECOUNT_ONE   (1U << 18)

Definition at line 44 of file buf_internals.h.

◆ BUF_USAGECOUNT_SHIFT

#define BUF_USAGECOUNT_SHIFT   18

Definition at line 45 of file buf_internals.h.

◆ BUFFERDESC_PAD_TO_SIZE

#define BUFFERDESC_PAD_TO_SIZE   (SIZEOF_VOID_P == 8 ? 64 : 1)

Definition at line 275 of file buf_internals.h.

◆ FREENEXT_END_OF_LIST

#define FREENEXT_END_OF_LIST   (-1)

Definition at line 349 of file buf_internals.h.

◆ FREENEXT_NOT_IN_LIST

#define FREENEXT_NOT_IN_LIST   (-2)

Definition at line 350 of file buf_internals.h.

Typedef Documentation

◆ BufferDesc

typedef struct BufferDesc BufferDesc

◆ BufferDescPadded

◆ BufferTag

typedef struct buftag BufferTag

◆ CkptSortItem

typedef struct CkptSortItem CkptSortItem

◆ PendingWriteback

◆ WritebackContext

Function Documentation

◆ AtEOXact_LocalBuffers()

void AtEOXact_LocalBuffers ( bool  isCommit)

Definition at line 598 of file localbuf.c.

599 {
601 }
static void CheckForLocalBufferLeaks(void)
Definition: localbuf.c:569

References CheckForLocalBufferLeaks().

Referenced by AtEOXact_Buffers().

◆ BufferDescriptorGetBuffer()

static Buffer BufferDescriptorGetBuffer ( const BufferDesc bdesc)
inlinestatic

◆ BufferDescriptorGetContentLock()

◆ BufferDescriptorGetIOCV()

static ConditionVariable* BufferDescriptorGetIOCV ( const BufferDesc bdesc)
inlinestatic

Definition at line 334 of file buf_internals.h.

336 {
337  return &(BufferIOCVArray[bdesc->buf_id]).cv;
PGDLLIMPORT ConditionVariableMinimallyPadded * BufferIOCVArray
Definition: buf_init.c:23

References BufferDesc::buf_id, and BufferIOCVArray.

Referenced by InitBufferPool(), TerminateBufferIO(), and WaitIO().

◆ BufferTagsEqual()

static bool BufferTagsEqual ( const BufferTag tag1,
const BufferTag tag2 
)
inlinestatic

Definition at line 151 of file buf_internals.h.

153 {
154  return (tag1->spcOid == tag2->spcOid) &&
155  (tag1->dbOid == tag2->dbOid) &&
156  (tag1->relNumber == tag2->relNumber) &&
157  (tag1->blockNum == tag2->blockNum) &&
158  (tag1->forkNum == tag2->forkNum);
BlockNumber blockNum
Definition: buf_internals.h:96
RelFileNumber relNumber
Definition: buf_internals.h:94
ForkNumber forkNum
Definition: buf_internals.h:95
Oid spcOid
Definition: buf_internals.h:92
Oid dbOid
Definition: buf_internals.h:93

References buftag::blockNum, buftag::dbOid, buftag::forkNum, buftag::relNumber, and buftag::spcOid.

Referenced by InvalidateBuffer(), LocalBufferAlloc(), and ReadRecentBuffer().

◆ BufMappingPartitionLock()

static LWLock* BufMappingPartitionLock ( uint32  hashcode)
inlinestatic

Definition at line 183 of file buf_internals.h.

185 {
187  BufTableHashPartition(hashcode)].lock;
static uint32 BufTableHashPartition(uint32 hashcode)
LWLockPadded * MainLWLockArray
Definition: lwlock.c:205
#define BUFFER_MAPPING_LWLOCK_OFFSET
Definition: lwlock.h:102
LWLock lock
Definition: lwlock.h:65

References BUFFER_MAPPING_LWLOCK_OFFSET, BufTableHashPartition(), LWLockPadded::lock, and MainLWLockArray.

Referenced by BufferAlloc(), FindAndDropRelationBuffers(), InvalidateBuffer(), and PrefetchSharedBuffer().

◆ BufMappingPartitionLockByIndex()

static LWLock* BufMappingPartitionLockByIndex ( uint32  index)
inlinestatic

◆ BufTableDelete()

void BufTableDelete ( BufferTag tagPtr,
uint32  hashcode 
)

Definition at line 149 of file buf_table.c.

150 {
151  BufferLookupEnt *result;
152 
153  result = (BufferLookupEnt *)
155  (void *) tagPtr,
156  hashcode,
157  HASH_REMOVE,
158  NULL);
159 
160  if (!result) /* shouldn't happen */
161  elog(ERROR, "shared buffer hash table corrupted");
162 }
static HTAB * SharedBufHash
Definition: buf_table.c:34
void * hash_search_with_hash_value(HTAB *hashp, const void *keyPtr, uint32 hashvalue, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:966
#define ERROR
Definition: elog.h:35
@ HASH_REMOVE
Definition: hsearch.h:115

References elog(), ERROR, HASH_REMOVE, hash_search_with_hash_value(), and SharedBufHash.

Referenced by BufferAlloc(), and InvalidateBuffer().

◆ BufTableHashCode()

uint32 BufTableHashCode ( BufferTag tagPtr)

Definition at line 79 of file buf_table.c.

80 {
81  return get_hash_value(SharedBufHash, (void *) tagPtr);
82 }
uint32 get_hash_value(HTAB *hashp, const void *keyPtr)
Definition: dynahash.c:909

References get_hash_value(), and SharedBufHash.

Referenced by BufferAlloc(), FindAndDropRelationBuffers(), InvalidateBuffer(), and PrefetchSharedBuffer().

◆ BufTableHashPartition()

static uint32 BufTableHashPartition ( uint32  hashcode)
inlinestatic

Definition at line 177 of file buf_internals.h.

179 {
180  return hashcode % NUM_BUFFER_PARTITIONS;
#define NUM_BUFFER_PARTITIONS
Definition: lwlock.h:91

References NUM_BUFFER_PARTITIONS.

Referenced by BufMappingPartitionLock().

◆ BufTableInsert()

int BufTableInsert ( BufferTag tagPtr,
uint32  hashcode,
int  buf_id 
)

Definition at line 119 of file buf_table.c.

120 {
121  BufferLookupEnt *result;
122  bool found;
123 
124  Assert(buf_id >= 0); /* -1 is reserved for not-in-table */
125  Assert(tagPtr->blockNum != P_NEW); /* invalid tag */
126 
127  result = (BufferLookupEnt *)
129  (void *) tagPtr,
130  hashcode,
131  HASH_ENTER,
132  &found);
133 
134  if (found) /* found something already in the table */
135  return result->id;
136 
137  result->id = buf_id;
138 
139  return -1;
140 }
#define P_NEW
Definition: bufmgr.h:100
@ HASH_ENTER
Definition: hsearch.h:114
Assert(fmt[strlen(fmt) - 1] !='\n')

References Assert(), buftag::blockNum, HASH_ENTER, hash_search_with_hash_value(), BufferLookupEnt::id, P_NEW, and SharedBufHash.

Referenced by BufferAlloc().

◆ BufTableLookup()

int BufTableLookup ( BufferTag tagPtr,
uint32  hashcode 
)

Definition at line 91 of file buf_table.c.

92 {
93  BufferLookupEnt *result;
94 
95  result = (BufferLookupEnt *)
97  (void *) tagPtr,
98  hashcode,
99  HASH_FIND,
100  NULL);
101 
102  if (!result)
103  return -1;
104 
105  return result->id;
106 }
@ HASH_FIND
Definition: hsearch.h:113

References HASH_FIND, hash_search_with_hash_value(), BufferLookupEnt::id, and SharedBufHash.

Referenced by BufferAlloc(), FindAndDropRelationBuffers(), and PrefetchSharedBuffer().

◆ BufTableShmemSize()

Size BufTableShmemSize ( int  size)

Definition at line 42 of file buf_table.c.

43 {
44  return hash_estimate_size(size, sizeof(BufferLookupEnt));
45 }
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:781

References hash_estimate_size().

Referenced by StrategyShmemSize().

◆ BufTagGetForkNum()

◆ BufTagGetRelFileLocator()

◆ BufTagGetRelNumber()

static RelFileNumber BufTagGetRelNumber ( const BufferTag tag)
inlinestatic

Definition at line 100 of file buf_internals.h.

102 {
103  return tag->relNumber;

References buftag::relNumber.

Referenced by apw_dump_now(), BufferSync(), BufTagGetRelFileLocator(), BufTagMatchesRelFileLocator(), and pg_buffercache_pages().

◆ BufTagMatchesRelFileLocator()

static bool BufTagMatchesRelFileLocator ( const BufferTag tag,
const RelFileLocator rlocator 
)
inlinestatic

◆ BufTagSetRelForkDetails()

static void BufTagSetRelForkDetails ( BufferTag tag,
RelFileNumber  relnumber,
ForkNumber  forknum 
)
inlinestatic

Definition at line 112 of file buf_internals.h.

115 {
116  tag->relNumber = relnumber;
117  tag->forkNum = forknum;

References buftag::forkNum, and buftag::relNumber.

Referenced by ClearBufferTag(), and InitBufferTag().

◆ ClearBufferTag()

static void ClearBufferTag ( BufferTag tag)
inlinestatic

Definition at line 132 of file buf_internals.h.

134 {
135  tag->spcOid = InvalidOid;
136  tag->dbOid = InvalidOid;
#define InvalidBlockNumber
Definition: block.h:33
static void BufTagSetRelForkDetails(BufferTag *tag, RelFileNumber relnumber, ForkNumber forknum)
#define InvalidOid
Definition: postgres_ext.h:36
@ InvalidForkNumber
Definition: relpath.h:49
#define InvalidRelFileNumber
Definition: relpath.h:26

References buftag::blockNum, BufTagSetRelForkDetails(), buftag::dbOid, InvalidBlockNumber, InvalidForkNumber, InvalidOid, InvalidRelFileNumber, and buftag::spcOid.

Referenced by DropRelationAllLocalBuffers(), DropRelationLocalBuffers(), InitBufferPool(), InvalidateBuffer(), and LocalBufferAlloc().

◆ DropRelationAllLocalBuffers()

void DropRelationAllLocalBuffers ( RelFileLocator  rlocator)

Definition at line 375 of file localbuf.c.

376 {
377  int i;
378 
379  for (i = 0; i < NLocBuffer; i++)
380  {
382  LocalBufferLookupEnt *hresult;
383  uint32 buf_state;
384 
385  buf_state = pg_atomic_read_u32(&bufHdr->state);
386 
387  if ((buf_state & BM_TAG_VALID) &&
388  BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator))
389  {
390  if (LocalRefCount[i] != 0)
391  elog(ERROR, "block %u of %s is still referenced (local %u)",
392  bufHdr->tag.blockNum,
394  MyBackendId,
395  BufTagGetForkNum(&bufHdr->tag)),
396  LocalRefCount[i]);
397  /* Remove entry from hashtable */
398  hresult = (LocalBufferLookupEnt *)
399  hash_search(LocalBufHash, (void *) &bufHdr->tag,
400  HASH_REMOVE, NULL);
401  if (!hresult) /* shouldn't happen */
402  elog(ERROR, "local buffer hash table corrupted");
403  /* Mark buffer invalid */
404  ClearBufferTag(&bufHdr->tag);
405  buf_state &= ~BUF_FLAG_MASK;
406  buf_state &= ~BUF_USAGECOUNT_MASK;
407  pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
408  }
409  }
410 }
static void pg_atomic_unlocked_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:272
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:236
#define BM_TAG_VALID
Definition: buf_internals.h:61
#define BUF_USAGECOUNT_MASK
Definition: buf_internals.h:43
static ForkNumber BufTagGetForkNum(const BufferTag *tag)
static BufferDesc * GetLocalBufferDescriptor(uint32 id)
static bool BufTagMatchesRelFileLocator(const BufferTag *tag, const RelFileLocator *rlocator)
#define BUF_FLAG_MASK
Definition: buf_internals.h:46
static void ClearBufferTag(BufferTag *tag)
static RelFileLocator BufTagGetRelFileLocator(const BufferTag *tag)
unsigned int uint32
Definition: c.h:442
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:953
BackendId MyBackendId
Definition: globals.c:85
int i
Definition: isn.c:73
int32 * LocalRefCount
Definition: localbuf.c:45
static HTAB * LocalBufHash
Definition: localbuf.c:49
int NLocBuffer
Definition: localbuf.c:41
#define relpathbackend(rlocator, backend, forknum)
Definition: relpath.h:85
BufferTag tag
pg_atomic_uint32 state

References buftag::blockNum, BM_TAG_VALID, BUF_FLAG_MASK, BUF_USAGECOUNT_MASK, BufTagGetForkNum(), BufTagGetRelFileLocator(), BufTagMatchesRelFileLocator(), ClearBufferTag(), elog(), ERROR, GetLocalBufferDescriptor(), HASH_REMOVE, hash_search(), i, LocalBufHash, LocalRefCount, MyBackendId, NLocBuffer, pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), relpathbackend, BufferDesc::state, and BufferDesc::tag.

Referenced by DropRelationsAllBuffers().

◆ DropRelationLocalBuffers()

void DropRelationLocalBuffers ( RelFileLocator  rlocator,
ForkNumber  forkNum,
BlockNumber  firstDelBlock 
)

Definition at line 326 of file localbuf.c.

328 {
329  int i;
330 
331  for (i = 0; i < NLocBuffer; i++)
332  {
334  LocalBufferLookupEnt *hresult;
335  uint32 buf_state;
336 
337  buf_state = pg_atomic_read_u32(&bufHdr->state);
338 
339  if ((buf_state & BM_TAG_VALID) &&
340  BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator) &&
341  BufTagGetForkNum(&bufHdr->tag) == forkNum &&
342  bufHdr->tag.blockNum >= firstDelBlock)
343  {
344  if (LocalRefCount[i] != 0)
345  elog(ERROR, "block %u of %s is still referenced (local %u)",
346  bufHdr->tag.blockNum,
348  MyBackendId,
349  BufTagGetForkNum(&bufHdr->tag)),
350  LocalRefCount[i]);
351 
352  /* Remove entry from hashtable */
353  hresult = (LocalBufferLookupEnt *)
354  hash_search(LocalBufHash, (void *) &bufHdr->tag,
355  HASH_REMOVE, NULL);
356  if (!hresult) /* shouldn't happen */
357  elog(ERROR, "local buffer hash table corrupted");
358  /* Mark buffer invalid */
359  ClearBufferTag(&bufHdr->tag);
360  buf_state &= ~BUF_FLAG_MASK;
361  buf_state &= ~BUF_USAGECOUNT_MASK;
362  pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
363  }
364  }
365 }

References buftag::blockNum, BM_TAG_VALID, BUF_FLAG_MASK, BUF_USAGECOUNT_MASK, BufTagGetForkNum(), BufTagGetRelFileLocator(), BufTagMatchesRelFileLocator(), ClearBufferTag(), elog(), ERROR, GetLocalBufferDescriptor(), HASH_REMOVE, hash_search(), i, LocalBufHash, LocalRefCount, MyBackendId, NLocBuffer, pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), relpathbackend, BufferDesc::state, and BufferDesc::tag.

Referenced by DropRelationBuffers().

◆ GetBufferDescriptor()

◆ GetLocalBufferDescriptor()

◆ have_free_buffer()

bool have_free_buffer ( void  )

Definition at line 180 of file freelist.c.

181 {
183  return true;
184  else
185  return false;
186 }
static BufferStrategyControl * StrategyControl
Definition: freelist.c:64

References BufferStrategyControl::firstFreeBuffer, and StrategyControl.

Referenced by apw_load_buffers(), and autoprewarm_database_main().

◆ InitBufferTag()

static void InitBufferTag ( BufferTag tag,
const RelFileLocator rlocator,
ForkNumber  forkNum,
BlockNumber  blockNum 
)
inlinestatic

◆ InitBufTable()

void InitBufTable ( int  size)

Definition at line 52 of file buf_table.c.

53 {
54  HASHCTL info;
55 
56  /* assume no locking is needed yet */
57 
58  /* BufferTag maps to Buffer */
59  info.keysize = sizeof(BufferTag);
60  info.entrysize = sizeof(BufferLookupEnt);
62 
63  SharedBufHash = ShmemInitHash("Shared Buffer Lookup Table",
64  size, size,
65  &info,
67 }
struct buftag BufferTag
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
#define HASH_PARTITION
Definition: hsearch.h:92
HTAB * ShmemInitHash(const char *name, long init_size, long max_size, HASHCTL *infoP, int hash_flags)
Definition: shmem.c:341
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
long num_partitions
Definition: hsearch.h:68

References HASHCTL::entrysize, HASH_BLOBS, HASH_ELEM, HASH_PARTITION, HASHCTL::keysize, NUM_BUFFER_PARTITIONS, HASHCTL::num_partitions, SharedBufHash, and ShmemInitHash().

Referenced by StrategyInitialize().

◆ IssuePendingWritebacks()

void IssuePendingWritebacks ( WritebackContext context)

Definition at line 4948 of file bufmgr.c.

4949 {
4950  int i;
4951 
4952  if (context->nr_pending == 0)
4953  return;
4954 
4955  /*
4956  * Executing the writes in-order can make them a lot faster, and allows to
4957  * merge writeback requests to consecutive blocks into larger writebacks.
4958  */
4959  sort_pending_writebacks(context->pending_writebacks, context->nr_pending);
4960 
4961  /*
4962  * Coalesce neighbouring writes, but nothing else. For that we iterate
4963  * through the, now sorted, array of pending flushes, and look forward to
4964  * find all neighbouring (or identical) writes.
4965  */
4966  for (i = 0; i < context->nr_pending; i++)
4967  {
4970  SMgrRelation reln;
4971  int ahead;
4972  BufferTag tag;
4973  RelFileLocator currlocator;
4974  Size nblocks = 1;
4975 
4976  cur = &context->pending_writebacks[i];
4977  tag = cur->tag;
4978  currlocator = BufTagGetRelFileLocator(&tag);
4979 
4980  /*
4981  * Peek ahead, into following writeback requests, to see if they can
4982  * be combined with the current one.
4983  */
4984  for (ahead = 0; i + ahead + 1 < context->nr_pending; ahead++)
4985  {
4986 
4987  next = &context->pending_writebacks[i + ahead + 1];
4988 
4989  /* different file, stop */
4990  if (!RelFileLocatorEquals(currlocator,
4991  BufTagGetRelFileLocator(&next->tag)) ||
4992  BufTagGetForkNum(&cur->tag) != BufTagGetForkNum(&next->tag))
4993  break;
4994 
4995  /* ok, block queued twice, skip */
4996  if (cur->tag.blockNum == next->tag.blockNum)
4997  continue;
4998 
4999  /* only merge consecutive writes */
5000  if (cur->tag.blockNum + 1 != next->tag.blockNum)
5001  break;
5002 
5003  nblocks++;
5004  cur = next;
5005  }
5006 
5007  i += ahead;
5008 
5009  /* and finally tell the kernel to write the data to storage */
5010  reln = smgropen(currlocator, InvalidBackendId);
5011  smgrwriteback(reln, BufTagGetForkNum(&tag), tag.blockNum, nblocks);
5012  }
5013 
5014  context->nr_pending = 0;
5015 }
#define InvalidBackendId
Definition: backendid.h:23
static int32 next
Definition: blutils.c:219
size_t Size
Definition: c.h:541
struct cursor * cur
Definition: ecpg.c:28
#define RelFileLocatorEquals(locator1, locator2)
void smgrwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: smgr.c:567
SMgrRelation smgropen(RelFileLocator rlocator, BackendId backend)
Definition: smgr.c:146
PendingWriteback pending_writebacks[WRITEBACK_MAX_PENDING_FLUSHES]

References buftag::blockNum, BufTagGetForkNum(), BufTagGetRelFileLocator(), cur, i, InvalidBackendId, next, WritebackContext::nr_pending, WritebackContext::pending_writebacks, RelFileLocatorEquals, smgropen(), and smgrwriteback().

Referenced by BufferSync(), and ScheduleBufferTagForWriteback().

◆ LocalBufferAlloc()

BufferDesc* LocalBufferAlloc ( SMgrRelation  smgr,
ForkNumber  forkNum,
BlockNumber  blockNum,
bool foundPtr 
)

Definition at line 109 of file localbuf.c.

111 {
112  BufferTag newTag; /* identity of requested block */
113  LocalBufferLookupEnt *hresult;
114  BufferDesc *bufHdr;
115  int b;
116  int trycounter;
117  bool found;
118  uint32 buf_state;
119 
120  InitBufferTag(&newTag, &smgr->smgr_rlocator.locator, forkNum, blockNum);
121 
122  /* Initialize local buffers if first request in this session */
123  if (LocalBufHash == NULL)
125 
126  /* See if the desired buffer already exists */
127  hresult = (LocalBufferLookupEnt *)
128  hash_search(LocalBufHash, (void *) &newTag, HASH_FIND, NULL);
129 
130  if (hresult)
131  {
132  b = hresult->id;
133  bufHdr = GetLocalBufferDescriptor(b);
134  Assert(BufferTagsEqual(&bufHdr->tag, &newTag));
135 #ifdef LBDEBUG
136  fprintf(stderr, "LB ALLOC (%u,%d,%d) %d\n",
137  smgr->smgr_rlocator.locator.relNumber, forkNum, blockNum, -b - 1);
138 #endif
139  buf_state = pg_atomic_read_u32(&bufHdr->state);
140 
141  /* this part is equivalent to PinBuffer for a shared buffer */
142  if (LocalRefCount[b] == 0)
143  {
145  {
146  buf_state += BUF_USAGECOUNT_ONE;
147  pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
148  }
149  }
150  LocalRefCount[b]++;
152  BufferDescriptorGetBuffer(bufHdr));
153  if (buf_state & BM_VALID)
154  *foundPtr = true;
155  else
156  {
157  /* Previous read attempt must have failed; try again */
158  *foundPtr = false;
159  }
160  return bufHdr;
161  }
162 
163 #ifdef LBDEBUG
164  fprintf(stderr, "LB ALLOC (%u,%d,%d) %d\n",
165  smgr->smgr_rlocator.locator.relNumber, forkNum, blockNum,
166  -nextFreeLocalBuf - 1);
167 #endif
168 
169  /*
170  * Need to get a new buffer. We use a clock sweep algorithm (essentially
171  * the same as what freelist.c does now...)
172  */
173  trycounter = NLocBuffer;
174  for (;;)
175  {
177 
178  if (++nextFreeLocalBuf >= NLocBuffer)
179  nextFreeLocalBuf = 0;
180 
181  bufHdr = GetLocalBufferDescriptor(b);
182 
183  if (LocalRefCount[b] == 0)
184  {
185  buf_state = pg_atomic_read_u32(&bufHdr->state);
186 
187  if (BUF_STATE_GET_USAGECOUNT(buf_state) > 0)
188  {
189  buf_state -= BUF_USAGECOUNT_ONE;
190  pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
191  trycounter = NLocBuffer;
192  }
193  else
194  {
195  /* Found a usable buffer */
196  LocalRefCount[b]++;
198  BufferDescriptorGetBuffer(bufHdr));
199  break;
200  }
201  }
202  else if (--trycounter == 0)
203  ereport(ERROR,
204  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
205  errmsg("no empty local buffer available")));
206  }
207 
208  /*
209  * this buffer is not referenced but it might still be dirty. if that's
210  * the case, write it out before reusing it!
211  */
212  if (buf_state & BM_DIRTY)
213  {
214  SMgrRelation oreln;
215  Page localpage = (char *) LocalBufHdrGetBlock(bufHdr);
216 
217  /* Find smgr relation for buffer */
218  oreln = smgropen(BufTagGetRelFileLocator(&bufHdr->tag), MyBackendId);
219 
220  PageSetChecksumInplace(localpage, bufHdr->tag.blockNum);
221 
222  /* And write... */
223  smgrwrite(oreln,
224  BufTagGetForkNum(&bufHdr->tag),
225  bufHdr->tag.blockNum,
226  localpage,
227  false);
228 
229  /* Mark not-dirty now in case we error out below */
230  buf_state &= ~BM_DIRTY;
231  pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
232 
234  }
235 
236  /*
237  * lazy memory allocation: allocate space on first use of a buffer.
238  */
239  if (LocalBufHdrGetBlock(bufHdr) == NULL)
240  {
241  /* Set pointer for use by BufferGetBlock() macro */
243  }
244 
245  /*
246  * Update the hash table: remove old entry, if any, and make new one.
247  */
248  if (buf_state & BM_TAG_VALID)
249  {
250  hresult = (LocalBufferLookupEnt *)
251  hash_search(LocalBufHash, (void *) &bufHdr->tag,
252  HASH_REMOVE, NULL);
253  if (!hresult) /* shouldn't happen */
254  elog(ERROR, "local buffer hash table corrupted");
255  /* mark buffer invalid just in case hash insert fails */
256  ClearBufferTag(&bufHdr->tag);
257  buf_state &= ~(BM_VALID | BM_TAG_VALID);
258  pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
259  }
260 
261  hresult = (LocalBufferLookupEnt *)
262  hash_search(LocalBufHash, (void *) &newTag, HASH_ENTER, &found);
263  if (found) /* shouldn't happen */
264  elog(ERROR, "local buffer hash table corrupted");
265  hresult->id = b;
266 
267  /*
268  * it's all ours now.
269  */
270  bufHdr->tag = newTag;
271  buf_state &= ~(BM_VALID | BM_DIRTY | BM_JUST_DIRTIED | BM_IO_ERROR);
272  buf_state |= BM_TAG_VALID;
273  buf_state &= ~BUF_USAGECOUNT_MASK;
274  buf_state += BUF_USAGECOUNT_ONE;
275  pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
276 
277  *foundPtr = false;
278  return bufHdr;
279 }
#define BM_MAX_USAGE_COUNT
Definition: buf_internals.h:76
static void InitBufferTag(BufferTag *tag, const RelFileLocator *rlocator, ForkNumber forkNum, BlockNumber blockNum)
static bool BufferTagsEqual(const BufferTag *tag1, const BufferTag *tag2)
#define BM_DIRTY
Definition: buf_internals.h:59
#define BM_JUST_DIRTIED
Definition: buf_internals.h:64
#define BUF_STATE_GET_USAGECOUNT(state)
Definition: buf_internals.h:50
#define BUF_USAGECOUNT_ONE
Definition: buf_internals.h:44
#define BM_VALID
Definition: buf_internals.h:60
#define BM_IO_ERROR
Definition: buf_internals.h:63
static Buffer BufferDescriptorGetBuffer(const BufferDesc *bdesc)
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1539
Pointer Page
Definition: bufpage.h:78
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ereport(elevel,...)
Definition: elog.h:145
BufferUsage pgBufferUsage
Definition: instrument.c:20
int b
Definition: isn.c:70
#define LocalBufHdrGetBlock(bufHdr)
Definition: localbuf.c:38
static Block GetLocalBufferStorage(void)
Definition: localbuf.c:514
static void InitLocalBuffers(void)
Definition: localbuf.c:419
static int nextFreeLocalBuf
Definition: localbuf.c:47
#define fprintf
Definition: port.h:242
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
void ResourceOwnerRememberBuffer(ResourceOwner owner, Buffer buffer)
Definition: resowner.c:963
void smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:554
int64 local_blks_written
Definition: instrument.h:33
RelFileLocator locator
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:42

References Assert(), b, buftag::blockNum, BM_DIRTY, BM_IO_ERROR, BM_JUST_DIRTIED, BM_MAX_USAGE_COUNT, BM_TAG_VALID, BM_VALID, BUF_STATE_GET_USAGECOUNT, BUF_USAGECOUNT_MASK, BUF_USAGECOUNT_ONE, BufferDescriptorGetBuffer(), BufferTagsEqual(), BufTagGetForkNum(), BufTagGetRelFileLocator(), ClearBufferTag(), CurrentResourceOwner, elog(), ereport, errcode(), errmsg(), ERROR, fprintf, GetLocalBufferDescriptor(), GetLocalBufferStorage(), HASH_ENTER, HASH_FIND, HASH_REMOVE, hash_search(), LocalBufferLookupEnt::id, InitBufferTag(), InitLocalBuffers(), BufferUsage::local_blks_written, LocalBufHash, LocalBufHdrGetBlock, LocalRefCount, RelFileLocatorBackend::locator, MyBackendId, nextFreeLocalBuf, NLocBuffer, PageSetChecksumInplace(), pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), pgBufferUsage, RelFileLocator::relNumber, ResourceOwnerRememberBuffer(), SMgrRelationData::smgr_rlocator, smgropen(), smgrwrite(), BufferDesc::state, and BufferDesc::tag.

Referenced by ReadBuffer_common().

◆ LockBufHdr()

uint32 LockBufHdr ( BufferDesc desc)

Definition at line 4755 of file bufmgr.c.

4756 {
4757  SpinDelayStatus delayStatus;
4758  uint32 old_buf_state;
4759 
4760  init_local_spin_delay(&delayStatus);
4761 
4762  while (true)
4763  {
4764  /* set BM_LOCKED flag */
4765  old_buf_state = pg_atomic_fetch_or_u32(&desc->state, BM_LOCKED);
4766  /* if it wasn't set before we're OK */
4767  if (!(old_buf_state & BM_LOCKED))
4768  break;
4769  perform_spin_delay(&delayStatus);
4770  }
4771  finish_spin_delay(&delayStatus);
4772  return old_buf_state | BM_LOCKED;
4773 }
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition: atomics.h:367
#define BM_LOCKED
Definition: buf_internals.h:58
void perform_spin_delay(SpinDelayStatus *status)
Definition: s_lock.c:126
void finish_spin_delay(SpinDelayStatus *status)
Definition: s_lock.c:186
#define init_local_spin_delay(status)
Definition: s_lock.h:863

References BM_LOCKED, finish_spin_delay(), init_local_spin_delay, perform_spin_delay(), pg_atomic_fetch_or_u32(), and BufferDesc::state.

Referenced by AbortBufferIO(), apw_dump_now(), BufferAlloc(), BufferGetLSNAtomic(), BufferSync(), ConditionalLockBufferForCleanup(), DropDatabaseBuffers(), DropRelationBuffers(), DropRelationsAllBuffers(), FindAndDropRelationBuffers(), FlushBuffer(), FlushDatabaseBuffers(), FlushRelationBuffers(), FlushRelationsAllBuffers(), GetBufferFromRing(), InvalidateBuffer(), IsBufferCleanupOK(), LockBufferForCleanup(), MarkBufferDirtyHint(), pg_buffercache_pages(), ReadBuffer_common(), ReadRecentBuffer(), StartBufferIO(), StrategyGetBuffer(), SyncOneBuffer(), TerminateBufferIO(), UnlockBuffers(), UnpinBuffer(), and WaitIO().

◆ MarkLocalBufferDirty()

void MarkLocalBufferDirty ( Buffer  buffer)

Definition at line 286 of file localbuf.c.

287 {
288  int bufid;
289  BufferDesc *bufHdr;
290  uint32 buf_state;
291 
292  Assert(BufferIsLocal(buffer));
293 
294 #ifdef LBDEBUG
295  fprintf(stderr, "LB DIRTY %d\n", buffer);
296 #endif
297 
298  bufid = -(buffer + 1);
299 
300  Assert(LocalRefCount[bufid] > 0);
301 
302  bufHdr = GetLocalBufferDescriptor(bufid);
303 
304  buf_state = pg_atomic_read_u32(&bufHdr->state);
305 
306  if (!(buf_state & BM_DIRTY))
308 
309  buf_state |= BM_DIRTY;
310 
311  pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
312 }
#define BufferIsLocal(buffer)
Definition: buf.h:37
int64 local_blks_dirtied
Definition: instrument.h:32

References Assert(), BM_DIRTY, BufferIsLocal, fprintf, GetLocalBufferDescriptor(), BufferUsage::local_blks_dirtied, LocalRefCount, pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), pgBufferUsage, and BufferDesc::state.

Referenced by MarkBufferDirty(), and MarkBufferDirtyHint().

◆ PrefetchLocalBuffer()

PrefetchBufferResult PrefetchLocalBuffer ( SMgrRelation  smgr,
ForkNumber  forkNum,
BlockNumber  blockNum 
)

Definition at line 64 of file localbuf.c.

66 {
67  PrefetchBufferResult result = {InvalidBuffer, false};
68  BufferTag newTag; /* identity of requested block */
69  LocalBufferLookupEnt *hresult;
70 
71  InitBufferTag(&newTag, &smgr->smgr_rlocator.locator, forkNum, blockNum);
72 
73  /* Initialize local buffers if first request in this session */
74  if (LocalBufHash == NULL)
76 
77  /* See if the desired buffer already exists */
78  hresult = (LocalBufferLookupEnt *)
79  hash_search(LocalBufHash, (void *) &newTag, HASH_FIND, NULL);
80 
81  if (hresult)
82  {
83  /* Yes, so nothing to do */
84  result.recent_buffer = -hresult->id - 1;
85  }
86  else
87  {
88 #ifdef USE_PREFETCH
89  /* Not in buffers, so initiate prefetch */
90  smgrprefetch(smgr, forkNum, blockNum);
91  result.initiated_io = true;
92 #endif /* USE_PREFETCH */
93  }
94 
95  return result;
96 }
#define InvalidBuffer
Definition: buf.h:25
bool smgrprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: smgr.c:518
Buffer recent_buffer
Definition: bufmgr.h:54

References HASH_FIND, hash_search(), LocalBufferLookupEnt::id, InitBufferTag(), PrefetchBufferResult::initiated_io, InitLocalBuffers(), InvalidBuffer, LocalBufHash, RelFileLocatorBackend::locator, PrefetchBufferResult::recent_buffer, SMgrRelationData::smgr_rlocator, and smgrprefetch().

Referenced by PrefetchBuffer().

◆ ScheduleBufferTagForWriteback()

void ScheduleBufferTagForWriteback ( WritebackContext context,
BufferTag tag 
)

Definition at line 4907 of file bufmgr.c.

4908 {
4909  PendingWriteback *pending;
4910 
4911  /*
4912  * Add buffer to the pending writeback array, unless writeback control is
4913  * disabled.
4914  */
4915  if (*context->max_pending > 0)
4916  {
4918 
4919  pending = &context->pending_writebacks[context->nr_pending++];
4920 
4921  pending->tag = *tag;
4922  }
4923 
4924  /*
4925  * Perform pending flushes if the writeback limit is exceeded. This
4926  * includes the case where previously an item has been added, but control
4927  * is now disabled.
4928  */
4929  if (context->nr_pending >= *context->max_pending)
4930  IssuePendingWritebacks(context);
4931 }
void IssuePendingWritebacks(WritebackContext *context)
Definition: bufmgr.c:4948
#define WRITEBACK_MAX_PENDING_FLUSHES

References Assert(), IssuePendingWritebacks(), WritebackContext::max_pending, WritebackContext::nr_pending, WritebackContext::pending_writebacks, PendingWriteback::tag, and WRITEBACK_MAX_PENDING_FLUSHES.

Referenced by BufferAlloc(), and SyncOneBuffer().

◆ StrategyFreeBuffer()

void StrategyFreeBuffer ( BufferDesc buf)

Definition at line 363 of file freelist.c.

364 {
366 
367  /*
368  * It is possible that we are told to put something in the freelist that
369  * is already in it; don't screw up the list if so.
370  */
371  if (buf->freeNext == FREENEXT_NOT_IN_LIST)
372  {
373  buf->freeNext = StrategyControl->firstFreeBuffer;
374  if (buf->freeNext < 0)
377  }
378 
380 }
#define FREENEXT_NOT_IN_LIST
static char * buf
Definition: pg_test_fsync.c:67
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
slock_t buffer_strategy_lock
Definition: freelist.c:32

References buf, BufferStrategyControl::buffer_strategy_lock, BufferStrategyControl::firstFreeBuffer, FREENEXT_NOT_IN_LIST, BufferStrategyControl::lastFreeBuffer, SpinLockAcquire, SpinLockRelease, and StrategyControl.

Referenced by InvalidateBuffer().

◆ StrategyGetBuffer()

BufferDesc* StrategyGetBuffer ( BufferAccessStrategy  strategy,
uint32 buf_state 
)

Definition at line 201 of file freelist.c.

202 {
203  BufferDesc *buf;
204  int bgwprocno;
205  int trycounter;
206  uint32 local_buf_state; /* to avoid repeated (de-)referencing */
207 
208  /*
209  * If given a strategy object, see whether it can select a buffer. We
210  * assume strategy objects don't need buffer_strategy_lock.
211  */
212  if (strategy != NULL)
213  {
214  buf = GetBufferFromRing(strategy, buf_state);
215  if (buf != NULL)
216  return buf;
217  }
218 
219  /*
220  * If asked, we need to waken the bgwriter. Since we don't want to rely on
221  * a spinlock for this we force a read from shared memory once, and then
222  * set the latch based on that value. We need to go through that length
223  * because otherwise bgwprocno might be reset while/after we check because
224  * the compiler might just reread from memory.
225  *
226  * This can possibly set the latch of the wrong process if the bgwriter
227  * dies in the wrong moment. But since PGPROC->procLatch is never
228  * deallocated the worst consequence of that is that we set the latch of
229  * some arbitrary process.
230  */
232  if (bgwprocno != -1)
233  {
234  /* reset bgwprocno first, before setting the latch */
236 
237  /*
238  * Not acquiring ProcArrayLock here which is slightly icky. It's
239  * actually fine because procLatch isn't ever freed, so we just can
240  * potentially set the wrong process' (or no process') latch.
241  */
242  SetLatch(&ProcGlobal->allProcs[bgwprocno].procLatch);
243  }
244 
245  /*
246  * We count buffer allocation requests so that the bgwriter can estimate
247  * the rate of buffer consumption. Note that buffers recycled by a
248  * strategy object are intentionally not counted here.
249  */
251 
252  /*
253  * First check, without acquiring the lock, whether there's buffers in the
254  * freelist. Since we otherwise don't require the spinlock in every
255  * StrategyGetBuffer() invocation, it'd be sad to acquire it here -
256  * uselessly in most cases. That obviously leaves a race where a buffer is
257  * put on the freelist but we don't see the store yet - but that's pretty
258  * harmless, it'll just get used during the next buffer acquisition.
259  *
260  * If there's buffers on the freelist, acquire the spinlock to pop one
261  * buffer of the freelist. Then check whether that buffer is usable and
262  * repeat if not.
263  *
264  * Note that the freeNext fields are considered to be protected by the
265  * buffer_strategy_lock not the individual buffer spinlocks, so it's OK to
266  * manipulate them without holding the spinlock.
267  */
269  {
270  while (true)
271  {
272  /* Acquire the spinlock to remove element from the freelist */
274 
276  {
278  break;
279  }
280 
282  Assert(buf->freeNext != FREENEXT_NOT_IN_LIST);
283 
284  /* Unconditionally remove buffer from freelist */
285  StrategyControl->firstFreeBuffer = buf->freeNext;
286  buf->freeNext = FREENEXT_NOT_IN_LIST;
287 
288  /*
289  * Release the lock so someone else can access the freelist while
290  * we check out this buffer.
291  */
293 
294  /*
295  * If the buffer is pinned or has a nonzero usage_count, we cannot
296  * use it; discard it and retry. (This can only happen if VACUUM
297  * put a valid buffer in the freelist and then someone else used
298  * it before we got to it. It's probably impossible altogether as
299  * of 8.3, but we'd better check anyway.)
300  */
301  local_buf_state = LockBufHdr(buf);
302  if (BUF_STATE_GET_REFCOUNT(local_buf_state) == 0
303  && BUF_STATE_GET_USAGECOUNT(local_buf_state) == 0)
304  {
305  if (strategy != NULL)
306  AddBufferToRing(strategy, buf);
307  *buf_state = local_buf_state;
308  return buf;
309  }
310  UnlockBufHdr(buf, local_buf_state);
311  }
312  }
313 
314  /* Nothing on the freelist, so run the "clock sweep" algorithm */
315  trycounter = NBuffers;
316  for (;;)
317  {
319 
320  /*
321  * If the buffer is pinned or has a nonzero usage_count, we cannot use
322  * it; decrement the usage_count (unless pinned) and keep scanning.
323  */
324  local_buf_state = LockBufHdr(buf);
325 
326  if (BUF_STATE_GET_REFCOUNT(local_buf_state) == 0)
327  {
328  if (BUF_STATE_GET_USAGECOUNT(local_buf_state) != 0)
329  {
330  local_buf_state -= BUF_USAGECOUNT_ONE;
331 
332  trycounter = NBuffers;
333  }
334  else
335  {
336  /* Found a usable buffer */
337  if (strategy != NULL)
338  AddBufferToRing(strategy, buf);
339  *buf_state = local_buf_state;
340  return buf;
341  }
342  }
343  else if (--trycounter == 0)
344  {
345  /*
346  * We've scanned all the buffers without making any state changes,
347  * so all the buffers are pinned (or were when we looked at them).
348  * We could hope that someone will free one eventually, but it's
349  * probably better to fail than to risk getting stuck in an
350  * infinite loop.
351  */
352  UnlockBufHdr(buf, local_buf_state);
353  elog(ERROR, "no unpinned buffers available");
354  }
355  UnlockBufHdr(buf, local_buf_state);
356  }
357 }
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:323
static BufferDesc * GetBufferDescriptor(uint32 id)
static void UnlockBufHdr(BufferDesc *desc, uint32 buf_state)
#define BUF_STATE_GET_REFCOUNT(state)
Definition: buf_internals.h:49
uint32 LockBufHdr(BufferDesc *desc)
Definition: bufmgr.c:4755
static uint32 ClockSweepTick(void)
Definition: freelist.c:113
static void AddBufferToRing(BufferAccessStrategy strategy, BufferDesc *buf)
Definition: freelist.c:668
#define INT_ACCESS_ONCE(var)
Definition: freelist.c:23
static BufferDesc * GetBufferFromRing(BufferAccessStrategy strategy, uint32 *buf_state)
Definition: freelist.c:610
int NBuffers
Definition: globals.c:136
void SetLatch(Latch *latch)
Definition: latch.c:591
PROC_HDR * ProcGlobal
Definition: proc.c:80
pg_atomic_uint32 numBufferAllocs
Definition: freelist.c:54
Latch procLatch
Definition: proc.h:170
PGPROC * allProcs
Definition: proc.h:362

References AddBufferToRing(), PROC_HDR::allProcs, Assert(), BufferStrategyControl::bgwprocno, buf, BUF_STATE_GET_REFCOUNT, BUF_STATE_GET_USAGECOUNT, BUF_USAGECOUNT_ONE, BufferStrategyControl::buffer_strategy_lock, ClockSweepTick(), elog(), ERROR, BufferStrategyControl::firstFreeBuffer, FREENEXT_NOT_IN_LIST, GetBufferDescriptor(), GetBufferFromRing(), INT_ACCESS_ONCE, LockBufHdr(), NBuffers, BufferStrategyControl::numBufferAllocs, pg_atomic_fetch_add_u32(), ProcGlobal, PGPROC::procLatch, SetLatch(), SpinLockAcquire, SpinLockRelease, StrategyControl, and UnlockBufHdr().

Referenced by BufferAlloc().

◆ StrategyInitialize()

void StrategyInitialize ( bool  init)

Definition at line 474 of file freelist.c.

475 {
476  bool found;
477 
478  /*
479  * Initialize the shared buffer lookup hashtable.
480  *
481  * Since we can't tolerate running out of lookup table entries, we must be
482  * sure to specify an adequate table size here. The maximum steady-state
483  * usage is of course NBuffers entries, but BufferAlloc() tries to insert
484  * a new entry before deleting the old. In principle this could be
485  * happening in each partition concurrently, so we could need as many as
486  * NBuffers + NUM_BUFFER_PARTITIONS entries.
487  */
489 
490  /*
491  * Get or create the shared strategy control block
492  */
494  ShmemInitStruct("Buffer Strategy Status",
495  sizeof(BufferStrategyControl),
496  &found);
497 
498  if (!found)
499  {
500  /*
501  * Only done once, usually in postmaster
502  */
503  Assert(init);
504 
506 
507  /*
508  * Grab the whole linked list of free buffers for our strategy. We
509  * assume it was previously set up by InitBufferPool().
510  */
513 
514  /* Initialize the clock sweep pointer */
516 
517  /* Clear statistics */
520 
521  /* No pending notification */
523  }
524  else
525  Assert(!init);
526 }
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:218
void InitBufTable(int size)
Definition: buf_table.c:52
int init
Definition: isn.c:75
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
#define SpinLockInit(lock)
Definition: spin.h:60
pg_atomic_uint32 nextVictimBuffer
Definition: freelist.c:39

References Assert(), BufferStrategyControl::bgwprocno, BufferStrategyControl::buffer_strategy_lock, BufferStrategyControl::completePasses, BufferStrategyControl::firstFreeBuffer, init, InitBufTable(), BufferStrategyControl::lastFreeBuffer, NBuffers, BufferStrategyControl::nextVictimBuffer, NUM_BUFFER_PARTITIONS, BufferStrategyControl::numBufferAllocs, pg_atomic_init_u32(), ShmemInitStruct(), SpinLockInit, and StrategyControl.

Referenced by InitBufferPool().

◆ StrategyNotifyBgWriter()

void StrategyNotifyBgWriter ( int  bgwprocno)

Definition at line 431 of file freelist.c.

432 {
433  /*
434  * We acquire buffer_strategy_lock just to ensure that the store appears
435  * atomic to StrategyGetBuffer. The bgwriter should call this rather
436  * infrequently, so there's no performance penalty from being safe.
437  */
439  StrategyControl->bgwprocno = bgwprocno;
441 }

References BufferStrategyControl::bgwprocno, BufferStrategyControl::buffer_strategy_lock, SpinLockAcquire, SpinLockRelease, and StrategyControl.

Referenced by BackgroundWriterMain().

◆ StrategyRejectBuffer()

bool StrategyRejectBuffer ( BufferAccessStrategy  strategy,
BufferDesc buf 
)

Definition at line 685 of file freelist.c.

686 {
687  /* We only do this in bulkread mode */
688  if (strategy->btype != BAS_BULKREAD)
689  return false;
690 
691  /* Don't muck with behavior of normal buffer-replacement strategy */
692  if (!strategy->current_was_in_ring ||
693  strategy->buffers[strategy->current] != BufferDescriptorGetBuffer(buf))
694  return false;
695 
696  /*
697  * Remove the dirty buffer from the ring; necessary to prevent infinite
698  * loop if all ring members are dirty.
699  */
700  strategy->buffers[strategy->current] = InvalidBuffer;
701 
702  return true;
703 }
@ BAS_BULKREAD
Definition: bufmgr.h:30
BufferAccessStrategyType btype
Definition: freelist.c:74
Buffer buffers[FLEXIBLE_ARRAY_MEMBER]
Definition: freelist.c:96

References BAS_BULKREAD, BufferAccessStrategyData::btype, buf, BufferDescriptorGetBuffer(), BufferAccessStrategyData::buffers, BufferAccessStrategyData::current, BufferAccessStrategyData::current_was_in_ring, and InvalidBuffer.

Referenced by BufferAlloc().

◆ StrategyShmemSize()

Size StrategyShmemSize ( void  )

Definition at line 453 of file freelist.c.

454 {
455  Size size = 0;
456 
457  /* size of lookup hash table ... see comment in StrategyInitialize */
459 
460  /* size of the shared replacement strategy control block */
461  size = add_size(size, MAXALIGN(sizeof(BufferStrategyControl)));
462 
463  return size;
464 }
Size BufTableShmemSize(int size)
Definition: buf_table.c:42
#define MAXALIGN(LEN)
Definition: c.h:747
Size add_size(Size s1, Size s2)
Definition: shmem.c:502

References add_size(), BufTableShmemSize(), MAXALIGN, NBuffers, and NUM_BUFFER_PARTITIONS.

Referenced by BufferShmemSize().

◆ StrategySyncStart()

int StrategySyncStart ( uint32 complete_passes,
uint32 num_buf_alloc 
)

Definition at line 394 of file freelist.c.

395 {
396  uint32 nextVictimBuffer;
397  int result;
398 
401  result = nextVictimBuffer % NBuffers;
402 
403  if (complete_passes)
404  {
405  *complete_passes = StrategyControl->completePasses;
406 
407  /*
408  * Additionally add the number of wraparounds that happened before
409  * completePasses could be incremented. C.f. ClockSweepTick().
410  */
411  *complete_passes += nextVictimBuffer / NBuffers;
412  }
413 
414  if (num_buf_alloc)
415  {
417  }
419  return result;
420 }
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition: atomics.h:287

References BufferStrategyControl::buffer_strategy_lock, BufferStrategyControl::completePasses, NBuffers, BufferStrategyControl::nextVictimBuffer, BufferStrategyControl::numBufferAllocs, pg_atomic_exchange_u32(), pg_atomic_read_u32(), SpinLockAcquire, SpinLockRelease, and StrategyControl.

Referenced by BgBufferSync().

◆ UnlockBufHdr()

◆ WritebackContextInit()

void WritebackContextInit ( WritebackContext context,
int *  max_pending 
)

Definition at line 4895 of file bufmgr.c.

4896 {
4897  Assert(*max_pending <= WRITEBACK_MAX_PENDING_FLUSHES);
4898 
4899  context->max_pending = max_pending;
4900  context->nr_pending = 0;
4901 }

References Assert(), WritebackContext::max_pending, WritebackContext::nr_pending, and WRITEBACK_MAX_PENDING_FLUSHES.

Referenced by BackgroundWriterMain(), BufferSync(), and InitBufferPool().

Variable Documentation

◆ BackendWritebackContext

PGDLLIMPORT WritebackContext BackendWritebackContext
extern

Definition at line 24 of file buf_init.c.

Referenced by BufferAlloc(), and InitBufferPool().

◆ BufferDescriptors

PGDLLIMPORT BufferDescPadded* BufferDescriptors
extern

Definition at line 21 of file buf_init.c.

Referenced by GetBufferDescriptor(), and InitBufferPool().

◆ BufferIOCVArray

Definition at line 23 of file buf_init.c.

Referenced by BufferDescriptorGetIOCV(), and InitBufferPool().

◆ CkptBufferIds

PGDLLIMPORT CkptSortItem* CkptBufferIds
extern

Definition at line 25 of file buf_init.c.

Referenced by BufferSync(), and InitBufferPool().

◆ LocalBufferDescriptors

PGDLLIMPORT BufferDesc* LocalBufferDescriptors
extern

Definition at line 43 of file localbuf.c.

Referenced by GetLocalBufferDescriptor(), and InitLocalBuffers().