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

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

Definition at line 323 of file brin_pageops.c.

References BufferGetPage, and PageGetExactFreeSpace().

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

324 {
325  return
326  ((newsz <= origsz) ||
327  PageGetExactFreeSpace(BufferGetPage(buffer)) >= (newsz - origsz));
328 }
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:207
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:633
OffsetNumber brin_doinsert ( Relation  idxrel,
BlockNumber  pagesPerRange,
BrinRevmap revmap,
Buffer buffer,
BlockNumber  heapBlk,
BrinTuple tup,
Size  itemsz 
)

Definition at line 342 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().

345 {
346  Page page;
347  BlockNumber blk;
348  OffsetNumber off;
349  Buffer revmapbuf;
350  ItemPointerData tid;
351  bool extended;
352 
353  Assert(itemsz == MAXALIGN(itemsz));
354 
355  /* If the item is oversized, don't even bother. */
356  if (itemsz > BrinMaxItemSize)
357  {
358  ereport(ERROR,
359  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
360  errmsg("index row size %lu exceeds maximum %lu for index \"%s\"",
361  (unsigned long) itemsz,
362  (unsigned long) BrinMaxItemSize,
363  RelationGetRelationName(idxrel))));
364  return InvalidOffsetNumber; /* keep compiler quiet */
365  }
366 
367  /* Make sure the revmap is long enough to contain the entry we need */
368  brinRevmapExtend(revmap, heapBlk);
369 
370  /*
371  * Acquire lock on buffer supplied by caller, if any. If it doesn't have
372  * enough space, unpin it to obtain a new one below.
373  */
374  if (BufferIsValid(*buffer))
375  {
376  /*
377  * It's possible that another backend (or ourselves!) extended the
378  * revmap over the page we held a pin on, so we cannot assume that
379  * it's still a regular page.
380  */
383  {
386  }
387  }
388 
389  /*
390  * If we still don't have a usable buffer, have brin_getinsertbuffer
391  * obtain one for us.
392  */
393  if (!BufferIsValid(*buffer))
394  {
395  do
396  *buffer = brin_getinsertbuffer(idxrel, InvalidBuffer, itemsz, &extended);
397  while (!BufferIsValid(*buffer));
398  }
399  else
400  extended = false;
401 
402  /* Now obtain lock on revmap buffer */
403  revmapbuf = brinLockRevmapPageForUpdate(revmap, heapBlk);
404 
405  page = BufferGetPage(*buffer);
407 
408  /* Execute the actual insertion */
410  if (extended)
412  off = PageAddItem(page, (Item) tup, itemsz, InvalidOffsetNumber,
413  false, false);
414  if (off == InvalidOffsetNumber)
415  elog(ERROR, "could not insert new index tuple to page");
417 
418  BRIN_elog((DEBUG2, "inserted tuple (%u,%u) for range starting at %u",
419  blk, off, heapBlk));
420 
421  ItemPointerSet(&tid, blk, off);
422  brinSetHeapBlockItemptr(revmapbuf, pagesPerRange, heapBlk, tid);
423  MarkBufferDirty(revmapbuf);
424 
425  /* XLOG stuff */
426  if (RelationNeedsWAL(idxrel))
427  {
428  xl_brin_insert xlrec;
429  XLogRecPtr recptr;
430  uint8 info;
431 
432  info = XLOG_BRIN_INSERT | (extended ? XLOG_BRIN_INIT_PAGE : 0);
433  xlrec.heapBlk = heapBlk;
434  xlrec.pagesPerRange = pagesPerRange;
435  xlrec.offnum = off;
436 
437  XLogBeginInsert();
438  XLogRegisterData((char *) &xlrec, SizeOfBrinInsert);
439 
441  XLogRegisterBufData(0, (char *) tup, itemsz);
442 
443  XLogRegisterBuffer(1, revmapbuf, 0);
444 
445  recptr = XLogInsert(RM_BRIN_ID, info);
446 
447  PageSetLSN(page, recptr);
448  PageSetLSN(BufferGetPage(revmapbuf), recptr);
449  }
450 
452 
453  /* Tuple is firmly on buffer; we can release our locks */
455  LockBuffer(revmapbuf, BUFFER_LOCK_UNLOCK);
456 
457  if (extended)
458  FreeSpaceMapVacuum(idxrel);
459 
460  return off;
461 }
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:132
unsigned char uint8
Definition: c.h:266
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:130
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:413
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:35
static Size br_page_get_freespace(Page page)
Definition: brin_pageops.c:886
#define RelationGetRelationName(relation)
Definition: rel.h:437
#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:675
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:207
OffsetNumber offnum
Definition: brin_xlog.h:71
#define MAXALIGN(LEN)
Definition: c.h:588
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
#define RelationNeedsWAL(relation)
Definition: rel.h:506
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:664
#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:365
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:86
void brin_page_init(Page page, uint16 type)
Definition: brin_pageops.c:469
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_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, 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 %lu exceeds maximum %lu for index \"%s\"",
77  (unsigned long) newsz,
78  (unsigned long) BrinMaxItemSize,
79  RelationGetRelationName(idxrel))));
80  return false; /* keep compiler quiet */
81  }
82 
83  /* make sure the revmap is long enough to contain the entry we need */
84  brinRevmapExtend(revmap, heapBlk);
85 
86  if (!samepage)
87  {
88  /* need a page on which to put the item */
89  newbuf = brin_getinsertbuffer(idxrel, oldbuf, newsz, &extended);
90  if (!BufferIsValid(newbuf))
91  {
92  Assert(!extended);
93  return false;
94  }
95 
96  /*
97  * Note: it's possible (though unlikely) that the returned newbuf is
98  * the same as oldbuf, if brin_getinsertbuffer determined that the old
99  * buffer does in fact have enough space.
100  */
101  if (newbuf == oldbuf)
102  {
103  Assert(!extended);
104  newbuf = InvalidBuffer;
105  }
106  }
107  else
108  {
110  newbuf = InvalidBuffer;
111  extended = false;
112  }
113  oldpage = BufferGetPage(oldbuf);
114  oldlp = PageGetItemId(oldpage, oldoff);
115 
116  /*
117  * Check that the old tuple wasn't updated concurrently: it might have
118  * moved someplace else entirely ...
119  */
120  if (!ItemIdIsNormal(oldlp))
121  {
123 
124  /*
125  * If this happens, and the new buffer was obtained by extending the
126  * relation, then we need to ensure we don't leave it uninitialized or
127  * forget about it.
128  */
129  if (BufferIsValid(newbuf))
130  {
131  if (extended)
132  brin_initialize_empty_new_buffer(idxrel, newbuf);
133  UnlockReleaseBuffer(newbuf);
134  if (extended)
135  FreeSpaceMapVacuum(idxrel);
136  }
137  return false;
138  }
139 
140  oldsz = ItemIdGetLength(oldlp);
141  oldtup = (BrinTuple *) PageGetItem(oldpage, oldlp);
142 
143  /*
144  * ... or it might have been updated in place to different contents.
145  */
146  if (!brin_tuples_equal(oldtup, oldsz, origtup, origsz))
147  {
149  if (BufferIsValid(newbuf))
150  {
151  if (extended)
152  brin_initialize_empty_new_buffer(idxrel, newbuf);
153  UnlockReleaseBuffer(newbuf);
154  if (extended)
155  FreeSpaceMapVacuum(idxrel);
156  }
157  return false;
158  }
159 
160  /*
161  * Great, the old tuple is intact. We can proceed with the update.
162  *
163  * If there's enough room in the old page for the new tuple, replace it.
164  *
165  * Note that there might now be enough space on the page even though the
166  * caller told us there isn't, if a concurrent update moved another tuple
167  * elsewhere or replaced a tuple with a smaller one.
168  */
169  if (((BrinPageFlags(oldpage) & BRIN_EVACUATE_PAGE) == 0) &&
170  brin_can_do_samepage_update(oldbuf, origsz, newsz))
171  {
172  if (BufferIsValid(newbuf))
173  {
174  /* as above */
175  if (extended)
176  brin_initialize_empty_new_buffer(idxrel, newbuf);
177  UnlockReleaseBuffer(newbuf);
178  }
179 
181  if (!PageIndexTupleOverwrite(oldpage, oldoff, (Item) newtup, newsz))
182  elog(ERROR, "failed to replace BRIN tuple");
183  MarkBufferDirty(oldbuf);
184 
185  /* XLOG stuff */
186  if (RelationNeedsWAL(idxrel))
187  {
189  XLogRecPtr recptr;
191 
192  xlrec.offnum = oldoff;
193 
194  XLogBeginInsert();
195  XLogRegisterData((char *) &xlrec, SizeOfBrinSamepageUpdate);
196 
198  XLogRegisterBufData(0, (char *) newtup, newsz);
199 
200  recptr = XLogInsert(RM_BRIN_ID, info);
201 
202  PageSetLSN(oldpage, recptr);
203  }
204 
206 
208 
209  if (extended)
210  FreeSpaceMapVacuum(idxrel);
211 
212  return true;
213  }
214  else if (newbuf == InvalidBuffer)
215  {
216  /*
217  * Not enough space, but caller said that there was. Tell them to
218  * start over.
219  */
221  return false;
222  }
223  else
224  {
225  /*
226  * Not enough free space on the oldpage. Put the new tuple on the new
227  * page, and update the revmap.
228  */
229  Page newpage = BufferGetPage(newbuf);
230  Buffer revmapbuf;
231  ItemPointerData newtid;
232  OffsetNumber newoff;
234  Size freespace = 0;
235 
236  revmapbuf = brinLockRevmapPageForUpdate(revmap, heapBlk);
237 
239 
240  /*
241  * We need to initialize the page if it's newly obtained. Note we
242  * will WAL-log the initialization as part of the update, so we don't
243  * need to do that here.
244  */
245  if (extended)
247 
248  PageIndexTupleDeleteNoCompact(oldpage, oldoff);
249  newoff = PageAddItem(newpage, (Item) newtup, newsz,
250  InvalidOffsetNumber, false, false);
251  if (newoff == InvalidOffsetNumber)
252  elog(ERROR, "failed to add BRIN tuple to new page");
253  MarkBufferDirty(oldbuf);
254  MarkBufferDirty(newbuf);
255 
256  /* needed to update FSM below */
257  if (extended)
258  {
259  newblk = BufferGetBlockNumber(newbuf);
260  freespace = br_page_get_freespace(newpage);
261  }
262 
263  ItemPointerSet(&newtid, BufferGetBlockNumber(newbuf), newoff);
264  brinSetHeapBlockItemptr(revmapbuf, pagesPerRange, heapBlk, newtid);
265  MarkBufferDirty(revmapbuf);
266 
267  /* XLOG stuff */
268  if (RelationNeedsWAL(idxrel))
269  {
270  xl_brin_update xlrec;
271  XLogRecPtr recptr;
272  uint8 info;
273 
274  info = XLOG_BRIN_UPDATE | (extended ? XLOG_BRIN_INIT_PAGE : 0);
275 
276  xlrec.insert.offnum = newoff;
277  xlrec.insert.heapBlk = heapBlk;
278  xlrec.insert.pagesPerRange = pagesPerRange;
279  xlrec.oldOffnum = oldoff;
280 
281  XLogBeginInsert();
282 
283  /* new page */
284  XLogRegisterData((char *) &xlrec, SizeOfBrinUpdate);
285 
286  XLogRegisterBuffer(0, newbuf, REGBUF_STANDARD | (extended ? REGBUF_WILL_INIT : 0));
287  XLogRegisterBufData(0, (char *) newtup, newsz);
288 
289  /* revmap page */
290  XLogRegisterBuffer(1, revmapbuf, 0);
291 
292  /* old page */
294 
295  recptr = XLogInsert(RM_BRIN_ID, info);
296 
297  PageSetLSN(oldpage, recptr);
298  PageSetLSN(newpage, recptr);
299  PageSetLSN(BufferGetPage(revmapbuf), recptr);
300  }
301 
303 
304  LockBuffer(revmapbuf, BUFFER_LOCK_UNLOCK);
306  UnlockReleaseBuffer(newbuf);
307 
308  if (extended)
309  {
310  Assert(BlockNumberIsValid(newblk));
311  RecordPageWithFreeSpace(idxrel, newblk, freespace);
312  FreeSpaceMapVacuum(idxrel);
313  }
314 
315  return true;
316  }
317 }
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:132
OffsetNumber offnum
Definition: brin_xlog.h:104
unsigned char uint8
Definition: c.h:266
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:130
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:413
uint32 BlockNumber
Definition: block.h:31
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
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:1066
bool brin_tuples_equal(const BrinTuple *a, Size alen, const BrinTuple *b, Size blen)
Definition: brin_tuple.c:331
BlockNumber pagesPerRange
Definition: brin_xlog.h:68
#define REGBUF_STANDARD
Definition: xloginsert.h:35
static Size br_page_get_freespace(Page page)
Definition: brin_pageops.c:886
#define RelationGetRelationName(relation)
Definition: rel.h:437
#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:323
void FreeSpaceMapVacuum(Relation rel)
Definition: freespace.c:379
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
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:958
#define BrinMaxItemSize
Definition: brin_pageops.c:30
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
#define ItemIdIsNormal(itemId)
Definition: itemid.h:98
#define SizeOfBrinSamepageUpdate
Definition: brin_xlog.h:107
size_t Size
Definition: c.h:356
#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:854
#define MAXALIGN(LEN)
Definition: c.h:588
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
#define RelationNeedsWAL(relation)
Definition: rel.h:506
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:664
#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:365
OffsetNumber oldOffnum
Definition: brin_xlog.h:90
int Buffer
Definition: buf.h:23
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
Pointer Page
Definition: bufpage.h:74
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:86
void brin_page_init(Page page, uint16 type)
Definition: brin_pageops.c:469
void brin_evacuate_page ( Relation  idxRel,
BlockNumber  pagesPerRange,
BrinRevmap revmap,
Buffer  buf 
)

