PostgreSQL Source Code  git master
gindatapage.c File Reference
#include "postgres.h"
#include "access/gin_private.h"
#include "access/ginxlog.h"
#include "access/xloginsert.h"
#include "lib/ilist.h"
#include "miscadmin.h"
#include "storage/predicate.h"
#include "utils/rel.h"
Include dependency graph for gindatapage.c:

Go to the source code of this file.

Data Structures

struct  disassembledLeaf
 
struct  leafSegmentInfo
 

Macros

#define GinPostingListSegmentMaxSize   384
 
#define GinPostingListSegmentTargetSize   256
 
#define GinPostingListSegmentMinSize   128
 
#define MinTuplesPerSegment   ((GinPostingListSegmentMaxSize - 2) / 6)
 

Functions

static ItemPointer dataLeafPageGetUncompressed (Page page, int *nitems)
 
static void dataSplitPageInternal (GinBtree btree, Buffer origbuf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, Page *newlpage, Page *newrpage)
 
static disassembledLeafdisassembleLeaf (Page page)
 
static bool leafRepackItems (disassembledLeaf *leaf, ItemPointer remaining)
 
static bool addItemsToLeaf (disassembledLeaf *leaf, ItemPointer newItems, int nNewItems)
 
static void computeLeafRecompressWALData (disassembledLeaf *leaf)
 
static void dataPlaceToPageLeafRecompress (Buffer buf, disassembledLeaf *leaf)
 
static void dataPlaceToPageLeafSplit (disassembledLeaf *leaf, ItemPointerData lbound, ItemPointerData rbound, Page lpage, Page rpage)
 
ItemPointer GinDataLeafPageGetItems (Page page, int *nitems, ItemPointerData advancePast)
 
int GinDataLeafPageGetItemsToTbm (Page page, TIDBitmap *tbm)
 
static bool dataIsMoveRight (GinBtree btree, Page page)
 
static BlockNumber dataLocateItem (GinBtree btree, GinBtreeStack *stack)
 
static OffsetNumber dataFindChildPtr (GinBtree btree, Page page, BlockNumber blkno, OffsetNumber storedOff)
 
static BlockNumber dataGetLeftMostPage (GinBtree btree, Page page)
 
void GinDataPageAddPostingItem (Page page, PostingItem *data, OffsetNumber offset)
 
void GinPageDeletePostingItem (Page page, OffsetNumber offset)
 
static GinPlaceToPageRC dataBeginPlaceToPageLeaf (GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, void **ptp_workspace, Page *newlpage, Page *newrpage)
 
static void dataExecPlaceToPageLeaf (GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, void *ptp_workspace)
 
void ginVacuumPostingTreeLeaf (Relation indexrel, Buffer buffer, GinVacuumState *gvs)
 
static GinPlaceToPageRC dataBeginPlaceToPageInternal (GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void **ptp_workspace, Page *newlpage, Page *newrpage)
 
static void dataExecPlaceToPageInternal (GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void *ptp_workspace)
 
static GinPlaceToPageRC dataBeginPlaceToPage (GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void **ptp_workspace, Page *newlpage, Page *newrpage)
 
static void dataExecPlaceToPage (GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void *ptp_workspace)
 
static void * dataPrepareDownlink (GinBtree btree, Buffer lbuf)
 
void ginDataFillRoot (GinBtree btree, Page root, BlockNumber lblkno, Page lpage, BlockNumber rblkno, Page rpage)
 
BlockNumber createPostingTree (Relation index, ItemPointerData *items, uint32 nitems, GinStatsData *buildStats, Buffer entrybuffer)
 
static void ginPrepareDataScan (GinBtree btree, Relation index, BlockNumber rootBlkno)
 
void ginInsertItemPointers (Relation index, BlockNumber rootBlkno, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
 
GinBtreeStackginScanBeginPostingTree (GinBtree btree, Relation index, BlockNumber rootBlkno, Snapshot snapshot)
 

Macro Definition Documentation

◆ GinPostingListSegmentMaxSize

#define GinPostingListSegmentMaxSize   384

Definition at line 34 of file gindatapage.c.

Referenced by createPostingTree(), dataBeginPlaceToPageLeaf(), and leafRepackItems().

◆ GinPostingListSegmentMinSize

#define GinPostingListSegmentMinSize   128

Definition at line 36 of file gindatapage.c.

Referenced by leafRepackItems().

◆ GinPostingListSegmentTargetSize

#define GinPostingListSegmentTargetSize   256

Definition at line 35 of file gindatapage.c.

Referenced by addItemsToLeaf(), and leafRepackItems().

◆ MinTuplesPerSegment

#define MinTuplesPerSegment   ((GinPostingListSegmentMaxSize - 2) / 6)

Definition at line 43 of file gindatapage.c.

Referenced by dataBeginPlaceToPageLeaf().

Function Documentation

◆ addItemsToLeaf()

static bool addItemsToLeaf ( disassembledLeaf leaf,
ItemPointer  newItems,
int  nNewItems 
)
static

Definition at line 1438 of file gindatapage.c.

References leafSegmentInfo::action, Assert, cur, dlist_iter::cur, dlist_container, dlist_foreach, dlist_has_next(), dlist_is_empty(), dlist_next_node(), dlist_push_tail(), GinPostingList::first, GIN_SEGMENT_ADDITEMS, GIN_SEGMENT_INSERT, GIN_SEGMENT_REPLACE, GIN_SEGMENT_UNMODIFIED, ginCompareItemPointers(), ginMergeItemPointers(), ginPostingListDecode(), GinPostingListSegmentTargetSize, leafSegmentInfo::items, leafSegmentInfo::modifieditems, next, leafSegmentInfo::nitems, leafSegmentInfo::nmodifieditems, leafSegmentInfo::node, palloc(), leafSegmentInfo::seg, disassembledLeaf::segments, and SizeOfGinPostingList.

Referenced by dataBeginPlaceToPageLeaf().

1439 {
1440  dlist_iter iter;
1441  ItemPointer nextnew = newItems;
1442  int newleft = nNewItems;
1443  bool modified = false;
1444  leafSegmentInfo *newseg;
1445 
1446  /*
1447  * If the page is completely empty, just construct one new segment to hold
1448  * all the new items.
1449  */
1450  if (dlist_is_empty(&leaf->segments))
1451  {
1452  newseg = palloc(sizeof(leafSegmentInfo));
1453  newseg->seg = NULL;
1454  newseg->items = newItems;
1455  newseg->nitems = nNewItems;
1456  newseg->action = GIN_SEGMENT_INSERT;
1457  dlist_push_tail(&leaf->segments, &newseg->node);
1458  return true;
1459  }
1460 
1461  dlist_foreach(iter, &leaf->segments)
1462  {
1464  int nthis;
1465  ItemPointer tmpitems;
1466  int ntmpitems;
1467 
1468  /*
1469  * How many of the new items fall into this segment?
1470  */
1471  if (!dlist_has_next(&leaf->segments, iter.cur))
1472  nthis = newleft;
1473  else
1474  {
1476  ItemPointerData next_first;
1477 
1479  dlist_next_node(&leaf->segments, iter.cur));
1480  if (next->items)
1481  next_first = next->items[0];
1482  else
1483  {
1484  Assert(next->seg != NULL);
1485  next_first = next->seg->first;
1486  }
1487 
1488  nthis = 0;
1489  while (nthis < newleft && ginCompareItemPointers(&nextnew[nthis], &next_first) < 0)
1490  nthis++;
1491  }
1492  if (nthis == 0)
1493  continue;
1494 
1495  /* Merge the new items with the existing items. */
1496  if (!cur->items)
1497  cur->items = ginPostingListDecode(cur->seg, &cur->nitems);
1498 
1499  /*
1500  * Fast path for the important special case that we're appending to
1501  * the end of the page: don't let the last segment on the page grow
1502  * larger than the target, create a new segment before that happens.
1503  */
1504  if (!dlist_has_next(&leaf->segments, iter.cur) &&
1505  ginCompareItemPointers(&cur->items[cur->nitems - 1], &nextnew[0]) < 0 &&
1506  cur->seg != NULL &&
1508  {
1509  newseg = palloc(sizeof(leafSegmentInfo));
1510  newseg->seg = NULL;
1511  newseg->items = nextnew;
1512  newseg->nitems = nthis;
1513  newseg->action = GIN_SEGMENT_INSERT;
1514  dlist_push_tail(&leaf->segments, &newseg->node);
1515  modified = true;
1516  break;
1517  }
1518 
1519  tmpitems = ginMergeItemPointers(cur->items, cur->nitems,
1520  nextnew, nthis,
1521  &ntmpitems);
1522  if (ntmpitems != cur->nitems)
1523  {
1524  /*
1525  * If there are no duplicates, track the added items so that we
1526  * can emit a compact ADDITEMS WAL record later on. (it doesn't
1527  * seem worth re-checking which items were duplicates, if there
1528  * were any)
1529  */
1530  if (ntmpitems == nthis + cur->nitems &&
1532  {
1534  cur->modifieditems = nextnew;
1535  cur->nmodifieditems = nthis;
1536  }
1537  else
1538  cur->action = GIN_SEGMENT_REPLACE;
1539 
1540  cur->items = tmpitems;
1541  cur->nitems = ntmpitems;
1542  cur->seg = NULL;
1543  modified = true;
1544  }
1545 
1546  nextnew += nthis;
1547  newleft -= nthis;
1548  if (newleft == 0)
1549  break;
1550  }
1551 
1552  return modified;
1553 }
GinPostingList * seg
Definition: gindatapage.c:100
static int32 next
Definition: blutils.c:219
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded)
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
#define GinPostingListSegmentTargetSize
Definition: gindatapage.c:35
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
struct cursor * cur
Definition: ecpg.c:28
ItemPointer ginMergeItemPointers(ItemPointerData *a, uint32 na, ItemPointerData *b, uint32 nb, int *nmerged)
#define GIN_SEGMENT_INSERT
Definition: ginxlog.h:93
dlist_head segments
Definition: gindatapage.c:50
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
Definition: ilist.h:421
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
ItemPointerData first
Definition: ginblock.h:338
#define GIN_SEGMENT_REPLACE
Definition: ginxlog.h:94
ItemPointerData * modifieditems
Definition: gindatapage.c:90
#define GIN_SEGMENT_ADDITEMS
Definition: ginxlog.h:95
static bool dlist_has_next(dlist_head *head, dlist_node *node)
Definition: ilist.h:402
dlist_node * cur
Definition: ilist.h:161
#define Assert(condition)
Definition: c.h:745
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
dlist_node node
Definition: gindatapage.c:72
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:484
void * palloc(Size size)
Definition: mcxt.c:950
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
ItemPointer items
Definition: gindatapage.c:101
#define GIN_SEGMENT_UNMODIFIED
Definition: ginxlog.h:91
uint16 nmodifieditems
Definition: gindatapage.c:91

◆ computeLeafRecompressWALData()

static void computeLeafRecompressWALData ( disassembledLeaf leaf)
static

Definition at line 869 of file gindatapage.c.

References leafSegmentInfo::action, generate_unaccent_rules::action, dlist_iter::cur, dlist_container, dlist_foreach, elog, ERROR, GIN_SEGMENT_ADDITEMS, GIN_SEGMENT_DELETE, GIN_SEGMENT_INSERT, GIN_SEGMENT_REPLACE, GIN_SEGMENT_UNMODIFIED, leafSegmentInfo::modifieditems, ginxlogRecompressDataLeaf::nactions, leafSegmentInfo::nmodifieditems, palloc(), leafSegmentInfo::seg, disassembledLeaf::segments, SHORTALIGN, SizeOfGinPostingList, disassembledLeaf::walinfo, and disassembledLeaf::walinfolen.

Referenced by dataBeginPlaceToPageLeaf(), and ginVacuumPostingTreeLeaf().

870 {
871  int nmodified = 0;
872  char *walbufbegin;
873  char *walbufend;
874  dlist_iter iter;
875  int segno;
876  ginxlogRecompressDataLeaf *recompress_xlog;
877 
878  /* Count the modified segments */
879  dlist_foreach(iter, &leaf->segments)
880  {
882  iter.cur);
883 
884  if (seginfo->action != GIN_SEGMENT_UNMODIFIED)
885  nmodified++;
886  }
887 
888  walbufbegin =
890  BLCKSZ + /* max size needed to hold the segment data */
891  nmodified * 2 /* (segno + action) per action */
892  );
893  walbufend = walbufbegin;
894 
895  recompress_xlog = (ginxlogRecompressDataLeaf *) walbufend;
896  walbufend += sizeof(ginxlogRecompressDataLeaf);
897 
898  recompress_xlog->nactions = nmodified;
899 
900  segno = 0;
901  dlist_foreach(iter, &leaf->segments)
902  {
904  iter.cur);
905  int segsize = 0;
906  int datalen;
907  uint8 action = seginfo->action;
908 
909  if (action == GIN_SEGMENT_UNMODIFIED)
910  {
911  segno++;
912  continue;
913  }
914 
915  if (action != GIN_SEGMENT_DELETE)
916  segsize = SizeOfGinPostingList(seginfo->seg);
917 
918  /*
919  * If storing the uncompressed list of added item pointers would take
920  * more space than storing the compressed segment as is, do that
921  * instead.
922  */
923  if (action == GIN_SEGMENT_ADDITEMS &&
924  seginfo->nmodifieditems * sizeof(ItemPointerData) > segsize)
925  {
926  action = GIN_SEGMENT_REPLACE;
927  }
928 
929  *((uint8 *) (walbufend++)) = segno;
930  *(walbufend++) = action;
931 
932  switch (action)
933  {
934  case GIN_SEGMENT_DELETE:
935  datalen = 0;
936  break;
937 
939  datalen = seginfo->nmodifieditems * sizeof(ItemPointerData);
940  memcpy(walbufend, &seginfo->nmodifieditems, sizeof(uint16));
941  memcpy(walbufend + sizeof(uint16), seginfo->modifieditems, datalen);
942  datalen += sizeof(uint16);
943  break;
944 
945  case GIN_SEGMENT_INSERT:
946  case GIN_SEGMENT_REPLACE:
947  datalen = SHORTALIGN(segsize);
948  memcpy(walbufend, seginfo->seg, segsize);
949  break;
950 
951  default:
952  elog(ERROR, "unexpected GIN leaf action %d", action);
953  }
954  walbufend += datalen;
955 
956  if (action != GIN_SEGMENT_INSERT)
957  segno++;
958  }
959 
960  /* Pass back the constructed info via *leaf */
961  leaf->walinfo = walbufbegin;
962  leaf->walinfolen = walbufend - walbufbegin;
963 }
GinPostingList * seg
Definition: gindatapage.c:100
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
unsigned char uint8
Definition: c.h:372
#define GIN_SEGMENT_INSERT
Definition: ginxlog.h:93
dlist_head segments
Definition: gindatapage.c:50
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
unsigned short uint16
Definition: c.h:373
#define GIN_SEGMENT_REPLACE
Definition: ginxlog.h:94
#define ERROR
Definition: elog.h:43
ItemPointerData * modifieditems
Definition: gindatapage.c:90
#define GIN_SEGMENT_ADDITEMS
Definition: ginxlog.h:95
#define GIN_SEGMENT_DELETE
Definition: ginxlog.h:92
dlist_node * cur
Definition: ilist.h:161
struct ItemPointerData ItemPointerData
void * palloc(Size size)
Definition: mcxt.c:950
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
#define elog(elevel,...)
Definition: elog.h:214
#define GIN_SEGMENT_UNMODIFIED
Definition: ginxlog.h:91
#define SHORTALIGN(LEN)
Definition: c.h:694
uint16 nmodifieditems
Definition: gindatapage.c:91

