PostgreSQL Source Code  git master
brin_pageops.h File Reference
Include dependency graph for brin_pageops.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool brin_doupdate (Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, BlockNumber heapBlk, Buffer oldbuf, OffsetNumber oldoff, const BrinTuple *origtup, Size origsz, const BrinTuple *newtup, Size newsz, bool samepage)
 
bool brin_can_do_samepage_update (Buffer buffer, Size origsz, Size newsz)
 
OffsetNumber brin_doinsert (Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, Buffer *buffer, BlockNumber heapBlk, BrinTuple *tup, Size itemsz)
 
void brin_page_init (Page page, uint16 type)
 
void brin_metapage_init (Page page, BlockNumber pagesPerRange, uint16 version)
 
bool brin_start_evacuating_page (Relation idxRel, Buffer buf)
 
void brin_evacuate_page (Relation idxRel, BlockNumber pagesPerRange, BrinRevmap *revmap, Buffer buf)
 
bool brin_page_cleanup (Relation idxrel, Buffer buf)
 

Function Documentation

◆ brin_can_do_samepage_update()

bool brin_can_do_samepage_update ( Buffer  buffer,
Size  origsz,
Size  newsz 
)

Definition at line 327 of file brin_pageops.c.

References BufferGetPage, and PageGetExactFreeSpace().

Referenced by brin_doupdate(), brininsert(), and summarize_range().

328 {
329  return
330  ((newsz <= origsz) ||
331  PageGetExactFreeSpace(BufferGetPage(buffer)) >= (newsz - origsz));
332 }
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:629

◆ brin_doinsert()

OffsetNumber brin_doinsert ( Relation  idxrel,
BlockNumber  pagesPerRange,
BrinRevmap revmap,
Buffer buffer,
BlockNumber  heapBlk,
BrinTuple tup,
Size  itemsz 
)

Definition at line 346 of file brin_pageops.c.

