PostgreSQL Source Code  git master
visibilitymap.c File Reference
#include "postgres.h"
#include "access/heapam_xlog.h"
#include "access/visibilitymap.h"
#include "access/xloginsert.h"
#include "access/xlogutils.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "storage/smgr.h"
#include "utils/inval.h"
Include dependency graph for visibilitymap.c:

Go to the source code of this file.

Macros

#define MAPSIZE   (BLCKSZ - MAXALIGN(SizeOfPageHeaderData))
 
#define HEAPBLOCKS_PER_BYTE   (BITS_PER_BYTE / BITS_PER_HEAPBLOCK)
 
#define HEAPBLOCKS_PER_PAGE   (MAPSIZE * HEAPBLOCKS_PER_BYTE)
 
#define HEAPBLK_TO_MAPBLOCK(x)   ((x) / HEAPBLOCKS_PER_PAGE)
 
#define HEAPBLK_TO_MAPBYTE(x)   (((x) % HEAPBLOCKS_PER_PAGE) / HEAPBLOCKS_PER_BYTE)
 
#define HEAPBLK_TO_OFFSET(x)   (((x) % HEAPBLOCKS_PER_BYTE) * BITS_PER_HEAPBLOCK)
 
#define VISIBLE_MASK64
 
#define FROZEN_MASK64
 

Functions

static Buffer vm_readbuf (Relation rel, BlockNumber blkno, bool extend)
 
static void vm_extend (Relation rel, BlockNumber vm_nblocks)
 
bool visibilitymap_clear (Relation rel, BlockNumber heapBlk, Buffer vmbuf, uint8 flags)
 