◆ createPostingTree()

BlockNumber createPostingTree ( Relation  index,
ItemPointerData items,
uint32  nitems,
GinStatsData buildStats,
Buffer  entrybuffer 
)

Definition at line 1769 of file gindatapage.c.

References BufferGetBlockNumber(), BufferGetPage, DEBUG2, elog, END_CRIT_SECTION, GIN_COMPRESSED, GIN_DATA, GIN_LEAF, ginCompressPostingList(), GinDataLeafPageGetPostingList, GinDataPageMaxDataSize, GinDataPageSetDataSize, GinInitPage(), ginInsertItemPointers(), GinNewBuffer(), GinPageGetOpaque, GinPostingListSegmentMaxSize, InvalidBlockNumber, MarkBufferDirty(), GinStatsData::nDataPages, PageRestoreTempPage(), PageSetLSN, palloc(), pfree(), PredicateLockPageSplit(), REGBUF_WILL_INIT, RelationNeedsWAL, ginxlogCreatePostingTree::size, SizeOfGinPostingList, START_CRIT_SECTION, UnlockReleaseBuffer(), XLOG_GIN_CREATE_PTREE, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by addItemPointersToLeafTuple(), and buildFreshLeafTuple().

1771 {
1772  BlockNumber blkno;
1773  Buffer buffer;
1774  Page tmppage;
1775  Page page;
1776  Pointer ptr;
1777  int nrootitems;
1778  int rootsize;
1779  bool is_build = (buildStats != NULL);
1780 
1781  /* Construct the new root page in memory first. */
1782  tmppage = (Page) palloc(BLCKSZ);
1783  GinInitPage(tmppage, GIN_DATA | GIN_LEAF | GIN_COMPRESSED, BLCKSZ);
1784  GinPageGetOpaque(tmppage)->rightlink = InvalidBlockNumber;
1785 
1786  /*
1787  * Write as many of the items to the root page as fit. In segments of max
1788  * GinPostingListSegmentMaxSize bytes each.
1789  */
1790  nrootitems = 0;
1791  rootsize = 0;
1792  ptr = (Pointer) GinDataLeafPageGetPostingList(tmppage);
1793  while (nrootitems < nitems)
1794  {
1795  GinPostingList *segment;
1796  int npacked;
1797  int segsize;
1798 
1799  segment = ginCompressPostingList(&items[nrootitems],
1800  nitems - nrootitems,
1802  &npacked);
1803  segsize = SizeOfGinPostingList(segment);
1804  if (rootsize + segsize > GinDataPageMaxDataSize)
1805  break;
1806 
1807  memcpy(ptr, segment, segsize);
1808  ptr += segsize;
1809  rootsize += segsize;
1810  nrootitems += npacked;
1811  pfree(segment);
1812  }
1813  GinDataPageSetDataSize(tmppage, rootsize);
1814 
1815  /*
1816  * All set. Get a new physical page, and copy the in-memory page to it.
1817  */
1818  buffer = GinNewBuffer(index);
1819  page = BufferGetPage(buffer);
1820  blkno = BufferGetBlockNumber(buffer);
1821 
1822  /*
1823  * Copy any predicate locks from the entry tree leaf (containing posting
1824  * list) to the posting tree.
1825  */
1826  PredicateLockPageSplit(index, BufferGetBlockNumber(entrybuffer), blkno);
1827 
1829 
1830  PageRestoreTempPage(tmppage, page);
1831  MarkBufferDirty(buffer);
1832 
1833  if (RelationNeedsWAL(index) && !is_build)
1834  {
1835  XLogRecPtr recptr;
1837 
1838  data.size = rootsize;
1839 
1840  XLogBeginInsert();
1841  XLogRegisterData((char *) &data, sizeof(ginxlogCreatePostingTree));
1842 
1844  rootsize);
1846 
1847  recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_CREATE_PTREE);
1848  PageSetLSN(page, recptr);
1849  }
1850 
1851  UnlockReleaseBuffer(buffer);
1852 
1853  END_CRIT_SECTION();
1854 
1855  /* During index build, count the newly-added data page */
1856  if (buildStats)
1857  buildStats->nDataPages++;
1858 
1859  elog(DEBUG2, "created GIN posting tree with %d items", nrootitems);
1860 
1861  /*
1862  * Add any remaining TIDs to the newly-created posting tree.
1863  */
1864  if (nitems > nrootitems)
1865  {
1866  ginInsertItemPointers(index, blkno,
1867  items + nrootitems,
1868  nitems - nrootitems,
1869  buildStats);
1870  }
1871 
1872  return blkno;
1873 }
GinPostingList * ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize, int *nwritten)
Buffer GinNewBuffer(Relation index)
Definition: ginutil.c:298
#define GinPostingListSegmentMaxSize
Definition: gindatapage.c:34
void ginInsertItemPointers(Relation index, BlockNumber rootBlkno, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gindatapage.c:1902
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:403
void GinInitPage(Page page, uint32 f, Size pageSize)
Definition: ginutil.c:344
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:220
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
#define REGBUF_WILL_INIT
Definition: xloginsert.h:33
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
uint32 BlockNumber
Definition: block.h:31
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
#define GIN_COMPRESSED
Definition: ginblock.h:49
#define GIN_DATA
Definition: ginblock.h:41
#define GinDataPageSetDataSize(page, size)
Definition: ginblock.h:310
void pfree(void *pointer)
Definition: mcxt.c:1057
char * Pointer
Definition: c.h:351
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3534
#define XLOG_GIN_CREATE_PTREE
Definition: ginxlog.h:19
#define DEBUG2
Definition: elog.h:24
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:330
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:422
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:279
BlockNumber nDataPages
Definition: gin.h:47
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidBlockNumber
Definition: block.h:33
#define RelationNeedsWAL(relation)
Definition: rel.h:562
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2661
#define GinDataPageMaxDataSize
Definition: ginblock.h:320
void * palloc(Size size)
Definition: mcxt.c:950
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
#define elog(elevel,...)
Definition: elog.h:214
void XLogBeginInsert(void)
Definition: xloginsert.c:123
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
void PredicateLockPageSplit(Relation relation, BlockNumber oldblkno, BlockNumber newblkno)
Definition: predicate.c:3096
Pointer Page
Definition: bufpage.h:78
#define GIN_LEAF
Definition: ginblock.h:42

◆ dataBeginPlaceToPage()

static GinPlaceToPageRC dataBeginPlaceToPage ( GinBtree  btree,
Buffer  buf,
GinBtreeStack stack,
void *  insertdata,
BlockNumber  updateblkno,
void **  ptp_workspace,
Page newlpage,
Page newrpage 
)
static

Definition at line 1195 of file gindatapage.c.

References Assert, BufferGetPage, dataBeginPlaceToPageInternal(), dataBeginPlaceToPageLeaf(), GinPageIsData, and GinPageIsLeaf.

Referenced by ginPrepareDataScan().

1199 {
1200  Page page = BufferGetPage(buf);
1201 
1202  Assert(GinPageIsData(page));
1203 
1204  if (GinPageIsLeaf(page))
1205  return dataBeginPlaceToPageLeaf(btree, buf, stack, insertdata,
1206  ptp_workspace,
1207  newlpage, newrpage);
1208  else
1209  return dataBeginPlaceToPageInternal(btree, buf, stack,
1210  insertdata, updateblkno,
1211  ptp_workspace,
1212  newlpage, newrpage);
1213 }
static GinPlaceToPageRC dataBeginPlaceToPageInternal(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void **ptp_workspace, Page *newlpage, Page *newrpage)
Definition: gindatapage.c:1116
static char * buf
Definition: pg_test_fsync.c:67
#define GinPageIsLeaf(page)
Definition: ginblock.h:113
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
static GinPlaceToPageRC dataBeginPlaceToPageLeaf(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, void **ptp_workspace, Page *newlpage, Page *newrpage)
Definition: gindatapage.c:448
#define GinPageIsData(page)
Definition: ginblock.h:116
#define Assert(condition)
Definition: c.h:745
Pointer Page
Definition: bufpage.h:78

◆ dataBeginPlaceToPageInternal()

static GinPlaceToPageRC dataBeginPlaceToPageInternal ( GinBtree  btree,
Buffer  buf,
GinBtreeStack stack,
void *  insertdata,
BlockNumber  updateblkno,
void **  ptp_workspace,
Page newlpage,
Page newrpage 
)
static

Definition at line 1116 of file gindatapage.c.

References BufferGetPage, dataSplitPageInternal(), GinNonLeafDataPageGetFreeSpace, GPTP_INSERT, and GPTP_SPLIT.

Referenced by dataBeginPlaceToPage().

1120 {
1121  Page page = BufferGetPage(buf);
1122 
1123  /* If it doesn't fit, deal with split case */
1124  if (GinNonLeafDataPageGetFreeSpace(page) < sizeof(PostingItem))
1125  {
1126  dataSplitPageInternal(btree, buf, stack, insertdata, updateblkno,
1127  newlpage, newrpage);
1128  return GPTP_SPLIT;
1129  }
1130 
1131  /* Else, we're ready to proceed with insertion */
1132  return GPTP_INSERT;
1133 }
static void dataSplitPageInternal(GinBtree btree, Buffer origbuf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, Page *newlpage, Page *newrpage)
Definition: gindatapage.c:1246
static char * buf
Definition: pg_test_fsync.c:67
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define GinNonLeafDataPageGetFreeSpace(page)
Definition: ginblock.h:316
Pointer Page
Definition: bufpage.h:78

◆ dataBeginPlaceToPageLeaf()

static GinPlaceToPageRC dataBeginPlaceToPageLeaf ( GinBtree  btree,
Buffer  buf,
GinBtreeStack stack,
void *  insertdata,
void **  ptp_workspace,
Page newlpage,
Page newrpage 
)
static

Definition at line 448 of file gindatapage.c.

References leafSegmentInfo::action, addItemsToLeaf(), Assert, BufferGetBlockNumber(), BufferGetPage, computeLeafRecompressWALData(), GinBtreeDataLeafInsertData::curitem, dataPlaceToPageLeafSplit(), DEBUG2, disassembleLeaf(), dlist_container, dlist_has_prev(), dlist_is_empty(), dlist_prev_node(), dlist_tail_node(), elog, ERROR, GIN_SEGMENT_DELETE, ginCompareItemPointers(), GinDataLeafPageGetFreeSpace, GinDataPageGetRightBound, GinDataPageMaxDataSize, GinPageIsCompressed, GinPageRightMost, ginPostingListDecode(), GinPostingListSegmentMaxSize, GPTP_INSERT, GPTP_NO_WORK, GPTP_SPLIT, i, GinBtreeData::index, GinBtreeData::isBuild, ItemPointerCompare(), ItemPointerIsValid, ItemPointerSetMin, leafSegmentInfo::items, GinBtreeDataLeafInsertData::items, disassembledLeaf::lastleft, leafRepackItems(), disassembledLeaf::lsize, Min, MinTuplesPerSegment, GinBtreeDataLeafInsertData::nitem, leafSegmentInfo::nitems, palloc(), RelationNeedsWAL, remaining, disassembledLeaf::rsize, leafSegmentInfo::seg, disassembledLeaf::segments, and SizeOfGinPostingList.

Referenced by dataBeginPlaceToPage().

452 {
453  GinBtreeDataLeafInsertData *items = insertdata;
454  ItemPointer newItems = &items->items[items->curitem];
455  int maxitems = items->nitem - items->curitem;
456  Page page = BufferGetPage(buf);
457  int i;
458  ItemPointerData rbound;
459  ItemPointerData lbound;
460  bool needsplit;
461  bool append;
462  int segsize;
463  Size freespace;
464  disassembledLeaf *leaf;
465  leafSegmentInfo *lastleftinfo;
466  ItemPointerData maxOldItem;
468 
469  rbound = *GinDataPageGetRightBound(page);
470 
471  /*
472  * Count how many of the new items belong to this page.
473  */
474  if (!GinPageRightMost(page))
475  {
476  for (i = 0; i < maxitems; i++)
477  {
478  if (ginCompareItemPointers(&newItems[i], &rbound) > 0)
479  {
480  /*
481  * This needs to go to some other location in the tree. (The
482  * caller should've chosen the insert location so that at
483  * least the first item goes here.)
484  */
485  Assert(i > 0);
486  break;
487  }
488  }
489  maxitems = i;
490  }
491 
492  /* Disassemble the data on the page */
493  leaf = disassembleLeaf(page);
494 
495  /*
496  * Are we appending to the end of the page? IOW, are all the new items
497  * larger than any of the existing items.
498  */
499  if (!dlist_is_empty(&leaf->segments))
500  {
501  lastleftinfo = dlist_container(leafSegmentInfo, node,
502  dlist_tail_node(&leaf->segments));
503  if (!lastleftinfo->items)
504  lastleftinfo->items = ginPostingListDecode(lastleftinfo->seg,
505  &lastleftinfo->nitems);
506  maxOldItem = lastleftinfo->items[lastleftinfo->nitems - 1];
507  if (ginCompareItemPointers(&newItems[0], &maxOldItem) >= 0)
508  append = true;
509  else
510  append = false;
511  }
512  else
513  {
514  ItemPointerSetMin(&maxOldItem);
515  append = true;
516  }
517 
518  /*
519  * If we're appending to the end of the page, we will append as many items
520  * as we can fit (after splitting), and stop when the pages becomes full.
521  * Otherwise we have to limit the number of new items to insert, because
522  * once we start packing we can't just stop when we run out of space,
523  * because we must make sure that all the old items still fit.
524  */
525  if (GinPageIsCompressed(page))
526  freespace = GinDataLeafPageGetFreeSpace(page);
527  else
528  freespace = 0;
529  if (append)
530  {
531  /*
532  * Even when appending, trying to append more items than will fit is
533  * not completely free, because we will merge the new items and old
534  * items into an array below. In the best case, every new item fits in
535  * a single byte, and we can use all the free space on the old page as
536  * well as the new page. For simplicity, ignore segment overhead etc.
537  */
538  maxitems = Min(maxitems, freespace + GinDataPageMaxDataSize);
539  }
540  else
541  {
542  /*
543  * Calculate a conservative estimate of how many new items we can fit
544  * on the two pages after splitting.
545  *
546  * We can use any remaining free space on the old page to store full
547  * segments, as well as the new page. Each full-sized segment can hold
548  * at least MinTuplesPerSegment items
549  */
550  int nnewsegments;
551 
552  nnewsegments = freespace / GinPostingListSegmentMaxSize;
554  maxitems = Min(maxitems, nnewsegments * MinTuplesPerSegment);
555  }
556 
557  /* Add the new items to the segment list */
558  if (!addItemsToLeaf(leaf, newItems, maxitems))
559  {
560  /* all items were duplicates, we have nothing to do */
561  items->curitem += maxitems;
562 
563  return GPTP_NO_WORK;
564  }
565 
566  /*
567  * Pack the items back to compressed segments, ready for writing to disk.
568  */
569  needsplit = leafRepackItems(leaf, &remaining);
570 
571  /*
572  * Did all the new items fit?
573  *
574  * If we're appending, it's OK if they didn't. But as a sanity check,
575  * verify that all the old items fit.
576  */
577  if (ItemPointerIsValid(&remaining))
578  {
579  if (!append || ItemPointerCompare(&maxOldItem, &remaining) >= 0)
580  elog(ERROR, "could not split GIN page; all old items didn't fit");
581 
582  /* Count how many of the new items did fit. */
583  for (i = 0; i < maxitems; i++)
584  {
585  if (ginCompareItemPointers(&newItems[i], &remaining) >= 0)
586  break;
587  }
588  if (i == 0)
589  elog(ERROR, "could not split GIN page; no new items fit");
590  maxitems = i;
591  }
592 
593  if (!needsplit)
594  {
595  /*
596  * Great, all the items fit on a single page. If needed, prepare data
597  * for a WAL record describing the changes we'll make.
598  */
599  if (RelationNeedsWAL(btree->index) && !btree->isBuild)
601 
602  /*
603  * We're ready to enter the critical section, but
604  * dataExecPlaceToPageLeaf will need access to the "leaf" data.
605  */
606  *ptp_workspace = leaf;
607 
608  if (append)
609  elog(DEBUG2, "appended %d new items to block %u; %d bytes (%d to go)",
610  maxitems, BufferGetBlockNumber(buf), (int) leaf->lsize,
611  items->nitem - items->curitem - maxitems);
612  else
613  elog(DEBUG2, "inserted %d new items to block %u; %d bytes (%d to go)",
614  maxitems, BufferGetBlockNumber(buf), (int) leaf->lsize,
615  items->nitem - items->curitem - maxitems);
616  }
617  else
618  {
619  /*
620  * Have to split.
621  *
622  * leafRepackItems already divided the segments between the left and
623  * the right page. It filled the left page as full as possible, and
624  * put the rest to the right page. When building a new index, that's
625  * good, because the table is scanned from beginning to end and there
626  * won't be any more insertions to the left page during the build.
627  * This packs the index as tight as possible. But otherwise, split
628  * 50/50, by moving segments from the left page to the right page
629  * until they're balanced.
630  *
631  * As a further heuristic, when appending items to the end of the
632  * page, try to make the left page 75% full, on the assumption that
633  * subsequent insertions will probably also go to the end. This packs
634  * the index somewhat tighter when appending to a table, which is very
635  * common.
636  */
637  if (!btree->isBuild)
638  {
639  while (dlist_has_prev(&leaf->segments, leaf->lastleft))
640  {
641  lastleftinfo = dlist_container(leafSegmentInfo, node, leaf->lastleft);
642 
643  /* ignore deleted segments */
644  if (lastleftinfo->action != GIN_SEGMENT_DELETE)
645  {
646  segsize = SizeOfGinPostingList(lastleftinfo->seg);
647 
648  /*
649  * Note that we check that the right page doesn't become
650  * more full than the left page even when appending. It's
651  * possible that we added enough items to make both pages
652  * more than 75% full.
653  */
654  if ((leaf->lsize - segsize) - (leaf->rsize + segsize) < 0)
655  break;
656  if (append)
657  {
658  if ((leaf->lsize - segsize) < (BLCKSZ * 3) / 4)
659  break;
660  }
661 
662  leaf->lsize -= segsize;
663  leaf->rsize += segsize;
664  }
665  leaf->lastleft = dlist_prev_node(&leaf->segments, leaf->lastleft);
666  }
667  }
670 
671  /*
672  * Fetch the max item in the left page's last segment; it becomes the
673  * right bound of the page.
674  */
675  lastleftinfo = dlist_container(leafSegmentInfo, node, leaf->lastleft);
676  if (!lastleftinfo->items)
677  lastleftinfo->items = ginPostingListDecode(lastleftinfo->seg,
678  &lastleftinfo->nitems);
679  lbound = lastleftinfo->items[lastleftinfo->nitems - 1];
680 
681  /*
682  * Now allocate a couple of temporary page images, and fill them.
683  */
684  *newlpage = palloc(BLCKSZ);
685  *newrpage = palloc(BLCKSZ);
686 
687  dataPlaceToPageLeafSplit(leaf, lbound, rbound,
688  *newlpage, *newrpage);
689 
690  Assert(GinPageRightMost(page) ||
692  GinDataPageGetRightBound(*newrpage)) < 0);
693 
694  if (append)
695  elog(DEBUG2, "appended %d items to block %u; split %d/%d (%d to go)",
696  maxitems, BufferGetBlockNumber(buf), (int) leaf->lsize, (int) leaf->rsize,
697  items->nitem - items->curitem - maxitems);
698  else
699  elog(DEBUG2, "inserted %d items to block %u; split %d/%d (%d to go)",
700  maxitems, BufferGetBlockNumber(buf), (int) leaf->lsize, (int) leaf->rsize,
701  items->nitem - items->curitem - maxitems);
702  }
703 
704  items->curitem += maxitems;
705 
706  return needsplit ? GPTP_SPLIT : GPTP_INSERT;
707 }
int remaining
Definition: informix.c:667
GinPostingList * seg
Definition: gindatapage.c:100
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:52
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:82
static void dataPlaceToPageLeafSplit(disassembledLeaf *leaf, ItemPointerData lbound, ItemPointerData rbound, Page lpage, Page rpage)
Definition: gindatapage.c:1031
#define GinPostingListSegmentMaxSize
Definition: gindatapage.c:34
dlist_node * lastleft
Definition: gindatapage.c:56
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded)
static dlist_node * dlist_tail_node(dlist_head *head)
Definition: ilist.h:466
#define Min(x, y)
Definition: c.h:927
#define MinTuplesPerSegment
Definition: gindatapage.c:43
#define GinPageIsCompressed(page)
Definition: ginblock.h:122
dlist_head segments
Definition: gindatapage.c:50
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
#define ERROR
Definition: elog.h:43
#define GinPageRightMost(page)
Definition: ginblock.h:130
#define DEBUG2
Definition: elog.h:24
static char * buf
Definition: pg_test_fsync.c:67
static dlist_node * dlist_prev_node(dlist_head *head, dlist_node *node)
Definition: ilist.h:431
#define GIN_SEGMENT_DELETE
Definition: ginxlog.h:92
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define ItemPointerSetMin(p)
Definition: ginblock.h:167
ItemPointerData * items
Definition: gin_private.h:193
static bool addItemsToLeaf(disassembledLeaf *leaf, ItemPointer newItems, int nNewItems)
Definition: gindatapage.c:1438
Relation index
Definition: gin_private.h:165
static void computeLeafRecompressWALData(disassembledLeaf *leaf)
Definition: gindatapage.c:869
static bool leafRepackItems(disassembledLeaf *leaf, ItemPointer remaining)
Definition: gindatapage.c:1565
#define Assert(condition)
Definition: c.h:745
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
size_t Size
Definition: c.h:473
#define GinDataLeafPageGetFreeSpace(page)
Definition: ginblock.h:287
static disassembledLeaf * disassembleLeaf(Page page)
Definition: gindatapage.c:1364
#define RelationNeedsWAL(relation)
Definition: rel.h:562
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:484
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2661
#define GinDataPageMaxDataSize
Definition: ginblock.h:320
#define GinDataPageGetRightBound(page)
Definition: ginblock.h:289
void * palloc(Size size)
Definition: mcxt.c:950
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
#define elog(elevel,...)
Definition: elog.h:214
ItemPointer items
Definition: gindatapage.c:101
int i
static bool dlist_has_prev(dlist_head *head, dlist_node *node)
Definition: ilist.h:412
Pointer Page
Definition: bufpage.h:78

