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 
149  /* Must never clear all_visible bit while leaving all_frozen bit set */
152 
153 #ifdef TRACE_VISIBILITYMAP
154  elog(DEBUG1, "vm_clear %s %d", RelationGetRelationName(rel), heapBlk);
155 #endif
156 
157  if (!BufferIsValid(vmbuf) || BufferGetBlockNumber(vmbuf) != mapBlock)
158  elog(ERROR, "wrong buffer passed to visibilitymap_clear");
159 
161  map = PageGetContents(BufferGetPage(vmbuf));
162 
163  if (map[mapByte] & mask)
164  {
165  map[mapByte] &= ~mask;
166 
167  MarkBufferDirty(vmbuf);
168  cleared = true;
169  }
170 
172 
uint32 BlockNumber
Definition: block.h:31
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2811
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1631
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4246
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:110
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:285
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:112
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:233
static char * PageGetContents(Page page)
Definition: bufpage.h:254
unsigned char uint8
Definition: c.h:488
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
Assert(fmt[strlen(fmt) - 1] !='\n')
#define RelationGetRelationName(relation)
Definition: rel.h:537
#define HEAPBLK_TO_OFFSET(x)
#define HEAPBLK_TO_MAPBLOCK(x)
#define HEAPBLK_TO_MAPBYTE(x)
#define VISIBILITYMAP_VALID_BITS
#define VISIBILITYMAP_ALL_VISIBLE

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, VISIBILITYMAP_ALL_VISIBLE, 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 385 of file visibilitymap.c.

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

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

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_heap(), lazy_scan_skip(), 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 215 of file visibilitymap.c.

218 {
219  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
220 

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 452 of file visibilitymap.c.

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

249 {
250  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
251  uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
252  uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
253  Page page;
254  uint8 *map;
255 
256 #ifdef TRACE_VISIBILITYMAP
257  elog(DEBUG1, "vm_set %s %d", RelationGetRelationName(rel), heapBlk);
258 #endif
259 
262 
263  /* Must never set all_frozen bit without also setting all_visible bit */
266 
267  /* Check that we have the right heap page pinned, if present */
268  if (BufferIsValid(heapBuf) && BufferGetBlockNumber(heapBuf) != heapBlk)
269  elog(ERROR, "wrong heap buffer passed to visibilitymap_set");
270 
271  /* Check that we have the right VM page pinned */
272  if (!BufferIsValid(vmBuf) || BufferGetBlockNumber(vmBuf) != mapBlock)
273  elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
274 
275  page = BufferGetPage(vmBuf);
276  map = (uint8 *) PageGetContents(page);
278 
279  if (flags != (map[mapByte] >> mapOffset & VISIBILITYMAP_VALID_BITS))
280  {
282 
283  map[mapByte] |= (flags << mapOffset);
284  MarkBufferDirty(vmBuf);
285 
286  if (RelationNeedsWAL(rel))
287  {
288  if (XLogRecPtrIsInvalid(recptr))
289  {
290  Assert(!InRecovery);
291  recptr = log_heap_visible(rel->rd_locator, heapBuf, vmBuf,
292  cutoff_xid, flags);
293 
294  /*
295  * If data checksums are enabled (or wal_log_hints=on), we
296  * need to protect the heap page from being torn.
297  *
298  * If not, then we must *not* update the heap page's LSN. In
299  * this case, the FPI for the heap page was omitted from the
300  * WAL record inserted above, so it would be incorrect to
301  * update the heap page's LSN.
302  */
303  if (XLogHintBitIsNeeded())
304  {
305  Page heapPage = BufferGetPage(heapBuf);
306 
307  PageSetLSN(heapPage, recptr);
308  }
309  }
310  PageSetLSN(page, recptr);
311  }
312 
314  }
315 
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:8271
RelFileLocator rd_locator
Definition: rel.h:56
#define VISIBILITYMAP_ALL_FROZEN
#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_ALL_FROZEN, 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 621 of file visibilitymap.c.

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

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 552 of file visibilitymap.c.

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