PostgreSQL Source Code  git master
nbtdedup.c File Reference
#include "postgres.h"
#include "access/nbtree.h"
#include "access/nbtxlog.h"
#include "miscadmin.h"
#include "utils/rel.h"
Include dependency graph for nbtdedup.c:

Go to the source code of this file.

Functions

static bool _bt_do_singleval (Relation rel, Page page, BTDedupState state, OffsetNumber minoff, IndexTuple newitem)
 
static void _bt_singleval_fillfactor (Page page, BTDedupState state, Size newitemsz)
 
void _bt_dedup_one_page (Relation rel, Buffer buf, Relation heapRel, IndexTuple newitem, Size newitemsz, bool checkingunique)
 
void _bt_dedup_start_pending (BTDedupState state, IndexTuple base, OffsetNumber baseoff)
 
bool _bt_dedup_save_htid (BTDedupState state, IndexTuple itup)
 
Size _bt_dedup_finish_pending (Page newpage, BTDedupState state)
 
IndexTuple _bt_form_posting (IndexTuple base, ItemPointer htids, int nhtids)
 
void _bt_update_posting (BTVacuumPosting vacposting)
 
IndexTuple _bt_swap_posting (IndexTuple newitem, IndexTuple oposting, int postingoff)
 

Function Documentation

◆ _bt_dedup_finish_pending()

Size _bt_dedup_finish_pending ( Page  newpage,
BTDedupState  state 
)

Definition at line 431 of file nbtdedup.c.

References _bt_form_posting(), Assert, BTDedupStateData::base, BTDedupInterval::baseoff, BTDedupStateData::baseoff, elog, ERROR, BTDedupStateData::htids, IndexTupleSize, BTDedupStateData::intervals, InvalidOffsetNumber, MAXALIGN, BTDedupStateData::nhtids, BTDedupStateData::nintervals, BTDedupInterval::nitems, BTDedupStateData::nitems, OffsetNumberNext, PageAddItem, PageGetMaxOffsetNumber, pfree(), and BTDedupStateData::phystupsize.

Referenced by _bt_dedup_one_page(), and btree_xlog_dedup().

432 {
433  OffsetNumber tupoff;
434  Size tuplesz;
435  Size spacesaving;
436 
437  Assert(state->nitems > 0);
438  Assert(state->nitems <= state->nhtids);
439  Assert(state->intervals[state->nintervals].baseoff == state->baseoff);
440 
441  tupoff = OffsetNumberNext(PageGetMaxOffsetNumber(newpage));
442  if (state->nitems == 1)
443  {
444  /* Use original, unchanged base tuple */
445  tuplesz = IndexTupleSize(state->base);
446  if (PageAddItem(newpage, (Item) state->base, tuplesz, tupoff,
447  false, false) == InvalidOffsetNumber)
448  elog(ERROR, "deduplication failed to add tuple to page");
449 
450  spacesaving = 0;
451  }
452  else
453  {
454  IndexTuple final;
455 
456  /* Form a tuple with a posting list */
457  final = _bt_form_posting(state->base, state->htids, state->nhtids);
458  tuplesz = IndexTupleSize(final);
459  Assert(tuplesz <= state->maxpostingsize);
460 
461  /* Save final number of items for posting list */
462  state->intervals[state->nintervals].nitems = state->nitems;
463 
464  Assert(tuplesz == MAXALIGN(IndexTupleSize(final)));
465  if (PageAddItem(newpage, (Item) final, tuplesz, tupoff, false,
466  false) == InvalidOffsetNumber)
467  elog(ERROR, "deduplication failed to add tuple to page");
468 
469  pfree(final);
470  spacesaving = state->phystupsize - (tuplesz + sizeof(ItemIdData));
471  /* Increment nintervals, since we wrote a new posting list tuple */
472  state->nintervals++;
473  Assert(spacesaving > 0 && spacesaving < BLCKSZ);
474  }
475 
476  /* Reset state for next pending posting list */
477  state->nhtids = 0;
478  state->nitems = 0;
479  state->phystupsize = 0;
480 
481  return spacesaving;
482 }
IndexTuple base
Definition: nbtree.h:745
IndexTuple _bt_form_posting(IndexTuple base, ItemPointer htids, int nhtids)
Definition: nbtdedup.c:601
OffsetNumber baseoff
Definition: nbtree.h:746
Pointer Item
Definition: item.h:17
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
uint16 OffsetNumber
Definition: off.h:24
ItemPointer htids
Definition: nbtree.h:750
void pfree(void *pointer)
Definition: mcxt.c:1056
Size phystupsize
Definition: nbtree.h:753
#define ERROR
Definition: elog.h:43
BTDedupInterval intervals[MaxIndexTuplesPerPage]
Definition: nbtree.h:762
struct ItemIdData ItemIdData
#define InvalidOffsetNumber
Definition: off.h:26
uint16 nitems
Definition: nbtree.h:717
#define Assert(condition)
Definition: c.h:738
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:691
#define elog(elevel,...)
Definition: elog.h:214
OffsetNumber baseoff
Definition: nbtree.h:716
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ _bt_dedup_one_page()

