PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
localbuf.c File Reference
#include "postgres.h"
#include "access/parallel.h"
#include "executor/instrument.h"
#include "pgstat.h"
#include "storage/aio.h"
#include "storage/buf_internals.h"
#include "storage/bufmgr.h"
#include "storage/fd.h"
#include "utils/guc_hooks.h"
#include "utils/memdebug.h"
#include "utils/memutils.h"
#include "utils/resowner.h"
Include dependency graph for localbuf.c:

Go to the source code of this file.

Data Structures

struct  LocalBufferLookupEnt
 

Macros

#define LocalBufHdrGetBlock(bufHdr)    LocalBufferBlockPointers[-((bufHdr)->buf_id + 2)]
 

Functions

static void InitLocalBuffers (void)
 
static Block GetLocalBufferStorage (void)
 
static Buffer GetLocalVictimBuffer (void)
 
PrefetchBufferResult PrefetchLocalBuffer (SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum)
 
BufferDescLocalBufferAlloc (SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum, bool *foundPtr)
 
void FlushLocalBuffer (BufferDesc *bufHdr, SMgrRelation reln)
 
uint32 GetLocalPinLimit (void)
 
uint32 GetAdditionalLocalPinLimit (void)
 
void LimitAdditionalLocalPins (uint32 *additional_pins)
 
BlockNumber ExtendBufferedRelLocal (BufferManagerRelation bmr, ForkNumber fork, uint32 flags, uint32 extend_by, BlockNumber extend_upto, Buffer *buffers, uint32 *extended_by)
 
void MarkLocalBufferDirty (Buffer buffer)
 
bool StartLocalBufferIO (BufferDesc *bufHdr, bool forInput, bool nowait)
 
void TerminateLocalBufferIO (BufferDesc *bufHdr, bool clear_dirty, uint32 set_flag_bits, bool release_aio)
 
void InvalidateLocalBuffer (BufferDesc *bufHdr, bool check_unreferenced)
 
void DropRelationLocalBuffers (RelFileLocator rlocator, ForkNumber forkNum, BlockNumber firstDelBlock)
 
void DropRelationAllLocalBuffers (RelFileLocator rlocator)
 
bool PinLocalBuffer (BufferDesc *buf_hdr, bool adjust_usagecount)
 
void UnpinLocalBuffer (Buffer buffer)
 
void UnpinLocalBufferNoOwner (Buffer buffer)
 
bool check_temp_buffers (int *newval, void **extra, GucSource source)
 
static void CheckForLocalBufferLeaks (void)
 
void AtEOXact_LocalBuffers (bool isCommit)
 
void AtProcExit_LocalBuffers (void)
 

Variables

int NLocBuffer = 0
 
BufferDescLocalBufferDescriptors = NULL
 
BlockLocalBufferBlockPointers = NULL
 
int32LocalRefCount = NULL
 
static int nextFreeLocalBufId = 0
 
static HTABLocalBufHash = NULL
 
static int NLocalPinnedBuffers = 0
 

Macro Definition Documentation

◆ LocalBufHdrGetBlock

#define LocalBufHdrGetBlock (   bufHdr)     LocalBufferBlockPointers[-((bufHdr)->buf_id + 2)]

Definition at line 41 of file localbuf.c.

Function Documentation

◆ AtEOXact_LocalBuffers()

void AtEOXact_LocalBuffers ( bool  isCommit)

Definition at line 993 of file localbuf.c.

994{
996}
static void CheckForLocalBufferLeaks(void)
Definition: localbuf.c:960

References CheckForLocalBufferLeaks().

Referenced by AtEOXact_Buffers().

◆ AtProcExit_LocalBuffers()

void AtProcExit_LocalBuffers ( void  )

Definition at line 1004 of file localbuf.c.

1005{
1006 /*
1007 * We shouldn't be holding any remaining pins; if we are, and assertions
1008 * aren't enabled, we'll fail later in DropRelationBuffers while trying to
1009 * drop the temp rels.
1010 */
1012}

References CheckForLocalBufferLeaks().

Referenced by AtProcExit_Buffers().

◆ check_temp_buffers()

bool check_temp_buffers ( int *  newval,
void **  extra,
GucSource  source 
)

Definition at line 868 of file localbuf.c.

869{
870 /*
871 * Once local buffers have been initialized, it's too late to change this.
872 * However, if this is only a test call, allow it.
873 */
875 {
876 GUC_check_errdetail("\"temp_buffers\" cannot be changed after any temporary tables have been accessed in the session.");
877 return false;
878 }
879 return true;
880}
#define newval
#define GUC_check_errdetail
Definition: guc.h:481
@ PGC_S_TEST
Definition: guc.h:125
int NLocBuffer
Definition: localbuf.c:44
static rewind_source * source
Definition: pg_rewind.c:89

References GUC_check_errdetail, newval, NLocBuffer, PGC_S_TEST, and source.

◆ CheckForLocalBufferLeaks()

static void CheckForLocalBufferLeaks ( void  )
static

Definition at line 960 of file localbuf.c.

