PostgreSQL Source Code  git master
visibilitymap.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * visibilitymap.c
4  * bitmap for tracking visibility of heap tuples
5  *
6  * Portions Copyright (c) 1996-2022, 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/lmgr.h"
97 #include "storage/smgr.h"
98 #include "utils/inval.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_MAPBYTE(x) (((x) % HEAPBLOCKS_PER_PAGE) / HEAPBLOCKS_PER_BYTE)
119 #define HEAPBLK_TO_OFFSET(x) (((x) % HEAPBLOCKS_PER_BYTE) * BITS_PER_HEAPBLOCK)
120 
121 /* Masks for counting subsets of bits in the visibility map. */
122 #define VISIBLE_MASK64 UINT64CONST(0x5555555555555555) /* The lower bit of each
123  * bit pair */
124 #define FROZEN_MASK64 UINT64CONST(0xaaaaaaaaaaaaaaaa) /* The upper bit of each
125  * bit pair */
126 
127 /* prototypes for internal routines */
128 static Buffer vm_readbuf(Relation rel, BlockNumber blkno, bool extend);
129 static void 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 bool
141 {
142  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
143  int mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
144  int mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
145  uint8 mask = flags << mapOffset;
146  char *map;
147  bool cleared = false;
148 
150 
151 #ifdef TRACE_VISIBILITYMAP
152  elog(DEBUG1, "vm_clear %s %d", RelationGetRelationName(rel), heapBlk);
153 #endif
154 
155  if (!BufferIsValid(buf) || BufferGetBlockNumber(buf) != mapBlock)
156  elog(ERROR, "wrong buffer passed to visibilitymap_clear");
157 
160 
161  if (map[mapByte] & mask)
162  {
163  map[mapByte] &= ~mask;
164 
166  cleared = true;
167  }
168 
170 
171  return cleared;
172 }
173 
174 /*
175  * visibilitymap_pin - pin a map page for setting a bit
176  *
177  * Setting a bit in the visibility map is a two-phase operation. First, call
178  * visibilitymap_pin, to pin the visibility map page containing the bit for
179  * the heap page. Because that can require I/O to read the map page, you
180  * shouldn't hold a lock on the heap page while doing that. Then, call
181  * visibilitymap_set to actually set the bit.
182  *
183  * On entry, *buf should be InvalidBuffer or a valid buffer returned by
184  * an earlier call to visibilitymap_pin or visibilitymap_get_status on the same
185  * relation. On return, *buf is a valid buffer with the map page containing
186  * the bit for heapBlk.
187  *
188  * If the page doesn't exist in the map file yet, it is extended.
189  */
190 void
192 {
193  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
194 
195  /* Reuse the old pinned buffer if possible */
196  if (BufferIsValid(*buf))
197  {
198  if (BufferGetBlockNumber(*buf) == mapBlock)
199  return;
200 
201  ReleaseBuffer(*buf);
202  }
203  *buf = vm_readbuf(rel, mapBlock, true);
204 }
205 
206 /*
207  * visibilitymap_pin_ok - do we already have the correct page pinned?
208  *
209  * On entry, buf should be InvalidBuffer or a valid buffer returned by
210  * an earlier call to visibilitymap_pin or visibilitymap_get_status on the same
211  * relation. The return value indicates whether the buffer covers the
212  * given heapBlk.
213  */
214 bool
216 {
217  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
218 
219  return BufferIsValid(buf) && BufferGetBlockNumber(buf) == mapBlock;
220 }
221 
222 /*
223  * visibilitymap_set - set bit(s) on a previously pinned page
224  *
225  * recptr is the LSN of the XLOG record we're replaying, if we're in recovery,
226  * or InvalidXLogRecPtr in normal running. The page LSN is advanced to the
227  * one provided; in normal running, we generate a new XLOG record and set the
228  * page LSN to that value. cutoff_xid is the largest xmin on the page being
229  * marked all-visible; it is needed for Hot Standby, and can be
230  * InvalidTransactionId if the page contains no tuples. It can also be set
231  * to InvalidTransactionId when a page that is already all-visible is being
232  * marked all-frozen.
233  *
234  * Caller is expected to set the heap page's PD_ALL_VISIBLE bit before calling
235  * this function. Except in recovery, caller should also pass the heap
236  * buffer. When checksums are enabled and we're not in recovery, we must add
237  * the heap buffer to the WAL chain to protect it from being torn.
238  *
239  * You must pass a buffer containing the correct map page to this function.
240  * Call visibilitymap_pin first to pin the right one. This function doesn't do
241  * any I/O.
242  */
243 void
244 visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf,
245  XLogRecPtr recptr, Buffer vmBuf, TransactionId cutoff_xid,
246  uint8 flags)
247 {
248  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
249  uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
250  uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
251  Page page;
252  uint8 *map;
253 
254 #ifdef TRACE_VISIBILITYMAP
255  elog(DEBUG1, "vm_set %s %d", RelationGetRelationName(rel), heapBlk);
256 #endif
257 
259  Assert(InRecovery || BufferIsValid(heapBuf));
261 
262  /* Check that we have the right heap page pinned, if present */
263  if (BufferIsValid(heapBuf) && BufferGetBlockNumber(heapBuf) != heapBlk)
264  elog(ERROR, "wrong heap buffer passed to visibilitymap_set");
265 
266  /* Check that we have the right VM page pinned */
267  if (!BufferIsValid(vmBuf) || BufferGetBlockNumber(vmBuf) != mapBlock)
268  elog(ERROR, "wrong VM buffer passed to visibilitymap_set");
269 
270  page = BufferGetPage(vmBuf);
271  map = (uint8 *) PageGetContents(page);
273 
274  if (flags != (map[mapByte] >> mapOffset & VISIBILITYMAP_VALID_BITS))
275  {
277 
278  map[mapByte] |= (flags << mapOffset);
279  MarkBufferDirty(vmBuf);
280 
281  if (RelationNeedsWAL(rel))
282  {
283  if (XLogRecPtrIsInvalid(recptr))
284  {
285  Assert(!InRecovery);
286  recptr = log_heap_visible(rel->rd_node, heapBuf, vmBuf,
287  cutoff_xid, flags);
288 
289  /*
290  * If data checksums are enabled (or wal_log_hints=on), we
291  * need to protect the heap page from being torn.
292  */
293  if (XLogHintBitIsNeeded())
294  {
295  Page heapPage = BufferGetPage(heapBuf);
296 
297  /* caller is expected to set PD_ALL_VISIBLE first */
298  Assert(PageIsAllVisible(heapPage));
299  PageSetLSN(heapPage, recptr);
300  }
301  }
302  PageSetLSN(page, recptr);
303  }
304 
306  }
307 
309 }
310 
311 /*
312  * visibilitymap_get_status - get status of bits
313  *
314  * Are all tuples on heapBlk visible to all or are marked frozen, according
315  * to the visibility map?
316  *
317  * On entry, *buf should be InvalidBuffer or a valid buffer returned by an
318  * earlier call to visibilitymap_pin or visibilitymap_get_status on the same
319  * relation. On return, *buf is a valid buffer with the map page containing
320  * the bit for heapBlk, or InvalidBuffer. The caller is responsible for
321  * releasing *buf after it's done testing and setting bits.
322  *
323  * NOTE: This function is typically called without a lock on the heap page,
324  * so somebody else could change the bit just after we look at it. In fact,
325  * since we don't lock the visibility map page either, it's even possible that
326  * someone else could have changed the bit just before we look at it, but yet
327  * we might see the old value. It is the caller's responsibility to deal with
328  * all concurrency issues!
329  */
330 uint8
332 {
333  BlockNumber mapBlock = HEAPBLK_TO_MAPBLOCK(heapBlk);
334  uint32 mapByte = HEAPBLK_TO_MAPBYTE(heapBlk);
335  uint8 mapOffset = HEAPBLK_TO_OFFSET(heapBlk);
336  char *map;
337  uint8 result;
338 
339 #ifdef TRACE_VISIBILITYMAP
340  elog(DEBUG1, "vm_get_status %s %d", RelationGetRelationName(rel), heapBlk);
341 #endif
342 
343  /* Reuse the old pinned buffer if possible */
344  if (BufferIsValid(*buf))
345  {
346  if (BufferGetBlockNumber(*buf) != mapBlock)
347  {
348  ReleaseBuffer(*buf);
349  *buf = InvalidBuffer;
350  }
351  }
352 
353  if (!BufferIsValid(*buf))
354  {
355  *buf = vm_readbuf(rel, mapBlock, false);
356  if (!BufferIsValid(*buf))
357  return false;
358  }
359 
361 
362  /*
363  * A single byte read is atomic. There could be memory-ordering effects
364  * here, but for performance reasons we make it the caller's job to worry
365  * about that.
366  */
367  result = ((map[mapByte] >> mapOffset) & VISIBILITYMAP_VALID_BITS);
368  return result;
369 }
370 
371 /*
372  * visibilitymap_count - count number of bits set in visibility map
373  *
374  * Note: we ignore the possibility of race conditions when the table is being
375  * extended concurrently with the call. New pages added to the table aren't
376  * going to be marked all-visible or all-frozen, so they won't affect the result.
377  */
378 void
379 visibilitymap_count(Relation rel, BlockNumber *all_visible, BlockNumber *all_frozen)
380 {
381  BlockNumber mapBlock;
382  BlockNumber nvisible = 0;
383  BlockNumber nfrozen = 0;
384 
385  /* all_visible must be specified */
386  Assert(all_visible);
387 
388  for (mapBlock = 0;; mapBlock++)
389  {
390  Buffer mapBuffer;
391  uint64 *map;
392  int i;
393 
394  /*
395  * Read till we fall off the end of the map. We assume that any extra
396  * bytes in the last page are zeroed, so we don't bother excluding
397  * them from the count.
398  */
399  mapBuffer = vm_readbuf(rel, mapBlock, false);
400  if (!BufferIsValid(mapBuffer))
401  break;
402 
403  /*
404  * We choose not to lock the page, since the result is going to be
405  * immediately stale anyway if anyone is concurrently setting or
406  * clearing bits, and we only really need an approximate value.
407  */
408  map = (uint64 *) PageGetContents(BufferGetPage(mapBuffer));
409 
410  StaticAssertStmt(MAPSIZE % sizeof(uint64) == 0,
411  "unsupported MAPSIZE");
412  if (all_frozen == NULL)
413  {
414  for (i = 0; i < MAPSIZE / sizeof(uint64); i++)
415  nvisible += pg_popcount64(map[i] & VISIBLE_MASK64);
416  }
417  else
418  {
419  for (i = 0; i < MAPSIZE / sizeof(uint64); i++)
420  {
421  nvisible += pg_popcount64(map[i] & VISIBLE_MASK64);
422  nfrozen += pg_popcount64(map[i] & FROZEN_MASK64);
423  }
424  }
425 
426  ReleaseBuffer(mapBuffer);
427  }
428 
429  *all_visible = nvisible;
430  if (all_frozen)
431  *all_frozen = nfrozen;
432 }
433 
434 /*
435  * visibilitymap_prepare_truncate -
436  * prepare for truncation of the visibility map
437  *
438  * nheapblocks is the new size of the heap.
439  *
440  * Return the number of blocks of new visibility map.
441  * If it's InvalidBlockNumber, there is nothing to truncate;
442  * otherwise the caller is responsible for calling smgrtruncate()
443  * to truncate the visibility map pages.
444  */
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 
491  LockBuffer(mapBuffer, BUFFER_LOCK_EXCLUSIVE);
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 }
538 
539 /*
540  * Read a visibility map page.
541  *
542  * If the page doesn't exist, InvalidBuffer is returned, or if 'extend' is
543  * true, the visibility map file is extended.
544  */
545 static Buffer
546 vm_readbuf(Relation rel, BlockNumber blkno, bool extend)
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  /* Handle requests beyond EOF */
571  if (blkno >= reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM])
572  {
573  if (extend)
574  vm_extend(rel, blkno + 1);
575  else
576  return InvalidBuffer;
577  }
578 
579  /*
580  * Use ZERO_ON_ERROR mode, and initialize the page if necessary. It's
581  * always safe to clear bits, so it's better to clear corrupt pages than
582  * error out.
583  *
584  * The initialize-the-page part is trickier than it looks, because of the
585  * possibility of multiple backends doing this concurrently, and our
586  * desire to not uselessly take the buffer lock in the normal path where
587  * the page is OK. We must take the lock to initialize the page, so
588  * recheck page newness after we have the lock, in case someone else
589  * already did it. Also, because we initially check PageIsNew with no
590  * lock, it's possible to fall through and return the buffer while someone
591  * else is still initializing the page (i.e., we might see pd_upper as set
592  * but other page header fields are still zeroes). This is harmless for
593  * callers that will take a buffer lock themselves, but some callers
594  * inspect the page without any lock at all. The latter is OK only so
595  * long as it doesn't depend on the page header having correct contents.
596  * Current usage is safe because PageGetContents() does not require that.
597  */
599  RBM_ZERO_ON_ERROR, NULL);
601  {
604  PageInit(BufferGetPage(buf), BLCKSZ, 0);
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  */
614 static void
615 vm_extend(Relation rel, BlockNumber vm_nblocks)
616 {
617  BlockNumber vm_nblocks_now;
618  PGAlignedBlock pg;
619  SMgrRelation reln;
620 
621  PageInit((Page) pg.data, BLCKSZ, 0);
622 
623  /*
624  * We use the relation extension lock to lock out other backends trying to
625  * extend the visibility map at the same time. It also locks out extension
626  * of the main fork, unnecessarily, but extending the visibility map
627  * happens seldom enough that it doesn't seem worthwhile to have a
628  * separate lock tag type for it.
629  *
630  * Note that another backend might have extended or created the relation
631  * by the time we get the lock.
632  */
634 
635  /*
636  * Caution: re-using this smgr pointer could fail if the relcache entry
637  * gets closed. It's safe as long as we only do smgr-level operations
638  * between here and the last use of the pointer.
639  */
640  reln = RelationGetSmgr(rel);
641 
642  /*
643  * Create the file first if it doesn't exist. If smgr_vm_nblocks is
644  * positive then it must exist, no need for an smgrexists call.
645  */
646  if ((reln->smgr_cached_nblocks[VISIBILITYMAP_FORKNUM] == 0 ||
649  smgrcreate(reln, VISIBILITYMAP_FORKNUM, false);
650 
651  /* Invalidate cache so that smgrnblocks() asks the kernel. */
653  vm_nblocks_now = smgrnblocks(reln, VISIBILITYMAP_FORKNUM);
654 
655  /* Now extend the file */
656  while (vm_nblocks_now < vm_nblocks)
657  {
658  PageSetChecksumInplace((Page) pg.data, vm_nblocks_now);
659 
660  smgrextend(reln, VISIBILITYMAP_FORKNUM, vm_nblocks_now, pg.data, false);
661  vm_nblocks_now++;
662  }
663 
664  /*
665  * Send a shared-inval message to force other backends to close any smgr
666  * references they may have for this rel, which we are about to change.
667  * This is a useful optimization because it means that backends don't have
668  * to keep checking for creation or extension of the file, which happens
669  * infrequently.
670  */
672 
674 }
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:2755
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3915
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3938
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1573
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4156
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:749
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
@ RBM_ZERO_ON_ERROR
Definition: bufmgr.h:44
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1539
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42
Pointer Page
Definition: bufpage.h:78
#define PageIsAllVisible(page)
Definition: bufpage.h:384
#define PageSetLSN(page, lsn)
Definition: bufpage.h:367
#define PageIsNew(page)
Definition: bufpage.h:228
#define PageGetContents(page)
Definition: bufpage.h:245
unsigned int uint32
Definition: c.h:441
unsigned char uint8
Definition: c.h:439
#define MemSet(start, val, len)
Definition: c.h:1008
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:918
uint32 TransactionId
Definition: c.h:587
#define DEBUG1
Definition: elog.h:24
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
XLogRecPtr log_heap_visible(RelFileNode rnode, Buffer heap_buffer, Buffer vm_buffer, TransactionId cutoff_xid, uint8 vmflags)
Definition: heapam.c:8192
void CacheInvalidateSmgr(RelFileNodeBackend rnode)
Definition: inval.c:1462
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:431
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:481
#define ExclusiveLock
Definition: lockdefs.h:42
#define START_CRIT_SECTION()
Definition: miscadmin.h:148
#define END_CRIT_SECTION()
Definition: miscadmin.h:150
int pg_popcount64(uint64 word)
Definition: pg_bitutils.c:284
static char * buf
Definition: pg_test_fsync.c:67
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:556
#define RelationGetRelationName(relation)
Definition: rel.h:523
#define RelationNeedsWAL(relation)
Definition: rel.h:613
@ VISIBILITYMAP_FORKNUM
Definition: relpath.h:45
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:579
void smgrextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:493
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:369
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:247
RelFileNode rd_node
Definition: rel.h:56
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:54
char data[BLCKSZ]
Definition: c.h:1138
static void vm_extend(Relation rel, BlockNumber vm_nblocks)
#define MAPSIZE
bool visibilitymap_clear(Relation rel, BlockNumber heapBlk, Buffer buf, uint8 flags)
#define VISIBLE_MASK64
void visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf, XLogRecPtr recptr, Buffer vmBuf, TransactionId cutoff_xid, uint8 flags)
#define HEAPBLK_TO_OFFSET(x)
bool visibilitymap_pin_ok(BlockNumber heapBlk, Buffer buf)
uint8 visibilitymap_get_status(Relation rel, BlockNumber heapBlk, Buffer *buf)
BlockNumber visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
void visibilitymap_pin(Relation rel, BlockNumber heapBlk, Buffer *buf)
void visibilitymap_count(Relation rel, BlockNumber *all_visible, BlockNumber *all_frozen)
static Buffer vm_readbuf(Relation rel, BlockNumber blkno, bool extend)
#define FROZEN_MASK64
#define HEAPBLK_TO_MAPBLOCK(x)
#define HEAPBLK_TO_MAPBYTE(x)
#define VISIBILITYMAP_VALID_BITS
#define XLogHintBitIsNeeded()
Definition: xlog.h:115
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1177
bool InRecovery
Definition: xlogutils.c:53