PostgreSQL Source Code  git master
brin_revmap.c File Reference
#include "postgres.h"
#include "access/brin_page.h"
#include "access/brin_pageops.h"
#include "access/brin_revmap.h"
#include "access/brin_tuple.h"
#include "access/brin_xlog.h"
#include "access/rmgr.h"
#include "access/xloginsert.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "utils/rel.h"
Include dependency graph for brin_revmap.c:

Go to the source code of this file.

Data Structures

struct  BrinRevmap
 

Macros

#define HEAPBLK_TO_REVMAP_BLK(pagesPerRange, heapBlk)    ((heapBlk / pagesPerRange) / REVMAP_PAGE_MAXITEMS)
 
#define HEAPBLK_TO_REVMAP_INDEX(pagesPerRange, heapBlk)    ((heapBlk / pagesPerRange) % REVMAP_PAGE_MAXITEMS)
 

Functions

static BlockNumber revmap_get_blkno (BrinRevmap *revmap, BlockNumber heapBlk)
 
static Buffer revmap_get_buffer (BrinRevmap *revmap, BlockNumber heapBlk)
 
static BlockNumber revmap_extend_and_get_blkno (BrinRevmap *revmap, BlockNumber heapBlk)
 
static void revmap_physical_extend (BrinRevmap *revmap)
 
BrinRevmapbrinRevmapInitialize (Relation idxrel, BlockNumber *pagesPerRange)
 
void brinRevmapTerminate (BrinRevmap *revmap)
 
void brinRevmapExtend (BrinRevmap *revmap, BlockNumber heapBlk)
 
Buffer brinLockRevmapPageForUpdate (BrinRevmap *revmap, BlockNumber heapBlk)
 
void brinSetHeapBlockItemptr (Buffer buf, BlockNumber pagesPerRange, BlockNumber heapBlk, ItemPointerData tid)
 
BrinTuplebrinGetTupleForHeapBlock (BrinRevmap *revmap, BlockNumber heapBlk, Buffer *buf, OffsetNumber *off, Size *size, int mode)
 
bool brinRevmapDesummarizeRange (Relation idxrel, BlockNumber heapBlk)
 

Macro Definition Documentation

◆ HEAPBLK_TO_REVMAP_BLK

#define HEAPBLK_TO_REVMAP_BLK (   pagesPerRange,
  heapBlk 
)     ((heapBlk / pagesPerRange) / REVMAP_PAGE_MAXITEMS)

Definition at line 41 of file brin_revmap.c.

◆ HEAPBLK_TO_REVMAP_INDEX

#define HEAPBLK_TO_REVMAP_INDEX (   pagesPerRange,
  heapBlk 
)     ((heapBlk / pagesPerRange) % REVMAP_PAGE_MAXITEMS)

Definition at line 43 of file brin_revmap.c.

Function Documentation

◆ brinGetTupleForHeapBlock()

BrinTuple* brinGetTupleForHeapBlock ( BrinRevmap revmap,
BlockNumber  heapBlk,
Buffer buf,
OffsetNumber off,
Size size,
int  mode 
)

Definition at line 195 of file brin_revmap.c.