References Assert, br_page_get_freespace(), BRIN_elog, brin_getinsertbuffer(), brin_page_init(), BRIN_PAGETYPE_REGULAR, brinLockRevmapPageForUpdate(), BrinMaxItemSize, brinRevmapExtend(), brinSetHeapBlockItemptr(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage, BufferIsValid, DEBUG2, elog, END_CRIT_SECTION, ereport, errcode(), errmsg(), ERROR, FreeSpaceMapVacuum(), xl_brin_insert::heapBlk, InvalidBuffer, InvalidOffsetNumber, ItemPointerSet, LockBuffer(), MarkBufferDirty(), MAXALIGN, xl_brin_insert::offnum, PageAddItem, PageSetLSN, xl_brin_insert::pagesPerRange, REGBUF_STANDARD, REGBUF_WILL_INIT, RelationGetRelationName, RelationNeedsWAL, SizeOfBrinInsert, START_CRIT_SECTION, UnlockReleaseBuffer(), XLOG_BRIN_INIT_PAGE, XLOG_BRIN_INSERT, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by form_and_insert_tuple(), and summarize_range().

349 {
350  Page page;
351  BlockNumber blk;
352  OffsetNumber off;
353  Buffer revmapbuf;
354  ItemPointerData tid;
355  bool extended;
356 
357  Assert(itemsz == MAXALIGN(itemsz));
358 
359  /* If the item is oversized, don't even bother. */
360  if (itemsz > BrinMaxItemSize)
361  {
362  ereport(ERROR,
363  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
364  errmsg("index row size %zu exceeds maximum %zu for index \"%s\"",
365  itemsz, BrinMaxItemSize, RelationGetRelationName(idxrel))));
366  return InvalidOffsetNumber; /* keep compiler quiet */
367  }
368 
369  /* Make sure the revmap is long enough to contain the entry we need */
370  brinRevmapExtend(revmap, heapBlk);
371 
372  /*
373  * Acquire lock on buffer supplied by caller, if any. If it doesn't have
374  * enough space, unpin it to obtain a new one below.
375  */
376  if (BufferIsValid(*buffer))
377  {
378  /*
379  * It's possible that another backend (or ourselves!) extended the
380  * revmap over the page we held a pin on, so we cannot assume that
381  * it's still a regular page.
382  */
385  {
388  }
389  }
390 
391  /*
392  * If we still don't have a usable buffer, have brin_getinsertbuffer
393  * obtain one for us.
394  */
395  if (!BufferIsValid(*buffer))
396  {
397  do
398  *buffer = brin_getinsertbuffer(idxrel, InvalidBuffer, itemsz, &extended);
399  while (!BufferIsValid(*buffer));
400  }
401  else
402  extended = false;
403 
404  /* Now obtain lock on revmap buffer */
405  revmapbuf = brinLockRevmapPageForUpdate(revmap, heapBlk);
406 
407  page = BufferGetPage(*buffer);
409 
410  /* Execute the actual insertion */
412  if (extended)
414  off = PageAddItem(page, (Item) tup, itemsz, InvalidOffsetNumber,
415  false, false);
416  if (off == InvalidOffsetNumber)
417  elog(ERROR, "could not insert new index tuple to page");
419 
420  BRIN_elog((DEBUG2, "inserted tuple (%u,%u) for range starting at %u",
421  blk, off, heapBlk));
422 
423  ItemPointerSet(&tid, blk, off);
424  brinSetHeapBlockItemptr(revmapbuf, pagesPerRange, heapBlk, tid);
425  MarkBufferDirty(revmapbuf);
426 
427  /* XLOG stuff */
428  if (RelationNeedsWAL(idxrel))
429  {
430  xl_brin_insert xlrec;
431  XLogRecPtr recptr;
432  uint8 info;
433 
434  info = XLOG_BRIN_INSERT | (extended ? XLOG_BRIN_INIT_PAGE : 0);
435  xlrec.heapBlk = heapBlk;
436  xlrec.pagesPerRange = pagesPerRange;
437  xlrec.offnum = off;
438 
439  XLogBeginInsert();
440  XLogRegisterData((char *) &xlrec, SizeOfBrinInsert);
441 
443  XLogRegisterBufData(0, (char *) tup, itemsz);
444 
445  XLogRegisterBuffer(1, revmapbuf, 0);
446 
447  recptr = XLogInsert(RM_BRIN_ID, info);
448 
449  PageSetLSN(page, recptr);
450  PageSetLSN(BufferGetPage(revmapbuf), recptr);
451  }
452 
454 
455  /* Tuple is firmly on buffer; we can release our locks */
457  LockBuffer(revmapbuf, BUFFER_LOCK_UNLOCK);
458 
459  if (extended)
460  FreeSpaceMapVacuum(idxrel);
461 
462  return off;
463 }
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:361
BlockNumber heapBlk
Definition: brin_xlog.h:65
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
#define BRIN_elog(args)
Definition: brin_internal.h:81
#define SizeOfBrinInsert
Definition: brin_xlog.h:74
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
unsigned char uint8
Definition: c.h:304
Pointer Item
Definition: item.h:17
#define InvalidBuffer
Definition: buf.h:25
Buffer brinLockRevmapPageForUpdate(BrinRevmap *revmap, BlockNumber heapBlk)
Definition: brin_revmap.c:137
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:412
uint32 BlockNumber
Definition: block.h:31
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
uint16 OffsetNumber
Definition: off.h:24
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
#define BRIN_PAGETYPE_REGULAR
Definition: brin_page.h:53
#define DEBUG2
Definition: elog.h:24
void brinRevmapExtend(BrinRevmap *revmap, BlockNumber heapBlk)
Definition: brin_revmap.c:115
BlockNumber pagesPerRange
Definition: brin_xlog.h:68
#define REGBUF_STANDARD
Definition: xloginsert.h:34
static Size br_page_get_freespace(Page page)
Definition: brin_pageops.c:896
#define RelationGetRelationName(relation)
Definition: rel.h:445
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
void FreeSpaceMapVacuum(Relation rel)
Definition: freespace.c:379
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
#define XLOG_BRIN_INIT_PAGE
Definition: brin_xlog.h:43
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
#define XLOG_BRIN_INSERT
Definition: brin_xlog.h:32
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define InvalidOffsetNumber
Definition: off.h:26
#define BrinMaxItemSize
Definition: brin_pageops.c:30
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:680
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
OffsetNumber offnum
Definition: brin_xlog.h:71
#define MAXALIGN(LEN)
Definition: c.h:633
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
#define RelationNeedsWAL(relation)
Definition: rel.h:514
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
int errmsg(const char *fmt,...)
Definition: elog.c:797
static Buffer brin_getinsertbuffer(Relation irel, Buffer oldbuf, Size itemsz, bool *extended)
Definition: brin_pageops.c:676
#define elog
Definition: elog.h:219
void brinSetHeapBlockItemptr(Buffer buf, BlockNumber pagesPerRange, BlockNumber heapBlk, ItemPointerData tid)
Definition: brin_revmap.c:158
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define PageSetLSN(page, lsn)
Definition: bufpage.h:364
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:105
void brin_page_init(Page page, uint16 type)
Definition: brin_pageops.c:471