void _bt_dedup_one_page ( Relation  rel,
Buffer  buf,
Relation  heapRel,
IndexTuple  newitem,
Size  newitemsz,
bool  checkingunique 
)

Definition at line 56 of file nbtdedup.c.

References _bt_dedup_finish_pending(), _bt_dedup_save_htid(), _bt_dedup_start_pending(), _bt_delitems_delete(), _bt_do_singleval(), _bt_keep_natts_fast(), _bt_singleval_fillfactor(), Assert, BTDedupStateData::base, BTDedupStateData::baseoff, BTDedupStateData::basetupsize, BTMaxItemSize, BTP_HAS_GARBAGE, BTPageOpaqueData::btpo_flags, BufferGetPage, BTDedupStateData::deduplicate, elog, END_CRIT_SECTION, ERROR, BTDedupStateData::htids, INDEX_SIZE_MASK, IndexRelationGetNumberOfKeyAttributes, BTDedupStateData::intervals, InvalidOffsetNumber, ItemIdGetLength, ItemIdIsDead, MarkBufferDirty(), MaxIndexTuplesPerPage, BTDedupStateData::maxpostingsize, Min, BTDedupStateData::nhtids, xl_btree_dedup::nintervals, BTDedupStateData::nintervals, BTDedupStateData::nitems, OffsetNumberNext, P_FIRSTDATAKEY, P_HAS_GARBAGE, P_HIKEY, P_RIGHTMOST, PageAddItem, PageGetExactFreeSpace(), PageGetFreeSpace(), PageGetItem, PageGetItemId, PageGetLSN, PageGetMaxOffsetNumber, PageGetSpecialPointer, PageGetTempPageCopySpecial(), PageRestoreTempPage(), PageSetLSN, palloc(), pfree(), BTDedupStateData::phystupsize, REGBUF_STANDARD, RelationNeedsWAL, SizeOfBtreeDedup, START_CRIT_SECTION, XLOG_BTREE_DEDUP, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by _bt_findinsertloc().

