Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
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_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 (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

◆ FROZEN_MASK8

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

Definition at line 123 of file visibilitymap.c.

◆ HEAPBLK_TO_MAPBLOCK

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

Definition at line 117 of file visibilitymap.c.

◆ HEAPBLK_TO_MAPBYTE

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

Definition at line 118 of file visibilitymap.c.

◆ HEAPBLK_TO_OFFSET

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

Definition at line 119 of file visibilitymap.c.

◆ HEAPBLOCKS_PER_BYTE

#define HEAPBLOCKS_PER_BYTE   (BITS_PER_BYTE / BITS_PER_HEAPBLOCK)

Definition at line 111 of file visibilitymap.c.

◆ HEAPBLOCKS_PER_PAGE

#define HEAPBLOCKS_PER_PAGE   (MAPSIZE * HEAPBLOCKS_PER_BYTE)

Definition at line 114 of file visibilitymap.c.

◆ MAPSIZE

#define MAPSIZE   (BLCKSZ - MAXALIGN(SizeOfPageHeaderData))

Definition at line 108 of file visibilitymap.c.

◆ VISIBLE_MASK8

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

Definition at line 122 of file visibilitymap.c.

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:4198
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2921
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5572
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:196
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:417
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:198
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:368
static char * PageGetContents(Page page)
Definition: bufpage.h:258
uint8_t uint8
Definition: c.h:537
#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:548
#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 392 of file visibilitymap.c.

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

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

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 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(), and RelationGetBufferForTuple().

◆ visibilitymap_prepare_truncate()

BlockNumber visibilitymap_prepare_truncate ( Relation  rel,
BlockNumber  nheapblocks 
)

Definition at line 446 of file visibilitymap.c.

447{
448 BlockNumber newnblocks;
449
450 /* last remaining block, byte, and bit */
451 BlockNumber truncBlock = HEAPBLK_TO_MAPBLOCK(nheapblocks);
452 uint32 truncByte = HEAPBLK_TO_MAPBYTE(nheapblocks);
453 uint8 truncOffset = HEAPBLK_TO_OFFSET(nheapblocks);
454
455#ifdef TRACE_VISIBILITYMAP
456 elog(DEBUG1, "vm_truncate %s %d", RelationGetRelationName(rel), nheapblocks);
457#endif
458
459 /*
460 * If no visibility map has been created yet for this relation, there's
461 * nothing to truncate.
462 */
464 return InvalidBlockNumber;
465
466 /*
467 * Unless the new size is exactly at a visibility map page boundary, the
468 * tail bits in the last remaining map page, representing truncated heap
469 * blocks, need to be cleared. This is not only tidy, but also necessary
470 * because we don't get a chance to clear the bits if the heap is extended
471 * again.
472 */
473 if (truncByte != 0 || truncOffset != 0)
474 {
475 Buffer mapBuffer;
476 Page page;
477 char *map;
478
479 newnblocks = truncBlock + 1;
480
481 mapBuffer = vm_readbuf(rel, truncBlock, false);
482 if (!BufferIsValid(mapBuffer))
483 {
484 /* nothing to do, the file was already smaller */
485 return InvalidBlockNumber;
486 }
487
488 page = BufferGetPage(mapBuffer);
489 map = PageGetContents(page);
490
492
493 /* NO EREPORT(ERROR) from here till changes are logged */
495
496 /* Clear out the unwanted bytes. */
497 MemSet(&map[truncByte + 1], 0, MAPSIZE - (truncByte + 1));
498
499 /*----
500 * Mask out the unwanted bits of the last remaining byte.
501 *
502 * ((1 << 0) - 1) = 00000000
503 * ((1 << 1) - 1) = 00000001
504 * ...
505 * ((1 << 6) - 1) = 00111111
506 * ((1 << 7) - 1) = 01111111
507 *----
508 */
509 map[truncByte] &= (1 << truncOffset) - 1;
510
511 /*
512 * Truncation of a relation is WAL-logged at a higher-level, and we
513 * will be called at WAL replay. But if checksums are enabled, we need
514 * to still write a WAL record to protect against a torn page, if the
515 * page is flushed to disk before the truncation WAL record. We cannot
516 * use MarkBufferDirtyHint here, because that will not dirty the page
517 * during recovery.
518 */
519 MarkBufferDirty(mapBuffer);
521 log_newpage_buffer(mapBuffer, false);
522
524
525 UnlockReleaseBuffer(mapBuffer);
526 }
527 else
528 newnblocks = truncBlock;
529
530 if (smgrnblocks(RelationGetSmgr(rel), VISIBILITYMAP_FORKNUM) <= newnblocks)
531 {
532 /* nothing to do, the file was already smaller than requested size */
533 return InvalidBlockNumber;
534 }
535
536 return newnblocks;
537}
#define InvalidBlockNumber
Definition: block.h:33
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5355
PageData * Page
Definition: bufpage.h:82
#define MemSet(start, val, len)
Definition: c.h:1020
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:576
#define RelationNeedsWAL(relation)
Definition: rel.h:637
@ 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:1249
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 flags 0x%02X for %s %d",
259 flags, RelationGetRelationName(rel), heapBlk);
260#endif
261
264 Assert((flags & VISIBILITYMAP_VALID_BITS) == flags);
265
266 /* Must never set all_frozen bit without also setting all_visible bit */
268
269 /* Check that we have the right heap page pinned, if present */
270 if (BufferIsValid(heapBuf) && BufferGetBlockNumber(heapBuf) != heapBlk)
271 elog(ERROR, "wrong heap buffer passed to visibilitymap_set");
272
273 Assert(!BufferIsValid(heapBuf) || BufferIsExclusiveLocked(heapBuf));
274
275 /* Check that we have the right VM page pinned */
276 if (!BufferIsValid(vmBuf) || BufferGetBlockNumber(vmBuf) != mapBlock)
277 elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
278
279 page = BufferGetPage(vmBuf);
280 map = (uint8 *) PageGetContents(page);
282
283 status = (map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS;
284 if (flags != status)
285 {
287
288 map[mapByte] |= (flags << mapOffset);
289 MarkBufferDirty(vmBuf);
290
291 if (RelationNeedsWAL(rel))
292 {
293 if (XLogRecPtrIsInvalid(recptr))
294 {
296 recptr = log_heap_visible(rel, heapBuf, vmBuf, cutoff_xid, flags);
297
298 /*
299 * If data checksums are enabled (or wal_log_hints=on), we
300 * need to protect the heap page from being torn.
301 *
302 * If not, then we must *not* update the heap page's LSN. In
303 * this case, the FPI for the heap page was omitted from the
304 * WAL record inserted above, so it would be incorrect to
305 * update the heap page's LSN.
306 */
308 {
309 Page heapPage = BufferGetPage(heapBuf);
310
311 PageSetLSN(heapPage, recptr);
312 }
313 }
314 PageSetLSN(page, recptr);
315 }
316
318 }
319
321 return status;
322}
bool BufferIsExclusiveLocked(Buffer buffer)
Definition: bufmgr.c:2860
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:8804
#define VISIBILITYMAP_ALL_FROZEN
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29

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

