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

1432 {
1433  dlist_iter iter;
1434  ItemPointer nextnew = newItems;
1435  int newleft = nNewItems;
1436  bool modified = false;
1437  leafSegmentInfo *newseg;
1438 
1439  /*
1440  * If the page is completely empty, just construct one new segment to hold
1441  * all the new items.
1442  */
1443  if (dlist_is_empty(&leaf->segments))
1444  {
1445  newseg = palloc(sizeof(leafSegmentInfo));
1446  newseg->seg = NULL;
1447  newseg->items = newItems;
1448  newseg->nitems = nNewItems;
1449  newseg->action = GIN_SEGMENT_INSERT;
1450  dlist_push_tail(&leaf->segments, &newseg->node);
1451  return true;
1452  }
1453 
1454  dlist_foreach(iter, &leaf->segments)
1455  {
1457  int nthis;
1458  ItemPointer tmpitems;
1459  int ntmpitems;
1460 
1461  /*
1462  * How many of the new items fall into this segment?
1463  */
1464  if (!dlist_has_next(&leaf->segments, iter.cur))
1465  nthis = newleft;
1466  else
1467  {
1469  ItemPointerData next_first;
1470 
1472  dlist_next_node(&leaf->segments, iter.cur));
1473  if (next->items)
1474  next_first = next->items[0];
1475  else
1476  {
1477  Assert(next->seg != NULL);
1478  next_first = next->seg->first;
1479  }
1480 
1481  nthis = 0;
1482  while (nthis < newleft && ginCompareItemPointers(&nextnew[nthis], &next_first) < 0)
1483  nthis++;
1484  }
1485  if (nthis == 0)
1486  continue;
1487 
1488  /* Merge the new items with the existing items. */
1489  if (!cur->items)
1490  cur->items = ginPostingListDecode(cur->seg, &cur->nitems);
1491 
1492  /*
1493  * Fast path for the important special case that we're appending to
1494  * the end of the page: don't let the last segment on the page grow
1495  * larger than the target, create a new segment before that happens.
1496  */
1497  if (!dlist_has_next(&leaf->segments, iter.cur) &&
1498  ginCompareItemPointers(&cur->items[cur->nitems - 1], &nextnew[0]) < 0 &&
1499  cur->seg != NULL &&
1501  {
1502  newseg = palloc(sizeof(leafSegmentInfo));
1503  newseg->seg = NULL;
1504  newseg->items = nextnew;
1505  newseg->nitems = nthis;
1506  newseg->action = GIN_SEGMENT_INSERT;
1507  dlist_push_tail(&leaf->segments, &newseg->node);
1508  modified = true;
1509  break;
1510  }
1511 
1512  tmpitems = ginMergeItemPointers(cur->items, cur->nitems,
1513  nextnew, nthis,
1514  &ntmpitems);
1515  if (ntmpitems != cur->nitems)
1516  {
1517  /*
1518  * If there are no duplicates, track the added items so that we
1519  * can emit a compact ADDITEMS WAL record later on. (it doesn't
1520  * seem worth re-checking which items were duplicates, if there
1521  * were any)
1522  */
1523  if (ntmpitems == nthis + cur->nitems &&
1525  {
1527  cur->modifieditems = nextnew;
1528  cur->nmodifieditems = nthis;
1529  }
1530  else
1531  cur->action = GIN_SEGMENT_REPLACE;
1532 
1533  cur->items = tmpitems;
1534  cur->nitems = ntmpitems;
1535  cur->seg = NULL;
1536  modified = true;
1537  }
1538 
1539  nextnew += nthis;
1540  newleft -= nthis;
1541  if (newleft == 0)
1542  break;
1543  }
1544 
1545  return modified;
1546 }
GinPostingList * seg
Definition: gindatapage.c:100
static int32 next
Definition: blutils.c:211
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:95
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:331
#define GIN_SEGMENT_REPLACE
Definition: ginxlog.h:96
ItemPointerData * modifieditems
Definition: gindatapage.c:90
#define GIN_SEGMENT_ADDITEMS
Definition: ginxlog.h:97
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:699
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:463
void * palloc(Size size)
Definition: mcxt.c:924
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:336
ItemPointer items
Definition: gindatapage.c:101
#define GIN_SEGMENT_UNMODIFIED
Definition: ginxlog.h:93
uint16 nmodifieditems
Definition: gindatapage.c:91

◆ computeLeafRecompressWALData()

static void computeLeafRecompressWALData ( disassembledLeaf leaf)
static

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

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

◆ createPostingTree()

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

Definition at line 1762 of file gindatapage.c.

References buffer, 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().

1764 {
1765  BlockNumber blkno;
1766  Buffer buffer;
1767  Page tmppage;
1768  Page page;
1769  Pointer ptr;
1770  int nrootitems;
1771  int rootsize;
1772 
1773  /* Construct the new root page in memory first. */
1774  tmppage = (Page) palloc(BLCKSZ);
1775  GinInitPage(tmppage, GIN_DATA | GIN_LEAF | GIN_COMPRESSED, BLCKSZ);
1776  GinPageGetOpaque(tmppage)->rightlink = InvalidBlockNumber;
1777 
1778  /*
1779  * Write as many of the items to the root page as fit. In segments of max
1780  * GinPostingListSegmentMaxSize bytes each.
1781  */
1782  nrootitems = 0;
1783  rootsize = 0;
1784  ptr = (Pointer) GinDataLeafPageGetPostingList(tmppage);
1785  while (nrootitems < nitems)
1786  {
1787  GinPostingList *segment;
1788  int npacked;
1789  int segsize;
1790 
1791  segment = ginCompressPostingList(&items[nrootitems],
1792  nitems - nrootitems,
1794  &npacked);
1795  segsize = SizeOfGinPostingList(segment);
1796  if (rootsize + segsize > GinDataPageMaxDataSize)
1797  break;
1798 
1799  memcpy(ptr, segment, segsize);
1800  ptr += segsize;
1801  rootsize += segsize;
1802  nrootitems += npacked;
1803  pfree(segment);
1804  }
1805  GinDataPageSetDataSize(tmppage, rootsize);
1806 
1807  /*
1808  * All set. Get a new physical page, and copy the in-memory page to it.
1809  */
1810  buffer = GinNewBuffer(index);
1811  page = BufferGetPage(buffer);
1812  blkno = BufferGetBlockNumber(buffer);
1813 
1814  /*
1815  * Copy a predicate lock from entry tree leaf (containing posting list)
1816  * to posting tree.
1817  */
1818  PredicateLockPageSplit(index, BufferGetBlockNumber(entrybuffer), blkno);
1819 
1821 
1822  PageRestoreTempPage(tmppage, page);
1823  MarkBufferDirty(buffer);
1824 
1825  if (RelationNeedsWAL(index))
1826  {
1827  XLogRecPtr recptr;
1829 
1830  data.size = rootsize;
1831 
1832  XLogBeginInsert();
1833  XLogRegisterData((char *) &data, sizeof(ginxlogCreatePostingTree));
1834 
1836  rootsize);
1838 
1839  recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_CREATE_PTREE);
1840  PageSetLSN(page, recptr);
1841  }
1842 
1843  UnlockReleaseBuffer(buffer);
1844 
1845  END_CRIT_SECTION();
1846 
1847  /* During index build, count the newly-added data page */
1848  if (buildStats)
1849  buildStats->nDataPages++;
1850 
1851  elog(DEBUG2, "created GIN posting tree with %d items", nrootitems);
1852 
1853  /*
1854  * Add any remaining TIDs to the newly-created posting tree.
1855  */
1856  if (nitems > nrootitems)
1857  {
1858  ginInsertItemPointers(index, blkno,
1859  items + nrootitems,
1860  nitems - nrootitems,
1861  buildStats);
1862  }
1863 
1864  return blkno;
1865 }
GinPostingList * ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize, int *nwritten)
Buffer GinNewBuffer(Relation index)
Definition: ginutil.c:291
#define GinPostingListSegmentMaxSize
Definition: gindatapage.c:34
void ginInsertItemPointers(Relation index, BlockNumber rootBlkno, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gindatapage.c:1894
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:407
void GinInitPage(Page page, uint32 f, Size pageSize)
Definition: ginutil.c:342
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
uint32 BlockNumber
Definition: block.h:31
#define GinPageGetOpaque(page)
Definition: ginblock.h:109
#define GIN_COMPRESSED
Definition: ginblock.h:47
#define GIN_DATA
Definition: ginblock.h:39
#define GinDataPageSetDataSize(page, size)
Definition: ginblock.h:303
void pfree(void *pointer)
Definition: mcxt.c:1031
char * Pointer
Definition: c.h:302
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define XLOG_GIN_CREATE_PTREE
Definition: ginxlog.h:21
#define DEBUG2
Definition: elog.h:24
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:272
BlockNumber nDataPages
Definition: gin.h:46
uint64 XLogRecPtr
Definition: xlogdefs.h:21
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
#define InvalidBlockNumber
Definition: block.h:33
#define RelationNeedsWAL(relation)
Definition: rel.h:510
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
#define GinDataPageMaxDataSize
Definition: ginblock.h:313
void * palloc(Size size)
Definition: mcxt.c:924
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:336
#define elog
Definition: elog.h:219
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define PageSetLSN(page, lsn)
Definition: bufpage.h:364
int Buffer
Definition: buf.h:23
void PredicateLockPageSplit(Relation relation, BlockNumber oldblkno, BlockNumber newblkno)
Definition: predicate.c:3065
Pointer Page
Definition: bufpage.h:74
#define GIN_LEAF
Definition: ginblock.h:40

◆ 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 1192 of file gindatapage.c.

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

Referenced by ginPrepareDataScan().

1196 {
1197  Page page = BufferGetPage(buf);
1198 
1199  Assert(GinPageIsData(page));
1200 
1201  if (GinPageIsLeaf(page))
1202  return dataBeginPlaceToPageLeaf(btree, buf, stack, insertdata,
1203  ptp_workspace,
1204  newlpage, newrpage);
1205  else
1206  return dataBeginPlaceToPageInternal(btree, buf, stack,
1207  insertdata, updateblkno,
1208  ptp_workspace,
1209  newlpage, newrpage);
1210 }
static GinPlaceToPageRC dataBeginPlaceToPageInternal(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void **ptp_workspace, Page *newlpage, Page *newrpage)
Definition: gindatapage.c:1113
static char * buf
Definition: pg_test_fsync.c:67
#define GinPageIsLeaf(page)
Definition: ginblock.h:111
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
static GinPlaceToPageRC dataBeginPlaceToPageLeaf(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, void **ptp_workspace, Page *newlpage, Page *newrpage)
Definition: gindatapage.c:445
#define GinPageIsData(page)
Definition: ginblock.h:114
#define Assert(condition)
Definition: c.h:699
Pointer Page
Definition: bufpage.h:74

◆ 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 1113 of file gindatapage.c.

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

Referenced by dataBeginPlaceToPage().

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

◆ dataBeginPlaceToPageLeaf()

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

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

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

◆ dataExecPlaceToPage()

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

Definition at line 1222 of file gindatapage.c.

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

Referenced by ginPrepareDataScan().

1225 {
1226  Page page = BufferGetPage(buf);
1227 
1228  if (GinPageIsLeaf(page))
1229  dataExecPlaceToPageLeaf(btree, buf, stack, insertdata,
1230  ptp_workspace);
1231  else
1232  dataExecPlaceToPageInternal(btree, buf, stack, insertdata,
1233  updateblkno, ptp_workspace);
1234 }
static void dataExecPlaceToPageInternal(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void *ptp_workspace)
Definition: gindatapage.c:1139
static void dataExecPlaceToPageLeaf(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, void *ptp_workspace)
Definition: gindatapage.c:713
static char * buf
Definition: pg_test_fsync.c:67
#define GinPageIsLeaf(page)
Definition: ginblock.h:111
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
Pointer Page
Definition: bufpage.h:74

◆ dataExecPlaceToPageInternal()

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

Definition at line 1139 of file gindatapage.c.

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

Referenced by dataExecPlaceToPage().

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

◆ dataExecPlaceToPageLeaf()

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

Definition at line 713 of file gindatapage.c.

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

Referenced by dataExecPlaceToPage().

715 {
716  disassembledLeaf *leaf = (disassembledLeaf *) ptp_workspace;
717 
718  /* Apply changes to page */
720 
721  /* If needed, register WAL data built by computeLeafRecompressWALData */
722  if (RelationNeedsWAL(btree->index))
723  {
724  XLogRegisterBufData(0, leaf->walinfo, leaf->walinfolen);
725  }
726 }
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:361
static char * buf
Definition: pg_test_fsync.c:67
static void dataPlaceToPageLeafRecompress(Buffer buf, disassembledLeaf *leaf)
Definition: gindatapage.c:972
Relation index
Definition: gin_private.h:162
#define RelationNeedsWAL(relation)
Definition: rel.h:510

◆ dataFindChildPtr()

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

Definition at line 316 of file gindatapage.c.

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

Referenced by ginPrepareDataScan().

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

◆ dataGetLeftMostPage()

static BlockNumber dataGetLeftMostPage ( GinBtree  btree,
Page  page 
)
static

Definition at line 361 of file gindatapage.c.

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

Referenced by ginPrepareDataScan().

362 {
363  PostingItem *pitem;
364 
365  Assert(!GinPageIsLeaf(page));
366  Assert(GinPageIsData(page));
367  Assert(GinPageGetOpaque(page)->maxoff >= FirstOffsetNumber);
368 
370  return PostingItemGetBlockNumber(pitem);
371 }
#define GinPageGetOpaque(page)
Definition: ginblock.h:109
#define GinDataPageGetPostingItem(page, i)
Definition: ginblock.h:292
#define FirstOffsetNumber
Definition: off.h:27
#define GinPageIsLeaf(page)
Definition: ginblock.h:111
#define GinPageIsData(page)
Definition: ginblock.h:114
#define PostingItemGetBlockNumber(pointer)
Definition: ginblock.h:183
#define Assert(condition)
Definition: c.h:699

◆ dataIsMoveRight()

static bool dataIsMoveRight ( GinBtree  btree,
Page  page 
)
static

Definition at line 234 of file gindatapage.c.

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

Referenced by ginPrepareDataScan().

235 {
237 
238  if (GinPageRightMost(page))
239  return false;
240 
241  return (ginCompareItemPointers(&btree->itemptr, iptr) > 0) ? true : false;
242 }
#define true
Definition: c.h:279
#define GinPageRightMost(page)
Definition: ginblock.h:128
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:463
#define GinDataPageGetRightBound(page)
Definition: ginblock.h:282
ItemPointerData itemptr
Definition: gin_private.h:174

◆ 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:289
#define GinPageGetOpaque(page)
Definition: ginblock.h:109
#define GinPageIsCompressed(page)
Definition: ginblock.h:120
ItemPointerData * ItemPointer
Definition: itemptr.h:49
#define Assert(condition)
Definition: c.h:699

◆ dataLocateItem()

static BlockNumber dataLocateItem ( GinBtree  btree,
GinBtreeStack stack 
)
static

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

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

◆ dataPlaceToPageLeafRecompress()

static void dataPlaceToPageLeafRecompress ( Buffer  buf,
disassembledLeaf leaf 
)
static

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

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

◆ dataPlaceToPageLeafSplit()

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

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

1031 {
1032  char *ptr;
1033  int segsize;
1034  int lsize;
1035  int rsize;
1036  dlist_node *node;
1037  dlist_node *firstright;
1038  leafSegmentInfo *seginfo;
1039 
1040  /* Initialize temporary pages to hold the new left and right pages */
1041  GinInitPage(lpage, GIN_DATA | GIN_LEAF | GIN_COMPRESSED, BLCKSZ);
1042  GinInitPage(rpage, GIN_DATA | GIN_LEAF | GIN_COMPRESSED, BLCKSZ);
1043 
1044  /*
1045  * Copy the segments that go to the left page.
1046  *
1047  * XXX: We should skip copying the unmodified part of the left page, like
1048  * we do when recompressing.
1049  */
1050  lsize = 0;
1051  ptr = (char *) GinDataLeafPageGetPostingList(lpage);
1052  firstright = dlist_next_node(&leaf->segments, leaf->lastleft);
1053  for (node = dlist_head_node(&leaf->segments);
1054  node != firstright;
1055  node = dlist_next_node(&leaf->segments, node))
1056  {
1057  seginfo = dlist_container(leafSegmentInfo, node, node);
1058 
1059  if (seginfo->action != GIN_SEGMENT_DELETE)
1060  {
1061  segsize = SizeOfGinPostingList(seginfo->seg);
1062  memcpy(ptr, seginfo->seg, segsize);
1063  ptr += segsize;
1064  lsize += segsize;
1065  }
1066  }
1067  Assert(lsize == leaf->lsize);
1068  GinDataPageSetDataSize(lpage, lsize);
1069  *GinDataPageGetRightBound(lpage) = lbound;
1070 
1071  /* Copy the segments that go to the right page */
1072  ptr = (char *) GinDataLeafPageGetPostingList(rpage);
1073  rsize = 0;
1074  for (node = firstright;
1075  ;
1076  node = dlist_next_node(&leaf->segments, node))
1077  {
1078  seginfo = dlist_container(leafSegmentInfo, node, node);
1079 
1080  if (seginfo->action != GIN_SEGMENT_DELETE)
1081  {
1082  segsize = SizeOfGinPostingList(seginfo->seg);
1083  memcpy(ptr, seginfo->seg, segsize);
1084  ptr += segsize;
1085  rsize += segsize;
1086  }
1087 
1088  if (!dlist_has_next(&leaf->segments, node))
1089  break;
1090  }
1091  Assert(rsize == leaf->rsize);
1092  GinDataPageSetDataSize(rpage, rsize);
1093  *GinDataPageGetRightBound(rpage) = rbound;
1094 }
GinPostingList * seg
Definition: gindatapage.c:100
dlist_node * lastleft
Definition: gindatapage.c:56
void GinInitPage(Page page, uint32 f, Size pageSize)
Definition: ginutil.c:342
#define GIN_COMPRESSED
Definition: ginblock.h:47
#define GIN_DATA
Definition: ginblock.h:39
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:303
#define GIN_SEGMENT_DELETE
Definition: ginxlog.h:94
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:272
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:699
#define GinDataPageGetRightBound(page)
Definition: ginblock.h:282
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:336
#define GIN_LEAF
Definition: ginblock.h:40

◆ dataPrepareDownlink()

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

Definition at line 1324 of file gindatapage.c.

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

Referenced by ginPrepareDataScan().

1325 {
1326  PostingItem *pitem = palloc(sizeof(PostingItem));
1327  Page lpage = BufferGetPage(lbuf);
1328 
1330  pitem->key = *GinDataPageGetRightBound(lpage);
1331 
1332  return pitem;
1333 }
#define PostingItemSetBlockNumber(pointer, blockNumber)
Definition: ginblock.h:186
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
ItemPointerData key
Definition: ginblock.h:180
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
#define GinDataPageGetRightBound(page)
Definition: ginblock.h:282
void * palloc(Size size)
Definition: mcxt.c:924
Pointer Page
Definition: bufpage.h:74

◆ dataSplitPageInternal()

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

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

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

◆ disassembleLeaf()

static disassembledLeaf * disassembleLeaf ( Page  page)
static

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

1362 {
1363  disassembledLeaf *leaf;
1364  GinPostingList *seg;
1365  Pointer segbegin;
1366  Pointer segend;
1367 
1368  leaf = palloc0(sizeof(disassembledLeaf));
1369  dlist_init(&leaf->segments);
1370 
1371  if (GinPageIsCompressed(page))
1372  {
1373  /*
1374  * Create a leafSegment entry for each segment.
1375  */
1376  seg = GinDataLeafPageGetPostingList(page);
1377  segbegin = (Pointer) seg;
1378  segend = segbegin + GinDataLeafPageGetPostingListSize(page);
1379  while ((Pointer) seg < segend)
1380  {
1381  leafSegmentInfo *seginfo = palloc(sizeof(leafSegmentInfo));
1382 
1383  seginfo->action = GIN_SEGMENT_UNMODIFIED;
1384  seginfo->seg = seg;
1385  seginfo->items = NULL;
1386  seginfo->nitems = 0;
1387  dlist_push_tail(&leaf->segments, &seginfo->node);
1388 
1389  seg = GinNextPostingListSegment(seg);
1390  }
1391  leaf->oldformat = false;
1392  }
1393  else
1394  {
1395  /*
1396  * A pre-9.4 format uncompressed page is represented by a single
1397  * segment, with an array of items.
1398  */
1399  ItemPointer uncompressed;
1400  int nuncompressed;
1401  leafSegmentInfo *seginfo;
1402 
1403  uncompressed = dataLeafPageGetUncompressed(page, &nuncompressed);
1404 
1405  seginfo = palloc(sizeof(leafSegmentInfo));
1406 
1407  seginfo->action = GIN_SEGMENT_REPLACE;
1408  seginfo->seg = NULL;
1409  seginfo->items = palloc(nuncompressed * sizeof(ItemPointerData));
1410  memcpy(seginfo->items, uncompressed, nuncompressed * sizeof(ItemPointerData));
1411  seginfo->nitems = nuncompressed;
1412 
1413  dlist_push_tail(&leaf->segments, &seginfo->node);
1414 
1415  leaf->oldformat = true;
1416  }
1417 
1418  return leaf;
1419 }
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:120
dlist_head segments
Definition: gindatapage.c:50
char * Pointer
Definition: c.h:302
#define GIN_SEGMENT_REPLACE
Definition: ginxlog.h:96
#define GinNextPostingListSegment(cur)
Definition: ginblock.h:337
#define GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:274
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:272
void * palloc0(Size size)
Definition: mcxt.c:955
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:924
ItemPointer items
Definition: gindatapage.c:101
#define GIN_SEGMENT_UNMODIFIED
Definition: ginxlog.h:93
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 1340 of file gindatapage.c.

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

Referenced by ginPrepareDataScan().

1341 {
1342  PostingItem li,
1343  ri;
1344 
1345  li.key = *GinDataPageGetRightBound(lpage);
1346  PostingItemSetBlockNumber(&li, lblkno);
1348 
1349  ri.key = *GinDataPageGetRightBound(rpage);
1350  PostingItemSetBlockNumber(&ri, rblkno);
1352 }
#define PostingItemSetBlockNumber(pointer, blockNumber)
Definition: ginblock.h:186
void GinDataPageAddPostingItem(Page page, PostingItem *data, OffsetNumber offset)
Definition: gindatapage.c:377
ItemPointerData key
Definition: ginblock.h:180
#define InvalidOffsetNumber
Definition: off.h:26
#define GinDataPageGetRightBound(page)
Definition: ginblock.h:282

◆ 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:60
static int32 next
Definition: blutils.c:211
#define GinPageIsCompressed(page)
Definition: ginblock.h:120
ItemPointerData first
Definition: ginblock.h:331
char * Pointer
Definition: c.h:302
#define GinNextPostingListSegment(cur)
Definition: ginblock.h:337
#define GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:274
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:272
size_t Size
Definition: c.h:433
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:463
void * palloc(Size size)
Definition: mcxt.c:924
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:120
#define GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:274
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:272
size_t Size
Definition: c.h:433
static ItemPointer dataLeafPageGetUncompressed(Page page, int *nitems)
Definition: gindatapage.c:211

◆ GinDataPageAddPostingItem()

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

Definition at line 377 of file gindatapage.c.

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

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

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

◆ ginInsertItemPointers()

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

Definition at line 1894 of file gindatapage.c.

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

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

1897 {
1898  GinBtreeData btree;
1899  GinBtreeDataLeafInsertData insertdata;
1900  GinBtreeStack *stack;
1901 
1902  ginPrepareDataScan(&btree, index, rootBlkno);
1903  btree.isBuild = (buildStats != NULL);
1904  insertdata.items = items;
1905  insertdata.nitem = nitem;
1906  insertdata.curitem = 0;
1907 
1908  while (insertdata.curitem < insertdata.nitem)
1909  {
1910  /* search for the leaf page where the first item should go to */
1911  btree.itemptr = insertdata.items[insertdata.curitem];
1912  stack = ginFindLeafPage(&btree, false, NULL);
1913 
1914  GinCheckForSerializableConflictIn(btree.index, NULL, stack->buffer);
1915  ginInsertValue(&btree, stack, &insertdata, buildStats);
1916  }
1917 }
void GinCheckForSerializableConflictIn(Relation relation, HeapTuple tuple, Buffer buffer)
Definition: ginutil.c:723
GinBtreeStack * ginFindLeafPage(GinBtree btree, bool searchMode, Snapshot snapshot)
Definition: ginbtree.c:77
ItemPointerData * items
Definition: gin_private.h:190
void ginInsertValue(GinBtree btree, GinBtreeStack *stack, void *insertdata, GinStatsData *buildStats)
Definition: ginbtree.c:777
Relation index
Definition: gin_private.h:162
ItemPointerData itemptr
Definition: gin_private.h:174
void ginPrepareDataScan(GinBtree btree, Relation index, BlockNumber rootBlkno)
Definition: gindatapage.c:1868

◆ GinPageDeletePostingItem()

void GinPageDeletePostingItem ( Page  page,
OffsetNumber  offset 
)

Definition at line 414 of file gindatapage.c.

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

Referenced by ginDeletePage(), and ginRedoDeletePage().

415 {
416  OffsetNumber maxoff = GinPageGetOpaque(page)->maxoff;
417 
418  Assert(!GinPageIsLeaf(page));
419  Assert(offset >= FirstOffsetNumber && offset <= maxoff);
420 
421  if (offset != maxoff)
422  memmove(GinDataPageGetPostingItem(page, offset),
423  GinDataPageGetPostingItem(page, offset + 1),
424  sizeof(PostingItem) * (maxoff - offset));
425 
426  maxoff--;
427  GinPageGetOpaque(page)->maxoff = maxoff;
428 
429  GinDataPageSetDataSize(page, maxoff * sizeof(PostingItem));
430 }
#define GinPageGetOpaque(page)
Definition: ginblock.h:109
uint16 OffsetNumber
Definition: off.h:24
#define GinDataPageSetDataSize(page, size)
Definition: ginblock.h:303
#define GinDataPageGetPostingItem(page, i)
Definition: ginblock.h:292
#define memmove(d, s, c)
Definition: c.h:1100
#define FirstOffsetNumber
Definition: off.h:27
#define GinPageIsLeaf(page)
Definition: ginblock.h:111
#define Assert(condition)
Definition: c.h:699

◆ ginPrepareDataScan()

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

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

1869 {
1870  memset(btree, 0, sizeof(GinBtreeData));
1871 
1872  btree->index = index;
1873  btree->rootBlkno = rootBlkno;
1874 
1875  btree->findChildPage = dataLocateItem;
1877  btree->isMoveRight = dataIsMoveRight;
1878  btree->findItem = NULL;
1879  btree->findChildPtr = dataFindChildPtr;
1882  btree->fillRoot = ginDataFillRoot;
1884 
1885  btree->isData = true;
1886  btree->fullScan = false;
1887  btree->isBuild = false;
1888 }
void(* fillRoot)(GinBtree, Page, BlockNumber, Page, BlockNumber, Page)
Definition: gin_private.h:158
bool(* isMoveRight)(GinBtree, Page)
Definition: gin_private.h:150
static BlockNumber dataLocateItem(GinBtree btree, GinBtreeStack *stack)
Definition: gindatapage.c:249
BlockNumber rootBlkno
Definition: gin_private.h:163
bool(* findItem)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:151
static GinPlaceToPageRC dataBeginPlaceToPage(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void **ptp_workspace, Page *newlpage, Page *newrpage)
Definition: gindatapage.c:1192
GinPlaceToPageRC(* beginPlaceToPage)(GinBtree, Buffer, GinBtreeStack *, void *, BlockNumber, void **, Page *, Page *)
Definition: gin_private.h:155
static bool dataIsMoveRight(GinBtree btree, Page page)
Definition: gindatapage.c:234
void(* execPlaceToPage)(GinBtree, Buffer, GinBtreeStack *, void *, BlockNumber, void *)
Definition: gin_private.h:156
BlockNumber(* findChildPage)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:148
static OffsetNumber dataFindChildPtr(GinBtree btree, Page page, BlockNumber blkno, OffsetNumber storedOff)
Definition: gindatapage.c:316
BlockNumber(* getLeftMostChild)(GinBtree, Page)
Definition: gin_private.h:149
Relation index
Definition: gin_private.h:162
void *(* prepareDownlink)(GinBtree, Buffer)
Definition: gin_private.h:157
void ginDataFillRoot(GinBtree btree, Page root, BlockNumber lblkno, Page lpage, BlockNumber rblkno, Page rpage)
Definition: gindatapage.c:1340
static void * dataPrepareDownlink(GinBtree btree, Buffer lbuf)
Definition: gindatapage.c:1324
OffsetNumber(* findChildPtr)(GinBtree, Page, BlockNumber, OffsetNumber)
Definition: gin_private.h:154
static void dataExecPlaceToPage(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void *ptp_workspace)
Definition: gindatapage.c:1222
static BlockNumber dataGetLeftMostPage(GinBtree btree, Page page)
Definition: gindatapage.c:361

◆ ginScanBeginPostingTree()

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

Definition at line 1923 of file gindatapage.c.

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

Referenced by scanPostingTree(), and startScanEntry().

1925 {
1926  GinBtreeStack *stack;
1927 
1928  ginPrepareDataScan(btree, index, rootBlkno);
1929 
1930  btree->fullScan = true;
1931 
1932  stack = ginFindLeafPage(btree, true, snapshot);
1933 
1934  return stack;
1935 }
GinBtreeStack * ginFindLeafPage(GinBtree btree, bool searchMode, Snapshot snapshot)
Definition: ginbtree.c:77
void ginPrepareDataScan(GinBtree btree, Relation index, BlockNumber rootBlkno)
Definition: gindatapage.c:1868

◆ ginVacuumPostingTreeLeaf()

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

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

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

◆ leafRepackItems()

static bool leafRepackItems ( disassembledLeaf leaf,
ItemPointer  remaining 
)
static

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

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