961{
962#ifdef USE_ASSERT_CHECKING
963 if (LocalRefCount)
964 {
965 int RefCountErrors = 0;
966 int i;
967
968 for (i = 0; i < NLocBuffer; i++)
969 {
970 if (LocalRefCount[i] != 0)
971 {
972 Buffer b = -i - 1;
973 char *s;
974
976 elog(WARNING, "local buffer refcount leak: %s", s);
977 pfree(s);
978
979 RefCountErrors++;
980 }
981 }
982 Assert(RefCountErrors == 0);
983 }
984#endif
985}
int Buffer
Definition: buf.h:23
char * DebugPrintBufferRefcount(Buffer buffer)
Definition: bufmgr.c:4174
#define WARNING
Definition: elog.h:36
#define elog(elevel,...)
Definition: elog.h:226
Assert(PointerIsAligned(start, uint64))
int b
Definition: isn.c:74
int i
Definition: isn.c:77
int32 * LocalRefCount
Definition: localbuf.c:48
void pfree(void *pointer)
Definition: mcxt.c:2146

References Assert(), b, DebugPrintBufferRefcount(), elog, i, LocalRefCount, NLocBuffer, pfree(), and WARNING.

Referenced by AtEOXact_LocalBuffers(), and AtProcExit_LocalBuffers().

◆ DropRelationAllLocalBuffers()

void DropRelationAllLocalBuffers ( RelFileLocator  rlocator)

Definition at line 693 of file localbuf.c.

694{
695 int i;
696
697 for (i = 0; i < NLocBuffer; i++)
698 {
700 uint32 buf_state;
701
702 buf_state = pg_atomic_read_u32(&bufHdr->state);
703
704 if ((buf_state & BM_TAG_VALID) &&
705 BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator))
706 {
707 InvalidateLocalBuffer(bufHdr, true);
708 }
709 }
710}
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:239
#define BM_TAG_VALID
Definition: buf_internals.h:71
static bool BufTagMatchesRelFileLocator(const BufferTag *tag, const RelFileLocator *rlocator)
static BufferDesc * GetLocalBufferDescriptor(uint32 id)
uint32_t uint32
Definition: c.h:502
void InvalidateLocalBuffer(BufferDesc *bufHdr, bool check_unreferenced)
Definition: localbuf.c:603
BufferTag tag
pg_atomic_uint32 state

References BM_TAG_VALID, BufTagMatchesRelFileLocator(), GetLocalBufferDescriptor(), i, InvalidateLocalBuffer(), NLocBuffer, pg_atomic_read_u32(), BufferDesc::state, and BufferDesc::tag.

Referenced by DropRelationsAllBuffers().

◆ DropRelationLocalBuffers()

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

Definition at line 663 of file localbuf.c.

665{
666 int i;
667
668 for (i = 0; i < NLocBuffer; i++)
669 {
671 uint32 buf_state;
672
673 buf_state = pg_atomic_read_u32(&bufHdr->state);
674
675 if ((buf_state & BM_TAG_VALID) &&
676 BufTagMatchesRelFileLocator(&bufHdr->tag, &rlocator) &&
677 BufTagGetForkNum(&bufHdr->tag) == forkNum &&
678 bufHdr->tag.blockNum >= firstDelBlock)
679 {
680 InvalidateLocalBuffer(bufHdr, true);
681 }
682 }
683}
static ForkNumber BufTagGetForkNum(const BufferTag *tag)
BlockNumber blockNum

References buftag::blockNum, BM_TAG_VALID, BufTagGetForkNum(), BufTagMatchesRelFileLocator(), GetLocalBufferDescriptor(), i, InvalidateLocalBuffer(), NLocBuffer, pg_atomic_read_u32(), 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 345 of file localbuf.c.