◆ dataExecPlaceToPage()

static void dataExecPlaceToPage ( GinBtree  btree,
Buffer  buf,
GinBtreeStack stack,
void *  insertdata,
BlockNumber  updateblkno,
void *  ptp_workspace 
)
static

Definition at line 1225 of file gindatapage.c.

References BufferGetPage, dataExecPlaceToPageInternal(), dataExecPlaceToPageLeaf(), and GinPageIsLeaf.

Referenced by ginPrepareDataScan().

1228 {
1229  Page page = BufferGetPage(buf);
1230 
1231  if (GinPageIsLeaf(page))
1232  dataExecPlaceToPageLeaf(btree, buf, stack, insertdata,
1233  ptp_workspace);
1234  else
1235  dataExecPlaceToPageInternal(btree, buf, stack, insertdata,
1236  updateblkno, ptp_workspace);
1237 }
static void dataExecPlaceToPageInternal(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void *ptp_workspace)
Definition: gindatapage.c:1142
static void dataExecPlaceToPageLeaf(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, void *ptp_workspace)
Definition: gindatapage.c:716
static char * buf
Definition: pg_test_fsync.c:67
#define GinPageIsLeaf(page)
Definition: ginblock.h:113
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
Pointer Page
Definition: bufpage.h:78

◆ dataExecPlaceToPageInternal()

