PostgreSQL Source Code git master
buf_internals.h File Reference
#include "pgstat.h"
#include "port/atomics.h"
#include "storage/buf.h"
#include "storage/bufmgr.h"
#include "storage/condition_variable.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "storage/smgr.h"
#include "storage/spin.h"
#include "utils/relcache.h"
#include "utils/resowner.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)
 
static void ResourceOwnerRememberBuffer (ResourceOwner owner, Buffer buffer)
 
static void ResourceOwnerForgetBuffer (ResourceOwner owner, Buffer buffer)
 
static void ResourceOwnerRememberBufferIO (ResourceOwner owner, Buffer buffer)
 
static void ResourceOwnerForgetBufferIO (ResourceOwner owner, Buffer buffer)
 
void WritebackContextInit (WritebackContext *context, int *max_pending)
 
void IssuePendingWritebacks (WritebackContext *wb_context, IOContext io_context)
 
void ScheduleBufferTagForWriteback (WritebackContext *wb_context, IOContext io_context, BufferTag *tag)
 
IOContext IOContextForStrategy (BufferAccessStrategy strategy)
 
BufferDescStrategyGetBuffer (BufferAccessStrategy strategy, uint32 *buf_state, bool *from_ring)
 
void StrategyFreeBuffer (BufferDesc *buf)
 
bool StrategyRejectBuffer (BufferAccessStrategy strategy, BufferDesc *buf, bool from_ring)
 
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)
 
bool PinLocalBuffer (BufferDesc *buf_hdr, bool adjust_usagecount)
 
void UnpinLocalBuffer (Buffer buffer)
 
void UnpinLocalBufferNoOwner (Buffer buffer)
 
PrefetchBufferResult PrefetchLocalBuffer (SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum)
 
BufferDescLocalBufferAlloc (SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum, bool *foundPtr)
 
BlockNumber ExtendBufferedRelLocal (BufferManagerRelation bmr, ForkNumber fork, uint32 flags, uint32 extend_by, BlockNumber extend_upto, Buffer *buffers, uint32 *extended_by)
 
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
 
PGDLLIMPORT const ResourceOwnerDesc buffer_io_resowner_desc
 
PGDLLIMPORT const ResourceOwnerDesc buffer_pin_resowner_desc
 

Macro Definition Documentation

◆ BM_CHECKPOINT_NEEDED

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

Definition at line 67 of file buf_internals.h.

◆ BM_DIRTY

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

Definition at line 60 of file buf_internals.h.

◆ BM_IO_ERROR

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

Definition at line 64 of file buf_internals.h.

◆ BM_IO_IN_PROGRESS

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

Definition at line 63 of file buf_internals.h.

◆ BM_JUST_DIRTIED

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

Definition at line 65 of file buf_internals.h.

◆ BM_LOCKED

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

Definition at line 59 of file buf_internals.h.

◆ BM_MAX_USAGE_COUNT

#define BM_MAX_USAGE_COUNT   5

Definition at line 77 of file buf_internals.h.

◆ BM_PERMANENT

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

Definition at line 68 of file buf_internals.h.

◆ BM_PIN_COUNT_WAITER

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

Definition at line 66 of file buf_internals.h.

◆ BM_TAG_VALID

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

Definition at line 62 of file buf_internals.h.

◆ BM_VALID

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

Definition at line 61 of file buf_internals.h.

◆ BUF_FLAG_MASK

#define BUF_FLAG_MASK   0xFFC00000U

Definition at line 47 of file buf_internals.h.

◆ BUF_REFCOUNT_MASK

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

Definition at line 43 of file buf_internals.h.

◆ BUF_REFCOUNT_ONE

#define BUF_REFCOUNT_ONE   1

Definition at line 42 of file buf_internals.h.

◆ BUF_STATE_GET_REFCOUNT

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

Definition at line 50 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 51 of file buf_internals.h.

◆ BUF_USAGECOUNT_MASK

#define BUF_USAGECOUNT_MASK   0x003C0000U

Definition at line 44 of file buf_internals.h.

◆ BUF_USAGECOUNT_ONE

#define BUF_USAGECOUNT_ONE   (1U << 18)

Definition at line 45 of file buf_internals.h.

◆ BUF_USAGECOUNT_SHIFT

#define BUF_USAGECOUNT_SHIFT   18

Definition at line 46 of file buf_internals.h.

◆ BUFFERDESC_PAD_TO_SIZE

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

Definition at line 276 of file buf_internals.h.

◆ FREENEXT_END_OF_LIST

#define FREENEXT_END_OF_LIST   (-1)

Definition at line 350 of file buf_internals.h.

◆ FREENEXT_NOT_IN_LIST

#define FREENEXT_NOT_IN_LIST   (-2)

Definition at line 351 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 820 of file localbuf.c.

821{
823}
static void CheckForLocalBufferLeaks(void)
Definition: localbuf.c:787

References CheckForLocalBufferLeaks().

Referenced by AtEOXact_Buffers().

◆ BufferDescriptorGetBuffer()

◆ BufferDescriptorGetContentLock()

◆ BufferDescriptorGetIOCV()

static ConditionVariable * BufferDescriptorGetIOCV ( const BufferDesc bdesc)
inlinestatic

Definition at line 335 of file buf_internals.h.

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

References BufferDesc::buf_id, and BufferIOCVArray.

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

◆ BufferTagsEqual()

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

Definition at line 152 of file buf_internals.h.

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

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

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

◆ BufMappingPartitionLock()

static LWLock * BufMappingPartitionLock ( uint32  hashcode)
inlinestatic

Definition at line 184 of file buf_internals.h.