352{
353 BlockNumber first_block;
354 instr_time io_start;
355
356 /* Initialize local buffers if first request in this session */
357 if (LocalBufHash == NULL)
359
360 LimitAdditionalLocalPins(&extend_by);
361
362 for (uint32 i = 0; i < extend_by; i++)
363 {
364 BufferDesc *buf_hdr;
365 Block buf_block;
366
367 buffers[i] = GetLocalVictimBuffer();
368 buf_hdr = GetLocalBufferDescriptor(-buffers[i] - 1);
369 buf_block = LocalBufHdrGetBlock(buf_hdr);
370
371 /* new buffers are zero-filled */
372 MemSet(buf_block, 0, BLCKSZ);
373 }
374
375 first_block = smgrnblocks(bmr.smgr, fork);
376
377 if (extend_upto != InvalidBlockNumber)
378 {
379 /*
380 * In contrast to shared relations, nothing could change the relation
381 * size concurrently. Thus we shouldn't end up finding that we don't
382 * need to do anything.
383 */
384 Assert(first_block <= extend_upto);
385
386 Assert((uint64) first_block + extend_by <= extend_upto);
387 }
388
389 /* Fail if relation is already at maximum possible length */
390 if ((uint64) first_block + extend_by >= MaxBlockNumber)
392 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
393 errmsg("cannot extend relation %s beyond %u blocks",
394 relpath(bmr.smgr->smgr_rlocator, fork).str,
396
397 for (uint32 i = 0; i < extend_by; i++)
398 {
399 int victim_buf_id;
400 BufferDesc *victim_buf_hdr;
401 BufferTag tag;
402 LocalBufferLookupEnt *hresult;
403 bool found;
404
405 victim_buf_id = -buffers[i] - 1;
406 victim_buf_hdr = GetLocalBufferDescriptor(victim_buf_id);
407
408 /* in case we need to pin an existing buffer below */
410
411 InitBufferTag(&tag, &bmr.smgr->smgr_rlocator.locator, fork, first_block + i);
412
413 hresult = (LocalBufferLookupEnt *)
414 hash_search(LocalBufHash, &tag, HASH_ENTER, &found);
415 if (found)
416 {
417 BufferDesc *existing_hdr;
418 uint32 buf_state;
419
421
422 existing_hdr = GetLocalBufferDescriptor(hresult->id);
423 PinLocalBuffer(existing_hdr, false);
424 buffers[i] = BufferDescriptorGetBuffer(existing_hdr);
425
426 /*
427 * Clear the BM_VALID bit, do StartLocalBufferIO() and proceed.
428 */
429 buf_state = pg_atomic_read_u32(&existing_hdr->state);
430 Assert(buf_state & BM_TAG_VALID);
431 Assert(!(buf_state & BM_DIRTY));
432 buf_state &= ~BM_VALID;
433 pg_atomic_unlocked_write_u32(&existing_hdr->state, buf_state);
434
435 /* no need to loop for local buffers */
436 StartLocalBufferIO(existing_hdr, true, false);
437 }
438 else
439 {
440 uint32 buf_state = pg_atomic_read_u32(&victim_buf_hdr->state);
441
442 Assert(!(buf_state & (BM_VALID | BM_TAG_VALID | BM_DIRTY | BM_JUST_DIRTIED)));
443
444 victim_buf_hdr->tag = tag;
445
446 buf_state |= BM_TAG_VALID | BUF_USAGECOUNT_ONE;
447
448 pg_atomic_unlocked_write_u32(&victim_buf_hdr->state, buf_state);
449
450 hresult->id = victim_buf_id;
451
452 StartLocalBufferIO(victim_buf_hdr, true, false);
453 }
454 }
455
457
458 /* actually extend relation */
459 smgrzeroextend(bmr.smgr, fork, first_block, extend_by, false);
460
462 io_start, 1, extend_by * BLCKSZ);
463
464 for (uint32 i = 0; i < extend_by; i++)
465 {
466 Buffer buf = buffers[i];
467 BufferDesc *buf_hdr;
468 uint32 buf_state;
469
470 buf_hdr = GetLocalBufferDescriptor(-buf - 1);
471
472 buf_state = pg_atomic_read_u32(&buf_hdr->state);
473 buf_state |= BM_VALID;
474 pg_atomic_unlocked_write_u32(&buf_hdr->state, buf_state);
475 }
476
477 *extended_by = extend_by;
478
480
481 return first_block;
482}
static void pg_atomic_unlocked_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:295
uint32 BlockNumber
Definition: block.h:31
#define InvalidBlockNumber
Definition: block.h:33
#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:69
#define BM_JUST_DIRTIED
Definition: buf_internals.h:74
#define BUF_USAGECOUNT_ONE
Definition: buf_internals.h:54
#define BM_VALID
Definition: buf_internals.h:70
static Buffer BufferDescriptorGetBuffer(const BufferDesc *bdesc)
bool track_io_timing
Definition: bufmgr.c:147
void * Block
Definition: bufmgr.h:26
uint64_t uint64
Definition: c.h:503
#define MemSet(start, val, len)
Definition: c.h:991
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
@ HASH_ENTER
Definition: hsearch.h:114
BufferUsage pgBufferUsage
Definition: instrument.c:20
void UnpinLocalBuffer(Buffer buffer)
Definition: localbuf.c:832
bool StartLocalBufferIO(BufferDesc *bufHdr, bool forInput, bool nowait)
Definition: localbuf.c:521
static HTAB * LocalBufHash
Definition: localbuf.c:52
#define LocalBufHdrGetBlock(bufHdr)
Definition: localbuf.c:41
bool PinLocalBuffer(BufferDesc *buf_hdr, bool adjust_usagecount)
Definition: localbuf.c:796
static void InitLocalBuffers(void)
Definition: localbuf.c:719
void LimitAdditionalLocalPins(uint32 *additional_pins)
Definition: localbuf.c:322
static Buffer GetLocalVictimBuffer(void)
Definition: localbuf.c:223
static char * buf
Definition: pg_test_fsync.c:72
@ IOOBJECT_TEMP_RELATION
Definition: pgstat.h:275
@ IOCONTEXT_NORMAL
Definition: pgstat.h:286
@ IOOP_EXTEND
Definition: pgstat.h:311
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:121
#define relpath(rlocator, forknum)
Definition: relpath.h:150
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
void ResourceOwnerEnlarge(ResourceOwner owner)
Definition: resowner.c:452
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:819
void smgrzeroextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition: smgr.c:649
struct SMgrRelationData * smgr
Definition: bufmgr.h:104
int64 local_blks_written
Definition: instrument.h:33
RelFileLocator locator
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:38

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(), StartLocalBufferIO(), BufferDesc::state, BufferDesc::tag, track_io_timing, and UnpinLocalBuffer().

