41#define LocalBufHdrGetBlock(bufHdr) \
42 LocalBufferBlockPointers[-((bufHdr)->buf_id + 2)]
153 bufid = -victim_buffer - 1;
159 elog(
ERROR,
"local buffer hash table corrupted");
165 bufHdr->
tag = newTag;
194 elog(
ERROR,
"failed to start write IO on local buffer");
269 else if (--trycounter == 0)
271 (
errcode(ERRCODE_INSUFFICIENT_RESOURCES),
272 errmsg(
"no empty local buffer available")));
326 if (*additional_pins <= 1)
336 if (*additional_pins >= max_pins)
337 *additional_pins = max_pins;
372 MemSet(buf_block, 0, BLCKSZ);
384 Assert(first_block <= extend_upto);
386 Assert((
uint64) first_block + extend_by <= extend_upto);
392 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
393 errmsg(
"cannot extend relation %s beyond %u blocks",
405 victim_buf_id = -buffers[
i] - 1;
432 buf_state &= ~BM_VALID;
444 victim_buf_hdr->
tag = tag;
450 hresult->
id = victim_buf_id;
462 io_start, 1, extend_by * BLCKSZ);
477 *extended_by = extend_by;
498 fprintf(stderr,
"LB DIRTY %d\n", buffer);
569 buf_state &= ~BM_IO_ERROR;
572 buf_state &= ~BM_DIRTY;
582 buf_state |= set_flag_bits;
606 int bufid = -buffer - 1;
630 if (check_unreferenced &&
632 elog(
ERROR,
"block %u of %s is still referenced (local %d)",
643 elog(
ERROR,
"local buffer hash table corrupted");
646 buf_state &= ~BUF_FLAG_MASK;
647 buf_state &= ~BUF_USAGECOUNT_MASK;
680 for (
j = 0;
j < nforks;
j++)
742 (
errcode(ERRCODE_INVALID_TRANSACTION_STATE),
743 errmsg(
"cannot access temporary tables during a parallel operation")));
751 (
errcode(ERRCODE_OUT_OF_MEMORY),
752 errmsg(
"out of memory")));
757 for (
i = 0;
i < nbufs;
i++)
767 buf->buf_id = -
i - 2;
789 elog(
ERROR,
"could not initialize local buffer hash table");
807 int bufid = -buffer - 1;
815 if (adjust_usagecount &&
848 int buffid = -buffer - 1;
883 GUC_check_errdetail(
"\"temp_buffers\" cannot be changed after any temporary tables have been accessed in the session.");
901 static char *cur_block = NULL;
902 static int next_buf_in_block = 0;
903 static int num_bufs_in_block = 0;
904 static int total_bufs_allocated = 0;
911 if (next_buf_in_block >= num_bufs_in_block)
921 if (LocalBufferContext == NULL)
924 "LocalBufferContext",
928 num_bufs =
Max(num_bufs_in_block * 2, 16);
940 next_buf_in_block = 0;
941 num_bufs_in_block = num_bufs;
945 this_buf = cur_block + next_buf_in_block * BLCKSZ;
947 total_bufs_allocated++;
959 return (
Block) this_buf;
970#ifdef USE_ASSERT_CHECKING
973 int RefCountErrors = 0;
984 elog(
WARNING,
"local buffer refcount leak: %s", s);
990 Assert(RefCountErrors == 0);
bool pgaio_wref_valid(PgAioWaitRef *iow)
void pgaio_wref_clear(PgAioWaitRef *iow)
void pgaio_wref_wait(PgAioWaitRef *iow)
static void pg_atomic_unlocked_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
#define InvalidBlockNumber
#define BufferIsLocal(buffer)
#define BM_MAX_USAGE_COUNT
static void InitBufferTag(BufferTag *tag, const RelFileLocator *rlocator, ForkNumber forkNum, BlockNumber blockNum)
#define BUF_USAGECOUNT_MASK
static ForkNumber BufTagGetForkNum(const BufferTag *tag)
static bool BufferTagsEqual(const BufferTag *tag1, const BufferTag *tag2)
static bool BufTagMatchesRelFileLocator(const BufferTag *tag, const RelFileLocator *rlocator)
#define BUF_STATE_GET_USAGECOUNT(state)
static void ClearBufferTag(BufferTag *tag)
static void ResourceOwnerRememberBuffer(ResourceOwner owner, Buffer buffer)
static void ResourceOwnerForgetBuffer(ResourceOwner owner, Buffer buffer)
#define BUF_USAGECOUNT_ONE
#define BUF_STATE_GET_REFCOUNT(state)
static RelFileLocator BufTagGetRelFileLocator(const BufferTag *tag)
static BufferDesc * GetLocalBufferDescriptor(uint32 id)
static Buffer BufferDescriptorGetBuffer(const BufferDesc *bdesc)
char * DebugPrintBufferRefcount(Buffer buffer)
void PageSetChecksumInplace(Page page, BlockNumber blkno)
#define MemSet(start, val, len)
#define fprintf(file, fmt, msg)
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define GUC_check_errdetail
Assert(PointerIsAligned(start, uint64))
#define IsParallelWorker()
BufferUsage pgBufferUsage
void FlushLocalBuffer(BufferDesc *bufHdr, SMgrRelation reln)
void UnpinLocalBuffer(Buffer buffer)
bool StartLocalBufferIO(BufferDesc *bufHdr, bool forInput, bool nowait)
static HTAB * LocalBufHash
static int NLocalPinnedBuffers
void AtEOXact_LocalBuffers(bool isCommit)
#define LocalBufHdrGetBlock(bufHdr)
static void CheckForLocalBufferLeaks(void)
uint32 GetAdditionalLocalPinLimit(void)
static Block GetLocalBufferStorage(void)
static int nextFreeLocalBufId
bool check_temp_buffers(int *newval, void **extra, GucSource source)
void AtProcExit_LocalBuffers(void)
bool PinLocalBuffer(BufferDesc *buf_hdr, bool adjust_usagecount)
static void InitLocalBuffers(void)
void LimitAdditionalLocalPins(uint32 *additional_pins)
uint32 GetLocalPinLimit(void)
static Buffer GetLocalVictimBuffer(void)
void MarkLocalBufferDirty(Buffer buffer)
void DropRelationAllLocalBuffers(RelFileLocator rlocator)
void TerminateLocalBufferIO(BufferDesc *bufHdr, bool clear_dirty, uint32 set_flag_bits, bool release_aio)
void InvalidateLocalBuffer(BufferDesc *bufHdr, bool check_unreferenced)
PrefetchBufferResult PrefetchLocalBuffer(SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum)
BlockNumber ExtendBufferedRelLocal(BufferManagerRelation bmr, ForkNumber fork, uint32 flags, uint32 extend_by, BlockNumber extend_upto, Buffer *buffers, uint32 *extended_by)
Block * LocalBufferBlockPointers
void UnpinLocalBufferNoOwner(Buffer buffer)
void DropRelationLocalBuffers(RelFileLocator rlocator, ForkNumber *forkNum, int nforks, BlockNumber *firstDelBlock)
BufferDesc * LocalBufferDescriptors
BufferDesc * LocalBufferAlloc(SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum, bool *foundPtr)
void pfree(void *pointer)
void * MemoryContextAllocAligned(MemoryContext context, Size size, Size alignto, int flags)
MemoryContext TopMemoryContext
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
static rewind_source * source
instr_time pgstat_prepare_io_time(bool track_io_guc)
void pgstat_count_io_op(IOObject io_object, IOContext io_context, IOOp io_op, uint32 cnt, uint64 bytes)
void pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op, instr_time start_time, uint32 cnt, uint64 bytes)
#define relpath(rlocator, forknum)
#define relpathbackend(rlocator, backend, forknum)
ResourceOwner CurrentResourceOwner
void ResourceOwnerEnlarge(ResourceOwner owner)
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend)
void smgrzeroextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
bool smgrprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks)
static void smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
RelFileLocatorBackend smgr_rlocator