PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
brin_revmap.h File Reference
#include "access/brin_tuple.h"
#include "storage/block.h"
#include "storage/buf.h"
#include "storage/itemptr.h"
#include "storage/off.h"
#include "utils/relcache.h"
#include "utils/snapshot.h"
Include dependency graph for brin_revmap.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct BrinRevmap BrinRevmap
 

Functions

BrinRevmapbrinRevmapInitialize (Relation idxrel, BlockNumber *pagesPerRange, Snapshot snapshot)
 
void brinRevmapTerminate (BrinRevmap *revmap)
 
void brinRevmapExtend (BrinRevmap *revmap, BlockNumber heapBlk)
 
Buffer brinLockRevmapPageForUpdate (BrinRevmap *revmap, BlockNumber heapBlk)
 
void brinSetHeapBlockItemptr (Buffer rmbuf, BlockNumber pagesPerRange, BlockNumber heapBlk, ItemPointerData tid)
 
BrinTuplebrinGetTupleForHeapBlock (BrinRevmap *revmap, BlockNumber heapBlk, Buffer *buf, OffsetNumber *off, Size *size, int mode, Snapshot snapshot)
 

Typedef Documentation

Definition at line 24 of file brin_revmap.h.

Function Documentation

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

Definition at line 191 of file brin_revmap.c.

References Assert, BRIN_IS_REGULAR_PAGE, BrinTuple::bt_blkno, 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(), NULL, PageGetContents, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, ReadBuffer(), ReleaseBuffer(), revmap_get_blkno(), BrinRevmap::rm_currBuf, BrinRevmap::rm_irel, BrinRevmap::rm_pagesPerRange, RevmapContents::rm_tids, and TestForOldSnapshot().

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