186{
188 BufTableHashPartition(hashcode)].lock;
static uint32 BufTableHashPartition(uint32 hashcode)
LWLockPadded * MainLWLockArray
Definition: lwlock.c:188
#define BUFFER_MAPPING_LWLOCK_OFFSET
Definition: lwlock.h:104
LWLock lock
Definition: lwlock.h:70

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

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

◆ BufMappingPartitionLockByIndex()

static LWLock * BufMappingPartitionLockByIndex ( uint32  index)
inlinestatic

◆ BufTableDelete()

void BufTableDelete ( BufferTag tagPtr,
uint32  hashcode 
)

Definition at line 148 of file buf_table.c.

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

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

Referenced by InvalidateBuffer(), and InvalidateVictimBuffer().

◆ BufTableHashCode()

uint32 BufTableHashCode ( BufferTag tagPtr)

Definition at line 78 of file buf_table.c.

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

References get_hash_value(), and SharedBufHash.

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

◆ BufTableHashPartition()

static uint32 BufTableHashPartition ( uint32  hashcode)
inlinestatic

Definition at line 178 of file buf_internals.h.

180{
181 return hashcode % NUM_BUFFER_PARTITIONS;
#define NUM_BUFFER_PARTITIONS
Definition: lwlock.h:93

References NUM_BUFFER_PARTITIONS.

Referenced by BufMappingPartitionLock().

◆ BufTableInsert()

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

Definition at line 118 of file buf_table.c.

119{
120 BufferLookupEnt *result;
121 bool found;
122
123 Assert(buf_id >= 0); /* -1 is reserved for not-in-table */
124 Assert(tagPtr->blockNum != P_NEW); /* invalid tag */
125
126 result = (BufferLookupEnt *)
128 tagPtr,
129 hashcode,
131 &found);
132
133 if (found) /* found something already in the table */
134 return result->id;
135
136 result->id = buf_id;
137
138 return -1;
139}
#define P_NEW
Definition: bufmgr.h:184
#define Assert(condition)
Definition: c.h:815
@ HASH_ENTER
Definition: hsearch.h:114

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

Referenced by BufferAlloc(), and ExtendBufferedRelShared().

◆ BufTableLookup()

int BufTableLookup ( BufferTag tagPtr,
uint32  hashcode 
)

Definition at line 90 of file buf_table.c.

91{
92 BufferLookupEnt *result;
93
94 result = (BufferLookupEnt *)
96 tagPtr,
97 hashcode,
99 NULL);
100
101 if (!result)
102 return -1;
103
104 return result->id;
105}
@ 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 41 of file buf_table.c.

42{
44}
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:783
static pg_noinline void Size size
Definition: slab.c:607

References hash_estimate_size(), and size.

Referenced by StrategyShmemSize().

◆ BufTagGetForkNum()

◆ BufTagGetRelFileLocator()

◆ BufTagGetRelNumber()

static RelFileNumber BufTagGetRelNumber ( const BufferTag tag)
inlinestatic

◆ 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 113 of file buf_internals.h.

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

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

Referenced by ClearBufferTag(), and InitBufferTag().

◆ ClearBufferTag()

static void ClearBufferTag ( BufferTag tag)
inlinestatic

Definition at line 133 of file buf_internals.h.

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

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

Referenced by BufferManagerShmemInit(), DropRelationAllLocalBuffers(), DropRelationLocalBuffers(), GetLocalVictimBuffer(), InvalidateBuffer(), and InvalidateVictimBuffer().

◆ DropRelationAllLocalBuffers()

void DropRelationAllLocalBuffers ( RelFileLocator  rlocator)

Definition at line 538 of file localbuf.c.

539{
540 int i;
541
542 for (i = 0; i < NLocBuffer; i++)
543 {
545 LocalBufferLookupEnt *hresult;
546 uint32 buf_state;
547
548 buf_state = pg_atomic_read_u32(&bufHdr->state);
549
550 if ((buf_state & BM_TAG_VALID) &&
551 BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator))
552 {
553 if (LocalRefCount[i] != 0)
554 elog(ERROR, "block %u of %s is still referenced (local %u)",
555 bufHdr->tag.blockNum,
558 BufTagGetForkNum(&bufHdr->tag)),
560 /* Remove entry from hashtable */
561 hresult = (LocalBufferLookupEnt *)
562 hash_search(LocalBufHash, &bufHdr->tag, HASH_REMOVE, NULL);
563 if (!hresult) /* shouldn't happen */
564 elog(ERROR, "local buffer hash table corrupted");
565 /* Mark buffer invalid */
566 ClearBufferTag(&bufHdr->tag);
567 buf_state &= ~BUF_FLAG_MASK;
568 buf_state &= ~BUF_USAGECOUNT_MASK;
569 pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
570 }
571 }
572}
static void pg_atomic_unlocked_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:295
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:239
#define BM_TAG_VALID
Definition: buf_internals.h:62
static ForkNumber BufTagGetForkNum(const BufferTag *tag)
static bool BufTagMatchesRelFileLocator(const BufferTag *tag, const RelFileLocator *rlocator)
static void ClearBufferTag(BufferTag *tag)
static RelFileLocator BufTagGetRelFileLocator(const BufferTag *tag)
static BufferDesc * GetLocalBufferDescriptor(uint32 id)
uint32_t uint32
Definition: c.h:488
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
ProcNumber MyProcNumber
Definition: globals.c:89
int i
Definition: isn.c:72
int32 * LocalRefCount
Definition: localbuf.c:46
static HTAB * LocalBufHash
Definition: localbuf.c:50
int NLocBuffer
Definition: localbuf.c:42
#define relpathbackend(rlocator, backend, forknum)
Definition: relpath.h:93
BufferTag tag
pg_atomic_uint32 state