static void dataExecPlaceToPageInternal ( GinBtree  btree,
Buffer  buf,
GinBtreeStack stack,
void *  insertdata,
BlockNumber  updateblkno,
void *  ptp_workspace 
)
static

Definition at line 1142 of file gindatapage.c.

References BufferGetPage, GinDataPageAddPostingItem(), GinDataPageGetPostingItem, GinBtreeData::index, GinBtreeData::isBuild, ginxlogInsertDataInternal::newitem, GinBtreeStack::off, ginxlogInsertDataInternal::offset, PostingItemSetBlockNumber, RelationNeedsWAL, and XLogRegisterBufData().

Referenced by dataExecPlaceToPage().

1145 {
1146  Page page = BufferGetPage(buf);
1147  OffsetNumber off = stack->off;
1148  PostingItem *pitem;
1149 
1150  /* Update existing downlink to point to next page (on internal page) */
1151  pitem = GinDataPageGetPostingItem(page, off);
1152  PostingItemSetBlockNumber(pitem, updateblkno);
1153 
1154  /* Add new item */
1155  pitem = (PostingItem *) insertdata;
1156  GinDataPageAddPostingItem(page, pitem, off);
1157 
1158  if (RelationNeedsWAL(btree->index) && !btree->isBuild)
1159  {
1160  /*
1161  * This must be static, because it has to survive until XLogInsert,
1162  * and we can't palloc here. Ugly, but the XLogInsert infrastructure
1163  * isn't reentrant anyway.
1164  */
1165  static ginxlogInsertDataInternal data;
1166 
1167  data.offset = off;
1168  data.newitem = *pitem;
1169 
1170  XLogRegisterBufData(0, (char *) &data,
1171  sizeof(ginxlogInsertDataInternal));
1172  }
1173 }
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:368
#define PostingItemSetBlockNumber(pointer, blockNumber)
Definition: ginblock.h:193
OffsetNumber off
Definition: gin_private.h:131
OffsetNumber offset
Definition: ginxlog.h:99
uint16 OffsetNumber
Definition: off.h:24
void GinDataPageAddPostingItem(Page page, PostingItem *data, OffsetNumber offset)
Definition: gindatapage.c:380
#define GinDataPageGetPostingItem(page, i)
Definition: ginblock.h:299
static char * buf
Definition: pg_test_fsync.c:67
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
Relation index
Definition: gin_private.h:165
#define RelationNeedsWAL(relation)
Definition: rel.h:562
Pointer Page
Definition: bufpage.h:78

◆ dataExecPlaceToPageLeaf()

static void dataExecPlaceToPageLeaf ( GinBtree  btree,
Buffer  buf,
GinBtreeStack stack,
void *  insertdata,
void *  ptp_workspace 
)
static

Definition at line 716 of file gindatapage.c.

References dataPlaceToPageLeafRecompress(), GinBtreeData::index, GinBtreeData::isBuild, RelationNeedsWAL, disassembledLeaf::walinfo, disassembledLeaf::walinfolen, and XLogRegisterBufData().

Referenced by dataExecPlaceToPage().

718 {
719  disassembledLeaf *leaf = (disassembledLeaf *) ptp_workspace;
720 
721  /* Apply changes to page */
723 
724  /* If needed, register WAL data built by computeLeafRecompressWALData */
725  if (RelationNeedsWAL(btree->index) && !btree->isBuild)
726  {
727  XLogRegisterBufData(0, leaf->walinfo, leaf->walinfolen);
728  }
729 }
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:368
static char * buf
Definition: pg_test_fsync.c:67
static void dataPlaceToPageLeafRecompress(Buffer buf, disassembledLeaf *leaf)
Definition: gindatapage.c:975
Relation index
Definition: gin_private.h:165
#define RelationNeedsWAL(relation)
Definition: rel.h:562

◆ dataFindChildPtr()

static OffsetNumber dataFindChildPtr ( GinBtree  btree,
Page  page,
BlockNumber  blkno,
OffsetNumber  storedOff 
)
static

Definition at line 319 of file gindatapage.c.

References Assert, FirstOffsetNumber, GinDataPageGetPostingItem, GinPageGetOpaque, GinPageIsData, GinPageIsLeaf, i, InvalidOffsetNumber, and PostingItemGetBlockNumber.

Referenced by ginPrepareDataScan().

320 {
321  OffsetNumber i,
322  maxoff = GinPageGetOpaque(page)->maxoff;
323  PostingItem *pitem;
324 
325  Assert(!GinPageIsLeaf(page));
326  Assert(GinPageIsData(page));
327 
328  /* if page isn't changed, we return storedOff */
329  if (storedOff >= FirstOffsetNumber && storedOff <= maxoff)
330  {
331  pitem = GinDataPageGetPostingItem(page, storedOff);
332  if (PostingItemGetBlockNumber(pitem) == blkno)
333  return storedOff;
334 
335  /*
336  * we hope, that needed pointer goes to right. It's true if there
337  * wasn't a deletion
338  */
339  for (i = storedOff + 1; i <= maxoff; i++)
340  {
341  pitem = GinDataPageGetPostingItem(page, i);
342  if (PostingItemGetBlockNumber(pitem) == blkno)
343  return i;
344  }
345 
346  maxoff = storedOff - 1;
347  }
348 
349  /* last chance */
350  for (i = FirstOffsetNumber; i <= maxoff; i++)
351  {
352  pitem = GinDataPageGetPostingItem(page, i);
353  if (PostingItemGetBlockNumber(pitem) == blkno)
354  return i;
355  }
356 
357  return InvalidOffsetNumber;
358 }
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
uint16 OffsetNumber
Definition: off.h:24
#define GinDataPageGetPostingItem(page, i)
Definition: ginblock.h:299
#define FirstOffsetNumber
Definition: off.h:27
#define GinPageIsLeaf(page)
Definition: ginblock.h:113
#define InvalidOffsetNumber
Definition: off.h:26
#define GinPageIsData(page)
Definition: ginblock.h:116
#define PostingItemGetBlockNumber(pointer)
Definition: ginblock.h:190
#define Assert(condition)
Definition: c.h:745
int i

◆ dataGetLeftMostPage()

static BlockNumber dataGetLeftMostPage ( GinBtree  btree,
Page  page 
)
static

Definition at line 364 of file gindatapage.c.

References Assert, FirstOffsetNumber, GinDataPageGetPostingItem, GinPageGetOpaque, GinPageIsData, GinPageIsLeaf, and PostingItemGetBlockNumber.

Referenced by ginPrepareDataScan().

365 {
366  PostingItem *pitem;
367 
368  Assert(!GinPageIsLeaf(page));
369  Assert(GinPageIsData(page));
370  Assert(GinPageGetOpaque(page)->maxoff >= FirstOffsetNumber);
371 
373  return PostingItemGetBlockNumber(pitem);
374 }
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
#define GinDataPageGetPostingItem(page, i)
Definition: ginblock.h:299
#define FirstOffsetNumber
Definition: off.h:27
#define GinPageIsLeaf(page)
Definition: ginblock.h:113
#define GinPageIsData(page)
Definition: ginblock.h:116
#define PostingItemGetBlockNumber(pointer)
Definition: ginblock.h:190
#define Assert(condition)
Definition: c.h:745

◆ dataIsMoveRight()

static bool dataIsMoveRight ( GinBtree  btree,
Page  page 
)
static

Definition at line 234 of file gindatapage.c.

References ginCompareItemPointers(), GinDataPageGetRightBound, GinPageIsDeleted, GinPageRightMost, GinBtreeData::itemptr, and true.

Referenced by ginPrepareDataScan().

235 {
237 
238  if (GinPageRightMost(page))
239  return false;
240 
241  if (GinPageIsDeleted(page))
242  return true;
243 
244  return (ginCompareItemPointers(&btree->itemptr, iptr) > 0) ? true : false;
245 }
#define true
Definition: c.h:328
#define GinPageRightMost(page)
Definition: ginblock.h:130
#define GinPageIsDeleted(page)
Definition: ginblock.h:125
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:484
#define GinDataPageGetRightBound(page)
Definition: ginblock.h:289
ItemPointerData itemptr
Definition: gin_private.h:177

◆ dataLeafPageGetUncompressed()

static ItemPointer dataLeafPageGetUncompressed ( Page  page,
int *  nitems 
)
static

Definition at line 211 of file gindatapage.c.

References Assert, GinDataPageGetData, GinPageGetOpaque, and GinPageIsCompressed.

Referenced by disassembleLeaf(), GinDataLeafPageGetItems(), and GinDataLeafPageGetItemsToTbm().

212 {
213  ItemPointer items;
214 
215  Assert(!GinPageIsCompressed(page));
216 
217  /*
218  * In the old pre-9.4 page format, the whole page content is used for
219  * uncompressed items, and the number of items is stored in 'maxoff'
220  */
221  items = (ItemPointer) GinDataPageGetData(page);
222  *nitems = GinPageGetOpaque(page)->maxoff;
223 
224  return items;
225 }
#define GinDataPageGetData(page)
Definition: ginblock.h:296
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
#define GinPageIsCompressed(page)
Definition: ginblock.h:122
ItemPointerData * ItemPointer
Definition: itemptr.h:49
#define Assert(condition)
Definition: c.h:745

◆ dataLocateItem()

static BlockNumber dataLocateItem ( GinBtree  btree,
GinBtreeStack stack 
)
static

Definition at line 252 of file gindatapage.c.

References Assert, GinBtreeStack::buffer, BufferGetPage, FirstOffsetNumber, GinBtreeData::fullScan, GinBtreeData::getLeftMostChild, ginCompareItemPointers(), GinDataPageGetPostingItem, GinPageGetOpaque, GinPageIsData, GinPageIsLeaf, GinBtreeData::itemptr, PostingItem::key, GinBtreeStack::off, PostingItemGetBlockNumber, and GinBtreeStack::predictNumber.

Referenced by ginPrepareDataScan().