58 {
59  OffsetNumber offnum,
60  minoff,
61  maxoff;
62  Page page = BufferGetPage(buf);
63  BTPageOpaque opaque;
64  Page newpage;
65  int newpagendataitems = 0;
68  int ndeletable = 0;
69  Size pagesaving = 0;
70  bool singlevalstrat = false;
71  int nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
72 
73  /*
74  * We can't assume that there are no LP_DEAD items. For one thing, VACUUM
75  * will clear the BTP_HAS_GARBAGE hint without reliably removing items
76  * that are marked LP_DEAD. We don't want to unnecessarily unset LP_DEAD
77  * bits when deduplicating items. Allowing it would be correct, though
78  * wasteful.
79  */
80  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
81  minoff = P_FIRSTDATAKEY(opaque);
82  maxoff = PageGetMaxOffsetNumber(page);
83  for (offnum = minoff;
84  offnum <= maxoff;
85  offnum = OffsetNumberNext(offnum))
86  {
87  ItemId itemid = PageGetItemId(page, offnum);
88 
89  if (ItemIdIsDead(itemid))
90  deletable[ndeletable++] = offnum;
91  }
92 
93  if (ndeletable > 0)
94  {
95  _bt_delitems_delete(rel, buf, deletable, ndeletable, heapRel);
96 
97  /*
98  * Return when a split will be avoided. This is equivalent to
99  * avoiding a split using the usual _bt_vacuum_one_page() path.
100  */
101  if (PageGetFreeSpace(page) >= newitemsz)
102  return;
103 
104  /*
105  * Reconsider number of items on page, in case _bt_delitems_delete()
106  * managed to delete an item or two
107  */
108  minoff = P_FIRSTDATAKEY(opaque);
109  maxoff = PageGetMaxOffsetNumber(page);
110  }
111 
112  /* Passed-in newitemsz is MAXALIGNED but does not include line pointer */
113  newitemsz += sizeof(ItemIdData);
114 
115  /*
116  * By here, it's clear that deduplication will definitely be attempted.
117  * Initialize deduplication state.
118  *
119  * It would be possible for maxpostingsize (limit on posting list tuple
120  * size) to be set to one third of the page. However, it seems like a
121  * good idea to limit the size of posting lists to one sixth of a page.
122  * That ought to leave us with a good split point when pages full of
123  * duplicates can be split several times.
124  */
125  state = (BTDedupState) palloc(sizeof(BTDedupStateData));
126  state->deduplicate = true;
127  state->maxpostingsize = Min(BTMaxItemSize(page) / 2, INDEX_SIZE_MASK);
128  /* Metadata about base tuple of current pending posting list */
129  state->base = NULL;
130  state->baseoff = InvalidOffsetNumber;
131  state->basetupsize = 0;
132  /* Metadata about current pending posting list TIDs */
133  state->htids = palloc(state->maxpostingsize);
134  state->nhtids = 0;
135  state->nitems = 0;
136  /* Size of all physical tuples to be replaced by pending posting list */
137  state->phystupsize = 0;
138  /* nintervals should be initialized to zero */
139  state->nintervals = 0;
140 
141  /* Determine if "single value" strategy should be used */
142  if (!checkingunique)
143  singlevalstrat = _bt_do_singleval(rel, page, state, minoff, newitem);
144 
145  /*
146  * Deduplicate items from page, and write them to newpage.
147  *
148  * Copy the original page's LSN into newpage copy. This will become the
149  * updated version of the page. We need this because XLogInsert will
150  * examine the LSN and possibly dump it in a page image.
151  */
152  newpage = PageGetTempPageCopySpecial(page);
153  PageSetLSN(newpage, PageGetLSN(page));
154 
155  /* Copy high key, if any */
156  if (!P_RIGHTMOST(opaque))
157  {
158  ItemId hitemid = PageGetItemId(page, P_HIKEY);
159  Size hitemsz = ItemIdGetLength(hitemid);
160  IndexTuple hitem = (IndexTuple) PageGetItem(page, hitemid);
161 
162  if (PageAddItem(newpage, (Item) hitem, hitemsz, P_HIKEY,
163  false, false) == InvalidOffsetNumber)
164  elog(ERROR, "deduplication failed to add highkey");
165  }
166 
167  for (offnum = minoff;
168  offnum <= maxoff;
169  offnum = OffsetNumberNext(offnum))
170  {
171  ItemId itemid = PageGetItemId(page, offnum);
172  IndexTuple itup = (IndexTuple) PageGetItem(page, itemid);
173 
174  Assert(!ItemIdIsDead(itemid));
175 
176  if (offnum == minoff)
177  {
178  /*
179  * No previous/base tuple for the data item -- use the data item
180  * as base tuple of pending posting list
181  */
182  _bt_dedup_start_pending(state, itup, offnum);
183  }
184  else if (state->deduplicate &&
185  _bt_keep_natts_fast(rel, state->base, itup) > nkeyatts &&
186  _bt_dedup_save_htid(state, itup))
187  {
188  /*
189  * Tuple is equal to base tuple of pending posting list. Heap
190  * TID(s) for itup have been saved in state.
191  */
192  }
193  else
194  {
195  /*
196  * Tuple is not equal to pending posting list tuple, or
197  * _bt_dedup_save_htid() opted to not merge current item into
198  * pending posting list for some other reason (e.g., adding more
199  * TIDs would have caused posting list to exceed current
200  * maxpostingsize).
201  *
202  * If state contains pending posting list with more than one item,
203  * form new posting tuple, and actually update the page. Else
204  * reset the state and move on without modifying the page.
205  */
206  pagesaving += _bt_dedup_finish_pending(newpage, state);
207  newpagendataitems++;
208 
209  if (singlevalstrat)
210  {
211  /*
212  * Single value strategy's extra steps.
213  *
214  * Lower maxpostingsize for sixth and final item that might be
215  * deduplicated by current deduplication pass. When sixth
216  * item formed/observed, stop deduplicating items.
217  *
218  * Note: It's possible that this will be reached even when
219  * current deduplication pass has yet to merge together some
220  * existing items. It doesn't matter whether or not the
221  * current call generated the maxpostingsize-capped duplicate
222  * tuples at the start of the page.
223  */
224  if (newpagendataitems == 5)
225  _bt_singleval_fillfactor(page, state, newitemsz);
226  else if (newpagendataitems == 6)
227  {
228  state->deduplicate = false;
229  singlevalstrat = false; /* won't be back here */
230  }
231  }
232 
233  /* itup starts new pending posting list */
234  _bt_dedup_start_pending(state, itup, offnum);
235  }
236  }
237 
238  /* Handle the last item */
239  pagesaving += _bt_dedup_finish_pending(newpage, state);
240  newpagendataitems++;
241 
242  /*
243  * If no items suitable for deduplication were found, newpage must be
244  * exactly the same as the original page, so just return from function.
245  *
246  * We could determine whether or not to proceed on the basis the space
247  * savings being sufficient to avoid an immediate page split instead. We
248  * don't do that because there is some small value in nbtsplitloc.c always
249  * operating against a page that is fully deduplicated (apart from
250  * newitem). Besides, most of the cost has already been paid.
251  */
252  if (state->nintervals == 0)
253  {
254  /* cannot leak memory here */
255  pfree(newpage);
256  pfree(state->htids);
257  pfree(state);
258  return;
259  }
260 
261  /*
262  * By here, it's clear that deduplication will definitely go ahead.
263  *
264  * Clear the BTP_HAS_GARBAGE page flag in the unlikely event that it is
265  * still falsely set, just to keep things tidy. (We can't rely on
266  * _bt_vacuum_one_page() having done this already, and we can't rely on a
267  * page split or VACUUM getting to it in the near future.)
268  */
269  if (P_HAS_GARBAGE(opaque))
270  {
271  BTPageOpaque nopaque = (BTPageOpaque) PageGetSpecialPointer(newpage);
272 
273  nopaque->btpo_flags &= ~BTP_HAS_GARBAGE;
274  }
275 
277 
278  PageRestoreTempPage(newpage, page);
280 
281  /* XLOG stuff */
282  if (RelationNeedsWAL(rel))
283  {
284  XLogRecPtr recptr;
285  xl_btree_dedup xlrec_dedup;
286 
287  xlrec_dedup.nintervals = state->nintervals;
288 
289  XLogBeginInsert();
291  XLogRegisterData((char *) &xlrec_dedup, SizeOfBtreeDedup);
292 
293  /*
294  * The intervals array is not in the buffer, but pretend that it is.
295  * When XLogInsert stores the whole buffer, the array need not be
296  * stored too.
297  */
298  XLogRegisterBufData(0, (char *) state->intervals,
299  state->nintervals * sizeof(BTDedupInterval));
300 
301  recptr = XLogInsert(RM_BTREE_ID, XLOG_BTREE_DEDUP);
302 
303  PageSetLSN(page, recptr);
304  }
305 
307 
308  /* Local space accounting should agree with page accounting */
309  Assert(pagesaving < newitemsz || PageGetExactFreeSpace(page) >= newitemsz);
310 
311  /* cannot leak memory here */
312  pfree(state->htids);
313  pfree(state);
314 }
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:362
IndexTuple base
Definition: nbtree.h:745
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:403
uint16 nintervals
Definition: nbtxlog.h:172
OffsetNumber baseoff
Definition: nbtree.h:746
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:214
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:244
#define Min(x, y)
Definition: c.h:920
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
Pointer Item
Definition: item.h:17
#define INDEX_SIZE_MASK
Definition: itup.h:65
#define P_HAS_GARBAGE(opaque)
Definition: nbtree.h:220
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
void _bt_dedup_start_pending(BTDedupState state, IndexTuple base, OffsetNumber baseoff)
Definition: nbtdedup.c:326
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
Size _bt_dedup_finish_pending(Page newpage, BTDedupState state)
Definition: nbtdedup.c:431
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:574
uint16 OffsetNumber
Definition: off.h:24
Page PageGetTempPageCopySpecial(Page page)
Definition: bufpage.c:381
ItemPointer htids
Definition: nbtree.h:750
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
Size phystupsize
Definition: nbtree.h:753
#define ERROR
Definition: elog.h:43
BTDedupInterval intervals[MaxIndexTuplesPerPage]
Definition: nbtree.h:762
static bool _bt_do_singleval(Relation rel, Page page, BTDedupState state, OffsetNumber minoff, IndexTuple newitem)
Definition: nbtdedup.c:519
void _bt_delitems_delete(Relation rel, Buffer buf, OffsetNumber *deletable, int ndeletable, Relation heapRel)
Definition: nbtpage.c:1183
static char * buf
Definition: pg_test_fsync.c:67
bool _bt_dedup_save_htid(BTDedupState state, IndexTuple itup)
Definition: nbtdedup.c:377
IndexTupleData * IndexTuple
Definition: itup.h:53
#define REGBUF_STANDARD
Definition: xloginsert.h:35
struct ItemIdData ItemIdData
#define SizeOfBtreeDedup
Definition: nbtxlog.h:177
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:475
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define XLOG_BTREE_DEDUP
Definition: nbtxlog.h:32
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:324
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:416
#define InvalidOffsetNumber
Definition: off.h:26
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
Definition: regguts.h:298
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
size_t Size
Definition: c.h:466
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
Size basetupsize
Definition: nbtree.h:747
#define RelationNeedsWAL(relation)
Definition: rel.h:562
Size maxpostingsize
Definition: nbtree.h:742
#define PageGetLSN(page)
Definition: bufpage.h:366
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:625
#define BTMaxItemSize(page)
Definition: nbtree.h:157
#define P_HIKEY
Definition: nbtree.h:242
int _bt_keep_natts_fast(Relation rel, IndexTuple lastleft, IndexTuple firstright)
Definition: nbtutils.c:2418
#define MaxIndexTuplesPerPage
Definition: itup.h:145
void * palloc(Size size)
Definition: mcxt.c:949
bool deduplicate
Definition: nbtree.h:741
#define elog(elevel,...)
Definition: elog.h:214
BTDedupStateData * BTDedupState
Definition: nbtree.h:765
void XLogBeginInsert(void)
Definition: xloginsert.c:121
uint16 btpo_flags
Definition: nbtree.h:65
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:213
#define BTP_HAS_GARBAGE
Definition: nbtree.h:78
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
static void _bt_singleval_fillfactor(Page page, BTDedupState state, Size newitemsz)
Definition: nbtdedup.c:559

