PostgreSQL Source Code git master
Loading...
Searching...
No Matches
visibilitymap.c File Reference
#include "postgres.h"
#include "access/heapam_xlog.h"
#include "access/visibilitymap.h"
#include "access/xloginsert.h"
#include "access/xlogutils.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "storage/bufmgr.h"
#include "storage/smgr.h"
#include "utils/inval.h"
#include "utils/rel.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_MAPBLOCK_LIMIT(x)    (((x) + HEAPBLOCKS_PER_PAGE - 1) / 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_MASK8   (0x55) /* The lower bit of each bit pair */
 
#define FROZEN_MASK8   (0xaa) /* The upper bit of each bit pair */
 

Functions

static Buffer vm_readbuf (Relation rel, BlockNumber blkno, bool extend)
 
static Buffer vm_extend (Relation rel, BlockNumber vm_nblocks)
 
bool visibilitymap_clear (RelFileLocator rlocator, 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, 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

◆ FROZEN_MASK8

#define FROZEN_MASK8   (0xaa) /* The upper bit of each bit pair */

Definition at line 136 of file visibilitymap.c.

◆ HEAPBLK_TO_MAPBLOCK

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

Definition at line 128 of file visibilitymap.c.

◆ HEAPBLK_TO_MAPBLOCK_LIMIT

#define HEAPBLK_TO_MAPBLOCK_LIMIT (   x)     (((x) + HEAPBLOCKS_PER_PAGE - 1) / HEAPBLOCKS_PER_PAGE)

Definition at line 129 of file visibilitymap.c.

154{
155 int mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
156 int mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
158 uint8 mask = flags << mapOffset;
159 Page page;
160 char *map;
161 bool cleared = false;
162
163 /* Must never clear all_visible bit while leaving all_frozen bit set */
166
167#ifdef TRACE_VISIBILITYMAP
168 elog(DEBUG1, "vm_clear %s %d",
170 heapBlk);
171#endif
172
174 elog(ERROR, "wrong buffer passed to visibilitymap_clear");
175
177
178 page = BufferGetPage(vmbuf);
179 map = PageGetContents(page);
180
181 if (map[mapByte] & mask)
182 {
183 map[mapByte] &= ~mask;
184
186 cleared = true;
187 }
188
189 return cleared;
190}
191
192/*
193 * visibilitymap_pin - pin a map page for setting a bit
194 *
195 * Setting a bit in the visibility map is a two-phase operation. First, call
196 * visibilitymap_pin, to pin the visibility map page containing the bit for
197 * the heap page. Because that can require I/O to read the map page, you
198 * shouldn't hold a lock on the heap page while doing that. Then, call
199 * visibilitymap_set to actually set the bit.
200 *
201 * On entry, *vmbuf should be InvalidBuffer or a valid buffer returned by
202 * an earlier call to visibilitymap_pin or visibilitymap_get_status on the same
203 * relation. On return, *vmbuf is a valid buffer with the map page containing
204 * the bit for heapBlk.
205 *
206 * If the page doesn't exist in the map file yet, it is extended.
207 */
208void
210{
212
213 /* Reuse the old pinned buffer if possible */
214 if (BufferIsValid(*vmbuf))
215 {
217 return;
218
220 }
221 *vmbuf = vm_readbuf(rel, mapBlock, true);
222}
223
224/*
225 * visibilitymap_pin_ok - do we already have the correct page pinned?
226 *
227 * On entry, vmbuf should be InvalidBuffer or a valid buffer returned by
228 * an earlier call to visibilitymap_pin or visibilitymap_get_status on the same
229 * relation. The return value indicates whether the buffer covers the
230 * given heapBlk.
231 */
232bool
234{
236
238}
239
240/*
241 * Set VM (visibility map) flags in the VM block in vmBuf.
242 *
243 * This function is intended for callers that log VM changes together
244 * with the heap page modifications that rendered the page all-visible.
245 *
246 * vmBuf must be pinned and exclusively locked, and it must cover the VM bits
247 * corresponding to heapBlk.
248 *
249 * In normal operation (not recovery), this must be called inside a critical
250 * section that also applies the necessary heap page changes and, if
251 * applicable, emits WAL.
252 *
253 * The caller is responsible for ensuring consistency between the heap page
254 * and the VM page by holding a pin and exclusive lock on the buffer
255 * containing heapBlk.
256 *
257 * rlocator is used only for debugging messages.
258 */
259void
261 Buffer vmBuf, uint8 flags,
262 RelFileLocator rlocator)
263{
267 Page page;
268 uint8 *map;
269 uint8 status;
270
271#ifdef TRACE_VISIBILITYMAP
272 elog(DEBUG1, "vm_set flags 0x%02X for %s %d",
273 flags,
275 heapBlk);
276#endif
277
278 /* Call in same critical section where WAL is emitted. */
280
281 /* Flags should be valid. Also never clear bits with this function */
282 Assert((flags & VISIBILITYMAP_VALID_BITS) == flags);
283
284 /* Must never set all_frozen bit without also setting all_visible bit */
286
287 /* Check that we have the right VM page pinned */
289 elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
290
292
293 page = BufferGetPage(vmBuf);
294 map = (uint8 *) PageGetContents(page);
295
296 status = (map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS;
297 if (flags != status)
298 {
299 map[mapByte] |= (flags << mapOffset);
301 }
302}
303
304/*
305 * visibilitymap_get_status - get status of bits
306 *
307 * Are all tuples on heapBlk visible to all or are marked frozen, according
308 * to the visibility map?
309 *
310 * On entry, *vmbuf should be InvalidBuffer or a valid buffer returned by an
311 * earlier call to visibilitymap_pin or visibilitymap_get_status on the same
312 * relation. On return, *vmbuf is a valid buffer with the map page containing
313 * the bit for heapBlk, or InvalidBuffer. The caller is responsible for
314 * releasing *vmbuf after it's done testing and setting bits.
315 *
316 * NOTE: This function is typically called without a lock on the heap page,
317 * so somebody else could change the bit just after we look at it. In fact,
318 * since we don't lock the visibility map page either, it's even possible that
319 * someone else could have changed the bit just before we look at it, but yet
320 * we might see the old value. It is the caller's responsibility to deal with
321 * all concurrency issues!
322 */
323uint8
325{
329 char *map;
331
332#ifdef TRACE_VISIBILITYMAP
333 elog(DEBUG1, "vm_get_status %s %d", RelationGetRelationName(rel), heapBlk);
334#endif
335
336 /* Reuse the old pinned buffer if possible */
337 if (BufferIsValid(*vmbuf))
338 {
340 {
343 }
344 }
345
346 if (!BufferIsValid(*vmbuf))
347 {
348 *vmbuf = vm_readbuf(rel, mapBlock, false);
349 if (!BufferIsValid(*vmbuf))
350 return (uint8) 0;
351 }
352
354
355 /*
356 * A single byte read is atomic. There could be memory-ordering effects
357 * here, but for performance reasons we make it the caller's job to worry
358 * about that.
359 */
361 return result;
362}
363
364/*
365 * visibilitymap_count - count number of bits set in visibility map
366 *
367 * Note: we ignore the possibility of race conditions when the table is being
368 * extended concurrently with the call. New pages added to the table aren't
369 * going to be marked all-visible or all-frozen, so they won't affect the result.
370 */
371void
372visibilitymap_count(Relation rel, BlockNumber *all_visible, BlockNumber *all_frozen)
373{
376 BlockNumber nfrozen = 0;
377
378 /* all_visible must be specified */
379 Assert(all_visible);
380
381 for (mapBlock = 0;; mapBlock++)
382 {
384 uint64 *map;
385
386 /*
387 * Read till we fall off the end of the map. We assume that any extra
388 * bytes in the last page are zeroed, so we don't bother excluding
389 * them from the count.
390 */
391 mapBuffer = vm_readbuf(rel, mapBlock, false);
393 break;
394
395 /*
396 * We choose not to lock the page, since the result is going to be
397 * immediately stale anyway if anyone is concurrently setting or
398 * clearing bits, and we only really need an approximate value.
399 */
401
402 nvisible += pg_popcount_masked((const char *) map, MAPSIZE, VISIBLE_MASK8);
403 if (all_frozen)
404 nfrozen += pg_popcount_masked((const char *) map, MAPSIZE, FROZEN_MASK8);
405
407 }
408
409 *all_visible = nvisible;
410 if (all_frozen)
411 *all_frozen = nfrozen;
412}
413
414/*
415 * visibilitymap_prepare_truncate -
416 * prepare for truncation of the visibility map
417 *
418 * nheapblocks is the new size of the heap.
419 *
420 * Return the number of blocks of new visibility map.
421 * If it's InvalidBlockNumber, there is nothing to truncate;
422 * otherwise the caller is responsible for calling smgrtruncate()
423 * to truncate the visibility map pages.
424 */
427{
429
430 /* last remaining block, byte, and bit */
434
435#ifdef TRACE_VISIBILITYMAP
436 elog(DEBUG1, "vm_truncate %s %d", RelationGetRelationName(rel), nheapblocks);
437#endif
438
439 /*
440 * If no visibility map has been created yet for this relation, there's
441 * nothing to truncate.
442 */
444 return InvalidBlockNumber;
445
446 /*
447 * Unless the new size is exactly at a visibility map page boundary, the
448 * tail bits in the last remaining map page, representing truncated heap
449 * blocks, need to be cleared. This is not only tidy, but also necessary
450 * because we don't get a chance to clear the bits if the heap is extended
451 * again.
452 */
453 if (truncByte != 0 || truncOffset != 0)
454 {
456 Page page;
457 char *map;
458
460
461 mapBuffer = vm_readbuf(rel, truncBlock, false);
463 {
464 /* nothing to do, the file was already smaller */
465 return InvalidBlockNumber;
466 }
467
468 page = BufferGetPage(mapBuffer);
469 map = PageGetContents(page);
470
472
473 /* NO EREPORT(ERROR) from here till changes are logged */
475
476 /* Clear out the unwanted bytes. */
477 MemSet(&map[truncByte + 1], 0, MAPSIZE - (truncByte + 1));
478
479 /*----
480 * Mask out the unwanted bits of the last remaining byte.
481 *
482 * ((1 << 0) - 1) = 00000000
483 * ((1 << 1) - 1) = 00000001
484 * ...
485 * ((1 << 6) - 1) = 00111111
486 * ((1 << 7) - 1) = 01111111
487 *----
488 */
489 map[truncByte] &= (1 << truncOffset) - 1;
490
491 /*
492 * Truncation of a relation is WAL-logged at a higher-level, and we
493 * will be called at WAL replay. But if checksums are enabled, we need
494 * to still write a WAL record to protect against a torn page, if the
495 * page is flushed to disk before the truncation WAL record. We cannot
496 * use MarkBufferDirtyHint here, because that will not dirty the page
497 * during recovery.
498 */
502
504
506 }
507 else
509
511 {
512 /* nothing to do, the file was already smaller than requested size */
513 return InvalidBlockNumber;
514 }
515
516 return newnblocks;
517}
518
519/*
520 * visibilitymap_truncation_length -
521 * compute truncation length for visibility map
522 *
523 * Given a proposed truncation length for the main fork, compute the
524 * correct truncation length for the visibility map. Should return the
525 * same answer as visibilitymap_prepare_truncate(), but without modifying
526 * anything.
527 */
530{
532}
533
534/*
535 * Read a visibility map page.
536 *
537 * If the page doesn't exist, InvalidBuffer is returned, or if 'extend' is
538 * true, the visibility map file is extended.
539 */
540static Buffer
541vm_readbuf(Relation rel, BlockNumber blkno, bool extend)
542{
543 Buffer buf;
545
546 /*
547 * Caution: re-using this smgr pointer could fail if the relcache entry
548 * gets closed. It's safe as long as we only do smgr-level operations
549 * between here and the last use of the pointer.
550 */
551 reln = RelationGetSmgr(rel);
552
553 /*
554 * If we haven't cached the size of the visibility map fork yet, check it
555 * first.
556 */
557 if (reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] == InvalidBlockNumber)
558 {
561 else
562 reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] = 0;
563 }
564
565 /*
566 * For reading we use ZERO_ON_ERROR mode, and initialize the page if
567 * necessary. It's always safe to clear bits, so it's better to clear
568 * corrupt pages than error out.
569 *
570 * We use the same path below to initialize pages when extending the
571 * relation, as a concurrent extension can end up with vm_extend()
572 * returning an already-initialized page.
573 */
574 if (blkno >= reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM])
575 {
576 if (extend)
577 buf = vm_extend(rel, blkno + 1);
578 else
579 return InvalidBuffer;
580 }
581 else
584
585 /*
586 * Initializing the page when needed is trickier than it looks, because of
587 * the possibility of multiple backends doing this concurrently, and our
588 * desire to not uselessly take the buffer lock in the normal path where
589 * the page is OK. We must take the lock to initialize the page, so
590 * recheck page newness after we have the lock, in case someone else
591 * already did it. Also, because we initially check PageIsNew with no
592 * lock, it's possible to fall through and return the buffer while someone
593 * else is still initializing the page (i.e., we might see pd_upper as set
594 * but other page header fields are still zeroes). This is harmless for
595 * callers that will take a buffer lock themselves, but some callers
596 * inspect the page without any lock at all. The latter is OK only so
597 * long as it doesn't depend on the page header having correct contents.
598 * Current usage is safe because PageGetContents() does not require that.
599 */
601 {
606 }
607 return buf;
608}
609
610/*
611 * Ensure that the visibility map fork is at least vm_nblocks long, extending
612 * it if necessary with zeroed pages.
613 */
614static Buffer
616{
617 Buffer buf;
618
624
625 /*
626 * Send a shared-inval message to force other backends to close any smgr
627 * references they may have for this rel, which we are about to change.
628 * This is a useful optimization because it means that backends don't have
629 * to keep checking for creation or extension of the file, which happens
630 * infrequently.
631 */
632 CacheInvalidateSmgr(RelationGetSmgr(rel)->smgr_rlocator);
633
634 return buf;
635}
uint32 BlockNumber
Definition block.h:31
#define InvalidBlockNumber
Definition block.h:33
int Buffer
Definition buf.h:23
#define InvalidBuffer
Definition buf.h:25
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition bufmgr.c:4469
bool BufferIsLockedByMeInMode(Buffer buffer, BufferLockMode mode)
Definition bufmgr.c:3110
Buffer ExtendBufferedRelTo(BufferManagerRelation bmr, ForkNumber fork, BufferAccessStrategy strategy, uint32 flags, BlockNumber extend_to, ReadBufferMode mode)
Definition bufmgr.c:1031
void ReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5609
void UnlockReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5626
void MarkBufferDirty(Buffer buffer)
Definition bufmgr.c:3170
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition bufmgr.c:926
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
@ EB_CLEAR_SIZE_CACHE
Definition bufmgr.h:90
@ EB_CREATE_FORK_IF_NEEDED
Definition bufmgr.h:84
@ RBM_ZERO_ON_ERROR
Definition bufmgr.h:51
#define BMR_REL(p_rel)
Definition bufmgr.h:114
static bool BufferIsValid(Buffer bufnum)
Definition bufmgr.h:419
void PageInit(Page page, Size pageSize, Size specialSize)
Definition bufpage.c:42
static bool PageIsNew(const PageData *page)
Definition bufpage.h:258
static char * PageGetContents(Page page)
Definition bufpage.h:282
PageData * Page
Definition bufpage.h:81
uint8_t uint8
Definition c.h:681
#define Assert(condition)
Definition c.h:1002
uint64_t uint64
Definition c.h:684
uint32_t uint32
Definition c.h:683
#define MemSet(start, val, len)
Definition c.h:1147
uint32 result
#define DEBUG1
Definition elog.h:31
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
ProcNumber MyProcNumber
Definition globals.c:92
volatile uint32 CritSectionCount
Definition globals.c:45
const char * str
void CacheInvalidateSmgr(RelFileLocatorBackend rlocator)
Definition inval.c:1752
#define START_CRIT_SECTION()
Definition miscadmin.h:152
#define END_CRIT_SECTION()
Definition miscadmin.h:154
static uint64 pg_popcount_masked(const char *buf, int bytes, uint8 mask)
static char buf[DEFAULT_XLOG_SEG_SIZE]
static int fb(int x)
static SMgrRelation RelationGetSmgr(Relation rel)
Definition rel.h:578
#define RelationGetRelationName(relation)
Definition rel.h:550
#define RelationNeedsWAL(relation)
Definition rel.h:639
@ VISIBILITYMAP_FORKNUM
Definition relpath.h:60
@ MAIN_FORKNUM
Definition relpath.h:58
#define relpathbackend(rlocator, backend, forknum)
Definition relpath.h:141
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition smgr.c:819
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition smgr.c:462
#define MAPSIZE
BlockNumber visibilitymap_truncation_length(BlockNumber nheapblocks)
#define HEAPBLK_TO_MAPBLOCK_LIMIT(x)
#define FROZEN_MASK8
bool visibilitymap_pin_ok(BlockNumber heapBlk, Buffer vmbuf)
#define VISIBLE_MASK8
#define HEAPBLK_TO_OFFSET(x)
void visibilitymap_pin(Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
void visibilitymap_set(BlockNumber heapBlk, Buffer vmBuf, uint8 flags, RelFileLocator rlocator)
uint8 visibilitymap_get_status(Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
static Buffer vm_extend(Relation rel, BlockNumber vm_nblocks)
BlockNumber visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
void visibilitymap_count(Relation rel, BlockNumber *all_visible, BlockNumber *all_frozen)
static Buffer vm_readbuf(Relation rel, BlockNumber blkno, bool extend)
#define HEAPBLK_TO_MAPBLOCK(x)
#define HEAPBLK_TO_MAPBYTE(x)
#define VISIBILITYMAP_VALID_BITS
#define VISIBILITYMAP_ALL_FROZEN
#define VISIBILITYMAP_ALL_VISIBLE
#define XLogHintBitIsNeeded()
Definition xlog.h:123
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
bool InRecovery
Definition xlogutils.c:50

◆ HEAPBLK_TO_MAPBYTE

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

Definition at line 131 of file visibilitymap.c.

◆ HEAPBLK_TO_OFFSET

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

Definition at line 132 of file visibilitymap.c.

◆ HEAPBLOCKS_PER_BYTE

#define HEAPBLOCKS_PER_BYTE   (BITS_PER_BYTE / BITS_PER_HEAPBLOCK)

Definition at line 122 of file visibilitymap.c.

◆ HEAPBLOCKS_PER_PAGE

#define HEAPBLOCKS_PER_PAGE   (MAPSIZE * HEAPBLOCKS_PER_BYTE)

Definition at line 125 of file visibilitymap.c.

◆ MAPSIZE

Definition at line 119 of file visibilitymap.c.

◆ VISIBLE_MASK8

#define VISIBLE_MASK8   (0x55) /* The lower bit of each bit pair */

Definition at line 135 of file visibilitymap.c.

Function Documentation

◆ visibilitymap_clear()

bool visibilitymap_clear ( RelFileLocator  rlocator,
BlockNumber  heapBlk,
Buffer  vmbuf,
uint8  flags 
)

Definition at line 152 of file visibilitymap.c.

154{
155 int mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
156 int mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
158 uint8 mask = flags << mapOffset;
159 Page page;
160 char *map;
161 bool cleared = false;
162
163 /* Must never clear all_visible bit while leaving all_frozen bit set */
166
167#ifdef TRACE_VISIBILITYMAP
168 elog(DEBUG1, "vm_clear %s %d",
170 heapBlk);
171#endif
172
174 elog(ERROR, "wrong buffer passed to visibilitymap_clear");
175
177
178 page = BufferGetPage(vmbuf);
179 map = PageGetContents(page);
180
181 if (map[mapByte] & mask)
182 {
183 map[mapByte] &= ~mask;
184
186 cleared = true;
187 }
188
189 return cleared;
190}

References Assert, BUFFER_LOCK_EXCLUSIVE, BufferGetBlockNumber(), BufferGetPage(), BufferIsLockedByMeInMode(), BufferIsValid(), DEBUG1, elog, ERROR, fb(), HEAPBLK_TO_MAPBLOCK, HEAPBLK_TO_MAPBYTE, HEAPBLK_TO_OFFSET, MAIN_FORKNUM, MarkBufferDirty(), MyProcNumber, PageGetContents(), relpathbackend, str, 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_update(), and heap_xlog_vm_clear().

◆ visibilitymap_count()

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

Definition at line 372 of file visibilitymap.c.

373{
376 BlockNumber nfrozen = 0;
377
378 /* all_visible must be specified */
379 Assert(all_visible);
380
381 for (mapBlock = 0;; mapBlock++)
382 {
384 uint64 *map;
385
386 /*
387 * Read till we fall off the end of the map. We assume that any extra
388 * bytes in the last page are zeroed, so we don't bother excluding
389 * them from the count.
390 */
391 mapBuffer = vm_readbuf(rel, mapBlock, false);
393 break;
394
395 /*
396 * We choose not to lock the page, since the result is going to be
397 * immediately stale anyway if anyone is concurrently setting or
398 * clearing bits, and we only really need an approximate value.
399 */
401
402 nvisible += pg_popcount_masked((const char *) map, MAPSIZE, VISIBLE_MASK8);
403 if (all_frozen)
404 nfrozen += pg_popcount_masked((const char *) map, MAPSIZE, FROZEN_MASK8);
405
407 }
408
409 *all_visible = nvisible;
410 if (all_frozen)
411 *all_frozen = nfrozen;
412}

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 
)

Definition at line 324 of file visibilitymap.c.

325{
329 char *map;
331
332#ifdef TRACE_VISIBILITYMAP
333 elog(DEBUG1, "vm_get_status %s %d", RelationGetRelationName(rel), heapBlk);
334#endif
335
336 /* Reuse the old pinned buffer if possible */
337 if (BufferIsValid(*vmbuf))
338 {
340 {
343 }
344 }
345
346 if (!BufferIsValid(*vmbuf))
347 {
348 *vmbuf = vm_readbuf(rel, mapBlock, false);
349 if (!BufferIsValid(*vmbuf))
350 return (uint8) 0;
351 }
352
354
355 /*
356 * A single byte read is atomic. There could be memory-ordering effects
357 * here, but for performance reasons we make it the caller's job to worry
358 * about that.
359 */
361 return result;
362}

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

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

◆ visibilitymap_pin_ok()

bool visibilitymap_pin_ok ( BlockNumber  heapBlk,
Buffer  vmbuf 
)

◆ visibilitymap_prepare_truncate()

BlockNumber visibilitymap_prepare_truncate ( Relation  rel,
BlockNumber  nheapblocks 
)

Definition at line 426 of file visibilitymap.c.

427{
429
430 /* last remaining block, byte, and bit */
434
435#ifdef TRACE_VISIBILITYMAP
436 elog(DEBUG1, "vm_truncate %s %d", RelationGetRelationName(rel), nheapblocks);
437#endif
438
439 /*
440 * If no visibility map has been created yet for this relation, there's
441 * nothing to truncate.
442 */
444 return InvalidBlockNumber;
445
446 /*
447 * Unless the new size is exactly at a visibility map page boundary, the
448 * tail bits in the last remaining map page, representing truncated heap
449 * blocks, need to be cleared. This is not only tidy, but also necessary
450 * because we don't get a chance to clear the bits if the heap is extended
451 * again.
452 */
453 if (truncByte != 0 || truncOffset != 0)
454 {
456 Page page;
457 char *map;
458
460
461 mapBuffer = vm_readbuf(rel, truncBlock, false);
463 {
464 /* nothing to do, the file was already smaller */
465 return InvalidBlockNumber;
466 }
467
468 page = BufferGetPage(mapBuffer);
469 map = PageGetContents(page);
470
472
473 /* NO EREPORT(ERROR) from here till changes are logged */
475
476 /* Clear out the unwanted bytes. */
477 MemSet(&map[truncByte + 1], 0, MAPSIZE - (truncByte + 1));
478
479 /*----
480 * Mask out the unwanted bits of the last remaining byte.
481 *
482 * ((1 << 0) - 1) = 00000000
483 * ((1 << 1) - 1) = 00000001
484 * ...
485 * ((1 << 6) - 1) = 00111111
486 * ((1 << 7) - 1) = 01111111
487 *----
488 */
489 map[truncByte] &= (1 << truncOffset) - 1;
490
491 /*
492 * Truncation of a relation is WAL-logged at a higher-level, and we
493 * will be called at WAL replay. But if checksums are enabled, we need
494 * to still write a WAL record to protect against a torn page, if the
495 * page is flushed to disk before the truncation WAL record. We cannot
496 * use MarkBufferDirtyHint here, because that will not dirty the page
497 * during recovery.
498 */
502
504
506 }
507 else
509
511 {
512 /* nothing to do, the file was already smaller than requested size */
513 return InvalidBlockNumber;
514 }
515
516 return newnblocks;
517}

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,
RelFileLocator  rlocator 
)