References buftag::blockNum, BM_TAG_VALID, BufTagGetForkNum(), BufTagGetRelFileLocator(), BufTagMatchesRelFileLocator(), ClearBufferTag(), elog, ERROR, GetLocalBufferDescriptor(), HASH_REMOVE, hash_search(), i, LocalBufHash, LocalRefCount, MyProcNumber, 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 490 of file localbuf.c.

492{
493 int i;
494
495 for (i = 0; i < NLocBuffer; i++)
496 {
498 LocalBufferLookupEnt *hresult;
499 uint32 buf_state;
500
501 buf_state = pg_atomic_read_u32(&bufHdr->state);
502
503 if ((buf_state & BM_TAG_VALID) &&
504 BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator) &&
505 BufTagGetForkNum(&bufHdr->tag) == forkNum &&
506 bufHdr->tag.blockNum >= firstDelBlock)
507 {
508 if (LocalRefCount[i] != 0)
509 elog(ERROR, "block %u of %s is still referenced (local %u)",
510 bufHdr->tag.blockNum,
513 BufTagGetForkNum(&bufHdr->tag)),
515
516 /* Remove entry from hashtable */
517 hresult = (LocalBufferLookupEnt *)
518 hash_search(LocalBufHash, &bufHdr->tag, HASH_REMOVE, NULL);
519 if (!hresult) /* shouldn't happen */
520 elog(ERROR, "local buffer hash table corrupted");
521 /* Mark buffer invalid */
522 ClearBufferTag(&bufHdr->tag);
523 buf_state &= ~BUF_FLAG_MASK;
524 buf_state &= ~BUF_USAGECOUNT_MASK;
525 pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
526 }
527 }
528}

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

Referenced by DropRelationBuffers().

◆ ExtendBufferedRelLocal()

BlockNumber ExtendBufferedRelLocal ( BufferManagerRelation  bmr,
ForkNumber  fork,
uint32  flags,
uint32  extend_by,
BlockNumber  extend_upto,
Buffer buffers,
uint32 extended_by 
)

Definition at line 314 of file localbuf.c.

321{
322 BlockNumber first_block;
323 instr_time io_start;
324
325 /* Initialize local buffers if first request in this session */
326 if (LocalBufHash == NULL)
328
329 LimitAdditionalLocalPins(&extend_by);
330
331 for (uint32 i = 0; i < extend_by; i++)
332 {
333 BufferDesc *buf_hdr;
334 Block buf_block;
335
336 buffers[i] = GetLocalVictimBuffer();
337 buf_hdr = GetLocalBufferDescriptor(-buffers[i] - 1);
338 buf_block = LocalBufHdrGetBlock(buf_hdr);
339
340 /* new buffers are zero-filled */
341 MemSet(buf_block, 0, BLCKSZ);
342 }
343
344 first_block = smgrnblocks(bmr.smgr, fork);
345
346 if (extend_upto != InvalidBlockNumber)
347 {
348 /*
349 * In contrast to shared relations, nothing could change the relation
350 * size concurrently. Thus we shouldn't end up finding that we don't
351 * need to do anything.
352 */
353 Assert(first_block <= extend_upto);
354
355 Assert((uint64) first_block + extend_by <= extend_upto);
356 }
357
358 /* Fail if relation is already at maximum possible length */
359 if ((uint64) first_block + extend_by >= MaxBlockNumber)
361 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
362 errmsg("cannot extend relation %s beyond %u blocks",
363 relpath(bmr.smgr->smgr_rlocator, fork),
365
366 for (uint32 i = 0; i < extend_by; i++)
367 {
368 int victim_buf_id;
369 BufferDesc *victim_buf_hdr;
370 BufferTag tag;
371 LocalBufferLookupEnt *hresult;
372 bool found;
373
374 victim_buf_id = -buffers[i] - 1;
375 victim_buf_hdr = GetLocalBufferDescriptor(victim_buf_id);
376
377 /* in case we need to pin an existing buffer below */
379
380 InitBufferTag(&tag, &bmr.smgr->smgr_rlocator.locator, fork, first_block + i);
381
382 hresult = (LocalBufferLookupEnt *)
383 hash_search(LocalBufHash, &tag, HASH_ENTER, &found);
384 if (found)
385 {
386 BufferDesc *existing_hdr;
387 uint32 buf_state;
388
390
391 existing_hdr = GetLocalBufferDescriptor(hresult->id);
392 PinLocalBuffer(existing_hdr, false);
393 buffers[i] = BufferDescriptorGetBuffer(existing_hdr);
394
395 buf_state = pg_atomic_read_u32(&existing_hdr->state);
396 Assert(buf_state & BM_TAG_VALID);
397 Assert(!(buf_state & BM_DIRTY));
398 buf_state &= ~BM_VALID;
399 pg_atomic_unlocked_write_u32(&existing_hdr->state, buf_state);
400 }
401 else
402 {
403 uint32 buf_state = pg_atomic_read_u32(&victim_buf_hdr->state);
404
405 Assert(!(buf_state & (BM_VALID | BM_TAG_VALID | BM_DIRTY | BM_JUST_DIRTIED)));
406
407 victim_buf_hdr->tag = tag;
408
409 buf_state |= BM_TAG_VALID | BUF_USAGECOUNT_ONE;
410
411 pg_atomic_unlocked_write_u32(&victim_buf_hdr->state, buf_state);
412
413 hresult->id = victim_buf_id;
414 }
415 }
416
418
419 /* actually extend relation */
420 smgrzeroextend(bmr.smgr, fork, first_block, extend_by, false);
421
423 io_start, 1, extend_by * BLCKSZ);
424
425 for (uint32 i = 0; i < extend_by; i++)
426 {
427 Buffer buf = buffers[i];
428 BufferDesc *buf_hdr;
429 uint32 buf_state;
430
431 buf_hdr = GetLocalBufferDescriptor(-buf - 1);
432
433 buf_state = pg_atomic_read_u32(&buf_hdr->state);
434 buf_state |= BM_VALID;
435 pg_atomic_unlocked_write_u32(&buf_hdr->state, buf_state);
436 }
437
438 *extended_by = extend_by;
439
441
442 return first_block;
443}
uint32 BlockNumber
Definition: block.h:31
#define MaxBlockNumber
Definition: block.h:35
static void InitBufferTag(BufferTag *tag, const RelFileLocator *rlocator, ForkNumber forkNum, BlockNumber blockNum)
#define BM_DIRTY
Definition: buf_internals.h:60
#define BM_JUST_DIRTIED
Definition: buf_internals.h:65
#define BUF_USAGECOUNT_ONE
Definition: buf_internals.h:45
#define BM_VALID
Definition: buf_internals.h:61
static Buffer BufferDescriptorGetBuffer(const BufferDesc *bdesc)
bool track_io_timing
Definition: bufmgr.c:143
void * Block
Definition: bufmgr.h:25
uint64_t uint64
Definition: c.h:489
#define MemSet(start, val, len)
Definition: c.h:977
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
BufferUsage pgBufferUsage
Definition: instrument.c:20
void UnpinLocalBuffer(Buffer buffer)
Definition: localbuf.c:682
#define LocalBufHdrGetBlock(bufHdr)
Definition: localbuf.c:39
bool PinLocalBuffer(BufferDesc *buf_hdr, bool adjust_usagecount)
Definition: localbuf.c:656
static void InitLocalBuffers(void)
Definition: localbuf.c:581
void LimitAdditionalLocalPins(uint32 *additional_pins)
Definition: localbuf.c:291
static Buffer GetLocalVictimBuffer(void)
Definition: localbuf.c:177
static char * buf
Definition: pg_test_fsync.c:72
@ IOOBJECT_TEMP_RELATION
Definition: pgstat.h:276
@ IOCONTEXT_NORMAL
Definition: pgstat.h:287
@ IOOP_EXTEND
Definition: pgstat.h:312
instr_time pgstat_prepare_io_time(bool track_io_guc)
Definition: pgstat_io.c:90
void pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op, instr_time start_time, uint32 cnt, uint64 bytes)
Definition: pgstat_io.c:120
#define relpath(rlocator, forknum)
Definition: relpath.h:102
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
void ResourceOwnerEnlarge(ResourceOwner owner)
Definition: resowner.c:442
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:677
void smgrzeroextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition: smgr.c:563
struct SMgrRelationData * smgr
Definition: bufmgr.h:103
int64 local_blks_written
Definition: instrument.h:33
RelFileLocator locator
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:37

