PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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)
 
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)
 

Typedef Documentation

◆ BrinRevmap

typedef struct BrinRevmap BrinRevmap

Definition at line 24 of file brin_revmap.h.

Function Documentation

◆ brinGetTupleForHeapBlock()

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

Definition at line 194 of file brin_revmap.c.

196{
197 Relation idxRel = revmap->rm_irel;
198 BlockNumber mapBlk;
199 RevmapContents *contents;
200 ItemPointerData *iptr;
201 BlockNumber blk;
202 Page page;
203 ItemId lp;
204 BrinTuple *tup;
205 ItemPointerData previptr;
206
207 /* normalize the heap block number to be the first page in the range */
208 heapBlk = (heapBlk / revmap->rm_pagesPerRange) * revmap->rm_pagesPerRange;
209
210 /*
211 * Compute the revmap page number we need. If Invalid is returned (i.e.,
212 * the revmap page hasn't been created yet), the requested page range is
213 * not summarized.
214 */
215 mapBlk = revmap_get_blkno(revmap, heapBlk);
216 if (mapBlk == InvalidBlockNumber)
217 {
218 *off = InvalidOffsetNumber;
219 return NULL;
220 }
221
222 ItemPointerSetInvalid(&previptr);
223 for (;;)
224 {
226
227 if (revmap->rm_currBuf == InvalidBuffer ||
228 BufferGetBlockNumber(revmap->rm_currBuf) != mapBlk)
229 {
230 if (revmap->rm_currBuf != InvalidBuffer)
231 ReleaseBuffer(revmap->rm_currBuf);
232
233 Assert(mapBlk != InvalidBlockNumber);
234 revmap->rm_currBuf = ReadBuffer(revmap->rm_irel, mapBlk);
235 }
236
238
239 contents = (RevmapContents *)
241 iptr = contents->rm_tids;
242 iptr += HEAPBLK_TO_REVMAP_INDEX(revmap->rm_pagesPerRange, heapBlk);
243
244 if (!ItemPointerIsValid(iptr))
245 {
247 return NULL;
248 }
249
250 /*
251 * Check the TID we got in a previous iteration, if any, and save the
252 * current TID we got from the revmap; if we loop, we can sanity-check
253 * that the next one we get is different. Otherwise we might be stuck
254 * looping forever if the revmap is somehow badly broken.
255 */
256 if (ItemPointerIsValid(&previptr) && ItemPointerEquals(&previptr, iptr))
258 (errcode(ERRCODE_INDEX_CORRUPTED),
259 errmsg_internal("corrupted BRIN index: inconsistent range map")));
260 previptr = *iptr;
261
262 blk = ItemPointerGetBlockNumber(iptr);
263 *off = ItemPointerGetOffsetNumber(iptr);
264
266
267 /* Ok, got a pointer to where the BrinTuple should be. Fetch it. */
268 if (!BufferIsValid(*buf) || BufferGetBlockNumber(*buf) != blk)
269 {
270 if (BufferIsValid(*buf))
272 *buf = ReadBuffer(idxRel, blk);
273 }
275 page = BufferGetPage(*buf);
276
277 /* If we land on a revmap page, start over */
278 if (BRIN_IS_REGULAR_PAGE(page))
279 {
280 /*
281 * If the offset number is greater than what's in the page, it's
282 * possible that the range was desummarized concurrently. Just
283 * return NULL to handle that case.
284 */
285 if (*off > PageGetMaxOffsetNumber(page))
286 {
288 return NULL;
289 }
290
291 lp = PageGetItemId(page, *off);
292 if (ItemIdIsUsed(lp))
293 {
294 tup = (BrinTuple *) PageGetItem(page, lp);
295
296 if (tup->bt_blkno == heapBlk)
297 {
298 if (size)
299 *size = ItemIdGetLength(lp);
300 /* found it! */
301 return tup;
302 }
303 }
304 }
305
306 /*
307 * No luck. Assume that the revmap was updated concurrently.
308 */
310 }
311 /* not reached, but keep compiler quiet */
312 return NULL;
313}
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:442
#define HEAPBLK_TO_REVMAP_INDEX(pagesPerRange, heapBlk)
Definition: brin_revmap.c:42
#define InvalidBuffer
Definition: buf.h:25
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3724
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4924
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5158
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:746
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:189
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:190
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:400
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:351
Pointer Page
Definition: bufpage.h:81
static Item PageGetItem(Page page, ItemId itemId)
Definition: bufpage.h:354
static char * PageGetContents(Page page)
Definition: bufpage.h:257
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:243
static OffsetNumber PageGetMaxOffsetNumber(Page page)
Definition: bufpage.h:372
#define Assert(condition)
Definition: c.h:812
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
int errcode(int sqlerrcode)
Definition: elog.c:853
#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
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define InvalidOffsetNumber
Definition: off.h:26
static PgChecksumMode mode
Definition: pg_checksums.c:55
static char * buf
Definition: pg_test_fsync.c:72
static pg_noinline void Size size
Definition: slab.c:607
BlockNumber rm_pagesPerRange
Definition: brin_revmap.c:49
Buffer rm_currBuf
Definition: brin_revmap.c:52
Relation rm_irel
Definition: brin_revmap.c:48
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, RevmapContents::rm_tids, and size.

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

◆ brinLockRevmapPageForUpdate()

Buffer brinLockRevmapPageForUpdate ( BrinRevmap revmap,
BlockNumber  heapBlk 
)

Definition at line 134 of file brin_revmap.c.

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

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 323 of file brin_revmap.c.

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

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

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 70 of file brin_revmap.c.

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

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 100 of file brin_revmap.c.

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

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 155 of file brin_revmap.c.

157{
158 RevmapContents *contents;
159 ItemPointerData *iptr;
160 Page page;
161
162 /* The correct page should already be pinned and locked */
163 page = BufferGetPage(buf);
164 contents = (RevmapContents *) PageGetContents(page);
165 iptr = (ItemPointerData *) contents->rm_tids;
166 iptr += HEAPBLK_TO_REVMAP_INDEX(pagesPerRange, heapBlk);
167
168 if (ItemPointerIsValid(&tid))
169 ItemPointerSet(iptr,
172 else
174}
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().