◆ _bt_dedup_save_htid()

bool _bt_dedup_save_htid ( BTDedupState  state,
IndexTuple  itup 
)

Definition at line 377 of file nbtdedup.c.

References Assert, BTDedupStateData::basetupsize, BTreeTupleGetNPosting(), BTreeTupleGetPosting(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), BTDedupStateData::htids, IndexTupleSize, MAXALIGN, BTDedupStateData::maxpostingsize, BTDedupStateData::nhtids, BTDedupStateData::nitems, BTDedupStateData::phystupsize, and IndexTupleData::t_tid.

Referenced by _bt_dedup_one_page(), _bt_load(), and btree_xlog_dedup().

378 {
379  int nhtids;
380  ItemPointer htids;
381  Size mergedtupsz;
382 
383  Assert(!BTreeTupleIsPivot(itup));
384 
385  if (!BTreeTupleIsPosting(itup))
386  {
387  nhtids = 1;
388  htids = &itup->t_tid;
389  }
390  else
391  {
392  nhtids = BTreeTupleGetNPosting(itup);
393  htids = BTreeTupleGetPosting(itup);
394  }
395 
396  /*
397  * Don't append (have caller finish pending posting list as-is) if
398  * appending heap TID(s) from itup would put us over maxpostingsize limit.
399  *
400  * This calculation needs to match the code used within _bt_form_posting()
401  * for new posting list tuples.
402  */
403  mergedtupsz = MAXALIGN(state->basetupsize +
404  (state->nhtids + nhtids) * sizeof(ItemPointerData));
405 
406  if (mergedtupsz > state->maxpostingsize)
407  return false;
408 
409  /*
410  * Save heap TIDs to pending posting list tuple -- itup can be merged into
411  * pending posting list
412  */
413  state->nitems++;
414  memcpy(state->htids + state->nhtids, htids,
415  sizeof(ItemPointerData) * nhtids);
416  state->nhtids += nhtids;
417  state->phystupsize += MAXALIGN(IndexTupleSize(itup)) + sizeof(ItemIdData);
418 
419  return true;
420 }
static bool BTreeTupleIsPivot(IndexTuple itup)
Definition: nbtree.h:348
static ItemPointer BTreeTupleGetPosting(IndexTuple posting)
Definition: nbtree.h:405
ItemPointerData t_tid
Definition: itup.h:37
ItemPointer htids
Definition: nbtree.h:750
Size phystupsize
Definition: nbtree.h:753
struct ItemIdData ItemIdData
static bool BTreeTupleIsPosting(IndexTuple itup)
Definition: nbtree.h:360
#define Assert(condition)
Definition: c.h:738
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:691
Size basetupsize
Definition: nbtree.h:747
Size maxpostingsize
Definition: nbtree.h:742
static uint16 BTreeTupleGetNPosting(IndexTuple posting)
Definition: nbtree.h:386
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ _bt_dedup_start_pending()

