PostgreSQL Source Code git master
visibilitymap.h File Reference
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)
 
uint8 visibilitymap_set (Relation rel, BlockNumber heapBlk, Buffer heapBuf, XLogRecPtr recptr, Buffer vmBuf, TransactionId cutoff_xid, uint8 flags)
 
uint8 visibilitymap_set_vmbits (BlockNumber heapBlk, Buffer vmBuf, uint8 flags, const RelFileLocator rlocator)
 
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 27 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 25 of file visibilitymap.h.

Function Documentation

◆ visibilitymap_clear()

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

Definition at line 139 of file visibilitymap.c.

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
148 /* Must never clear all_visible bit while leaving all_frozen bit set */
151
152#ifdef TRACE_VISIBILITYMAP
153 elog(DEBUG1, "vm_clear %s %d", RelationGetRelationName(rel), heapBlk);
154#endif
155
156 if (!BufferIsValid(vmbuf) || BufferGetBlockNumber(vmbuf) != mapBlock)
157 elog(ERROR, "wrong buffer passed to visibilitymap_clear");
158
160 map = PageGetContents(BufferGetPage(vmbuf));
161
162 if (map[mapByte] & mask)
163 {
164 map[mapByte] &= ~mask;
165
166 MarkBufferDirty(vmbuf);
167 cleared = true;
168 }
169
171
172 return cleared;
173}
uint32 BlockNumber
Definition: block.h:31
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:4223
void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition: bufmgr.c:5604
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2943
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:436
@ BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:207
@ BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:205
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:387
static char * PageGetContents(Page page)
Definition: bufpage.h:257
uint8_t uint8
Definition: c.h:539
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
Assert(PointerIsAligned(start, uint64))
#define RelationGetRelationName(relation)
Definition: rel.h:549
#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 461 of file visibilitymap.c.

462{
463 BlockNumber mapBlock;
464 BlockNumber nvisible = 0;
465 BlockNumber nfrozen = 0;
466
467 /* all_visible must be specified */
468 Assert(all_visible);
469
470 for (mapBlock = 0;; mapBlock++)
471 {
472 Buffer mapBuffer;
473 uint64 *map;
474
475 /*
476 * Read till we fall off the end of the map. We assume that any extra
477 * bytes in the last page are zeroed, so we don't bother excluding
478 * them from the count.
479 */
480 mapBuffer = vm_readbuf(rel, mapBlock, false);
481 if (!BufferIsValid(mapBuffer))
482 break;
483
484 /*
485 * We choose not to lock the page, since the result is going to be
486 * immediately stale anyway if anyone is concurrently setting or
487 * clearing bits, and we only really need an approximate value.
488 */
489 map = (uint64 *) PageGetContents(BufferGetPage(mapBuffer));
490
491 nvisible += pg_popcount_masked((const char *) map, MAPSIZE, VISIBLE_MASK8);
492 if (all_frozen)
493 nfrozen += pg_popcount_masked((const char *) map, MAPSIZE, FROZEN_MASK8);
494
495 ReleaseBuffer(mapBuffer);
496 }
497
498 *all_visible = nvisible;
499 if (all_frozen)
500 *all_frozen = nfrozen;
501}
int Buffer
Definition: buf.h:23
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5366
uint64_t uint64
Definition: c.h:542
static uint64 pg_popcount_masked(const char *buf, int bytes, bits8 mask)
Definition: pg_bitutils.h:394
#define MAPSIZE
#define FROZEN_MASK8
#define VISIBLE_MASK8
static Buffer vm_readbuf(Relation rel, BlockNumber blkno, bool extend)

References Assert(), BufferGetPage(), BufferIsValid(), FROZEN_MASK8, MAPSIZE, PageGetContents(), pg_popcount_masked(), ReleaseBuffer(), VISIBLE_MASK8, and vm_readbuf().

Referenced by do_analyze_rel(), heap_vacuum_eager_scan_setup(), heap_vacuum_rel(), and index_update_stats().

◆ visibilitymap_get_status()

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

Definition at line 413 of file visibilitymap.c.