Referenced by ExtendBufferedRelCommon().

◆ FlushLocalBuffer()

void FlushLocalBuffer ( BufferDesc bufHdr,
SMgrRelation  reln 
)

Definition at line 182 of file localbuf.c.

183{
184 instr_time io_start;
185 Page localpage = (char *) LocalBufHdrGetBlock(bufHdr);
186
188
189 /*
190 * Try to start an I/O operation. There currently are no reasons for
191 * StartLocalBufferIO to return false, so we raise an error in that case.
192 */
193 if (!StartLocalBufferIO(bufHdr, false, false))
194 elog(ERROR, "failed to start write IO on local buffer");
195
196 /* Find smgr relation for buffer */
197 if (reln == NULL)
198 reln = smgropen(BufTagGetRelFileLocator(&bufHdr->tag),
200
201 PageSetChecksumInplace(localpage, bufHdr->tag.blockNum);
202
204
205 /* And write... */
206 smgrwrite(reln,
207 BufTagGetForkNum(&bufHdr->tag),
208 bufHdr->tag.blockNum,
209 localpage,
210 false);
211
212 /* Temporary table I/O does not use Buffer Access Strategies */
214 IOOP_WRITE, io_start, 1, BLCKSZ);
215
216 /* Mark not-dirty */
217 TerminateLocalBufferIO(bufHdr, true, 0, false);
218
220}
static RelFileLocator BufTagGetRelFileLocator(const BufferTag *tag)
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1541
PageData * Page
Definition: bufpage.h:82
ProcNumber MyProcNumber
Definition: globals.c:91
void TerminateLocalBufferIO(BufferDesc *bufHdr, bool clear_dirty, uint32 set_flag_bits, bool release_aio)
Definition: localbuf.c:560
@ IOOP_WRITE
Definition: pgstat.h:313
SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend)
Definition: smgr.c:240
static void smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: smgr.h:131

References Assert(), buftag::blockNum, BufferDescriptorGetBuffer(), BufTagGetForkNum(), BufTagGetRelFileLocator(), elog, ERROR, IOCONTEXT_NORMAL, IOOBJECT_TEMP_RELATION, IOOP_WRITE, BufferUsage::local_blks_written, LocalBufHdrGetBlock, LocalRefCount, MyProcNumber, PageSetChecksumInplace(), pgBufferUsage, pgstat_count_io_op_time(), pgstat_prepare_io_time(), smgropen(), smgrwrite(), StartLocalBufferIO(), BufferDesc::tag, TerminateLocalBufferIO(), and track_io_timing.

Referenced by FlushRelationBuffers(), GetLocalVictimBuffer(), and invalidate_rel_block().

◆ GetAdditionalLocalPinLimit()

uint32 GetAdditionalLocalPinLimit ( void  )

Definition at line 314 of file localbuf.c.

315{
318}
int num_temp_buffers
Definition: guc_tables.c:552
static int NLocalPinnedBuffers
Definition: localbuf.c:55

References Assert(), NLocalPinnedBuffers, and num_temp_buffers.

Referenced by read_stream_start_pending_read().

◆ GetLocalBufferStorage()

static Block GetLocalBufferStorage ( void  )
static

Definition at line 892 of file localbuf.c.

893{
894 static char *cur_block = NULL;
895 static int next_buf_in_block = 0;
896 static int num_bufs_in_block = 0;
897 static int total_bufs_allocated = 0;
898 static MemoryContext LocalBufferContext = NULL;
899
900 char *this_buf;
901
902 Assert(total_bufs_allocated < NLocBuffer);
903
904 if (next_buf_in_block >= num_bufs_in_block)
905 {
906 /* Need to make a new request to memmgr */
907 int num_bufs;
908
909 /*
910 * We allocate local buffers in a context of their own, so that the
911 * space eaten for them is easily recognizable in MemoryContextStats
912 * output. Create the context on first use.
913 */
914 if (LocalBufferContext == NULL)
915 LocalBufferContext =
917 "LocalBufferContext",
919
920 /* Start with a 16-buffer request; subsequent ones double each time */
921 num_bufs = Max(num_bufs_in_block * 2, 16);
922 /* But not more than what we need for all remaining local bufs */
923 num_bufs = Min(num_bufs, NLocBuffer - total_bufs_allocated);
924 /* And don't overflow MaxAllocSize, either */
925 num_bufs = Min(num_bufs, MaxAllocSize / BLCKSZ);
926
927 /* Buffers should be I/O aligned. */
928 cur_block = (char *)
930 MemoryContextAlloc(LocalBufferContext,
931 num_bufs * BLCKSZ + PG_IO_ALIGN_SIZE));
932 next_buf_in_block = 0;
933 num_bufs_in_block = num_bufs;
934 }
935
936 /* Allocate next buffer in current memory block */
937 this_buf = cur_block + next_buf_in_block * BLCKSZ;
938 next_buf_in_block++;
939 total_bufs_allocated++;
940
941 /*
942 * Caller's PinLocalBuffer() was too early for Valgrind updates, so do it
943 * here. The block is actually undefined, but we want consistency with
944 * the regular case of not needing to allocate memory. This is
945 * specifically needed when method_io_uring.c fills the block, because
946 * Valgrind doesn't recognize io_uring reads causing undefined memory to
947 * become defined.
948 */
949 VALGRIND_MAKE_MEM_DEFINED(this_buf, BLCKSZ);
950
951 return (Block) this_buf;
952}
#define Min(x, y)
Definition: c.h:975
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:775
#define Max(x, y)
Definition: c.h:969
#define MaxAllocSize
Definition: fe_memutils.h:22
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1256
MemoryContext TopMemoryContext
Definition: mcxt.c:165
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
Definition: memdebug.h:26
#define AllocSetContextCreate
Definition: memutils.h:149
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:180
#define PG_IO_ALIGN_SIZE

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), Max, MaxAllocSize, MemoryContextAlloc(), Min, NLocBuffer, PG_IO_ALIGN_SIZE, TopMemoryContext, TYPEALIGN, and VALGRIND_MAKE_MEM_DEFINED.