253 {
254  OffsetNumber low,
255  high,
256  maxoff;
257  PostingItem *pitem = NULL;
258  int result;
259  Page page = BufferGetPage(stack->buffer);
260 
261  Assert(!GinPageIsLeaf(page));
262  Assert(GinPageIsData(page));
263 
264  if (btree->fullScan)
265  {
266  stack->off = FirstOffsetNumber;
267  stack->predictNumber *= GinPageGetOpaque(page)->maxoff;
268  return btree->getLeftMostChild(btree, page);
269  }
270 
271  low = FirstOffsetNumber;
272  maxoff = high = GinPageGetOpaque(page)->maxoff;
273  Assert(high >= low);
274 
275  high++;
276 
277  while (high > low)
278  {
279  OffsetNumber mid = low + ((high - low) / 2);
280 
281  pitem = GinDataPageGetPostingItem(page, mid);
282 
283  if (mid == maxoff)
284  {
285  /*
286  * Right infinity, page already correctly chosen with a help of
287  * dataIsMoveRight
288  */
289  result = -1;
290  }
291  else
292  {
293  pitem = GinDataPageGetPostingItem(page, mid);
294  result = ginCompareItemPointers(&btree->itemptr, &(pitem->key));
295  }
296 
297  if (result == 0)
298  {
299  stack->off = mid;
300  return PostingItemGetBlockNumber(pitem);
301  }
302  else if (result > 0)
303  low = mid + 1;
304  else
305  high = mid;
306  }
307 
308  Assert(high >= FirstOffsetNumber && high <= maxoff);
309 
310  stack->off = high;
311  pitem = GinDataPageGetPostingItem(page, high);
312  return PostingItemGetBlockNumber(pitem);
313 }
OffsetNumber off
Definition: gin_private.h:131
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
uint16 OffsetNumber
Definition: off.h:24
#define GinDataPageGetPostingItem(page, i)
Definition: ginblock.h:299
#define FirstOffsetNumber
Definition: off.h:27
BlockNumber(* getLeftMostChild)(GinBtree, Page)
Definition: gin_private.h:152
#define GinPageIsLeaf(page)
Definition: ginblock.h:113
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
ItemPointerData key
Definition: ginblock.h:187
#define GinPageIsData(page)
Definition: ginblock.h:116
#define PostingItemGetBlockNumber(pointer)
Definition: ginblock.h:190
#define Assert(condition)
Definition: c.h:745
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:484
uint32 predictNumber
Definition: gin_private.h:134
ItemPointerData itemptr
Definition: gin_private.h:177
Pointer Page
Definition: bufpage.h:78

◆ dataPlaceToPageLeafRecompress()

static void dataPlaceToPageLeafRecompress ( Buffer  buf,
disassembledLeaf leaf 
)
static

Definition at line 975 of file gindatapage.c.

References leafSegmentInfo::action, Assert, BufferGetPage, dlist_iter::cur, dlist_container, dlist_foreach, GIN_SEGMENT_DELETE, GIN_SEGMENT_UNMODIFIED, GinDataLeafPageGetPostingList, GinDataPageMaxDataSize, GinDataPageSetDataSize, GinPageGetOpaque, GinPageIsCompressed, GinPageSetCompressed, InvalidOffsetNumber, disassembledLeaf::oldformat, leafSegmentInfo::seg, disassembledLeaf::segments, and SizeOfGinPostingList.

Referenced by dataExecPlaceToPageLeaf(), and ginVacuumPostingTreeLeaf().

976 {
977  Page page = BufferGetPage(buf);
978  char *ptr;
979  int newsize;
980  bool modified = false;
981  dlist_iter iter;
982  int segsize;
983 
984  /*
985  * If the page was in pre-9.4 format before, convert the header, and force
986  * all segments to be copied to the page whether they were modified or
987  * not.
988  */
989  if (!GinPageIsCompressed(page))
990  {
991  Assert(leaf->oldformat);
992  GinPageSetCompressed(page);
993  GinPageGetOpaque(page)->maxoff = InvalidOffsetNumber;
994  modified = true;
995  }
996 
997  ptr = (char *) GinDataLeafPageGetPostingList(page);
998  newsize = 0;
999  dlist_foreach(iter, &leaf->segments)
1000  {
1001  leafSegmentInfo *seginfo = dlist_container(leafSegmentInfo, node, iter.cur);
1002 
1003  if (seginfo->action != GIN_SEGMENT_UNMODIFIED)
1004  modified = true;
1005 
1006  if (seginfo->action != GIN_SEGMENT_DELETE)
1007  {
1008  segsize = SizeOfGinPostingList(seginfo->seg);
1009 
1010  if (modified)
1011  memcpy(ptr, seginfo->seg, segsize);
1012 
1013  ptr += segsize;
1014  newsize += segsize;
1015  }
1016  }
1017 
1018  Assert(newsize <= GinDataPageMaxDataSize);
1019  GinDataPageSetDataSize(page, newsize);
1020 }
GinPostingList * seg
Definition: gindatapage.c:100
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
#define GinPageIsCompressed(page)
Definition: ginblock.h:122
dlist_head segments
Definition: gindatapage.c:50
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
#define GinDataPageSetDataSize(page, size)
Definition: ginblock.h:310
#define GinPageSetCompressed(page)
Definition: ginblock.h:123
static char * buf
Definition: pg_test_fsync.c:67
#define GIN_SEGMENT_DELETE
Definition: ginxlog.h:92
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:279
dlist_node * cur
Definition: ilist.h:161
#define InvalidOffsetNumber
Definition: off.h:26
#define Assert(condition)
Definition: c.h:745
#define GinDataPageMaxDataSize
Definition: ginblock.h:320
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
#define GIN_SEGMENT_UNMODIFIED
Definition: ginxlog.h:91
Pointer Page
Definition: bufpage.h:78

◆ dataPlaceToPageLeafSplit()

static void dataPlaceToPageLeafSplit ( disassembledLeaf leaf,
ItemPointerData  lbound,
ItemPointerData  rbound,
Page  lpage,
Page  rpage 
)
static

Definition at line 1031 of file gindatapage.c.

References leafSegmentInfo::action, Assert, dlist_container, dlist_has_next(), dlist_head_node(), dlist_next_node(), GIN_COMPRESSED, GIN_DATA, GIN_LEAF, GIN_SEGMENT_DELETE, GinDataLeafPageGetPostingList, GinDataPageGetRightBound, GinDataPageSetDataSize, GinInitPage(), disassembledLeaf::lastleft, disassembledLeaf::lsize, disassembledLeaf::rsize, leafSegmentInfo::seg, disassembledLeaf::segments, and SizeOfGinPostingList.

Referenced by dataBeginPlaceToPageLeaf().

1034 {
1035  char *ptr;
1036  int segsize;
1037  int lsize;
1038  int rsize;
1039  dlist_node *node;
1040  dlist_node *firstright;
1041  leafSegmentInfo *seginfo;
1042 
1043  /* Initialize temporary pages to hold the new left and right pages */
1044  GinInitPage(lpage, GIN_DATA | GIN_LEAF | GIN_COMPRESSED, BLCKSZ);
1045  GinInitPage(rpage, GIN_DATA | GIN_LEAF | GIN_COMPRESSED, BLCKSZ);
1046 
1047  /*
1048  * Copy the segments that go to the left page.
1049  *
1050  * XXX: We should skip copying the unmodified part of the left page, like
1051  * we do when recompressing.
1052  */
1053  lsize = 0;
1054  ptr = (char *) GinDataLeafPageGetPostingList(lpage);
1055  firstright = dlist_next_node(&leaf->segments, leaf->lastleft);
1056  for (node = dlist_head_node(&leaf->segments);
1057  node != firstright;
1058  node = dlist_next_node(&leaf->segments, node))
1059  {
1060  seginfo = dlist_container(leafSegmentInfo, node, node);
1061 
1062  if (seginfo->action != GIN_SEGMENT_DELETE)
1063  {
1064  segsize = SizeOfGinPostingList(seginfo->seg);
1065  memcpy(ptr, seginfo->seg, segsize);
1066  ptr += segsize;
1067  lsize += segsize;
1068  }
1069  }
1070  Assert(lsize == leaf->lsize);
1071  GinDataPageSetDataSize(lpage, lsize);
1072  *GinDataPageGetRightBound(lpage) = lbound;
1073 
1074  /* Copy the segments that go to the right page */
1075  ptr = (char *) GinDataLeafPageGetPostingList(rpage);
1076  rsize = 0;
1077  for (node = firstright;
1078  ;
1079  node = dlist_next_node(&leaf->segments, node))
1080  {
1081  seginfo = dlist_container(leafSegmentInfo, node, node);
1082 
1083  if (seginfo->action != GIN_SEGMENT_DELETE)
1084  {
1085  segsize = SizeOfGinPostingList(seginfo->seg);
1086  memcpy(ptr, seginfo->seg, segsize);
1087  ptr += segsize;
1088  rsize += segsize;
1089  }
1090 
1091  if (!dlist_has_next(&leaf->segments, node))
1092  break;
1093  }
1094  Assert(rsize == leaf->rsize);
1095  GinDataPageSetDataSize(rpage, rsize);
1096  *GinDataPageGetRightBound(rpage) = rbound;
1097 }
GinPostingList * seg
Definition: gindatapage.c:100
dlist_node * lastleft
Definition: gindatapage.c:56
void GinInitPage(Page page, uint32 f, Size pageSize)
Definition: ginutil.c:344
#define GIN_COMPRESSED
Definition: ginblock.h:49
#define GIN_DATA
Definition: ginblock.h:41
dlist_head segments
Definition: gindatapage.c:50
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
Definition: ilist.h:421
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
#define GinDataPageSetDataSize(page, size)
Definition: ginblock.h:310
#define GIN_SEGMENT_DELETE
Definition: ginxlog.h:92
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:279
static bool dlist_has_next(dlist_head *head, dlist_node *node)
Definition: ilist.h:402
static dlist_node * dlist_head_node(dlist_head *head)
Definition: ilist.h:449
#define Assert(condition)
Definition: c.h:745
#define GinDataPageGetRightBound(page)
Definition: ginblock.h:289
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
#define GIN_LEAF
Definition: ginblock.h:42

◆ dataPrepareDownlink()

static void* dataPrepareDownlink ( GinBtree  btree,
Buffer  lbuf 
)
static

Definition at line 1327 of file gindatapage.c.

References BufferGetBlockNumber(), BufferGetPage, GinDataPageGetRightBound, PostingItem::key, palloc(), and PostingItemSetBlockNumber.

Referenced by ginPrepareDataScan().

1328 {
1329  PostingItem *pitem = palloc(sizeof(PostingItem));
1330  Page lpage = BufferGetPage(lbuf);
1331 
1333  pitem->key = *GinDataPageGetRightBound(lpage);
1334 
1335  return pitem;
1336 }
#define PostingItemSetBlockNumber(pointer, blockNumber)
Definition: ginblock.h:193
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
ItemPointerData key
Definition: ginblock.h:187
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2661
#define GinDataPageGetRightBound(page)
Definition: ginblock.h:289
void * palloc(Size size)
Definition: mcxt.c:950
Pointer Page
Definition: bufpage.h:78

◆ dataSplitPageInternal()

static void dataSplitPageInternal ( GinBtree  btree,
Buffer  origbuf,
GinBtreeStack stack,
void *  insertdata,
BlockNumber  updateblkno,
Page newlpage,
Page newrpage 
)
static

Definition at line 1246 of file gindatapage.c.

References BufferGetPage, FirstOffsetNumber, GinDataPageGetPostingItem, GinDataPageGetRightBound, GinDataPageSetDataSize, GinInitPage(), GinNonLeafDataPageGetFreeSpace, GinPageGetOpaque, GinPageRightMost, GinBtreeData::isBuild, GinBtreeStack::off, PageGetPageSize, PageGetTempPage(), and PostingItemSetBlockNumber.

Referenced by dataBeginPlaceToPageInternal().