References Assert, BM_DIRTY, BM_JUST_DIRTIED, BM_TAG_VALID, BM_VALID, buf, BUF_USAGECOUNT_ONE, BufferDescriptorGetBuffer(), CurrentResourceOwner, ereport, errcode(), errmsg(), ERROR, GetLocalBufferDescriptor(), GetLocalVictimBuffer(), HASH_ENTER, hash_search(), i, LocalBufferLookupEnt::id, InitBufferTag(), InitLocalBuffers(), InvalidBlockNumber, IOCONTEXT_NORMAL, IOOBJECT_TEMP_RELATION, IOOP_EXTEND, LimitAdditionalLocalPins(), BufferUsage::local_blks_written, LocalBufHash, LocalBufHdrGetBlock, RelFileLocatorBackend::locator, MaxBlockNumber, MemSet, pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), pgBufferUsage, pgstat_count_io_op_time(), pgstat_prepare_io_time(), PinLocalBuffer(), relpath, ResourceOwnerEnlarge(), BufferManagerRelation::smgr, SMgrRelationData::smgr_rlocator, smgrnblocks(), smgrzeroextend(), BufferDesc::state, BufferDesc::tag, track_io_timing, and UnpinLocalBuffer().

Referenced by ExtendBufferedRelCommon().

◆ GetBufferDescriptor()

◆ GetLocalBufferDescriptor()

◆ have_free_buffer()

bool have_free_buffer ( void  )

Definition at line 175 of file freelist.c.

176{
178 return true;
179 else
180 return false;
181}
static BufferStrategyControl * StrategyControl
Definition: freelist.c:65

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 51 of file buf_table.c.