197 {
198  Relation idxRel = revmap->rm_irel;
199  BlockNumber mapBlk;
200  RevmapContents *contents;
201  ItemPointerData *iptr;
202  BlockNumber blk;
203  Page page;
204  ItemId lp;
205  BrinTuple *tup;
206  ItemPointerData previptr;
207 
208  /* normalize the heap block number to be the first page in the range */
209  heapBlk = (heapBlk / revmap->rm_pagesPerRange) * revmap->rm_pagesPerRange;
210 
211  /*
212  * Compute the revmap page number we need. If Invalid is returned (i.e.,
213  * the revmap page hasn't been created yet), the requested page range is
214  * not summarized.
215  */
216  mapBlk = revmap_get_blkno(revmap, heapBlk);
217  if (mapBlk == InvalidBlockNumber)
218  {
219  *off = InvalidOffsetNumber;
220  return NULL;
221  }
222 
223  ItemPointerSetInvalid(&previptr);
224  for (;;)
225  {
227 
228  if (revmap->rm_currBuf == InvalidBuffer ||
229  BufferGetBlockNumber(revmap->rm_currBuf) != mapBlk)
230  {
231  if (revmap->rm_currBuf != InvalidBuffer)
232  ReleaseBuffer(revmap->rm_currBuf);
233 
234  Assert(mapBlk != InvalidBlockNumber);
235  revmap->rm_currBuf = ReadBuffer(revmap->rm_irel, mapBlk);
236  }
237 
239 
240  contents = (RevmapContents *)
242  iptr = contents->rm_tids;
243  iptr += HEAPBLK_TO_REVMAP_INDEX(revmap->rm_pagesPerRange, heapBlk);
244 
245  if (!ItemPointerIsValid(iptr))
246  {
248  return NULL;
249  }
250 
251  /*
252  * Check the TID we got in a previous iteration, if any, and save the
253  * current TID we got from the revmap; if we loop, we can sanity-check
254  * that the next one we get is different. Otherwise we might be stuck
255  * looping forever if the revmap is somehow badly broken.
256  */
257  if (ItemPointerIsValid(&previptr) && ItemPointerEquals(&previptr, iptr))
258  ereport(ERROR,
259  (errcode(ERRCODE_INDEX_CORRUPTED),
260  errmsg_internal("corrupted BRIN index: inconsistent range map")));
261  previptr = *iptr;
262 
263  blk = ItemPointerGetBlockNumber(iptr);
264  *off = ItemPointerGetOffsetNumber(iptr);
265 
267 
268  /* Ok, got a pointer to where the BrinTuple should be. Fetch it. */
269  if (!BufferIsValid(*buf) || BufferGetBlockNumber(*buf) != blk)
270  {
271  if (BufferIsValid(*buf))
272  ReleaseBuffer(*buf);
273  *buf = ReadBuffer(idxRel, blk);
274  }
275  LockBuffer(*buf, mode);
276  page = BufferGetPage(*buf);
277 
278  /* If we land on a revmap page, start over */
279  if (BRIN_IS_REGULAR_PAGE(page))
280  {
281  /*
282  * If the offset number is greater than what's in the page, it's
283  * possible that the range was desummarized concurrently. Just
284  * return NULL to handle that case.
285  */
286  if (*off > PageGetMaxOffsetNumber(page))
287  {
289  return NULL;
290  }
291 
292  lp = PageGetItemId(page, *off);
293  if (ItemIdIsUsed(lp))
294  {
295  tup = (BrinTuple *) PageGetItem(page, lp);
296 
297  if (tup->bt_blkno == heapBlk)
298  {
299  if (size)
300  *size = ItemIdGetLength(lp);
301  /* found it! */
302  return tup;
303  }
304  }
305  }
306 
307  /*
308  * No luck. Assume that the revmap was updated concurrently.
309  */
311  }
312  /* not reached, but keep compiler quiet */
313  return NULL;
314 }
uint32 BlockNumber
Definition: block.h:31
#define InvalidBlockNumber
Definition: block.h:33
#define BRIN_IS_REGULAR_PAGE(page)
Definition: brin_page.h:57
static BlockNumber revmap_get_blkno(BrinRevmap *revmap, BlockNumber heapBlk)
Definition: brin_revmap.c:443
#define HEAPBLK_TO_REVMAP_INDEX(pagesPerRange, heapBlk)
Definition: brin_revmap.c:43
#define InvalidBuffer
Definition: buf.h:25
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3378
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4561
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4796
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:735
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:157
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:158
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:350
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:301
static char * PageGetContents(Page page)
Definition: bufpage.h:254
Pointer Page
Definition: bufpage.h:78
static Item PageGetItem(Page page, ItemId itemId)
Definition: bufpage.h:351
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:240
static OffsetNumber PageGetMaxOffsetNumber(Page page)
Definition: bufpage.h:369
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1162
int errcode(int sqlerrcode)
Definition: elog.c:860
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define ItemIdIsUsed(itemId)
Definition: itemid.h:92
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:35
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition: itemptr.h:184
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition: itemptr.h:124
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition: itemptr.h:103
static bool ItemPointerIsValid(const ItemPointerData *pointer)
Definition: itemptr.h:83
Assert(fmt[strlen(fmt) - 1] !='\n')
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define InvalidOffsetNumber
Definition: off.h:26
static PgChecksumMode mode
Definition: pg_checksums.c:56
static char * buf
Definition: pg_test_fsync.c:73
BlockNumber rm_pagesPerRange
Definition: brin_revmap.c:50
Buffer rm_currBuf
Definition: brin_revmap.c:53
Relation rm_irel
Definition: brin_revmap.c:49
BlockNumber bt_blkno
Definition: brin_tuple.h:66
ItemPointerData rm_tids[1]
Definition: brin_page.h:85

