PostgreSQL Source Code  git master
visibilitymap.c File Reference
#include "postgres.h"
#include "access/heapam_xlog.h"
#include "access/visibilitymap.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.

Referenced by visibilitymap_count().

◆ HEAPBLK_TO_MAPBLOCK

◆ HEAPBLK_TO_MAPBYTE

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

◆ HEAPBLK_TO_OFFSET

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

◆ HEAPBLOCKS_PER_BYTE

#define HEAPBLOCKS_PER_BYTE   (BITS_PER_BYTE / BITS_PER_HEAPBLOCK)

Definition at line 110 of file visibilitymap.c.

◆ HEAPBLOCKS_PER_PAGE

#define HEAPBLOCKS_PER_PAGE   (MAPSIZE * HEAPBLOCKS_PER_BYTE)

Definition at line 113 of file visibilitymap.c.

◆ MAPSIZE

#define MAPSIZE   (BLCKSZ - MAXALIGN(SizeOfPageHeaderData))

Definition at line 107 of file visibilitymap.c.

Referenced by visibilitymap_count(), and visibilitymap_prepare_truncate().

◆ VISIBLE_MASK64

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

Definition at line 121 of file visibilitymap.c.

Referenced by visibilitymap_count().

Function Documentation

◆ visibilitymap_clear()

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

Definition at line 139 of file visibilitymap.c.

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