52{
53 HASHCTL info;
54
55 /* assume no locking is needed yet */
56
57 /* BufferTag maps to Buffer */
58 info.keysize = sizeof(BufferTag);
59 info.entrysize = sizeof(BufferLookupEnt);
61
62 SharedBufHash = ShmemInitHash("Shared Buffer Lookup Table",
63 size, size,
64 &info,
66}
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:327
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, ShmemInitHash(), and size.

Referenced by StrategyInitialize().

◆ IOContextForStrategy()

IOContext IOContextForStrategy ( BufferAccessStrategy  strategy)

Definition at line 758 of file freelist.c.

759{
760 if (!strategy)
761 return IOCONTEXT_NORMAL;
762
763 switch (strategy->btype)
764 {
765 case BAS_NORMAL:
766
767 /*
768 * Currently, GetAccessStrategy() returns NULL for
769 * BufferAccessStrategyType BAS_NORMAL, so this case is
770 * unreachable.
771 */
773 return IOCONTEXT_NORMAL;
774 case BAS_BULKREAD:
775 return IOCONTEXT_BULKREAD;
776 case BAS_BULKWRITE:
777 return IOCONTEXT_BULKWRITE;
778 case BAS_VACUUM:
779 return IOCONTEXT_VACUUM;
780 }
781
782 elog(ERROR, "unrecognized BufferAccessStrategyType: %d", strategy->btype);
784}
@ BAS_BULKREAD
Definition: bufmgr.h:36
@ BAS_NORMAL
Definition: bufmgr.h:35
@ BAS_VACUUM
Definition: bufmgr.h:39
@ BAS_BULKWRITE
Definition: bufmgr.h:38
#define pg_unreachable()
Definition: c.h:318
@ IOCONTEXT_VACUUM
Definition: pgstat.h:288
@ IOCONTEXT_BULKREAD
Definition: pgstat.h:284
@ IOCONTEXT_BULKWRITE
Definition: pgstat.h:285
BufferAccessStrategyType btype
Definition: freelist.c:75

References BAS_BULKREAD, BAS_BULKWRITE, BAS_NORMAL, BAS_VACUUM, BufferAccessStrategyData::btype, elog, ERROR, IOCONTEXT_BULKREAD, IOCONTEXT_BULKWRITE, IOCONTEXT_NORMAL, IOCONTEXT_VACUUM, and pg_unreachable.

Referenced by ExtendBufferedRelShared(), PinBufferForBlock(), and WaitReadBuffers().

◆ IssuePendingWritebacks()

void IssuePendingWritebacks ( WritebackContext wb_context,
IOContext  io_context 
)

Definition at line 5907 of file bufmgr.c.

5908{
5909 instr_time io_start;
5910 int i;
5911
5912 if (wb_context->nr_pending == 0)
5913 return;
5914
5915 /*
5916 * Executing the writes in-order can make them a lot faster, and allows to
5917 * merge writeback requests to consecutive blocks into larger writebacks.
5918 */
5919 sort_pending_writebacks(wb_context->pending_writebacks,
5920 wb_context->nr_pending);
5921
5923
5924 /*
5925 * Coalesce neighbouring writes, but nothing else. For that we iterate
5926 * through the, now sorted, array of pending flushes, and look forward to
5927 * find all neighbouring (or identical) writes.
5928 */
5929 for (i = 0; i < wb_context->nr_pending; i++)
5930 {
5933 SMgrRelation reln;
5934 int ahead;
5935 BufferTag tag;
5936 RelFileLocator currlocator;
5937 Size nblocks = 1;
5938
5939 cur = &wb_context->pending_writebacks[i];
5940 tag = cur->tag;
5941 currlocator = BufTagGetRelFileLocator(&tag);
5942
5943 /*
5944 * Peek ahead, into following writeback requests, to see if they can
5945 * be combined with the current one.
5946 */
5947 for (ahead = 0; i + ahead + 1 < wb_context->nr_pending; ahead++)
5948 {
5949
5950 next = &wb_context->pending_writebacks[i + ahead + 1];
5951
5952 /* different file, stop */
5953 if (!RelFileLocatorEquals(currlocator,
5954 BufTagGetRelFileLocator(&next->tag)) ||
5955 BufTagGetForkNum(&cur->tag) != BufTagGetForkNum(&next->tag))
5956 break;
5957
5958 /* ok, block queued twice, skip */
5959 if (cur->tag.blockNum == next->tag.blockNum)
5960 continue;
5961
5962 /* only merge consecutive writes */
5963 if (cur->tag.blockNum + 1 != next->tag.blockNum)
5964 break;
5965
5966 nblocks++;
5967 cur = next;
5968 }
5969
5970 i += ahead;
5971
5972 /* and finally tell the kernel to write the data to storage */
5973 reln = smgropen(currlocator, INVALID_PROC_NUMBER);
5974 smgrwriteback(reln, BufTagGetForkNum(&tag), tag.blockNum, nblocks);
5975 }
5976
5977 /*
5978 * Assume that writeback requests are only issued for buffers containing
5979 * blocks of permanent relations.
5980 */
5982 IOOP_WRITEBACK, io_start, wb_context->nr_pending, 0);
5983
5984 wb_context->nr_pending = 0;
5985}
static int32 next
Definition: blutils.c:221
size_t Size
Definition: c.h:562
struct cursor * cur
Definition: ecpg.c:29
@ IOOBJECT_RELATION
Definition: pgstat.h:275
@ IOOP_WRITEBACK
Definition: pgstat.h:309
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
#define RelFileLocatorEquals(locator1, locator2)
void smgrwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: smgr.c:665
SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend)
Definition: smgr.c:201
PendingWriteback pending_writebacks[WRITEBACK_MAX_PENDING_FLUSHES]

References buftag::blockNum, BufTagGetForkNum(), BufTagGetRelFileLocator(), cur, i, INVALID_PROC_NUMBER, IOOBJECT_RELATION, IOOP_WRITEBACK, next, WritebackContext::nr_pending, WritebackContext::pending_writebacks, pgstat_count_io_op_time(), pgstat_prepare_io_time(), RelFileLocatorEquals, smgropen(), smgrwriteback(), and track_io_timing.

Referenced by BufferSync(), and ScheduleBufferTagForWriteback().

◆ LocalBufferAlloc()

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

Definition at line 116 of file localbuf.c.