Definition at line 545 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().

547 {
548  OffsetNumber off;
549  OffsetNumber maxoff;
550  Page page;
551 
552  page = BufferGetPage(buf);
553 
555 
556  maxoff = PageGetMaxOffsetNumber(page);
557  for (off = FirstOffsetNumber; off <= maxoff; off++)
558  {
559  BrinTuple *tup;
560  Size sz;
561  ItemId lp;
562 
564 
565  lp = PageGetItemId(page, off);
566  if (ItemIdIsUsed(lp))
567  {
568  sz = ItemIdGetLength(lp);
569  tup = (BrinTuple *) PageGetItem(page, lp);
570  tup = brin_copy_tuple(tup, sz);
571 
573 
574  if (!brin_doupdate(idxRel, pagesPerRange, revmap, tup->bt_blkno,
575  buf, off, tup, sz, tup, sz, false))
576  off--; /* retry */
577 
579 
580  /* It's possible that someone extended the revmap over this page */
581  if (!BRIN_IS_REGULAR_PAGE(page))
582  break;
583  }
584  }
585 
587 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define BRIN_IS_REGULAR_PAGE(page)
Definition: brin_page.h:57
BrinTuple * brin_copy_tuple(BrinTuple *tuple, Size len)
Definition: brin_tuple.c:317
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:354
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:65
#define FirstOffsetNumber
Definition: off.h:27
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
BlockNumber bt_blkno
Definition: brin_tuple.h:52
#define Assert(condition)
Definition: c.h:675
size_t Size
Definition: c.h:356
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define BRIN_EVACUATE_PAGE
Definition: brin_page.h:60
#define BrinPageFlags(page)
Definition: brin_page.h:46
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
Pointer Page
Definition: bufpage.h:74
void brin_metapage_init ( Page  page,
BlockNumber  pagesPerRange,
uint16  version 
)

Definition at line 480 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().

481 {
482  BrinMetaPageData *metadata;
483 
485 
486  metadata = (BrinMetaPageData *) PageGetContents(page);
487 
488  metadata->brinMagic = BRIN_META_MAGIC;
489  metadata->brinVersion = version;
490  metadata->pagesPerRange = pagesPerRange;
491 
492  /*
493  * Note we cheat here a little. 0 is not a valid revmap block number
494  * (because it's the metapage buffer), but doing this enables the first
495  * revmap page to be created when the index is.
496  */
497  metadata->lastRevmapPage = 0;
498 }
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:243
BlockNumber pagesPerRange
Definition: brin_page.h:68
void brin_page_init(Page page, uint16 type)
Definition: brin_pageops.c:469
#define BRIN_META_MAGIC
Definition: brin_page.h:73
bool brin_page_cleanup ( Relation  idxrel,
Buffer  buf 
)

Definition at line 597 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().

598 {
599  Page page = BufferGetPage(buf);
600  Size freespace;
601 
602  /*
603  * If a page was left uninitialized, initialize it now; also record it in
604  * FSM.
605  *
606  * Somebody else might be extending the relation concurrently. To avoid
607  * re-initializing the page before they can grab the buffer lock, we
608  * acquire the extension lock momentarily. Since they hold the extension
609  * lock from before getting the page and after its been initialized, we're
610  * sure to see their initialization.
611  */
612  if (PageIsNew(page))
613  {
616 
618  if (PageIsNew(page))
619  {
622  return true;
623  }
625  }
626 
627  /* Nothing to be done for non-regular index pages */
630  return false;
631 
632  /* Measure free space and record it */
633  freespace = br_page_get_freespace(page);
634  if (freespace > GetRecordedFreeSpace(idxrel, BufferGetBlockNumber(buf)))
635  {
636  RecordPageWithFreeSpace(idxrel, BufferGetBlockNumber(buf), freespace);
637  return true;
638  }
639 
640  return false;
641 }
#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:65
static Size br_page_get_freespace(Page page)
Definition: brin_pageops.c:886
#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:356
static void brin_initialize_empty_new_buffer(Relation idxrel, Buffer buffer)
Definition: brin_pageops.c:854
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
#define PageIsNew(page)
Definition: bufpage.h:226
#define ShareLock
Definition: lockdefs.h:41
Pointer Page
Definition: bufpage.h:74
void brin_page_init ( Page  page,
uint16  type 
)

Definition at line 469 of file brin_pageops.c.

References BrinPageType, and PageInit().

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().

470 {
471  PageInit(page, BLCKSZ, sizeof(BrinSpecialSpace));
472 
473  BrinPageType(page) = type;
474 }
#define BrinPageType(page)
Definition: brin_page.h:42
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:41
bool brin_start_evacuating_page ( Relation  idxRel,
Buffer  buf 
)

Definition at line 510 of file brin_pageops.c.

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

Referenced by revmap_physical_extend().

511 {
512  OffsetNumber off;
513  OffsetNumber maxoff;
514  Page page;
515 
516  page = BufferGetPage(buf);
517 
518  if (PageIsNew(page))
519  return false;
520 
521  maxoff = PageGetMaxOffsetNumber(page);
522  for (off = FirstOffsetNumber; off <= maxoff; off++)
523  {
524  ItemId lp;
525 
526  lp = PageGetItemId(page, off);
527  if (ItemIdIsUsed(lp))
528  {
529  /* prevent other backends from adding more stuff to this page */
531  MarkBufferDirtyHint(buf, true);
532 
533  return true;
534  }
535  }
536  return false;
537 }
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:3379
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
uint16 OffsetNumber
Definition: off.h:24
static char * buf
Definition: pg_test_fsync.c:65
#define FirstOffsetNumber
Definition: off.h:27
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
#define PageIsNew(page)
Definition: bufpage.h:226
#define BRIN_EVACUATE_PAGE
Definition: brin_page.h:60
#define BrinPageFlags(page)
Definition: brin_page.h:46
Pointer Page
Definition: bufpage.h:74