References Assert(), BRIN_IS_REGULAR_PAGE, BrinTuple::bt_blkno, buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage(), BufferIsValid(), CHECK_FOR_INTERRUPTS, ereport, errcode(), errmsg_internal(), ERROR, HEAPBLK_TO_REVMAP_INDEX, InvalidBlockNumber, InvalidBuffer, InvalidOffsetNumber, ItemIdGetLength, ItemIdIsUsed, ItemPointerEquals(), ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), ItemPointerIsValid(), ItemPointerSetInvalid(), LockBuffer(), mode, PageGetContents(), PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), ReadBuffer(), ReleaseBuffer(), revmap_get_blkno(), BrinRevmap::rm_currBuf, BrinRevmap::rm_irel, BrinRevmap::rm_pagesPerRange, and RevmapContents::rm_tids.

Referenced by bringetbitmap(), brininsert(), brinsummarize(), and summarize_range().

◆ brinLockRevmapPageForUpdate()

Buffer brinLockRevmapPageForUpdate ( BrinRevmap revmap,
BlockNumber  heapBlk 
)

Definition at line 135 of file brin_revmap.c.

136 {
137  Buffer rmBuf;
138 
139  rmBuf = revmap_get_buffer(revmap, heapBlk);
141 
142  return rmBuf;
143 }
static Buffer revmap_get_buffer(BrinRevmap *revmap, BlockNumber heapBlk)
Definition: brin_revmap.c:464
int Buffer
Definition: buf.h:23
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:159

References BUFFER_LOCK_EXCLUSIVE, LockBuffer(), and revmap_get_buffer().

Referenced by brin_doinsert(), brin_doupdate(), and brinRevmapDesummarizeRange().

◆ brinRevmapDesummarizeRange()

bool brinRevmapDesummarizeRange ( Relation  idxrel,
BlockNumber  heapBlk 
)

Definition at line 324 of file brin_revmap.c.

