PostgreSQL Source Code  git master
visibilitymap.h File Reference
#include "access/visibilitymapdefs.h"
#include "access/xlogdefs.h"
#include "storage/block.h"
#include "storage/buf.h"
#include "utils/relcache.h"
Include dependency graph for visibilitymap.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define VM_ALL_VISIBLE(r, b, v)    ((visibilitymap_get_status((r), (b), (v)) & VISIBILITYMAP_ALL_VISIBLE) != 0)
 
#define VM_ALL_FROZEN(r, b, v)    ((visibilitymap_get_status((r), (b), (v)) & VISIBILITYMAP_ALL_FROZEN) != 0)
 

Functions

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

◆ VM_ALL_FROZEN

#define VM_ALL_FROZEN (   r,
  b,
 
)     ((visibilitymap_get_status((r), (b), (v)) & VISIBILITYMAP_ALL_FROZEN) != 0)

Definition at line 26 of file visibilitymap.h.

◆ VM_ALL_VISIBLE

#define VM_ALL_VISIBLE (   r,
  b,
 
)     ((visibilitymap_get_status((r), (b), (v)) & VISIBILITYMAP_ALL_VISIBLE) != 0)

Definition at line 24 of file visibilitymap.h.

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:3377
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2189
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4795
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:157
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:350
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:159
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:301
static char * PageGetContents(Page page)
Definition: bufpage.h:254
unsigned char uint8
Definition: c.h:491
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
Assert(fmt[strlen(fmt) - 1] !='\n')
#define RelationGetRelationName(relation)
Definition: rel.h:539
#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_prune().

◆ visibilitymap_count()

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

Definition at line 384 of file visibilitymap.c.

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

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

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(), find_next_unskippable_block(), lazy_scan_prune(), 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(), heap_multi_insert(), and RelationGetBufferForTuple().

◆ visibilitymap_prepare_truncate()

BlockNumber visibilitymap_prepare_truncate ( Relation  rel,
BlockNumber  nheapblocks 
)

Definition at line 451 of file visibilitymap.c.

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

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  Assert((flags & VISIBILITYMAP_VALID_BITS) == flags);
263 
264  /* 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, heapBuf, vmBuf, cutoff_xid, flags);
292 
293  /*
294  * If data checksums are enabled (or wal_log_hints=on), we
295  * need to protect the heap page from being torn.
296  *
297  * If not, then we must *not* update the heap page's LSN. In
298  * this case, the FPI for the heap page was omitted from the
299  * WAL record inserted above, so it would be incorrect to
300  * update the heap page's LSN.
301  */
302  if (XLogHintBitIsNeeded())
303  {
304  Page heapPage = BufferGetPage(heapBuf);
305 
306  PageSetLSN(heapPage, recptr);
307  }
308  }
309  PageSetLSN(page, recptr);
310  }
311 
313  }
314 
static bool PageIsAllVisible(Page page)
Definition: bufpage.h:426
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:388
XLogRecPtr log_heap_visible(Relation rel, Buffer heap_buffer, Buffer vm_buffer, TransactionId snapshotConflictHorizon, uint8 vmflags)
Definition: heapam.c:8330
#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(), RelationGetRelationName, RelationNeedsWAL, START_CRIT_SECTION, VISIBILITYMAP_ALL_FROZEN, VISIBILITYMAP_VALID_BITS, XLogHintBitIsNeeded, and XLogRecPtrIsInvalid.

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