33
33
* is set, we know the condition is true, but if a bit is not set, it might or
34
34
* might not be true.
35
35
*
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
37
37
* must make sure that whenever a bit is cleared, the bit is cleared on WAL
38
38
* replay of the updating operation as well.
39
39
*
104
104
*/
105
105
#define MAPSIZE (BLCKSZ - MAXALIGN(SizeOfPageHeaderData))
106
106
107
+ /* Number of heap blocks we can represent in one byte */
108
+ #define HEAPBLOCKS_PER_BYTE (BITS_PER_BYTE / BITS_PER_HEAPBLOCK)
109
+
107
110
/* Number of heap blocks we can represent in one visibility map page. */
108
111
#define HEAPBLOCKS_PER_PAGE (MAPSIZE * HEAPBLOCKS_PER_BYTE)
109
112
110
113
/* Mapping from heap block number to the right bit in the visibility map */
111
114
#define HEAPBLK_TO_MAPBLOCK (x ) ((x) / HEAPBLOCKS_PER_PAGE)
112
115
#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)
114
117
115
118
/* tables for fast counting of set bits for visible and frozen */
116
119
static const uint8 number_of_ones_for_visible [256 ] = {
@@ -156,7 +159,7 @@ static void vm_extend(Relation rel, BlockNumber nvmblocks);
156
159
157
160
158
161
/*
159
- * visibilitymap_clear - clear all bits in visibility map
162
+ * visibilitymap_clear - clear all bits for one page in visibility map
160
163
*
161
164
* You must pass a buffer containing the correct map page to this function.
162
165
* 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)
167
170
{
168
171
BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK (heapBlk );
169
172
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 ;
172
175
char * map ;
173
176
174
177
#ifdef TRACE_VISIBILITYMAP
@@ -267,7 +270,7 @@ visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf,
267
270
{
268
271
BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK (heapBlk );
269
272
uint32 mapByte = HEAPBLK_TO_MAPBYTE (heapBlk );
270
- uint8 mapBit = HEAPBLK_TO_MAPBIT (heapBlk );
273
+ uint8 mapOffset = HEAPBLK_TO_OFFSET (heapBlk );
271
274
Page page ;
272
275
uint8 * map ;
273
276
@@ -291,11 +294,11 @@ visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf,
291
294
map = (uint8 * )PageGetContents (page );
292
295
LockBuffer (vmBuf , BUFFER_LOCK_EXCLUSIVE );
293
296
294
- if (flags != (map [mapByte ] >> mapBit & VISIBILITYMAP_VALID_BITS ))
297
+ if (flags != (map [mapByte ] >> mapOffset & VISIBILITYMAP_VALID_BITS ))
295
298
{
296
299
START_CRIT_SECTION ();
297
300
298
- map [mapByte ] |= (flags << mapBit );
301
+ map [mapByte ] |= (flags << mapOffset );
299
302
MarkBufferDirty (vmBuf );
300
303
301
304
if (RelationNeedsWAL (rel ))
@@ -338,8 +341,7 @@ visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf,
338
341
* earlier call to visibilitymap_pin or visibilitymap_get_status on the same
339
342
* relation. On return, *buf is a valid buffer with the map page containing
340
343
* 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.
343
345
*
344
346
* NOTE: This function is typically called without a lock on the heap page,
345
347
* 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)
353
355
{
354
356
BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK (heapBlk );
355
357
uint32 mapByte = HEAPBLK_TO_MAPBYTE (heapBlk );
356
- uint8 mapBit = HEAPBLK_TO_MAPBIT (heapBlk );
358
+ uint8 mapOffset = HEAPBLK_TO_OFFSET (heapBlk );
357
359
char * map ;
360
+ uint8 result ;
358
361
359
362
#ifdef TRACE_VISIBILITYMAP
360
363
elog (DEBUG1 , "vm_get_status %s %d" , RelationGetRelationName (rel ), heapBlk );
@@ -384,7 +387,8 @@ visibilitymap_get_status(Relation rel, BlockNumber heapBlk, Buffer *buf)
384
387
* here, but for performance reasons we make it the caller's job to worry
385
388
* about that.
386
389
*/
387
- return ((map [mapByte ] >> mapBit ) & VISIBILITYMAP_VALID_BITS );
390
+ result = ((map [mapByte ] >> mapOffset ) & VISIBILITYMAP_VALID_BITS );
391
+ return result ;
388
392
}
389
393
390
394
/*
@@ -456,7 +460,7 @@ visibilitymap_truncate(Relation rel, BlockNumber nheapblocks)
456
460
/* last remaining block, byte, and bit */
457
461
BlockNumber truncBlock = HEAPBLK_TO_MAPBLOCK (nheapblocks );
458
462
uint32 truncByte = HEAPBLK_TO_MAPBYTE (nheapblocks );
459
- uint8 truncBit = HEAPBLK_TO_MAPBIT (nheapblocks );
463
+ uint8 truncOffset = HEAPBLK_TO_OFFSET (nheapblocks );
460
464
461
465
#ifdef TRACE_VISIBILITYMAP
462
466
elog (DEBUG1 , "vm_truncate %s %d" , RelationGetRelationName (rel ), nheapblocks );
@@ -478,7 +482,7 @@ visibilitymap_truncate(Relation rel, BlockNumber nheapblocks)
478
482
* because we don't get a chance to clear the bits if the heap is extended
479
483
* again.
480
484
*/
481
- if (truncByte != 0 || truncBit != 0 )
485
+ if (truncByte != 0 || truncOffset != 0 )
482
486
{
483
487
Buffer mapBuffer ;
484
488
Page page ;
@@ -511,7 +515,7 @@ visibilitymap_truncate(Relation rel, BlockNumber nheapblocks)
511
515
* ((1 << 7) - 1) = 01111111
512
516
*----
513
517
*/
514
- map [truncByte ] &= (1 << truncBit ) - 1 ;
518
+ map [truncByte ] &= (1 << truncOffset ) - 1 ;
515
519
516
520
MarkBufferDirty (mapBuffer );
517
521
UnlockReleaseBuffer (mapBuffer );
0 commit comments