325 {
326  BrinRevmap *revmap;
327  BlockNumber pagesPerRange;
328  RevmapContents *contents;
329  ItemPointerData *iptr;
330  ItemPointerData invalidIptr;
331  BlockNumber revmapBlk;
332  Buffer revmapBuf;
333  Buffer regBuf;
334  Page revmapPg;
335  Page regPg;
336  OffsetNumber revmapOffset;
337  OffsetNumber regOffset;
338  ItemId lp;
339 
340  revmap = brinRevmapInitialize(idxrel, &pagesPerRange);
341 
342  revmapBlk = revmap_get_blkno(revmap, heapBlk);
343  if (!BlockNumberIsValid(revmapBlk))
344  {
345  /* revmap page doesn't exist: range not summarized, we're done */
346  brinRevmapTerminate(revmap);
347  return true;
348  }
349 
350  /* Lock the revmap page, obtain the index tuple pointer from it */
351  revmapBuf = brinLockRevmapPageForUpdate(revmap, heapBlk);
352  revmapPg = BufferGetPage(revmapBuf);
353  revmapOffset = HEAPBLK_TO_REVMAP_INDEX(revmap->rm_pagesPerRange, heapBlk);
354 
355  contents = (RevmapContents *) PageGetContents(revmapPg);
356  iptr = contents->rm_tids;
357  iptr += revmapOffset;
358 
359  if (!ItemPointerIsValid(iptr))
360  {
361  /* no index tuple: range not summarized, we're done */
362  LockBuffer(revmapBuf, BUFFER_LOCK_UNLOCK);
363  brinRevmapTerminate(revmap);
364  return true;
365  }
366 
367  regBuf = ReadBuffer(idxrel, ItemPointerGetBlockNumber(iptr));
369  regPg = BufferGetPage(regBuf);
370 
371  /* if this is no longer a regular page, tell caller to start over */
372  if (!BRIN_IS_REGULAR_PAGE(regPg))
373  {
374  LockBuffer(revmapBuf, BUFFER_LOCK_UNLOCK);
376  brinRevmapTerminate(revmap);
377  return false;
378  }
379 
380  regOffset = ItemPointerGetOffsetNumber(iptr);
381  if (regOffset > PageGetMaxOffsetNumber(regPg))
382  ereport(ERROR,
383  (errcode(ERRCODE_INDEX_CORRUPTED),
384  errmsg("corrupted BRIN index: inconsistent range map")));
385 
386  lp = PageGetItemId(regPg, regOffset);
387  if (!ItemIdIsUsed(lp))
388  ereport(ERROR,
389  (errcode(ERRCODE_INDEX_CORRUPTED),
390  errmsg("corrupted BRIN index: inconsistent range map")));
391 
392  /*
393  * Placeholder tuples only appear during unfinished summarization, and we
394  * hold ShareUpdateExclusiveLock, so this function cannot run concurrently
395  * with that. So any placeholder tuples that exist are leftovers from a
396  * crashed or aborted summarization; remove them silently.
397  */
398 
400 
401  ItemPointerSetInvalid(&invalidIptr);
402  brinSetHeapBlockItemptr(revmapBuf, revmap->rm_pagesPerRange, heapBlk,
403  invalidIptr);
404  PageIndexTupleDeleteNoCompact(regPg, regOffset);
405  /* XXX record free space in FSM? */
406 
407  MarkBufferDirty(regBuf);
408  MarkBufferDirty(revmapBuf);
409 
410  if (RelationNeedsWAL(idxrel))
411  {
412  xl_brin_desummarize xlrec;
413  XLogRecPtr recptr;
414 
415  xlrec.pagesPerRange = revmap->rm_pagesPerRange;
416  xlrec.heapBlk = heapBlk;
417  xlrec.regOffset = regOffset;
418 
419  XLogBeginInsert();
420  XLogRegisterData((char *) &xlrec, SizeOfBrinDesummarize);
421  XLogRegisterBuffer(0, revmapBuf, 0);
423  recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_DESUMMARIZE);
424  PageSetLSN(revmapPg, recptr);
425  PageSetLSN(regPg, recptr);
426  }
427 
429 
430  UnlockReleaseBuffer(regBuf);
431  LockBuffer(revmapBuf, BUFFER_LOCK_UNLOCK);
432  brinRevmapTerminate(revmap);
433 
434  return true;
435 }
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
void brinRevmapTerminate(BrinRevmap *revmap)
Definition: brin_revmap.c:101
BrinRevmap * brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange)
Definition: brin_revmap.c:71
void brinSetHeapBlockItemptr(Buffer buf, BlockNumber pagesPerRange, BlockNumber heapBlk, ItemPointerData tid)
Definition: brin_revmap.c:156
Buffer brinLockRevmapPageForUpdate(BrinRevmap *revmap, BlockNumber heapBlk)
Definition: brin_revmap.c:135
#define XLOG_BRIN_DESUMMARIZE
Definition: brin_xlog.h:36
#define SizeOfBrinDesummarize
Definition: brin_xlog.h:142
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4578
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2190
void PageIndexTupleDeleteNoCompact(Page page, OffsetNumber offnum)
Definition: bufpage.c:1295
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:388
int errmsg(const char *fmt,...)
Definition: elog.c:1075
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
uint16 OffsetNumber
Definition: off.h:24
#define RelationNeedsWAL(relation)
Definition: rel.h:627
BlockNumber pagesPerRange
Definition: brin_xlog.h:135
BlockNumber heapBlk
Definition: brin_xlog.h:137
OffsetNumber regOffset
Definition: brin_xlog.h:139
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void XLogRegisterData(char *data, uint32 len)
Definition: xloginsert.c:365
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:475
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:243
void XLogBeginInsert(void)
Definition: xloginsert.c:150
#define REGBUF_STANDARD
Definition: xloginsert.h:34