◆ brin_doupdate()

bool brin_doupdate ( Relation  idxrel,
BlockNumber  pagesPerRange,
BrinRevmap revmap,
BlockNumber  heapBlk,
Buffer  oldbuf,
OffsetNumber  oldoff,
const BrinTuple origtup,
Size  origsz,
const BrinTuple newtup,
Size  newsz,
bool  samepage 
)

Definition at line 55 of file brin_pageops.c.

References Assert, BlockNumberIsValid, br_page_get_freespace(), brin_can_do_samepage_update(), BRIN_EVACUATE_PAGE, brin_getinsertbuffer(), brin_initialize_empty_new_buffer(), BRIN_IS_REGULAR_PAGE, brin_page_init(), BRIN_PAGETYPE_REGULAR, brin_tuples_equal(), brinLockRevmapPageForUpdate(), BrinMaxItemSize, BrinPageFlags, brinRevmapExtend(), brinSetHeapBlockItemptr(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage, BufferIsValid, elog, END_CRIT_SECTION, ereport, errcode(), errmsg(), ERROR, FreeSpaceMapVacuum(), xl_brin_insert::heapBlk, xl_brin_update::insert, InvalidBlockNumber, InvalidBuffer, InvalidOffsetNumber, ItemIdGetLength, ItemIdIsNormal, ItemPointerSet, LockBuffer(), MarkBufferDirty(), MAXALIGN, xl_brin_insert::offnum, xl_brin_samepage_update::offnum, xl_brin_update::oldOffnum, PageAddItem, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageIndexTupleDeleteNoCompact(), PageIndexTupleOverwrite(), PageSetLSN, xl_brin_insert::pagesPerRange, RecordPageWithFreeSpace(), REGBUF_STANDARD, REGBUF_WILL_INIT, RelationGetRelationName, RelationNeedsWAL, SizeOfBrinSamepageUpdate, SizeOfBrinUpdate, START_CRIT_SECTION, UnlockReleaseBuffer(), XLOG_BRIN_INIT_PAGE, XLOG_BRIN_SAMEPAGE_UPDATE, XLOG_BRIN_UPDATE, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by brin_evacuate_page(), brininsert(), and summarize_range().

61 {
62  Page oldpage;
63  ItemId oldlp;
64  BrinTuple *oldtup;
65  Size oldsz;
66  Buffer newbuf;
67  bool extended;
68 
69  Assert(newsz == MAXALIGN(newsz));
70 
71  /* If the item is oversized, don't bother. */
72  if (newsz > BrinMaxItemSize)
73  {
74  ereport(ERROR,
75  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
76  errmsg("index row size %zu exceeds maximum %zu for index \"%s\"",
77  newsz, BrinMaxItemSize, RelationGetRelationName(idxrel))));
78  return false; /* keep compiler quiet */
79  }
80 
81  /* make sure the revmap is long enough to contain the entry we need */
82  brinRevmapExtend(revmap, heapBlk);
83 
84  if (!samepage)
85  {
86  /* need a page on which to put the item */
87  newbuf = brin_getinsertbuffer(idxrel, oldbuf, newsz, &extended);
88  if (!BufferIsValid(newbuf))
89  {
90  Assert(!extended);
91  return false;
92  }
93 
94  /*
95  * Note: it's possible (though unlikely) that the returned newbuf is
96  * the same as oldbuf, if brin_getinsertbuffer determined that the old
97  * buffer does in fact have enough space.
98  */
99  if (newbuf == oldbuf)
100  {
101  Assert(!extended);
102  newbuf = InvalidBuffer;
103  }
104  }
105  else
106  {
108  newbuf = InvalidBuffer;
109  extended = false;
110  }
111  oldpage = BufferGetPage(oldbuf);
112  oldlp = PageGetItemId(oldpage, oldoff);
113 
114  /*
115  * Check that the old tuple wasn't updated concurrently: it might have
116  * moved someplace else entirely, and for that matter the whole page
117  * might've become a revmap page. Note that in the first two cases
118  * checked here, the "oldlp" we just calculated is garbage; but
119  * PageGetItemId() is simple enough that it was safe to do that
120  * calculation anyway.
121  */
122  if (!BRIN_IS_REGULAR_PAGE(oldpage) ||
123  oldoff > PageGetMaxOffsetNumber(oldpage) ||
124  !ItemIdIsNormal(oldlp))
125  {
127 
128  /*
129  * If this happens, and the new buffer was obtained by extending the
130  * relation, then we need to ensure we don't leave it uninitialized or
131  * forget about it.
132  */
133  if (BufferIsValid(newbuf))
134  {
135  if (extended)
136  brin_initialize_empty_new_buffer(idxrel, newbuf);
137  UnlockReleaseBuffer(newbuf);
138  if (extended)
139  FreeSpaceMapVacuum(idxrel);
140  }
141  return false;
142  }
143 
144  oldsz = ItemIdGetLength(oldlp);
145  oldtup = (BrinTuple *) PageGetItem(oldpage, oldlp);
146 
147  /*
148  * ... or it might have been updated in place to different contents.
149  */
150  if (!brin_tuples_equal(oldtup, oldsz, origtup, origsz))
151  {
153  if (BufferIsValid(newbuf))
154  {
155  if (extended)
156  brin_initialize_empty_new_buffer(idxrel, newbuf);
157  UnlockReleaseBuffer(newbuf);
158  if (extended)
159  FreeSpaceMapVacuum(idxrel);
160  }
161  return false;
162  }
163 
164  /*
165  * Great, the old tuple is intact. We can proceed with the update.
166  *
167  * If there's enough room in the old page for the new tuple, replace it.
168  *
169  * Note that there might now be enough space on the page even though the
170  * caller told us there isn't, if a concurrent update moved another tuple
171  * elsewhere or replaced a tuple with a smaller one.
172  */
173  if (((BrinPageFlags(oldpage) & BRIN_EVACUATE_PAGE) == 0) &&
174  brin_can_do_samepage_update(oldbuf, origsz, newsz))
175  {
176  if (BufferIsValid(newbuf))
177  {
178  /* as above */
179  if (extended)
180  brin_initialize_empty_new_buffer(idxrel, newbuf);
181  UnlockReleaseBuffer(newbuf);
182  }
183 
185  if (!PageIndexTupleOverwrite(oldpage, oldoff, (Item) newtup, newsz))
186  elog(ERROR, "failed to replace BRIN tuple");
187  MarkBufferDirty(oldbuf);
188 
189  /* XLOG stuff */
190  if (RelationNeedsWAL(idxrel))
191  {
193  XLogRecPtr recptr;
195 
196  xlrec.offnum = oldoff;
197 
198  XLogBeginInsert();
199  XLogRegisterData((char *) &xlrec, SizeOfBrinSamepageUpdate);
200 
202  XLogRegisterBufData(0, (char *) newtup, newsz);
203 
204  recptr = XLogInsert(RM_BRIN_ID, info);
205 
206  PageSetLSN(oldpage, recptr);
207  }
208 
210 
212 
213  if (extended)
214  FreeSpaceMapVacuum(idxrel);
215 
216  return true;
217  }
218  else if (newbuf == InvalidBuffer)
219  {
220  /*
221  * Not enough space, but caller said that there was. Tell them to
222  * start over.
223  */
225  return false;
226  }
227  else
228  {
229  /*
230  * Not enough free space on the oldpage. Put the new tuple on the new
231  * page, and update the revmap.
232  */
233  Page newpage = BufferGetPage(newbuf);
234  Buffer revmapbuf;
235  ItemPointerData newtid;
236  OffsetNumber newoff;
238  Size freespace = 0;
239 
240  revmapbuf = brinLockRevmapPageForUpdate(revmap, heapBlk);
241 
243 
244  /*
245  * We need to initialize the page if it's newly obtained. Note we
246  * will WAL-log the initialization as part of the update, so we don't
247  * need to do that here.
248  */
249  if (extended)
251 
252  PageIndexTupleDeleteNoCompact(oldpage, oldoff);
253  newoff = PageAddItem(newpage, (Item) newtup, newsz,
254  InvalidOffsetNumber, false, false);
255  if (newoff == InvalidOffsetNumber)
256  elog(ERROR, "failed to add BRIN tuple to new page");
257  MarkBufferDirty(oldbuf);
258  MarkBufferDirty(newbuf);
259 
260  /* needed to update FSM below */
261  if (extended)
262  {
263  newblk = BufferGetBlockNumber(newbuf);
264  freespace = br_page_get_freespace(newpage);
265  }
266 
267  ItemPointerSet(&newtid, BufferGetBlockNumber(newbuf), newoff);
268  brinSetHeapBlockItemptr(revmapbuf, pagesPerRange, heapBlk, newtid);
269  MarkBufferDirty(revmapbuf);
270 
271  /* XLOG stuff */
272  if (RelationNeedsWAL(idxrel))
273  {
274  xl_brin_update xlrec;
275  XLogRecPtr recptr;
276  uint8 info;
277 
278  info = XLOG_BRIN_UPDATE | (extended ? XLOG_BRIN_INIT_PAGE : 0);
279 
280  xlrec.insert.offnum = newoff;
281  xlrec.insert.heapBlk = heapBlk;
282  xlrec.insert.pagesPerRange = pagesPerRange;
283  xlrec.oldOffnum = oldoff;
284 
285  XLogBeginInsert();
286 
287  /* new page */
288  XLogRegisterData((char *) &xlrec, SizeOfBrinUpdate);
289 
290  XLogRegisterBuffer(0, newbuf, REGBUF_STANDARD | (extended ? REGBUF_WILL_INIT : 0));
291  XLogRegisterBufData(0, (char *) newtup, newsz);
292 
293  /* revmap page */
294  XLogRegisterBuffer(1, revmapbuf, 0);
295 
296  /* old page */
298 
299  recptr = XLogInsert(RM_BRIN_ID, info);
300 
301  PageSetLSN(oldpage, recptr);
302  PageSetLSN(newpage, recptr);
303  PageSetLSN(BufferGetPage(revmapbuf), recptr);
304  }
305 
307 
308  LockBuffer(revmapbuf, BUFFER_LOCK_UNLOCK);
310  UnlockReleaseBuffer(newbuf);
311 
312  if (extended)
313  {
314  Assert(BlockNumberIsValid(newblk));
315  RecordPageWithFreeSpace(idxrel, newblk, freespace);
316  FreeSpaceMapVacuum(idxrel);
317  }
318 
319  return true;
320  }
321 }
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:361
BlockNumber heapBlk
Definition: brin_xlog.h:65
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:181
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
OffsetNumber offnum
Definition: brin_xlog.h:104
unsigned char uint8
Definition: c.h:304
Pointer Item
Definition: item.h:17
#define InvalidBuffer
Definition: buf.h:25
Buffer brinLockRevmapPageForUpdate(BrinRevmap *revmap, BlockNumber heapBlk)
Definition: brin_revmap.c:137
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:412
#define BRIN_IS_REGULAR_PAGE(page)
Definition: brin_page.h:57
uint32 BlockNumber
Definition: block.h:31
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
uint16 OffsetNumber
Definition: off.h:24
#define SizeOfBrinUpdate
Definition: brin_xlog.h:95
#define XLOG_BRIN_UPDATE
Definition: brin_xlog.h:33
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
#define BRIN_PAGETYPE_REGULAR
Definition: brin_page.h:53
void brinRevmapExtend(BrinRevmap *revmap, BlockNumber heapBlk)
Definition: brin_revmap.c:115
bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum, Item newtup, Size newsize)
Definition: bufpage.c:1062
bool brin_tuples_equal(const BrinTuple *a, Size alen, const BrinTuple *b, Size blen)
Definition: brin_tuple.c:340
BlockNumber pagesPerRange
Definition: brin_xlog.h:68
#define REGBUF_STANDARD
Definition: xloginsert.h:34
static Size br_page_get_freespace(Page page)
Definition: brin_pageops.c:896
#define RelationGetRelationName(relation)
Definition: rel.h:445
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
bool brin_can_do_samepage_update(Buffer buffer, Size origsz, Size newsz)
Definition: brin_pageops.c:327
void FreeSpaceMapVacuum(Relation rel)
Definition: freespace.c:379
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
#define XLOG_BRIN_INIT_PAGE
Definition: brin_xlog.h:43
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define InvalidOffsetNumber
Definition: off.h:26
xl_brin_insert insert
Definition: brin_xlog.h:92
#define BlockNumberIsValid(blockNumber)
Definition: block.h:70
void PageIndexTupleDeleteNoCompact(Page page, OffsetNumber offnum)
Definition: bufpage.c:954
#define BrinMaxItemSize
Definition: brin_pageops.c:30
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:680
#define ItemIdIsNormal(itemId)
Definition: itemid.h:98
#define SizeOfBrinSamepageUpdate
Definition: brin_xlog.h:107
size_t Size
Definition: c.h:414
#define XLOG_BRIN_SAMEPAGE_UPDATE
Definition: brin_xlog.h:34
#define InvalidBlockNumber
Definition: block.h:33
OffsetNumber offnum
Definition: brin_xlog.h:71
static void brin_initialize_empty_new_buffer(Relation idxrel, Buffer buffer)
Definition: brin_pageops.c:864
#define MAXALIGN(LEN)
Definition: c.h:633
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
#define RelationNeedsWAL(relation)
Definition: rel.h:514
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
int errmsg(const char *fmt,...)
Definition: elog.c:797
static Buffer brin_getinsertbuffer(Relation irel, Buffer oldbuf, Size itemsz, bool *extended)
Definition: brin_pageops.c:676
#define BRIN_EVACUATE_PAGE
Definition: brin_page.h:60
#define elog
Definition: elog.h:219
#define BrinPageFlags(page)
Definition: brin_page.h:46
void brinSetHeapBlockItemptr(Buffer buf, BlockNumber pagesPerRange, BlockNumber heapBlk, ItemPointerData tid)
Definition: brin_revmap.c:158
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define PageSetLSN(page, lsn)
Definition: bufpage.h:364
OffsetNumber oldOffnum
Definition: brin_xlog.h:90
int Buffer
Definition: buf.h:23
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:105
void brin_page_init(Page page, uint16 type)
Definition: brin_pageops.c:471

