| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef BUFMGR_INTERNALS_H |
| #define BUFMGR_INTERNALS_H |
|
|
| #include "pgstat.h" |
| #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" |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #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) |
| #define BM_DIRTY (1U << 23) |
| #define BM_VALID (1U << 24) |
| #define BM_TAG_VALID (1U << 25) |
| #define BM_IO_IN_PROGRESS (1U << 26) |
| #define BM_IO_ERROR (1U << 27) |
| #define BM_JUST_DIRTIED (1U << 28) |
| #define BM_PIN_COUNT_WAITER (1U << 29) |
| #define BM_CHECKPOINT_NEEDED (1U << 30) |
| #define BM_PERMANENT (1U << 31) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #define BM_MAX_USAGE_COUNT 5 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct buftag |
| { |
| Oid spcOid; |
| Oid dbOid; |
| RelFileNumber relNumber; |
| ForkNumber forkNum; |
| BlockNumber blockNum; |
| } BufferTag; |
|
|
| static inline RelFileNumber |
| BufTagGetRelNumber(const BufferTag *tag) |
| { |
| return tag->relNumber; |
| } |
|
|
| static inline ForkNumber |
| BufTagGetForkNum(const BufferTag *tag) |
| { |
| return tag->forkNum; |
| } |
|
|
| static inline void |
| BufTagSetRelForkDetails(BufferTag *tag, RelFileNumber relnumber, |
| ForkNumber forknum) |
| { |
| tag->relNumber = relnumber; |
| tag->forkNum = forknum; |
| } |
|
|
| static inline RelFileLocator |
| BufTagGetRelFileLocator(const BufferTag *tag) |
| { |
| RelFileLocator rlocator; |
|
|
| rlocator.spcOid = tag->spcOid; |
| rlocator.dbOid = tag->dbOid; |
| rlocator.relNumber = BufTagGetRelNumber(tag); |
|
|
| return rlocator; |
| } |
|
|
| static inline void |
| ClearBufferTag(BufferTag *tag) |
| { |
| tag->spcOid = InvalidOid; |
| tag->dbOid = InvalidOid; |
| BufTagSetRelForkDetails(tag, InvalidRelFileNumber, InvalidForkNumber); |
| tag->blockNum = InvalidBlockNumber; |
| } |
|
|
| static inline void |
| InitBufferTag(BufferTag *tag, const RelFileLocator *rlocator, |
| ForkNumber forkNum, BlockNumber blockNum) |
| { |
| tag->spcOid = rlocator->spcOid; |
| tag->dbOid = rlocator->dbOid; |
| BufTagSetRelForkDetails(tag, rlocator->relNumber, forkNum); |
| tag->blockNum = blockNum; |
| } |
|
|
| static inline bool |
| BufferTagsEqual(const BufferTag *tag1, const BufferTag *tag2) |
| { |
| return (tag1->spcOid == tag2->spcOid) && |
| (tag1->dbOid == tag2->dbOid) && |
| (tag1->relNumber == tag2->relNumber) && |
| (tag1->blockNum == tag2->blockNum) && |
| (tag1->forkNum == tag2->forkNum); |
| } |
|
|
| static inline bool |
| BufTagMatchesRelFileLocator(const BufferTag *tag, |
| const RelFileLocator *rlocator) |
| { |
| return (tag->spcOid == rlocator->spcOid) && |
| (tag->dbOid == rlocator->dbOid) && |
| (BufTagGetRelNumber(tag) == rlocator->relNumber); |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| static inline uint32 |
| BufTableHashPartition(uint32 hashcode) |
| { |
| return hashcode % NUM_BUFFER_PARTITIONS; |
| } |
|
|
| static inline LWLock * |
| BufMappingPartitionLock(uint32 hashcode) |
| { |
| return &MainLWLockArray[BUFFER_MAPPING_LWLOCK_OFFSET + |
| BufTableHashPartition(hashcode)].lock; |
| } |
|
|
| static inline LWLock * |
| BufMappingPartitionLockByIndex(uint32 index) |
| { |
| return &MainLWLockArray[BUFFER_MAPPING_LWLOCK_OFFSET + index].lock; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct BufferDesc |
| { |
| BufferTag tag; |
| int buf_id; |
|
|
| |
| pg_atomic_uint32 state; |
|
|
| int wait_backend_pgprocno; |
| int freeNext; |
| LWLock content_lock; |
| } BufferDesc; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #define BUFFERDESC_PAD_TO_SIZE (SIZEOF_VOID_P == 8 ? 64 : 1) |
|
|
| typedef union BufferDescPadded |
| { |
| BufferDesc bufferdesc; |
| char pad[BUFFERDESC_PAD_TO_SIZE]; |
| } BufferDescPadded; |
|
|
| |
| |
| |
| |
| typedef struct PendingWriteback |
| { |
| |
| BufferTag tag; |
| } PendingWriteback; |
|
|
| |
| typedef struct WritebackContext |
| { |
| |
| int *max_pending; |
|
|
| |
| int nr_pending; |
|
|
| |
| PendingWriteback pending_writebacks[WRITEBACK_MAX_PENDING_FLUSHES]; |
| } WritebackContext; |
|
|
| |
| extern PGDLLIMPORT BufferDescPadded *BufferDescriptors; |
| extern PGDLLIMPORT ConditionVariableMinimallyPadded *BufferIOCVArray; |
| extern PGDLLIMPORT WritebackContext BackendWritebackContext; |
|
|
| |
| extern PGDLLIMPORT BufferDesc *LocalBufferDescriptors; |
|
|
|
|
| static inline BufferDesc * |
| GetBufferDescriptor(uint32 id) |
| { |
| return &(BufferDescriptors[id]).bufferdesc; |
| } |
|
|
| static inline BufferDesc * |
| GetLocalBufferDescriptor(uint32 id) |
| { |
| return &LocalBufferDescriptors[id]; |
| } |
|
|
| static inline Buffer |
| BufferDescriptorGetBuffer(const BufferDesc *bdesc) |
| { |
| return (Buffer) (bdesc->buf_id + 1); |
| } |
|
|
| static inline ConditionVariable * |
| BufferDescriptorGetIOCV(const BufferDesc *bdesc) |
| { |
| return &(BufferIOCVArray[bdesc->buf_id]).cv; |
| } |
|
|
| static inline LWLock * |
| BufferDescriptorGetContentLock(const BufferDesc *bdesc) |
| { |
| return (LWLock *) (&bdesc->content_lock); |
| } |
|
|
| |
| |
| |
| |
| #define FREENEXT_END_OF_LIST (-1) |
| #define FREENEXT_NOT_IN_LIST (-2) |
|
|
| |
| |
| |
| |
| extern uint32 LockBufHdr(BufferDesc *desc); |
|
|
| static inline void |
| UnlockBufHdr(BufferDesc *desc, uint32 buf_state) |
| { |
| pg_write_barrier(); |
| pg_atomic_write_u32(&desc->state, buf_state & (~BM_LOCKED)); |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| typedef struct CkptSortItem |
| { |
| Oid tsId; |
| RelFileNumber relNumber; |
| ForkNumber forkNum; |
| BlockNumber blockNum; |
| int buf_id; |
| } CkptSortItem; |
|
|
| extern PGDLLIMPORT CkptSortItem *CkptBufferIds; |
|
|
| |
| |
| |
| |
| extern void WritebackContextInit(WritebackContext *context, int *max_pending); |
| extern void IssuePendingWritebacks(WritebackContext *wb_context, IOContext io_context); |
| extern void ScheduleBufferTagForWriteback(WritebackContext *wb_context, |
| IOContext io_context, BufferTag *tag); |
|
|
| |
| extern IOContext IOContextForStrategy(BufferAccessStrategy strategy); |
| extern BufferDesc *StrategyGetBuffer(BufferAccessStrategy strategy, |
| uint32 *buf_state, bool *from_ring); |
| extern void StrategyFreeBuffer(BufferDesc *buf); |
| extern bool StrategyRejectBuffer(BufferAccessStrategy strategy, |
| BufferDesc *buf, bool from_ring); |
|
|
| extern int StrategySyncStart(uint32 *complete_passes, uint32 *num_buf_alloc); |
| extern void StrategyNotifyBgWriter(int bgwprocno); |
|
|
| extern Size StrategyShmemSize(void); |
| extern void StrategyInitialize(bool init); |
| extern bool have_free_buffer(void); |
|
|
| |
| extern Size BufTableShmemSize(int size); |
| extern void InitBufTable(int size); |
| extern uint32 BufTableHashCode(BufferTag *tagPtr); |
| extern int BufTableLookup(BufferTag *tagPtr, uint32 hashcode); |
| extern int BufTableInsert(BufferTag *tagPtr, uint32 hashcode, int buf_id); |
| extern void BufTableDelete(BufferTag *tagPtr, uint32 hashcode); |
|
|
| |
| extern bool PinLocalBuffer(BufferDesc *buf_hdr, bool adjust_usagecount); |
| extern void UnpinLocalBuffer(Buffer buffer); |
| extern PrefetchBufferResult PrefetchLocalBuffer(SMgrRelation smgr, |
| ForkNumber forkNum, |
| BlockNumber blockNum); |
| extern BufferDesc *LocalBufferAlloc(SMgrRelation smgr, ForkNumber forkNum, |
| BlockNumber blockNum, bool *foundPtr); |
| extern BlockNumber ExtendBufferedRelLocal(BufferManagerRelation bmr, |
| ForkNumber fork, |
| uint32 flags, |
| uint32 extend_by, |
| BlockNumber extend_upto, |
| Buffer *buffers, |
| uint32 *extended_by); |
| extern void MarkLocalBufferDirty(Buffer buffer); |
| extern void DropRelationLocalBuffers(RelFileLocator rlocator, |
| ForkNumber forkNum, |
| BlockNumber firstDelBlock); |
| extern void DropRelationAllLocalBuffers(RelFileLocator rlocator); |
| extern void AtEOXact_LocalBuffers(bool isCommit); |
|
|
| #endif |
|
|