References BlockNumberIsValid(), BRIN_IS_REGULAR_PAGE, brinLockRevmapPageForUpdate(), brinRevmapInitialize(), brinRevmapTerminate(), brinSetHeapBlockItemptr(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetPage(), END_CRIT_SECTION, ereport, errcode(), errmsg(), ERROR, xl_brin_desummarize::heapBlk, HEAPBLK_TO_REVMAP_INDEX, ItemIdIsUsed, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), ItemPointerIsValid(), ItemPointerSetInvalid(), LockBuffer(), MarkBufferDirty(), PageGetContents(), PageGetItemId(), PageGetMaxOffsetNumber(), PageIndexTupleDeleteNoCompact(), PageSetLSN(), xl_brin_desummarize::pagesPerRange, ReadBuffer(), REGBUF_STANDARD, xl_brin_desummarize::regOffset, RelationNeedsWAL, revmap_get_blkno(), BrinRevmap::rm_pagesPerRange, RevmapContents::rm_tids, SizeOfBrinDesummarize, START_CRIT_SECTION, UnlockReleaseBuffer(), XLOG_BRIN_DESUMMARIZE, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by brin_desummarize_range().

◆ brinRevmapExtend()

void brinRevmapExtend ( BrinRevmap revmap,
BlockNumber  heapBlk 
)

Definition at line 113 of file brin_revmap.c.

114 {
116 
117  mapBlk = revmap_extend_and_get_blkno(revmap, heapBlk);
118 
119  /* Ensure the buffer we got is in the expected range */
120  Assert(mapBlk != InvalidBlockNumber &&
121  mapBlk != BRIN_METAPAGE_BLKNO &&
122  mapBlk <= revmap->rm_lastRevmapPage);
123 }
#define BRIN_METAPAGE_BLKNO
Definition: brin_page.h:75
static BlockNumber revmap_extend_and_get_blkno(BrinRevmap *revmap, BlockNumber heapBlk)
Definition: brin_revmap.c:501
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:171

References Assert(), BRIN_METAPAGE_BLKNO, InvalidBlockNumber, PG_USED_FOR_ASSERTS_ONLY, and revmap_extend_and_get_blkno().

Referenced by brin_doinsert(), and brin_doupdate().

◆ brinRevmapInitialize()

BrinRevmap* brinRevmapInitialize ( Relation  idxrel,
BlockNumber pagesPerRange 
)

Definition at line 71 of file brin_revmap.c.

72 {
73  BrinRevmap *revmap;
74  Buffer meta;
75  BrinMetaPageData *metadata;
76  Page page;
77 
78  meta = ReadBuffer(idxrel, BRIN_METAPAGE_BLKNO);
80  page = BufferGetPage(meta);
81  metadata = (BrinMetaPageData *) PageGetContents(page);
82 
83  revmap = palloc(sizeof(BrinRevmap));
84  revmap->rm_irel = idxrel;
85  revmap->rm_pagesPerRange = metadata->pagesPerRange;
86  revmap->rm_lastRevmapPage = metadata->lastRevmapPage;
87  revmap->rm_metaBuf = meta;
88  revmap->rm_currBuf = InvalidBuffer;
89 
90  *pagesPerRange = metadata->pagesPerRange;
91 
93 
94  return revmap;
95 }
void * palloc(Size size)
Definition: mcxt.c:1201
BlockNumber lastRevmapPage
Definition: brin_page.h:69
BlockNumber pagesPerRange
Definition: brin_page.h:68
BlockNumber rm_lastRevmapPage
Definition: brin_revmap.c:51
Buffer rm_metaBuf
Definition: brin_revmap.c:52

References BRIN_METAPAGE_BLKNO, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage(), InvalidBuffer, BrinMetaPageData::lastRevmapPage, LockBuffer(), PageGetContents(), BrinMetaPageData::pagesPerRange, palloc(), ReadBuffer(), BrinRevmap::rm_currBuf, BrinRevmap::rm_irel, BrinRevmap::rm_lastRevmapPage, BrinRevmap::rm_metaBuf, and BrinRevmap::rm_pagesPerRange.