Referenced by GetLocalVictimBuffer().

◆ GetLocalPinLimit()

uint32 GetLocalPinLimit ( void  )

Definition at line 306 of file localbuf.c.

307{
308 /* Every backend has its own temporary buffers, and can pin them all. */
309 return num_temp_buffers;
310}

References num_temp_buffers.

Referenced by read_stream_begin_impl().

◆ GetLocalVictimBuffer()

static Buffer GetLocalVictimBuffer ( void  )
static

Definition at line 223 of file localbuf.c.

224{
225 int victim_bufid;
226 int trycounter;
227 BufferDesc *bufHdr;
228
230
231 /*
232 * Need to get a new buffer. We use a clock sweep algorithm (essentially
233 * the same as what freelist.c does now...)
234 */
235 trycounter = NLocBuffer;
236 for (;;)
237 {
238 victim_bufid = nextFreeLocalBufId;
239
242
243 bufHdr = GetLocalBufferDescriptor(victim_bufid);
244
245 if (LocalRefCount[victim_bufid] == 0)
246 {
247 uint32 buf_state = pg_atomic_read_u32(&bufHdr->state);
248
249 if (BUF_STATE_GET_USAGECOUNT(buf_state) > 0)
250 {
251 buf_state -= BUF_USAGECOUNT_ONE;
252 pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
253 trycounter = NLocBuffer;
254 }
255 else if (BUF_STATE_GET_REFCOUNT(buf_state) > 0)
256 {
257 /*
258 * This can be reached if the backend initiated AIO for this
259 * buffer and then errored out.
260 */
261 }
262 else
263 {
264 /* Found a usable buffer */
265 PinLocalBuffer(bufHdr, false);
266 break;
267 }
268 }
269 else if (--trycounter == 0)
271 (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
272 errmsg("no empty local buffer available")));
273 }
274
275 /*
276 * lazy memory allocation: allocate space on first use of a buffer.
277 */
278 if (LocalBufHdrGetBlock(bufHdr) == NULL)
279 {
280 /* Set pointer for use by BufferGetBlock() macro */
282 }
283
284 /*
285 * this buffer is not referenced but it might still be dirty. if that's
286 * the case, write it out before reusing it!
287 */
288 if (pg_atomic_read_u32(&bufHdr->state) & BM_DIRTY)
289 FlushLocalBuffer(bufHdr, NULL);
290
291 /*
292 * Remove the victim buffer from the hashtable and mark as invalid.
293 */
294 if (pg_atomic_read_u32(&bufHdr->state) & BM_TAG_VALID)
295 {
296 InvalidateLocalBuffer(bufHdr, false);
297
299 }
300
301 return BufferDescriptorGetBuffer(bufHdr);
302}
#define BUF_STATE_GET_USAGECOUNT(state)
Definition: buf_internals.h:60
#define BUF_STATE_GET_REFCOUNT(state)
Definition: buf_internals.h:59
void FlushLocalBuffer(BufferDesc *bufHdr, SMgrRelation reln)
Definition: localbuf.c:182
static Block GetLocalBufferStorage(void)
Definition: localbuf.c:892
static int nextFreeLocalBufId
Definition: localbuf.c:50
@ IOOP_EVICT
Definition: pgstat.h:304
void pgstat_count_io_op(IOObject io_object, IOContext io_context, IOOp io_op, uint32 cnt, uint64 bytes)
Definition: pgstat_io.c:68

References BM_DIRTY, BM_TAG_VALID, BUF_STATE_GET_REFCOUNT, BUF_STATE_GET_USAGECOUNT, BUF_USAGECOUNT_ONE, BufferDescriptorGetBuffer(), CurrentResourceOwner, ereport, errcode(), errmsg(), ERROR, FlushLocalBuffer(), GetLocalBufferDescriptor(), GetLocalBufferStorage(), InvalidateLocalBuffer(), IOCONTEXT_NORMAL, IOOBJECT_TEMP_RELATION, IOOP_EVICT, LocalBufHdrGetBlock, LocalRefCount, nextFreeLocalBufId, NLocBuffer, pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), pgstat_count_io_op(), PinLocalBuffer(), ResourceOwnerEnlarge(), and BufferDesc::state.