void visibilitymap_pin (Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
 
bool visibilitymap_pin_ok (BlockNumber heapBlk, Buffer vmbuf)
 
void visibilitymap_set (Relation rel, BlockNumber heapBlk, Buffer heapBuf, XLogRecPtr recptr, Buffer vmBuf, TransactionId cutoff_xid, uint8 flags)
 
uint8 visibilitymap_get_status (Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
 
void visibilitymap_count (Relation rel, BlockNumber *all_visible, BlockNumber *all_frozen)
 
BlockNumber visibilitymap_prepare_truncate (Relation rel, BlockNumber nheapblocks)
 

Macro Definition Documentation

◆ FROZEN_MASK64

#define FROZEN_MASK64
Value:
UINT64CONST(0xaaaaaaaaaaaaaaaa) /* The upper bit of each
* bit pair */

Definition at line 123 of file visibilitymap.c.

◆ HEAPBLK_TO_MAPBLOCK

#define HEAPBLK_TO_MAPBLOCK (   x)    ((x) / HEAPBLOCKS_PER_PAGE)

Definition at line 117 of file visibilitymap.c.

◆ HEAPBLK_TO_MAPBYTE

#define HEAPBLK_TO_MAPBYTE (   x)    (((x) % HEAPBLOCKS_PER_PAGE) / HEAPBLOCKS_PER_BYTE)

Definition at line 118 of file visibilitymap.c.

◆ HEAPBLK_TO_OFFSET

#define HEAPBLK_TO_OFFSET (   x)    (((x) % HEAPBLOCKS_PER_BYTE) * BITS_PER_HEAPBLOCK)

Definition at line 119 of file visibilitymap.c.

◆ HEAPBLOCKS_PER_BYTE

#define HEAPBLOCKS_PER_BYTE   (BITS_PER_BYTE / BITS_PER_HEAPBLOCK)

Definition at line 111 of file visibilitymap.c.

◆ HEAPBLOCKS_PER_PAGE

#define HEAPBLOCKS_PER_PAGE   (MAPSIZE * HEAPBLOCKS_PER_BYTE)

Definition at line 114 of file visibilitymap.c.

◆ MAPSIZE

#define MAPSIZE   (BLCKSZ - MAXALIGN(SizeOfPageHeaderData))

Definition at line 108 of file visibilitymap.c.

◆ VISIBLE_MASK64

#define VISIBLE_MASK64
Value:
UINT64CONST(0x5555555555555555) /* The lower bit of each
* bit pair */

Definition at line 122 of file visibilitymap.c.

Function Documentation

◆ visibilitymap_clear()

bool visibilitymap_clear ( Relation  rel,
BlockNumber  heapBlk,
Buffer  vmbuf,
uint8  flags 
)

Definition at line 138 of file visibilitymap.c.

141 {
142  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
143  int mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
144  int mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
145  uint8 mask = flags << mapOffset;
146  char *map;
147  bool cleared = false;
148 
150 
151 #ifdef TRACE_VISIBILITYMAP
152  elog(DEBUG1, "vm_clear %s %d", RelationGetRelationName(rel), heapBlk);
153 #endif
154 
155  if (!BufferIsValid(vmbuf) || BufferGetBlockNumber(vmbuf) != mapBlock)
156  elog(ERROR, "wrong buffer passed to visibilitymap_clear");
157 
159  map = PageGetContents(BufferGetPage(vmbuf));
160 
161  if (map[mapByte] & mask)
162  {
163  map[mapByte] &= ~mask;
164 
165  MarkBufferDirty(vmbuf);
166  cleared = true;
167  }
168 
170 
uint32 BlockNumber
Definition: block.h:31
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2763
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1583
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4172
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:105
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:280
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:107
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:228
static char * PageGetContents(Page page)
Definition: bufpage.h:254
unsigned char uint8
Definition: c.h:440
#define DEBUG1
Definition: elog.h:26
#define ERROR
Definition: elog.h:35
Assert(fmt[strlen(fmt) - 1] !='\n')
#define RelationGetRelationName(relation)
Definition: rel.h:535
#define HEAPBLK_TO_OFFSET(x)
#define HEAPBLK_TO_MAPBLOCK(x)
#define HEAPBLK_TO_MAPBYTE(x)
#define VISIBILITYMAP_VALID_BITS

References Assert(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage(), BufferIsValid(), DEBUG1, elog(), ERROR, HEAPBLK_TO_MAPBLOCK, HEAPBLK_TO_MAPBYTE, HEAPBLK_TO_OFFSET, LockBuffer(), MarkBufferDirty(), PageGetContents(), RelationGetRelationName, and VISIBILITYMAP_VALID_BITS.

Referenced by heap_delete(), heap_force_common(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_update(), heap_xlog_delete(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_update(), and lazy_scan_heap().

◆ visibilitymap_count()

void visibilitymap_count ( Relation  rel,
BlockNumber all_visible,
BlockNumber all_frozen 
)

Definition at line 382 of file visibilitymap.c.

385 {
386  BlockNumber mapBlock;
387  BlockNumber nvisible = 0;
388  BlockNumber nfrozen = 0;
389 
390  /* all_visible must be specified */
391  Assert(all_visible);
392 
393  for (mapBlock = 0;; mapBlock++)
394  {
395  Buffer mapBuffer;
396  uint64 *map;
397  int i;
398 
399  /*
400  * Read till we fall off the end of the map. We assume that any extra
401  * bytes in the last page are zeroed, so we don't bother excluding
402  * them from the count.
403  */
404  mapBuffer = vm_readbuf(rel, mapBlock, false);
405  if (!BufferIsValid(mapBuffer))
406  break;
407 
408  /*
409  * We choose not to lock the page, since the result is going to be
410  * immediately stale anyway if anyone is concurrently setting or
411  * clearing bits, and we only really need an approximate value.
412  */
413  map = (uint64 *) PageGetContents(BufferGetPage(mapBuffer));
414 
415  StaticAssertStmt(MAPSIZE % sizeof(uint64) == 0,
416  "unsupported MAPSIZE");
417  if (all_frozen == NULL)
418  {
419  for (i = 0; i < MAPSIZE / sizeof(uint64); i++)
420  nvisible += pg_popcount64(map[i] & VISIBLE_MASK64);
421  }
422  else
423  {
424  for (i = 0; i < MAPSIZE / sizeof(uint64); i++)
425  {
426  nvisible += pg_popcount64(map[i] & VISIBLE_MASK64);
427  nfrozen += pg_popcount64(map[i] & FROZEN_MASK64);
428  }
429  }
430 
431  ReleaseBuffer(mapBuffer);
432  }
433 
434  *all_visible = nvisible;
435  if (all_frozen)
int Buffer
Definition: buf.h:23
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3931
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:869
int i
Definition: isn.c:73
int pg_popcount64(uint64 word)
Definition: pg_bitutils.c:284
#define MAPSIZE
#define VISIBLE_MASK64
static Buffer vm_readbuf(Relation rel, BlockNumber blkno, bool extend)
#define FROZEN_MASK64

References Assert(), BufferGetPage(), BufferIsValid(), FROZEN_MASK64, i, MAPSIZE, PageGetContents(), pg_popcount64(), ReleaseBuffer(), StaticAssertStmt, VISIBLE_MASK64, and vm_readbuf().

Referenced by do_analyze_rel(), heap_vacuum_rel(), and index_update_stats().

◆ visibilitymap_get_status()

uint8 visibilitymap_get_status ( Relation  rel,
BlockNumber  heapBlk,
Buffer vmbuf 
)

Definition at line 334 of file visibilitymap.c.

337 {
338  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
339  uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
340  uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
341  char *map;
342  uint8 result;
343 
344 #ifdef TRACE_VISIBILITYMAP
345  elog(DEBUG1, "vm_get_status %s %d", RelationGetRelationName(rel), heapBlk);
346 #endif
347 
348  /* Reuse the old pinned buffer if possible */
349  if (BufferIsValid(*vmbuf))
350  {
351  if (BufferGetBlockNumber(*vmbuf) != mapBlock)
352  {
353  ReleaseBuffer(*vmbuf);
354  *vmbuf = InvalidBuffer;
355  }
356  }
357 
358  if (!BufferIsValid(*vmbuf))
359  {
360  *vmbuf = vm_readbuf(rel, mapBlock, false);
361  if (!BufferIsValid(*vmbuf))
362  return false;
363  }
364 
365  map = PageGetContents(BufferGetPage(*vmbuf));
366 
367  /*
368  * A single byte read is atomic. There could be memory-ordering effects
369  * here, but for performance reasons we make it the caller's job to worry
370  * about that.
371  */
372  result = ((map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS);
#define InvalidBuffer
Definition: buf.h:25
unsigned int uint32
Definition: c.h:442

References BufferGetBlockNumber(), BufferGetPage(), BufferIsValid(), DEBUG1, elog(), HEAPBLK_TO_MAPBLOCK, HEAPBLK_TO_MAPBYTE, HEAPBLK_TO_OFFSET, InvalidBuffer, PageGetContents(), RelationGetRelationName, ReleaseBuffer(), VISIBILITYMAP_VALID_BITS, and vm_readbuf().

Referenced by collect_visibility_data(), lazy_scan_skip(), lazy_vacuum_heap_page(), pg_visibility(), pg_visibility_map(), pg_visibility_map_summary(), and verify_heapam().

◆ visibilitymap_pin()

void visibilitymap_pin ( Relation  rel,
BlockNumber  heapBlk,
Buffer vmbuf 
)

◆ visibilitymap_pin_ok()

bool visibilitymap_pin_ok ( BlockNumber  heapBlk,
Buffer  vmbuf 
)

Definition at line 213 of file visibilitymap.c.

216 {
217  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
218 

References BufferGetBlockNumber(), BufferIsValid(), and HEAPBLK_TO_MAPBLOCK.

Referenced by GetVisibilityMapPins(), and heap_multi_insert().

◆ visibilitymap_prepare_truncate()

BlockNumber visibilitymap_prepare_truncate ( Relation  rel,
BlockNumber  nheapblocks 
)

Definition at line 449 of file visibilitymap.c.

452 {
453  BlockNumber newnblocks;
454 
455  /* last remaining block, byte, and bit */
456  BlockNumber truncBlock = HEAPBLK_TO_MAPBLOCK(nheapblocks);
457  uint32 truncByte = HEAPBLK_TO_MAPBYTE(nheapblocks);
458  uint8 truncOffset = HEAPBLK_TO_OFFSET(nheapblocks);
459 
460 #ifdef TRACE_VISIBILITYMAP
461  elog(DEBUG1, "vm_truncate %s %d", RelationGetRelationName(rel), nheapblocks);
462 #endif
463 
464  /*
465  * If no visibility map has been created yet for this relation, there's
466  * nothing to truncate.
467  */
469  return InvalidBlockNumber;
470 
471  /*
472  * Unless the new size is exactly at a visibility map page boundary, the
473  * tail bits in the last remaining map page, representing truncated heap
474  * blocks, need to be cleared. This is not only tidy, but also necessary
475  * because we don't get a chance to clear the bits if the heap is extended
476  * again.
477  */
478  if (truncByte != 0 || truncOffset != 0)
479  {
480  Buffer mapBuffer;
481  Page page;
482  char *map;
483 
484  newnblocks = truncBlock + 1;
485 
486  mapBuffer = vm_readbuf(rel, truncBlock, false);
487  if (!BufferIsValid(mapBuffer))
488  {
489  /* nothing to do, the file was already smaller */
490  return InvalidBlockNumber;
491  }
492 
493  page = BufferGetPage(mapBuffer);
494  map = PageGetContents(page);
495 
496  LockBuffer(mapBuffer, BUFFER_LOCK_EXCLUSIVE);
497 
498  /* NO EREPORT(ERROR) from here till changes are logged */
500 
501  /* Clear out the unwanted bytes. */
502  MemSet(&map[truncByte + 1], 0, MAPSIZE - (truncByte + 1));
503 
504  /*----
505  * Mask out the unwanted bits of the last remaining byte.
506  *
507  * ((1 << 0) - 1) = 00000000
508  * ((1 << 1) - 1) = 00000001
509  * ...
510  * ((1 << 6) - 1) = 00111111
511  * ((1 << 7) - 1) = 01111111
512  *----
513  */
514  map[truncByte] &= (1 << truncOffset) - 1;
515 
516  /*
517  * Truncation of a relation is WAL-logged at a higher-level, and we
518  * will be called at WAL replay. But if checksums are enabled, we need
519  * to still write a WAL record to protect against a torn page, if the
520  * page is flushed to disk before the truncation WAL record. We cannot
521  * use MarkBufferDirtyHint here, because that will not dirty the page
522  * during recovery.
523  */
524  MarkBufferDirty(mapBuffer);
526  log_newpage_buffer(mapBuffer, false);
527 
529 
530  UnlockReleaseBuffer(mapBuffer);
531  }
532  else
533  newnblocks = truncBlock;
534 
535  if (smgrnblocks(RelationGetSmgr(rel), VISIBILITYMAP_FORKNUM) <= newnblocks)
536  {
537  /* nothing to do, the file was already smaller than requested size */
538  return InvalidBlockNumber;
539  }
540 
#define InvalidBlockNumber
Definition: block.h:33
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3954
Pointer Page
Definition: bufpage.h:78
#define MemSet(start, val, len)
Definition: c.h:953
#define START_CRIT_SECTION()
Definition: miscadmin.h:148
#define END_CRIT_SECTION()
Definition: miscadmin.h:150
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:569
#define RelationNeedsWAL(relation)
Definition: rel.h:626
@ VISIBILITYMAP_FORKNUM
Definition: relpath.h:52
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:579
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:247
#define XLogHintBitIsNeeded()
Definition: xlog.h:115
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1191
bool InRecovery
Definition: xlogutils.c:53

References BUFFER_LOCK_EXCLUSIVE, BufferGetPage(), BufferIsValid(), DEBUG1, elog(), END_CRIT_SECTION, HEAPBLK_TO_MAPBLOCK, HEAPBLK_TO_MAPBYTE, HEAPBLK_TO_OFFSET, InRecovery, InvalidBlockNumber, LockBuffer(), log_newpage_buffer(), MAPSIZE, MarkBufferDirty(), MemSet, PageGetContents(), RelationGetRelationName, RelationGetSmgr(), RelationNeedsWAL, smgrexists(), smgrnblocks(), START_CRIT_SECTION, UnlockReleaseBuffer(), VISIBILITYMAP_FORKNUM, vm_readbuf(), and XLogHintBitIsNeeded.

Referenced by pg_truncate_visibility_map(), RelationTruncate(), and smgr_redo().

◆ visibilitymap_set()

void visibilitymap_set ( Relation  rel,
BlockNumber  heapBlk,
Buffer  heapBuf,
XLogRecPtr  recptr,
Buffer  vmBuf,
TransactionId  cutoff_xid,
uint8  flags 
)

Definition at line 242 of file visibilitymap.c.

247 {
248  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
249  uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
250  uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
251  Page page;
252  uint8 *map;
253 
254 #ifdef TRACE_VISIBILITYMAP
255  elog(DEBUG1, "vm_set %s %d", RelationGetRelationName(rel), heapBlk);
256 #endif
257 
259  Assert(InRecovery || BufferIsValid(heapBuf));
261 
262  /* Check that we have the right heap page pinned, if present */
263  if (BufferIsValid(heapBuf) && BufferGetBlockNumber(heapBuf) != heapBlk)
264  elog(ERROR, "wrong heap buffer passed to visibilitymap_set");
265 
266  /* Check that we have the right VM page pinned */
267  if (!BufferIsValid(vmBuf) || BufferGetBlockNumber(vmBuf) != mapBlock)
268  elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
269 
270  page = BufferGetPage(vmBuf);
271  map = (uint8 *) PageGetContents(page);
273 
274  if (flags != (map[mapByte] >> mapOffset & VISIBILITYMAP_VALID_BITS))
275  {
277 
278  map[mapByte] |= (flags << mapOffset);
279  MarkBufferDirty(vmBuf);
280 
281  if (RelationNeedsWAL(rel))
282  {
283  if (XLogRecPtrIsInvalid(recptr))
284  {
285  Assert(!InRecovery);
286  recptr = log_heap_visible(rel->rd_locator, heapBuf, vmBuf,
287  cutoff_xid, flags);
288 
289  /*
290  * If data checksums are enabled (or wal_log_hints=on), we
291  * need to protect the heap page from being torn.
292  *
293  * If not, then we must *not* update the heap page's LSN. In
294  * this case, the FPI for the heap page was omitted from the
295  * WAL record inserted above, so it would be incorrect to
296  * update the heap page's LSN.
297  */
298  if (XLogHintBitIsNeeded())
299  {
300  Page heapPage = BufferGetPage(heapBuf);
301 
302  /* caller is expected to set PD_ALL_VISIBLE first */
303  Assert(PageIsAllVisible(heapPage));
304  PageSetLSN(heapPage, recptr);
305  }
306  }
307  PageSetLSN(page, recptr);
308  }
309 
311  }
312 
static bool PageIsAllVisible(Page page)
Definition: bufpage.h:426
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:388
XLogRecPtr log_heap_visible(RelFileLocator rlocator, Buffer heap_buffer, Buffer vm_buffer, TransactionId snapshotConflictHorizon, uint8 vmflags)
Definition: heapam.c:8251
RelFileLocator rd_locator
Definition: rel.h:56
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29

References Assert(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage(), BufferIsValid(), DEBUG1, elog(), END_CRIT_SECTION, ERROR, HEAPBLK_TO_MAPBLOCK, HEAPBLK_TO_MAPBYTE, HEAPBLK_TO_OFFSET, InRecovery, LockBuffer(), log_heap_visible(), MarkBufferDirty(), PageGetContents(), PageIsAllVisible(), PageSetLSN(), RelationData::rd_locator, RelationGetRelationName, RelationNeedsWAL, START_CRIT_SECTION, VISIBILITYMAP_VALID_BITS, XLogHintBitIsNeeded, and XLogRecPtrIsInvalid.

Referenced by heap_multi_insert(), heap_xlog_visible(), lazy_scan_heap(), lazy_scan_new_or_empty(), and lazy_vacuum_heap_page().

◆ vm_extend()

static void vm_extend ( Relation  rel,
BlockNumber  vm_nblocks 
)
static

Definition at line 618 of file visibilitymap.c.

621 {
622  BlockNumber vm_nblocks_now;
623  PGAlignedBlock pg;
624  SMgrRelation reln;
625 
626  PageInit((Page) pg.data, BLCKSZ, 0);
627 
628  /*
629  * We use the relation extension lock to lock out other backends trying to
630  * extend the visibility map at the same time. It also locks out extension
631  * of the main fork, unnecessarily, but extending the visibility map
632  * happens seldom enough that it doesn't seem worthwhile to have a
633  * separate lock tag type for it.
634  *
635  * Note that another backend might have extended or created the relation
636  * by the time we get the lock.
637  */
639 
640  /*
641  * Caution: re-using this smgr pointer could fail if the relcache entry
642  * gets closed. It's safe as long as we only do smgr-level operations
643  * between here and the last use of the pointer.
644  */
645  reln = RelationGetSmgr(rel);
646 
647  /*
648  * Create the file first if it doesn't exist. If smgr_vm_nblocks is
649  * positive then it must exist, no need for an smgrexists call.
650  */
651  if ((reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] == 0 ||
654  smgrcreate(reln, VISIBILITYMAP_FORKNUM, false);
655 
656  /* Invalidate cache so that smgrnblocks() asks the kernel. */
658  vm_nblocks_now = smgrnblocks(reln, VISIBILITYMAP_FORKNUM);
659 
660  /* Now extend the file */
661  while (vm_nblocks_now < vm_nblocks)
662  {
663  PageSetChecksumInplace((Page) pg.data, vm_nblocks_now);
664 
665  smgrextend(reln, VISIBILITYMAP_FORKNUM, vm_nblocks_now, pg.data, false);
666  vm_nblocks_now++;
667  }
668 
669  /*
670  * Send a shared-inval message to force other backends to close any smgr
671  * references they may have for this rel, which we are about to change.
672  * This is a useful optimization because it means that backends don't have
673  * to keep checking for creation or extension of the file, which happens
674  * infrequently.
675  */
677 
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1539
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42
void CacheInvalidateSmgr(RelFileLocatorBackend rlocator)
Definition: inval.c:1462
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:431
#define ExclusiveLock
Definition: lockdefs.h:42
void smgrextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:493
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:369
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:54
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:42
char data[BLCKSZ]
Definition: c.h:1079

References CacheInvalidateSmgr(), PGAlignedBlock::data, ExclusiveLock, InvalidBlockNumber, LockRelationForExtension(), PageInit(), PageSetChecksumInplace(), RelationGetSmgr(), SMgrRelationData::smgr_cached_nblocks, SMgrRelationData::smgr_rlocator, smgrcreate(), smgrexists(), smgrextend(), smgrnblocks(), UnlockRelationForExtension(), and VISIBILITYMAP_FORKNUM.

Referenced by vm_readbuf().

◆ vm_readbuf()

static Buffer vm_readbuf ( Relation  rel,
BlockNumber  blkno,
bool  extend 
)
static

Definition at line 549 of file visibilitymap.c.

552 {
553  Buffer buf;
554  SMgrRelation reln;
555 
556  /*
557  * Caution: re-using this smgr pointer could fail if the relcache entry
558  * gets closed. It's safe as long as we only do smgr-level operations
559  * between here and the last use of the pointer.
560  */
561  reln = RelationGetSmgr(rel);
562 
563  /*
564  * If we haven't cached the size of the visibility map fork yet, check it
565  * first.
566  */
568  {
571  else
573  }
574 
575  /* Handle requests beyond EOF */
576  if (blkno >= reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM])
577  {
578  if (extend)
579  vm_extend(rel, blkno + 1);
580  else
581  return InvalidBuffer;
582  }
583 
584  /*
585  * Use ZERO_ON_ERROR mode, and initialize the page if necessary. It's
586  * always safe to clear bits, so it's better to clear corrupt pages than
587  * error out.
588  *
589  * The initialize-the-page part is trickier than it looks, because of the
590  * possibility of multiple backends doing this concurrently, and our
591  * desire to not uselessly take the buffer lock in the normal path where
592  * the page is OK. We must take the lock to initialize the page, so
593  * recheck page newness after we have the lock, in case someone else
594  * already did it. Also, because we initially check PageIsNew with no
595  * lock, it's possible to fall through and return the buffer while someone
596  * else is still initializing the page (i.e., we might see pd_upper as set
597  * but other page header fields are still zeroes). This is harmless for
598  * callers that will take a buffer lock themselves, but some callers
599  * inspect the page without any lock at all. The latter is OK only so
600  * long as it doesn't depend on the page header having correct contents.
601  * Current usage is safe because PageGetContents() does not require that.
602  */
604  RBM_ZERO_ON_ERROR, NULL);
606  {
609  PageInit(BufferGetPage(buf), BLCKSZ, 0);
611  }
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:759
@ RBM_ZERO_ON_ERROR
Definition: bufmgr.h:44
static bool PageIsNew(Page page)
Definition: bufpage.h:230
static char * buf
Definition: pg_test_fsync.c:67
static void vm_extend(Relation rel, BlockNumber vm_nblocks)

References buf, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetPage(), InvalidBlockNumber, InvalidBuffer, LockBuffer(), PageInit(), PageIsNew(), RBM_ZERO_ON_ERROR, ReadBufferExtended(), RelationGetSmgr(), SMgrRelationData::smgr_cached_nblocks, smgrexists(), smgrnblocks(), VISIBILITYMAP_FORKNUM, and vm_extend().

Referenced by visibilitymap_count(), visibilitymap_get_status(), visibilitymap_pin(), and visibilitymap_prepare_truncate().