Referenced by brinbeginscan(), brinbuild(), brinRevmapDesummarizeRange(), brinsummarize(), and initialize_brin_insertstate().

◆ brinRevmapTerminate()

void brinRevmapTerminate ( BrinRevmap revmap)

Definition at line 101 of file brin_revmap.c.

102 {
103  ReleaseBuffer(revmap->rm_metaBuf);
104  if (revmap->rm_currBuf != InvalidBuffer)
105  ReleaseBuffer(revmap->rm_currBuf);
106  pfree(revmap);
107 }
void pfree(void *pointer)
Definition: mcxt.c:1431

References InvalidBuffer, pfree(), ReleaseBuffer(), BrinRevmap::rm_currBuf, and BrinRevmap::rm_metaBuf.

Referenced by brinbuild(), brinendscan(), brininsertcleanup(), brinRevmapDesummarizeRange(), and brinsummarize().

◆ brinSetHeapBlockItemptr()

void brinSetHeapBlockItemptr ( Buffer  buf,
BlockNumber  pagesPerRange,
BlockNumber  heapBlk,
ItemPointerData  tid 
)

Definition at line 156 of file brin_revmap.c.

158 {
159  RevmapContents *contents;
160  ItemPointerData *iptr;
161  Page page;
162 
163  /* The correct page should already be pinned and locked */
164  page = BufferGetPage(buf);
165  contents = (RevmapContents *) PageGetContents(page);
166  iptr = (ItemPointerData *) contents->rm_tids;
167  iptr += HEAPBLK_TO_REVMAP_INDEX(pagesPerRange, heapBlk);
168 
169  if (ItemPointerIsValid(&tid))
170  ItemPointerSet(iptr,
173  else
174  ItemPointerSetInvalid(iptr);
175 }
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition: itemptr.h:135

References buf, BufferGetPage(), HEAPBLK_TO_REVMAP_INDEX, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), ItemPointerIsValid(), ItemPointerSet(), ItemPointerSetInvalid(), PageGetContents(), and RevmapContents::rm_tids.

Referenced by brin_doinsert(), brin_doupdate(), brin_xlog_desummarize_page(), brin_xlog_insert_update(), and brinRevmapDesummarizeRange().

◆ revmap_extend_and_get_blkno()

static BlockNumber revmap_extend_and_get_blkno ( BrinRevmap revmap,
BlockNumber  heapBlk 
)
static

Definition at line 501 of file brin_revmap.c.

502 {
503  BlockNumber targetblk;
504 
505  /* obtain revmap block number, skip 1 for metapage block */
506  targetblk = HEAPBLK_TO_REVMAP_BLK(revmap->rm_pagesPerRange, heapBlk) + 1;
507 
508  /* Extend the revmap, if necessary */
509  while (targetblk > revmap->rm_lastRevmapPage)
510  {
512  revmap_physical_extend(revmap);
513  }
514 
515  return targetblk;
516 }
static void revmap_physical_extend(BrinRevmap *revmap)
Definition: brin_revmap.c:523
#define HEAPBLK_TO_REVMAP_BLK(pagesPerRange, heapBlk)
Definition: brin_revmap.c:41

References CHECK_FOR_INTERRUPTS, HEAPBLK_TO_REVMAP_BLK, revmap_physical_extend(), BrinRevmap::rm_lastRevmapPage, and BrinRevmap::rm_pagesPerRange.

Referenced by brinRevmapExtend().

◆ revmap_get_blkno()

static BlockNumber revmap_get_blkno ( BrinRevmap revmap,
BlockNumber  heapBlk 
)
static

Definition at line 443 of file brin_revmap.c.

444 {
445  BlockNumber targetblk;
446 
447  /* obtain revmap block number, skip 1 for metapage block */
448  targetblk = HEAPBLK_TO_REVMAP_BLK(revmap->rm_pagesPerRange, heapBlk) + 1;
449 
450  /* Normal case: the revmap page is already allocated */
451  if (targetblk <= revmap->rm_lastRevmapPage)
452  return targetblk;
453 
454  return InvalidBlockNumber;
455 }

References HEAPBLK_TO_REVMAP_BLK, InvalidBlockNumber, and BrinRevmap::rm_pagesPerRange.