414{
415 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
416 uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
417 uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
418 char *map;
419 uint8 result;
420
421#ifdef TRACE_VISIBILITYMAP
422 elog(DEBUG1, "vm_get_status %s %d", RelationGetRelationName(rel), heapBlk);
423#endif
424
425 /* Reuse the old pinned buffer if possible */
426 if (BufferIsValid(*vmbuf))
427 {
428 if (BufferGetBlockNumber(*vmbuf) != mapBlock)
429 {
430 ReleaseBuffer(*vmbuf);
431 *vmbuf = InvalidBuffer;
432 }
433 }
434
435 if (!BufferIsValid(*vmbuf))
436 {
437 *vmbuf = vm_readbuf(rel, mapBlock, false);
438 if (!BufferIsValid(*vmbuf))
439 return (uint8) 0;
440 }
441
442 map = PageGetContents(BufferGetPage(*vmbuf));
443
444 /*
445 * A single byte read is atomic. There could be memory-ordering effects
446 * here, but for performance reasons we make it the caller's job to worry
447 * about that.
448 */
449 result = ((map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS);
450 return result;
451}
#define InvalidBuffer
Definition: buf.h:25
uint32_t uint32
Definition: c.h:541

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

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

217{
218 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
219
220 return BufferIsValid(vmbuf) && BufferGetBlockNumber(vmbuf) == mapBlock;
221}

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

Referenced by GetVisibilityMapPins(), and RelationGetBufferForTuple().

◆ visibilitymap_prepare_truncate()

BlockNumber visibilitymap_prepare_truncate ( Relation  rel,
BlockNumber  nheapblocks 
)

Definition at line 515 of file visibilitymap.c.

516{
517 BlockNumber newnblocks;
518
519 /* last remaining block, byte, and bit */
520 BlockNumber truncBlock = HEAPBLK_TO_MAPBLOCK(nheapblocks);
521 uint32 truncByte = HEAPBLK_TO_MAPBYTE(nheapblocks);
522 uint8 truncOffset = HEAPBLK_TO_OFFSET(nheapblocks);
523
524#ifdef TRACE_VISIBILITYMAP
525 elog(DEBUG1, "vm_truncate %s %d", RelationGetRelationName(rel), nheapblocks);
526#endif
527
528 /*
529 * If no visibility map has been created yet for this relation, there's
530 * nothing to truncate.
531 */
533 return InvalidBlockNumber;
534
535 /*
536 * Unless the new size is exactly at a visibility map page boundary, the
537 * tail bits in the last remaining map page, representing truncated heap
538 * blocks, need to be cleared. This is not only tidy, but also necessary
539 * because we don't get a chance to clear the bits if the heap is extended
540 * again.
541 */
542 if (truncByte != 0 || truncOffset != 0)
543 {
544 Buffer mapBuffer;
545 Page page;
546 char *map;
547
548 newnblocks = truncBlock + 1;
549
550 mapBuffer = vm_readbuf(rel, truncBlock, false);
551 if (!BufferIsValid(mapBuffer))
552 {
553 /* nothing to do, the file was already smaller */
554 return InvalidBlockNumber;
555 }
556
557 page = BufferGetPage(mapBuffer);
558 map = PageGetContents(page);
559
561
562 /* NO EREPORT(ERROR) from here till changes are logged */
564
565 /* Clear out the unwanted bytes. */
566 MemSet(&map[truncByte + 1], 0, MAPSIZE - (truncByte + 1));
567
568 /*----
569 * Mask out the unwanted bits of the last remaining byte.
570 *
571 * ((1 << 0) - 1) = 00000000
572 * ((1 << 1) - 1) = 00000001
573 * ...
574 * ((1 << 6) - 1) = 00111111
575 * ((1 << 7) - 1) = 01111111
576 *----
577 */
578 map[truncByte] &= (1 << truncOffset) - 1;
579
580 /*
581 * Truncation of a relation is WAL-logged at a higher-level, and we
582 * will be called at WAL replay. But if checksums are enabled, we need
583 * to still write a WAL record to protect against a torn page, if the
584 * page is flushed to disk before the truncation WAL record. We cannot
585 * use MarkBufferDirtyHint here, because that will not dirty the page
586 * during recovery.
587 */
588 MarkBufferDirty(mapBuffer);
590 log_newpage_buffer(mapBuffer, false);
591
593
594 UnlockReleaseBuffer(mapBuffer);
595 }
596 else
597 newnblocks = truncBlock;
598
599 if (smgrnblocks(RelationGetSmgr(rel), VISIBILITYMAP_FORKNUM) <= newnblocks)
600 {
601 /* nothing to do, the file was already smaller than requested size */
602 return InvalidBlockNumber;
603 }
604
605 return newnblocks;
606}
#define InvalidBlockNumber
Definition: block.h:33
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5383
PageData * Page
Definition: bufpage.h:81
#define MemSet(start, val, len)
Definition: c.h:1022
#define START_CRIT_SECTION()
Definition: miscadmin.h:150
#define END_CRIT_SECTION()
Definition: miscadmin.h:152
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:577
#define RelationNeedsWAL(relation)
Definition: rel.h:638
@ VISIBILITYMAP_FORKNUM
Definition: relpath.h:60
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:819
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:462
#define XLogHintBitIsNeeded()
Definition: xlog.h:120
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1259
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()

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

Definition at line 247 of file visibilitymap.c.

250{
251 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
252 uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
253 uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
254 Page page;
255 uint8 *map;
256 uint8 status;
257
258#ifdef TRACE_VISIBILITYMAP
259 elog(DEBUG1, "vm_set flags 0x%02X for %s %d",
260 flags, RelationGetRelationName(rel), heapBlk);
261#endif
262
265 Assert((flags & VISIBILITYMAP_VALID_BITS) == flags);
266
267 /* Must never set all_frozen bit without also setting all_visible bit */
269
270 /* Check that we have the right heap page pinned, if present */
271 if (BufferIsValid(heapBuf) && BufferGetBlockNumber(heapBuf) != heapBlk)
272 elog(ERROR, "wrong heap buffer passed to visibilitymap_set");
273
274 Assert(!BufferIsValid(heapBuf) ||
276
277 /* Check that we have the right VM page pinned */
278 if (!BufferIsValid(vmBuf) || BufferGetBlockNumber(vmBuf) != mapBlock)
279 elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
280
281 page = BufferGetPage(vmBuf);
282 map = (uint8 *) PageGetContents(page);
284
285 status = (map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS;
286 if (flags != status)
287 {
289
290 map[mapByte] |= (flags << mapOffset);
291 MarkBufferDirty(vmBuf);
292
293 if (RelationNeedsWAL(rel))
294 {
295 if (!XLogRecPtrIsValid(recptr))
296 {
298 recptr = log_heap_visible(rel, heapBuf, vmBuf, cutoff_xid, flags);
299
300 /*
301 * If data checksums are enabled (or wal_log_hints=on), we
302 * need to protect the heap page from being torn.
303 *
304 * If not, then we must *not* update the heap page's LSN. In
305 * this case, the FPI for the heap page was omitted from the
306 * WAL record inserted above, so it would be incorrect to
307 * update the heap page's LSN.
308 */
310 {
311 Page heapPage = BufferGetPage(heapBuf);
312
313 PageSetLSN(heapPage, recptr);
314 }
315 }
316 PageSetLSN(page, recptr);
317 }
318
320 }
321
323 return status;
324}
bool BufferIsLockedByMeInMode(Buffer buffer, BufferLockMode mode)
Definition: bufmgr.c:2869
static bool PageIsAllVisible(const PageData *page)
Definition: bufpage.h:428
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:390
XLogRecPtr log_heap_visible(Relation rel, Buffer heap_buffer, Buffer vm_buffer, TransactionId snapshotConflictHorizon, uint8 vmflags)
Definition: heapam.c:8830
#define VISIBILITYMAP_ALL_FROZEN
#define XLogRecPtrIsValid(r)
Definition: xlogdefs.h:29

References Assert(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage(), BufferIsLockedByMeInMode(), 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 XLogRecPtrIsValid.

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

◆ visibilitymap_set_vmbits()

uint8 visibilitymap_set_vmbits ( BlockNumber  heapBlk,
Buffer  vmBuf,
uint8  flags,
const RelFileLocator  rlocator 
)

Definition at line 347 of file visibilitymap.c.

350{
351 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
352 uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
353 uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
354 Page page;
355 uint8 *map;
356 uint8 status;
357
358#ifdef TRACE_VISIBILITYMAP
359 elog(DEBUG1, "vm_set flags 0x%02X for %s %d",
360 flags,
362 heapBlk);
363#endif
364
365 /* Call in same critical section where WAL is emitted. */
367
368 /* Flags should be valid. Also never clear bits with this function */
369 Assert((flags & VISIBILITYMAP_VALID_BITS) == flags);
370
371 /* Must never set all_frozen bit without also setting all_visible bit */
373
374 /* Check that we have the right VM page pinned */
375 if (!BufferIsValid(vmBuf) || BufferGetBlockNumber(vmBuf) != mapBlock)
376 elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
377
379
380 page = BufferGetPage(vmBuf);
381 map = (uint8 *) PageGetContents(page);
382
383 status = (map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS;
384 if (flags != status)
385 {
386 map[mapByte] |= (flags << mapOffset);
387 MarkBufferDirty(vmBuf);
388 }
389
390 return status;
391}
ProcNumber MyProcNumber
Definition: globals.c:90
volatile uint32 CritSectionCount
Definition: globals.c:45
const char * str
@ MAIN_FORKNUM
Definition: relpath.h:58
#define relpathbackend(rlocator, backend, forknum)
Definition: relpath.h:141

References Assert(), BUFFER_LOCK_EXCLUSIVE, BufferGetBlockNumber(), BufferGetPage(), BufferIsLockedByMeInMode(), BufferIsValid(), CritSectionCount, DEBUG1, elog, ERROR, HEAPBLK_TO_MAPBLOCK, HEAPBLK_TO_MAPBYTE, HEAPBLK_TO_OFFSET, InRecovery, MAIN_FORKNUM, MarkBufferDirty(), MyProcNumber, PageGetContents(), relpathbackend, str, VISIBILITYMAP_ALL_FROZEN, and VISIBILITYMAP_VALID_BITS.

Referenced by heap_multi_insert(), heap_xlog_multi_insert(), heap_xlog_prune_freeze(), and lazy_vacuum_heap_page().