PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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)
 
void visibilitymap_set (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)
 
BlockNumber visibilitymap_truncation_length (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 
)
extern

Definition at line 151 of file visibilitymap.c.

152{
154 int mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
155 int mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
156 uint8 mask = flags << mapOffset;
157 char *map;
158 bool cleared = false;
159
160 /* Must never clear all_visible bit while leaving all_frozen bit set */
163
164#ifdef TRACE_VISIBILITYMAP
165 elog(DEBUG1, "vm_clear %s %d", RelationGetRelationName(rel), heapBlk);
166#endif
167
169 elog(ERROR, "wrong buffer passed to visibilitymap_clear");
170
173
174 if (map[mapByte] & mask)
175 {
176 map[mapByte] &= ~mask;
177
179 cleared = true;
180 }
181
183
184 return cleared;
185}
uint32 BlockNumber
Definition block.h:31
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition bufmgr.c:4446
void MarkBufferDirty(Buffer buffer)
Definition bufmgr.c:3147
static Page BufferGetPage(Buffer buffer)
Definition bufmgr.h:468
@ BUFFER_LOCK_EXCLUSIVE
Definition bufmgr.h:222
@ BUFFER_LOCK_UNLOCK
Definition bufmgr.h:207
static void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition bufmgr.h:334
static bool BufferIsValid(Buffer bufnum)
Definition bufmgr.h:419
static char * PageGetContents(Page page)
Definition bufpage.h:282
uint8_t uint8
Definition c.h:622
#define Assert(condition)
Definition c.h:943
#define DEBUG1
Definition elog.h:31
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
static int fb(int x)
#define RelationGetRelationName(relation)
Definition rel.h:550
#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, fb(), 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_page_fix_vm_corruption(), heap_update(), heap_xlog_delete(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), and heap_xlog_update().

◆ visibilitymap_count()

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

Definition at line 367 of file visibilitymap.c.

368{
371 BlockNumber nfrozen = 0;
372
373 /* all_visible must be specified */
374 Assert(all_visible);
375
376 for (mapBlock = 0;; mapBlock++)
377 {
379 uint64 *map;
380
381 /*
382 * Read till we fall off the end of the map. We assume that any extra
383 * bytes in the last page are zeroed, so we don't bother excluding
384 * them from the count.
385 */
386 mapBuffer = vm_readbuf(rel, mapBlock, false);
388 break;
389
390 /*
391 * We choose not to lock the page, since the result is going to be
392 * immediately stale anyway if anyone is concurrently setting or
393 * clearing bits, and we only really need an approximate value.
394 */
396
397 nvisible += pg_popcount_masked((const char *) map, MAPSIZE, VISIBLE_MASK8);
398 if (all_frozen)
399 nfrozen += pg_popcount_masked((const char *) map, MAPSIZE, FROZEN_MASK8);
400
402 }
403
404 *all_visible = nvisible;
405 if (all_frozen)
406 *all_frozen = nfrozen;
407}
int Buffer
Definition buf.h:23
void ReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5586
uint64_t uint64
Definition c.h:625
static uint64 pg_popcount_masked(const char *buf, int bytes, uint8 mask)
#define MAPSIZE
#define FROZEN_MASK8
#define VISIBLE_MASK8
static Buffer vm_readbuf(Relation rel, BlockNumber blkno, bool extend)

References Assert, BufferGetPage(), BufferIsValid(), fb(), 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(), index_update_stats(), and pg_visibility_map_summary().

◆ visibilitymap_get_status()

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

Definition at line 319 of file visibilitymap.c.

320{
324 char *map;
326
327#ifdef TRACE_VISIBILITYMAP
328 elog(DEBUG1, "vm_get_status %s %d", RelationGetRelationName(rel), heapBlk);
329#endif
330
331 /* Reuse the old pinned buffer if possible */
332 if (BufferIsValid(*vmbuf))
333 {
335 {
338 }
339 }
340
341 if (!BufferIsValid(*vmbuf))
342 {
343 *vmbuf = vm_readbuf(rel, mapBlock, false);
344 if (!BufferIsValid(*vmbuf))
345 return (uint8) 0;
346 }
347
349
350 /*
351 * A single byte read is atomic. There could be memory-ordering effects
352 * here, but for performance reasons we make it the caller's job to worry
353 * about that.
354 */
356 return result;
357}
#define InvalidBuffer
Definition buf.h:25
uint32_t uint32
Definition c.h:624
uint32 result

References BufferGetBlockNumber(), BufferGetPage(), BufferIsValid(), DEBUG1, elog, fb(), HEAPBLK_TO_MAPBLOCK, HEAPBLK_TO_MAPBYTE, HEAPBLK_TO_OFFSET, InvalidBuffer, PageGetContents(), RelationGetRelationName, ReleaseBuffer(), result, VISIBILITYMAP_VALID_BITS, and vm_readbuf().

Referenced by collect_visibility_data(), find_next_unskippable_block(), heapcheck_read_stream_next_unskippable(), pg_visibility(), pg_visibility_map(), and prune_freeze_setup().

◆ visibilitymap_pin()

◆ visibilitymap_pin_ok()

bool visibilitymap_pin_ok ( BlockNumber  heapBlk,
Buffer  vmbuf 
)
extern

◆ visibilitymap_prepare_truncate()

