Debian Codegraph @

src/backend/access/heap/visibilitymap.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * visibilitymap.c
4 * bitmap for tracking visibility of heap tuples
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/access/heap/visibilitymap.c
12 *
13 * INTERFACE ROUTINES
14 * visibilitymap_clear - clear bits for one page in the visibility map
15 * visibilitymap_pin - pin a map page for setting a bit
16 * visibilitymap_pin_ok - check whether correct map page is already pinned
17 * visibilitymap_set - set a bit in a previously pinned page
18 * visibilitymap_get_status - get status of bits
19 * visibilitymap_count - count number of bits set in visibility map
20 * visibilitymap_prepare_truncate -
21 * prepare for truncation of the visibility map
22 *
23 * NOTES
24 *
25 * The visibility map is a bitmap with two bits (all-visible and all-frozen)
26 * per heap page. A set all-visible bit means that all tuples on the page are
27 * known visible to all transactions, and therefore the page doesn't need to
28 * be vacuumed. A set all-frozen bit means that all tuples on the page are
29 * completely frozen, and therefore the page doesn't need to be vacuumed even
30 * if whole table scanning vacuum is required (e.g. anti-wraparound vacuum).
31 * The all-frozen bit must be set only when the page is already all-visible.
32 *
33 * The map is conservative in the sense that we make sure that whenever a bit
34 * is set, we know the condition is true, but if a bit is not set, it might or
35 * might not be true.
36 *
37 * Clearing visibility map bits is not separately WAL-logged. The callers
38 * must make sure that whenever a bit is cleared, the bit is cleared on WAL
39 * replay of the updating operation as well.
40 *
41 * When we *set* a visibility map during VACUUM, we must write WAL. This may
42 * seem counterintuitive, since the bit is basically a hint: if it is clear,
43 * it may still be the case that every tuple on the page is visible to all
44 * transactions; we just don't know that for certain. The difficulty is that
45 * there are two bits which are typically set together: the PD_ALL_VISIBLE bit
46 * on the page itself, and the visibility map bit. If a crash occurs after the
47 * visibility map page makes it to disk and before the updated heap page makes
48 * it to disk, redo must set the bit on the heap page. Otherwise, the next
49 * insert, update, or delete on the heap page will fail to realize that the
50 * visibility map bit must be cleared, possibly causing index-only scans to
51 * return wrong answers.
52 *
53 * VACUUM will normally skip pages for which the visibility map bit is set;
54 * such pages can't contain any dead tuples and therefore don't need vacuuming.
55 *
56 * LOCKING
57 *
58 * In heapam.c, whenever a page is modified so that not all tuples on the
59 * page are visible to everyone anymore, the corresponding bit in the
60 * visibility map is cleared. In order to be crash-safe, we need to do this
61 * while still holding a lock on the heap page and in the same critical
62 * section that logs the page modification. However, we don't want to hold
63 * the buffer lock over any I/O that may be required to read in the visibility
64 * map page. To avoid this, we examine the heap page before locking it;
65 * if the page-level PD_ALL_VISIBLE bit is set, we pin the visibility map
66 * bit. Then, we lock the buffer. But this creates a race condition: there
67 * is a possibility that in the time it takes to lock the buffer, the
68 * PD_ALL_VISIBLE bit gets set. If that happens, we have to unlock the
69 * buffer, pin the visibility map page, and relock the buffer. This shouldn't
70 * happen often, because only VACUUM currently sets visibility map bits,
71 * and the race will only occur if VACUUM processes a given page at almost
72 * exactly the same time that someone tries to further modify it.
73 *
74 * To set a bit, you need to hold a lock on the heap page. That prevents
75 * the race condition where VACUUM sees that all tuples on the page are
76 * visible to everyone, but another backend modifies the page before VACUUM
77 * sets the bit in the visibility map.
78 *
79 * When a bit is set, the LSN of the visibility map page is updated to make
80 * sure that the visibility map update doesn't get written to disk before the
81 * WAL record of the changes that made it possible to set the bit is flushed.
82 * But when a bit is cleared, we don't have to do that because it's always
83 * safe to clear a bit in the map from correctness point of view.
84 *
85 *-------------------------------------------------------------------------
86 */
87 #include "postgres.h"
88
89 #include "access/heapam_xlog.h"
90 #include "access/visibilitymap.h"
91 #include "access/xloginsert.h"
92 #include "access/xlogutils.h"
93 #include "miscadmin.h"
94 #include "port/pg_bitutils.h"
95 #include "storage/bufmgr.h"
96 #include "storage/smgr.h"
97 #include "utils/inval.h"
98 #include "utils/rel.h"
99
100
101 /*#define TRACE_VISIBILITYMAP */
102
103 /*
104 * Size of the bitmap on each visibility map page, in bytes. There's no
105 * extra headers, so the whole page minus the standard page header is
106 * used for the bitmap.
107 */
108 #define MAPSIZE (BLCKSZ - MAXALIGN(SizeOfPageHeaderData))
109
110 /* Number of heap blocks we can represent in one byte */
111 #define HEAPBLOCKS_PER_BYTE (BITS_PER_BYTE / BITS_PER_HEAPBLOCK)
112
113 /* Number of heap blocks we can represent in one visibility map page. */
114 #define HEAPBLOCKS_PER_PAGE (MAPSIZE * HEAPBLOCKS_PER_BYTE)
115
116 /* Mapping from heap block number to the right bit in the visibility map */
117 #define HEAPBLK_TO_MAPBLOCK(x) ((x) / HEAPBLOCKS_PER_PAGE)
118 #define HEAPBLK_TO_MAPBLOCK_LIMIT(x) \
119 (((x) + HEAPBLOCKS_PER_PAGE - 1) / HEAPBLOCKS_PER_PAGE)
120 #define HEAPBLK_TO_MAPBYTE(x) (((x) % HEAPBLOCKS_PER_PAGE) / HEAPBLOCKS_PER_BYTE)
121 #define HEAPBLK_TO_OFFSET(x) (((x) % HEAPBLOCKS_PER_BYTE) * BITS_PER_HEAPBLOCK)
122
123 /* Masks for counting subsets of bits in the visibility map. */
124 #define VISIBLE_MASK8 (0x55) /* The lower bit of each bit pair */
125 #define FROZEN_MASK8 (0xaa) /* The upper bit of each bit pair */
126
127 /* prototypes for internal routines */
128 static Buffer vm_readbuf(Relation rel, BlockNumber blkno, bool extend);
129 static Buffer vm_extend(Relation rel, BlockNumber vm_nblocks);
130
131
132 /*
133 * visibilitymap_clear - clear specified bits for one page in visibility map
134 *
135 * You must pass a buffer containing the correct map page to this function.
136 * Call visibilitymap_pin first to pin the right one. This function doesn't do
137 * any I/O. Returns true if any bits have been cleared and false otherwise.
138 *
139 * Most callers should use visibilitymap_clear_locked rather than this
140 * function. It is usually necessary to register the VM buffer in the WAL
141 * record, and this necessitates holding the lock for longer than it is held
142 * here. However, we retain this function for behavioral compatibility on the
143 * back branches (out-of-tree callers may expect it to take the lock). And,
144 * since we have it, we use it for in-tree callers that don't have to manage
145 * the VM buffer lock themselves.
146 */
147 bool
148 visibilitymap_clear(Relation rel, BlockNumber heapBlk, Buffer vmbuf, uint8 flags)
149 {
150 bool cleared = false;
151
152 LockBuffer(vmbuf, BUFFER_LOCK_EXCLUSIVE);
153
154 cleared = visibilitymap_clear_locked(rel, heapBlk, vmbuf, flags);
155
156 LockBuffer(vmbuf, BUFFER_LOCK_UNLOCK);
157
158 return cleared;
159 }
160
161 /*
162 * Clear specified bits, caller holds VM lock
163 *
164 * Like visibilitymap_clear(), except the caller must already hold the VM
165 * buffer exclusive lock and is responsible for unlocking it.
166 * Returns true if any bits were actually cleared, false otherwise.
167 */
168 bool
169 visibilitymap_clear_locked(Relation rel, BlockNumber heapBlk, Buffer vmbuf, uint8 flags)
170 {
171 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
172 int mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
173 int mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
174 uint8 mask = flags << mapOffset;
175 char *map;
176 bool cleared = false;
177
178 /* Must never clear all_visible bit while leaving all_frozen bit set */
179 Assert(flags & VISIBILITYMAP_VALID_BITS);
180 Assert(flags != VISIBILITYMAP_ALL_VISIBLE);
181
182 #ifdef TRACE_VISIBILITYMAP
183 elog(DEBUG1, "vm_clear %s %d", RelationGetRelationName(rel), heapBlk);
184 #endif
185
186 if (!BufferIsValid(vmbuf) || BufferGetBlockNumber(vmbuf) != mapBlock)
187 elog(ERROR, "wrong buffer passed to visibilitymap_clear");
188
189 Assert(BufferIsExclusiveLocked(vmbuf));
190
191 map = PageGetContents(BufferGetPage(vmbuf));
192
193 if (map[mapByte] & mask)
194 {
195 map[mapByte] &= ~mask;
196
197 MarkBufferDirty(vmbuf);
198 cleared = true;
199 }
200
201 return cleared;
202 }
203
204 /*
205 * visibilitymap_pin - pin a map page for setting a bit
206 *
207 * Setting a bit in the visibility map is a two-phase operation. First, call
208 * visibilitymap_pin, to pin the visibility map page containing the bit for
209 * the heap page. Because that can require I/O to read the map page, you
210 * shouldn't hold a lock on the heap page while doing that. Then, call
211 * visibilitymap_set to actually set the bit.
212 *
213 * On entry, *vmbuf should be InvalidBuffer or a valid buffer returned by
214 * an earlier call to visibilitymap_pin or visibilitymap_get_status on the same
215 * relation. On return, *vmbuf is a valid buffer with the map page containing
216 * the bit for heapBlk.
217 *
218 * If the page doesn't exist in the map file yet, it is extended.
219 */
220 void
221 visibilitymap_pin(Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
222 {
223 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
224
225 /* Reuse the old pinned buffer if possible */
226 if (BufferIsValid(*vmbuf))
227 {
228 if (BufferGetBlockNumber(*vmbuf) == mapBlock)
229 return;
230
231 ReleaseBuffer(*vmbuf);
232 }
233 *vmbuf = vm_readbuf(rel, mapBlock, true);
234 }
235
236 /*
237 * visibilitymap_pin_ok - do we already have the correct page pinned?
238 *
239 * On entry, vmbuf should be InvalidBuffer or a valid buffer returned by
240 * an earlier call to visibilitymap_pin or visibilitymap_get_status on the same
241 * relation. The return value indicates whether the buffer covers the
242 * given heapBlk.
243 */
244 bool
245 visibilitymap_pin_ok(BlockNumber heapBlk, Buffer vmbuf)
246 {
247 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
248
249 return BufferIsValid(vmbuf) && BufferGetBlockNumber(vmbuf) == mapBlock;
250 }
251
252 /*
253 * visibilitymap_set - set bit(s) on a previously pinned page
254 *
255 * recptr is the LSN of the XLOG record we're replaying, if we're in recovery,
256 * or InvalidXLogRecPtr in normal running. The VM page LSN is advanced to the
257 * one provided; in normal running, we generate a new XLOG record and set the
258 * page LSN to that value (though the heap page's LSN may *not* be updated;
259 * see below). cutoff_xid is the largest xmin on the page being marked
260 * all-visible; it is needed for Hot Standby, and can be InvalidTransactionId
261 * if the page contains no tuples. It can also be set to InvalidTransactionId
262 * when a page that is already all-visible is being marked all-frozen.
263 *
264 * Caller is expected to set the heap page's PD_ALL_VISIBLE bit before calling
265 * this function. Except in recovery, caller should also pass the heap
266 * buffer. When checksums are enabled and we're not in recovery, we must add
267 * the heap buffer to the WAL chain to protect it from being torn.
268 *
269 * You must pass a buffer containing the correct map page to this function.
270 * Call visibilitymap_pin first to pin the right one. This function doesn't do
271 * any I/O.
272 *
273 * Returns the state of the page's VM bits before setting flags.
274 */
275 uint8
276 visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf,
277 XLogRecPtr recptr, Buffer vmBuf, TransactionId cutoff_xid,
278 uint8 flags)
279 {
280 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
281 uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
282 uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
283 Page page;
284 uint8 *map;
285 uint8 status;
286
287 #ifdef TRACE_VISIBILITYMAP
288 elog(DEBUG1, "vm_set %s %d", RelationGetRelationName(rel), heapBlk);
289 #endif
290
291 Assert(InRecovery || XLogRecPtrIsInvalid(recptr));
292 Assert(InRecovery || PageIsAllVisible((Page) BufferGetPage(heapBuf)));
293 Assert((flags & VISIBILITYMAP_VALID_BITS) == flags);
294
295 /* Must never set all_frozen bit without also setting all_visible bit */
296 Assert(flags != VISIBILITYMAP_ALL_FROZEN);
297
298 /* Check that we have the right heap page pinned, if present */
299 if (BufferIsValid(heapBuf) && BufferGetBlockNumber(heapBuf) != heapBlk)
300 elog(ERROR, "wrong heap buffer passed to visibilitymap_set");
301
302 /* Check that we have the right VM page pinned */
303 if (!BufferIsValid(vmBuf) || BufferGetBlockNumber(vmBuf) != mapBlock)
304 elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
305
306 page = BufferGetPage(vmBuf);
307 map = (uint8 *) PageGetContents(page);
308 LockBuffer(vmBuf, BUFFER_LOCK_EXCLUSIVE);
309
310 status = (map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS;
311 if (flags != status)
312 {
313 START_CRIT_SECTION();
314
315 map[mapByte] |= (flags << mapOffset);
316 MarkBufferDirty(vmBuf);
317
318 if (RelationNeedsWAL(rel))
319 {
320 if (XLogRecPtrIsInvalid(recptr))
321 {
322 Assert(!InRecovery);
323 recptr = log_heap_visible(rel, heapBuf, vmBuf, cutoff_xid, flags);
324
325 /*
326 * If data checksums are enabled (or wal_log_hints=on), we
327 * need to protect the heap page from being torn.
328 *
329 * If not, then we must *not* update the heap page's LSN. In
330 * this case, the FPI for the heap page was omitted from the
331 * WAL record inserted above, so it would be incorrect to
332 * update the heap page's LSN.
333 */
334 if (XLogHintBitIsNeeded())
335 {
336 Page heapPage = BufferGetPage(heapBuf);
337
338 PageSetLSN(heapPage, recptr);
339 }
340 }
341 PageSetLSN(page, recptr);
342 }
343
344 END_CRIT_SECTION();
345 }
346
347 LockBuffer(vmBuf, BUFFER_LOCK_UNLOCK);
348 return status;
349 }
350
351 /*
352 * visibilitymap_get_status - get status of bits
353 *
354 * Are all tuples on heapBlk visible to all or are marked frozen, according
355 * to the visibility map?
356 *
357 * On entry, *vmbuf should be InvalidBuffer or a valid buffer returned by an
358 * earlier call to visibilitymap_pin or visibilitymap_get_status on the same
359 * relation. On return, *vmbuf is a valid buffer with the map page containing
360 * the bit for heapBlk, or InvalidBuffer. The caller is responsible for
361 * releasing *vmbuf after it's done testing and setting bits.
362 *
363 * NOTE: This function is typically called without a lock on the heap page,
364 * so somebody else could change the bit just after we look at it. In fact,
365 * since we don't lock the visibility map page either, it's even possible that
366 * someone else could have changed the bit just before we look at it, but yet
367 * we might see the old value. It is the caller's responsibility to deal with
368 * all concurrency issues!
369 */
370 uint8
371 visibilitymap_get_status(Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
372 {
373 BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
374 uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
375 uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
376 char *map;
377 uint8 result;
378
379 #ifdef TRACE_VISIBILITYMAP
380 elog(DEBUG1, "vm_get_status %s %d", RelationGetRelationName(rel), heapBlk);
381 #endif
382
383 /* Reuse the old pinned buffer if possible */
384 if (BufferIsValid(*vmbuf))
385 {
386 if (BufferGetBlockNumber(*vmbuf) != mapBlock)
387 {
388 ReleaseBuffer(*vmbuf);
389 *vmbuf = InvalidBuffer;
390 }
391 }
392
393 if (!BufferIsValid(*vmbuf))
394 {
395 *vmbuf = vm_readbuf(rel, mapBlock, false);
396 if (!BufferIsValid(*vmbuf))
397 return false;
398 }
399
400 map = PageGetContents(BufferGetPage(*vmbuf));
401
402 /*
403 * A single byte read is atomic. There could be memory-ordering effects
404 * here, but for performance reasons we make it the caller's job to worry
405 * about that.
406 */
407 result = ((map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS);
408 return result;
409 }
410
411 /*
412 * visibilitymap_count - count number of bits set in visibility map
413 *
414 * Note: we ignore the possibility of race conditions when the table is being
415 * extended concurrently with the call. New pages added to the table aren't
416 * going to be marked all-visible or all-frozen, so they won't affect the result.
417 */
418 void
419 visibilitymap_count(Relation rel, BlockNumber *all_visible, BlockNumber *all_frozen)
420 {
421 BlockNumber mapBlock;
422 BlockNumber nvisible = 0;
423 BlockNumber nfrozen = 0;
424
425 /* all_visible must be specified */
426 Assert(all_visible);
427
428 for (mapBlock = 0;; mapBlock++)
429 {
430 Buffer mapBuffer;
431 uint64 *map;
432
433 /*
434 * Read till we fall off the end of the map. We assume that any extra
435 * bytes in the last page are zeroed, so we don't bother excluding
436 * them from the count.
437 */
438 mapBuffer = vm_readbuf(rel, mapBlock, false);
439 if (!BufferIsValid(mapBuffer))
440 break;
441
442 /*
443 * We choose not to lock the page, since the result is going to be
444 * immediately stale anyway if anyone is concurrently setting or
445 * clearing bits, and we only really need an approximate value.
446 */
447 map = (uint64 *) PageGetContents(BufferGetPage(mapBuffer));
448
449 nvisible += pg_popcount_masked((const char *) map, MAPSIZE, VISIBLE_MASK8);
450 if (all_frozen)
451 nfrozen += pg_popcount_masked((const char *) map, MAPSIZE, FROZEN_MASK8);
452
453 ReleaseBuffer(mapBuffer);
454 }
455
456 *all_visible = nvisible;
457 if (all_frozen)
458 *all_frozen = nfrozen;
459 }
460
461 /*
462 * visibilitymap_prepare_truncate -
463 * prepare for truncation of the visibility map
464 *
465 * nheapblocks is the new size of the heap.
466 *
467 * Return the number of blocks of new visibility map.
468 * If it's InvalidBlockNumber, there is nothing to truncate;
469 * otherwise the caller is responsible for calling smgrtruncate()
470 * to truncate the visibility map pages.
471 */
472 BlockNumber
473 visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
474 {
475 BlockNumber newnblocks;
476
477 /* last remaining block, byte, and bit */
478 BlockNumber truncBlock = HEAPBLK_TO_MAPBLOCK(nheapblocks);
479 uint32 truncByte = HEAPBLK_TO_MAPBYTE(nheapblocks);
480 uint8 truncOffset = HEAPBLK_TO_OFFSET(nheapblocks);
481
482 #ifdef TRACE_VISIBILITYMAP
483 elog(DEBUG1, "vm_truncate %s %d", RelationGetRelationName(rel), nheapblocks);
484 #endif
485
486 /*
487 * If no visibility map has been created yet for this relation, there's
488 * nothing to truncate.
489 */
490 if (!smgrexists(RelationGetSmgr(rel), VISIBILITYMAP_FORKNUM))
491 return InvalidBlockNumber;
492
493 /*
494 * Unless the new size is exactly at a visibility map page boundary, the
495 * tail bits in the last remaining map page, representing truncated heap
496 * blocks, need to be cleared. This is not only tidy, but also necessary
497 * because we don't get a chance to clear the bits if the heap is extended
498 * again.
499 */
500 if (truncByte != 0 || truncOffset != 0)
501 {
502 Buffer mapBuffer;
503 Page page;
504 char *map;
505
506 newnblocks = truncBlock + 1;
507
508 mapBuffer = vm_readbuf(rel, truncBlock, false);
509 if (!BufferIsValid(mapBuffer))
510 {
511 /* nothing to do, the file was already smaller */
512 return InvalidBlockNumber;
513 }
514
515 page = BufferGetPage(mapBuffer);
516 map = PageGetContents(page);
517
518 LockBuffer(mapBuffer, BUFFER_LOCK_EXCLUSIVE);
519
520 /* NO EREPORT(ERROR) from here till changes are logged */
521 START_CRIT_SECTION();
522
523 /* Clear out the unwanted bytes. */
524 MemSet(&map[truncByte + 1], 0, MAPSIZE - (truncByte + 1));
525
526 /*----
527 * Mask out the unwanted bits of the last remaining byte.
528 *
529 * ((1 << 0) - 1) = 00000000
530 * ((1 << 1) - 1) = 00000001
531 * ...
532 * ((1 << 6) - 1) = 00111111
533 * ((1 << 7) - 1) = 01111111
534 *----
535 */
536 map[truncByte] &= (1 << truncOffset) - 1;
537
538 /*
539 * Truncation of a relation is WAL-logged at a higher-level, and we
540 * will be called at WAL replay. But if checksums are enabled, we need
541 * to still write a WAL record to protect against a torn page, if the
542 * page is flushed to disk before the truncation WAL record. We cannot
543 * use MarkBufferDirtyHint here, because that will not dirty the page
544 * during recovery.
545 */
546 MarkBufferDirty(mapBuffer);
547 if (!InRecovery && RelationNeedsWAL(rel) && XLogHintBitIsNeeded())
548 log_newpage_buffer(mapBuffer, false);
549
550 END_CRIT_SECTION();
551
552 UnlockReleaseBuffer(mapBuffer);
553 }
554 else
555 newnblocks = truncBlock;
556
557 if (smgrnblocks(RelationGetSmgr(rel), VISIBILITYMAP_FORKNUM) <= newnblocks)
558 {
559 /* nothing to do, the file was already smaller than requested size */
560 return InvalidBlockNumber;
561 }
562
563 return newnblocks;
564 }
565
566 /*
567 * visibilitymap_truncation_length -
568 * compute truncation length for visibility map
569 *
570 * Given a proposed truncation length for the main fork, compute the
571 * correct truncation length for the visibility map. Should return the
572 * same answer as visibilitymap_prepare_truncate(), but without modifying
573 * anything.
574 */
575 BlockNumber
576 visibilitymap_truncation_length(BlockNumber nheapblocks)
577 {
578 return HEAPBLK_TO_MAPBLOCK_LIMIT(nheapblocks);
579 }
580
581 /*
582 * Read a visibility map page.
583 *
584 * If the page doesn't exist, InvalidBuffer is returned, or if 'extend' is
585 * true, the visibility map file is extended.
586 */
587 static Buffer
588 vm_readbuf(Relation rel, BlockNumber blkno, bool extend)
589 {
590 Buffer buf;
591 SMgrRelation reln;
592
593 /*
594 * Caution: re-using this smgr pointer could fail if the relcache entry
595 * gets closed. It's safe as long as we only do smgr-level operations
596 * between here and the last use of the pointer.
597 */
598 reln = RelationGetSmgr(rel);
599
600 /*
601 * If we haven't cached the size of the visibility map fork yet, check it
602 * first.
603 */
604 if (reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] == InvalidBlockNumber)
605 {
606 if (smgrexists(reln, VISIBILITYMAP_FORKNUM))
607 smgrnblocks(reln, VISIBILITYMAP_FORKNUM);
608 else
609 reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] = 0;
610 }
611
612 /*
613 * For reading we use ZERO_ON_ERROR mode, and initialize the page if
614 * necessary. It's always safe to clear bits, so it's better to clear
615 * corrupt pages than error out.
616 *
617 * We use the same path below to initialize pages when extending the
618 * relation, as a concurrent extension can end up with vm_extend()
619 * returning an already-initialized page.
620 */
621 if (blkno >= reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM])
622 {
623 if (extend)
624 buf = vm_extend(rel, blkno + 1);
625 else
626 return InvalidBuffer;
627 }
628 else
629 buf = ReadBufferExtended(rel, VISIBILITYMAP_FORKNUM, blkno,
630 RBM_ZERO_ON_ERROR, NULL);
631
632 /*
633 * Initializing the page when needed is trickier than it looks, because of
634 * the possibility of multiple backends doing this concurrently, and our
635 * desire to not uselessly take the buffer lock in the normal path where
636 * the page is OK. We must take the lock to initialize the page, so
637 * recheck page newness after we have the lock, in case someone else
638 * already did it. Also, because we initially check PageIsNew with no
639 * lock, it's possible to fall through and return the buffer while someone
640 * else is still initializing the page (i.e., we might see pd_upper as set
641 * but other page header fields are still zeroes). This is harmless for
642 * callers that will take a buffer lock themselves, but some callers
643 * inspect the page without any lock at all. The latter is OK only so
644 * long as it doesn't depend on the page header having correct contents.
645 * Current usage is safe because PageGetContents() does not require that.
646 */
647 if (PageIsNew(BufferGetPage(buf)))
648 {
649 LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
650 if (PageIsNew(BufferGetPage(buf)))
651 PageInit(BufferGetPage(buf), BLCKSZ, 0);
652 LockBuffer(buf, BUFFER_LOCK_UNLOCK);
653 }
654 return buf;
655 }
656
657 /*
658 * Ensure that the visibility map fork is at least vm_nblocks long, extending
659 * it if necessary with zeroed pages.
660 */
661 static Buffer
662 vm_extend(Relation rel, BlockNumber vm_nblocks)
663 {
664 Buffer buf;
665
666 buf = ExtendBufferedRelTo(BMR_REL(rel), VISIBILITYMAP_FORKNUM, NULL,
667 EB_CREATE_FORK_IF_NEEDED |
668 EB_CLEAR_SIZE_CACHE,
669 vm_nblocks,
670 RBM_ZERO_ON_ERROR);
671
672 /*
673 * Send a shared-inval message to force other backends to close any smgr
674 * references they may have for this rel, which we are about to change.
675 * This is a useful optimization because it means that backends don't have
676 * to keep checking for creation or extension of the file, which happens
677 * infrequently.
678 */
679 CacheInvalidateSmgr(RelationGetSmgr(rel)->smgr_rlocator);
680
681 return buf;
682 }