194 {
195  Relation idxRel = revmap->rm_irel;
196  BlockNumber mapBlk;
197  RevmapContents *contents;
198  ItemPointerData *iptr;
199  BlockNumber blk;
200  Page page;
201  ItemId lp;
202  BrinTuple *tup;
203  ItemPointerData previptr;
204 
205  /* normalize the heap block number to be the first page in the range */
206  heapBlk = (heapBlk / revmap->rm_pagesPerRange) * revmap->rm_pagesPerRange;
207 
208  /* Compute the revmap page number we need */
209  mapBlk = revmap_get_blkno(revmap, heapBlk);
210  if (mapBlk == InvalidBlockNumber)
211  {
212  *off = InvalidOffsetNumber;
213  return NULL;
214  }
215 
216  ItemPointerSetInvalid(&previptr);
217  for (;;)
218  {
220 
221  if (revmap->rm_currBuf == InvalidBuffer ||
222  BufferGetBlockNumber(revmap->rm_currBuf) != mapBlk)
223  {
224  if (revmap->rm_currBuf != InvalidBuffer)
225  ReleaseBuffer(revmap->rm_currBuf);
226 
227  Assert(mapBlk != InvalidBlockNumber);
228  revmap->rm_currBuf = ReadBuffer(revmap->rm_irel, mapBlk);
229  }
230 
232 
233  contents = (RevmapContents *)
235  iptr = contents->rm_tids;
236  iptr += HEAPBLK_TO_REVMAP_INDEX(revmap->rm_pagesPerRange, heapBlk);
237 
238  if (!ItemPointerIsValid(iptr))
239  {
241  return NULL;
242  }
243 
244  /*
245  * Check the TID we got in a previous iteration, if any, and save the
246  * current TID we got from the revmap; if we loop, we can sanity-check
247  * that the next one we get is different. Otherwise we might be stuck
248  * looping forever if the revmap is somehow badly broken.
249  */
250  if (ItemPointerIsValid(&previptr) && ItemPointerEquals(&previptr, iptr))
251  ereport(ERROR,
252  (errcode(ERRCODE_INDEX_CORRUPTED),
253  errmsg_internal("corrupted BRIN index: inconsistent range map")));
254  previptr = *iptr;
255 
256  blk = ItemPointerGetBlockNumber(iptr);
257  *off = ItemPointerGetOffsetNumber(iptr);
258 
260 
261  /* Ok, got a pointer to where the BrinTuple should be. Fetch it. */
262  if (!BufferIsValid(*buf) || BufferGetBlockNumber(*buf) != blk)
263  {
264  if (BufferIsValid(*buf))
265  ReleaseBuffer(*buf);
266  *buf = ReadBuffer(idxRel, blk);
267  }
268  LockBuffer(*buf, mode);
269  page = BufferGetPage(*buf);
270  TestForOldSnapshot(snapshot, idxRel, page);
271 
272  /* If we land on a revmap page, start over */
273  if (BRIN_IS_REGULAR_PAGE(page))
274  {
275  if (*off > PageGetMaxOffsetNumber(page))
276  ereport(ERROR,
277  (errcode(ERRCODE_INDEX_CORRUPTED),
278  errmsg_internal("corrupted BRIN index: inconsistent range map")));
279  lp = PageGetItemId(page, *off);
280  if (ItemIdIsUsed(lp))
281  {
282  tup = (BrinTuple *) PageGetItem(page, lp);
283 
284  if (tup->bt_blkno == heapBlk)
285  {
286  if (size)
287  *size = ItemIdGetLength(lp);
288  /* found it! */
289  return tup;
290  }
291  }
292  }
293 
294  /*
295  * No luck. Assume that the revmap was updated concurrently.
296  */
298  }
299  /* not reached, but keep compiler quiet */
300  return NULL;
301 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:59
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
Relation rm_irel
Definition: brin_revmap.c:49
#define HEAPBLK_TO_REVMAP_INDEX(pagesPerRange, heapBlk)
Definition: brin_revmap.c:43
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:265
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define InvalidBuffer
Definition: buf.h:25
int errcode(int sqlerrcode)
Definition: elog.c:575
#define BRIN_IS_REGULAR_PAGE(page)
Definition: brin_page.h:57
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:65
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
#define PageGetContents(page)
Definition: bufpage.h:243
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define InvalidOffsetNumber
Definition: off.h:26
BlockNumber bt_blkno
Definition: brin_tuple.h:52
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
Buffer rm_currBuf
Definition: brin_revmap.c:53
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define InvalidBlockNumber
Definition: block.h:33
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:94
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:149
BlockNumber rm_pagesPerRange
Definition: brin_revmap.c:50
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:75
static BlockNumber revmap_get_blkno(BrinRevmap *revmap, BlockNumber heapBlk)
Definition: brin_revmap.c:309
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
Pointer Page
Definition: bufpage.h:74
ItemPointerData rm_tids[1]
Definition: brin_page.h:85
Buffer brinLockRevmapPageForUpdate ( BrinRevmap revmap,
BlockNumber  heapBlk 
)

Definition at line 137 of file brin_revmap.c.

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

Referenced by brin_doinsert(), and brin_doupdate().

138 {
139  Buffer rmBuf;
140 
141  rmBuf = revmap_get_buffer(revmap, heapBlk);
143 
144  return rmBuf;
145 }
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
static Buffer revmap_get_buffer(BrinRevmap *revmap, BlockNumber heapBlk)
Definition: brin_revmap.c:330
int Buffer
Definition: buf.h:23
void brinRevmapExtend ( BrinRevmap revmap,
BlockNumber  heapBlk 
)

Definition at line 115 of file brin_revmap.c.

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

Referenced by brin_doinsert(), and brin_doupdate().

116 {
118 
119  mapBlk = revmap_extend_and_get_blkno(revmap, heapBlk);
120 
121  /* Ensure the buffer we got is in the expected range */
122  Assert(mapBlk != InvalidBlockNumber &&
123  mapBlk != BRIN_METAPAGE_BLKNO &&
124  mapBlk <= revmap->rm_lastRevmapPage);
125 }
static BlockNumber revmap_extend_and_get_blkno(BrinRevmap *revmap, BlockNumber heapBlk)
Definition: brin_revmap.c:367
#define BRIN_METAPAGE_BLKNO
Definition: brin_page.h:75
uint32 BlockNumber
Definition: block.h:31
#define Assert(condition)
Definition: c.h:675
#define InvalidBlockNumber
Definition: block.h:33
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:990
BrinRevmap* brinRevmapInitialize ( Relation  idxrel,
BlockNumber pagesPerRange,
Snapshot  snapshot 
)

Definition at line 71 of file brin_revmap.c.

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, BrinRevmap::rm_pagesPerRange, and TestForOldSnapshot().

Referenced by brinbeginscan(), brinbuild(), brininsert(), and brinsummarize().

73 {
74  BrinRevmap *revmap;
75  Buffer meta;
76  BrinMetaPageData *metadata;
77  Page page;
78 
79  meta = ReadBuffer(idxrel, BRIN_METAPAGE_BLKNO);
81  page = BufferGetPage(meta);
82  TestForOldSnapshot(snapshot, idxrel, page);
83  metadata = (BrinMetaPageData *) PageGetContents(page);
84 
85  revmap = palloc(sizeof(BrinRevmap));
86  revmap->rm_irel = idxrel;
87  revmap->rm_pagesPerRange = metadata->pagesPerRange;
88  revmap->rm_lastRevmapPage = metadata->lastRevmapPage;
89  revmap->rm_metaBuf = meta;
90  revmap->rm_currBuf = InvalidBuffer;
91 
92  *pagesPerRange = metadata->pagesPerRange;
93 
95 
96  return revmap;
97 }
BlockNumber rm_lastRevmapPage
Definition: brin_revmap.c:51
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
Relation rm_irel
Definition: brin_revmap.c:49
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:265
#define BRIN_METAPAGE_BLKNO
Definition: brin_page.h:75
#define InvalidBuffer
Definition: buf.h:25
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
BlockNumber lastRevmapPage
Definition: brin_page.h:69
#define PageGetContents(page)
Definition: bufpage.h:243
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
BlockNumber pagesPerRange
Definition: brin_page.h:68
Buffer rm_currBuf
Definition: brin_revmap.c:53
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
Buffer rm_metaBuf
Definition: brin_revmap.c:52
void * palloc(Size size)
Definition: mcxt.c:849
BlockNumber rm_pagesPerRange
Definition: brin_revmap.c:50
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
void brinRevmapTerminate ( BrinRevmap revmap)

Definition at line 103 of file brin_revmap.c.

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

Referenced by brinbuild(), brinendscan(), brininsert(), and brinsummarize().

104 {
105  ReleaseBuffer(revmap->rm_metaBuf);
106  if (revmap->rm_currBuf != InvalidBuffer)
107  ReleaseBuffer(revmap->rm_currBuf);
108  pfree(revmap);
109 }
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
void pfree(void *pointer)
Definition: mcxt.c:950
Buffer rm_currBuf
Definition: brin_revmap.c:53
Buffer rm_metaBuf
Definition: brin_revmap.c:52
void brinSetHeapBlockItemptr ( Buffer  rmbuf,
BlockNumber  pagesPerRange,
BlockNumber  heapBlk,
ItemPointerData  tid 
)

Definition at line 158 of file brin_revmap.c.

References BufferGetPage, HEAPBLK_TO_REVMAP_INDEX, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, ItemPointerSet, PageGetContents, and RevmapContents::rm_tids.

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

160 {
161  RevmapContents *contents;
162  ItemPointerData *iptr;
163  Page page;
164 
165  /* The correct page should already be pinned and locked */
166  page = BufferGetPage(buf);
167  contents = (RevmapContents *) PageGetContents(page);
168  iptr = (ItemPointerData *) contents->rm_tids;
169  iptr += HEAPBLK_TO_REVMAP_INDEX(pagesPerRange, heapBlk);
170 
171  ItemPointerSet(iptr,
174 }
#define HEAPBLK_TO_REVMAP_INDEX(pagesPerRange, heapBlk)
Definition: brin_revmap.c:43
static char * buf
Definition: pg_test_fsync.c:65
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define PageGetContents(page)
Definition: bufpage.h:243
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:94
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:75
Pointer Page
Definition: bufpage.h:74
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:104
ItemPointerData rm_tids[1]
Definition: brin_page.h:85