3333 * is set, we know the condition is true, but if a bit is not set, it might or
3434 * might not be true.
3535 *
36- * Clearing both visibility map bits is not separately WAL-logged. The callers
36+ * Clearing visibility map bits is not separately WAL-logged. The callers
3737 * must make sure that whenever a bit is cleared, the bit is cleared on WAL
3838 * replay of the updating operation as well.
3939 *
104104 */
105105#define MAPSIZE (BLCKSZ - MAXALIGN(SizeOfPageHeaderData))
106106
107+ /* Number of heap blocks we can represent in one byte */
108+ #define HEAPBLOCKS_PER_BYTE (BITS_PER_BYTE / BITS_PER_HEAPBLOCK)
109+
107110/* Number of heap blocks we can represent in one visibility map page. */
108111#define HEAPBLOCKS_PER_PAGE (MAPSIZE * HEAPBLOCKS_PER_BYTE)
109112
110113/* Mapping from heap block number to the right bit in the visibility map */
111114#define HEAPBLK_TO_MAPBLOCK (x ) ((x) / HEAPBLOCKS_PER_PAGE)
112115#define HEAPBLK_TO_MAPBYTE (x ) (((x) % HEAPBLOCKS_PER_PAGE) / HEAPBLOCKS_PER_BYTE)
113- #define HEAPBLK_TO_MAPBIT (x ) (((x) % HEAPBLOCKS_PER_BYTE) * BITS_PER_HEAPBLOCK)
116+ #define HEAPBLK_TO_OFFSET (x ) (((x) % HEAPBLOCKS_PER_BYTE) * BITS_PER_HEAPBLOCK)
114117
115118/* tables for fast counting of set bits for visible and frozen */
116119static const uint8 number_of_ones_for_visible [256 ] = {
@@ -156,7 +159,7 @@ static void vm_extend(Relation rel, BlockNumber nvmblocks);
156159
157160
158161/*
159- * visibilitymap_clear - clear all bits in visibility map
162+ * visibilitymap_clear - clear all bits for one page in visibility map
160163 *
161164 * You must pass a buffer containing the correct map page to this function.
162165 * Call visibilitymap_pin first to pin the right one. This function doesn't do
@@ -167,8 +170,8 @@ visibilitymap_clear(Relation rel, BlockNumber heapBlk, Buffer buf)
167170{
168171 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK (heapBlk );
169172 int mapByte = HEAPBLK_TO_MAPBYTE (heapBlk );
170- int mapBit = HEAPBLK_TO_MAPBIT (heapBlk );
171- uint8 mask = VISIBILITYMAP_VALID_BITS << mapBit ;
173+ int mapOffset = HEAPBLK_TO_OFFSET (heapBlk );
174+ uint8 mask = VISIBILITYMAP_VALID_BITS << mapOffset ;
172175 char * map ;
173176
174177#ifdef TRACE_VISIBILITYMAP
@@ -267,7 +270,7 @@ visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf,
267270{
268271 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK (heapBlk );
269272 uint32 mapByte = HEAPBLK_TO_MAPBYTE (heapBlk );
270- uint8 mapBit = HEAPBLK_TO_MAPBIT (heapBlk );
273+ uint8 mapOffset = HEAPBLK_TO_OFFSET (heapBlk );
271274 Page page ;
272275 uint8 * map ;
273276
@@ -291,11 +294,11 @@ visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf,
291294 map = (uint8 * )PageGetContents (page );
292295 LockBuffer (vmBuf , BUFFER_LOCK_EXCLUSIVE );
293296
294- if (flags != (map [mapByte ] >> mapBit & VISIBILITYMAP_VALID_BITS ))
297+ if (flags != (map [mapByte ] >> mapOffset & VISIBILITYMAP_VALID_BITS ))
295298 {
296299 START_CRIT_SECTION ();
297300
298- map [mapByte ] |= (flags << mapBit );
301+ map [mapByte ] |= (flags << mapOffset );
299302 MarkBufferDirty (vmBuf );
300303
301304 if (RelationNeedsWAL (rel ))
@@ -338,8 +341,7 @@ visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf,
338341 * earlier call to visibilitymap_pin or visibilitymap_get_status on the same
339342 * relation. On return, *buf is a valid buffer with the map page containing
340343 * the bit for heapBlk, or InvalidBuffer. The caller is responsible for
341- * releasing *buf after it's done testing and setting bits, and must pass flags
342- * for which it needs to check the value in visibility map.
344+ * releasing *buf after it's done testing and setting bits.
343345 *
344346 * NOTE: This function is typically called without a lock on the heap page,
345347 * so somebody else could change the bit just after we look at it. In fact,
@@ -353,8 +355,9 @@ visibilitymap_get_status(Relation rel, BlockNumber heapBlk, Buffer *buf)
353355{
354356 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK (heapBlk );
355357 uint32 mapByte = HEAPBLK_TO_MAPBYTE (heapBlk );
356- uint8 mapBit = HEAPBLK_TO_MAPBIT (heapBlk );
358+ uint8 mapOffset = HEAPBLK_TO_OFFSET (heapBlk );
357359 char * map ;
360+ uint8 result ;
358361
359362#ifdef TRACE_VISIBILITYMAP
360363 elog (DEBUG1 , "vm_get_status %s %d" , RelationGetRelationName (rel ), heapBlk );
@@ -384,7 +387,8 @@ visibilitymap_get_status(Relation rel, BlockNumber heapBlk, Buffer *buf)
384387 * here, but for performance reasons we make it the caller's job to worry
385388 * about that.
386389 */
387- return ((map [mapByte ] >> mapBit ) & VISIBILITYMAP_VALID_BITS );
390+ result = ((map [mapByte ] >> mapOffset ) & VISIBILITYMAP_VALID_BITS );
391+ return result ;
388392}
389393
390394/*
@@ -456,7 +460,7 @@ visibilitymap_truncate(Relation rel, BlockNumber nheapblocks)
456460 /* last remaining block, byte, and bit */
457461 BlockNumber truncBlock = HEAPBLK_TO_MAPBLOCK (nheapblocks );
458462 uint32 truncByte = HEAPBLK_TO_MAPBYTE (nheapblocks );
459- uint8 truncBit = HEAPBLK_TO_MAPBIT (nheapblocks );
463+ uint8 truncOffset = HEAPBLK_TO_OFFSET (nheapblocks );
460464
461465#ifdef TRACE_VISIBILITYMAP
462466 elog (DEBUG1 , "vm_truncate %s %d" , RelationGetRelationName (rel ), nheapblocks );
@@ -478,7 +482,7 @@ visibilitymap_truncate(Relation rel, BlockNumber nheapblocks)
478482 * because we don't get a chance to clear the bits if the heap is extended
479483 * again.
480484 */
481- if (truncByte != 0 || truncBit != 0 )
485+ if (truncByte != 0 || truncOffset != 0 )
482486 {
483487 Buffer mapBuffer ;
484488 Page page ;
@@ -511,7 +515,7 @@ visibilitymap_truncate(Relation rel, BlockNumber nheapblocks)
511515 * ((1 << 7) - 1) = 01111111
512516 *----
513517 */
514- map [truncByte ] &= (1 << truncBit ) - 1 ;
518+ map [truncByte ] &= (1 << truncOffset ) - 1 ;
515519
516520 MarkBufferDirty (mapBuffer );
517521 UnlockReleaseBuffer (mapBuffer );
0 commit comments