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

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
147 /* Must never clear all_visible bit while leaving all_frozen bit set */
150
151#ifdef TRACE_VISIBILITYMAP
152 elog(DEBUG1, "vm_clear %s %d", RelationGetRelationName(rel), heapBlk);
153#endif
154
155 if (!BufferIsValid(vmbuf) || BufferGetBlockNumber(vmbuf) != mapBlock)
156 elog(ERROR, "wrong buffer passed to visibilitymap_clear");
157
159 map = PageGetContents(BufferGetPage(vmbuf));
160
161 if (map[mapByte] & mask)
162 {
163 map[mapByte] &= ~mask;
164
165 MarkBufferDirty(vmbuf);
166 cleared = true;
167 }
168
170
171 return cleared;
172}
uint32 BlockNumber
Definition: block.h:31
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3724
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2532
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5100
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:189
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:396
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:191
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:347
static char * PageGetContents(Page page)
Definition: bufpage.h:258
uint8_t uint8
Definition: c.h:486
#define Assert(condition)
Definition: c.h:815
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define RelationGetRelationName(relation)
Definition: rel.h:546
#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 389 of file visibilitymap.c.

390{
391 BlockNumber mapBlock;
392 BlockNumber nvisible = 0;
393 BlockNumber nfrozen = 0;
394
395 /* all_visible must be specified */
396 Assert(all_visible);
397
398 for (mapBlock = 0;; mapBlock++)
399 {
400 Buffer mapBuffer;
401 uint64 *map;
402
403 /*
404 * Read till we fall off the end of the map. We assume that any extra
405 * bytes in the last page are zeroed, so we don't bother excluding
406 * them from the count.
407 */
408 mapBuffer = vm_readbuf(rel, mapBlock, false);
409 if (!BufferIsValid(mapBuffer))
410 break;
411
412 /*
413 * We choose not to lock the page, since the result is going to be
414 * immediately stale anyway if anyone is concurrently setting or
415 * clearing bits, and we only really need an approximate value.
416 */
417 map = (uint64 *) PageGetContents(BufferGetPage(mapBuffer));
418
419 nvisible += pg_popcount_masked((const char *) map, MAPSIZE, VISIBLE_MASK8);
420 if (all_frozen)
421 nfrozen += pg_popcount_masked((const char *) map, MAPSIZE, FROZEN_MASK8);
422
423 ReleaseBuffer(mapBuffer);
424 }
425
426 *all_visible = nvisible;
427 if (all_frozen)
428 *all_frozen = nfrozen;
429}
int Buffer
Definition: buf.h:23
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4866
uint64_t uint64
Definition: c.h:489
static uint64 pg_popcount_masked(const char *buf, int bytes, bits8 mask)
Definition: pg_bitutils.h:368
#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 341 of file visibilitymap.c.

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

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.

216{
217 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
218
219 return BufferIsValid(vmbuf) && BufferGetBlockNumber(vmbuf) == mapBlock;
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 443 of file visibilitymap.c.

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

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

Definition at line 246 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 uint8 status;
256
257#ifdef TRACE_VISIBILITYMAP
258 elog(DEBUG1, "vm_set %s %d", RelationGetRelationName(rel), heapBlk);
259#endif
260
263 Assert((flags & VISIBILITYMAP_VALID_BITS) == flags);
264
265 /* Must never set all_frozen bit without also setting all_visible bit */
267
268 /* Check that we have the right heap page pinned, if present */
269 if (BufferIsValid(heapBuf) && BufferGetBlockNumber(heapBuf) != heapBlk)
270 elog(ERROR, "wrong heap buffer passed to visibilitymap_set");
271
272 /* Check that we have the right VM page pinned */
273 if (!BufferIsValid(vmBuf) || BufferGetBlockNumber(vmBuf) != mapBlock)
274 elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
275
276 page = BufferGetPage(vmBuf);
277 map = (uint8 *) PageGetContents(page);
279
280 status = (map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS;
281 if (flags != status)
282 {
284
285 map[mapByte] |= (flags << mapOffset);
286 MarkBufferDirty(vmBuf);
287
288 if (RelationNeedsWAL(rel))
289 {
290 if (XLogRecPtrIsInvalid(recptr))
291 {
293 recptr = log_heap_visible(rel, heapBuf, vmBuf, cutoff_xid, flags);
294
295 /*
296 * If data checksums are enabled (or wal_log_hints=on), we
297 * need to protect the heap page from being torn.
298 *
299 * If not, then we must *not* update the heap page's LSN. In
300 * this case, the FPI for the heap page was omitted from the
301 * WAL record inserted above, so it would be incorrect to
302 * update the heap page's LSN.
303 */
305 {
306 Page heapPage = BufferGetPage(heapBuf);
307
308 PageSetLSN(heapPage, recptr);
309 }
310 }
311 PageSetLSN(page, recptr);
312 }
313
315 }
316
318 return status;
319}
static bool PageIsAllVisible(const PageData *page)
Definition: bufpage.h:429
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:391
XLogRecPtr log_heap_visible(Relation rel, Buffer heap_buffer, Buffer vm_buffer, TransactionId snapshotConflictHorizon, uint8 vmflags)
Definition: heapam.c:8710
#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().