◆ vm_extend()

static Buffer vm_extend ( Relation  rel,
BlockNumber  vm_nblocks 
)
static

Definition at line 620 of file visibilitymap.c.

621{
622 Buffer buf;
623
627 vm_nblocks,
629
630 /*
631 * Send a shared-inval message to force other backends to close any smgr
632 * references they may have for this rel, which we are about to change.
633 * This is a useful optimization because it means that backends don't have
634 * to keep checking for creation or extension of the file, which happens
635 * infrequently.
636 */
637 CacheInvalidateSmgr(RelationGetSmgr(rel)->smgr_rlocator);
638
639 return buf;
640}
Buffer ExtendBufferedRelTo(BufferManagerRelation bmr, ForkNumber fork, BufferAccessStrategy strategy, uint32 flags, BlockNumber extend_to, ReadBufferMode mode)
Definition: bufmgr.c:922
@ 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:111
void CacheInvalidateSmgr(RelFileLocatorBackend rlocator)
Definition: inval.c:1751
static char * buf
Definition: pg_test_fsync.c:72

References BMR_REL, buf, CacheInvalidateSmgr(), EB_CLEAR_SIZE_CACHE, EB_CREATE_FORK_IF_NEEDED, ExtendBufferedRelTo(), 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 546 of file visibilitymap.c.

547{
548 Buffer buf;
549 SMgrRelation reln;
550
551 /*
552 * Caution: re-using this smgr pointer could fail if the relcache entry
553 * gets closed. It's safe as long as we only do smgr-level operations
554 * between here and the last use of the pointer.
555 */
556 reln = RelationGetSmgr(rel);
557
558 /*
559 * If we haven't cached the size of the visibility map fork yet, check it
560 * first.
561 */
563 {
566 else
568 }
569
570 /*
571 * For reading we use ZERO_ON_ERROR mode, and initialize the page if
572 * necessary. It's always safe to clear bits, so it's better to clear
573 * corrupt pages than error out.
574 *
575 * We use the same path below to initialize pages when extending the
576 * relation, as a concurrent extension can end up with vm_extend()
577 * returning an already-initialized page.
578 */
579 if (blkno >= reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM])
580 {
581 if (extend)
582 buf = vm_extend(rel, blkno + 1);
583 else
584 return InvalidBuffer;
585 }
586 else
588 RBM_ZERO_ON_ERROR, NULL);
589
590 /*
591 * Initializing the page when needed is trickier than it looks, because of
592 * the possibility of multiple backends doing this concurrently, and our
593 * desire to not uselessly take the buffer lock in the normal path where
594 * the page is OK. We must take the lock to initialize the page, so
595 * recheck page newness after we have the lock, in case someone else
596 * already did it. Also, because we initially check PageIsNew with no
597 * lock, it's possible to fall through and return the buffer while someone
598 * else is still initializing the page (i.e., we might see pd_upper as set
599 * but other page header fields are still zeroes). This is harmless for
600 * callers that will take a buffer lock themselves, but some callers
601 * inspect the page without any lock at all. The latter is OK only so
602 * long as it doesn't depend on the page header having correct contents.
603 * Current usage is safe because PageGetContents() does not require that.
604 */
606 {
609 PageInit(BufferGetPage(buf), BLCKSZ, 0);
611 }
612 return buf;
613}
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:805
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42
static bool PageIsNew(const PageData *page)
Definition: bufpage.h:234
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:47
static Buffer vm_extend(Relation rel, BlockNumber vm_nblocks)

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

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