Referenced by brinGetTupleForHeapBlock(), brinRevmapDesummarizeRange(), and revmap_get_buffer().

◆ revmap_get_buffer()

static Buffer revmap_get_buffer ( BrinRevmap revmap,
BlockNumber  heapBlk 
)
static

Definition at line 464 of file brin_revmap.c.

465 {
466  BlockNumber mapBlk;
467 
468  /* Translate the heap block number to physical index location. */
469  mapBlk = revmap_get_blkno(revmap, heapBlk);
470 
471  if (mapBlk == InvalidBlockNumber)
472  elog(ERROR, "revmap does not cover heap block %u", heapBlk);
473 
474  /* Ensure the buffer we got is in the expected range */
475  Assert(mapBlk != BRIN_METAPAGE_BLKNO &&
476  mapBlk <= revmap->rm_lastRevmapPage);
477 
478  /*
479  * Obtain the buffer from which we need to read. If we already have the
480  * correct buffer in our access struct, use that; otherwise, release that,
481  * (if valid) and read the one we need.
482  */
483  if (revmap->rm_currBuf == InvalidBuffer ||
484  mapBlk != BufferGetBlockNumber(revmap->rm_currBuf))
485  {
486  if (revmap->rm_currBuf != InvalidBuffer)
487  ReleaseBuffer(revmap->rm_currBuf);
488 
489  revmap->rm_currBuf = ReadBuffer(revmap->rm_irel, mapBlk);
490  }
491 
492  return revmap->rm_currBuf;
493 }

References Assert(), BRIN_METAPAGE_BLKNO, BufferGetBlockNumber(), elog(), ERROR, InvalidBlockNumber, InvalidBuffer, ReadBuffer(), ReleaseBuffer(), revmap_get_blkno(), BrinRevmap::rm_currBuf, and BrinRevmap::rm_irel.

Referenced by brinLockRevmapPageForUpdate().

◆ revmap_physical_extend()

static void revmap_physical_extend ( BrinRevmap revmap)
static

Definition at line 523 of file brin_revmap.c.