1250 {
1251  Page oldpage = BufferGetPage(origbuf);
1252  OffsetNumber off = stack->off;
1253  int nitems = GinPageGetOpaque(oldpage)->maxoff;
1254  int nleftitems;
1255  int nrightitems;
1256  Size pageSize = PageGetPageSize(oldpage);
1257  ItemPointerData oldbound = *GinDataPageGetRightBound(oldpage);
1258  ItemPointer bound;
1259  Page lpage;
1260  Page rpage;
1262  PostingItem allitems[(BLCKSZ / sizeof(PostingItem)) + 1];
1263 
1264  lpage = PageGetTempPage(oldpage);
1265  rpage = PageGetTempPage(oldpage);
1266  GinInitPage(lpage, GinPageGetOpaque(oldpage)->flags, pageSize);
1267  GinInitPage(rpage, GinPageGetOpaque(oldpage)->flags, pageSize);
1268 
1269  /*
1270  * First construct a new list of PostingItems, which includes all the old
1271  * items, and the new item.
1272  */
1273  memcpy(allitems, GinDataPageGetPostingItem(oldpage, FirstOffsetNumber),
1274  (off - 1) * sizeof(PostingItem));
1275 
1276  allitems[off - 1] = *((PostingItem *) insertdata);
1277  memcpy(&allitems[off], GinDataPageGetPostingItem(oldpage, off),
1278  (nitems - (off - 1)) * sizeof(PostingItem));
1279  nitems++;
1280 
1281  /* Update existing downlink to point to next page */
1282  PostingItemSetBlockNumber(&allitems[off], updateblkno);
1283 
1284  /*
1285  * When creating a new index, fit as many tuples as possible on the left
1286  * page, on the assumption that the table is scanned from beginning to
1287  * end. This packs the index as tight as possible.
1288  */
1289  if (btree->isBuild && GinPageRightMost(oldpage))
1291  else
1292  separator = nitems / 2;
1293  nleftitems = separator;
1294  nrightitems = nitems - separator;
1295 
1297  allitems,
1298  nleftitems * sizeof(PostingItem));
1299  GinPageGetOpaque(lpage)->maxoff = nleftitems;
1301  &allitems[separator],
1302  nrightitems * sizeof(PostingItem));
1303  GinPageGetOpaque(rpage)->maxoff = nrightitems;
1304 
1305  /*
1306  * Also set pd_lower for both pages, like GinDataPageAddPostingItem does.
1307  */
1308  GinDataPageSetDataSize(lpage, nleftitems * sizeof(PostingItem));
1309  GinDataPageSetDataSize(rpage, nrightitems * sizeof(PostingItem));
1310 
1311  /* set up right bound for left page */
1312  bound = GinDataPageGetRightBound(lpage);
1313  *bound = GinDataPageGetPostingItem(lpage, nleftitems)->key;
1314 
1315  /* set up right bound for right page */
1316  *GinDataPageGetRightBound(rpage) = oldbound;
1317 
1318  /* return temp pages to caller */
1319  *newlpage = lpage;
1320  *newrpage = rpage;
1321 }
#define PostingItemSetBlockNumber(pointer, blockNumber)
Definition: ginblock.h:193
void GinInitPage(Page page, uint32 f, Size pageSize)
Definition: ginutil.c:344
OffsetNumber off
Definition: gin_private.h:131
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
uint16 OffsetNumber
Definition: off.h:24
#define GinDataPageSetDataSize(page, size)
Definition: ginblock.h:310
#define GinPageRightMost(page)
Definition: ginblock.h:130
#define GinDataPageGetPostingItem(page, i)
Definition: ginblock.h:299
#define FirstOffsetNumber
Definition: off.h:27
#define PageGetPageSize(page)
Definition: bufpage.h:268
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
Page PageGetTempPage(Page page)
Definition: bufpage.c:344
size_t Size
Definition: c.h:473
#define GinDataPageGetRightBound(page)
Definition: ginblock.h:289
#define GinNonLeafDataPageGetFreeSpace(page)
Definition: ginblock.h:316
Pointer Page
Definition: bufpage.h:78

◆ disassembleLeaf()

static disassembledLeaf * disassembleLeaf ( Page  page)
static

Definition at line 1364 of file gindatapage.c.

References leafSegmentInfo::action, dataLeafPageGetUncompressed(), dlist_init(), dlist_push_tail(), GIN_SEGMENT_REPLACE, GIN_SEGMENT_UNMODIFIED, GinDataLeafPageGetPostingList, GinDataLeafPageGetPostingListSize, GinNextPostingListSegment, GinPageIsCompressed, leafSegmentInfo::items, leafSegmentInfo::nitems, leafSegmentInfo::node, disassembledLeaf::oldformat, palloc(), palloc0(), leafSegmentInfo::seg, and disassembledLeaf::segments.

Referenced by dataBeginPlaceToPageLeaf(), and ginVacuumPostingTreeLeaf().

1365 {
1366  disassembledLeaf *leaf;
1367  GinPostingList *seg;
1368  Pointer segbegin;
1369  Pointer segend;
1370 
1371  leaf = palloc0(sizeof(disassembledLeaf));
1372  dlist_init(&leaf->segments);
1373 
1374  if (GinPageIsCompressed(page))
1375  {
1376  /*
1377  * Create a leafSegmentInfo entry for each segment.
1378  */
1379  seg = GinDataLeafPageGetPostingList(page);
1380  segbegin = (Pointer) seg;
1381  segend = segbegin + GinDataLeafPageGetPostingListSize(page);
1382  while ((Pointer) seg < segend)
1383  {
1384  leafSegmentInfo *seginfo = palloc(sizeof(leafSegmentInfo));
1385 
1386  seginfo->action = GIN_SEGMENT_UNMODIFIED;
1387  seginfo->seg = seg;
1388  seginfo->items = NULL;
1389  seginfo->nitems = 0;
1390  dlist_push_tail(&leaf->segments, &seginfo->node);
1391 
1392  seg = GinNextPostingListSegment(seg);
1393  }
1394  leaf->oldformat = false;
1395  }
1396  else
1397  {
1398  /*
1399  * A pre-9.4 format uncompressed page is represented by a single
1400  * segment, with an array of items. The corner case is uncompressed
1401  * page containing no items, which is represented as no segments.
1402  */
1403  ItemPointer uncompressed;
1404  int nuncompressed;
1405  leafSegmentInfo *seginfo;
1406 
1407  uncompressed = dataLeafPageGetUncompressed(page, &nuncompressed);
1408 
1409  if (nuncompressed > 0)
1410  {
1411  seginfo = palloc(sizeof(leafSegmentInfo));
1412 
1413  seginfo->action = GIN_SEGMENT_REPLACE;
1414  seginfo->seg = NULL;
1415  seginfo->items = palloc(nuncompressed * sizeof(ItemPointerData));
1416  memcpy(seginfo->items, uncompressed, nuncompressed * sizeof(ItemPointerData));
1417  seginfo->nitems = nuncompressed;
1418 
1419  dlist_push_tail(&leaf->segments, &seginfo->node);
1420  }
1421 
1422  leaf->oldformat = true;
1423  }
1424 
1425  return leaf;
1426 }
GinPostingList * seg
Definition: gindatapage.c:100
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
#define GinPageIsCompressed(page)
Definition: ginblock.h:122
dlist_head segments
Definition: gindatapage.c:50
char * Pointer
Definition: c.h:351
#define GIN_SEGMENT_REPLACE
Definition: ginxlog.h:94
#define GinNextPostingListSegment(cur)
Definition: ginblock.h:344
#define GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:281
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:279
void * palloc0(Size size)
Definition: mcxt.c:981
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
dlist_node node
Definition: gindatapage.c:72
void * palloc(Size size)
Definition: mcxt.c:950
ItemPointer items
Definition: gindatapage.c:101
#define GIN_SEGMENT_UNMODIFIED
Definition: ginxlog.h:91
static ItemPointer dataLeafPageGetUncompressed(Page page, int *nitems)
Definition: gindatapage.c:211

◆ ginDataFillRoot()

void ginDataFillRoot ( GinBtree  btree,
Page  root,
BlockNumber  lblkno,
Page  lpage,
BlockNumber  rblkno,
Page  rpage 
)

Definition at line 1343 of file gindatapage.c.

References GinDataPageAddPostingItem(), GinDataPageGetRightBound, InvalidOffsetNumber, PostingItem::key, and PostingItemSetBlockNumber.

Referenced by ginPrepareDataScan().

1344 {
1345  PostingItem li,
1346  ri;
1347 
1348  li.key = *GinDataPageGetRightBound(lpage);
1349  PostingItemSetBlockNumber(&li, lblkno);
1351 
1352  ri.key = *GinDataPageGetRightBound(rpage);
1353  PostingItemSetBlockNumber(&ri, rblkno);
1355 }
#define PostingItemSetBlockNumber(pointer, blockNumber)
Definition: ginblock.h:193
void GinDataPageAddPostingItem(Page page, PostingItem *data, OffsetNumber offset)
Definition: gindatapage.c:380
ItemPointerData key
Definition: ginblock.h:187
#define InvalidOffsetNumber
Definition: off.h:26
#define GinDataPageGetRightBound(page)
Definition: ginblock.h:289

◆ GinDataLeafPageGetItems()

ItemPointer GinDataLeafPageGetItems ( Page  page,
int *  nitems,
ItemPointerData  advancePast 
)

Definition at line 135 of file gindatapage.c.

References dataLeafPageGetUncompressed(), GinPostingList::first, ginCompareItemPointers(), GinDataLeafPageGetPostingList, GinDataLeafPageGetPostingListSize, GinNextPostingListSegment, GinPageIsCompressed, ginPostingListDecodeAllSegments(), ItemPointerIsValid, next, and palloc().

Referenced by entryLoadMoreItems(), and startScanEntry().