118{
119 BufferTag newTag; /* identity of requested block */
120 LocalBufferLookupEnt *hresult;
121 BufferDesc *bufHdr;
122 Buffer victim_buffer;
123 int bufid;
124 bool found;
125
126 InitBufferTag(&newTag, &smgr->smgr_rlocator.locator, forkNum, blockNum);
127
128 /* Initialize local buffers if first request in this session */
129 if (LocalBufHash == NULL)
131
133
134 /* See if the desired buffer already exists */
135 hresult = (LocalBufferLookupEnt *)
136 hash_search(LocalBufHash, &newTag, HASH_FIND, NULL);
137
138 if (hresult)
139 {
140 bufid = hresult->id;
141 bufHdr = GetLocalBufferDescriptor(bufid);
142 Assert(BufferTagsEqual(&bufHdr->tag, &newTag));
143
144 *foundPtr = PinLocalBuffer(bufHdr, true);
145 }
146 else
147 {
148 uint32 buf_state;
149
150 victim_buffer = GetLocalVictimBuffer();
151 bufid = -victim_buffer - 1;
152 bufHdr = GetLocalBufferDescriptor(bufid);
153
154 hresult = (LocalBufferLookupEnt *)
155 hash_search(LocalBufHash, &newTag, HASH_ENTER, &found);
156 if (found) /* shouldn't happen */
157 elog(ERROR, "local buffer hash table corrupted");
158 hresult->id = bufid;
159
160 /*
161 * it's all ours now.
162 */
163 bufHdr->tag = newTag;
164
165 buf_state = pg_atomic_read_u32(&bufHdr->state);
166 buf_state &= ~(BUF_FLAG_MASK | BUF_USAGECOUNT_MASK);
167 buf_state |= BM_TAG_VALID | BUF_USAGECOUNT_ONE;
168 pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
169
170 *foundPtr = false;
171 }
172
173 return bufHdr;
174}
#define BUF_USAGECOUNT_MASK
Definition: buf_internals.h:44
static bool BufferTagsEqual(const BufferTag *tag1, const BufferTag *tag2)
#define BUF_FLAG_MASK
Definition: buf_internals.h:47

References Assert, BM_TAG_VALID, BUF_FLAG_MASK, BUF_USAGECOUNT_MASK, BUF_USAGECOUNT_ONE, BufferTagsEqual(), CurrentResourceOwner, elog, ERROR, GetLocalBufferDescriptor(), GetLocalVictimBuffer(), HASH_ENTER, HASH_FIND, hash_search(), LocalBufferLookupEnt::id, InitBufferTag(), InitLocalBuffers(), LocalBufHash, RelFileLocatorBackend::locator, pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), PinLocalBuffer(), ResourceOwnerEnlarge(), SMgrRelationData::smgr_rlocator, BufferDesc::state, and BufferDesc::tag.

Referenced by PinBufferForBlock().

◆ LockBufHdr()

uint32 LockBufHdr ( BufferDesc desc)

Definition at line 5703 of file bufmgr.c.

5704{
5705 SpinDelayStatus delayStatus;
5706 uint32 old_buf_state;
5707
5709
5710 init_local_spin_delay(&delayStatus);
5711
5712 while (true)
5713 {
5714 /* set BM_LOCKED flag */
5715 old_buf_state = pg_atomic_fetch_or_u32(&desc->state, BM_LOCKED);
5716 /* if it wasn't set before we're OK */
5717 if (!(old_buf_state & BM_LOCKED))
5718 break;
5719 perform_spin_delay(&delayStatus);
5720 }
5721 finish_spin_delay(&delayStatus);
5722 return old_buf_state | BM_LOCKED;
5723}
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition: atomics.h:410
#define BufferIsLocal(buffer)
Definition: buf.h:37
#define BM_LOCKED
Definition: buf_internals.h:59
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:751

References Assert, BM_LOCKED, BufferDescriptorGetBuffer(), BufferIsLocal, 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(), EvictUnpinnedBuffer(), ExtendBufferedRelShared(), FindAndDropRelationBuffers(), FlushBuffer(), FlushDatabaseBuffers(), FlushRelationBuffers(), FlushRelationsAllBuffers(), GetBufferFromRing(), GetVictimBuffer(), InvalidateBuffer(), InvalidateVictimBuffer(), IsBufferCleanupOK(), LockBufferForCleanup(), MarkBufferDirtyHint(), pg_buffercache_pages(), ReadRecentBuffer(), StartBufferIO(), StrategyGetBuffer(), SyncOneBuffer(), TerminateBufferIO(), UnlockBuffers(), UnpinBufferNoOwner(), and WaitIO().

◆ MarkLocalBufferDirty()

void MarkLocalBufferDirty ( Buffer  buffer)

Definition at line 450 of file localbuf.c.

451{
452 int bufid;
453 BufferDesc *bufHdr;
454 uint32 buf_state;
455
456 Assert(BufferIsLocal(buffer));
457
458#ifdef LBDEBUG
459 fprintf(stderr, "LB DIRTY %d\n", buffer);
460#endif
461
462 bufid = -buffer - 1;
463
464 Assert(LocalRefCount[bufid] > 0);
465
466 bufHdr = GetLocalBufferDescriptor(bufid);
467
468 buf_state = pg_atomic_read_u32(&bufHdr->state);
469
470 if (!(buf_state & BM_DIRTY))
472
473 buf_state |= BM_DIRTY;
474
475 pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
476}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
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().

◆ PinLocalBuffer()

bool PinLocalBuffer ( BufferDesc buf_hdr,
bool  adjust_usagecount 
)

Definition at line 656 of file localbuf.c.