Definition at line 260 of file visibilitymap.c.

263{
267 Page page;
268 uint8 *map;
269 uint8 status;
270
271#ifdef TRACE_VISIBILITYMAP
272 elog(DEBUG1, "vm_set flags 0x%02X for %s %d",
273 flags,
275 heapBlk);
276#endif
277
278 /* Call in same critical section where WAL is emitted. */
280
281 /* Flags should be valid. Also never clear bits with this function */
282 Assert((flags & VISIBILITYMAP_VALID_BITS) == flags);
283
284 /* Must never set all_frozen bit without also setting all_visible bit */
286
287 /* Check that we have the right VM page pinned */
289 elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
290
292
293 page = BufferGetPage(vmBuf);
294 map = (uint8 *) PageGetContents(page);
295
296 status = (map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS;
297 if (flags != status)
298 {
299 map[mapByte] |= (flags << mapOffset);
301 }
302}

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)

Definition at line 529 of file visibilitymap.c.

530{
532}

References fb(), and HEAPBLK_TO_MAPBLOCK_LIMIT.

Referenced by SummarizeSmgrRecord().

◆ vm_extend()

static Buffer vm_extend ( Relation  rel,
BlockNumber  vm_nblocks 
)
static

Definition at line 615 of file visibilitymap.c.