136 {
137  ItemPointer result;
138 
139  if (GinPageIsCompressed(page))
140  {
143  Pointer endptr = ((Pointer) seg) + len;
145 
146  /* Skip to the segment containing advancePast+1 */
147  if (ItemPointerIsValid(&advancePast))
148  {
149  next = GinNextPostingListSegment(seg);
150  while ((Pointer) next < endptr &&
151  ginCompareItemPointers(&next->first, &advancePast) <= 0)
152  {
153  seg = next;
154  next = GinNextPostingListSegment(seg);
155  }
156  len = endptr - (Pointer) seg;
157  }
158 
159  if (len > 0)
160  result = ginPostingListDecodeAllSegments(seg, len, nitems);
161  else
162  {
163  result = NULL;
164  *nitems = 0;
165  }
166  }
167  else
168  {
169  ItemPointer tmp = dataLeafPageGetUncompressed(page, nitems);
170 
171  result = palloc((*nitems) * sizeof(ItemPointerData));
172  memcpy(result, tmp, (*nitems) * sizeof(ItemPointerData));
173  }
174 
175  return result;
176 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:82
static int32 next
Definition: blutils.c:219
#define GinPageIsCompressed(page)
Definition: ginblock.h:122
ItemPointerData first
Definition: ginblock.h:338
char * Pointer
Definition: c.h:351
#define GinNextPostingListSegment(cur)
Definition: ginblock.h:344
#define GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:281
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:279
size_t Size
Definition: c.h:473
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:484
void * palloc(Size size)
Definition: mcxt.c:950
ItemPointer ginPostingListDecodeAllSegments(GinPostingList *segment, int len, int *ndecoded_out)
static ItemPointer dataLeafPageGetUncompressed(Page page, int *nitems)
Definition: gindatapage.c:211

◆ GinDataLeafPageGetItemsToTbm()

int GinDataLeafPageGetItemsToTbm ( Page  page,
TIDBitmap tbm 
)

Definition at line 182 of file gindatapage.c.

References dataLeafPageGetUncompressed(), GinDataLeafPageGetPostingList, GinDataLeafPageGetPostingListSize, GinPageIsCompressed, ginPostingListDecodeAllSegmentsToTbm(), and tbm_add_tuples().

Referenced by scanPostingTree().

183 {
184  ItemPointer uncompressed;
185  int nitems;
186 
187  if (GinPageIsCompressed(page))
188  {
191 
192  nitems = ginPostingListDecodeAllSegmentsToTbm(segment, len, tbm);
193  }
194  else
195  {
196  uncompressed = dataLeafPageGetUncompressed(page, &nitems);
197 
198  if (nitems > 0)
199  tbm_add_tuples(tbm, uncompressed, nitems, false);
200  }
201 
202  return nitems;
203 }
int ginPostingListDecodeAllSegmentsToTbm(GinPostingList *ptr, int len, TIDBitmap *tbm)
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:376
#define GinPageIsCompressed(page)
Definition: ginblock.h:122
#define GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:281
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:279
size_t Size
Definition: c.h:473
static ItemPointer dataLeafPageGetUncompressed(Page page, int *nitems)
Definition: gindatapage.c:211

◆ GinDataPageAddPostingItem()

void GinDataPageAddPostingItem ( Page  page,
PostingItem data,
OffsetNumber  offset 
)

Definition at line 380 of file gindatapage.c.

References Assert, GinDataPageGetPostingItem, GinDataPageSetDataSize, GinPageGetOpaque, GinPageIsLeaf, InvalidBlockNumber, InvalidOffsetNumber, and PostingItemGetBlockNumber.

Referenced by dataExecPlaceToPageInternal(), ginDataFillRoot(), and ginRedoInsertData().

381 {
382  OffsetNumber maxoff = GinPageGetOpaque(page)->maxoff;
383  char *ptr;
384 
386  Assert(!GinPageIsLeaf(page));
387 
388  if (offset == InvalidOffsetNumber)
389  {
390  ptr = (char *) GinDataPageGetPostingItem(page, maxoff + 1);
391  }
392  else
393  {
394  ptr = (char *) GinDataPageGetPostingItem(page, offset);
395  if (offset != maxoff + 1)
396  memmove(ptr + sizeof(PostingItem),
397  ptr,
398  (maxoff - offset + 1) * sizeof(PostingItem));
399  }
400  memcpy(ptr, data, sizeof(PostingItem));
401 
402  maxoff++;
403  GinPageGetOpaque(page)->maxoff = maxoff;
404 
405  /*
406  * Also set pd_lower to the end of the posting items, to follow the
407  * "standard" page layout, so that we can squeeze out the unused space
408  * from full-page images.
409  */
410  GinDataPageSetDataSize(page, maxoff * sizeof(PostingItem));
411 }
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
uint16 OffsetNumber
Definition: off.h:24
#define GinDataPageSetDataSize(page, size)
Definition: ginblock.h:310
#define GinDataPageGetPostingItem(page, i)
Definition: ginblock.h:299
#define GinPageIsLeaf(page)
Definition: ginblock.h:113
#define InvalidOffsetNumber
Definition: off.h:26
#define PostingItemGetBlockNumber(pointer)
Definition: ginblock.h:190
#define Assert(condition)
Definition: c.h:745
#define InvalidBlockNumber
Definition: block.h:33

◆ ginInsertItemPointers()

void ginInsertItemPointers ( Relation  index,
BlockNumber  rootBlkno,
ItemPointerData items,
uint32  nitem,
GinStatsData buildStats 
)

Definition at line 1902 of file gindatapage.c.

References GinBtreeDataLeafInsertData::curitem, ginFindLeafPage(), ginInsertValue(), ginPrepareDataScan(), GinBtreeData::isBuild, GinBtreeData::itemptr, GinBtreeDataLeafInsertData::items, and GinBtreeDataLeafInsertData::nitem.

Referenced by addItemPointersToLeafTuple(), createPostingTree(), and ginEntryInsert().

1905 {
1906  GinBtreeData btree;
1907  GinBtreeDataLeafInsertData insertdata;
1908  GinBtreeStack *stack;
1909 
1910  ginPrepareDataScan(&btree, index, rootBlkno);
1911  btree.isBuild = (buildStats != NULL);
1912  insertdata.items = items;
1913  insertdata.nitem = nitem;
1914  insertdata.curitem = 0;
1915 
1916  while (insertdata.curitem < insertdata.nitem)
1917  {
1918  /* search for the leaf page where the first item should go to */
1919  btree.itemptr = insertdata.items[insertdata.curitem];
1920  stack = ginFindLeafPage(&btree, false, true, NULL);
1921 
1922  ginInsertValue(&btree, stack, &insertdata, buildStats);
1923  }
1924 }
GinBtreeStack * ginFindLeafPage(GinBtree btree, bool searchMode, bool rootConflictCheck, Snapshot snapshot)
Definition: ginbtree.c:80
static void ginPrepareDataScan(GinBtree btree, Relation index, BlockNumber rootBlkno)
Definition: gindatapage.c:1876
ItemPointerData * items
Definition: gin_private.h:193
void ginInsertValue(GinBtree btree, GinBtreeStack *stack, void *insertdata, GinStatsData *buildStats)
Definition: ginbtree.c:776
ItemPointerData itemptr
Definition: gin_private.h:177

◆ GinPageDeletePostingItem()

void GinPageDeletePostingItem ( Page  page,
OffsetNumber  offset 
)

Definition at line 417 of file gindatapage.c.

References Assert, FirstOffsetNumber, GinDataPageGetPostingItem, GinDataPageSetDataSize, GinPageGetOpaque, and GinPageIsLeaf.

Referenced by ginDeletePage(), and ginRedoDeletePage().

418 {
419  OffsetNumber maxoff = GinPageGetOpaque(page)->maxoff;
420 
421  Assert(!GinPageIsLeaf(page));
422  Assert(offset >= FirstOffsetNumber && offset <= maxoff);
423 
424  if (offset != maxoff)
425  memmove(GinDataPageGetPostingItem(page, offset),
426  GinDataPageGetPostingItem(page, offset + 1),
427  sizeof(PostingItem) * (maxoff - offset));
428 
429  maxoff--;
430  GinPageGetOpaque(page)->maxoff = maxoff;
431 
432  GinDataPageSetDataSize(page, maxoff * sizeof(PostingItem));
433 }
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
uint16 OffsetNumber
Definition: off.h:24
#define GinDataPageSetDataSize(page, size)
Definition: ginblock.h:310
#define GinDataPageGetPostingItem(page, i)
Definition: ginblock.h:299
#define FirstOffsetNumber
Definition: off.h:27
#define GinPageIsLeaf(page)
Definition: ginblock.h:113
#define Assert(condition)
Definition: c.h:745

◆ ginPrepareDataScan()

static void ginPrepareDataScan ( GinBtree  btree,
Relation  index,
BlockNumber  rootBlkno 
)
static

Definition at line 1876 of file gindatapage.c.

References GinBtreeData::beginPlaceToPage, dataBeginPlaceToPage(), dataExecPlaceToPage(), dataFindChildPtr(), dataGetLeftMostPage(), dataIsMoveRight(), dataLocateItem(), dataPrepareDownlink(), GinBtreeData::execPlaceToPage, GinBtreeData::fillRoot, GinBtreeData::findChildPage, GinBtreeData::findChildPtr, GinBtreeData::findItem, GinBtreeData::fullScan, GinBtreeData::getLeftMostChild, ginDataFillRoot(), GinBtreeData::index, GinBtreeData::isBuild, GinBtreeData::isData, GinBtreeData::isMoveRight, GinBtreeData::prepareDownlink, and GinBtreeData::rootBlkno.

Referenced by ginInsertItemPointers(), and ginScanBeginPostingTree().

1877 {
1878  memset(btree, 0, sizeof(GinBtreeData));
1879 
1880  btree->index = index;
1881  btree->rootBlkno = rootBlkno;
1882 
1883  btree->findChildPage = dataLocateItem;
1885  btree->isMoveRight = dataIsMoveRight;
1886  btree->findItem = NULL;
1887  btree->findChildPtr = dataFindChildPtr;
1890  btree->fillRoot = ginDataFillRoot;
1892 
1893  btree->isData = true;
1894  btree->fullScan = false;
1895  btree->isBuild = false;
1896 }
void(* fillRoot)(GinBtree, Page, BlockNumber, Page, BlockNumber, Page)
Definition: gin_private.h:161
bool(* isMoveRight)(GinBtree, Page)
Definition: gin_private.h:153
static BlockNumber dataLocateItem(GinBtree btree, GinBtreeStack *stack)
Definition: gindatapage.c:252
BlockNumber rootBlkno
Definition: gin_private.h:166
bool(* findItem)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:154
static GinPlaceToPageRC dataBeginPlaceToPage(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void **ptp_workspace, Page *newlpage, Page *newrpage)
Definition: gindatapage.c:1195
GinPlaceToPageRC(* beginPlaceToPage)(GinBtree, Buffer, GinBtreeStack *, void *, BlockNumber, void **, Page *, Page *)
Definition: gin_private.h:158
static bool dataIsMoveRight(GinBtree btree, Page page)
Definition: gindatapage.c:234
void(* execPlaceToPage)(GinBtree, Buffer, GinBtreeStack *, void *, BlockNumber, void *)
Definition: gin_private.h:159
BlockNumber(* findChildPage)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:151
static OffsetNumber dataFindChildPtr(GinBtree btree, Page page, BlockNumber blkno, OffsetNumber storedOff)
Definition: gindatapage.c:319
BlockNumber(* getLeftMostChild)(GinBtree, Page)
Definition: gin_private.h:152
Relation index
Definition: gin_private.h:165
void *(* prepareDownlink)(GinBtree, Buffer)
Definition: gin_private.h:160
void ginDataFillRoot(GinBtree btree, Page root, BlockNumber lblkno, Page lpage, BlockNumber rblkno, Page rpage)
Definition: gindatapage.c:1343
static void * dataPrepareDownlink(GinBtree btree, Buffer lbuf)
Definition: gindatapage.c:1327
OffsetNumber(* findChildPtr)(GinBtree, Page, BlockNumber, OffsetNumber)
Definition: gin_private.h:157
static void dataExecPlaceToPage(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void *ptp_workspace)
Definition: gindatapage.c:1225
static BlockNumber dataGetLeftMostPage(GinBtree btree, Page page)
Definition: gindatapage.c:364

◆ ginScanBeginPostingTree()

GinBtreeStack* ginScanBeginPostingTree ( GinBtree  btree,
Relation  index,
BlockNumber  rootBlkno,
Snapshot  snapshot 
)

Definition at line 1930 of file gindatapage.c.

References GinBtreeData::fullScan, ginFindLeafPage(), and ginPrepareDataScan().

Referenced by scanPostingTree(), and startScanEntry().

1932 {
1933  GinBtreeStack *stack;
1934 
1935  ginPrepareDataScan(btree, index, rootBlkno);
1936 
1937  btree->fullScan = true;
1938 
1939  stack = ginFindLeafPage(btree, true, false, snapshot);
1940 
1941  return stack;
1942 }
GinBtreeStack * ginFindLeafPage(GinBtree btree, bool searchMode, bool rootConflictCheck, Snapshot snapshot)
Definition: ginbtree.c:80
static void ginPrepareDataScan(GinBtree btree, Relation index, BlockNumber rootBlkno)
Definition: gindatapage.c:1876

◆ ginVacuumPostingTreeLeaf()

void ginVacuumPostingTreeLeaf ( Relation  indexrel,
Buffer  buffer,
GinVacuumState gvs 
)

Definition at line 735 of file gindatapage.c.

References leafSegmentInfo::action, BufferGetPage, computeLeafRecompressWALData(), dlist_iter::cur, dataPlaceToPageLeafRecompress(), disassembleLeaf(), dlist_container, dlist_foreach, elog, END_CRIT_SECTION, ERROR, GIN_SEGMENT_DELETE, GIN_SEGMENT_REPLACE, GIN_SEGMENT_UNMODIFIED, ginCompressPostingList(), GinDataPageMaxDataSize, ginPostingListDecode(), ginVacuumItemPointers(), leafSegmentInfo::items, MarkBufferDirty(), leafSegmentInfo::nitems, PageSetLSN, palloc(), pfree(), REGBUF_STANDARD, RelationNeedsWAL, leafSegmentInfo::seg, disassembledLeaf::segments, SizeOfGinPostingList, START_CRIT_SECTION, disassembledLeaf::walinfo, disassembledLeaf::walinfolen, XLOG_GIN_VACUUM_DATA_LEAF_PAGE, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), and XLogRegisterBuffer().

Referenced by ginVacuumPostingTreeLeaves().