void _bt_dedup_start_pending ( BTDedupState  state,
IndexTuple  base,
OffsetNumber  baseoff 
)

Definition at line 326 of file nbtdedup.c.

References Assert, BTDedupStateData::base, BTDedupInterval::baseoff, BTDedupStateData::baseoff, BTDedupStateData::basetupsize, BTreeTupleGetNPosting(), BTreeTupleGetPosting(), BTreeTupleGetPostingOffset(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), BTDedupStateData::htids, IndexTupleSize, BTDedupStateData::intervals, MAXALIGN, BTDedupStateData::nhtids, BTDedupStateData::nintervals, BTDedupStateData::nitems, BTDedupStateData::phystupsize, and IndexTupleData::t_tid.

Referenced by _bt_dedup_one_page(), _bt_load(), and btree_xlog_dedup().

328 {
329  Assert(state->nhtids == 0);
330  Assert(state->nitems == 0);
331  Assert(!BTreeTupleIsPivot(base));
332 
333  /*
334  * Copy heap TID(s) from new base tuple for new candidate posting list
335  * into working state's array
336  */
337  if (!BTreeTupleIsPosting(base))
338  {
339  memcpy(state->htids, &base->t_tid, sizeof(ItemPointerData));
340  state->nhtids = 1;
341  state->basetupsize = IndexTupleSize(base);
342  }
343  else
344  {
345  int nposting;
346 
347  nposting = BTreeTupleGetNPosting(base);
348  memcpy(state->htids, BTreeTupleGetPosting(base),
349  sizeof(ItemPointerData) * nposting);
350  state->nhtids = nposting;
351  /* basetupsize should not include existing posting list */
353  }
354 
355  /*
356  * Save new base tuple itself -- it'll be needed if we actually create a
357  * new posting list from new pending posting list.
358  *
359  * Must maintain physical size of all existing tuples (including line
360  * pointer overhead) so that we can calculate space savings on page.
361  */
362  state->nitems = 1;
363  state->base = base;
364  state->baseoff = baseoff;
365  state->phystupsize = MAXALIGN(IndexTupleSize(base)) + sizeof(ItemIdData);
366  /* Also save baseoff in pending state for interval */
367  state->intervals[state->nintervals].baseoff = state->baseoff;
368 }
IndexTuple base
Definition: nbtree.h:745
static bool BTreeTupleIsPivot(IndexTuple itup)
Definition: nbtree.h:348
OffsetNumber baseoff
Definition: nbtree.h:746
static ItemPointer BTreeTupleGetPosting(IndexTuple posting)
Definition: nbtree.h:405
ItemPointerData t_tid
Definition: itup.h:37
ItemPointer htids
Definition: nbtree.h:750
Size phystupsize
Definition: nbtree.h:753
BTDedupInterval intervals[MaxIndexTuplesPerPage]
Definition: nbtree.h:762
struct ItemIdData ItemIdData
static bool BTreeTupleIsPosting(IndexTuple itup)
Definition: nbtree.h:360
static uint32 BTreeTupleGetPostingOffset(IndexTuple posting)
Definition: nbtree.h:397
#define Assert(condition)
Definition: c.h:738
#define MAXALIGN(LEN)
Definition: c.h:691
Size basetupsize
Definition: nbtree.h:747
static uint16 BTreeTupleGetNPosting(IndexTuple posting)
Definition: nbtree.h:386
OffsetNumber baseoff
Definition: nbtree.h:716
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ _bt_do_singleval()

