PostgreSQL Source Code  git master
visibilitymap.c File Reference
#include "postgres.h"
#include "access/heapam_xlog.h"
#include "access/visibilitymap.h"
#include "access/xlog.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)
 
void visibilitymap_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 122 of file visibilitymap.c.

Referenced by visibilitymap_count().

◆ HEAPBLK_TO_MAPBLOCK

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

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

◆ HEAPBLOCKS_PER_PAGE

#define HEAPBLOCKS_PER_PAGE   (MAPSIZE * HEAPBLOCKS_PER_BYTE)

Definition at line 112 of file visibilitymap.c.

◆ MAPSIZE

#define MAPSIZE   (BLCKSZ - MAXALIGN(SizeOfPageHeaderData))

Definition at line 106 of file visibilitymap.c.

Referenced by visibilitymap_count(), and visibilitymap_truncate().

◆ VISIBLE_MASK64

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

Definition at line 120 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 138 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_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().

139 {
140  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
141  int mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
142  int mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
143  uint8 mask = flags << mapOffset;
144  char *map;
145  bool cleared = false;
146 
148 
149 #ifdef TRACE_VISIBILITYMAP
150  elog(DEBUG1, "vm_clear %s %d", RelationGetRelationName(rel), heapBlk);
151 #endif
152 
153  if (!BufferIsValid(buf) || BufferGetBlockNumber(buf) != mapBlock)
154  elog(ERROR, "wrong buffer passed to visibilitymap_clear");
155 
158 
159  if (map[mapByte] & mask)
160  {
161  map[mapByte] &= ~mask;
162 
164  cleared = true;
165  }
166 
168 
169  return cleared;
170 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
#define DEBUG1
Definition: elog.h:25
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1458
unsigned char uint8
Definition: c.h:356
#define HEAPBLK_TO_MAPBYTE(x)
uint32 BlockNumber
Definition: block.h:31
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:88
#define VISIBILITYMAP_VALID_BITS
Definition: visibilitymap.h:28
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:68
#define RelationGetRelationName(relation)
Definition: rel.h:450
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define PageGetContents(page)
Definition: bufpage.h:246
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
#define Assert(condition)
Definition: c.h:732
#define HEAPBLK_TO_OFFSET(x)
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
#define elog(elevel,...)
Definition: elog.h:226
#define HEAPBLK_TO_MAPBLOCK(x)

◆ visibilitymap_count()

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

Definition at line 377 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().

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

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

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

Referenced by GetVisibilityMapPins(), heap_delete(), 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().

190 {
191  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
192 
193  /* Reuse the old pinned buffer if possible */
194  if (BufferIsValid(*buf))
195  {
196  if (BufferGetBlockNumber(*buf) == mapBlock)
197  return;
198 
199  ReleaseBuffer(*buf);
200  }
201  *buf = vm_readbuf(rel, mapBlock, true);
202 }
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3353
static char * buf
Definition: pg_test_fsync.c:68
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
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 213 of file visibilitymap.c.

References BufferGetBlockNumber(), BufferIsValid, and HEAPBLK_TO_MAPBLOCK.

Referenced by GetVisibilityMapPins().

214 {
215  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
216 
217  return BufferIsValid(buf) && BufferGetBlockNumber(buf) == mapBlock;
218 }
uint32 BlockNumber
Definition: block.h:31
static char * buf
Definition: pg_test_fsync.c:68
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
#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 242 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_xlog_visible(), lazy_scan_heap(), and lazy_vacuum_page().

245 {
246  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
247  uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
248  uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
249  Page page;
250  uint8 *map;
251 
252 #ifdef TRACE_VISIBILITYMAP
253  elog(DEBUG1, "vm_set %s %d", RelationGetRelationName(rel), heapBlk);
254 #endif
255 
257  Assert(InRecovery || BufferIsValid(heapBuf));
259 
260  /* Check that we have the right heap page pinned, if present */
261  if (BufferIsValid(heapBuf) && BufferGetBlockNumber(heapBuf) != heapBlk)
262  elog(ERROR, "wrong heap buffer passed to visibilitymap_set");
263 
264  /* Check that we have the right VM page pinned */
265  if (!BufferIsValid(vmBuf) || BufferGetBlockNumber(vmBuf) != mapBlock)
266  elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
267 
268  page = BufferGetPage(vmBuf);
269  map = (uint8 *) PageGetContents(page);
271 
272  if (flags != (map[mapByte] >> mapOffset & VISIBILITYMAP_VALID_BITS))
273  {
275 
276  map[mapByte] |= (flags << mapOffset);
277  MarkBufferDirty(vmBuf);
278 
279  if (RelationNeedsWAL(rel))
280  {
281  if (XLogRecPtrIsInvalid(recptr))
282  {
283  Assert(!InRecovery);
284  recptr = log_heap_visible(rel->rd_node, heapBuf, vmBuf,
285  cutoff_xid, flags);
286 
287  /*
288  * If data checksums are enabled (or wal_log_hints=on), we
289  * need to protect the heap page from being torn.
290  */
291  if (XLogHintBitIsNeeded())
292  {
293  Page heapPage = BufferGetPage(heapBuf);
294 
295  /* caller is expected to set PD_ALL_VISIBLE first */
296  Assert(PageIsAllVisible(heapPage));
297  PageSetLSN(heapPage, recptr);
298  }
299  }
300  PageSetLSN(page, recptr);
301  }
302 
304  }
305 
307 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
#define DEBUG1
Definition: elog.h:25
#define PageIsAllVisible(page)
Definition: bufpage.h:385
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1458
bool InRecovery
Definition: xlog.c:200
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
unsigned char uint8
Definition: c.h:356
#define HEAPBLK_TO_MAPBYTE(x)
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
uint32 BlockNumber
Definition: block.h:31
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:88
#define VISIBILITYMAP_VALID_BITS
Definition: visibilitymap.h:28
#define ERROR
Definition: elog.h:43
XLogRecPtr log_heap_visible(RelFileNode rnode, Buffer heap_buffer, Buffer vm_buffer, TransactionId cutoff_xid, uint8 vmflags)
Definition: heapam.c:7268
#define RelationGetRelationName(relation)
Definition: rel.h:450
unsigned int uint32
Definition: c.h:358
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define PageGetContents(page)
Definition: bufpage.h:246
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
RelFileNode rd_node
Definition: rel.h:54
#define Assert(condition)
Definition: c.h:732
#define HEAPBLK_TO_OFFSET(x)
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
#define RelationNeedsWAL(relation)
Definition: rel.h:518
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
#define elog(elevel,...)
Definition: elog.h:226
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
#define XLogHintBitIsNeeded()
Definition: xlog.h:192
Pointer Page
Definition: bufpage.h:78
#define HEAPBLK_TO_MAPBLOCK(x)

◆ visibilitymap_truncate()

void visibilitymap_truncate ( Relation  rel,
BlockNumber  nheapblocks 
)

Definition at line 442 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, LockBuffer(), log_newpage_buffer(), MAPSIZE, MarkBufferDirty(), MemSet, PageGetContents, RelationData::rd_smgr, RelationGetRelationName, RelationNeedsWAL, RelationOpenSmgr, SMgrRelationData::smgr_vm_nblocks, smgrexists(), smgrnblocks(), smgrtruncate(), START_CRIT_SECTION, UnlockReleaseBuffer(), VISIBILITYMAP_FORKNUM, vm_readbuf(), and XLogHintBitIsNeeded.

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

443 {
444  BlockNumber newnblocks;
445 
446  /* last remaining block, byte, and bit */
447  BlockNumber truncBlock = HEAPBLK_TO_MAPBLOCK(nheapblocks);
448  uint32 truncByte = HEAPBLK_TO_MAPBYTE(nheapblocks);
449  uint8 truncOffset = HEAPBLK_TO_OFFSET(nheapblocks);
450 
451 #ifdef TRACE_VISIBILITYMAP
452  elog(DEBUG1, "vm_truncate %s %d", RelationGetRelationName(rel), nheapblocks);
453 #endif
454 
455  RelationOpenSmgr(rel);
456 
457  /*
458  * If no visibility map has been created yet for this relation, there's
459  * nothing to truncate.
460  */
462  return;
463 
464  /*
465  * Unless the new size is exactly at a visibility map page boundary, the
466  * tail bits in the last remaining map page, representing truncated heap
467  * blocks, need to be cleared. This is not only tidy, but also necessary
468  * because we don't get a chance to clear the bits if the heap is extended
469  * again.
470  */
471  if (truncByte != 0 || truncOffset != 0)
472  {
473  Buffer mapBuffer;
474  Page page;
475  char *map;
476 
477  newnblocks = truncBlock + 1;
478 
479  mapBuffer = vm_readbuf(rel, truncBlock, false);
480  if (!BufferIsValid(mapBuffer))
481  {
482  /* nothing to do, the file was already smaller */
483  return;
484  }
485 
486  page = BufferGetPage(mapBuffer);
487  map = PageGetContents(page);
488 
489  LockBuffer(mapBuffer, BUFFER_LOCK_EXCLUSIVE);
490 
491  /* NO EREPORT(ERROR) from here till changes are logged */
493 
494  /* Clear out the unwanted bytes. */
495  MemSet(&map[truncByte + 1], 0, MAPSIZE - (truncByte + 1));
496 
497  /*----
498  * Mask out the unwanted bits of the last remaining byte.
499  *
500  * ((1 << 0) - 1) = 00000000
501  * ((1 << 1) - 1) = 00000001
502  * ...
503  * ((1 << 6) - 1) = 00111111
504  * ((1 << 7) - 1) = 01111111
505  *----
506  */
507  map[truncByte] &= (1 << truncOffset) - 1;
508 
509  /*
510  * Truncation of a relation is WAL-logged at a higher-level, and we
511  * will be called at WAL replay. But if checksums are enabled, we need
512  * to still write a WAL record to protect against a torn page, if the
513  * page is flushed to disk before the truncation WAL record. We cannot
514  * use MarkBufferDirtyHint here, because that will not dirty the page
515  * during recovery.
516  */
517  MarkBufferDirty(mapBuffer);
519  log_newpage_buffer(mapBuffer, false);
520 
522 
523  UnlockReleaseBuffer(mapBuffer);
524  }
525  else
526  newnblocks = truncBlock;
527 
528  if (smgrnblocks(rel->rd_smgr, VISIBILITYMAP_FORKNUM) <= newnblocks)
529  {
530  /* nothing to do, the file was already smaller than requested size */
531  return;
532  }
533 
534  /* Truncate the unused VM pages, and send smgr inval message */
535  smgrtruncate(rel->rd_smgr, VISIBILITYMAP_FORKNUM, newnblocks);
536 
537  /*
538  * We might as well update the local smgr_vm_nblocks setting. smgrtruncate
539  * sent an smgr cache inval message, which will cause other backends to
540  * invalidate their copy of smgr_vm_nblocks, and this one too at the next
541  * command boundary. But this ensures it isn't outright wrong until then.
542  */
543  if (rel->rd_smgr)
544  rel->rd_smgr->smgr_vm_nblocks = newnblocks;
545 }
BlockNumber smgr_vm_nblocks
Definition: smgr.h:56
#define DEBUG1
Definition: elog.h:25
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1009
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1458
struct SMgrRelationData * rd_smgr
Definition: rel.h:56
bool InRecovery
Definition: xlog.c:200
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
unsigned char uint8
Definition: c.h:356
#define HEAPBLK_TO_MAPBYTE(x)
void smgrtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: smgr.c:621
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
#define MemSet(start, val, len)
Definition: c.h:955
uint32 BlockNumber
Definition: block.h:31
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:247
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:88
#define RelationOpenSmgr(relation)
Definition: rel.h:473
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3376
#define RelationGetRelationName(relation)
Definition: rel.h:450
unsigned int uint32
Definition: c.h:358
#define MAPSIZE
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define PageGetContents(page)
Definition: bufpage.h:246
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:609
#define HEAPBLK_TO_OFFSET(x)
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
#define RelationNeedsWAL(relation)
Definition: rel.h:518
#define elog(elevel,...)
Definition: elog.h:226
static Buffer vm_readbuf(Relation rel, BlockNumber blkno, bool extend)
int Buffer
Definition: buf.h:23
#define XLogHintBitIsNeeded()
Definition: xlog.h:192
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 625 of file visibilitymap.c.

References CacheInvalidateSmgr(), PGAlignedBlock::data, ExclusiveLock, InvalidBlockNumber, LockRelationForExtension(), PageInit(), PageSetChecksumInplace(), RelationData::rd_smgr, RelationOpenSmgr, SMgrRelationData::smgr_rnode, SMgrRelationData::smgr_vm_nblocks, smgrcreate(), smgrexists(), smgrextend(), smgrnblocks(), UnlockRelationForExtension(), and VISIBILITYMAP_FORKNUM.

Referenced by vm_readbuf().

626 {
627  BlockNumber vm_nblocks_now;
628  PGAlignedBlock pg;
629 
630  PageInit((Page) pg.data, BLCKSZ, 0);
631 
632  /*
633  * We use the relation extension lock to lock out other backends trying to
634  * extend the visibility map at the same time. It also locks out extension
635  * of the main fork, unnecessarily, but extending the visibility map
636  * happens seldom enough that it doesn't seem worthwhile to have a
637  * separate lock tag type for it.
638  *
639  * Note that another backend might have extended or created the relation
640  * by the time we get the lock.
641  */
643 
644  /* Might have to re-open if a cache flush happened */
645  RelationOpenSmgr(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 ((rel->rd_smgr->smgr_vm_nblocks == 0 ||
655 
656  vm_nblocks_now = smgrnblocks(rel->rd_smgr, VISIBILITYMAP_FORKNUM);
657 
658  /* Now extend the file */
659  while (vm_nblocks_now < vm_nblocks)
660  {
661  PageSetChecksumInplace((Page) pg.data, vm_nblocks_now);
662 
663  smgrextend(rel->rd_smgr, VISIBILITYMAP_FORKNUM, vm_nblocks_now,
664  pg.data, false);
665  vm_nblocks_now++;
666  }
667 
668  /*
669  * Send a shared-inval message to force other backends to close any smgr
670  * references they may have for this rel, which we are about to change.
671  * This is a useful optimization because it means that backends don't have
672  * to keep checking for creation or extension of the file, which happens
673  * infrequently.
674  */
676 
677  /* Update local cache with the up-to-date size */
678  rel->rd_smgr->smgr_vm_nblocks = vm_nblocks_now;
679 
681 }
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1369
BlockNumber smgr_vm_nblocks
Definition: smgr.h:56
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:333
#define ExclusiveLock
Definition: lockdefs.h:44
struct SMgrRelationData * rd_smgr
Definition: rel.h:56
uint32 BlockNumber
Definition: block.h:31
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:247
#define RelationOpenSmgr(relation)
Definition: rel.h:473
char data[BLCKSZ]
Definition: c.h:1060
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:402
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:452
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:609
#define InvalidBlockNumber
Definition: block.h:33
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1198
void smgrextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:537
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 554 of file visibilitymap.c.

References buf, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetPage, InvalidBlockNumber, InvalidBuffer, LockBuffer(), PageInit(), PageIsNew, RBM_ZERO_ON_ERROR, RelationData::rd_smgr, ReadBufferExtended(), RelationOpenSmgr, SMgrRelationData::smgr_vm_nblocks, smgrexists(), smgrnblocks(), VISIBILITYMAP_FORKNUM, and vm_extend().

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

555 {
556  Buffer buf;
557 
558  /*
559  * We might not have opened the relation at the smgr level yet, or we
560  * might have been forced to close it by a sinval message. The code below
561  * won't necessarily notice relation extension immediately when extend =
562  * false, so we rely on sinval messages to ensure that our ideas about the
563  * size of the map aren't too far out of date.
564  */
565  RelationOpenSmgr(rel);
566 
567  /*
568  * If we haven't cached the size of the visibility map fork yet, check it
569  * first.
570  */
572  {
576  else
577  rel->rd_smgr->smgr_vm_nblocks = 0;
578  }
579 
580  /* Handle requests beyond EOF */
581  if (blkno >= rel->rd_smgr->smgr_vm_nblocks)
582  {
583  if (extend)
584  vm_extend(rel, blkno + 1);
585  else
586  return InvalidBuffer;
587  }
588 
589  /*
590  * Use ZERO_ON_ERROR mode, and initialize the page if necessary. It's
591  * always safe to clear bits, so it's better to clear corrupt pages than
592  * error out.
593  *
594  * The initialize-the-page part is trickier than it looks, because of the
595  * possibility of multiple backends doing this concurrently, and our
596  * desire to not uselessly take the buffer lock in the normal path where
597  * the page is OK. We must take the lock to initialize the page, so
598  * recheck page newness after we have the lock, in case someone else
599  * already did it. Also, because we initially check PageIsNew with no
600  * lock, it's possible to fall through and return the buffer while someone
601  * else is still initializing the page (i.e., we might see pd_upper as set
602  * but other page header fields are still zeroes). This is harmless for
603  * callers that will take a buffer lock themselves, but some callers
604  * inspect the page without any lock at all. The latter is OK only so
605  * long as it doesn't depend on the page header having correct contents.
606  * Current usage is safe because PageGetContents() does not require that.
607  */
608  buf = ReadBufferExtended(rel, VISIBILITYMAP_FORKNUM, blkno,
609  RBM_ZERO_ON_ERROR, NULL);
610  if (PageIsNew(BufferGetPage(buf)))
611  {
613  if (PageIsNew(BufferGetPage(buf)))
614  PageInit(BufferGetPage(buf), BLCKSZ, 0);
616  }
617  return buf;
618 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
BlockNumber smgr_vm_nblocks
Definition: smgr.h:56
static void vm_extend(Relation rel, BlockNumber vm_nblocks)
struct SMgrRelationData * rd_smgr
Definition: rel.h:56
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
#define InvalidBuffer
Definition: buf.h:25
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:247
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:88
#define RelationOpenSmgr(relation)
Definition: rel.h:473
static char * buf
Definition: pg_test_fsync.c:68
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3590
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:609
#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