657{
658 uint32 buf_state;
659 Buffer buffer = BufferDescriptorGetBuffer(buf_hdr);
660 int bufid = -buffer - 1;
661
662 buf_state = pg_atomic_read_u32(&buf_hdr->state);
663
664 if (LocalRefCount[bufid] == 0)
665 {
667 if (adjust_usagecount &&
669 {
670 buf_state += BUF_USAGECOUNT_ONE;
671 pg_atomic_unlocked_write_u32(&buf_hdr->state, buf_state);
672 }
673 }
674 LocalRefCount[bufid]++;
677
678 return buf_state & BM_VALID;
679}
#define BM_MAX_USAGE_COUNT
Definition: buf_internals.h:77
#define BUF_STATE_GET_USAGECOUNT(state)
Definition: buf_internals.h:51
static void ResourceOwnerRememberBuffer(ResourceOwner owner, Buffer buffer)
static int NLocalPinnedBuffers
Definition: localbuf.c:53

References BM_MAX_USAGE_COUNT, BM_VALID, BUF_STATE_GET_USAGECOUNT, BUF_USAGECOUNT_ONE, BufferDescriptorGetBuffer(), CurrentResourceOwner, LocalRefCount, NLocalPinnedBuffers, pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), ResourceOwnerRememberBuffer(), and BufferDesc::state.

Referenced by ExtendBufferedRelLocal(), GetLocalVictimBuffer(), LocalBufferAlloc(), and ReadRecentBuffer().

◆ PrefetchLocalBuffer()

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

Definition at line 69 of file localbuf.c.

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

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

Referenced by PrefetchBuffer().

◆ ResourceOwnerForgetBuffer()

static void ResourceOwnerForgetBuffer ( ResourceOwner  owner,
Buffer  buffer 
)
inlinestatic

Definition at line 396 of file buf_internals.h.