◆ brin_evacuate_page()

void brin_evacuate_page ( Relation  idxRel,
BlockNumber  pagesPerRange,
BrinRevmap revmap,
Buffer  buf 
)

Definition at line 555 of file brin_pageops.c.

References Assert, brin_copy_tuple(), brin_doupdate(), BRIN_EVACUATE_PAGE, BRIN_IS_REGULAR_PAGE, BrinPageFlags, BrinTuple::bt_blkno, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, CHECK_FOR_INTERRUPTS, FirstOffsetNumber, ItemIdGetLength, ItemIdIsUsed, LockBuffer(), PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, and UnlockReleaseBuffer().

Referenced by revmap_physical_extend().

557 {
558  OffsetNumber off;
559  OffsetNumber maxoff;
560  Page page;
561  BrinTuple *btup = NULL;
562  Size btupsz = 0;
563 
564  page = BufferGetPage(buf);
565 
567 
568  maxoff = PageGetMaxOffsetNumber(page);
569  for (off = FirstOffsetNumber; off <= maxoff; off++)
570  {
571  BrinTuple *tup;
572  Size sz;
573  ItemId lp;
574 
576 
577  lp = PageGetItemId(page, off);
578  if (ItemIdIsUsed(lp))
579  {
580  sz = ItemIdGetLength(lp);
581  tup = (BrinTuple *) PageGetItem(page, lp);
582  tup = brin_copy_tuple(tup, sz, btup, &btupsz);
583 
585 
586  if (!brin_doupdate(idxRel, pagesPerRange, revmap, tup->bt_blkno,
587  buf, off, tup, sz, tup, sz, false))
588  off--; /* retry */
589 
591 
592  /* It's possible that someone extended the revmap over this page */
593  if (!BRIN_IS_REGULAR_PAGE(page))
594  break;
595  }
596  }
597 
599 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
Definition: brin_tuple.c:321
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define BRIN_IS_REGULAR_PAGE(page)
Definition: brin_page.h:57
bool brin_doupdate(Relation idxrel, BlockNumber pagesPerRange, BrinRevmap *revmap, BlockNumber heapBlk, Buffer oldbuf, OffsetNumber oldoff, const BrinTuple *origtup, Size origsz, const BrinTuple *newtup, Size newsz, bool samepage)
Definition: brin_pageops.c:55
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
uint16 OffsetNumber
Definition: off.h:24
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
static char * buf
Definition: pg_test_fsync.c:67
#define FirstOffsetNumber
Definition: off.h:27
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
BlockNumber bt_blkno
Definition: brin_tuple.h:57
#define Assert(condition)
Definition: c.h:680
size_t Size
Definition: c.h:414
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define BRIN_EVACUATE_PAGE
Definition: brin_page.h:60
#define BrinPageFlags(page)
Definition: brin_page.h:46
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74

◆ brin_metapage_init()

void brin_metapage_init ( Page  page,
BlockNumber  pagesPerRange,
uint16  version 
)

Definition at line 482 of file brin_pageops.c.

References BRIN_META_MAGIC, brin_page_init(), BRIN_PAGETYPE_META, BrinMetaPageData::brinMagic, BrinMetaPageData::brinVersion, BrinMetaPageData::lastRevmapPage, PageGetContents, and BrinMetaPageData::pagesPerRange.

Referenced by brin_xlog_createidx(), brinbuild(), and brinbuildempty().

483 {
484  BrinMetaPageData *metadata;
485 
487 
488  metadata = (BrinMetaPageData *) PageGetContents(page);
489 
490  metadata->brinMagic = BRIN_META_MAGIC;
491  metadata->brinVersion = version;
492  metadata->pagesPerRange = pagesPerRange;
493 
494  /*
495  * Note we cheat here a little. 0 is not a valid revmap block number
496  * (because it's the metapage buffer), but doing this enables the first
497  * revmap page to be created when the index is.
498  */
499  metadata->lastRevmapPage = 0;
500 
501  /*
502  * Set pd_lower just past the end of the metadata. This is essential,
503  * because without doing so, metadata will be lost if xlog.c compresses
504  * the page.
505  */
506  ((PageHeader) page)->pd_lower =
507  ((char *) metadata + sizeof(BrinMetaPageData)) - (char *) page;
508 }
uint32 brinVersion
Definition: brin_page.h:67
uint32 brinMagic
Definition: brin_page.h:66
#define BRIN_PAGETYPE_META
Definition: brin_page.h:51
BlockNumber lastRevmapPage
Definition: brin_page.h:69
#define PageGetContents(page)
Definition: bufpage.h:242
BlockNumber pagesPerRange
Definition: brin_page.h:68
PageHeaderData * PageHeader
Definition: bufpage.h:162
void brin_page_init(Page page, uint16 type)
Definition: brin_pageops.c:471
#define BRIN_META_MAGIC
Definition: brin_page.h:73

◆ brin_page_cleanup()

bool brin_page_cleanup ( Relation  idxrel,
Buffer  buf 
)

Definition at line 609 of file brin_pageops.c.

References br_page_get_freespace(), brin_initialize_empty_new_buffer(), BRIN_IS_META_PAGE, BRIN_IS_REVMAP_PAGE, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage, GetRecordedFreeSpace(), LockBuffer(), LockRelationForExtension(), PageIsNew, RecordPageWithFreeSpace(), ShareLock, and UnlockRelationForExtension().

Referenced by brin_vacuum_scan().

610 {
611  Page page = BufferGetPage(buf);
612  Size freespace;
613 
614  /*
615  * If a page was left uninitialized, initialize it now; also record it in
616  * FSM.
617  *
618  * Somebody else might be extending the relation concurrently. To avoid
619  * re-initializing the page before they can grab the buffer lock, we
620  * acquire the extension lock momentarily. Since they hold the extension
621  * lock from before getting the page and after its been initialized, we're
622  * sure to see their initialization.
623  */
624  if (PageIsNew(page))
625  {
628 
630  if (PageIsNew(page))
631  {
634  return true;
635  }
637  }
638 
639  /* Nothing to be done for non-regular index pages */
642  return false;
643 
644  /* Measure free space and record it */
645  freespace = br_page_get_freespace(page);
646  if (freespace > GetRecordedFreeSpace(idxrel, BufferGetBlockNumber(buf)))
647  {
648  RecordPageWithFreeSpace(idxrel, BufferGetBlockNumber(buf), freespace);
649  return true;
650  }
651 
652  return false;
653 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:181
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
static char * buf
Definition: pg_test_fsync.c:67
static Size br_page_get_freespace(Page page)
Definition: brin_pageops.c:896
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:332
#define BRIN_IS_META_PAGE(page)
Definition: brin_page.h:55
Size GetRecordedFreeSpace(Relation rel, BlockNumber heapBlk)
Definition: freespace.c:270
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:382
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define BRIN_IS_REVMAP_PAGE(page)
Definition: brin_page.h:56
size_t Size
Definition: c.h:414
static void brin_initialize_empty_new_buffer(Relation idxrel, Buffer buffer)
Definition: brin_pageops.c:864
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
#define PageIsNew(page)
Definition: bufpage.h:225
#define ShareLock
Definition: lockdefs.h:41
Pointer Page
Definition: bufpage.h:74

◆ brin_page_init()

void brin_page_init ( Page  page,
uint16  type 
)

Definition at line 471 of file brin_pageops.c.

References BrinPageType, PageInit(), and generate_unaccent_rules::type.

Referenced by brin_doinsert(), brin_doupdate(), brin_initialize_empty_new_buffer(), brin_metapage_init(), brin_xlog_insert_update(), brin_xlog_revmap_extend(), and revmap_physical_extend().

472 {
473  PageInit(page, BLCKSZ, sizeof(BrinSpecialSpace));
474 
475  BrinPageType(page) = type;
476 }
#define BrinPageType(page)
Definition: brin_page.h:42
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:41

◆ brin_start_evacuating_page()

bool brin_start_evacuating_page ( Relation  idxRel,
Buffer  buf 
)

Definition at line 520 of file brin_pageops.c.

References BRIN_EVACUATE_PAGE, BrinPageFlags, BufferGetPage, FirstOffsetNumber, ItemIdIsUsed, MarkBufferDirtyHint(), PageGetItemId, PageGetMaxOffsetNumber, and PageIsNew.

Referenced by revmap_physical_extend().

521 {
522  OffsetNumber off;
523  OffsetNumber maxoff;
524  Page page;
525 
526  page = BufferGetPage(buf);
527 
528  if (PageIsNew(page))
529  return false;
530 
531  maxoff = PageGetMaxOffsetNumber(page);
532  for (off = FirstOffsetNumber; off <= maxoff; off++)
533  {
534  ItemId lp;
535 
536  lp = PageGetItemId(page, off);
537  if (ItemIdIsUsed(lp))
538  {
539  /* prevent other backends from adding more stuff to this page */
541  MarkBufferDirtyHint(buf, true);
542 
543  return true;
544  }
545  }
546  return false;
547 }
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:3379
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
uint16 OffsetNumber
Definition: off.h:24
static char * buf
Definition: pg_test_fsync.c:67
#define FirstOffsetNumber
Definition: off.h:27
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
#define PageIsNew(page)
Definition: bufpage.h:225
#define BRIN_EVACUATE_PAGE
Definition: brin_page.h:60
#define BrinPageFlags(page)
Definition: brin_page.h:46
Pointer Page
Definition: bufpage.h:74