140 {
141  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
142  int mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
143  int mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
144  uint8 mask = flags << mapOffset;
145  char *map;
146  bool cleared = false;
147 
149 
150 #ifdef TRACE_VISIBILITYMAP
151  elog(DEBUG1, "vm_clear %s %d", RelationGetRelationName(rel), heapBlk);
152 #endif
153 
154  if (!BufferIsValid(buf) || BufferGetBlockNumber(buf) != mapBlock)
155  elog(ERROR, "wrong buffer passed to visibilitymap_clear");
156 
159 
160  if (map[mapByte] & mask)
161  {
162  map[mapByte] &= ~mask;
163 
165  cleared = true;
166  }
167 
169 
170  return cleared;
171 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define DEBUG1
Definition: elog.h:25
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1565
unsigned char uint8
Definition: c.h:439
#define HEAPBLK_TO_MAPBYTE(x)
uint32 BlockNumber
Definition: block.h:31
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
#define ERROR
Definition: elog.h:46
static char * buf
Definition: pg_test_fsync.c:68
#define RelationGetRelationName(relation)
Definition: rel.h:511
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define PageGetContents(page)
Definition: bufpage.h:246
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4007
#define Assert(condition)
Definition: c.h:804
#define HEAPBLK_TO_OFFSET(x)
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define VISIBILITYMAP_VALID_BITS
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2748
#define elog(elevel,...)
Definition: elog.h:232
#define HEAPBLK_TO_MAPBLOCK(x)

◆ visibilitymap_count()

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

Definition at line 378 of file visibilitymap.c.

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

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

◆ visibilitymap_get_status()

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

Definition at line 330 of file visibilitymap.c.

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_vacuum_heap_page(), pg_visibility(), pg_visibility_map(), pg_visibility_map_summary(), and verify_heapam().

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

◆ visibilitymap_pin()

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

Definition at line 190 of file visibilitymap.c.

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

Referenced by GetVisibilityMapPins(), heap_delete(), heap_force_common(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_update(), heap_xlog_delete(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_update(), heap_xlog_visible(), lazy_scan_heap(), and RelationGetBufferForTuple().

191 {
192  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
193 
194  /* Reuse the old pinned buffer if possible */
195  if (BufferIsValid(*buf))
196  {
197  if (BufferGetBlockNumber(*buf) == mapBlock)
198  return;
199 
200  ReleaseBuffer(*buf);
201  }
202  *buf = vm_readbuf(rel, mapBlock, true);
203 }
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3768
static char * buf
Definition: pg_test_fsync.c:68
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2748
static Buffer vm_readbuf(Relation rel, BlockNumber blkno, bool extend)
#define HEAPBLK_TO_MAPBLOCK(x)

◆ visibilitymap_pin_ok()

bool visibilitymap_pin_ok ( BlockNumber  heapBlk,
Buffer  buf 
)

Definition at line 214 of file visibilitymap.c.

References BufferGetBlockNumber(), BufferIsValid, and HEAPBLK_TO_MAPBLOCK.

Referenced by GetVisibilityMapPins(), and heap_multi_insert().

215 {
216  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
217 
218  return BufferIsValid(buf) && BufferGetBlockNumber(buf) == mapBlock;
219 }
uint32 BlockNumber
Definition: block.h:31
static char * buf
Definition: pg_test_fsync.c:68
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2748
#define HEAPBLK_TO_MAPBLOCK(x)

◆ visibilitymap_prepare_truncate()

BlockNumber visibilitymap_prepare_truncate ( Relation  rel,
BlockNumber  nheapblocks 
)

Definition at line 445 of file visibilitymap.c.

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

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

◆ visibilitymap_set()

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

Definition at line 243 of file visibilitymap.c.

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(), and lazy_vacuum_heap_page().

246 {
247  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
248  uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
249  uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
250  Page page;
251  uint8 *map;
252 
253 #ifdef TRACE_VISIBILITYMAP
254  elog(DEBUG1, "vm_set %s %d", RelationGetRelationName(rel), heapBlk);
255 #endif
256 
258  Assert(InRecovery || BufferIsValid(heapBuf));
260 
261  /* Check that we have the right heap page pinned, if present */
262  if (BufferIsValid(heapBuf) && BufferGetBlockNumber(heapBuf) != heapBlk)
263  elog(ERROR, "wrong heap buffer passed to visibilitymap_set");
264 
265  /* Check that we have the right VM page pinned */
266  if (!BufferIsValid(vmBuf) || BufferGetBlockNumber(vmBuf) != mapBlock)
267  elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
268 
269  page = BufferGetPage(vmBuf);
270  map = (uint8 *) PageGetContents(page);
272 
273  if (flags != (map[mapByte] >> mapOffset & VISIBILITYMAP_VALID_BITS))
274  {
276 
277  map[mapByte] |= (flags << mapOffset);
278  MarkBufferDirty(vmBuf);
279 
280  if (RelationNeedsWAL(rel))
281  {
282  if (XLogRecPtrIsInvalid(recptr))
283  {
284  Assert(!InRecovery);
285  recptr = log_heap_visible(rel->rd_node, heapBuf, vmBuf,
286  cutoff_xid, flags);
287 
288  /*
289  * If data checksums are enabled (or wal_log_hints=on), we
290  * need to protect the heap page from being torn.
291  */
292  if (XLogHintBitIsNeeded())
293  {
294  Page heapPage = BufferGetPage(heapBuf);
295 
296  /* caller is expected to set PD_ALL_VISIBLE first */
297  Assert(PageIsAllVisible(heapPage));
298  PageSetLSN(heapPage, recptr);
299  }
300  }
301  PageSetLSN(page, recptr);
302  }
303 
305  }
306 
308 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define DEBUG1
Definition: elog.h:25
#define PageIsAllVisible(page)
Definition: bufpage.h:385
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1565
#define END_CRIT_SECTION()
Definition: miscadmin.h:149
unsigned char uint8
Definition: c.h:439
#define HEAPBLK_TO_MAPBYTE(x)
#define START_CRIT_SECTION()
Definition: miscadmin.h:147
uint32 BlockNumber
Definition: block.h:31
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
#define ERROR
Definition: elog.h:46
XLogRecPtr log_heap_visible(RelFileNode rnode, Buffer heap_buffer, Buffer vm_buffer, TransactionId cutoff_xid, uint8 vmflags)
Definition: heapam.c:7978
#define RelationGetRelationName(relation)
Definition: rel.h:511
unsigned int uint32
Definition: c.h:441
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define PageGetContents(page)
Definition: bufpage.h:246
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4007
bool InRecovery
Definition: xlogutils.c:52
RelFileNode rd_node
Definition: rel.h:56
#define Assert(condition)
Definition: c.h:804
#define HEAPBLK_TO_OFFSET(x)
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define RelationNeedsWAL(relation)
Definition: rel.h:601
#define VISIBILITYMAP_VALID_BITS
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2748
#define elog(elevel,...)
Definition: elog.h:232
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
#define XLogHintBitIsNeeded()
Definition: xlog.h:177
Pointer Page
Definition: bufpage.h:78
#define HEAPBLK_TO_MAPBLOCK(x)

◆ vm_extend()

static void vm_extend ( Relation  rel,
BlockNumber  vm_nblocks 
)
static

Definition at line 614 of file visibilitymap.c.

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

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

◆ vm_readbuf()

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

Definition at line 545 of file visibilitymap.c.

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

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