BlockNumber visibilitymap_prepare_truncate ( Relation  rel,
BlockNumber  nheapblocks 
)
extern

Definition at line 421 of file visibilitymap.c.

422{
424
425 /* last remaining block, byte, and bit */
429
430#ifdef TRACE_VISIBILITYMAP
431 elog(DEBUG1, "vm_truncate %s %d", RelationGetRelationName(rel), nheapblocks);
432#endif
433
434 /*
435 * If no visibility map has been created yet for this relation, there's
436 * nothing to truncate.
437 */
439 return InvalidBlockNumber;
440
441 /*
442 * Unless the new size is exactly at a visibility map page boundary, the
443 * tail bits in the last remaining map page, representing truncated heap
444 * blocks, need to be cleared. This is not only tidy, but also necessary
445 * because we don't get a chance to clear the bits if the heap is extended
446 * again.
447 */
448 if (truncByte != 0 || truncOffset != 0)
449 {
451 Page page;
452 char *map;
453
455
456 mapBuffer = vm_readbuf(rel, truncBlock, false);
458 {
459 /* nothing to do, the file was already smaller */
460 return InvalidBlockNumber;
461 }
462
463 page = BufferGetPage(mapBuffer);
464 map = PageGetContents(page);
465
467
468 /* NO EREPORT(ERROR) from here till changes are logged */
470
471 /* Clear out the unwanted bytes. */
472 MemSet(&map[truncByte + 1], 0, MAPSIZE - (truncByte + 1));
473
474 /*----
475 * Mask out the unwanted bits of the last remaining byte.
476 *
477 * ((1 << 0) - 1) = 00000000
478 * ((1 << 1) - 1) = 00000001
479 * ...
480 * ((1 << 6) - 1) = 00111111
481 * ((1 << 7) - 1) = 01111111
482 *----
483 */
484 map[truncByte] &= (1 << truncOffset) - 1;
485
486 /*
487 * Truncation of a relation is WAL-logged at a higher-level, and we
488 * will be called at WAL replay. But if checksums are enabled, we need
489 * to still write a WAL record to protect against a torn page, if the
490 * page is flushed to disk before the truncation WAL record. We cannot
491 * use MarkBufferDirtyHint here, because that will not dirty the page
492 * during recovery.
493 */
497
499
501 }
502 else
504
506 {
507 /* nothing to do, the file was already smaller than requested size */
508 return InvalidBlockNumber;
509 }
510
511 return newnblocks;
512}
#define InvalidBlockNumber
Definition block.h:33
void UnlockReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5603
PageData * Page
Definition bufpage.h:81
#define MemSet(start, val, len)
Definition c.h:1107
#define START_CRIT_SECTION()
Definition miscadmin.h:152
#define END_CRIT_SECTION()
Definition miscadmin.h:154
static SMgrRelation RelationGetSmgr(Relation rel)
Definition rel.h:578
#define RelationNeedsWAL(relation)
Definition rel.h:639
@ 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:123
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
bool InRecovery
Definition xlogutils.c:50

References BUFFER_LOCK_EXCLUSIVE, BufferGetPage(), BufferIsValid(), DEBUG1, elog, END_CRIT_SECTION, fb(), 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 ( BlockNumber  heapBlk,
Buffer  vmBuf,
uint8  flags,
const RelFileLocator  rlocator 
)
extern

Definition at line 255 of file visibilitymap.c.

258{
262 Page page;
263 uint8 *map;
264 uint8 status;
265
266#ifdef TRACE_VISIBILITYMAP
267 elog(DEBUG1, "vm_set flags 0x%02X for %s %d",
268 flags,
270 heapBlk);
271#endif
272
273 /* Call in same critical section where WAL is emitted. */
275
276 /* Flags should be valid. Also never clear bits with this function */
277 Assert((flags & VISIBILITYMAP_VALID_BITS) == flags);
278
279 /* Must never set all_frozen bit without also setting all_visible bit */
281
282 /* Check that we have the right VM page pinned */
284 elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
285
287
288 page = BufferGetPage(vmBuf);
289 map = (uint8 *) PageGetContents(page);
290
291 status = (map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS;
292 if (flags != status)
293 {
294 map[mapByte] |= (flags << mapOffset);
296 }
297}
bool BufferIsLockedByMeInMode(Buffer buffer, BufferLockMode mode)
Definition bufmgr.c:3087
ProcNumber MyProcNumber
Definition globals.c:92
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
#define VISIBILITYMAP_ALL_FROZEN

References Assert, BUFFER_LOCK_EXCLUSIVE, BufferGetBlockNumber(), BufferGetPage(), BufferIsLockedByMeInMode(), BufferIsValid(), CritSectionCount, DEBUG1, elog, ERROR, fb(), 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_page_prune_and_freeze(), heap_xlog_multi_insert(), heap_xlog_prune_freeze(), lazy_scan_new_or_empty(), and lazy_vacuum_heap_page().

◆ visibilitymap_truncation_length()

BlockNumber visibilitymap_truncation_length ( BlockNumber  nheapblocks)
extern

Definition at line 524 of file visibilitymap.c.

525{
527}
#define HEAPBLK_TO_MAPBLOCK_LIMIT(x)

References fb(), and HEAPBLK_TO_MAPBLOCK_LIMIT.

Referenced by SummarizeSmgrRecord().