398{
PGDLLIMPORT const ResourceOwnerDesc buffer_pin_resowner_desc
Definition: bufmgr.c:235
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
void ResourceOwnerForget(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition: resowner.c:554

References buffer_pin_resowner_desc, Int32GetDatum(), and ResourceOwnerForget().

Referenced by UnpinBuffer(), and UnpinLocalBuffer().

◆ ResourceOwnerForgetBufferIO()

static void ResourceOwnerForgetBufferIO ( ResourceOwner  owner,
Buffer  buffer 
)
inlinestatic

Definition at line 406 of file buf_internals.h.

408{
PGDLLIMPORT const ResourceOwnerDesc buffer_io_resowner_desc
Definition: bufmgr.c:226

References buffer_io_resowner_desc, Int32GetDatum(), and ResourceOwnerForget().

Referenced by TerminateBufferIO().

◆ ResourceOwnerRememberBuffer()

static void ResourceOwnerRememberBuffer ( ResourceOwner  owner,
Buffer  buffer 
)
inlinestatic

Definition at line 391 of file buf_internals.h.

393{
void ResourceOwnerRemember(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition: resowner.c:514

References buffer_pin_resowner_desc, Int32GetDatum(), and ResourceOwnerRemember().

Referenced by IncrBufferRefCount(), PinBuffer(), PinBuffer_Locked(), and PinLocalBuffer().

◆ ResourceOwnerRememberBufferIO()

static void ResourceOwnerRememberBufferIO ( ResourceOwner  owner,
Buffer  buffer 
)
inlinestatic

◆ ScheduleBufferTagForWriteback()

void ScheduleBufferTagForWriteback ( WritebackContext wb_context,
IOContext  io_context,
BufferTag tag 
)

Definition at line 5857 of file bufmgr.c.

5859{
5860 PendingWriteback *pending;
5861
5862 /*
5863 * As pg_flush_data() doesn't do anything with fsync disabled, there's no
5864 * point in tracking in that case.
5865 */
5867 !enableFsync)
5868 return;
5869
5870 /*
5871 * Add buffer to the pending writeback array, unless writeback control is
5872 * disabled.
5873 */
5874 if (*wb_context->max_pending > 0)
5875 {
5877
5878 pending = &wb_context->pending_writebacks[wb_context->nr_pending++];
5879
5880 pending->tag = *tag;
5881 }
5882
5883 /*
5884 * Perform pending flushes if the writeback limit is exceeded. This
5885 * includes the case where previously an item has been added, but control
5886 * is now disabled.
5887 */
5888 if (wb_context->nr_pending >= *wb_context->max_pending)
5889 IssuePendingWritebacks(wb_context, io_context);
5890}
void IssuePendingWritebacks(WritebackContext *wb_context, IOContext io_context)
Definition: bufmgr.c:5907
bool enableFsync
Definition: globals.c:128
#define WRITEBACK_MAX_PENDING_FLUSHES

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

Referenced by GetVictimBuffer(), 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 {
374 if (buf->freeNext < 0)
377 }
378
380}
#define FREENEXT_NOT_IN_LIST
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
slock_t buffer_strategy_lock
Definition: freelist.c:33

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

Referenced by BufferAlloc(), ExtendBufferedRelShared(), and InvalidateBuffer().

◆ StrategyGetBuffer()

BufferDesc * StrategyGetBuffer ( BufferAccessStrategy  strategy,
uint32 buf_state,
bool *  from_ring 
)

Definition at line 196 of file freelist.c.

197{
199 int bgwprocno;
200 int trycounter;
201 uint32 local_buf_state; /* to avoid repeated (de-)referencing */
202
203 *from_ring = false;
204
205 /*
206 * If given a strategy object, see whether it can select a buffer. We
207 * assume strategy objects don't need buffer_strategy_lock.
208 */
209 if (strategy != NULL)
210 {
211 buf = GetBufferFromRing(strategy, buf_state);
212 if (buf != NULL)
213 {
214 *from_ring = true;
215 return buf;
216 }
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 */
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 */
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:366
static void UnlockBufHdr(BufferDesc *desc, uint32 buf_state)
#define BUF_STATE_GET_REFCOUNT(state)
Definition: buf_internals.h:50
static BufferDesc * GetBufferDescriptor(uint32 id)
uint32 LockBufHdr(BufferDesc *desc)
Definition: bufmgr.c:5703
static uint32 ClockSweepTick(void)
Definition: freelist.c:108
static void AddBufferToRing(BufferAccessStrategy strategy, BufferDesc *buf)
Definition: freelist.c:748
#define INT_ACCESS_ONCE(var)
Definition: freelist.c:24
static BufferDesc * GetBufferFromRing(BufferAccessStrategy strategy, uint32 *buf_state)
Definition: freelist.c:695
int NBuffers
Definition: globals.c:141
void SetLatch(Latch *latch)
Definition: latch.c:632
PROC_HDR * ProcGlobal
Definition: proc.c:78
pg_atomic_uint32 numBufferAllocs
Definition: freelist.c:55
Latch procLatch
Definition: proc.h:169
PGPROC * allProcs
Definition: proc.h:371

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 GetVictimBuffer().

◆ 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 BufferManagerShmemInit().
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:221
void InitBufTable(int size)
Definition: buf_table.c:51
int init
Definition: isn.c:74
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:382
#define SpinLockInit(lock)
Definition: spin.h:57
pg_atomic_uint32 nextVictimBuffer
Definition: freelist.c:40

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 BufferManagerShmemInit().

◆ 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,
bool  from_ring 
)

Definition at line 798 of file freelist.c.

799{
800 /* We only do this in bulkread mode */
801 if (strategy->btype != BAS_BULKREAD)
802 return false;
803
804 /* Don't muck with behavior of normal buffer-replacement strategy */
805 if (!from_ring ||
806 strategy->buffers[strategy->current] != BufferDescriptorGetBuffer(buf))
807 return false;
808
809 /*
810 * Remove the dirty buffer from the ring; necessary to prevent infinite
811 * loop if all ring members are dirty.
812 */
813 strategy->buffers[strategy->current] = InvalidBuffer;
814
815 return true;
816}
Buffer buffers[FLEXIBLE_ARRAY_MEMBER]
Definition: freelist.c:91

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

Referenced by GetVictimBuffer().

◆ 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 */
462
463 return size;
464}
Size BufTableShmemSize(int size)
Definition: buf_table.c:41
#define MAXALIGN(LEN)
Definition: c.h:768
Size add_size(Size s1, Size s2)
Definition: shmem.c:488

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

Referenced by BufferManagerShmemSize().

◆ 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:330

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()

◆ UnpinLocalBuffer()

void UnpinLocalBuffer ( Buffer  buffer)

Definition at line 682 of file localbuf.c.

683{
686}
static void ResourceOwnerForgetBuffer(ResourceOwner owner, Buffer buffer)
void UnpinLocalBufferNoOwner(Buffer buffer)
Definition: localbuf.c:689

References CurrentResourceOwner, ResourceOwnerForgetBuffer(), and UnpinLocalBufferNoOwner().

Referenced by ExtendBufferedRelLocal(), ReleaseAndReadBuffer(), and ReleaseBuffer().

◆ UnpinLocalBufferNoOwner()

void UnpinLocalBufferNoOwner ( Buffer  buffer)

Definition at line 689 of file localbuf.c.

690{
691 int buffid = -buffer - 1;
692
693 Assert(BufferIsLocal(buffer));
694 Assert(LocalRefCount[buffid] > 0);
696
697 if (--LocalRefCount[buffid] == 0)
699}

References Assert, BufferIsLocal, LocalRefCount, and NLocalPinnedBuffers.

Referenced by ResOwnerReleaseBufferPin(), and UnpinLocalBuffer().

◆ WritebackContextInit()

void WritebackContextInit ( WritebackContext context,
int *  max_pending 
)

Definition at line 5845 of file bufmgr.c.

5846{
5847 Assert(*max_pending <= WRITEBACK_MAX_PENDING_FLUSHES);
5848
5849 context->max_pending = max_pending;
5850 context->nr_pending = 0;
5851}

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

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

Variable Documentation

◆ BackendWritebackContext

PGDLLIMPORT WritebackContext BackendWritebackContext
extern

Definition at line 23 of file buf_init.c.

Referenced by BufferManagerShmemInit(), and GetVictimBuffer().

◆ buffer_io_resowner_desc

PGDLLIMPORT const ResourceOwnerDesc buffer_io_resowner_desc
extern

Definition at line 226 of file bufmgr.c.

Referenced by ResourceOwnerForgetBufferIO(), and ResourceOwnerRememberBufferIO().

◆ buffer_pin_resowner_desc

PGDLLIMPORT const ResourceOwnerDesc buffer_pin_resowner_desc
extern

Definition at line 235 of file bufmgr.c.

Referenced by ResourceOwnerForgetBuffer(), and ResourceOwnerRememberBuffer().

◆ BufferDescriptors

PGDLLIMPORT BufferDescPadded* BufferDescriptors
extern

Definition at line 20 of file buf_init.c.

Referenced by BufferManagerShmemInit(), and GetBufferDescriptor().

◆ BufferIOCVArray

Definition at line 22 of file buf_init.c.

Referenced by BufferDescriptorGetIOCV(), and BufferManagerShmemInit().

◆ CkptBufferIds

PGDLLIMPORT CkptSortItem* CkptBufferIds
extern

Definition at line 24 of file buf_init.c.

Referenced by BufferManagerShmemInit(), and BufferSync().

◆ LocalBufferDescriptors

PGDLLIMPORT BufferDesc* LocalBufferDescriptors
extern

Definition at line 44 of file localbuf.c.

Referenced by GetLocalBufferDescriptor(), and InitLocalBuffers().