static bool _bt_do_singleval ( Relation  rel,
Page  page,
BTDedupState  state,
OffsetNumber  minoff,
IndexTuple  newitem 
)
static

Definition at line 519 of file nbtdedup.c.

References _bt_keep_natts_fast(), IndexRelationGetNumberOfKeyAttributes, PageGetItem, PageGetItemId, and PageGetMaxOffsetNumber.

Referenced by _bt_dedup_one_page().

521 {
522  int nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
523  ItemId itemid;
524  IndexTuple itup;
525 
526  itemid = PageGetItemId(page, minoff);
527  itup = (IndexTuple) PageGetItem(page, itemid);
528 
529  if (_bt_keep_natts_fast(rel, newitem, itup) > nkeyatts)
530  {
531  itemid = PageGetItemId(page, PageGetMaxOffsetNumber(page));
532  itup = (IndexTuple) PageGetItem(page, itemid);
533 
534  if (_bt_keep_natts_fast(rel, newitem, itup) > nkeyatts)
535  return true;
536  }
537 
538  return false;
539 }
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
IndexTupleData * IndexTuple
Definition: itup.h:53
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:475
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
int _bt_keep_natts_fast(Relation rel, IndexTuple lastleft, IndexTuple firstright)
Definition: nbtutils.c:2418
#define PageGetItem(page, itemId)
Definition: bufpage.h:340

◆ _bt_form_posting()

IndexTuple _bt_form_posting ( IndexTuple  base,
ItemPointer  htids,
int  nhtids 
)

Definition at line 601 of file nbtdedup.c.

References Assert, BTreeTupleGetPosting(), BTreeTupleGetPostingOffset(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), BTreeTupleSetPosting(), INDEX_ALT_TID_MASK, INDEX_SIZE_MASK, IndexTupleSize, ItemPointerCopy, ItemPointerIsValid, MAXALIGN, palloc0(), PG_UINT16_MAX, IndexTupleData::t_info, and IndexTupleData::t_tid.

Referenced by _bt_dedup_finish_pending(), _bt_sort_dedup_finish_pending(), and bt_posting_plain_tuple().

