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 buf, uint8 flags)
 
void visibilitymap_pin (Relation rel, BlockNumber heapBlk, Buffer *buf)
 
bool visibilitymap_pin_ok (BlockNumber heapBlk, Buffer buf)
 
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 *buf)
 
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  buf,
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(buf) || BufferGetBlockNumber(buf) != mapBlock)
156  elog(ERROR, "wrong buffer passed to visibilitymap_clear");
157 
160 
161  if (map[mapByte] & mask)
162  {
163  map[mapByte] &= ~mask;
164 
166  cleared = true;
167  }
168 
170 
uint32 BlockNumber
Definition: block.h:31
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2755
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1573
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4156
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define PageGetContents(page)
Definition: bufpage.h:245
unsigned char uint8
Definition: c.h:439
#define DEBUG1
Definition: elog.h:24
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
Assert(fmt[strlen(fmt) - 1] !='\n')
static char * buf
Definition: pg_test_fsync.c:67
#define RelationGetRelationName(relation)
Definition: rel.h:523
#define HEAPBLK_TO_OFFSET(x)
#define HEAPBLK_TO_MAPBLOCK(x)
#define HEAPBLK_TO_MAPBYTE(x)
#define VISIBILITYMAP_VALID_BITS

References Assert(), buf, 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 377 of file visibilitymap.c.

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

Definition at line 329 of file visibilitymap.c.

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

References buf, 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 buf 
)

◆ visibilitymap_pin_ok()

bool visibilitymap_pin_ok ( BlockNumber  heapBlk,
Buffer  buf 
)

Definition at line 213 of file visibilitymap.c.

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

References buf, 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 444 of file visibilitymap.c.

447 {
448  BlockNumber newnblocks;
449 
450  /* last remaining block, byte, and bit */
451  BlockNumber truncBlock = HEAPBLK_TO_MAPBLOCK(nheapblocks);
452  uint32 truncByte = HEAPBLK_TO_MAPBYTE(nheapblocks);
453  uint8 truncOffset = HEAPBLK_TO_OFFSET(nheapblocks);
454 
455 #ifdef TRACE_VISIBILITYMAP
456  elog(DEBUG1, "vm_truncate %s %d", RelationGetRelationName(rel), nheapblocks);
457 #endif
458 
459  /*
460  * If no visibility map has been created yet for this relation, there's
461  * nothing to truncate.
462  */
464  return InvalidBlockNumber;
465 
466  /*
467  * Unless the new size is exactly at a visibility map page boundary, the
468  * tail bits in the last remaining map page, representing truncated heap
469  * blocks, need to be cleared. This is not only tidy, but also necessary
470  * because we don't get a chance to clear the bits if the heap is extended
471  * again.
472  */
473  if (truncByte != 0 || truncOffset != 0)
474  {
475  Buffer mapBuffer;
476  Page page;
477  char *map;
478 
479  newnblocks = truncBlock + 1;
480 
481  mapBuffer = vm_readbuf(rel, truncBlock, false);
482  if (!BufferIsValid(mapBuffer))
483  {
484  /* nothing to do, the file was already smaller */
485  return InvalidBlockNumber;
486  }
487 
488  page = BufferGetPage(mapBuffer);
489  map = PageGetContents(page);
490 
491  LockBuffer(mapBuffer, BUFFER_LOCK_EXCLUSIVE);
492 
493  /* NO EREPORT(ERROR) from here till changes are logged */
495 
496  /* Clear out the unwanted bytes. */
497  MemSet(&map[truncByte + 1], 0, MAPSIZE - (truncByte + 1));
498 
499  /*----
500  * Mask out the unwanted bits of the last remaining byte.
501  *
502  * ((1 << 0) - 1) = 00000000
503  * ((1 << 1) - 1) = 00000001
504  * ...
505  * ((1 << 6) - 1) = 00111111
506  * ((1 << 7) - 1) = 01111111
507  *----
508  */
509  map[truncByte] &= (1 << truncOffset) - 1;
510 
511  /*
512  * Truncation of a relation is WAL-logged at a higher-level, and we
513  * will be called at WAL replay. But if checksums are enabled, we need
514  * to still write a WAL record to protect against a torn page, if the
515  * page is flushed to disk before the truncation WAL record. We cannot
516  * use MarkBufferDirtyHint here, because that will not dirty the page
517  * during recovery.
518  */
519  MarkBufferDirty(mapBuffer);
521  log_newpage_buffer(mapBuffer, false);
522 
524 
525  UnlockReleaseBuffer(mapBuffer);
526  }
527  else
528  newnblocks = truncBlock;
529 
530  if (smgrnblocks(RelationGetSmgr(rel), VISIBILITYMAP_FORKNUM) <= newnblocks)
531  {
532  /* nothing to do, the file was already smaller than requested size */
533  return InvalidBlockNumber;
534  }
535 
#define InvalidBlockNumber
Definition: block.h:33
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3938
Pointer Page
Definition: bufpage.h:78
#define MemSet(start, val, len)
Definition: c.h:1008
#define START_CRIT_SECTION()
Definition: miscadmin.h:148
#define END_CRIT_SECTION()
Definition: miscadmin.h:150
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:556
#define RelationNeedsWAL(relation)
Definition: rel.h:613
@ VISIBILITYMAP_FORKNUM
Definition: relpath.h:45
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:1177
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_node, 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 (XLogHintBitIsNeeded())
294  {
295  Page heapPage = BufferGetPage(heapBuf);
296 
297  /* caller is expected to set PD_ALL_VISIBLE first */
298  Assert(PageIsAllVisible(heapPage));
299  PageSetLSN(heapPage, recptr);
300  }
301  }
302  PageSetLSN(page, recptr);
303  }
304 
306  }
307 
#define PageIsAllVisible(page)
Definition: bufpage.h:384
#define PageSetLSN(page, lsn)
Definition: bufpage.h:367
XLogRecPtr log_heap_visible(RelFileNode rnode, Buffer heap_buffer, Buffer vm_buffer, TransactionId cutoff_xid, uint8 vmflags)
Definition: heapam.c:8192
RelFileNode rd_node
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_node, 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 613 of file visibilitymap.c.

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

References CacheInvalidateSmgr(), PGAlignedBlock::data, ExclusiveLock, InvalidBlockNumber, LockRelationForExtension(), PageInit(), PageSetChecksumInplace(), RelationGetSmgr(), SMgrRelationData::smgr_cached_nblocks, SMgrRelationData::smgr_rnode, 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 544 of file visibilitymap.c.

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