736 {
737  Page page = BufferGetPage(buffer);
738  disassembledLeaf *leaf;
739  bool removedsomething = false;
740  dlist_iter iter;
741 
742  leaf = disassembleLeaf(page);
743 
744  /* Vacuum each segment. */
745  dlist_foreach(iter, &leaf->segments)
746  {
747  leafSegmentInfo *seginfo = dlist_container(leafSegmentInfo, node, iter.cur);
748  int oldsegsize;
749  ItemPointer cleaned;
750  int ncleaned;
751 
752  if (!seginfo->items)
753  seginfo->items = ginPostingListDecode(seginfo->seg,
754  &seginfo->nitems);
755  if (seginfo->seg)
756  oldsegsize = SizeOfGinPostingList(seginfo->seg);
757  else
758  oldsegsize = GinDataPageMaxDataSize;
759 
760  cleaned = ginVacuumItemPointers(gvs,
761  seginfo->items,
762  seginfo->nitems,
763  &ncleaned);
764  pfree(seginfo->items);
765  seginfo->items = NULL;
766  seginfo->nitems = 0;
767  if (cleaned)
768  {
769  if (ncleaned > 0)
770  {
771  int npacked;
772 
773  seginfo->seg = ginCompressPostingList(cleaned,
774  ncleaned,
775  oldsegsize,
776  &npacked);
777  /* Removing an item never increases the size of the segment */
778  if (npacked != ncleaned)
779  elog(ERROR, "could not fit vacuumed posting list");
780  seginfo->action = GIN_SEGMENT_REPLACE;
781  }
782  else
783  {
784  seginfo->seg = NULL;
785  seginfo->items = NULL;
786  seginfo->action = GIN_SEGMENT_DELETE;
787  }
788  seginfo->nitems = ncleaned;
789 
790  removedsomething = true;
791  }
792  }
793 
794  /*
795  * If we removed any items, reconstruct the page from the pieces.
796  *
797  * We don't try to re-encode the segments here, even though some of them
798  * might be really small now that we've removed some items from them. It
799  * seems like a waste of effort, as there isn't really any benefit from
800  * larger segments per se; larger segments only help to pack more items in
801  * the same space. We might as well delay doing that until the next
802  * insertion, which will need to re-encode at least part of the page
803  * anyway.
804  *
805  * Also note if the page was in uncompressed, pre-9.4 format before, it is
806  * now represented as one huge segment that contains all the items. It
807  * might make sense to split that, to speed up random access, but we don't
808  * bother. You'll have to REINDEX anyway if you want the full gain of the
809  * new tighter index format.
810  */
811  if (removedsomething)
812  {
813  bool modified;
814 
815  /*
816  * Make sure we have a palloc'd copy of all segments, after the first
817  * segment that is modified. (dataPlaceToPageLeafRecompress requires
818  * this).
819  */
820  modified = false;
821  dlist_foreach(iter, &leaf->segments)
822  {
824  iter.cur);
825 
826  if (seginfo->action != GIN_SEGMENT_UNMODIFIED)
827  modified = true;
828  if (modified && seginfo->action != GIN_SEGMENT_DELETE)
829  {
830  int segsize = SizeOfGinPostingList(seginfo->seg);
831  GinPostingList *tmp = (GinPostingList *) palloc(segsize);
832 
833  memcpy(tmp, seginfo->seg, segsize);
834  seginfo->seg = tmp;
835  }
836  }
837 
838  if (RelationNeedsWAL(indexrel))
840 
841  /* Apply changes to page */
843 
844  dataPlaceToPageLeafRecompress(buffer, leaf);
845 
846  MarkBufferDirty(buffer);
847 
848  if (RelationNeedsWAL(indexrel))
849  {
850  XLogRecPtr recptr;
851 
852  XLogBeginInsert();
854  XLogRegisterBufData(0, leaf->walinfo, leaf->walinfolen);
855  recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_VACUUM_DATA_LEAF_PAGE);
856  PageSetLSN(page, recptr);
857  }
858 
860  }
861 }
GinPostingList * seg
Definition: gindatapage.c:100
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:368
GinPostingList * ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize, int *nwritten)
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded)
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:220
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
ItemPointer ginVacuumItemPointers(GinVacuumState *gvs, ItemPointerData *items, int nitem, int *nremaining)
Definition: ginvacuum.c:48
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
dlist_head segments
Definition: gindatapage.c:50
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
void pfree(void *pointer)
Definition: mcxt.c:1057
#define GIN_SEGMENT_REPLACE
Definition: ginxlog.h:94
#define ERROR
Definition: elog.h:43
#define REGBUF_STANDARD
Definition: xloginsert.h:35
#define GIN_SEGMENT_DELETE
Definition: ginxlog.h:92
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define XLOG_GIN_VACUUM_DATA_LEAF_PAGE
Definition: ginxlog.h:141
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:422
static void dataPlaceToPageLeafRecompress(Buffer buf, disassembledLeaf *leaf)
Definition: gindatapage.c:975
dlist_node * cur
Definition: ilist.h:161
static void computeLeafRecompressWALData(disassembledLeaf *leaf)
Definition: gindatapage.c:869
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static disassembledLeaf * disassembleLeaf(Page page)
Definition: gindatapage.c:1364
#define RelationNeedsWAL(relation)
Definition: rel.h:562
#define GinDataPageMaxDataSize
Definition: ginblock.h:320
void * palloc(Size size)
Definition: mcxt.c:950
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
#define elog(elevel,...)
Definition: elog.h:214
ItemPointer items
Definition: gindatapage.c:101
#define GIN_SEGMENT_UNMODIFIED
Definition: ginxlog.h:91
void XLogBeginInsert(void)
Definition: xloginsert.c:123
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
Pointer Page
Definition: bufpage.h:78

◆ leafRepackItems()

static bool leafRepackItems ( disassembledLeaf leaf,
ItemPointer  remaining 
)
static

Definition at line 1565 of file gindatapage.c.

References leafSegmentInfo::action, Assert, dlist_iter::cur, dlist_container, dlist_delete(), dlist_foreach, dlist_has_next(), dlist_head_node(), dlist_insert_after(), dlist_next_node(), dlist_prev_node(), GinPostingList::first, GIN_SEGMENT_DELETE, GIN_SEGMENT_INSERT, GIN_SEGMENT_REPLACE, GIN_SEGMENT_UNMODIFIED, ginCompressPostingList(), GinDataPageMaxDataSize, ginMergeItemPointers(), ginPostingListDecode(), GinPostingListSegmentMaxSize, GinPostingListSegmentMinSize, GinPostingListSegmentTargetSize, ItemPointerSetInvalid, leafSegmentInfo::items, disassembledLeaf::lastleft, disassembledLeaf::lsize, leafSegmentInfo::modifieditems, leafSegmentInfo::nitems, leafSegmentInfo::nmodifieditems, leafSegmentInfo::node, palloc(), pfree(), disassembledLeaf::rsize, leafSegmentInfo::seg, disassembledLeaf::segments, and SizeOfGinPostingList.

Referenced by dataBeginPlaceToPageLeaf().

1566 {
1567  int pgused = 0;
1568  bool needsplit = false;
1569  dlist_iter iter;
1570  int segsize;
1571  leafSegmentInfo *nextseg;
1572  int npacked;
1573  bool modified;
1574  dlist_node *cur_node;
1575  dlist_node *next_node;
1576 
1577  ItemPointerSetInvalid(remaining);
1578 
1579  /*
1580  * cannot use dlist_foreach_modify here because we insert adjacent items
1581  * while iterating.
1582  */
1583  for (cur_node = dlist_head_node(&leaf->segments);
1584  cur_node != NULL;
1585  cur_node = next_node)
1586  {
1588  cur_node);
1589 
1590  if (dlist_has_next(&leaf->segments, cur_node))
1591  next_node = dlist_next_node(&leaf->segments, cur_node);
1592  else
1593  next_node = NULL;
1594 
1595  /* Compress the posting list, if necessary */
1596  if (seginfo->action != GIN_SEGMENT_DELETE)
1597  {
1598  if (seginfo->seg == NULL)
1599  {
1600  if (seginfo->nitems > GinPostingListSegmentMaxSize)
1601  npacked = 0; /* no chance that it would fit. */
1602  else
1603  {
1604  seginfo->seg = ginCompressPostingList(seginfo->items,
1605  seginfo->nitems,
1607  &npacked);
1608  }
1609  if (npacked != seginfo->nitems)
1610  {
1611  /*
1612  * Too large. Compress again to the target size, and
1613  * create a new segment to represent the remaining items.
1614  * The new segment is inserted after this one, so it will
1615  * be processed in the next iteration of this loop.
1616  */
1617  if (seginfo->seg)
1618  pfree(seginfo->seg);
1619  seginfo->seg = ginCompressPostingList(seginfo->items,
1620  seginfo->nitems,
1622  &npacked);
1623  if (seginfo->action != GIN_SEGMENT_INSERT)
1624  seginfo->action = GIN_SEGMENT_REPLACE;
1625 
1626  nextseg = palloc(sizeof(leafSegmentInfo));
1627  nextseg->action = GIN_SEGMENT_INSERT;
1628  nextseg->seg = NULL;
1629  nextseg->items = &seginfo->items[npacked];
1630  nextseg->nitems = seginfo->nitems - npacked;
1631  next_node = &nextseg->node;
1632  dlist_insert_after(cur_node, next_node);
1633  }
1634  }
1635 
1636  /*
1637  * If the segment is very small, merge it with the next segment.
1638  */
1639  if (SizeOfGinPostingList(seginfo->seg) < GinPostingListSegmentMinSize && next_node)
1640  {
1641  int nmerged;
1642 
1643  nextseg = dlist_container(leafSegmentInfo, node, next_node);
1644 
1645  if (seginfo->items == NULL)
1646  seginfo->items = ginPostingListDecode(seginfo->seg,
1647  &seginfo->nitems);
1648  if (nextseg->items == NULL)
1649  nextseg->items = ginPostingListDecode(nextseg->seg,
1650  &nextseg->nitems);
1651  nextseg->items =
1652  ginMergeItemPointers(seginfo->items, seginfo->nitems,
1653  nextseg->items, nextseg->nitems,
1654  &nmerged);
1655  Assert(nmerged == seginfo->nitems + nextseg->nitems);
1656  nextseg->nitems = nmerged;
1657  nextseg->seg = NULL;
1658 
1659  nextseg->action = GIN_SEGMENT_REPLACE;
1660  nextseg->modifieditems = NULL;
1661  nextseg->nmodifieditems = 0;
1662 
1663  if (seginfo->action == GIN_SEGMENT_INSERT)
1664  {
1665  dlist_delete(cur_node);
1666  continue;
1667  }
1668  else
1669  {
1670  seginfo->action = GIN_SEGMENT_DELETE;
1671  seginfo->seg = NULL;
1672  }
1673  }
1674 
1675  seginfo->items = NULL;
1676  seginfo->nitems = 0;
1677  }
1678 
1679  if (seginfo->action == GIN_SEGMENT_DELETE)
1680  continue;
1681 
1682  /*
1683  * OK, we now have a compressed version of this segment ready for
1684  * copying to the page. Did we exceed the size that fits on one page?
1685  */
1686  segsize = SizeOfGinPostingList(seginfo->seg);
1687  if (pgused + segsize > GinDataPageMaxDataSize)
1688  {
1689  if (!needsplit)
1690  {
1691  /* switch to right page */
1692  Assert(pgused > 0);
1693  leaf->lastleft = dlist_prev_node(&leaf->segments, cur_node);
1694  needsplit = true;
1695  leaf->lsize = pgused;
1696  pgused = 0;
1697  }
1698  else
1699  {
1700  /*
1701  * Filled both pages. The last segment we constructed did not
1702  * fit.
1703  */
1704  *remaining = seginfo->seg->first;
1705 
1706  /*
1707  * remove all segments that did not fit from the list.
1708  */
1709  while (dlist_has_next(&leaf->segments, cur_node))
1710  dlist_delete(dlist_next_node(&leaf->segments, cur_node));
1711  dlist_delete(cur_node);
1712  break;
1713  }
1714  }
1715 
1716  pgused += segsize;
1717  }
1718 
1719  if (!needsplit)
1720  {
1721  leaf->lsize = pgused;
1722  leaf->rsize = 0;
1723  }
1724  else
1725  leaf->rsize = pgused;
1726 
1729 
1730  /*
1731  * Make a palloc'd copy of every segment after the first modified one,
1732  * because as we start copying items to the original page, we might
1733  * overwrite an existing segment.
1734  */
1735  modified = false;
1736  dlist_foreach(iter, &leaf->segments)
1737  {
1739  iter.cur);
1740 
1741  if (!modified && seginfo->action != GIN_SEGMENT_UNMODIFIED)
1742  {
1743  modified = true;
1744  }
1745  else if (modified && seginfo->action == GIN_SEGMENT_UNMODIFIED)
1746  {
1747  GinPostingList *tmp;
1748 
1749  segsize = SizeOfGinPostingList(seginfo->seg);
1750  tmp = palloc(segsize);
1751  memcpy(tmp, seginfo->seg, segsize);
1752  seginfo->seg = tmp;
1753  }
1754  }
1755 
1756  return needsplit;
1757 }
GinPostingList * seg
Definition: gindatapage.c:100
GinPostingList * ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize, int *nwritten)
#define GinPostingListSegmentMaxSize
Definition: gindatapage.c:34
dlist_node * lastleft
Definition: gindatapage.c:56
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded)
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
#define GinPostingListSegmentTargetSize
Definition: gindatapage.c:35
#define GinPostingListSegmentMinSize
Definition: gindatapage.c:36
static void dlist_insert_after(dlist_node *after, dlist_node *node)
Definition: ilist.h:334
ItemPointer ginMergeItemPointers(ItemPointerData *a, uint32 na, ItemPointerData *b, uint32 nb, int *nmerged)
#define GIN_SEGMENT_INSERT
Definition: ginxlog.h:93
dlist_head segments
Definition: gindatapage.c:50
static dlist_node * dlist_next_node(dlist_head *head, dlist_node *node)
Definition: ilist.h:421
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
ItemPointerData first
Definition: ginblock.h:338
void pfree(void *pointer)
Definition: mcxt.c:1057
#define GIN_SEGMENT_REPLACE
Definition: ginxlog.h:94
ItemPointerData * modifieditems
Definition: gindatapage.c:90
static dlist_node * dlist_prev_node(dlist_head *head, dlist_node *node)
Definition: ilist.h:431
#define GIN_SEGMENT_DELETE
Definition: ginxlog.h:92
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
static bool dlist_has_next(dlist_head *head, dlist_node *node)
Definition: ilist.h:402
dlist_node * cur
Definition: ilist.h:161
static dlist_node * dlist_head_node(dlist_head *head)
Definition: ilist.h:449
#define Assert(condition)
Definition: c.h:745
dlist_node node
Definition: gindatapage.c:72
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
#define GinDataPageMaxDataSize
Definition: ginblock.h:320
void * palloc(Size size)
Definition: mcxt.c:950
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
ItemPointer items
Definition: gindatapage.c:101
#define GIN_SEGMENT_UNMODIFIED
Definition: ginxlog.h:91
uint16 nmodifieditems
Definition: gindatapage.c:91