602 {
603  uint32 keysize,
604  newsize;
605  IndexTuple itup;
606 
607  if (BTreeTupleIsPosting(base))
608  keysize = BTreeTupleGetPostingOffset(base);
609  else
610  keysize = IndexTupleSize(base);
611 
612  Assert(!BTreeTupleIsPivot(base));
613  Assert(nhtids > 0 && nhtids <= PG_UINT16_MAX);
614  Assert(keysize == MAXALIGN(keysize));
615 
616  /* Determine final size of new tuple */
617  if (nhtids > 1)
618  newsize = MAXALIGN(keysize +
619  nhtids * sizeof(ItemPointerData));
620  else
621  newsize = keysize;
622 
623  Assert(newsize <= INDEX_SIZE_MASK);
624  Assert(newsize == MAXALIGN(newsize));
625 
626  /* Allocate memory using palloc0() (matches index_form_tuple()) */
627  itup = palloc0(newsize);
628  memcpy(itup, base, keysize);
629  itup->t_info &= ~INDEX_SIZE_MASK;
630  itup->t_info |= newsize;
631  if (nhtids > 1)
632  {
633  /* Form posting list tuple */
634  BTreeTupleSetPosting(itup, nhtids, keysize);
635  memcpy(BTreeTupleGetPosting(itup), htids,
636  sizeof(ItemPointerData) * nhtids);
637  Assert(_bt_posting_valid(itup));
638  }
639  else
640  {
641  /* Form standard non-pivot tuple */
642  itup->t_info &= ~INDEX_ALT_TID_MASK;
643  ItemPointerCopy(htids, &itup->t_tid);
645  }
646 
647  return itup;
648 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:82
static bool BTreeTupleIsPivot(IndexTuple itup)
Definition: nbtree.h:348
static ItemPointer BTreeTupleGetPosting(IndexTuple posting)
Definition: nbtree.h:405
ItemPointerData t_tid
Definition: itup.h:37
#define INDEX_SIZE_MASK
Definition: itup.h:65
#define INDEX_ALT_TID_MASK
Definition: nbtree.h:334
#define PG_UINT16_MAX
Definition: c.h:448
static void BTreeTupleSetPosting(IndexTuple itup, uint16 nhtids, int postingoffset)
Definition: nbtree.h:372
unsigned int uint32
Definition: c.h:367
void * palloc0(Size size)
Definition: mcxt.c:980
static bool BTreeTupleIsPosting(IndexTuple itup)
Definition: nbtree.h:360
static uint32 BTreeTupleGetPostingOffset(IndexTuple posting)
Definition: nbtree.h:397
#define Assert(condition)
Definition: c.h:738
#define MAXALIGN(LEN)
Definition: c.h:691
unsigned short t_info
Definition: itup.h:49
#define IndexTupleSize(itup)
Definition: itup.h:71
#define ItemPointerCopy(fromPointer, toPointer)
Definition: itemptr.h:161

◆ _bt_singleval_fillfactor()

static void _bt_singleval_fillfactor ( Page  page,
BTDedupState  state,
Size  newitemsz 
)
static

Definition at line 559 of file nbtdedup.c.

References BTREE_SINGLEVAL_FILLFACTOR, MAXALIGN, BTDedupStateData::maxpostingsize, PageGetPageSize, and SizeOfPageHeaderData.

Referenced by _bt_dedup_one_page().

560 {
561  Size leftfree;
562  int reduction;
563 
564  /* This calculation needs to match nbtsplitloc.c */
565  leftfree = PageGetPageSize(page) - SizeOfPageHeaderData -
566  MAXALIGN(sizeof(BTPageOpaqueData));
567  /* Subtract size of new high key (includes pivot heap TID space) */
568  leftfree -= newitemsz + MAXALIGN(sizeof(ItemPointerData));
569 
570  /*
571  * Reduce maxpostingsize by an amount equal to target free space on left
572  * half of page
573  */
574  reduction = leftfree * ((100 - BTREE_SINGLEVAL_FILLFACTOR) / 100.0);
575  if (state->maxpostingsize > reduction)
576  state->maxpostingsize -= reduction;
577  else
578  state->maxpostingsize = 0;
579 }
#define SizeOfPageHeaderData
Definition: bufpage.h:216
#define PageGetPageSize(page)
Definition: bufpage.h:268
#define BTREE_SINGLEVAL_FILLFACTOR
Definition: nbtree.h:196
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:691
Size maxpostingsize
Definition: nbtree.h:742

◆ _bt_swap_posting()

IndexTuple _bt_swap_posting ( IndexTuple  newitem,
IndexTuple  oposting,
int  postingoff 
)

Definition at line 759 of file nbtdedup.c.

References Assert, BTreeTupleGetHeapTID(), BTreeTupleGetMaxHeapTID(), BTreeTupleGetNPosting(), BTreeTupleGetPostingN(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), CopyIndexTuple(), i, ItemPointerCompare(), ItemPointerCopy, ItemPointerIsValid, and IndexTupleData::t_tid.

Referenced by _bt_insertonpg(), btree_xlog_insert(), and btree_xlog_split().

760 {
761  int nhtids;
762  char *replacepos;
763  char *replaceposright;
764  Size nmovebytes;
765  IndexTuple nposting;
766 
767  nhtids = BTreeTupleGetNPosting(oposting);
768  Assert(_bt_posting_valid(oposting));
769  Assert(postingoff > 0 && postingoff < nhtids);
770 
771  /*
772  * Move item pointers in posting list to make a gap for the new item's
773  * heap TID. We shift TIDs one place to the right, losing original
774  * rightmost TID. (nmovebytes must not include TIDs to the left of
775  * postingoff, nor the existing rightmost/max TID that gets overwritten.)
776  */
777  nposting = CopyIndexTuple(oposting);
778  replacepos = (char *) BTreeTupleGetPostingN(nposting, postingoff);
779  replaceposright = (char *) BTreeTupleGetPostingN(nposting, postingoff + 1);
780  nmovebytes = (nhtids - postingoff - 1) * sizeof(ItemPointerData);
781  memmove(replaceposright, replacepos, nmovebytes);
782 
783  /* Fill the gap at postingoff with TID of new item (original new TID) */
784  Assert(!BTreeTupleIsPivot(newitem) && !BTreeTupleIsPosting(newitem));
785  ItemPointerCopy(&newitem->t_tid, (ItemPointer) replacepos);
786 
787  /* Now copy oposting's rightmost/max TID into new item (final new TID) */
788  ItemPointerCopy(BTreeTupleGetMaxHeapTID(oposting), &newitem->t_tid);
789 
791  BTreeTupleGetHeapTID(newitem)) < 0);
792  Assert(_bt_posting_valid(nposting));
793 
794  return nposting;
795 }
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:52
static bool BTreeTupleIsPivot(IndexTuple itup)
Definition: nbtree.h:348
static ItemPointer BTreeTupleGetHeapTID(IndexTuple itup)
Definition: nbtree.h:506
ItemPointerData t_tid
Definition: itup.h:37
IndexTuple CopyIndexTuple(IndexTuple source)
Definition: indextuple.c:510
static ItemPointer BTreeTupleGetMaxHeapTID(IndexTuple itup)
Definition: nbtree.h:532
static ItemPointer BTreeTupleGetPostingN(IndexTuple posting, int n)
Definition: nbtree.h:412
static bool BTreeTupleIsPosting(IndexTuple itup)
Definition: nbtree.h:360
#define Assert(condition)
Definition: c.h:738
size_t Size
Definition: c.h:466
static uint16 BTreeTupleGetNPosting(IndexTuple posting)
Definition: nbtree.h:386
#define ItemPointerCopy(fromPointer, toPointer)
Definition: itemptr.h:161