524 {
525  Buffer buf;
526  Page page;
527  Page metapage;
528  BrinMetaPageData *metadata;
529  BlockNumber mapBlk;
530  BlockNumber nblocks;
531  Relation irel = revmap->rm_irel;
532 
533  /*
534  * Lock the metapage. This locks out concurrent extensions of the revmap,
535  * but note that we still need to grab the relation extension lock because
536  * another backend can extend the index with regular BRIN pages.
537  */
539  metapage = BufferGetPage(revmap->rm_metaBuf);
540  metadata = (BrinMetaPageData *) PageGetContents(metapage);
541 
542  /*
543  * Check that our cached lastRevmapPage value was up-to-date; if it
544  * wasn't, update the cached copy and have caller start over.
545  */
546  if (metadata->lastRevmapPage != revmap->rm_lastRevmapPage)
547  {
548  revmap->rm_lastRevmapPage = metadata->lastRevmapPage;
550  return;
551  }
552  mapBlk = metadata->lastRevmapPage + 1;
553 
554  nblocks = RelationGetNumberOfBlocks(irel);
555  if (mapBlk < nblocks)
556  {
557  buf = ReadBuffer(irel, mapBlk);
559  page = BufferGetPage(buf);
560  }
561  else
562  {
563  buf = ExtendBufferedRel(BMR_REL(irel), MAIN_FORKNUM, NULL,
564  EB_LOCK_FIRST);
565  if (BufferGetBlockNumber(buf) != mapBlk)
566  {
567  /*
568  * Very rare corner case: somebody extended the relation
569  * concurrently after we read its length. If this happens, give
570  * up and have caller start over. We will have to evacuate that
571  * page from under whoever is using it.
572  */
575  return;
576  }
577  page = BufferGetPage(buf);
578  }
579 
580  /* Check that it's a regular block (or an empty page) */
581  if (!PageIsNew(page) && !BRIN_IS_REGULAR_PAGE(page))
582  ereport(ERROR,
583  (errcode(ERRCODE_INDEX_CORRUPTED),
584  errmsg("unexpected page type 0x%04X in BRIN index \"%s\" block %u",
585  BrinPageType(page),
588 
589  /* If the page is in use, evacuate it and restart */
590  if (brin_start_evacuating_page(irel, buf))
591  {
593  brin_evacuate_page(irel, revmap->rm_pagesPerRange, revmap, buf);
594 
595  /* have caller start over */
596  return;
597  }
598 
599  /*
600  * Ok, we have now locked the metapage and the target block. Re-initialize
601  * the target block as a revmap page, and update the metapage.
602  */
604 
605  /* the rm_tids array is initialized to all invalid by PageInit */
608 
609  metadata->lastRevmapPage = mapBlk;
610 
611  /*
612  * Set pd_lower just past the end of the metadata. This is essential,
613  * because without doing so, metadata will be lost if xlog.c compresses
614  * the page. (We must do this here because pre-v11 versions of PG did not
615  * set the metapage's pd_lower correctly, so a pg_upgraded index might
616  * contain the wrong value.)
617  */
618  ((PageHeader) metapage)->pd_lower =
619  ((char *) metadata + sizeof(BrinMetaPageData)) - (char *) metapage;
620 
621  MarkBufferDirty(revmap->rm_metaBuf);
622 
623  if (RelationNeedsWAL(revmap->rm_irel))
624  {
625  xl_brin_revmap_extend xlrec;
626  XLogRecPtr recptr;
627 
628  xlrec.targetBlk = mapBlk;
629 
630  XLogBeginInsert();
631  XLogRegisterData((char *) &xlrec, SizeOfBrinRevmapExtend);
633 
635 
636  recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_REVMAP_EXTEND);
637  PageSetLSN(metapage, recptr);
638  PageSetLSN(page, recptr);
639  }
640 
642 
644 
646 }
#define BRIN_PAGETYPE_REVMAP
Definition: brin_page.h:52
#define BrinPageType(page)
Definition: brin_page.h:42
void brin_evacuate_page(Relation idxRel, BlockNumber pagesPerRange, BrinRevmap *revmap, Buffer buf)
Definition: brin_pageops.c:565
bool brin_start_evacuating_page(Relation idxRel, Buffer buf)
Definition: brin_pageops.c:525
void brin_page_init(Page page, uint16 type)
Definition: brin_pageops.c:476
#define XLOG_BRIN_REVMAP_EXTEND
Definition: brin_xlog.h:35
#define SizeOfBrinRevmapExtend
Definition: brin_xlog.h:124
Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
Definition: bufmgr.c:839
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:229
@ EB_LOCK_FIRST
Definition: bufmgr.h:85
#define BMR_REL(p_rel)
Definition: bufmgr.h:106
PageHeaderData * PageHeader
Definition: bufpage.h:170
static bool PageIsNew(Page page)
Definition: bufpage.h:230
#define RelationGetRelationName(relation)
Definition: rel.h:538
@ MAIN_FORKNUM
Definition: relpath.h:50
BlockNumber targetBlk
Definition: brin_xlog.h:121
#define REGBUF_WILL_INIT
Definition: xloginsert.h:33

References BMR_REL, brin_evacuate_page(), BRIN_IS_REGULAR_PAGE, brin_page_init(), BRIN_PAGETYPE_REVMAP, brin_start_evacuating_page(), BrinPageType, buf, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage(), EB_LOCK_FIRST, END_CRIT_SECTION, ereport, errcode(), errmsg(), ERROR, ExtendBufferedRel(), BrinMetaPageData::lastRevmapPage, LockBuffer(), MAIN_FORKNUM, MarkBufferDirty(), PageGetContents(), PageIsNew(), PageSetLSN(), ReadBuffer(), REGBUF_STANDARD, REGBUF_WILL_INIT, RelationGetNumberOfBlocks, RelationGetRelationName, RelationNeedsWAL, BrinRevmap::rm_irel, BrinRevmap::rm_lastRevmapPage, BrinRevmap::rm_metaBuf, BrinRevmap::rm_pagesPerRange, SizeOfBrinRevmapExtend, START_CRIT_SECTION, xl_brin_revmap_extend::targetBlk, UnlockReleaseBuffer(), XLOG_BRIN_REVMAP_EXTEND, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by revmap_extend_and_get_blkno().