Referenced by ExtendBufferedRelLocal(), and LocalBufferAlloc().

◆ InitLocalBuffers()

static void InitLocalBuffers ( void  )
static

Definition at line 719 of file localbuf.c.

720{
721 int nbufs = num_temp_buffers;
722 HASHCTL info;
723 int i;
724
725 /*
726 * Parallel workers can't access data in temporary tables, because they
727 * have no visibility into the local buffers of their leader. This is a
728 * convenient, low-cost place to provide a backstop check for that. Note
729 * that we don't wish to prevent a parallel worker from accessing catalog
730 * metadata about a temp table, so checks at higher levels would be
731 * inappropriate.
732 */
733 if (IsParallelWorker())
735 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
736 errmsg("cannot access temporary tables during a parallel operation")));
737
738 /* Allocate and zero buffer headers and auxiliary arrays */
740 LocalBufferBlockPointers = (Block *) calloc(nbufs, sizeof(Block));
741 LocalRefCount = (int32 *) calloc(nbufs, sizeof(int32));
744 (errcode(ERRCODE_OUT_OF_MEMORY),
745 errmsg("out of memory")));
746
748
749 /* initialize fields that need to start off nonzero */
750 for (i = 0; i < nbufs; i++)
751 {
753
754 /*
755 * negative to indicate local buffer. This is tricky: shared buffers
756 * start with 0. We have to start with -2. (Note that the routine
757 * BufferDescriptorGetBuffer adds 1 to buf_id so our first buffer id
758 * is -1.)
759 */
760 buf->buf_id = -i - 2;
761
762 pgaio_wref_clear(&buf->io_wref);
763
764 /*
765 * Intentionally do not initialize the buffer's atomic variable
766 * (besides zeroing the underlying memory above). That way we get
767 * errors on platforms without atomics, if somebody (re-)introduces
768 * atomic operations for local buffers.
769 */
770 }
771
772 /* Create the lookup hash table */
773 info.keysize = sizeof(BufferTag);
774 info.entrysize = sizeof(LocalBufferLookupEnt);
775
776 LocalBufHash = hash_create("Local Buffer Lookup Table",
777 nbufs,
778 &info,
780
781 if (!LocalBufHash)
782 elog(ERROR, "could not initialize local buffer hash table");
783
784 /* Initialization done, mark buffers allocated */
785 NLocBuffer = nbufs;
786}
void pgaio_wref_clear(PgAioWaitRef *iow)
Definition: aio.c:866
struct buftag BufferTag
int32_t int32
Definition: c.h:498
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
#define FATAL
Definition: elog.h:41
#define calloc(a, b)
Definition: header.h:55
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
#define IsParallelWorker()
Definition: parallel.h:60
Block * LocalBufferBlockPointers
Definition: localbuf.c:47
BufferDesc * LocalBufferDescriptors
Definition: localbuf.c:46
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76

References buf, calloc, elog, HASHCTL::entrysize, ereport, errcode(), errmsg(), ERROR, FATAL, GetLocalBufferDescriptor(), HASH_BLOBS, hash_create(), HASH_ELEM, i, IsParallelWorker, HASHCTL::keysize, LocalBufferBlockPointers, LocalBufferDescriptors, LocalBufHash, LocalRefCount, nextFreeLocalBufId, NLocBuffer, num_temp_buffers, and pgaio_wref_clear().

Referenced by ExtendBufferedRelLocal(), LocalBufferAlloc(), and PrefetchLocalBuffer().

◆ InvalidateLocalBuffer()

void InvalidateLocalBuffer ( BufferDesc bufHdr,
bool  check_unreferenced 
)

Definition at line 603 of file localbuf.c.

604{
605 Buffer buffer = BufferDescriptorGetBuffer(bufHdr);
606 int bufid = -buffer - 1;
607 uint32 buf_state;
608 LocalBufferLookupEnt *hresult;
609
610 /*
611 * It's possible that we started IO on this buffer before e.g. aborting
612 * the transaction that created a table. We need to wait for that IO to
613 * complete before removing / reusing the buffer.
614 */
615 if (pgaio_wref_valid(&bufHdr->io_wref))
616 {
617 PgAioWaitRef iow = bufHdr->io_wref;
618
619 pgaio_wref_wait(&iow);
620 Assert(!pgaio_wref_valid(&bufHdr->io_wref));
621 }
622
623 buf_state = pg_atomic_read_u32(&bufHdr->state);
624
625 /*
626 * We need to test not just LocalRefCount[bufid] but also the BufferDesc
627 * itself, as the latter is used to represent a pin by the AIO subsystem.
628 * This can happen if AIO is initiated and then the query errors out.
629 */
630 if (check_unreferenced &&
631 (LocalRefCount[bufid] != 0 || BUF_STATE_GET_REFCOUNT(buf_state) != 0))
632 elog(ERROR, "block %u of %s is still referenced (local %u)",
633 bufHdr->tag.blockNum,
636 BufTagGetForkNum(&bufHdr->tag)).str,
637 LocalRefCount[bufid]);
638
639 /* Remove entry from hashtable */
640 hresult = (LocalBufferLookupEnt *)
641 hash_search(LocalBufHash, &bufHdr->tag, HASH_REMOVE, NULL);
642 if (!hresult) /* shouldn't happen */
643 elog(ERROR, "local buffer hash table corrupted");
644 /* Mark buffer invalid */
645 ClearBufferTag(&bufHdr->tag);
646 buf_state &= ~BUF_FLAG_MASK;
647 buf_state &= ~BUF_USAGECOUNT_MASK;
648 pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
649}
bool pgaio_wref_valid(PgAioWaitRef *iow)
Definition: aio.c:873
void pgaio_wref_wait(PgAioWaitRef *iow)
Definition: aio.c:893
static void ClearBufferTag(BufferTag *tag)
@ HASH_REMOVE
Definition: hsearch.h:115
#define relpathbackend(rlocator, backend, forknum)
Definition: relpath.h:141
PgAioWaitRef io_wref