◆ _bt_update_posting()

void _bt_update_posting ( BTVacuumPosting  vacposting)

Definition at line 661 of file nbtdedup.c.

References Assert, BTreeTupleGetNPosting(), BTreeTupleGetPosting(), BTreeTupleGetPostingN(), BTreeTupleGetPostingOffset(), BTreeTupleSetPosting(), BTVacuumPostingData::deletetids, i, INDEX_ALT_TID_MASK, INDEX_SIZE_MASK, ItemPointerIsValid, BTVacuumPostingData::itup, MAXALIGN, BTVacuumPostingData::ndeletedtids, palloc0(), IndexTupleData::t_info, and IndexTupleData::t_tid.

Referenced by _bt_delitems_vacuum(), and btree_xlog_vacuum().

662 {
663  IndexTuple origtuple = vacposting->itup;
664  uint32 keysize,
665  newsize;
666  IndexTuple itup;
667  int nhtids;
668  int ui,
669  d;
670  ItemPointer htids;
671 
672  nhtids = BTreeTupleGetNPosting(origtuple) - vacposting->ndeletedtids;
673 
674  Assert(_bt_posting_valid(origtuple));
675  Assert(nhtids > 0 && nhtids < BTreeTupleGetNPosting(origtuple));
676 
677  /*
678  * Determine final size of new tuple.
679  *
680  * This calculation needs to match the code used within _bt_form_posting()
681  * for new posting list tuples. We avoid calling _bt_form_posting() here
682  * to save ourselves a second memory allocation for a htids workspace.
683  */
684  keysize = BTreeTupleGetPostingOffset(origtuple);
685  if (nhtids > 1)
686  newsize = MAXALIGN(keysize +
687  nhtids * sizeof(ItemPointerData));
688  else
689  newsize = keysize;
690 
691  Assert(newsize <= INDEX_SIZE_MASK);
692  Assert(newsize == MAXALIGN(newsize));
693 
694  /* Allocate memory using palloc0() (matches index_form_tuple()) */
695  itup = palloc0(newsize);
696  memcpy(itup, origtuple, keysize);
697  itup->t_info &= ~INDEX_SIZE_MASK;
698  itup->t_info |= newsize;
699 
700  if (nhtids > 1)
701  {
702  /* Form posting list tuple */
703  BTreeTupleSetPosting(itup, nhtids, keysize);
704  htids = BTreeTupleGetPosting(itup);
705  }
706  else
707  {
708  /* Form standard non-pivot tuple */
709  itup->t_info &= ~INDEX_ALT_TID_MASK;
710  htids = &itup->t_tid;
711  }
712 
713  ui = 0;
714  d = 0;
715  for (int i = 0; i < BTreeTupleGetNPosting(origtuple); i++)
716  {
717  if (d < vacposting->ndeletedtids && vacposting->deletetids[d] == i)
718  {
719  d++;
720  continue;
721  }
722  htids[ui++] = *BTreeTupleGetPostingN(origtuple, i);
723  }
724  Assert(ui == nhtids);
725  Assert(d == vacposting->ndeletedtids);
726  Assert(nhtids == 1 || _bt_posting_valid(itup));
727  Assert(nhtids > 1 || ItemPointerIsValid(&itup->t_tid));
728 
729  /* vacposting arg's itup will now point to updated version */
730  vacposting->itup = itup;
731 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:82
uint16 ndeletedtids
Definition: nbtree.h:781
static ItemPointer BTreeTupleGetPosting(IndexTuple posting)
Definition: nbtree.h:405
ItemPointerData t_tid
Definition: itup.h:37
IndexTuple itup
Definition: nbtree.h:777
#define INDEX_SIZE_MASK
Definition: itup.h:65
#define INDEX_ALT_TID_MASK
Definition: nbtree.h:334
static void BTreeTupleSetPosting(IndexTuple itup, uint16 nhtids, int postingoffset)
Definition: nbtree.h:372
unsigned int uint32
Definition: c.h:367
void * palloc0(Size size)
Definition: mcxt.c:980
uint16 deletetids[FLEXIBLE_ARRAY_MEMBER]
Definition: nbtree.h:782
static ItemPointer BTreeTupleGetPostingN(IndexTuple posting, int n)
Definition: nbtree.h:412
static uint32 BTreeTupleGetPostingOffset(IndexTuple posting)
Definition: nbtree.h:397
#define Assert(condition)
Definition: c.h:738
#define MAXALIGN(LEN)
Definition: c.h:691
int i
static uint16 BTreeTupleGetNPosting(IndexTuple posting)
Definition: nbtree.h:386
unsigned short t_info
Definition: itup.h:49