616{
617 Buffer buf;
618
624
625 /*
626 * Send a shared-inval message to force other backends to close any smgr
627 * references they may have for this rel, which we are about to change.
628 * This is a useful optimization because it means that backends don't have
629 * to keep checking for creation or extension of the file, which happens
630 * infrequently.
631 */
632 CacheInvalidateSmgr(RelationGetSmgr(rel)->smgr_rlocator);
633
634 return buf;
635}

References BMR_REL, buf, CacheInvalidateSmgr(), EB_CLEAR_SIZE_CACHE, EB_CREATE_FORK_IF_NEEDED, ExtendBufferedRelTo(), fb(), RBM_ZERO_ON_ERROR, RelationGetSmgr(), and VISIBILITYMAP_FORKNUM.

Referenced by vm_readbuf().

◆ vm_readbuf()

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

Definition at line 541 of file visibilitymap.c.

542{
543 Buffer buf;
545
546 /*
547 * Caution: re-using this smgr pointer could fail if the relcache entry
548 * gets closed. It's safe as long as we only do smgr-level operations
549 * between here and the last use of the pointer.
550 */
551 reln = RelationGetSmgr(rel);
552
553 /*
554 * If we haven't cached the size of the visibility map fork yet, check it
555 * first.
556 */
557 if (reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] == InvalidBlockNumber)
558 {
561 else
562 reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] = 0;
563 }
564
565 /*
566 * For reading we use ZERO_ON_ERROR mode, and initialize the page if
567 * necessary. It's always safe to clear bits, so it's better to clear
568 * corrupt pages than error out.
569 *
570 * We use the same path below to initialize pages when extending the
571 * relation, as a concurrent extension can end up with vm_extend()
572 * returning an already-initialized page.
573 */
574 if (blkno >= reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM])
575 {
576 if (extend)
577 buf = vm_extend(rel, blkno + 1);
578 else
579 return InvalidBuffer;
580 }
581 else
584
585 /*
586 * Initializing the page when needed is trickier than it looks, because of
587 * the possibility of multiple backends doing this concurrently, and our
588 * desire to not uselessly take the buffer lock in the normal path where
589 * the page is OK. We must take the lock to initialize the page, so
590 * recheck page newness after we have the lock, in case someone else
591 * already did it. Also, because we initially check PageIsNew with no
592 * lock, it's possible to fall through and return the buffer while someone
593 * else is still initializing the page (i.e., we might see pd_upper as set
594 * but other page header fields are still zeroes). This is harmless for
595 * callers that will take a buffer lock themselves, but some callers
596 * inspect the page without any lock at all. The latter is OK only so
597 * long as it doesn't depend on the page header having correct contents.
598 * Current usage is safe because PageGetContents() does not require that.
599 */
601 {
606 }
607 return buf;
608}

References buf, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetPage(), fb(), InvalidBlockNumber, InvalidBuffer, LockBuffer(), PageInit(), PageIsNew(), RBM_ZERO_ON_ERROR, ReadBufferExtended(), RelationGetSmgr(), smgrexists(), smgrnblocks(), VISIBILITYMAP_FORKNUM, and vm_extend().

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