References Assert(), buftag::blockNum, BUF_STATE_GET_REFCOUNT, BufferDescriptorGetBuffer(), BufTagGetForkNum(), BufTagGetRelFileLocator(), ClearBufferTag(), elog, ERROR, HASH_REMOVE, hash_search(), BufferDesc::io_wref, LocalBufHash, LocalRefCount, MyProcNumber, pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), pgaio_wref_valid(), pgaio_wref_wait(), relpathbackend, BufferDesc::state, and BufferDesc::tag.

Referenced by DropRelationAllLocalBuffers(), DropRelationLocalBuffers(), GetLocalVictimBuffer(), invalidate_rel_block(), and modify_rel_block().

◆ LimitAdditionalLocalPins()

void LimitAdditionalLocalPins ( uint32 additional_pins)

Definition at line 322 of file localbuf.c.

323{
324 uint32 max_pins;
325
326 if (*additional_pins <= 1)
327 return;
328
329 /*
330 * In contrast to LimitAdditionalPins() other backends don't play a role
331 * here. We can allow up to NLocBuffer pins in total, but it might not be
332 * initialized yet so read num_temp_buffers.
333 */
335
336 if (*additional_pins >= max_pins)
337 *additional_pins = max_pins;
338}

References NLocalPinnedBuffers, and num_temp_buffers.

Referenced by ExtendBufferedRelLocal().

◆ LocalBufferAlloc()

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

Definition at line 118 of file localbuf.c.

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

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

◆ MarkLocalBufferDirty()

void MarkLocalBufferDirty ( Buffer  buffer)

Definition at line 489 of file localbuf.c.

490{
491 int bufid;
492 BufferDesc *bufHdr;
493 uint32 buf_state;
494
495 Assert(BufferIsLocal(buffer));
496
497#ifdef LBDEBUG
498 fprintf(stderr, "LB DIRTY %d\n", buffer);
499#endif
500
501 bufid = -buffer - 1;
502
503 Assert(LocalRefCount[bufid] > 0);
504
505 bufHdr = GetLocalBufferDescriptor(bufid);
506
507 buf_state = pg_atomic_read_u32(&bufHdr->state);
508
509 if (!(buf_state & BM_DIRTY))
511
512 buf_state |= BM_DIRTY;
513
514 pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
515}
#define BufferIsLocal(buffer)
Definition: buf.h:37
#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 796 of file localbuf.c.

797{
798 uint32 buf_state;
799 Buffer buffer = BufferDescriptorGetBuffer(buf_hdr);
800 int bufid = -buffer - 1;
801
802 buf_state = pg_atomic_read_u32(&buf_hdr->state);
803
804 if (LocalRefCount[bufid] == 0)
805 {
807 buf_state += BUF_REFCOUNT_ONE;
808 if (adjust_usagecount &&
810 {
811 buf_state += BUF_USAGECOUNT_ONE;
812 }
813 pg_atomic_unlocked_write_u32(&buf_hdr->state, buf_state);
814
815 /*
816 * See comment in PinBuffer().
817 *
818 * If the buffer isn't allocated yet, it'll be marked as defined in
819 * GetLocalBufferStorage().
820 */
821 if (LocalBufHdrGetBlock(buf_hdr) != NULL)
823 }
824 LocalRefCount[bufid]++;
827
828 return buf_state & BM_VALID;
829}
#define BM_MAX_USAGE_COUNT
Definition: buf_internals.h:86
#define BUF_REFCOUNT_ONE
Definition: buf_internals.h:51
static void ResourceOwnerRememberBuffer(ResourceOwner owner, Buffer buffer)

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

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

◆ PrefetchLocalBuffer()

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

Definition at line 71 of file localbuf.c.

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

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

◆ StartLocalBufferIO()

bool StartLocalBufferIO ( BufferDesc bufHdr,
bool  forInput,
bool  nowait 
)

Definition at line 521 of file localbuf.c.

522{
523 uint32 buf_state;
524
525 /*
526 * With AIO the buffer could have IO in progress, e.g. when there are two
527 * scans of the same relation. Either wait for the other IO or return
528 * false.
529 */
530 if (pgaio_wref_valid(&bufHdr->io_wref))
531 {
532 PgAioWaitRef iow = bufHdr->io_wref;
533
534 if (nowait)
535 return false;
536
537 pgaio_wref_wait(&iow);
538 }
539
540 /* Once we get here, there is definitely no I/O active on this buffer */
541
542 /* Check if someone else already did the I/O */
543 buf_state = pg_atomic_read_u32(&bufHdr->state);
544 if (forInput ? (buf_state & BM_VALID) : !(buf_state & BM_DIRTY))
545 {
546 return false;
547 }
548
549 /* BM_IO_IN_PROGRESS isn't currently used for local buffers */
550
551 /* local buffers don't track IO using resowners */
552
553 return true;
554}

References BM_DIRTY, BM_VALID, BufferDesc::io_wref, pg_atomic_read_u32(), pgaio_wref_valid(), pgaio_wref_wait(), and BufferDesc::state.

Referenced by buffer_call_start_io(), ExtendBufferedRelLocal(), FlushLocalBuffer(), read_rel_block_ll(), ReadBuffersCanStartIOOnce(), and ZeroAndLockBuffer().

◆ TerminateLocalBufferIO()

void TerminateLocalBufferIO ( BufferDesc bufHdr,
bool  clear_dirty,
uint32  set_flag_bits,
bool  release_aio 
)

Definition at line 560 of file localbuf.c.

562{
563 /* Only need to adjust flags */
564 uint32 buf_state = pg_atomic_read_u32(&bufHdr->state);
565
566 /* BM_IO_IN_PROGRESS isn't currently used for local buffers */
567
568 /* Clear earlier errors, if this IO failed, it'll be marked again */
569 buf_state &= ~BM_IO_ERROR;
570
571 if (clear_dirty)
572 buf_state &= ~BM_DIRTY;
573
574 if (release_aio)
575 {
576 /* release pin held by IO subsystem, see also buffer_stage_common() */
577 Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
578 buf_state -= BUF_REFCOUNT_ONE;
579 pgaio_wref_clear(&bufHdr->io_wref);
580 }
581
582 buf_state |= set_flag_bits;
583 pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
584
585 /* local buffers don't track IO using resowners */
586
587 /* local buffers don't use the IO CV, as no other process can see buffer */
588
589 /* local buffers don't use BM_PIN_COUNT_WAITER, so no need to wake */
590}

References Assert(), BUF_REFCOUNT_ONE, BUF_STATE_GET_REFCOUNT, BufferDesc::io_wref, pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), pgaio_wref_clear(), and BufferDesc::state.

Referenced by buffer_call_terminate_io(), buffer_readv_complete_one(), FlushLocalBuffer(), and ZeroAndLockBuffer().

◆ UnpinLocalBuffer()

void UnpinLocalBuffer ( Buffer  buffer)

Definition at line 832 of file localbuf.c.

833{
836}
static void ResourceOwnerForgetBuffer(ResourceOwner owner, Buffer buffer)
void UnpinLocalBufferNoOwner(Buffer buffer)
Definition: localbuf.c:839

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

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

◆ UnpinLocalBufferNoOwner()

void UnpinLocalBufferNoOwner ( Buffer  buffer)

Definition at line 839 of file localbuf.c.

840{
841 int buffid = -buffer - 1;
842
843 Assert(BufferIsLocal(buffer));
844 Assert(LocalRefCount[buffid] > 0);
846
847 if (--LocalRefCount[buffid] == 0)
848 {
849 BufferDesc *buf_hdr = GetLocalBufferDescriptor(buffid);
850 uint32 buf_state;
851
853
854 buf_state = pg_atomic_read_u32(&buf_hdr->state);
855 Assert(BUF_STATE_GET_REFCOUNT(buf_state) > 0);
856 buf_state -= BUF_REFCOUNT_ONE;
857 pg_atomic_unlocked_write_u32(&buf_hdr->state, buf_state);
858
859 /* see comment in UnpinBufferNoOwner */
861 }
862}
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27

References Assert(), BUF_REFCOUNT_ONE, BUF_STATE_GET_REFCOUNT, BufferIsLocal, GetLocalBufferDescriptor(), LocalBufHdrGetBlock, LocalRefCount, NLocalPinnedBuffers, pg_atomic_read_u32(), pg_atomic_unlocked_write_u32(), BufferDesc::state, and VALGRIND_MAKE_MEM_NOACCESS.

Referenced by ResOwnerReleaseBufferPin(), and UnpinLocalBuffer().

Variable Documentation

◆ LocalBufferBlockPointers

Block* LocalBufferBlockPointers = NULL

Definition at line 47 of file localbuf.c.

Referenced by BufferGetBlock(), and InitLocalBuffers().

◆ LocalBufferDescriptors

BufferDesc* LocalBufferDescriptors = NULL

Definition at line 46 of file localbuf.c.

Referenced by GetLocalBufferDescriptor(), and InitLocalBuffers().

◆ LocalBufHash

HTAB* LocalBufHash = NULL
static

◆ LocalRefCount

◆ nextFreeLocalBufId

int nextFreeLocalBufId = 0
static

Definition at line 50 of file localbuf.c.

Referenced by GetLocalVictimBuffer(), and InitLocalBuffers().

◆ NLocalPinnedBuffers

int NLocalPinnedBuffers = 0
static

◆ NLocBuffer