PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
gin_private.h File Reference
#include "access/amapi.h"
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
#include "fmgr.h"
#include "storage/bufmgr.h"
#include "lib/rbtree.h"
Include dependency graph for gin_private.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  GinOptions
 
struct  GinState
 
struct  GinBtreeStack
 
struct  GinBtreeData
 
struct  GinBtreeEntryInsertData
 
struct  GinBtreeDataLeafInsertData
 
struct  GinScanKeyData
 
struct  GinScanEntryData
 
struct  GinScanOpaqueData
 
struct  GinEntryAccumulator
 
struct  BuildAccumulator
 
struct  GinTupleCollector
 

Macros

#define GIN_DEFAULT_USE_FASTUPDATE   true
 
#define GinGetUseFastUpdate(relation)
 
#define GinGetPendingListCleanupSize(relation)
 
#define GIN_UNLOCK   BUFFER_LOCK_UNLOCK
 
#define GIN_SHARE   BUFFER_LOCK_SHARE
 
#define GIN_EXCLUSIVE   BUFFER_LOCK_EXCLUSIVE
 

Typedefs

typedef struct GinOptions GinOptions
 
typedef struct GinState GinState
 
typedef struct GinBtreeStack GinBtreeStack
 
typedef struct GinBtreeDataGinBtree
 
typedef struct GinBtreeData GinBtreeData
 
typedef struct GinVacuumState GinVacuumState
 
typedef struct GinScanKeyDataGinScanKey
 
typedef struct GinScanEntryDataGinScanEntry
 
typedef struct GinScanKeyData GinScanKeyData
 
typedef struct GinScanEntryData GinScanEntryData
 
typedef struct GinScanOpaqueData GinScanOpaqueData
 
typedef GinScanOpaqueDataGinScanOpaque
 
typedef struct GinEntryAccumulator GinEntryAccumulator
 
typedef struct GinTupleCollector GinTupleCollector
 

Enumerations

enum  GinPlaceToPageRC { GPTP_NO_WORK, GPTP_INSERT, GPTP_SPLIT }
 

Functions

byteaginoptions (Datum reloptions, bool validate)
 
void initGinState (GinState *state, Relation index)
 
Buffer GinNewBuffer (Relation index)
 
void GinInitBuffer (Buffer b, uint32 f)
 
void GinInitPage (Page page, uint32 f, Size pageSize)
 
void GinInitMetabuffer (Buffer b)
 
int ginCompareEntries (GinState *ginstate, OffsetNumber attnum, Datum a, GinNullCategory categorya, Datum b, GinNullCategory categoryb)
 
int ginCompareAttEntries (GinState *ginstate, OffsetNumber attnuma, Datum a, GinNullCategory categorya, OffsetNumber attnumb, Datum b, GinNullCategory categoryb)
 
DatumginExtractEntries (GinState *ginstate, OffsetNumber attnum, Datum value, bool isNull, int32 *nentries, GinNullCategory **categories)
 
OffsetNumber gintuple_get_attrnum (GinState *ginstate, IndexTuple tuple)
 
Datum gintuple_get_key (GinState *ginstate, IndexTuple tuple, GinNullCategory *category)
 
IndexBuildResultginbuild (Relation heap, Relation index, struct IndexInfo *indexInfo)
 
void ginbuildempty (Relation index)
 
bool gininsert (Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, struct IndexInfo *indexInfo)
 
void ginEntryInsert (GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
 
GinBtreeStackginFindLeafPage (GinBtree btree, bool searchMode, Snapshot snapshot)
 
Buffer ginStepRight (Buffer buffer, Relation index, int lockmode)
 
void freeGinBtreeStack (GinBtreeStack *stack)
 
void ginInsertValue (GinBtree btree, GinBtreeStack *stack, void *insertdata, GinStatsData *buildStats)
 
IndexTuple GinFormTuple (GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, Pointer data, Size dataSize, int nipd, bool errorTooBig)
 
void ginPrepareEntryScan (GinBtree btree, OffsetNumber attnum, Datum key, GinNullCategory category, GinState *ginstate)
 
void ginEntryFillRoot (GinBtree btree, Page root, BlockNumber lblkno, Page lpage, BlockNumber rblkno, Page rpage)
 
ItemPointer ginReadTuple (GinState *ginstate, OffsetNumber attnum, IndexTuple itup, int *nitems)
 
ItemPointer GinDataLeafPageGetItems (Page page, int *nitems, ItemPointerData advancePast)
 
int GinDataLeafPageGetItemsToTbm (Page page, TIDBitmap *tbm)
 
BlockNumber createPostingTree (Relation index, ItemPointerData *items, uint32 nitems, GinStatsData *buildStats)
 
void GinDataPageAddPostingItem (Page page, PostingItem *data, OffsetNumber offset)
 
void GinPageDeletePostingItem (Page page, OffsetNumber offset)
 
void ginInsertItemPointers (Relation index, BlockNumber rootBlkno, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
 
GinBtreeStackginScanBeginPostingTree (GinBtree btree, Relation index, BlockNumber rootBlkno, Snapshot snapshot)
 
void ginDataFillRoot (GinBtree btree, Page root, BlockNumber lblkno, Page lpage, BlockNumber rblkno, Page rpage)
 
void ginPrepareDataScan (GinBtree btree, Relation index, BlockNumber rootBlkno)
 
void ginVacuumPostingTreeLeaf (Relation rel, Buffer buf, GinVacuumState *gvs)
 
IndexScanDesc ginbeginscan (Relation rel, int nkeys, int norderbys)
 
void ginendscan (IndexScanDesc scan)
 
void ginrescan (IndexScanDesc scan, ScanKey key, int nscankeys, ScanKey orderbys, int norderbys)
 
void ginNewScanKey (IndexScanDesc scan)
 
void ginFreeScanKeys (GinScanOpaque so)
 
int64 gingetbitmap (IndexScanDesc scan, TIDBitmap *tbm)
 
void ginInitConsistentFunction (GinState *ginstate, GinScanKey key)
 
IndexBulkDeleteResultginbulkdelete (IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
 
IndexBulkDeleteResultginvacuumcleanup (IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
 
ItemPointer ginVacuumItemPointers (GinVacuumState *gvs, ItemPointerData *items, int nitem, int *nremaining)
 
bool ginvalidate (Oid opclassoid)
 
void ginInitBA (BuildAccumulator *accum)
 
void ginInsertBAEntries (BuildAccumulator *accum, ItemPointer heapptr, OffsetNumber attnum, Datum *entries, GinNullCategory *categories, int32 nentries)
 
void ginBeginBAScan (BuildAccumulator *accum)
 
ItemPointerDataginGetBAEntry (BuildAccumulator *accum, OffsetNumber *attnum, Datum *key, GinNullCategory *category, uint32 *n)
 
void ginHeapTupleFastInsert (GinState *ginstate, GinTupleCollector *collector)
 
void ginHeapTupleFastCollect (GinState *ginstate, GinTupleCollector *collector, OffsetNumber attnum, Datum value, bool isNull, ItemPointer ht_ctid)
 
void ginInsertCleanup (GinState *ginstate, bool full_clean, bool fill_fsm, IndexBulkDeleteResult *stats)
 
GinPostingListginCompressPostingList (const ItemPointer ptrs, int nptrs, int maxsize, int *nwritten)
 
int ginPostingListDecodeAllSegmentsToTbm (GinPostingList *ptr, int totalsize, TIDBitmap *tbm)
 
ItemPointer ginPostingListDecodeAllSegments (GinPostingList *ptr, int len, int *ndecoded)
 
ItemPointer ginPostingListDecode (GinPostingList *ptr, int *ndecoded)
 
ItemPointer ginMergeItemPointers (ItemPointerData *a, uint32 na, ItemPointerData *b, uint32 nb, int *nmerged)
 
static int ginCompareItemPointers (ItemPointer a, ItemPointer b)
 

Macro Definition Documentation

#define GIN_DEFAULT_USE_FASTUPDATE   true

Definition at line 31 of file gin_private.h.

#define GinGetPendingListCleanupSize (   relation)
Value:
((relation)->rd_options && \
((GinOptions *) (relation)->rd_options)->pendingListCleanupSize != -1 ? \
((GinOptions *) (relation)->rd_options)->pendingListCleanupSize : \
int gin_pending_list_limit
Definition: ginfast.c:37

Definition at line 35 of file gin_private.h.

Referenced by ginHeapTupleFastInsert().

#define GinGetUseFastUpdate (   relation)
Value:
((relation)->rd_options ? \
((GinOptions *) (relation)->rd_options)->useFastUpdate : GIN_DEFAULT_USE_FASTUPDATE)
#define GIN_DEFAULT_USE_FASTUPDATE
Definition: gin_private.h:31

Definition at line 32 of file gin_private.h.

Referenced by gininsert().

Typedef Documentation

Definition at line 133 of file gin_private.h.

Definition at line 258 of file gin_private.h.

Definition at line 256 of file gin_private.h.

Definition at line 235 of file gin_private.h.

Enumeration Type Documentation

Enumerator
GPTP_NO_WORK 
GPTP_INSERT 
GPTP_SPLIT 

Definition at line 136 of file gin_private.h.

Function Documentation

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

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

1763 {
1764  BlockNumber blkno;
1765  Buffer buffer;
1766  Page tmppage;
1767  Page page;
1768  Pointer ptr;
1769  int nrootitems;
1770  int rootsize;
1771 
1772  /* Construct the new root page in memory first. */
1773  tmppage = (Page) palloc(BLCKSZ);
1774  GinInitPage(tmppage, GIN_DATA | GIN_LEAF | GIN_COMPRESSED, BLCKSZ);
1775  GinPageGetOpaque(tmppage)->rightlink = InvalidBlockNumber;
1776 
1777  /*
1778  * Write as many of the items to the root page as fit. In segments of max
1779  * GinPostingListSegmentMaxSize bytes each.
1780  */
1781  nrootitems = 0;
1782  rootsize = 0;
1783  ptr = (Pointer) GinDataLeafPageGetPostingList(tmppage);
1784  while (nrootitems < nitems)
1785  {
1786  GinPostingList *segment;
1787  int npacked;
1788  int segsize;
1789 
1790  segment = ginCompressPostingList(&items[nrootitems],
1791  nitems - nrootitems,
1793  &npacked);
1794  segsize = SizeOfGinPostingList(segment);
1795  if (rootsize + segsize > GinDataPageMaxDataSize)
1796  break;
1797 
1798  memcpy(ptr, segment, segsize);
1799  ptr += segsize;
1800  rootsize += segsize;
1801  nrootitems += npacked;
1802  pfree(segment);
1803  }
1804  GinDataPageSetDataSize(tmppage, rootsize);
1805 
1806  /*
1807  * All set. Get a new physical page, and copy the in-memory page to it.
1808  */
1809  buffer = GinNewBuffer(index);
1810  page = BufferGetPage(buffer);
1811  blkno = BufferGetBlockNumber(buffer);
1812 
1814 
1815  PageRestoreTempPage(tmppage, page);
1816  MarkBufferDirty(buffer);
1817 
1818  if (RelationNeedsWAL(index))
1819  {
1820  XLogRecPtr recptr;
1822 
1823  data.size = rootsize;
1824 
1825  XLogBeginInsert();
1826  XLogRegisterData((char *) &data, sizeof(ginxlogCreatePostingTree));
1827 
1829  rootsize);
1831 
1832  recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_CREATE_PTREE);
1833  PageSetLSN(page, recptr);
1834  }
1835 
1836  UnlockReleaseBuffer(buffer);
1837 
1838  END_CRIT_SECTION();
1839 
1840  /* During index build, count the newly-added data page */
1841  if (buildStats)
1842  buildStats->nDataPages++;
1843 
1844  elog(DEBUG2, "created GIN posting tree with %d items", nrootitems);
1845 
1846  /*
1847  * Add any remaining TIDs to the newly-created posting tree.
1848  */
1849  if (nitems > nrootitems)
1850  {
1851  ginInsertItemPointers(index, blkno,
1852  items + nrootitems,
1853  nitems - nrootitems,
1854  buildStats);
1855  }
1856 
1857  return blkno;
1858 }
GinPostingList * ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize, int *nwritten)
Buffer GinNewBuffer(Relation index)
Definition: ginutil.c:287
#define GinPostingListSegmentMaxSize
Definition: gindatapage.c:33
void ginInsertItemPointers(Relation index, BlockNumber rootBlkno, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gindatapage.c:1887
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:407
void GinInitPage(Page page, uint32 f, Size pageSize)
Definition: ginutil.c:338
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1445
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
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:293
void pfree(void *pointer)
Definition: mcxt.c:992
char * Pointer
Definition: c.h:242
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
#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:262
BlockNumber nDataPages
Definition: gin.h:46
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidBlockNumber
Definition: block.h:33
#define RelationNeedsWAL(relation)
Definition: rel.h:502
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2588
#define GinDataPageMaxDataSize
Definition: ginblock.h:303
void * palloc(Size size)
Definition: mcxt.c:891
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:326
#define elog
Definition: elog.h:219
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define PageSetLSN(page, lsn)
Definition: bufpage.h:365
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
#define GIN_LEAF
Definition: ginblock.h:40
void freeGinBtreeStack ( GinBtreeStack stack)

Definition at line 193 of file ginbtree.c.

References GinBtreeStack::buffer, InvalidBuffer, GinBtreeStack::parent, pfree(), and ReleaseBuffer().

Referenced by entryLoadMoreItems(), ginEntryInsert(), ginFinishSplit(), ginInsertValue(), scanPostingTree(), and startScanEntry().

194 {
195  while (stack)
196  {
197  GinBtreeStack *tmp = stack->parent;
198 
199  if (stack->buffer != InvalidBuffer)
200  ReleaseBuffer(stack->buffer);
201 
202  pfree(stack);
203  stack = tmp;
204  }
205 }
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3292
void pfree(void *pointer)
Definition: mcxt.c:992
struct GinBtreeStack * parent
Definition: gin_private.h:130
void ginBeginBAScan ( BuildAccumulator accum)

Definition at line 256 of file ginbulk.c.

References LeftRightWalk, rb_begin_iterate(), BuildAccumulator::tree, and BuildAccumulator::tree_walk.

Referenced by ginbuild(), ginBuildCallback(), and ginInsertCleanup().

257 {
258  rb_begin_iterate(accum->tree, LeftRightWalk, &accum->tree_walk);
259 }
void rb_begin_iterate(RBTree *rb, RBOrderControl ctrl, RBTreeIterator *iter)
Definition: rbtree.c:855
RBTreeIterator tree_walk
Definition: gin_private.h:412
IndexScanDesc ginbeginscan ( Relation  rel,
int  nkeys,
int  norderbys 
)

Definition at line 25 of file ginscan.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, CurrentMemoryContext, GinScanOpaqueData::ginstate, IndexScanDescData::indexRelation, initGinState(), GinScanOpaqueData::keyCtx, GinScanOpaqueData::keys, GinScanOpaqueData::nkeys, NULL, IndexScanDescData::opaque, palloc(), RelationGetIndexScan(), and GinScanOpaqueData::tempCtx.

Referenced by ginhandler().

26 {
27  IndexScanDesc scan;
28  GinScanOpaque so;
29 
30  /* no order by operators allowed */
31  Assert(norderbys == 0);
32 
33  scan = RelationGetIndexScan(rel, nkeys, norderbys);
34 
35  /* allocate private workspace */
36  so = (GinScanOpaque) palloc(sizeof(GinScanOpaqueData));
37  so->keys = NULL;
38  so->nkeys = 0;
40  "Gin scan temporary context",
43  "Gin scan key context",
45  initGinState(&so->ginstate, scan->indexRelation);
46 
47  scan->opaque = so;
48 
49  return scan;
50 }
Relation indexRelation
Definition: relscan.h:89
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
GinScanOpaqueData * GinScanOpaque
Definition: gin_private.h:364
MemoryContext keyCtx
Definition: gin_private.h:359
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:86
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
GinScanKey keys
Definition: gin_private.h:352
MemoryContext tempCtx
Definition: gin_private.h:349
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void * palloc(Size size)
Definition: mcxt.c:891
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:78
IndexBuildResult* ginbuild ( Relation  heap,
Relation  index,
struct IndexInfo indexInfo 
)

Definition at line 311 of file gininsert.c.

References GinBuildState::accum, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), BufferGetPage, GinBuildState::buildStats, CHECK_FOR_INTERRUPTS, CurrentMemoryContext, elog, END_CRIT_SECTION, ERROR, GinBuildState::funcCtx, GIN_LEAF, ginBeginBAScan(), ginBuildCallback(), ginEntryInsert(), ginGetBAEntry(), ginInitBA(), GinInitBuffer(), GinInitMetabuffer(), GinNewBuffer(), GinBuildState::ginstate, BuildAccumulator::ginstate, ginUpdateStats(), IndexBuildResult::heap_tuples, IndexBuildResult::index_tuples, IndexBuildHeapScan(), GinBuildState::indtuples, initGinState(), sort-test::list, MarkBufferDirty(), MemoryContextDelete(), MemoryContextSwitchTo(), GinStatsData::nEntryPages, GinStatsData::nTotalPages, NULL, PageSetLSN, palloc(), REGBUF_WILL_INIT, RelationGetNumberOfBlocks, RelationGetRelationName, RelationNeedsWAL, START_CRIT_SECTION, GinBuildState::tmpCtx, UnlockReleaseBuffer(), XLOG_GIN_CREATE_INDEX, XLogBeginInsert(), XLogInsert(), and XLogRegisterBuffer().

Referenced by ginhandler().

312 {
313  IndexBuildResult *result;
314  double reltuples;
315  GinBuildState buildstate;
316  Buffer RootBuffer,
317  MetaBuffer;
319  Datum key;
320  GinNullCategory category;
321  uint32 nlist;
322  MemoryContext oldCtx;
323  OffsetNumber attnum;
324 
325  if (RelationGetNumberOfBlocks(index) != 0)
326  elog(ERROR, "index \"%s\" already contains data",
327  RelationGetRelationName(index));
328 
329  initGinState(&buildstate.ginstate, index);
330  buildstate.indtuples = 0;
331  memset(&buildstate.buildStats, 0, sizeof(GinStatsData));
332 
333  /* initialize the meta page */
334  MetaBuffer = GinNewBuffer(index);
335 
336  /* initialize the root page */
337  RootBuffer = GinNewBuffer(index);
338 
340  GinInitMetabuffer(MetaBuffer);
341  MarkBufferDirty(MetaBuffer);
342  GinInitBuffer(RootBuffer, GIN_LEAF);
343  MarkBufferDirty(RootBuffer);
344 
345  if (RelationNeedsWAL(index))
346  {
347  XLogRecPtr recptr;
348  Page page;
349 
350  XLogBeginInsert();
351  XLogRegisterBuffer(0, MetaBuffer, REGBUF_WILL_INIT);
352  XLogRegisterBuffer(1, RootBuffer, REGBUF_WILL_INIT);
353 
354  recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_CREATE_INDEX);
355 
356  page = BufferGetPage(RootBuffer);
357  PageSetLSN(page, recptr);
358 
359  page = BufferGetPage(MetaBuffer);
360  PageSetLSN(page, recptr);
361  }
362 
363  UnlockReleaseBuffer(MetaBuffer);
364  UnlockReleaseBuffer(RootBuffer);
366 
367  /* count the root as first entry page */
368  buildstate.buildStats.nEntryPages++;
369 
370  /*
371  * create a temporary memory context that is used to hold data not yet
372  * dumped out to the index
373  */
375  "Gin build temporary context",
377 
378  /*
379  * create a temporary memory context that is used for calling
380  * ginExtractEntries(), and can be reset after each tuple
381  */
383  "Gin build temporary context for user-defined function",
385 
386  buildstate.accum.ginstate = &buildstate.ginstate;
387  ginInitBA(&buildstate.accum);
388 
389  /*
390  * Do the heap scan. We disallow sync scan here because dataPlaceToPage
391  * prefers to receive tuples in TID order.
392  */
393  reltuples = IndexBuildHeapScan(heap, index, indexInfo, false,
394  ginBuildCallback, (void *) &buildstate);
395 
396  /* dump remaining entries to the index */
397  oldCtx = MemoryContextSwitchTo(buildstate.tmpCtx);
398  ginBeginBAScan(&buildstate.accum);
399  while ((list = ginGetBAEntry(&buildstate.accum,
400  &attnum, &key, &category, &nlist)) != NULL)
401  {
402  /* there could be many entries, so be willing to abort here */
404  ginEntryInsert(&buildstate.ginstate, attnum, key, category,
405  list, nlist, &buildstate.buildStats);
406  }
407  MemoryContextSwitchTo(oldCtx);
408 
409  MemoryContextDelete(buildstate.funcCtx);
410  MemoryContextDelete(buildstate.tmpCtx);
411 
412  /*
413  * Update metapage stats
414  */
416  ginUpdateStats(index, &buildstate.buildStats);
417 
418  /*
419  * Return statistics
420  */
421  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
422 
423  result->heap_tuples = reltuples;
424  result->index_tuples = buildstate.indtuples;
425 
426  return result;
427 }
BlockNumber nEntryPages
Definition: gin.h:45
Buffer GinNewBuffer(Relation index)
Definition: ginutil.c:287
void GinInitMetabuffer(Buffer b)
Definition: ginutil.c:357
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1445
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
GinStatsData buildStats
Definition: gininsert.c:33
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
double indtuples
Definition: gininsert.c:32
static void ginBuildCallback(Relation index, HeapTuple htup, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
Definition: gininsert.c:270
uint16 OffsetNumber
Definition: off.h:24
GinState ginstate
Definition: gininsert.c:31
void ginEntryInsert(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gininsert.c:177
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
#define ERROR
Definition: elog.h:43
signed char GinNullCategory
Definition: ginblock.h:190
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
void ginUpdateStats(Relation index, const GinStatsData *stats)
Definition: ginutil.c:659
BuildAccumulator accum
Definition: gininsert.c:36
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:86
#define RelationGetRelationName(relation)
Definition: rel.h:433
unsigned int uint32
Definition: c.h:265
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
GinState * ginstate
Definition: gin_private.h:407
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define XLOG_GIN_CREATE_INDEX
Definition: ginxlog.h:19
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
uintptr_t Datum
Definition: postgres.h:374
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void GinInitBuffer(Buffer b, uint32 f)
Definition: ginutil.c:351
BlockNumber nTotalPages
Definition: gin.h:44
ItemPointerData * ginGetBAEntry(BuildAccumulator *accum, OffsetNumber *attnum, Datum *key, GinNullCategory *category, uint32 *n)
Definition: ginbulk.c:267
#define RelationNeedsWAL(relation)
Definition: rel.h:502
void ginBeginBAScan(BuildAccumulator *accum)
Definition: ginbulk.c:256
tuple list
Definition: sort-test.py:11
double IndexBuildHeapScan(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool allow_sync, IndexBuildCallback callback, void *callback_state)
Definition: index.c:2169
void * palloc(Size size)
Definition: mcxt.c:891
MemoryContext tmpCtx
Definition: gininsert.c:34
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:219
MemoryContext funcCtx
Definition: gininsert.c:35
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define PageSetLSN(page, lsn)
Definition: bufpage.h:365
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
double index_tuples
Definition: genam.h:33
double heap_tuples
Definition: genam.h:32
void ginInitBA(BuildAccumulator *accum)
Definition: ginbulk.c:109
#define GIN_LEAF
Definition: ginblock.h:40
void ginbuildempty ( Relation  index)

Definition at line 433 of file gininsert.c.

References BUFFER_LOCK_EXCLUSIVE, END_CRIT_SECTION, GIN_LEAF, GinInitBuffer(), GinInitMetabuffer(), INIT_FORKNUM, LockBuffer(), log_newpage_buffer(), MarkBufferDirty(), NULL, P_NEW, RBM_NORMAL, ReadBufferExtended(), START_CRIT_SECTION, and UnlockReleaseBuffer().

Referenced by ginhandler().

434 {
435  Buffer RootBuffer,
436  MetaBuffer;
437 
438  /* An empty GIN index has two pages. */
439  MetaBuffer =
441  LockBuffer(MetaBuffer, BUFFER_LOCK_EXCLUSIVE);
442  RootBuffer =
444  LockBuffer(RootBuffer, BUFFER_LOCK_EXCLUSIVE);
445 
446  /* Initialize and xlog metabuffer and root buffer. */
448  GinInitMetabuffer(MetaBuffer);
449  MarkBufferDirty(MetaBuffer);
450  log_newpage_buffer(MetaBuffer, false);
451  GinInitBuffer(RootBuffer, GIN_LEAF);
452  MarkBufferDirty(RootBuffer);
453  log_newpage_buffer(RootBuffer, false);
455 
456  /* Unlock and release the buffers. */
457  UnlockReleaseBuffer(MetaBuffer);
458  UnlockReleaseBuffer(RootBuffer);
459 }
void GinInitMetabuffer(Buffer b)
Definition: ginutil.c:357
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1010
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1445
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
#define P_NEW
Definition: bufmgr.h:82
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
#define NULL
Definition: c.h:226
void GinInitBuffer(Buffer b, uint32 f)
Definition: ginutil.c:351
int Buffer
Definition: buf.h:23
#define GIN_LEAF
Definition: ginblock.h:40
IndexBulkDeleteResult* ginbulkdelete ( IndexVacuumInfo info,
IndexBulkDeleteResult stats,
IndexBulkDeleteCallback  callback,
void *  callback_state 
)

Definition at line 518 of file ginvacuum.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, DataPageDeleteStack::blkno, BufferGetPage, GinVacuumState::callback, callback(), GinVacuumState::callback_state, CurrentMemoryContext, END_CRIT_SECTION, FirstOffsetNumber, GIN_EXCLUSIVE, GIN_ROOT_BLKNO, GIN_SHARE, GIN_UNLOCK, GinGetDownlink, ginInsertCleanup(), GinPageGetOpaque, GinPageIsData, GinPageIsLeaf, GinVacuumState::ginstate, ginVacuumEntryPage(), ginVacuumPostingTree(), i, GinVacuumState::index, IndexVacuumInfo::index, initGinState(), InvalidBlockNumber, IsAutoVacuumWorkerProcess(), LockBuffer(), MAIN_FORKNUM, MarkBufferDirty(), MemoryContextDelete(), NULL, IndexBulkDeleteResult::num_index_tuples, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageRestoreTempPage(), palloc0(), RBM_NORMAL, ReadBufferExtended(), GinVacuumState::result, START_CRIT_SECTION, GinVacuumState::strategy, IndexVacuumInfo::strategy, GinVacuumState::tmpCxt, UnlockReleaseBuffer(), vacuum_delay_point(), and xlogVacuumPage().

Referenced by ginhandler().

520 {
521  Relation index = info->index;
522  BlockNumber blkno = GIN_ROOT_BLKNO;
523  GinVacuumState gvs;
524  Buffer buffer;
525  BlockNumber rootOfPostingTree[BLCKSZ / (sizeof(IndexTupleData) + sizeof(ItemId))];
526  uint32 nRoot;
527 
529  "Gin vacuum temporary context",
531  gvs.index = index;
532  gvs.callback = callback;
533  gvs.callback_state = callback_state;
534  gvs.strategy = info->strategy;
535  initGinState(&gvs.ginstate, index);
536 
537  /* first time through? */
538  if (stats == NULL)
539  {
540  /* Yes, so initialize stats to zeroes */
542 
543  /*
544  * and cleanup any pending inserts
545  */
547  false, stats);
548  }
549 
550  /* we'll re-count the tuples each time */
551  stats->num_index_tuples = 0;
552  gvs.result = stats;
553 
554  buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno,
555  RBM_NORMAL, info->strategy);
556 
557  /* find leaf page */
558  for (;;)
559  {
560  Page page = BufferGetPage(buffer);
561  IndexTuple itup;
562 
563  LockBuffer(buffer, GIN_SHARE);
564 
565  Assert(!GinPageIsData(page));
566 
567  if (GinPageIsLeaf(page))
568  {
569  LockBuffer(buffer, GIN_UNLOCK);
570  LockBuffer(buffer, GIN_EXCLUSIVE);
571 
572  if (blkno == GIN_ROOT_BLKNO && !GinPageIsLeaf(page))
573  {
574  LockBuffer(buffer, GIN_UNLOCK);
575  continue; /* check it one more */
576  }
577  break;
578  }
579 
581 
582  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, FirstOffsetNumber));
583  blkno = GinGetDownlink(itup);
584  Assert(blkno != InvalidBlockNumber);
585 
586  UnlockReleaseBuffer(buffer);
587  buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno,
588  RBM_NORMAL, info->strategy);
589  }
590 
591  /* right now we found leftmost page in entry's BTree */
592 
593  for (;;)
594  {
595  Page page = BufferGetPage(buffer);
596  Page resPage;
597  uint32 i;
598 
599  Assert(!GinPageIsData(page));
600 
601  resPage = ginVacuumEntryPage(&gvs, buffer, rootOfPostingTree, &nRoot);
602 
603  blkno = GinPageGetOpaque(page)->rightlink;
604 
605  if (resPage)
606  {
608  PageRestoreTempPage(resPage, page);
609  MarkBufferDirty(buffer);
610  xlogVacuumPage(gvs.index, buffer);
611  UnlockReleaseBuffer(buffer);
613  }
614  else
615  {
616  UnlockReleaseBuffer(buffer);
617  }
618 
620 
621  for (i = 0; i < nRoot; i++)
622  {
623  ginVacuumPostingTree(&gvs, rootOfPostingTree[i]);
625  }
626 
627  if (blkno == InvalidBlockNumber) /* rightmost page */
628  break;
629 
630  buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno,
631  RBM_NORMAL, info->strategy);
632  LockBuffer(buffer, GIN_EXCLUSIVE);
633  }
634 
636 
637  return gvs.result;
638 }
#define GIN_UNLOCK
Definition: gin_private.h:43
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:407
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1445
Relation index
Definition: ginvacuum.c:29
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
BufferAccessStrategy strategy
Definition: ginvacuum.c:34
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
BufferAccessStrategy strategy
Definition: genam.h:51
void ginInsertCleanup(GinState *ginstate, bool full_clean, bool fill_fsm, IndexBulkDeleteResult *stats)
Definition: ginfast.c:729
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
Relation index
Definition: genam.h:46
uint32 BlockNumber
Definition: block.h:31
#define GinPageGetOpaque(page)
Definition: ginblock.h:109
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
IndexBulkDeleteResult * result
Definition: ginvacuum.c:30
Definition: type.h:90
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
static void xlogVacuumPage(Relation index, Buffer buffer)
Definition: ginvacuum.c:89
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:49
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
static Page ginVacuumEntryPage(GinVacuumState *gvs, Buffer buffer, BlockNumber *roots, uint32 *nroot)
Definition: ginvacuum.c:409
IndexBulkDeleteCallback callback
Definition: ginvacuum.c:31
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:86
unsigned int uint32
Definition: c.h:265
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define GinPageIsLeaf(page)
Definition: ginblock.h:111
void * callback_state
Definition: ginvacuum.c:32
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:2988
#define GIN_SHARE
Definition: gin_private.h:44
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
void * palloc0(Size size)
Definition: mcxt.c:920
struct IndexTupleData IndexTupleData
#define GIN_EXCLUSIVE
Definition: gin_private.h:45
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
#define GinGetDownlink(itup)
Definition: ginblock.h:241
#define GinPageIsData(page)
Definition: ginblock.h:114
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define InvalidBlockNumber
Definition: block.h:33
int i
static void ginVacuumPostingTree(GinVacuumState *gvs, BlockNumber rootBlkno)
Definition: ginvacuum.c:371
void vacuum_delay_point(void)
Definition: vacuum.c:1515
MemoryContext tmpCxt
Definition: ginvacuum.c:35
double num_index_tuples
Definition: genam.h:76
int Buffer
Definition: buf.h:23
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
#define GIN_ROOT_BLKNO
Definition: ginblock.h:51
Pointer Page
Definition: bufpage.h:74
GinState ginstate
Definition: ginvacuum.c:33
int ginCompareAttEntries ( GinState ginstate,
OffsetNumber  attnuma,
Datum  a,
GinNullCategory  categorya,
OffsetNumber  attnumb,
Datum  b,
GinNullCategory  categoryb 
)

Definition at line 403 of file ginutil.c.

References ginCompareEntries().

Referenced by cmpEntryAccumulator(), entryIsMoveRight(), entryLocateEntry(), and entryLocateLeafEntry().

406 {
407  /* attribute number is the first sort key */
408  if (attnuma != attnumb)
409  return (attnuma < attnumb) ? -1 : 1;
410 
411  return ginCompareEntries(ginstate, attnuma, a, categorya, b, categoryb);
412 }
int ginCompareEntries(GinState *ginstate, OffsetNumber attnum, Datum a, GinNullCategory categorya, Datum b, GinNullCategory categoryb)
Definition: ginutil.c:381
int ginCompareEntries ( GinState ginstate,
OffsetNumber  attnum,
Datum  a,
GinNullCategory  categorya,
Datum  b,
GinNullCategory  categoryb 
)

Definition at line 381 of file ginutil.c.

References GinState::compareFn, DatumGetInt32, FunctionCall2Coll(), GIN_CAT_NORM_KEY, and GinState::supportCollation.

Referenced by collectMatchBitmap(), collectMatchesForHeapRow(), ginCompareAttEntries(), and ginFillScanEntry().

384 {
385  /* if not of same null category, sort by that first */
386  if (categorya != categoryb)
387  return (categorya < categoryb) ? -1 : 1;
388 
389  /* all null items in same category are equal */
390  if (categorya != GIN_CAT_NORM_KEY)
391  return 0;
392 
393  /* both not null, so safe to call the compareFn */
394  return DatumGetInt32(FunctionCall2Coll(&ginstate->compareFn[attnum - 1],
395  ginstate->supportCollation[attnum - 1],
396  a, b));
397 }
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gin_private.h:82
#define DatumGetInt32(X)
Definition: postgres.h:480
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1306
#define GIN_CAT_NORM_KEY
Definition: ginblock.h:192
FmgrInfo compareFn[INDEX_MAX_KEYS]
Definition: gin_private.h:73
static int ginCompareItemPointers ( ItemPointer  a,
ItemPointer  b 
)
inlinestatic

Definition at line 461 of file gin_private.h.

References BlockIdData::bi_hi, BlockIdData::bi_lo, ItemPointerData::ip_blkid, and ItemPointerData::ip_posid.

Referenced by addItemsToLeaf(), dataBeginPlaceToPageLeaf(), dataIsMoveRight(), dataLocateItem(), entryGetItem(), entryLoadMoreItems(), ginCombineData(), GinDataLeafPageGetItems(), ginMergeItemPointers(), ginPostingListDecodeAllSegments(), keyGetItem(), qsortCompareItemPointers(), and scanGetItem().

462 {
463  uint64 ia = (uint64) a->ip_blkid.bi_hi << 32 | (uint64) a->ip_blkid.bi_lo << 16 | a->ip_posid;
464  uint64 ib = (uint64) b->ip_blkid.bi_hi << 32 | (uint64) b->ip_blkid.bi_lo << 16 | b->ip_posid;
465 
466  if (ia == ib)
467  return 0;
468  else if (ia > ib)
469  return 1;
470  else
471  return -1;
472 }
uint16 bi_hi
Definition: block.h:55
BlockIdData ip_blkid
Definition: itemptr.h:38
OffsetNumber ip_posid
Definition: itemptr.h:39
uint16 bi_lo
Definition: block.h:56
GinPostingList* ginCompressPostingList ( const ItemPointer  ptrs,
int  nptrs,
int  maxsize,
int *  nwritten 
)

Definition at line 184 of file ginpostinglist.c.

References Assert, buf, GinPostingList::bytes, encode_varbyte(), GinPostingList::first, ginPostingListDecode(), i, itemptr_to_uint64(), GinPostingList::nbytes, offsetof, palloc(), pfree(), SHORTALIGN, SHORTALIGN_DOWN, SizeOfGinPostingList, and val.

Referenced by addItemPointersToLeafTuple(), buildFreshLeafTuple(), createPostingTree(), ginRedoRecompress(), ginVacuumEntryPage(), ginVacuumPostingTreeLeaf(), and leafRepackItems().

186 {
187  uint64 prev;
188  int totalpacked = 0;
189  int maxbytes;
190  GinPostingList *result;
191  unsigned char *ptr;
192  unsigned char *endptr;
193 
194  maxsize = SHORTALIGN_DOWN(maxsize);
195 
196  result = palloc(maxsize);
197 
198  maxbytes = maxsize - offsetof(GinPostingList, bytes);
199  Assert(maxbytes > 0);
200 
201  /* Store the first special item */
202  result->first = ipd[0];
203 
204  prev = itemptr_to_uint64(&result->first);
205 
206  ptr = result->bytes;
207  endptr = result->bytes + maxbytes;
208  for (totalpacked = 1; totalpacked < nipd; totalpacked++)
209  {
210  uint64 val = itemptr_to_uint64(&ipd[totalpacked]);
211  uint64 delta = val - prev;
212 
213  Assert(val > prev);
214 
215  if (endptr - ptr >= 6)
216  encode_varbyte(delta, &ptr);
217  else
218  {
219  /*
220  * There are less than 6 bytes left. Have to check if the next
221  * item fits in that space before writing it out.
222  */
223  unsigned char buf[6];
224  unsigned char *p = buf;
225 
226  encode_varbyte(delta, &p);
227  if (p - buf > (endptr - ptr))
228  break; /* output is full */
229 
230  memcpy(ptr, buf, p - buf);
231  ptr += (p - buf);
232  }
233  prev = val;
234  }
235  result->nbytes = ptr - result->bytes;
236 
237  /*
238  * If we wrote an odd number of bytes, zero out the padding byte at the
239  * end.
240  */
241  if (result->nbytes != SHORTALIGN(result->nbytes))
242  result->bytes[result->nbytes] = 0;
243 
244  if (nwritten)
245  *nwritten = totalpacked;
246 
247  Assert(SizeOfGinPostingList(result) <= maxsize);
248 
249  /*
250  * Check that the encoded segment decodes back to the original items.
251  */
252 #if defined (CHECK_ENCODING_ROUNDTRIP)
253  {
254  int ndecoded;
255  ItemPointer tmp = ginPostingListDecode(result, &ndecoded);
256  int i;
257 
258  Assert(ndecoded == totalpacked);
259  for (i = 0; i < ndecoded; i++)
260  Assert(memcmp(&tmp[i], &ipd[i], sizeof(ItemPointerData)) == 0);
261  pfree(tmp);
262  }
263 #endif
264 
265  return result;
266 }
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded)
#define SHORTALIGN_DOWN(LEN)
Definition: c.h:592
ItemPointerData first
Definition: ginblock.h:321
void pfree(void *pointer)
Definition: mcxt.c:992
unsigned char bytes[FLEXIBLE_ARRAY_MEMBER]
Definition: ginblock.h:323
static char * buf
Definition: pg_test_fsync.c:65
uint16 nbytes
Definition: ginblock.h:322
#define Assert(condition)
Definition: c.h:671
static uint64 itemptr_to_uint64(const ItemPointer iptr)
static void encode_varbyte(uint64 val, unsigned char **ptr)
void * palloc(Size size)
Definition: mcxt.c:891
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:326
int i
#define SHORTALIGN(LEN)
Definition: c.h:580
long val
Definition: informix.c:689
#define offsetof(type, field)
Definition: c.h:551
void ginDataFillRoot ( GinBtree  btree,
Page  root,
BlockNumber  lblkno,
Page  lpage,
BlockNumber  rblkno,
Page  rpage 
)

Definition at line 1339 of file gindatapage.c.

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

Referenced by ginPrepareDataScan().

1340 {
1341  PostingItem li,
1342  ri;
1343 
1344  li.key = *GinDataPageGetRightBound(lpage);
1345  PostingItemSetBlockNumber(&li, lblkno);
1347 
1348  ri.key = *GinDataPageGetRightBound(rpage);
1349  PostingItemSetBlockNumber(&ri, rblkno);
1351 }
#define PostingItemSetBlockNumber(pointer, blockNumber)
Definition: ginblock.h:179
void GinDataPageAddPostingItem(Page page, PostingItem *data, OffsetNumber offset)
Definition: gindatapage.c:376
ItemPointerData key
Definition: ginblock.h:173
#define InvalidOffsetNumber
Definition: off.h:26
#define GinDataPageGetRightBound(page)
Definition: ginblock.h:272
ItemPointer GinDataLeafPageGetItems ( Page  page,
int *  nitems,
ItemPointerData  advancePast 
)

Definition at line 134 of file gindatapage.c.

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

Referenced by entryLoadMoreItems(), and startScanEntry().

135 {
136  ItemPointer result;
137 
138  if (GinPageIsCompressed(page))
139  {
142  Pointer endptr = ((Pointer) seg) + len;
144 
145  /* Skip to the segment containing advancePast+1 */
146  if (ItemPointerIsValid(&advancePast))
147  {
148  next = GinNextPostingListSegment(seg);
149  while ((Pointer) next < endptr &&
150  ginCompareItemPointers(&next->first, &advancePast) <= 0)
151  {
152  seg = next;
153  next = GinNextPostingListSegment(seg);
154  }
155  len = endptr - (Pointer) seg;
156  }
157 
158  if (len > 0)
159  result = ginPostingListDecodeAllSegments(seg, len, nitems);
160  else
161  {
162  result = NULL;
163  *nitems = 0;
164  }
165  }
166  else
167  {
168  ItemPointer tmp = dataLeafPageGetUncompressed(page, nitems);
169 
170  result = palloc((*nitems) * sizeof(ItemPointerData));
171  memcpy(result, tmp, (*nitems) * sizeof(ItemPointerData));
172  }
173 
174  return result;
175 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:59
static int32 next
Definition: blutils.c:210
#define GinPageIsCompressed(page)
Definition: ginblock.h:120
ItemPointerData first
Definition: ginblock.h:321
char * Pointer
Definition: c.h:242
#define GinNextPostingListSegment(cur)
Definition: ginblock.h:327
#define GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:264
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:262
#define NULL
Definition: c.h:226
size_t Size
Definition: c.h:353
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:461
void * palloc(Size size)
Definition: mcxt.c:891
ItemPointer ginPostingListDecodeAllSegments(GinPostingList *segment, int len, int *ndecoded_out)
static ItemPointer dataLeafPageGetUncompressed(Page page, int *nitems)
Definition: gindatapage.c:210
int GinDataLeafPageGetItemsToTbm ( Page  page,
TIDBitmap tbm 
)

Definition at line 181 of file gindatapage.c.

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

Referenced by scanPostingTree().

182 {
183  ItemPointer uncompressed;
184  int nitems;
185 
186  if (GinPageIsCompressed(page))
187  {
190 
191  nitems = ginPostingListDecodeAllSegmentsToTbm(segment, len, tbm);
192  }
193  else
194  {
195  uncompressed = dataLeafPageGetUncompressed(page, &nitems);
196 
197  if (nitems > 0)
198  tbm_add_tuples(tbm, uncompressed, nitems, false);
199  }
200 
201  return nitems;
202 }
int ginPostingListDecodeAllSegmentsToTbm(GinPostingList *ptr, int len, TIDBitmap *tbm)
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:290
#define GinPageIsCompressed(page)
Definition: ginblock.h:120
#define GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:264
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:262
size_t Size
Definition: c.h:353
static ItemPointer dataLeafPageGetUncompressed(Page page, int *nitems)
Definition: gindatapage.c:210
void GinDataPageAddPostingItem ( Page  page,
PostingItem data,
OffsetNumber  offset 
)

Definition at line 376 of file gindatapage.c.

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

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

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

Definition at line 432 of file ginscan.c.

References ginFreeScanKeys(), GinScanOpaqueData::keyCtx, MemoryContextDelete(), IndexScanDescData::opaque, pfree(), and GinScanOpaqueData::tempCtx.

Referenced by ginhandler().

433 {
434  GinScanOpaque so = (GinScanOpaque) scan->opaque;
435 
436  ginFreeScanKeys(so);
437 
440 
441  pfree(so);
442 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
void pfree(void *pointer)
Definition: mcxt.c:992
GinScanOpaqueData * GinScanOpaque
Definition: gin_private.h:364
MemoryContext keyCtx
Definition: gin_private.h:359
MemoryContext tempCtx
Definition: gin_private.h:349
void ginFreeScanKeys(GinScanOpaque so)
Definition: ginscan.c:232
void ginEntryFillRoot ( GinBtree  btree,
Page  root,
BlockNumber  lblkno,
Page  lpage,
BlockNumber  rblkno,
Page  rpage 
)

Definition at line 721 of file ginentrypage.c.

References elog, ERROR, getRightMostTuple(), GinFormInteriorTuple(), IndexTupleSize, InvalidOffsetNumber, PageAddItem, and pfree().

Referenced by ginPrepareEntryScan().

724 {
725  IndexTuple itup;
726 
727  itup = GinFormInteriorTuple(getRightMostTuple(lpage), lpage, lblkno);
728  if (PageAddItem(root, (Item) itup, IndexTupleSize(itup), InvalidOffsetNumber, false, false) == InvalidOffsetNumber)
729  elog(ERROR, "failed to add item to index root page");
730  pfree(itup);
731 
732  itup = GinFormInteriorTuple(getRightMostTuple(rpage), rpage, rblkno);
733  if (PageAddItem(root, (Item) itup, IndexTupleSize(itup), InvalidOffsetNumber, false, false) == InvalidOffsetNumber)
734  elog(ERROR, "failed to add item to index root page");
735  pfree(itup);
736 }
Pointer Item
Definition: item.h:17
static IndexTuple getRightMostTuple(Page page)
Definition: ginentrypage.c:236
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:413
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
static IndexTuple GinFormInteriorTuple(IndexTuple itup, Page page, BlockNumber childblk)
Definition: ginentrypage.c:202
#define InvalidOffsetNumber
Definition: off.h:26
#define elog
Definition: elog.h:219
#define IndexTupleSize(itup)
Definition: itup.h:70
void ginEntryInsert ( GinState ginstate,
OffsetNumber  attnum,
Datum  key,
GinNullCategory  category,
ItemPointerData items,
uint32  nitem,
GinStatsData buildStats 
)

Definition at line 177 of file gininsert.c.

References addItemPointersToLeafTuple(), GinBtreeStack::buffer, BufferGetPage, buildFreshLeafTuple(), GinBtreeEntryInsertData::entry, FALSE, GinBtreeData::findItem, freeGinBtreeStack(), GIN_UNLOCK, ginFindLeafPage(), GinGetPostingTree, ginInsertItemPointers(), ginInsertValue(), GinIsPostingTree, ginPrepareEntryScan(), GinState::index, GinBtreeEntryInsertData::isDelete, LockBuffer(), GinStatsData::nEntries, NULL, GinBtreeStack::off, PageGetItem, PageGetItemId, pfree(), and TRUE.

Referenced by ginbuild(), ginBuildCallback(), ginHeapTupleInsert(), and ginInsertCleanup().

181 {
182  GinBtreeData btree;
183  GinBtreeEntryInsertData insertdata;
184  GinBtreeStack *stack;
185  IndexTuple itup;
186  Page page;
187 
188  insertdata.isDelete = FALSE;
189 
190  /* During index build, count the to-be-inserted entry */
191  if (buildStats)
192  buildStats->nEntries++;
193 
194  ginPrepareEntryScan(&btree, attnum, key, category, ginstate);
195 
196  stack = ginFindLeafPage(&btree, false, NULL);
197  page = BufferGetPage(stack->buffer);
198 
199  if (btree.findItem(&btree, stack))
200  {
201  /* found pre-existing entry */
202  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, stack->off));
203 
204  if (GinIsPostingTree(itup))
205  {
206  /* add entries to existing posting tree */
207  BlockNumber rootPostingTree = GinGetPostingTree(itup);
208 
209  /* release all stack */
210  LockBuffer(stack->buffer, GIN_UNLOCK);
211  freeGinBtreeStack(stack);
212 
213  /* insert into posting tree */
214  ginInsertItemPointers(ginstate->index, rootPostingTree,
215  items, nitem,
216  buildStats);
217  return;
218  }
219 
220  /* modify an existing leaf entry */
221  itup = addItemPointersToLeafTuple(ginstate, itup,
222  items, nitem, buildStats);
223 
224  insertdata.isDelete = TRUE;
225  }
226  else
227  {
228  /* no match, so construct a new leaf entry */
229  itup = buildFreshLeafTuple(ginstate, attnum, key, category,
230  items, nitem, buildStats);
231  }
232 
233  /* Insert the new or modified leaf tuple */
234  insertdata.entry = itup;
235  ginInsertValue(&btree, stack, &insertdata, buildStats);
236  pfree(itup);
237 }
#define GIN_UNLOCK
Definition: gin_private.h:43
void ginInsertItemPointers(Relation index, BlockNumber rootBlkno, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gindatapage.c:1887
Relation index
Definition: gin_private.h:53
OffsetNumber off
Definition: gin_private.h:126
int64 nEntries
Definition: gin.h:47
uint32 BlockNumber
Definition: block.h:31
GinBtreeStack * ginFindLeafPage(GinBtree btree, bool searchMode, Snapshot snapshot)
Definition: ginbtree.c:76
bool(* findItem)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:149
void pfree(void *pointer)
Definition: mcxt.c:992
#define FALSE
Definition: c.h:218
IndexTupleData * IndexTuple
Definition: itup.h:53
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
void ginPrepareEntryScan(GinBtree btree, OffsetNumber attnum, Datum key, GinNullCategory category, GinState *ginstate)
Definition: ginentrypage.c:745
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
void ginInsertValue(GinBtree btree, GinBtreeStack *stack, void *insertdata, GinStatsData *buildStats)
Definition: ginbtree.c:755
#define NULL
Definition: c.h:226
static IndexTuple addItemPointersToLeafTuple(GinState *ginstate, IndexTuple old, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gininsert.c:48
#define GinGetPostingTree(itup)
Definition: ginblock.h:217
#define GinIsPostingTree(itup)
Definition: ginblock.h:215
void freeGinBtreeStack(GinBtreeStack *stack)
Definition: ginbtree.c:193
#define TRUE
Definition: c.h:214
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
Pointer Page
Definition: bufpage.h:74
static IndexTuple buildFreshLeafTuple(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gininsert.c:127
Datum* ginExtractEntries ( GinState ginstate,
OffsetNumber  attnum,
Datum  value,
bool  isNull,
int32 nentries,
GinNullCategory **  categories 
)

Definition at line 476 of file ginutil.c.

References arg, cmpEntriesArg::cmpDatumFunc, cmpEntries(), cmpEntriesArg::collation, GinState::compareFn, keyEntryData::datum, DatumGetPointer, GinState::extractValueFn, FunctionCall3Coll(), GIN_CAT_EMPTY_ITEM, GIN_CAT_NULL_ITEM, cmpEntriesArg::haveDups, i, keyEntryData::isnull, NULL, palloc(), palloc0(), pfree(), PointerGetDatum, qsort_arg(), and GinState::supportCollation.

Referenced by ginHeapTupleBulkInsert(), ginHeapTupleFastCollect(), and ginHeapTupleInsert().

479 {
480  Datum *entries;
481  bool *nullFlags;
482  int32 i;
483 
484  /*
485  * We don't call the extractValueFn on a null item. Instead generate a
486  * placeholder.
487  */
488  if (isNull)
489  {
490  *nentries = 1;
491  entries = (Datum *) palloc(sizeof(Datum));
492  entries[0] = (Datum) 0;
493  *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
494  (*categories)[0] = GIN_CAT_NULL_ITEM;
495  return entries;
496  }
497 
498  /* OK, call the opclass's extractValueFn */
499  nullFlags = NULL; /* in case extractValue doesn't set it */
500  entries = (Datum *)
501  DatumGetPointer(FunctionCall3Coll(&ginstate->extractValueFn[attnum - 1],
502  ginstate->supportCollation[attnum - 1],
503  value,
504  PointerGetDatum(nentries),
505  PointerGetDatum(&nullFlags)));
506 
507  /*
508  * Generate a placeholder if the item contained no keys.
509  */
510  if (entries == NULL || *nentries <= 0)
511  {
512  *nentries = 1;
513  entries = (Datum *) palloc(sizeof(Datum));
514  entries[0] = (Datum) 0;
515  *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
516  (*categories)[0] = GIN_CAT_EMPTY_ITEM;
517  return entries;
518  }
519 
520  /*
521  * If the extractValueFn didn't create a nullFlags array, create one,
522  * assuming that everything's non-null. Otherwise, run through the array
523  * and make sure each value is exactly 0 or 1; this ensures binary
524  * compatibility with the GinNullCategory representation.
525  */
526  if (nullFlags == NULL)
527  nullFlags = (bool *) palloc0(*nentries * sizeof(bool));
528  else
529  {
530  for (i = 0; i < *nentries; i++)
531  nullFlags[i] = (nullFlags[i] ? true : false);
532  }
533  /* now we can use the nullFlags as category codes */
534  *categories = (GinNullCategory *) nullFlags;
535 
536  /*
537  * If there's more than one key, sort and unique-ify.
538  *
539  * XXX Using qsort here is notationally painful, and the overhead is
540  * pretty bad too. For small numbers of keys it'd likely be better to use
541  * a simple insertion sort.
542  */
543  if (*nentries > 1)
544  {
545  keyEntryData *keydata;
547 
548  keydata = (keyEntryData *) palloc(*nentries * sizeof(keyEntryData));
549  for (i = 0; i < *nentries; i++)
550  {
551  keydata[i].datum = entries[i];
552  keydata[i].isnull = nullFlags[i];
553  }
554 
555  arg.cmpDatumFunc = &ginstate->compareFn[attnum - 1];
556  arg.collation = ginstate->supportCollation[attnum - 1];
557  arg.haveDups = false;
558  qsort_arg(keydata, *nentries, sizeof(keyEntryData),
559  cmpEntries, (void *) &arg);
560 
561  if (arg.haveDups)
562  {
563  /* there are duplicates, must get rid of 'em */
564  int32 j;
565 
566  entries[0] = keydata[0].datum;
567  nullFlags[0] = keydata[0].isnull;
568  j = 1;
569  for (i = 1; i < *nentries; i++)
570  {
571  if (cmpEntries(&keydata[i - 1], &keydata[i], &arg) != 0)
572  {
573  entries[j] = keydata[i].datum;
574  nullFlags[j] = keydata[i].isnull;
575  j++;
576  }
577  }
578  *nentries = j;
579  }
580  else
581  {
582  /* easy, no duplicates */
583  for (i = 0; i < *nentries; i++)
584  {
585  entries[i] = keydata[i].datum;
586  nullFlags[i] = keydata[i].isnull;
587  }
588  }
589 
590  pfree(keydata);
591  }
592 
593  return entries;
594 }
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gin_private.h:82
static struct @76 value
FmgrInfo * cmpDatumFunc
Definition: ginutil.c:430
bool isnull
Definition: ginutil.c:425
#define PointerGetDatum(X)
Definition: postgres.h:564
FmgrInfo extractValueFn[INDEX_MAX_KEYS]
Definition: gin_private.h:74
Datum datum
Definition: ginutil.c:424
signed int int32
Definition: c.h:253
void pfree(void *pointer)
Definition: mcxt.c:992
signed char GinNullCategory
Definition: ginblock.h:190
bool haveDups
Definition: ginutil.c:432
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
Definition: qsort_arg.c:113
void * palloc0(Size size)
Definition: mcxt.c:920
uintptr_t Datum
Definition: postgres.h:374
#define GIN_CAT_NULL_ITEM
Definition: ginblock.h:195
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1328
#define NULL
Definition: c.h:226
Oid collation
Definition: ginutil.c:431
#define DatumGetPointer(X)
Definition: postgres.h:557
void * palloc(Size size)
Definition: mcxt.c:891
int i
void * arg
#define GIN_CAT_EMPTY_ITEM
Definition: ginblock.h:194
static int cmpEntries(const void *a, const void *b, void *arg)
Definition: ginutil.c:436
FmgrInfo compareFn[INDEX_MAX_KEYS]
Definition: gin_private.h:73
GinBtreeStack* ginFindLeafPage ( GinBtree  btree,
bool  searchMode,
Snapshot  snapshot 
)

Definition at line 76 of file ginbtree.c.

References Assert, GinBtreeStack::blkno, GinBtreeStack::buffer, BufferGetPage, FALSE, GinBtreeData::findChildPage, GinBtreeData::fullScan, GIN_UNLOCK, ginFinishSplit(), GinPageGetOpaque, GinPageIsIncompleteSplit, GinPageIsLeaf, ginStepRight(), ginTraverseLock(), GinBtreeData::index, InvalidBlockNumber, InvalidOffsetNumber, GinBtreeData::isMoveRight, LockBuffer(), NULL, GinBtreeStack::off, palloc(), GinBtreeStack::parent, GinBtreeStack::predictNumber, ReadBuffer(), ReleaseAndReadBuffer(), GinBtreeData::rootBlkno, and TestForOldSnapshot().

Referenced by entryLoadMoreItems(), ginEntryInsert(), ginInsertItemPointers(), ginScanBeginPostingTree(), and startScanEntry().

77 {
78  GinBtreeStack *stack;
79 
80  stack = (GinBtreeStack *) palloc(sizeof(GinBtreeStack));
81  stack->blkno = btree->rootBlkno;
82  stack->buffer = ReadBuffer(btree->index, btree->rootBlkno);
83  stack->parent = NULL;
84  stack->predictNumber = 1;
85 
86  for (;;)
87  {
88  Page page;
89  BlockNumber child;
90  int access;
91 
92  stack->off = InvalidOffsetNumber;
93 
94  page = BufferGetPage(stack->buffer);
95  TestForOldSnapshot(snapshot, btree->index, page);
96 
97  access = ginTraverseLock(stack->buffer, searchMode);
98 
99  /*
100  * If we're going to modify the tree, finish any incomplete splits we
101  * encounter on the way.
102  */
103  if (!searchMode && GinPageIsIncompleteSplit(page))
104  ginFinishSplit(btree, stack, false, NULL);
105 
106  /*
107  * ok, page is correctly locked, we should check to move right ..,
108  * root never has a right link, so small optimization
109  */
110  while (btree->fullScan == FALSE && stack->blkno != btree->rootBlkno &&
111  btree->isMoveRight(btree, page))
112  {
113  BlockNumber rightlink = GinPageGetOpaque(page)->rightlink;
114 
115  if (rightlink == InvalidBlockNumber)
116  /* rightmost page */
117  break;
118 
119  stack->buffer = ginStepRight(stack->buffer, btree->index, access);
120  stack->blkno = rightlink;
121  page = BufferGetPage(stack->buffer);
122  TestForOldSnapshot(snapshot, btree->index, page);
123 
124  if (!searchMode && GinPageIsIncompleteSplit(page))
125  ginFinishSplit(btree, stack, false, NULL);
126  }
127 
128  if (GinPageIsLeaf(page)) /* we found, return locked page */
129  return stack;
130 
131  /* now we have correct buffer, try to find child */
132  child = btree->findChildPage(btree, stack);
133 
134  LockBuffer(stack->buffer, GIN_UNLOCK);
135  Assert(child != InvalidBlockNumber);
136  Assert(stack->blkno != child);
137 
138  if (searchMode)
139  {
140  /* in search mode we may forget path to leaf */
141  stack->blkno = child;
142  stack->buffer = ReleaseAndReadBuffer(stack->buffer, btree->index, stack->blkno);
143  }
144  else
145  {
146  GinBtreeStack *ptr = (GinBtreeStack *) palloc(sizeof(GinBtreeStack));
147 
148  ptr->parent = stack;
149  stack = ptr;
150  stack->blkno = child;
151  stack->buffer = ReadBuffer(btree->index, stack->blkno);
152  stack->predictNumber = 1;
153  }
154  }
155 }
#define GIN_UNLOCK
Definition: gin_private.h:43
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:265
OffsetNumber off
Definition: gin_private.h:126
#define GinPageIsIncompleteSplit(page)
Definition: ginblock.h:126
static void ginFinishSplit(GinBtree btree, GinBtreeStack *stack, bool freestack, GinStatsData *buildStats)
Definition: ginbtree.c:646
BlockNumber rootBlkno
Definition: gin_private.h:161
uint32 BlockNumber
Definition: block.h:31
#define GinPageGetOpaque(page)
Definition: ginblock.h:109
#define FALSE
Definition: c.h:218
static int ginTraverseLock(Buffer buffer, bool searchMode)
Definition: ginbtree.c:35
Buffer ginStepRight(Buffer buffer, Relation index, int lockmode)
Definition: ginbtree.c:165
BlockNumber(* findChildPage)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:146
#define GinPageIsLeaf(page)
Definition: ginblock.h:111
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
struct GinBtreeStack * parent
Definition: gin_private.h:130
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
Relation index
Definition: gin_private.h:160
#define InvalidOffsetNumber
Definition: off.h:26
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define InvalidBlockNumber
Definition: block.h:33
Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum)
Definition: bufmgr.c:1508
BlockNumber blkno
Definition: gin_private.h:124
uint32 predictNumber
Definition: gin_private.h:129
void * palloc(Size size)
Definition: mcxt.c:891
Pointer Page
Definition: bufpage.h:74
bool(* isMoveRight)(GinBtree, Page)
Definition: gin_private.h:148
IndexTuple GinFormTuple ( GinState ginstate,
OffsetNumber  attnum,
Datum  key,
GinNullCategory  category,
Pointer  data,
Size  dataSize,
int  nipd,
bool  errorTooBig 
)

Definition at line 45 of file ginentrypage.c.

References Assert, ereport, errcode(), errmsg(), ERROR, GIN_CAT_NORM_KEY, GinCategoryOffset, GinGetPosting, GinMaxItemSize, GinSetNPosting, GinSetNullCategory, GinSetPostingOffset, GinState::index, index_form_tuple(), INDEX_SIZE_MASK, IndexTupleHasNulls, IndexTupleSize, Max, MAXALIGN, NULL, GinState::oneCol, pfree(), RelationGetRelationName, repalloc(), SHORTALIGN, IndexTupleData::t_info, GinState::tupdesc, and UInt16GetDatum.

Referenced by addItemPointersToLeafTuple(), buildFreshLeafTuple(), ginHeapTupleFastCollect(), and ginVacuumEntryPage().

49 {
50  Datum datums[2];
51  bool isnull[2];
52  IndexTuple itup;
53  uint32 newsize;
54 
55  /* Build the basic tuple: optional column number, plus key datum */
56  if (ginstate->oneCol)
57  {
58  datums[0] = key;
59  isnull[0] = (category != GIN_CAT_NORM_KEY);
60  }
61  else
62  {
63  datums[0] = UInt16GetDatum(attnum);
64  isnull[0] = false;
65  datums[1] = key;
66  isnull[1] = (category != GIN_CAT_NORM_KEY);
67  }
68 
69  itup = index_form_tuple(ginstate->tupdesc[attnum - 1], datums, isnull);
70 
71  /*
72  * Determine and store offset to the posting list, making sure there is
73  * room for the category byte if needed.
74  *
75  * Note: because index_form_tuple MAXALIGNs the tuple size, there may well
76  * be some wasted pad space. Is it worth recomputing the data length to
77  * prevent that? That would also allow us to Assert that the real data
78  * doesn't overlap the GinNullCategory byte, which this code currently
79  * takes on faith.
80  */
81  newsize = IndexTupleSize(itup);
82 
83  if (IndexTupleHasNulls(itup))
84  {
85  uint32 minsize;
86 
87  Assert(category != GIN_CAT_NORM_KEY);
88  minsize = GinCategoryOffset(itup, ginstate) + sizeof(GinNullCategory);
89  newsize = Max(newsize, minsize);
90  }
91 
92  newsize = SHORTALIGN(newsize);
93 
94  GinSetPostingOffset(itup, newsize);
95  GinSetNPosting(itup, nipd);
96 
97  /*
98  * Add space needed for posting list, if any. Then check that the tuple
99  * won't be too big to store.
100  */
101  newsize += dataSize;
102 
103  newsize = MAXALIGN(newsize);
104 
105  if (newsize > GinMaxItemSize)
106  {
107  if (errorTooBig)
108  ereport(ERROR,
109  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
110  errmsg("index row size %zu exceeds maximum %zu for index \"%s\"",
111  (Size) newsize, (Size) GinMaxItemSize,
112  RelationGetRelationName(ginstate->index))));
113  pfree(itup);
114  return NULL;
115  }
116 
117  /*
118  * Resize tuple if needed
119  */
120  if (newsize != IndexTupleSize(itup))
121  {
122  itup = repalloc(itup, newsize);
123 
124  /*
125  * PostgreSQL 9.3 and earlier did not clear this new space, so we
126  * might find uninitialized padding when reading tuples from disk.
127  */
128  memset((char *) itup + IndexTupleSize(itup),
129  0, newsize - IndexTupleSize(itup));
130  /* set new size in tuple header */
131  itup->t_info &= ~INDEX_SIZE_MASK;
132  itup->t_info |= newsize;
133  }
134 
135  /*
136  * Copy in the posting list, if provided
137  */
138  if (data)
139  {
140  char *ptr = GinGetPosting(itup);
141 
142  memcpy(ptr, data, dataSize);
143  }
144 
145  /*
146  * Insert category byte, if needed
147  */
148  if (category != GIN_CAT_NORM_KEY)
149  {
150  Assert(IndexTupleHasNulls(itup));
151  GinSetNullCategory(itup, ginstate, category);
152  }
153  return itup;
154 }
#define GinSetNPosting(itup, n)
Definition: ginblock.h:213
Relation index
Definition: gin_private.h:53
#define INDEX_SIZE_MASK
Definition: itup.h:65
int errcode(int sqlerrcode)
Definition: elog.c:575
#define IndexTupleHasNulls(itup)
Definition: itup.h:72
#define GinGetPosting(itup)
Definition: ginblock.h:222
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:37
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
signed char GinNullCategory
Definition: ginblock.h:190
#define GinSetPostingOffset(itup, n)
Definition: ginblock.h:221
#define GIN_CAT_NORM_KEY
Definition: ginblock.h:192
#define RelationGetRelationName(relation)
Definition: rel.h:433
unsigned int uint32
Definition: c.h:265
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:374
TupleDesc tupdesc[INDEX_MAX_KEYS]
Definition: gin_private.h:68
#define Max(x, y)
Definition: c.h:796
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define GinSetNullCategory(itup, ginstate, c)
Definition: ginblock.h:206
size_t Size
Definition: c.h:353
#define MAXALIGN(LEN)
Definition: c.h:584
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define GinMaxItemSize
Definition: ginblock.h:232
unsigned short t_info
Definition: itup.h:49
#define SHORTALIGN(LEN)
Definition: c.h:580
bool oneCol
Definition: gin_private.h:54
#define GinCategoryOffset(itup, ginstate)
Definition: ginblock.h:201
#define UInt16GetDatum(X)
Definition: postgres.h:473
#define IndexTupleSize(itup)
Definition: itup.h:70
void ginFreeScanKeys ( GinScanOpaque  so)

Definition at line 232 of file ginscan.c.

References GinScanEntryData::buffer, GinScanOpaqueData::entries, i, InvalidBuffer, GinScanOpaqueData::keyCtx, GinScanOpaqueData::keys, GinScanEntryData::list, GinScanEntryData::matchBitmap, GinScanEntryData::matchIterator, MemoryContextResetAndDeleteChildren, GinScanOpaqueData::nkeys, NULL, pfree(), ReleaseBuffer(), tbm_end_iterate(), tbm_free(), and GinScanOpaqueData::totalentries.

Referenced by ginendscan(), gingetbitmap(), and ginrescan().

233 {
234  uint32 i;
235 
236  if (so->keys == NULL)
237  return;
238 
239  for (i = 0; i < so->totalentries; i++)
240  {
241  GinScanEntry entry = so->entries[i];
242 
243  if (entry->buffer != InvalidBuffer)
244  ReleaseBuffer(entry->buffer);
245  if (entry->list)
246  pfree(entry->list);
247  if (entry->matchIterator)
249  if (entry->matchBitmap)
250  tbm_free(entry->matchBitmap);
251  }
252 
254 
255  so->keys = NULL;
256  so->nkeys = 0;
257  so->entries = NULL;
258  so->totalentries = 0;
259 }
void tbm_end_iterate(TBMIterator *iterator)
Definition: tidbitmap.c:787
TBMIterator * matchIterator
Definition: gin_private.h:333
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3292
ItemPointerData * list
Definition: gin_private.h:337
void pfree(void *pointer)
Definition: mcxt.c:992
void tbm_free(TIDBitmap *tbm)
Definition: tidbitmap.c:272
MemoryContext keyCtx
Definition: gin_private.h:359
unsigned int uint32
Definition: c.h:265
TIDBitmap * matchBitmap
Definition: gin_private.h:332
GinScanKey keys
Definition: gin_private.h:352
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:88
#define NULL
Definition: c.h:226
GinScanEntry * entries
Definition: gin_private.h:355
int i
ItemPointerData* ginGetBAEntry ( BuildAccumulator accum,
OffsetNumber attnum,
Datum key,
GinNullCategory category,
uint32 n 
)

Definition at line 267 of file ginbulk.c.

References Assert, GinEntryAccumulator::attnum, GinEntryAccumulator::category, GinEntryAccumulator::count, GinEntryAccumulator::key, sort-test::list, GinEntryAccumulator::list, NULL, qsort, qsortCompareItemPointers(), rb_iterate(), GinEntryAccumulator::shouldSort, and BuildAccumulator::tree_walk.

Referenced by ginbuild(), ginBuildCallback(), and ginInsertCleanup().

270 {
271  GinEntryAccumulator *entry;
273 
274  entry = (GinEntryAccumulator *) rb_iterate(&accum->tree_walk);
275 
276  if (entry == NULL)
277  return NULL; /* no more entries */
278 
279  *attnum = entry->attnum;
280  *key = entry->key;
281  *category = entry->category;
282  list = entry->list;
283  *n = entry->count;
284 
285  Assert(list != NULL && entry->count > 0);
286 
287  if (entry->shouldSort && entry->count > 1)
288  qsort(list, entry->count, sizeof(ItemPointerData),
290 
291  return list;
292 }
OffsetNumber attnum
Definition: gin_private.h:398
RBNode * rb_iterate(RBTreeIterator *iter)
Definition: rbtree.c:886
GinNullCategory category
Definition: gin_private.h:397
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static int qsortCompareItemPointers(const void *a, const void *b)
Definition: ginbulk.c:245
tuple list
Definition: sort-test.py:11
RBTreeIterator tree_walk
Definition: gin_private.h:412
#define qsort(a, b, c, d)
Definition: port.h:440
ItemPointerData * list
Definition: gin_private.h:400
int64 gingetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

Definition at line 1790 of file ginget.c.

References CHECK_FOR_INTERRUPTS, ginFreeScanKeys(), GinIsVoidRes, ginNewScanKey(), ItemPointerGetBlockNumber, ItemPointerIsLossyPage, ItemPointerSetMin, IndexScanDescData::opaque, scanGetItem(), scanPendingInsert(), startScan(), tbm_add_page(), and tbm_add_tuples().

Referenced by ginhandler().

1791 {
1792  GinScanOpaque so = (GinScanOpaque) scan->opaque;
1793  int64 ntids;
1794  ItemPointerData iptr;
1795  bool recheck;
1796 
1797  /*
1798  * Set up the scan keys, and check for unsatisfiable query.
1799  */
1800  ginFreeScanKeys(so); /* there should be no keys yet, but just to be
1801  * sure */
1802  ginNewScanKey(scan);
1803 
1804  if (GinIsVoidRes(scan))
1805  return 0;
1806 
1807  ntids = 0;
1808 
1809  /*
1810  * First, scan the pending list and collect any matching entries into the
1811  * bitmap. After we scan a pending item, some other backend could post it
1812  * into the main index, and so we might visit it a second time during the
1813  * main scan. This is okay because we'll just re-set the same bit in the
1814  * bitmap. (The possibility of duplicate visits is a major reason why GIN
1815  * can't support the amgettuple API, however.) Note that it would not do
1816  * to scan the main index before the pending list, since concurrent
1817  * cleanup could then make us miss entries entirely.
1818  */
1819  scanPendingInsert(scan, tbm, &ntids);
1820 
1821  /*
1822  * Now scan the main index.
1823  */
1824  startScan(scan);
1825 
1826  ItemPointerSetMin(&iptr);
1827 
1828  for (;;)
1829  {
1831 
1832  if (!scanGetItem(scan, iptr, &iptr, &recheck))
1833  break;
1834 
1835  if (ItemPointerIsLossyPage(&iptr))
1837  else
1838  tbm_add_tuples(tbm, &iptr, 1, recheck);
1839  ntids++;
1840  }
1841 
1842  return ntids;
1843 }
static void scanPendingInsert(IndexScanDesc scan, TIDBitmap *tbm, int64 *ntids)
Definition: ginget.c:1701
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:290
#define ItemPointerIsLossyPage(p)
Definition: ginblock.h:162
static void startScan(IndexScanDesc scan)
Definition: ginget.c:536
GinScanOpaqueData * GinScanOpaque
Definition: gin_private.h:364
#define ItemPointerSetMin(p)
Definition: ginblock.h:150
static bool scanGetItem(IndexScanDesc scan, ItemPointerData advancePast, ItemPointerData *item, bool *recheck)
Definition: ginget.c:1182
void ginNewScanKey(IndexScanDesc scan)
Definition: ginscan.c:262
#define GinIsVoidRes(s)
Definition: ginget.c:1787
void tbm_add_page(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:356
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:66
void ginFreeScanKeys(GinScanOpaque so)
Definition: ginscan.c:232
void ginHeapTupleFastCollect ( GinState ginstate,
GinTupleCollector collector,
OffsetNumber  attnum,
Datum  value,
bool  isNull,
ItemPointer  ht_ctid 
)

Definition at line 456 of file ginfast.c.

References ginExtractEntries(), GinFormTuple(), i, IndexTupleSize, GinTupleCollector::lentuples, tupleDesc::natts, GinTupleCollector::ntuples, NULL, GinState::origTupdesc, palloc(), repalloc(), GinTupleCollector::sumsize, IndexTupleData::t_tid, and GinTupleCollector::tuples.

Referenced by gininsert().

460 {
461  Datum *entries;
462  GinNullCategory *categories;
463  int32 i,
464  nentries;
465 
466  /*
467  * Extract the key values that need to be inserted in the index
468  */
469  entries = ginExtractEntries(ginstate, attnum, value, isNull,
470  &nentries, &categories);
471 
472  /*
473  * Allocate/reallocate memory for storing collected tuples
474  */
475  if (collector->tuples == NULL)
476  {
477  collector->lentuples = nentries * ginstate->origTupdesc->natts;
478  collector->tuples = (IndexTuple *) palloc(sizeof(IndexTuple) * collector->lentuples);
479  }
480 
481  while (collector->ntuples + nentries > collector->lentuples)
482  {
483  collector->lentuples *= 2;
484  collector->tuples = (IndexTuple *) repalloc(collector->tuples,
485  sizeof(IndexTuple) * collector->lentuples);
486  }
487 
488  /*
489  * Build an index tuple for each key value, and add to array. In pending
490  * tuples we just stick the heap TID into t_tid.
491  */
492  for (i = 0; i < nentries; i++)
493  {
494  IndexTuple itup;
495 
496  itup = GinFormTuple(ginstate, attnum, entries[i], categories[i],
497  NULL, 0, 0, true);
498  itup->t_tid = *ht_ctid;
499  collector->tuples[collector->ntuples++] = itup;
500  collector->sumsize += IndexTupleSize(itup);
501  }
502 }
static struct @76 value
ItemPointerData t_tid
Definition: itup.h:37
int natts
Definition: tupdesc.h:73
signed int int32
Definition: c.h:253
signed char GinNullCategory
Definition: ginblock.h:190
IndexTuple * tuples
Definition: gin_private.h:429
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
IndexTuple GinFormTuple(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, Pointer data, Size dataSize, int nipd, bool errorTooBig)
Definition: ginentrypage.c:45
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
void * palloc(Size size)
Definition: mcxt.c:891
int i
TupleDesc origTupdesc
Definition: gin_private.h:67
Datum * ginExtractEntries(GinState *ginstate, OffsetNumber attnum, Datum value, bool isNull, int32 *nentries, GinNullCategory **categories)
Definition: ginutil.c:476
#define IndexTupleSize(itup)
Definition: itup.h:70
void ginHeapTupleFastInsert ( GinState ginstate,
GinTupleCollector collector 
)

Definition at line 222 of file ginfast.c.

References Assert, BufferGetPage, elog, END_CRIT_SECTION, ERROR, FirstOffsetNumber, GIN_EXCLUSIVE, GIN_METAPAGE_BLKNO, GIN_PAGE_FREESIZE, GIN_UNLOCK, GinGetPendingListCleanupSize, ginInsertCleanup(), GinListPageSize, GinPageGetMeta, GinPageGetOpaque, GinMetaPageData::head, i, GinState::index, IndexTupleSize, InvalidBlockNumber, InvalidBuffer, InvalidOffsetNumber, LockBuffer(), makeSublist(), MarkBufferDirty(), ginxlogUpdateMeta::metadata, ginxlogUpdateMeta::newRightlink, ginxlogUpdateMeta::node, GinMetaPageData::nPendingHeapTuples, GinMetaPageData::nPendingPages, ginxlogUpdateMeta::ntuples, GinTupleCollector::ntuples, NULL, OffsetNumberNext, PageAddItem, PageGetExactFreeSpace(), PageGetMaxOffsetNumber, PageIsEmpty, PageSetLSN, palloc(), ginxlogUpdateMeta::prevTail, RelationData::rd_node, ReadBuffer(), REGBUF_STANDARD, REGBUF_WILL_INIT, RelationGetRelationName, RelationNeedsWAL, START_CRIT_SECTION, GinTupleCollector::sumsize, GinMetaPageData::tail, GinMetaPageData::tailFreeSize, GinTupleCollector::tuples, UnlockReleaseBuffer(), XLOG_GIN_UPDATE_META_PAGE, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by gininsert().

223 {
224  Relation index = ginstate->index;
225  Buffer metabuffer;
226  Page metapage;
227  GinMetaPageData *metadata = NULL;
228  Buffer buffer = InvalidBuffer;
229  Page page = NULL;
230  ginxlogUpdateMeta data;
231  bool separateList = false;
232  bool needCleanup = false;
233  int cleanupSize;
234  bool needWal;
235 
236  if (collector->ntuples == 0)
237  return;
238 
239  needWal = RelationNeedsWAL(index);
240 
241  data.node = index->rd_node;
242  data.ntuples = 0;
244 
245  metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
246  metapage = BufferGetPage(metabuffer);
247 
248  if (collector->sumsize + collector->ntuples * sizeof(ItemIdData) > GinListPageSize)
249  {
250  /*
251  * Total size is greater than one page => make sublist
252  */
253  separateList = true;
254  }
255  else
256  {
257  LockBuffer(metabuffer, GIN_EXCLUSIVE);
258  metadata = GinPageGetMeta(metapage);
259 
260  if (metadata->head == InvalidBlockNumber ||
261  collector->sumsize + collector->ntuples * sizeof(ItemIdData) > metadata->tailFreeSize)
262  {
263  /*
264  * Pending list is empty or total size is greater than freespace
265  * on tail page => make sublist
266  *
267  * We unlock metabuffer to keep high concurrency
268  */
269  separateList = true;
270  LockBuffer(metabuffer, GIN_UNLOCK);
271  }
272  }
273 
274  if (separateList)
275  {
276  /*
277  * We should make sublist separately and append it to the tail
278  */
279  GinMetaPageData sublist;
280 
281  memset(&sublist, 0, sizeof(GinMetaPageData));
282  makeSublist(index, collector->tuples, collector->ntuples, &sublist);
283 
284  if (needWal)
285  XLogBeginInsert();
286 
287  /*
288  * metapage was unlocked, see above
289  */
290  LockBuffer(metabuffer, GIN_EXCLUSIVE);
291  metadata = GinPageGetMeta(metapage);
292 
293  if (metadata->head == InvalidBlockNumber)
294  {
295  /*
296  * Main list is empty, so just insert sublist as main list
297  */
299 
300  metadata->head = sublist.head;
301  metadata->tail = sublist.tail;
302  metadata->tailFreeSize = sublist.tailFreeSize;
303 
304  metadata->nPendingPages = sublist.nPendingPages;
305  metadata->nPendingHeapTuples = sublist.nPendingHeapTuples;
306  }
307  else
308  {
309  /*
310  * Merge lists
311  */
312  data.prevTail = metadata->tail;
313  data.newRightlink = sublist.head;
314 
315  buffer = ReadBuffer(index, metadata->tail);
316  LockBuffer(buffer, GIN_EXCLUSIVE);
317  page = BufferGetPage(buffer);
318 
319  Assert(GinPageGetOpaque(page)->rightlink == InvalidBlockNumber);
320 
322 
323  GinPageGetOpaque(page)->rightlink = sublist.head;
324 
325  MarkBufferDirty(buffer);
326 
327  metadata->tail = sublist.tail;
328  metadata->tailFreeSize = sublist.tailFreeSize;
329 
330  metadata->nPendingPages += sublist.nPendingPages;
331  metadata->nPendingHeapTuples += sublist.nPendingHeapTuples;
332 
333  if (needWal)
335  }
336  }
337  else
338  {
339  /*
340  * Insert into tail page. Metapage is already locked
341  */
342  OffsetNumber l,
343  off;
344  int i,
345  tupsize;
346  char *ptr;
347  char *collectordata;
348 
349  buffer = ReadBuffer(index, metadata->tail);
350  LockBuffer(buffer, GIN_EXCLUSIVE);
351  page = BufferGetPage(buffer);
352 
353  off = (PageIsEmpty(page)) ? FirstOffsetNumber :
355 
356  collectordata = ptr = (char *) palloc(collector->sumsize);
357 
358  data.ntuples = collector->ntuples;
359 
360  if (needWal)
361  XLogBeginInsert();
362 
364 
365  /*
366  * Increase counter of heap tuples
367  */
368  Assert(GinPageGetOpaque(page)->maxoff <= metadata->nPendingHeapTuples);
369  GinPageGetOpaque(page)->maxoff++;
370  metadata->nPendingHeapTuples++;
371 
372  for (i = 0; i < collector->ntuples; i++)
373  {
374  tupsize = IndexTupleSize(collector->tuples[i]);
375  l = PageAddItem(page, (Item) collector->tuples[i], tupsize, off, false, false);
376 
377  if (l == InvalidOffsetNumber)
378  elog(ERROR, "failed to add item to index page in \"%s\"",
379  RelationGetRelationName(index));
380 
381  memcpy(ptr, collector->tuples[i], tupsize);
382  ptr += tupsize;
383 
384  off++;
385  }
386 
387  Assert((ptr - collectordata) <= collector->sumsize);
388  if (needWal)
389  {
391  XLogRegisterBufData(1, collectordata, collector->sumsize);
392  }
393 
394  metadata->tailFreeSize = PageGetExactFreeSpace(page);
395 
396  MarkBufferDirty(buffer);
397  }
398 
399  /*
400  * Write metabuffer, make xlog entry
401  */
402  MarkBufferDirty(metabuffer);
403 
404  if (needWal)
405  {
406  XLogRecPtr recptr;
407 
408  memcpy(&data.metadata, metadata, sizeof(GinMetaPageData));
409 
410  XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT);
411  XLogRegisterData((char *) &data, sizeof(ginxlogUpdateMeta));
412 
413  recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_UPDATE_META_PAGE);
414  PageSetLSN(metapage, recptr);
415 
416  if (buffer != InvalidBuffer)
417  {
418  PageSetLSN(page, recptr);
419  }
420  }
421 
422  if (buffer != InvalidBuffer)
423  UnlockReleaseBuffer(buffer);
424 
425  /*
426  * Force pending list cleanup when it becomes too long. And,
427  * ginInsertCleanup could take significant amount of time, so we prefer to
428  * call it when it can do all the work in a single collection cycle. In
429  * non-vacuum mode, it shouldn't require maintenance_work_mem, so fire it
430  * while pending list is still small enough to fit into
431  * gin_pending_list_limit.
432  *
433  * ginInsertCleanup() should not be called inside our CRIT_SECTION.
434  */
435  cleanupSize = GinGetPendingListCleanupSize(index);
436  if (metadata->nPendingPages * GIN_PAGE_FREESIZE > cleanupSize * 1024L)
437  needCleanup = true;
438 
439  UnlockReleaseBuffer(metabuffer);
440 
442 
443  if (needCleanup)
444  ginInsertCleanup(ginstate, false, true, NULL);
445 }
BlockNumber prevTail
Definition: ginxlog.h:173
#define GIN_UNLOCK
Definition: gin_private.h:43
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:361
#define PageIsEmpty(page)
Definition: bufpage.h:219
RelFileNode node
Definition: ginxlog.h:171
Relation index
Definition: gin_private.h:53
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1445
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
#define GinListPageSize
Definition: ginblock.h:311
Pointer Item
Definition: item.h:17
#define InvalidBuffer
Definition: buf.h:25
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
void ginInsertCleanup(GinState *ginstate, bool full_clean, bool fill_fsm, IndexBulkDeleteResult *stats)
Definition: ginfast.c:729
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:413
#define GinPageGetOpaque(page)
Definition: ginblock.h:109
#define GIN_METAPAGE_BLKNO
Definition: ginblock.h:50
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
static void makeSublist(Relation index, IndexTuple *tuples, int32 ntuples, GinMetaPageData *res)
Definition: ginfast.c:148
uint16 OffsetNumber
Definition: off.h:24
Definition: type.h:90
int64 nPendingHeapTuples
Definition: ginblock.h:73
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
#define ERROR
Definition: elog.h:43
#define XLOG_GIN_UPDATE_META_PAGE
Definition: ginxlog.h:163
BlockNumber head
Definition: ginblock.h:60
BlockNumber tail
Definition: ginblock.h:61
#define FirstOffsetNumber
Definition: off.h:27
#define REGBUF_STANDARD
Definition: xloginsert.h:35
#define GinGetPendingListCleanupSize(relation)
Definition: gin_private.h:35
#define RelationGetRelationName(relation)
Definition: rel.h:433
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
BlockNumber newRightlink
Definition: ginxlog.h:174
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
IndexTuple * tuples
Definition: gin_private.h:429
#define GIN_EXCLUSIVE
Definition: gin_private.h:45
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
#define InvalidOffsetNumber
Definition: off.h:26
GinMetaPageData metadata
Definition: ginxlog.h:172
RelFileNode rd_node
Definition: rel.h:85
#define NULL
Definition: c.h:226
uint32 tailFreeSize
Definition: ginblock.h:66
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:671
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define InvalidBlockNumber
Definition: block.h:33
#define RelationNeedsWAL(relation)
Definition: rel.h:502
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:606
void * palloc(Size size)
Definition: mcxt.c:891
int i
#define GIN_PAGE_FREESIZE
Definition: ginfast.c:39
#define GinPageGetMeta(p)
Definition: ginblock.h:103
#define elog
Definition: elog.h:219
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define PageSetLSN(page, lsn)
Definition: bufpage.h:365
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
#define IndexTupleSize(itup)
Definition: itup.h:70
BlockNumber nPendingPages
Definition: ginblock.h:72
void ginInitBA ( BuildAccumulator accum)

Definition at line 109 of file ginbulk.c.

References BuildAccumulator::allocatedMemory, cmpEntryAccumulator(), BuildAccumulator::eas_used, BuildAccumulator::entryallocator, ginAllocEntryAccumulator(), ginCombineData(), NULL, rb_create(), and BuildAccumulator::tree.

Referenced by ginbuild(), ginBuildCallback(), and ginInsertCleanup().

110 {
111  /* accum->ginstate is intentionally not set here */
112  accum->allocatedMemory = 0;
113  accum->entryallocator = NULL;
114  accum->eas_used = 0;
115  accum->tree = rb_create(sizeof(GinEntryAccumulator),
119  NULL, /* no freefunc needed */
120  (void *) accum);
121 }
GinEntryAccumulator * entryallocator
Definition: gin_private.h:409
static void ginCombineData(RBNode *existing, const RBNode *newdata, void *arg)
Definition: ginbulk.c:30
RBTree * rb_create(Size node_size, rb_comparator comparator, rb_combiner combiner, rb_allocfunc allocfunc, rb_freefunc freefunc, void *arg)
Definition: rbtree.c:110
static int cmpEntryAccumulator(const RBNode *a, const RBNode *b, void *arg)
Definition: ginbulk.c:72
static RBNode * ginAllocEntryAccumulator(void *arg)
Definition: ginbulk.c:85
#define NULL
Definition: c.h:226
void GinInitBuffer ( Buffer  b,
uint32  f 
)

Definition at line 351 of file ginutil.c.

References BufferGetPage, BufferGetPageSize, and GinInitPage().

Referenced by ginbuild(), ginbuildempty(), ginRedoCreateIndex(), ginRedoCreatePTree(), ginRedoDeleteListPages(), ginRedoInsertListPage(), and writeListPage().

352 {
354 }
void GinInitPage(Page page, uint32 f, Size pageSize)
Definition: ginutil.c:338
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:147
void ginInitConsistentFunction ( GinState ginstate,
GinScanKey  key 
)

Definition at line 225 of file ginlogic.c.

References GinScanKeyData::attnum, GinScanKeyData::boolConsistentFn, GinScanKeyData::collation, GinScanKeyData::consistentFmgrInfo, GinState::consistentFn, directBoolConsistentFn(), directTriConsistentFn(), FmgrInfo::fn_oid, GIN_SEARCH_MODE_EVERYTHING, OidIsValid, GinScanKeyData::searchMode, shimBoolConsistentFn(), shimTriConsistentFn(), GinState::supportCollation, GinScanKeyData::triConsistentFmgrInfo, GinState::triConsistentFn, GinScanKeyData::triConsistentFn, trueConsistentFn(), and trueTriConsistentFn().

Referenced by ginFillScanKey().

226 {
228  {
231  }
232  else
233  {
234  key->consistentFmgrInfo = &ginstate->consistentFn[key->attnum - 1];
235  key->triConsistentFmgrInfo = &ginstate->triConsistentFn[key->attnum - 1];
236  key->collation = ginstate->supportCollation[key->attnum - 1];
237 
238  if (OidIsValid(ginstate->consistentFn[key->attnum - 1].fn_oid))
240  else
242 
243  if (OidIsValid(ginstate->triConsistentFn[key->attnum - 1].fn_oid))
245  else
247  }
248 }
FmgrInfo * triConsistentFmgrInfo
Definition: gin_private.h:288
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gin_private.h:82
static GinTernaryValue directTriConsistentFn(GinScanKey key)
Definition: ginlogic.c:95
static GinTernaryValue shimTriConsistentFn(GinScanKey key)
Definition: ginlogic.c:154
#define GIN_SEARCH_MODE_EVERYTHING
Definition: gin.h:36
FmgrInfo consistentFn[INDEX_MAX_KEYS]
Definition: gin_private.h:76
FmgrInfo * consistentFmgrInfo
Definition: gin_private.h:287
#define OidIsValid(objectId)
Definition: c.h:534
OffsetNumber attnum
Definition: gin_private.h:299
static GinTernaryValue trueTriConsistentFn(GinScanKey key)
Definition: ginlogic.c:62
Oid fn_oid
Definition: fmgr.h:56
static bool directBoolConsistentFn(GinScanKey key)
Definition: ginlogic.c:71
static bool trueConsistentFn(GinScanKey key)
Definition: ginlogic.c:56
bool(* boolConsistentFn)(GinScanKey key)
Definition: gin_private.h:285
GinTernaryValue(* triConsistentFn)(GinScanKey key)
Definition: gin_private.h:286
static bool shimBoolConsistentFn(GinScanKey key)
Definition: ginlogic.c:115
FmgrInfo triConsistentFn[INDEX_MAX_KEYS]
Definition: gin_private.h:77
void GinInitMetabuffer ( Buffer  b)

Definition at line 357 of file ginutil.c.

References BufferGetPage, BufferGetPageSize, GIN_CURRENT_VERSION, GIN_META, GinInitPage(), GinPageGetMeta, GinMetaPageData::ginVersion, GinMetaPageData::head, InvalidBlockNumber, GinMetaPageData::nDataPages, GinMetaPageData::nEntries, GinMetaPageData::nEntryPages, GinMetaPageData::nPendingHeapTuples, GinMetaPageData::nPendingPages, GinMetaPageData::nTotalPages, GinMetaPageData::tail, and GinMetaPageData::tailFreeSize.

Referenced by ginbuild(), ginbuildempty(), and ginRedoCreateIndex().

358 {
359  GinMetaPageData *metadata;
360  Page page = BufferGetPage(b);
361 
363 
364  metadata = GinPageGetMeta(page);
365 
366  metadata->head = metadata->tail = InvalidBlockNumber;
367  metadata->tailFreeSize = 0;
368  metadata->nPendingPages = 0;
369  metadata->nPendingHeapTuples = 0;
370  metadata->nTotalPages = 0;
371  metadata->nEntryPages = 0;
372  metadata->nDataPages = 0;
373  metadata->nEntries = 0;
374  metadata->ginVersion = GIN_CURRENT_VERSION;
375 }
BlockNumber nEntryPages
Definition: ginblock.h:79
void GinInitPage(Page page, uint32 f, Size pageSize)
Definition: ginutil.c:338
BlockNumber nTotalPages
Definition: ginblock.h:78
int64 nEntries
Definition: ginblock.h:81
int64 nPendingHeapTuples
Definition: ginblock.h:73
#define GIN_META
Definition: ginblock.h:42
BlockNumber head
Definition: ginblock.h:60
BlockNumber tail
Definition: ginblock.h:61
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:147
uint32 tailFreeSize
Definition: ginblock.h:66
#define GIN_CURRENT_VERSION
Definition: ginblock.h:101
#define InvalidBlockNumber
Definition: block.h:33
int32 ginVersion
Definition: ginblock.h:98
BlockNumber nDataPages
Definition: ginblock.h:80
#define GinPageGetMeta(p)
Definition: ginblock.h:103
Pointer Page
Definition: bufpage.h:74
BlockNumber nPendingPages
Definition: ginblock.h:72
void GinInitPage ( Page  page,
uint32  f,
Size  pageSize 
)

Definition at line 338 of file ginutil.c.

References GinPageOpaqueData::flags, GinPageGetOpaque, InvalidBlockNumber, PageInit(), and GinPageOpaqueData::rightlink.

Referenced by createPostingTree(), dataPlaceToPageLeafSplit(), dataSplitPageInternal(), entrySplitPage(), GinInitBuffer(), GinInitMetabuffer(), ginPlaceToPage(), ginRedoDeleteListPages(), and ginRedoUpdateMetapage().

339 {
340  GinPageOpaque opaque;
341 
342  PageInit(page, pageSize, sizeof(GinPageOpaqueData));
343 
344  opaque = GinPageGetOpaque(page);
345  memset(opaque, 0, sizeof(GinPageOpaqueData));
346  opaque->flags = f;
347  opaque->rightlink = InvalidBlockNumber;
348 }
BlockNumber rightlink
Definition: ginblock.h:30
#define GinPageGetOpaque(page)
Definition: ginblock.h:109
#define InvalidBlockNumber
Definition: block.h:33
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:41
bool gininsert ( Relation  index,
Datum values,
bool isnull,
ItemPointer  ht_ctid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
struct IndexInfo indexInfo 
)

Definition at line 484 of file gininsert.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), CurrentMemoryContext, GinGetUseFastUpdate, ginHeapTupleFastCollect(), ginHeapTupleFastInsert(), ginHeapTupleInsert(), i, IndexInfo::ii_AmCache, IndexInfo::ii_Context, initGinState(), MemoryContextDelete(), MemoryContextSwitchTo(), tupleDesc::natts, NULL, GinState::origTupdesc, and palloc().

Referenced by ginhandler().

488 {
489  GinState *ginstate = (GinState *) indexInfo->ii_AmCache;
490  MemoryContext oldCtx;
491  MemoryContext insertCtx;
492  int i;
493 
494  /* Initialize GinState cache if first call in this statement */
495  if (ginstate == NULL)
496  {
497  oldCtx = MemoryContextSwitchTo(indexInfo->ii_Context);
498  ginstate = (GinState *) palloc(sizeof(GinState));
499  initGinState(ginstate, index);
500  indexInfo->ii_AmCache = (void *) ginstate;
501  MemoryContextSwitchTo(oldCtx);
502  }
503 
505  "Gin insert temporary context",
507 
508  oldCtx = MemoryContextSwitchTo(insertCtx);
509 
510  if (GinGetUseFastUpdate(index))
511  {
512  GinTupleCollector collector;
513 
514  memset(&collector, 0, sizeof(GinTupleCollector));
515 
516  for (i = 0; i < ginstate->origTupdesc->natts; i++)
517  ginHeapTupleFastCollect(ginstate, &collector,
518  (OffsetNumber) (i + 1),
519  values[i], isnull[i],
520  ht_ctid);
521 
522  ginHeapTupleFastInsert(ginstate, &collector);
523  }
524  else
525  {
526  for (i = 0; i < ginstate->origTupdesc->natts; i++)
527  ginHeapTupleInsert(ginstate, (OffsetNumber) (i + 1),
528  values[i], isnull[i],
529  ht_ctid);
530  }
531 
532  MemoryContextSwitchTo(oldCtx);
533  MemoryContextDelete(insertCtx);
534 
535  return false;
536 }
MemoryContext ii_Context
Definition: execnodes.h:82
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
void ginHeapTupleFastInsert(GinState *ginstate, GinTupleCollector *collector)
Definition: ginfast.c:222
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static void ginHeapTupleInsert(GinState *ginstate, OffsetNumber attnum, Datum value, bool isNull, ItemPointer item)
Definition: gininsert.c:466
int natts
Definition: tupdesc.h:73
uint16 OffsetNumber
Definition: off.h:24
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:86
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void * ii_AmCache
Definition: execnodes.h:81
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
#define NULL
Definition: c.h:226
#define GinGetUseFastUpdate(relation)
Definition: gin_private.h:32
static Datum values[MAXATTR]
Definition: bootstrap.c:162
void * palloc(Size size)
Definition: mcxt.c:891
int i
void ginHeapTupleFastCollect(GinState *ginstate, GinTupleCollector *collector, OffsetNumber attnum, Datum value, bool isNull, ItemPointer ht_ctid)
Definition: ginfast.c:456
TupleDesc origTupdesc
Definition: gin_private.h:67
void ginInsertBAEntries ( BuildAccumulator accum,
ItemPointer  heapptr,
OffsetNumber  attnum,
Datum entries,
GinNullCategory categories,
int32  nentries 
)

Definition at line 209 of file ginbulk.c.

References Assert, FirstOffsetNumber, ginInsertBAEntry(), i, and ItemPointerIsValid.

Referenced by ginHeapTupleBulkInsert(), and processPendingPage().

213 {
214  uint32 step = nentries;
215 
216  if (nentries <= 0)
217  return;
218 
219  Assert(ItemPointerIsValid(heapptr) && attnum >= FirstOffsetNumber);
220 
221  /*
222  * step will contain largest power of 2 and <= nentries
223  */
224  step |= (step >> 1);
225  step |= (step >> 2);
226  step |= (step >> 4);
227  step |= (step >> 8);
228  step |= (step >> 16);
229  step >>= 1;
230  step++;
231 
232  while (step > 0)
233  {
234  int i;
235 
236  for (i = step - 1; i < nentries && i >= 0; i += step << 1 /* *2 */ )
237  ginInsertBAEntry(accum, heapptr, attnum,
238  entries[i], categories[i]);
239 
240  step >>= 1; /* /2 */
241  }
242 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:59
static void ginInsertBAEntry(BuildAccumulator *accum, ItemPointer heapptr, OffsetNumber attnum, Datum key, GinNullCategory category)
Definition: ginbulk.c:147
#define FirstOffsetNumber
Definition: off.h:27
unsigned int uint32
Definition: c.h:265
#define Assert(condition)
Definition: c.h:671
int i
void ginInsertCleanup ( GinState ginstate,
bool  full_clean,
bool  fill_fsm,
IndexBulkDeleteResult stats 
)

Definition at line 729 of file ginfast.c.

References BuildAccumulator::allocatedMemory, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, autovacuum_work_mem, BufferGetPage, ConditionalLockPage(), CurrentMemoryContext, ExclusiveLock, FirstOffsetNumber, GIN_EXCLUSIVE, GIN_METAPAGE_BLKNO, GIN_SHARE, GIN_UNLOCK, ginBeginBAScan(), ginEntryInsert(), ginGetBAEntry(), ginInitBA(), GinPageGetMeta, GinPageGetOpaque, GinPageHasFullRow, GinPageIsDeleted, BuildAccumulator::ginstate, GinMetaPageData::head, GinState::index, IndexFreeSpaceMapVacuum(), initKeyArray(), InvalidBlockNumber, IsAutoVacuumWorkerProcess(), sort-test::list, LockBuffer(), LockPage(), maintenance_work_mem, KeyArray::maxvalues, MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), NULL, opCtx, PageGetMaxOffsetNumber, processPendingPage(), ReadBuffer(), ReleaseBuffer(), shiftList(), GinMetaPageData::tail, UnlockPage(), UnlockReleaseBuffer(), vacuum_delay_point(), and work_mem.

Referenced by gin_clean_pending_list(), ginbulkdelete(), ginHeapTupleFastInsert(), and ginvacuumcleanup().

731 {
732  Relation index = ginstate->index;
733  Buffer metabuffer,
734  buffer;
735  Page metapage,
736  page;
737  GinMetaPageData *metadata;
739  oldCtx;
740  BuildAccumulator accum;
741  KeyArray datums;
742  BlockNumber blkno,
743  blknoFinish;
744  bool cleanupFinish = false;
745  bool fsm_vac = false;
746  Size workMemory;
747  bool inVacuum = (stats == NULL);
748 
749  /*
750  * We would like to prevent concurrent cleanup process. For that we will
751  * lock metapage in exclusive mode using LockPage() call. Nobody other
752  * will use that lock for metapage, so we keep possibility of concurrent
753  * insertion into pending list
754  */
755 
756  if (inVacuum)
757  {
758  /*
759  * We are called from [auto]vacuum/analyze or gin_clean_pending_list()
760  * and we would like to wait concurrent cleanup to finish.
761  */
763  workMemory =
766  }
767  else
768  {
769  /*
770  * We are called from regular insert and if we see concurrent cleanup
771  * just exit in hope that concurrent process will clean up pending
772  * list.
773  */
775  return;
776  workMemory = work_mem;
777  }
778 
779  metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
780  LockBuffer(metabuffer, GIN_SHARE);
781  metapage = BufferGetPage(metabuffer);
782  metadata = GinPageGetMeta(metapage);
783 
784  if (metadata->head == InvalidBlockNumber)
785  {
786  /* Nothing to do */
787  UnlockReleaseBuffer(metabuffer);
789  return;
790  }
791 
792  /*
793  * Remember a tail page to prevent infinite cleanup if other backends add
794  * new tuples faster than we can cleanup.
795  */
796  blknoFinish = metadata->tail;
797 
798  /*
799  * Read and lock head of pending list
800  */
801  blkno = metadata->head;
802  buffer = ReadBuffer(index, blkno);
803  LockBuffer(buffer, GIN_SHARE);
804  page = BufferGetPage(buffer);
805 
806  LockBuffer(metabuffer, GIN_UNLOCK);
807 
808  /*
809  * Initialize. All temporary space will be in opCtx
810  */
812  "GIN insert cleanup temporary context",
814 
815  oldCtx = MemoryContextSwitchTo(opCtx);
816 
817  initKeyArray(&datums, 128);
818  ginInitBA(&accum);
819  accum.ginstate = ginstate;
820 
821  /*
822  * At the top of this loop, we have pin and lock on the current page of
823  * the pending list. However, we'll release that before exiting the loop.
824  * Note we also have pin but not lock on the metapage.
825  */
826  for (;;)
827  {
828  Assert(!GinPageIsDeleted(page));
829 
830  /*
831  * Are we walk through the page which as we remember was a tail when
832  * we start our cleanup? But if caller asks us to clean up whole
833  * pending list then ignore old tail, we will work until list becomes
834  * empty.
835  */
836  if (blkno == blknoFinish && full_clean == false)
837  cleanupFinish = true;
838 
839  /*
840  * read page's datums into accum
841  */
842  processPendingPage(&accum, &datums, page, FirstOffsetNumber);
843 
845 
846  /*
847  * Is it time to flush memory to disk? Flush if we are at the end of
848  * the pending list, or if we have a full row and memory is getting
849  * full.
850  */
851  if (GinPageGetOpaque(page)->rightlink == InvalidBlockNumber ||
852  (GinPageHasFullRow(page) &&
853  (accum.allocatedMemory >= workMemory * 1024L)))
854  {
856  uint32 nlist;
857  Datum key;
858  GinNullCategory category;
859  OffsetNumber maxoff,
860  attnum;
861 
862  /*
863  * Unlock current page to increase performance. Changes of page
864  * will be checked later by comparing maxoff after completion of
865  * memory flush.
866  */
867  maxoff = PageGetMaxOffsetNumber(page);
868  LockBuffer(buffer, GIN_UNLOCK);
869 
870  /*
871  * Moving collected data into regular structure can take
872  * significant amount of time - so, run it without locking pending
873  * list.
874  */
875  ginBeginBAScan(&accum);
876  while ((list = ginGetBAEntry(&accum,
877  &attnum, &key, &category, &nlist)) != NULL)
878  {
879  ginEntryInsert(ginstate, attnum, key, category,
880  list, nlist, NULL);
882  }
883 
884  /*
885  * Lock the whole list to remove pages
886  */
887  LockBuffer(metabuffer, GIN_EXCLUSIVE);
888  LockBuffer(buffer, GIN_SHARE);
889 
890  Assert(!GinPageIsDeleted(page));
891 
892  /*
893  * While we left the page unlocked, more stuff might have gotten
894  * added to it. If so, process those entries immediately. There
895  * shouldn't be very many, so we don't worry about the fact that
896  * we're doing this with exclusive lock. Insertion algorithm
897  * guarantees that inserted row(s) will not continue on next page.
898  * NOTE: intentionally no vacuum_delay_point in this loop.
899  */
900  if (PageGetMaxOffsetNumber(page) != maxoff)
901  {
902  ginInitBA(&accum);
903  processPendingPage(&accum, &datums, page, maxoff + 1);
904 
905  ginBeginBAScan(&accum);
906  while ((list = ginGetBAEntry(&accum,
907  &attnum, &key, &category, &nlist)) != NULL)
908  ginEntryInsert(ginstate, attnum, key, category,
909  list, nlist, NULL);
910  }
911 
912  /*
913  * Remember next page - it will become the new list head
914  */
915  blkno = GinPageGetOpaque(page)->rightlink;
916  UnlockReleaseBuffer(buffer); /* shiftList will do exclusive
917  * locking */
918 
919  /*
920  * remove read pages from pending list, at this point all content
921  * of read pages is in regular structure
922  */
923  shiftList(index, metabuffer, blkno, fill_fsm, stats);
924 
925  /* At this point, some pending pages have been freed up */
926  fsm_vac = true;
927 
928  Assert(blkno == metadata->head);
929  LockBuffer(metabuffer, GIN_UNLOCK);
930 
931  /*
932  * if we removed the whole pending list or we cleanup tail (which
933  * we remembered on start our cleanup process) then just exit
934  */
935  if (blkno == InvalidBlockNumber || cleanupFinish)
936  break;
937 
938  /*
939  * release memory used so far and reinit state
940  */
941  MemoryContextReset(opCtx);
942  initKeyArray(&datums, datums.maxvalues);
943  ginInitBA(&accum);
944  }
945  else
946  {
947  blkno = GinPageGetOpaque(page)->rightlink;
948  UnlockReleaseBuffer(buffer);
949  }
950 
951  /*
952  * Read next page in pending list
953  */
955  buffer = ReadBuffer(index, blkno);
956  LockBuffer(buffer, GIN_SHARE);
957  page = BufferGetPage(buffer);
958  }
959 
961  ReleaseBuffer(metabuffer);
962 
963  /*
964  * As pending list pages can have a high churn rate, it is desirable to
965  * recycle them immediately to the FreeSpace Map when ordinary backends
966  * clean the list.
967  */
968  if (fsm_vac && fill_fsm)
970 
971 
972  /* Clean up temporary space */
973  MemoryContextSwitchTo(oldCtx);
974  MemoryContextDelete(opCtx);
975 }
#define GinPageHasFullRow(page)
Definition: ginblock.h:118
int autovacuum_work_mem
Definition: autovacuum.c:112
#define GIN_UNLOCK
Definition: gin_private.h:43
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
Relation index
Definition: gin_private.h:53
#define ExclusiveLock
Definition: lockdefs.h:44
static MemoryContext opCtx
Definition: ginxlog.c:22
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3292
#define GinPageGetOpaque(page)
Definition: ginblock.h:109
#define GIN_METAPAGE_BLKNO
Definition: ginblock.h:50
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
uint16 OffsetNumber
Definition: off.h:24
static void initKeyArray(KeyArray *keys, int32 maxvalues)
Definition: ginfast.c:621
Definition: type.h:90
void ginEntryInsert(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gininsert.c:177
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
signed char GinNullCategory
Definition: ginblock.h:190
BlockNumber head
Definition: ginblock.h:60
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
void LockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
Definition: lmgr.c:400
BlockNumber tail
Definition: ginblock.h:61
#define FirstOffsetNumber
Definition: off.h:27
unsigned int uint32
Definition: c.h:265
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
GinState * ginstate
Definition: gin_private.h:407
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:2988
#define GIN_SHARE
Definition: gin_private.h:44
int32 maxvalues
Definition: ginfast.c:47
static void processPendingPage(BuildAccumulator *accum, KeyArray *ka, Page page, OffsetNumber startoff)
Definition: ginfast.c:658
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
#define GinPageIsDeleted(page)
Definition: ginblock.h:123
uintptr_t Datum
Definition: postgres.h:374
#define GIN_EXCLUSIVE
Definition: gin_private.h:45
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
int work_mem
Definition: globals.c:112
int maintenance_work_mem
Definition: globals.c:113
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static void shiftList(Relation index, Buffer metabuffer, BlockNumber newHead, bool fill_fsm, IndexBulkDeleteResult *stats)
Definition: ginfast.c:511
void IndexFreeSpaceMapVacuum(Relation rel)
Definition: indexfsm.c:71
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
size_t Size
Definition: c.h:353
#define InvalidBlockNumber
Definition: block.h:33
ItemPointerData * ginGetBAEntry(BuildAccumulator *accum, OffsetNumber *attnum, Datum *key, GinNullCategory *category, uint32 *n)
Definition: ginbulk.c:267
void UnlockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
Definition: lmgr.c:435
bool ConditionalLockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
Definition: lmgr.c:419
void ginBeginBAScan(BuildAccumulator *accum)
Definition: ginbulk.c:256
tuple list
Definition: sort-test.py:11
#define GinPageGetMeta(p)
Definition: ginblock.h:103
void vacuum_delay_point(void)
Definition: vacuum.c:1515
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
void ginInitBA(BuildAccumulator *accum)
Definition: ginbulk.c:109
void ginInsertItemPointers ( Relation  index,
BlockNumber  rootBlkno,
ItemPointerData items,
uint32  nitem,
GinStatsData buildStats 
)

Definition at line 1887 of file gindatapage.c.

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

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

1890 {
1891  GinBtreeData btree;
1892  GinBtreeDataLeafInsertData insertdata;
1893  GinBtreeStack *stack;
1894 
1895  ginPrepareDataScan(&btree, index, rootBlkno);
1896  btree.isBuild = (buildStats != NULL);
1897  insertdata.items = items;
1898  insertdata.nitem = nitem;
1899  insertdata.curitem = 0;
1900 
1901  while (insertdata.curitem < insertdata.nitem)
1902  {
1903  /* search for the leaf page where the first item should go to */
1904  btree.itemptr = insertdata.items[insertdata.curitem];
1905  stack = ginFindLeafPage(&btree, false, NULL);
1906 
1907  ginInsertValue(&btree, stack, &insertdata, buildStats);
1908  }
1909 }
GinBtreeStack * ginFindLeafPage(GinBtree btree, bool searchMode, Snapshot snapshot)
Definition: ginbtree.c:76
ItemPointerData * items
Definition: gin_private.h:188
void ginInsertValue(GinBtree btree, GinBtreeStack *stack, void *insertdata, GinStatsData *buildStats)
Definition: ginbtree.c:755
#define NULL
Definition: c.h:226
ItemPointerData itemptr
Definition: gin_private.h:172
void ginPrepareDataScan(GinBtree btree, Relation index, BlockNumber rootBlkno)
Definition: gindatapage.c:1861
void ginInsertValue ( GinBtree  btree,
GinBtreeStack stack,
void *  insertdata,
GinStatsData buildStats 
)

Definition at line 755 of file ginbtree.c.

References GinBtreeStack::buffer, BufferGetPage, freeGinBtreeStack(), GIN_UNLOCK, ginFinishSplit(), GinPageIsIncompleteSplit, ginPlaceToPage(), InvalidBlockNumber, InvalidBuffer, and LockBuffer().

Referenced by ginEntryInsert(), and ginInsertItemPointers().

757 {
758  bool done;
759 
760  /* If the leaf page was incompletely split, finish the split first */
762  ginFinishSplit(btree, stack, false, buildStats);
763 
764  done = ginPlaceToPage(btree, stack,
765  insertdata, InvalidBlockNumber,
766  InvalidBuffer, buildStats);
767  if (done)
768  {
769  LockBuffer(stack->buffer, GIN_UNLOCK);
770  freeGinBtreeStack(stack);
771  }
772  else
773  ginFinishSplit(btree, stack, true, buildStats);
774 }
#define GIN_UNLOCK
Definition: gin_private.h:43
#define GinPageIsIncompleteSplit(page)
Definition: ginblock.h:126
static void ginFinishSplit(GinBtree btree, GinBtreeStack *stack, bool freestack, GinStatsData *buildStats)
Definition: ginbtree.c:646
#define InvalidBuffer
Definition: buf.h:25
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
#define InvalidBlockNumber
Definition: block.h:33
void freeGinBtreeStack(GinBtreeStack *stack)
Definition: ginbtree.c:193
static bool ginPlaceToPage(GinBtree btree, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, Buffer childbuf, GinStatsData *buildStats)
Definition: ginbtree.c:329
ItemPointer ginMergeItemPointers ( ItemPointerData a,
uint32  na,
ItemPointerData b,
uint32  nb,
int *  nmerged 
)

Definition at line 367 of file ginpostinglist.c.

References cmp(), ginCompareItemPointers(), and palloc().

Referenced by addItemPointersToLeafTuple(), addItemsToLeaf(), ginRedoRecompress(), and leafRepackItems().

370 {
371  ItemPointerData *dst;
372 
373  dst = (ItemPointer) palloc((na + nb) * sizeof(ItemPointerData));
374 
375  /*
376  * If the argument arrays don't overlap, we can just append them to each
377  * other.
378  */
379  if (na == 0 || nb == 0 || ginCompareItemPointers(&a[na - 1], &b[0]) < 0)
380  {
381  memcpy(dst, a, na * sizeof(ItemPointerData));
382  memcpy(&dst[na], b, nb * sizeof(ItemPointerData));
383  *nmerged = na + nb;
384  }
385  else if (ginCompareItemPointers(&b[nb - 1], &a[0]) < 0)
386  {
387  memcpy(dst, b, nb * sizeof(ItemPointerData));
388  memcpy(&dst[nb], a, na * sizeof(ItemPointerData));
389  *nmerged = na + nb;
390  }
391  else
392  {
393  ItemPointerData *dptr = dst;
394  ItemPointerData *aptr = a;
395  ItemPointerData *bptr = b;
396 
397  while (aptr - a < na && bptr - b < nb)
398  {
399  int cmp = ginCompareItemPointers(aptr, bptr);
400 
401  if (cmp > 0)
402  *dptr++ = *bptr++;
403  else if (cmp == 0)
404  {
405  /* only keep one copy of the identical items */
406  *dptr++ = *bptr++;
407  aptr++;
408  }
409  else
410  *dptr++ = *aptr++;
411  }
412 
413  while (aptr - a < na)
414  *dptr++ = *aptr++;
415 
416  while (bptr - b < nb)
417  *dptr++ = *bptr++;
418 
419  *nmerged = dptr - dst;
420  }
421 
422  return dst;
423 }
ItemPointerData * ItemPointer
Definition: itemptr.h:48
struct ItemPointerData ItemPointerData
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:461
void * palloc(Size size)
Definition: mcxt.c:891
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Buffer GinNewBuffer ( Relation  index)

Definition at line 287 of file ginutil.c.

References BufferGetPage, ConditionalLockBuffer(), ExclusiveLock, GetFreeIndexPage(), GIN_EXCLUSIVE, GIN_UNLOCK, GinPageIsDeleted, InvalidBlockNumber, LockBuffer(), LockRelationForExtension(), P_NEW, PageIsNew, ReadBuffer(), RELATION_IS_LOCAL, ReleaseBuffer(), and UnlockRelationForExtension().

Referenced by createPostingTree(), ginbuild(), ginPlaceToPage(), and makeSublist().

288 {
289  Buffer buffer;
290  bool needLock;
291 
292  /* First, try to get a page from FSM */
293  for (;;)
294  {
295  BlockNumber blkno = GetFreeIndexPage(index);
296 
297  if (blkno == InvalidBlockNumber)
298  break;
299 
300  buffer = ReadBuffer(index, blkno);
301 
302  /*
303  * We have to guard against the possibility that someone else already
304  * recycled this page; the buffer may be locked if so.
305  */
306  if (ConditionalLockBuffer(buffer))
307  {
308  Page page = BufferGetPage(buffer);
309 
310  if (PageIsNew(page))
311  return buffer; /* OK to use, if never initialized */
312 
313  if (GinPageIsDeleted(page))
314  return buffer; /* OK to use */
315 
316  LockBuffer(buffer, GIN_UNLOCK);
317  }
318 
319  /* Can't use it, so release buffer and try again */
320  ReleaseBuffer(buffer);
321  }
322 
323  /* Must extend the file */
324  needLock = !RELATION_IS_LOCAL(index);
325  if (needLock)
327 
328  buffer = ReadBuffer(index, P_NEW);
329  LockBuffer(buffer, GIN_EXCLUSIVE);
330 
331  if (needLock)
333 
334  return buffer;
335 }
#define GIN_UNLOCK
Definition: gin_private.h:43
#define ExclusiveLock
Definition: lockdefs.h:44
#define RELATION_IS_LOCAL(relation)
Definition: rel.h:520
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3292
#define P_NEW
Definition: bufmgr.h:82
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:3555
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:332
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:382
#define GinPageIsDeleted(page)
Definition: ginblock.h:123
#define GIN_EXCLUSIVE
Definition: gin_private.h:45
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
BlockNumber GetFreeIndexPage(Relation rel)
Definition: indexfsm.c:38
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define InvalidBlockNumber
Definition: block.h:33
#define PageIsNew(page)
Definition: bufpage.h:226
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
void ginNewScanKey ( IndexScanDesc  scan)

Definition at line 262 of file ginscan.c.

References GinScanOpaqueData::allocentries, DatumGetPointer, GinScanOpaqueData::entries, ereport, errcode(), errhint(), errmsg(), ERROR, GinState::extractQueryFn, FirstOffsetNumber, FunctionCall7Coll(), GIN_SEARCH_MODE_ALL, GIN_SEARCH_MODE_DEFAULT, GIN_SEARCH_MODE_EVERYTHING, ginFillScanKey(), ginGetStats(), GinScanOpaqueData::ginstate, GinStatsData::ginVersion, i, IndexScanDescData::indexRelation, InvalidStrategy, GinScanOpaqueData::isVoidRes, GinScanOpaqueData::keyCtx, IndexScanDescData::keyData, GinScanOpaqueData::keys, Max, MemoryContextSwitchTo(), GinScanOpaqueData::nkeys, NULL, IndexScanDescData::numberOfKeys, IndexScanDescData::opaque, palloc(), palloc0(), pgstat_count_index_scan, PointerGetDatum, RelationGetRelationName, ScanKeyData::sk_argument, ScanKeyData::sk_attno, ScanKeyData::sk_flags, SK_ISNULL, ScanKeyData::sk_strategy, GinState::supportCollation, GinScanOpaqueData::totalentries, and UInt16GetDatum.

Referenced by gingetbitmap().

263 {
264  ScanKey scankey = scan->keyData;
265  GinScanOpaque so = (GinScanOpaque) scan->opaque;
266  int i;
267  bool hasNullQuery = false;
268  MemoryContext oldCtx;
269 
270  /*
271  * Allocate all the scan key information in the key context. (If
272  * extractQuery leaks anything there, it won't be reset until the end of
273  * scan or rescan, but that's OK.)
274  */
275  oldCtx = MemoryContextSwitchTo(so->keyCtx);
276 
277  /* if no scan keys provided, allocate extra EVERYTHING GinScanKey */
278  so->keys = (GinScanKey)
279  palloc(Max(scan->numberOfKeys, 1) * sizeof(GinScanKeyData));
280  so->nkeys = 0;
281 
282  /* initialize expansible array of GinScanEntry pointers */
283  so->totalentries = 0;
284  so->allocentries = 32;
285  so->entries = (GinScanEntry *)
286  palloc(so->allocentries * sizeof(GinScanEntry));
287 
288  so->isVoidRes = false;
289 
290  for (i = 0; i < scan->numberOfKeys; i++)
291  {
292  ScanKey skey = &scankey[i];
293  Datum *queryValues;
294  int32 nQueryValues = 0;
295  bool *partial_matches = NULL;
296  Pointer *extra_data = NULL;
297  bool *nullFlags = NULL;
298  int32 searchMode = GIN_SEARCH_MODE_DEFAULT;
299 
300  /*
301  * We assume that GIN-indexable operators are strict, so a null query
302  * argument means an unsatisfiable query.
303  */
304  if (skey->sk_flags & SK_ISNULL)
305  {
306  so->isVoidRes = true;
307  break;
308  }
309 
310  /* OK to call the extractQueryFn */
311  queryValues = (Datum *)
313  so->ginstate.supportCollation[skey->sk_attno - 1],
314  skey->sk_argument,
315  PointerGetDatum(&nQueryValues),
317  PointerGetDatum(&partial_matches),
318  PointerGetDatum(&extra_data),
319  PointerGetDatum(&nullFlags),
320  PointerGetDatum(&searchMode)));
321 
322  /*
323  * If bogus searchMode is returned, treat as GIN_SEARCH_MODE_ALL; note
324  * in particular we don't allow extractQueryFn to select
325  * GIN_SEARCH_MODE_EVERYTHING.
326  */
327  if (searchMode < GIN_SEARCH_MODE_DEFAULT ||
328  searchMode > GIN_SEARCH_MODE_ALL)
329  searchMode = GIN_SEARCH_MODE_ALL;
330 
331  /* Non-default modes require the index to have placeholders */
332  if (searchMode != GIN_SEARCH_MODE_DEFAULT)
333  hasNullQuery = true;
334 
335  /*
336  * In default mode, no keys means an unsatisfiable query.
337  */
338  if (queryValues == NULL || nQueryValues <= 0)
339  {
340  if (searchMode == GIN_SEARCH_MODE_DEFAULT)
341  {
342  so->isVoidRes = true;
343  break;
344  }
345  nQueryValues = 0; /* ensure sane value */
346  }
347 
348  /*
349  * If the extractQueryFn didn't create a nullFlags array, create one,
350  * assuming that everything's non-null. Otherwise, run through the
351  * array and make sure each value is exactly 0 or 1; this ensures
352  * binary compatibility with the GinNullCategory representation. While
353  * at it, detect whether any null keys are present.
354  */
355  if (nullFlags == NULL)
356  nullFlags = (bool *) palloc0(nQueryValues * sizeof(bool));
357  else
358  {
359  int32 j;
360 
361  for (j = 0; j < nQueryValues; j++)
362  {
363  if (nullFlags[j])
364  {
365  nullFlags[j] = true; /* not any other nonzero value */
366  hasNullQuery = true;
367  }
368  }
369  }
370  /* now we can use the nullFlags as category codes */
371 
372  ginFillScanKey(so, skey->sk_attno,
373  skey->sk_strategy, searchMode,
374  skey->sk_argument, nQueryValues,
375  queryValues, (GinNullCategory *) nullFlags,
376  partial_matches, extra_data);
377  }
378 
379  /*
380  * If there are no regular scan keys, generate an EVERYTHING scankey to
381  * drive a full-index scan.
382  */
383  if (so->nkeys == 0 && !so->isVoidRes)
384  {
385  hasNullQuery = true;
388  (Datum) 0, 0,
389  NULL, NULL, NULL, NULL);
390  }
391 
392  /*
393  * If the index is version 0, it may be missing null and placeholder
394  * entries, which would render searches for nulls and full-index scans
395  * unreliable. Throw an error if so.
396  */
397  if (hasNullQuery && !so->isVoidRes)
398  {
399  GinStatsData ginStats;
400 
401  ginGetStats(scan->indexRelation, &ginStats);
402  if (ginStats.ginVersion < 1)
403  ereport(ERROR,
404  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
405  errmsg("old GIN indexes do not support whole-index scans nor searches for nulls"),
406  errhint("To fix this, do REINDEX INDEX \"%s\".",
408  }
409 
410  MemoryContextSwitchTo(oldCtx);
411 
413 }
#define InvalidStrategy
Definition: stratnum.h:24
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gin_private.h:82
int errhint(const char *fmt,...)
Definition: elog.c:987
struct GinScanKeyData GinScanKeyData
#define PointerGetDatum(X)
Definition: postgres.h:564
#define GIN_SEARCH_MODE_EVERYTHING
Definition: gin.h:36
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:253
Relation indexRelation
Definition: relscan.h:89
#define GIN_SEARCH_MODE_ALL
Definition: gin.h:35
char * Pointer
Definition: c.h:242
#define ERROR
Definition: elog.h:43
signed char GinNullCategory
Definition: ginblock.h:190
Datum FunctionCall7Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7)
Definition: fmgr.c:1441
struct GinScanKeyData * GinScanKey
Definition: gin_private.h:256
StrategyNumber sk_strategy
Definition: skey.h:68
GinScanOpaqueData * GinScanOpaque
Definition: gin_private.h:364
static void ginFillScanKey(GinScanOpaque so, OffsetNumber attnum, StrategyNumber strategy, int32 searchMode, Datum query, uint32 nQueryValues, Datum *queryValues, GinNullCategory *queryCategories, bool *partial_matches, Pointer *extra_data)
Definition: ginscan.c:132
#define FirstOffsetNumber
Definition: off.h:27
MemoryContext keyCtx
Definition: gin_private.h:359
#define RelationGetRelationName(relation)
Definition: rel.h:433
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:1155
FmgrInfo extractQueryFn[INDEX_MAX_KEYS]
Definition: gin_private.h:75
#define SK_ISNULL
Definition: skey.h:115
#define ereport(elevel, rest)
Definition: elog.h:122
GinScanKey keys
Definition: gin_private.h:352
void * palloc0(Size size)
Definition: mcxt.c:920
uintptr_t Datum
Definition: postgres.h:374
void ginGetStats(Relation index, GinStatsData *stats)
Definition: ginutil.c:632
#define GIN_SEARCH_MODE_DEFAULT
Definition: gin.h:33
#define Max(x, y)
Definition: c.h:796
int sk_flags
Definition: skey.h:66
#define NULL
Definition: c.h:226
ScanKey keyData
Definition: relscan.h:93
int32 ginVersion
Definition: gin.h:48
#define DatumGetPointer(X)
Definition: postgres.h:557
GinScanEntry * entries
Definition: gin_private.h:355
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Datum sk_argument
Definition: skey.h:72
#define UInt16GetDatum(X)
Definition: postgres.h:473
AttrNumber sk_attno
Definition: skey.h:67
bytea* ginoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 597 of file ginutil.c.

References allocateReloptStruct(), fillRelOptions(), lengthof, NULL, offsetof, options, parseRelOptions(), pfree(), RELOPT_KIND_GIN, RELOPT_TYPE_BOOL, and RELOPT_TYPE_INT.

Referenced by ginhandler().

598 {
600  GinOptions *rdopts;
601  int numoptions;
602  static const relopt_parse_elt tab[] = {
603  {"fastupdate", RELOPT_TYPE_BOOL, offsetof(GinOptions, useFastUpdate)},
604  {"gin_pending_list_limit", RELOPT_TYPE_INT, offsetof(GinOptions,
605  pendingListCleanupSize)}
606  };
607 
608  options = parseRelOptions(reloptions, validate, RELOPT_KIND_GIN,
609  &numoptions);
610 
611  /* if none set, we're done */
612  if (numoptions == 0)
613  return NULL;
614 
615  rdopts = allocateReloptStruct(sizeof(GinOptions), options, numoptions);
616 
617  fillRelOptions((void *) rdopts, sizeof(GinOptions), options, numoptions,
618  validate, tab, lengthof(tab));
619 
620  pfree(options);
621 
622  return (bytea *) rdopts;
623 }
#define lengthof(array)
Definition: c.h:558
void pfree(void *pointer)
Definition: mcxt.c:992
void * allocateReloptStruct(Size base, relopt_value *options, int numoptions)
Definition: reloptions.c:1174
static char ** options
void fillRelOptions(void *rdopts, Size basesize, relopt_value *options, int numoptions, bool validate, const relopt_parse_elt *elems, int numelems)
Definition: reloptions.c:1198
#define NULL
Definition: c.h:226
Definition: c.h:435
relopt_value * parseRelOptions(Datum options, bool validate, relopt_kind kind, int *numrelopts)
Definition: reloptions.c:975
#define offsetof(type, field)
Definition: c.h:551
void GinPageDeletePostingItem ( Page  page,
OffsetNumber  offset 
)

Definition at line 413 of file gindatapage.c.

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

Referenced by ginDeletePage(), and ginRedoDeletePage().

414 {
415  OffsetNumber maxoff = GinPageGetOpaque(page)->maxoff;
416 
417  Assert(!GinPageIsLeaf(page));
418  Assert(offset >= FirstOffsetNumber && offset <= maxoff);
419 
420  if (offset != maxoff)
421  memmove(GinDataPageGetPostingItem(page, offset),
422  GinDataPageGetPostingItem(page, offset + 1),
423  sizeof(PostingItem) * (maxoff - offset));
424 
425  maxoff--;
426  GinPageGetOpaque(page)->maxoff = maxoff;
427 
428  GinDataPageSetDataSize(page, maxoff * sizeof(PostingItem));
429 }
#define GinPageGetOpaque(page)
Definition: ginblock.h:109
uint16 OffsetNumber
Definition: off.h:24
#define GinDataPageSetDataSize(page, size)
Definition: ginblock.h:293
#define GinDataPageGetPostingItem(page, i)
Definition: ginblock.h:282
#define memmove(d, s, c)
Definition: c.h:1058
#define FirstOffsetNumber
Definition: off.h:27
#define GinPageIsLeaf(page)
Definition: ginblock.h:111
#define Assert(condition)
Definition: c.h:671
ItemPointer ginPostingListDecode ( GinPostingList ptr,
int *  ndecoded 
)

Definition at line 273 of file ginpostinglist.c.

References ginPostingListDecodeAllSegments(), and SizeOfGinPostingList.

Referenced by addItemsToLeaf(), dataBeginPlaceToPageLeaf(), gin_leafpage_items(), ginCompressPostingList(), ginReadTuple(), ginRedoRecompress(), ginVacuumEntryPage(), ginVacuumPostingTreeLeaf(), and leafRepackItems().

274 {
275  return ginPostingListDecodeAllSegments(plist,
276  SizeOfGinPostingList(plist),
277  ndecoded);
278 }
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:326
ItemPointer ginPostingListDecodeAllSegments(GinPostingList *segment, int len, int *ndecoded_out)
ItemPointer ginPostingListDecodeAllSegments ( GinPostingList ptr,
int  len,
int *  ndecoded 
)

Definition at line 286 of file ginpostinglist.c.

References Assert, GinPostingList::bytes, decode_varbyte(), GinPostingList::first, ginCompareItemPointers(), GinNextPostingListSegment, ItemPointerGetOffsetNumber, itemptr_to_uint64(), GinPostingList::nbytes, OffsetNumberIsValid, palloc(), repalloc(), uint64_to_itemptr(), and val.

Referenced by GinDataLeafPageGetItems(), ginPostingListDecode(), and ginPostingListDecodeAllSegmentsToTbm().

287 {
288  ItemPointer result;
289  int nallocated;
290  uint64 val;
291  char *endseg = ((char *) segment) + len;
292  int ndecoded;
293  unsigned char *ptr;
294  unsigned char *endptr;
295 
296  /*
297  * Guess an initial size of the array.
298  */
299  nallocated = segment->nbytes * 2 + 1;
300  result = palloc(nallocated * sizeof(ItemPointerData));
301 
302  ndecoded = 0;
303  while ((char *) segment < endseg)
304  {
305  /* enlarge output array if needed */
306  if (ndecoded >= nallocated)
307  {
308  nallocated *= 2;
309  result = repalloc(result, nallocated * sizeof(ItemPointerData));
310  }
311 
312  /* copy the first item */
314  Assert(ndecoded == 0 || ginCompareItemPointers(&segment->first, &result[ndecoded - 1]) > 0);
315  result[ndecoded] = segment->first;
316  ndecoded++;
317 
318  val = itemptr_to_uint64(&segment->first);
319  ptr = segment->bytes;
320  endptr = segment->bytes + segment->nbytes;
321  while (ptr < endptr)
322  {
323  /* enlarge output array if needed */
324  if (ndecoded >= nallocated)
325  {
326  nallocated *= 2;
327  result = repalloc(result, nallocated * sizeof(ItemPointerData));
328  }
329 
330  val += decode_varbyte(&ptr);
331 
332  uint64_to_itemptr(val, &result[ndecoded]);
333  ndecoded++;
334  }
335  segment = GinNextPostingListSegment(segment);
336  }
337 
338  if (ndecoded_out)
339  *ndecoded_out = ndecoded;
340  return result;
341 }
static uint64 decode_varbyte(unsigned char **ptr)
#define GinNextPostingListSegment(cur)
Definition: ginblock.h:327
static void uint64_to_itemptr(uint64 val, ItemPointer iptr)
uint16 nbytes
Definition: ginblock.h:322
#define Assert(condition)
Definition: c.h:671
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:76
static uint64 itemptr_to_uint64(const ItemPointer iptr)
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:461
void * palloc(Size size)
Definition: mcxt.c:891
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:40
long val
Definition: informix.c:689
int ginPostingListDecodeAllSegmentsToTbm ( GinPostingList ptr,
int  totalsize,
TIDBitmap tbm 
)

Definition at line 347 of file ginpostinglist.c.

References ginPostingListDecodeAllSegments(), pfree(), and tbm_add_tuples().

Referenced by GinDataLeafPageGetItemsToTbm().

349 {
350  int ndecoded;
351  ItemPointer items;
352 
353  items = ginPostingListDecodeAllSegments(ptr, len, &ndecoded);
354  tbm_add_tuples(tbm, items, ndecoded, false);
355  pfree(items);
356 
357  return ndecoded;
358 }
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:290
void pfree(void *pointer)
Definition: mcxt.c:992
ItemPointer ginPostingListDecodeAllSegments(GinPostingList *segment, int len, int *ndecoded_out)
void ginPrepareDataScan ( GinBtree  btree,
Relation  index,
BlockNumber  rootBlkno 
)

Definition at line 1861 of file gindatapage.c.

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

Referenced by ginInsertItemPointers(), and ginScanBeginPostingTree().

1862 {
1863  memset(btree, 0, sizeof(GinBtreeData));
1864 
1865  btree->index = index;
1866  btree->rootBlkno = rootBlkno;
1867 
1868  btree->findChildPage = dataLocateItem;
1870  btree->isMoveRight = dataIsMoveRight;
1871  btree->findItem = NULL;
1872  btree->findChildPtr = dataFindChildPtr;
1875  btree->fillRoot = ginDataFillRoot;
1877 
1878  btree->isData = TRUE;
1879  btree->fullScan = FALSE;
1880  btree->isBuild = FALSE;
1881 }
static BlockNumber dataLocateItem(GinBtree btree, GinBtreeStack *stack)
Definition: gindatapage.c:248
BlockNumber rootBlkno
Definition: gin_private.h:161
OffsetNumber(* findChildPtr)(GinBtree, Page, BlockNumber, OffsetNumber)
Definition: gin_private.h:152
static GinPlaceToPageRC dataBeginPlaceToPage(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void **ptp_workspace, Page *newlpage, Page *newrpage)
Definition: gindatapage.c:1191
bool(* findItem)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:149
BlockNumber(* getLeftMostChild)(GinBtree, Page)
Definition: gin_private.h:147
static bool dataIsMoveRight(GinBtree btree, Page page)
Definition: gindatapage.c:233
#define FALSE
Definition: c.h:218
static OffsetNumber dataFindChildPtr(GinBtree btree, Page page, BlockNumber blkno, OffsetNumber storedOff)
Definition: gindatapage.c:315
BlockNumber(* findChildPage)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:146
Relation index
Definition: gin_private.h:160
#define NULL
Definition: c.h:226
void *(* prepareDownlink)(GinBtree, Buffer)
Definition: gin_private.h:155
void ginDataFillRoot(GinBtree btree, Page root, BlockNumber lblkno, Page lpage, BlockNumber rblkno, Page rpage)
Definition: gindatapage.c:1339
#define TRUE
Definition: c.h:214
static void * dataPrepareDownlink(GinBtree btree, Buffer lbuf)
Definition: gindatapage.c:1323
static void dataExecPlaceToPage(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, void *ptp_workspace)
Definition: gindatapage.c:1221
void(* fillRoot)(GinBtree, Page, BlockNumber, Page, BlockNumber, Page)
Definition: gin_private.h:156
static BlockNumber dataGetLeftMostPage(GinBtree btree, Page page)
Definition: gindatapage.c:360
void(* execPlaceToPage)(GinBtree, Buffer, GinBtreeStack *, void *, BlockNumber, void *)
Definition: gin_private.h:154
bool(* isMoveRight)(GinBtree, Page)
Definition: gin_private.h:148
GinPlaceToPageRC(* beginPlaceToPage)(GinBtree, Buffer, GinBtreeStack *, void *, BlockNumber, void **, Page *, Page *)
Definition: gin_private.h:153
void ginPrepareEntryScan ( GinBtree  btree,
OffsetNumber  attnum,
Datum  key,
GinNullCategory  category,
GinState ginstate 
)

Definition at line 745 of file ginentrypage.c.

References GinBtreeData::beginPlaceToPage, GinBtreeData::entryAttnum, entryBeginPlaceToPage(), GinBtreeData::entryCategory, entryExecPlaceToPage(), entryFindChildPtr(), entryGetLeftMostPage(), entryIsMoveRight(), GinBtreeData::entryKey, entryLocateEntry(), entryLocateLeafEntry(), entryPrepareDownlink(), GinBtreeData::execPlaceToPage, FALSE, GinBtreeData::fillRoot, GinBtreeData::findChildPage, GinBtreeData::findChildPtr, GinBtreeData::findItem, GinBtreeData::fullScan, GinBtreeData::getLeftMostChild, GIN_ROOT_BLKNO, ginEntryFillRoot(), GinBtreeData::ginstate, GinState::index, GinBtreeData::index, GinBtreeData::isBuild, GinBtreeData::isData, GinBtreeData::isMoveRight, GinBtreeData::prepareDownlink, and GinBtreeData::rootBlkno.

Referenced by ginEntryInsert(), and startScanEntry().

748 {
749  memset(btree, 0, sizeof(GinBtreeData));
750 
751  btree->index = ginstate->index;
752  btree->rootBlkno = GIN_ROOT_BLKNO;
753  btree->ginstate = ginstate;
754 
757  btree->isMoveRight = entryIsMoveRight;
762  btree->fillRoot = ginEntryFillRoot;
764 
765  btree->isData = FALSE;
766  btree->fullScan = FALSE;
767  btree->isBuild = FALSE;
768 
769  btree->entryAttnum = attnum;
770  btree->entryKey = key;
771  btree->entryCategory = category;
772 }
static bool entryIsMoveRight(GinBtree btree, Page page)
Definition: ginentrypage.c:244
GinState * ginstate
Definition: gin_private.h:162
Relation index
Definition: gin_private.h:53
static BlockNumber entryLocateEntry(GinBtree btree, GinBtreeStack *stack)
Definition: ginentrypage.c:271
static OffsetNumber entryFindChildPtr(GinBtree btree, Page page, BlockNumber blkno, OffsetNumber storedOff)
Definition: ginentrypage.c:406
BlockNumber rootBlkno
Definition: gin_private.h:161
OffsetNumber(* findChildPtr)(GinBtree, Page, BlockNumber, OffsetNumber)
Definition: gin_private.h:152
OffsetNumber entryAttnum
Definition: gin_private.h:167
bool(* findItem)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:149
BlockNumber(* getLeftMostChild)(GinBtree, Page)
Definition: gin_private.h:147
#define FALSE
Definition: c.h:218
Datum entryKey
Definition: gin_private.h:168
void ginEntryFillRoot(GinBtree btree, Page root, BlockNumber lblkno, Page lpage, BlockNumber rblkno, Page rpage)
Definition: ginentrypage.c:721
GinNullCategory entryCategory
Definition: gin_private.h:169
BlockNumber(* findChildPage)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:146
static bool entryLocateLeafEntry(GinBtree btree, GinBtreeStack *stack)
Definition: ginentrypage.c:347
static BlockNumber entryGetLeftMostPage(GinBtree btree, Page page)
Definition: ginentrypage.c:447
static GinPlaceToPageRC entryBeginPlaceToPage(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertPayload, BlockNumber updateblkno, void **ptp_workspace, Page *newlpage, Page *newrpage)
Definition: ginentrypage.c:528
static void entryExecPlaceToPage(GinBtree btree, Buffer buf, GinBtreeStack *stack, void *insertPayload, BlockNumber updateblkno, void *ptp_workspace)
Definition: ginentrypage.c:555
Relation index
Definition: gin_private.h:160
static void * entryPrepareDownlink(GinBtree btree, Buffer lbuf)
Definition: ginentrypage.c:700
void *(* prepareDownlink)(GinBtree, Buffer)
Definition: gin_private.h:155
void(* fillRoot)(GinBtree, Page, BlockNumber, Page, BlockNumber, Page)
Definition: gin_private.h:156
void(* execPlaceToPage)(GinBtree, Buffer, GinBtreeStack *, void *, BlockNumber, void *)
Definition: gin_private.h:154
#define GIN_ROOT_BLKNO
Definition: ginblock.h:51
bool(* isMoveRight)(GinBtree, Page)
Definition: gin_private.h:148
GinPlaceToPageRC(* beginPlaceToPage)(GinBtree, Buffer, GinBtreeStack *, void *, BlockNumber, void **, Page *, Page *)
Definition: gin_private.h:153
ItemPointer ginReadTuple ( GinState ginstate,
OffsetNumber  attnum,
IndexTuple  itup,
int *  nitems 
)

Definition at line 163 of file ginentrypage.c.

References elog, ERROR, GinGetNPosting, GinGetPosting, GinItupIsCompressed, ginPostingListDecode(), and palloc().

Referenced by addItemPointersToLeafTuple(), collectMatchBitmap(), and startScanEntry().

165 {
166  Pointer ptr = GinGetPosting(itup);
167  int nipd = GinGetNPosting(itup);
168  ItemPointer ipd;
169  int ndecoded;
170 
171  if (GinItupIsCompressed(itup))
172  {
173  if (nipd > 0)
174  {
175  ipd = ginPostingListDecode((GinPostingList *) ptr, &ndecoded);
176  if (nipd != ndecoded)
177  elog(ERROR, "number of items mismatch in GIN entry tuple, %d in tuple header, %d decoded",
178  nipd, ndecoded);
179  }
180  else
181  {
182  ipd = palloc(0);
183  }
184  }
185  else
186  {
187  ipd = (ItemPointer) palloc(sizeof(ItemPointerData) * nipd);
188  memcpy(ipd, ptr, sizeof(ItemPointerData) * nipd);
189  }
190  *nitems = nipd;
191  return ipd;
192 }
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded)
#define GinGetNPosting(itup)
Definition: ginblock.h:212
#define GinGetPosting(itup)
Definition: ginblock.h:222
#define GinItupIsCompressed(itup)
Definition: ginblock.h:223
ItemPointerData * ItemPointer
Definition: itemptr.h:48
char * Pointer
Definition: c.h:242
#define ERROR
Definition: elog.h:43
void * palloc(Size size)
Definition: mcxt.c:891
#define elog
Definition: elog.h:219
void ginrescan ( IndexScanDesc  scan,
ScanKey  key,
int  nscankeys,
ScanKey  orderbys,
int  norderbys 
)

Definition at line 416 of file ginscan.c.

References ginFreeScanKeys(), IndexScanDescData::keyData, memmove, IndexScanDescData::numberOfKeys, and IndexScanDescData::opaque.

Referenced by ginhandler().

418 {
419  GinScanOpaque so = (GinScanOpaque) scan->opaque;
420 
421  ginFreeScanKeys(so);
422 
423  if (scankey && scan->numberOfKeys > 0)
424  {
425  memmove(scan->keyData, scankey,
426  scan->numberOfKeys * sizeof(ScanKeyData));
427  }
428 }
GinScanOpaqueData * GinScanOpaque
Definition: gin_private.h:364
#define memmove(d, s, c)
Definition: c.h:1058
ScanKey keyData
Definition: relscan.h:93
void ginFreeScanKeys(GinScanOpaque so)
Definition: ginscan.c:232
GinBtreeStack* ginScanBeginPostingTree ( GinBtree  btree,
Relation  index,
BlockNumber  rootBlkno,
Snapshot  snapshot 
)

Definition at line 1915 of file gindatapage.c.

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

Referenced by scanPostingTree(), and startScanEntry().

1917 {
1918  GinBtreeStack *stack;
1919 
1920  ginPrepareDataScan(btree, index, rootBlkno);
1921 
1922  btree->fullScan = TRUE;
1923 
1924  stack = ginFindLeafPage(btree, TRUE, snapshot);
1925 
1926  return stack;
1927 }
GinBtreeStack * ginFindLeafPage(GinBtree btree, bool searchMode, Snapshot snapshot)
Definition: ginbtree.c:76
#define TRUE
Definition: c.h:214
void ginPrepareDataScan(GinBtree btree, Relation index, BlockNumber rootBlkno)
Definition: gindatapage.c:1861
Buffer ginStepRight ( Buffer  buffer,
Relation  index,
int  lockmode 
)

Definition at line 165 of file ginbtree.c.

References BufferGetPage, elog, ERROR, GinPageGetOpaque, GinPageIsData, GinPageIsDeleted, GinPageIsLeaf, LockBuffer(), ReadBuffer(), and UnlockReleaseBuffer().

Referenced by entryLoadMoreItems(), ginFindLeafPage(), ginFindParents(), ginFinishSplit(), moveRightIfItNeeded(), and scanPostingTree().

166 {
167  Buffer nextbuffer;
168  Page page = BufferGetPage(buffer);
169  bool isLeaf = GinPageIsLeaf(page);
170  bool isData = GinPageIsData(page);
171  BlockNumber blkno = GinPageGetOpaque(page)->rightlink;
172 
173  nextbuffer = ReadBuffer(index, blkno);
174  LockBuffer(nextbuffer, lockmode);
175  UnlockReleaseBuffer(buffer);
176 
177  /* Sanity check that the page we stepped to is of similar kind. */
178  page = BufferGetPage(nextbuffer);
179  if (isLeaf != GinPageIsLeaf(page) || isData != GinPageIsData(page))
180  elog(ERROR, "right sibling of GIN page is of different type");
181 
182  /*
183  * Given the proper lock sequence above, we should never land on a deleted
184  * page.
185  */
186  if (GinPageIsDeleted(page))
187  elog(ERROR, "right sibling of GIN page was deleted");
188 
189  return nextbuffer;
190 }
uint32 BlockNumber
Definition: block.h:31
#define GinPageGetOpaque(page)
Definition: ginblock.h:109
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
#define ERROR
Definition: elog.h:43
#define GinPageIsLeaf(page)
Definition: ginblock.h:111
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define GinPageIsDeleted(page)
Definition: ginblock.h:123
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
#define GinPageIsData(page)
Definition: ginblock.h:114
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define elog
Definition: elog.h:219
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
OffsetNumber gintuple_get_attrnum ( GinState ginstate,
IndexTuple  tuple 
)

Definition at line 213 of file ginutil.c.

References Assert, DatumGetUInt16, FirstOffsetNumber, index_getattr, GinState::oneCol, and GinState::tupdesc.

Referenced by addItemPointersToLeafTuple(), collectMatchBitmap(), collectMatchesForHeapRow(), entryIsMoveRight(), entryLocateEntry(), entryLocateLeafEntry(), gintuple_get_key(), ginVacuumEntryPage(), matchPartialInPendingList(), and processPendingPage().

214 {
215  OffsetNumber colN;
216 
217  if (ginstate->oneCol)
218  {
219  /* column number is not stored explicitly */
220  colN = FirstOffsetNumber;
221  }
222  else
223  {
224  Datum res;
225  bool isnull;
226 
227  /*
228  * First attribute is always int16, so we can safely use any tuple
229  * descriptor to obtain first attribute of tuple
230  */
231  res = index_getattr(tuple, FirstOffsetNumber, ginstate->tupdesc[0],
232  &isnull);
233  Assert(!isnull);
234 
235  colN = DatumGetUInt16(res);
236  Assert(colN >= FirstOffsetNumber && colN <= ginstate->origTupdesc->natts);
237  }
238 
239  return colN;
240 }
#define DatumGetUInt16(X)
Definition: postgres.h:466
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
uintptr_t Datum
Definition: postgres.h:374
TupleDesc tupdesc[INDEX_MAX_KEYS]
Definition: gin_private.h:68
#define Assert(condition)
Definition: c.h:671
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
bool oneCol
Definition: gin_private.h:54
Datum gintuple_get_key ( GinState ginstate,
IndexTuple  tuple,
GinNullCategory category 
)

Definition at line 246 of file ginutil.c.

References FirstOffsetNumber, GIN_CAT_NORM_KEY, GinGetNullCategory, gintuple_get_attrnum(), index_getattr, OffsetNumberNext, GinState::oneCol, GinState::origTupdesc, and GinState::tupdesc.

Referenced by addItemPointersToLeafTuple(), collectMatchBitmap(), collectMatchesForHeapRow(), entryIsMoveRight(), entryLocateEntry(), entryLocateLeafEntry(), ginVacuumEntryPage(), matchPartialInPendingList(), and processPendingPage().

248 {
249  Datum res;
250  bool isnull;
251 
252  if (ginstate->oneCol)
253  {
254  /*
255  * Single column index doesn't store attribute numbers in tuples
256  */
257  res = index_getattr(tuple, FirstOffsetNumber, ginstate->origTupdesc,
258  &isnull);
259  }
260  else
261  {
262  /*
263  * Since the datum type depends on which index column it's from, we
264  * must be careful to use the right tuple descriptor here.
265  */
266  OffsetNumber colN = gintuple_get_attrnum(ginstate, tuple);
267 
269  ginstate->tupdesc[colN - 1],
270  &isnull);
271  }
272 
273  if (isnull)
274  *category = GinGetNullCategory(tuple, ginstate);
275  else
276  *category = GIN_CAT_NORM_KEY;
277 
278  return res;
279 }
#define GinGetNullCategory(itup, ginstate)
Definition: ginblock.h:204
uint16 OffsetNumber
Definition: off.h:24
OffsetNumber gintuple_get_attrnum(GinState *ginstate, IndexTuple tuple)
Definition: ginutil.c:213
#define GIN_CAT_NORM_KEY
Definition: ginblock.h:192
#define FirstOffsetNumber
Definition: off.h:27
uintptr_t Datum
Definition: postgres.h:374
TupleDesc tupdesc[INDEX_MAX_KEYS]
Definition: gin_private.h:68
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
TupleDesc origTupdesc
Definition: gin_private.h:67
bool oneCol
Definition: gin_private.h:54
IndexBulkDeleteResult* ginvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)

Definition at line 641 of file ginvacuum.c.

References IndexVacuumInfo::analyze_only, Assert, DataPageDeleteStack::blkno, BufferGetPage, IndexVacuumInfo::estimated_count, IndexBulkDeleteResult::estimated_count, ExclusiveLock, GIN_ROOT_BLKNO, GIN_SHARE, ginInsertCleanup(), GinPageIsData, GinPageIsDeleted, GinPageIsLeaf, GinPageIsList, ginUpdateStats(), IndexVacuumInfo::index, IndexFreeSpaceMapVacuum(), initGinState(), IsAutoVacuumWorkerProcess(), LockBuffer(), LockRelationForExtension(), MAIN_FORKNUM, GinStatsData::nDataPages, GinStatsData::nEntries, GinStatsData::nEntryPages, GinStatsData::nTotalPages, NULL, IndexVacuumInfo::num_heap_tuples, IndexBulkDeleteResult::num_index_tuples, IndexBulkDeleteResult::num_pages, PageGetMaxOffsetNumber, PageIsNew, IndexBulkDeleteResult::pages_free, palloc0(), RBM_NORMAL, ReadBufferExtended(), RecordFreeIndexPage(), RELATION_IS_LOCAL, RelationGetNumberOfBlocks, IndexVacuumInfo::strategy, UnlockRelationForExtension(), UnlockReleaseBuffer(), and vacuum_delay_point().

Referenced by ginhandler().

642 {
643  Relation index = info->index;
644  bool needLock;
645  BlockNumber npages,
646  blkno;
647  BlockNumber totFreePages;
648  GinState ginstate;
649  GinStatsData idxStat;
650 
651  /*
652  * In an autovacuum analyze, we want to clean up pending insertions.
653  * Otherwise, an ANALYZE-only call is a no-op.
654  */
655  if (info->analyze_only)
656  {
658  {
659  initGinState(&ginstate, index);
660  ginInsertCleanup(&ginstate, false, true, stats);
661  }
662  return stats;
663  }
664 
665  /*
666  * Set up all-zero stats and cleanup pending inserts if ginbulkdelete
667  * wasn't called
668  */
669  if (stats == NULL)
670  {
672  initGinState(&ginstate, index);
674  false, stats);
675  }
676 
677  memset(&idxStat, 0, sizeof(idxStat));
678 
679  /*
680  * XXX we always report the heap tuple count as the number of index
681  * entries. This is bogus if the index is partial, but it's real hard to
682  * tell how many distinct heap entries are referenced by a GIN index.
683  */
684  stats->num_index_tuples = info->num_heap_tuples;
685  stats->estimated_count = info->estimated_count;
686 
687  /*
688  * Need lock unless it's local to this backend.
689  */
690  needLock = !RELATION_IS_LOCAL(index);
691 
692  if (needLock)
694  npages = RelationGetNumberOfBlocks(index);
695  if (needLock)
697 
698  totFreePages = 0;
699 
700  for (blkno = GIN_ROOT_BLKNO; blkno < npages; blkno++)
701  {
702  Buffer buffer;
703  Page page;
704 
706 
707  buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno,
708  RBM_NORMAL, info->strategy);
709  LockBuffer(buffer, GIN_SHARE);
710  page = (Page) BufferGetPage(buffer);
711 
712  if (PageIsNew(page) || GinPageIsDeleted(page))
713  {
714  Assert(blkno != GIN_ROOT_BLKNO);
715  RecordFreeIndexPage(index, blkno);
716  totFreePages++;
717  }
718  else if (GinPageIsData(page))
719  {
720  idxStat.nDataPages++;
721  }
722  else if (!GinPageIsList(page))
723  {
724  idxStat.nEntryPages++;
725 
726  if (GinPageIsLeaf(page))
727  idxStat.nEntries += PageGetMaxOffsetNumber(page);
728  }
729 
730  UnlockReleaseBuffer(buffer);
731  }
732 
733  /* Update the metapage with accurate page and entry counts */
734  idxStat.nTotalPages = npages;
735  ginUpdateStats(info->index, &idxStat);
736 
737  /* Finally, vacuum the FSM */
739 
740  stats->pages_free = totFreePages;
741 
742  if (needLock)
744  stats->num_pages = RelationGetNumberOfBlocks(index);
745  if (needLock)
747 
748  return stats;
749 }
BlockNumber nEntryPages
Definition: gin.h:45
#define ExclusiveLock
Definition: lockdefs.h:44
#define RELATION_IS_LOCAL(relation)
Definition: rel.h:520
bool analyze_only
Definition: genam.h:47
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
BufferAccessStrategy strategy
Definition: genam.h:51
void ginInsertCleanup(GinState *ginstate, bool full_clean, bool fill_fsm, IndexBulkDeleteResult *stats)
Definition: ginfast.c:729
Relation index
Definition: genam.h:46
int64 nEntries
Definition: gin.h:47
uint32 BlockNumber
Definition: block.h:31
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
Definition: type.h:90
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
BlockNumber num_pages
Definition: genam.h:73
BlockNumber pages_free
Definition: genam.h:79
void ginUpdateStats(Relation index, const GinStatsData *stats)
Definition: ginutil.c:659
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:86
#define GinPageIsLeaf(page)
Definition: ginblock.h:111
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:2988
#define GIN_SHARE
Definition: gin_private.h:44
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:332
void * palloc0(Size size)
Definition: mcxt.c:920
#define GinPageIsDeleted(page)
Definition: ginblock.h:123
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:382
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
BlockNumber nDataPages
Definition: gin.h:46
#define GinPageIsData(page)
Definition: ginblock.h:114
double num_heap_tuples
Definition: genam.h:50
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void IndexFreeSpaceMapVacuum(Relation rel)
Definition: indexfsm.c:71
#define GinPageIsList(page)
Definition: ginblock.h:116
BlockNumber nTotalPages
Definition: gin.h:44
#define PageIsNew(page)
Definition: bufpage.h:226
void vacuum_delay_point(void)
Definition: vacuum.c:1515
void RecordFreeIndexPage(Relation rel, BlockNumber freeBlock)
Definition: indexfsm.c:52
double num_index_tuples
Definition: genam.h:76
int Buffer
Definition: buf.h:23
bool estimated_count
Definition: genam.h:75
#define GIN_ROOT_BLKNO
Definition: ginblock.h:51
Pointer Page
Definition: bufpage.h:74
bool estimated_count
Definition: genam.h:48
ItemPointer ginVacuumItemPointers ( GinVacuumState gvs,
ItemPointerData items,
int  nitem,
int *  nremaining 
)

Definition at line 47 of file ginvacuum.c.

References GinVacuumState::callback, GinVacuumState::callback_state, i, NULL, IndexBulkDeleteResult::num_index_tuples, palloc(), remaining, GinVacuumState::result, and IndexBulkDeleteResult::tuples_removed.

Referenced by ginVacuumEntryPage(), and ginVacuumPostingTreeLeaf().

49 {
50  int i,
51  remaining = 0;
52  ItemPointer tmpitems = NULL;
53 
54  /*
55  * Iterate over TIDs array
56  */
57  for (i = 0; i < nitem; i++)
58  {
59  if (gvs->callback(items + i, gvs->callback_state))
60  {
61  gvs->result->tuples_removed += 1;
62  if (!tmpitems)
63  {
64  /*
65  * First TID to be deleted: allocate memory to hold the
66  * remaining items.
67  */
68  tmpitems = palloc(sizeof(ItemPointerData) * nitem);
69  memcpy(tmpitems, items, sizeof(ItemPointerData) * i);
70  }
71  }
72  else
73  {
74  gvs->result->num_index_tuples += 1;
75  if (tmpitems)
76  tmpitems[remaining] = items[i];
77  remaining++;
78  }
79  }
80 
81  *nremaining = remaining;
82  return tmpitems;
83 }
int remaining
Definition: informix.c:692
double tuples_removed
Definition: genam.h:77
IndexBulkDeleteResult * result
Definition: ginvacuum.c:30
IndexBulkDeleteCallback callback
Definition: ginvacuum.c:31
void * callback_state
Definition: ginvacuum.c:32
#define NULL
Definition: c.h:226
void * palloc(Size size)
Definition: mcxt.c:891
int i
double num_index_tuples
Definition: genam.h:76
void ginVacuumPostingTreeLeaf ( Relation  rel,
Buffer  buf,
GinVacuumState gvs 
)

Definition at line 731 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, NULL, 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().

732 {
733  Page page = BufferGetPage(buffer);
734  disassembledLeaf *leaf;
735  bool removedsomething = false;
736  dlist_iter iter;
737 
738  leaf = disassembleLeaf(page);
739 
740  /* Vacuum each segment. */
741  dlist_foreach(iter, &leaf->segments)
742  {
743  leafSegmentInfo *seginfo = dlist_container(leafSegmentInfo, node, iter.cur);
744  int oldsegsize;
745  ItemPointer cleaned;
746  int ncleaned;
747 
748  if (!seginfo->items)
749  seginfo->items = ginPostingListDecode(seginfo->seg,
750  &seginfo->nitems);
751  if (seginfo->seg)
752  oldsegsize = SizeOfGinPostingList(seginfo->seg);
753  else
754  oldsegsize = GinDataPageMaxDataSize;
755 
756  cleaned = ginVacuumItemPointers(gvs,
757  seginfo->items,
758  seginfo->nitems,
759  &ncleaned);
760  pfree(seginfo->items);
761  seginfo->items = NULL;
762  seginfo->nitems = 0;
763  if (cleaned)
764  {
765  if (ncleaned > 0)
766  {
767  int npacked;
768 
769  seginfo->seg = ginCompressPostingList(cleaned,
770  ncleaned,
771  oldsegsize,
772  &npacked);
773  /* Removing an item never increases the size of the segment */
774  if (npacked != ncleaned)
775  elog(ERROR, "could not fit vacuumed posting list");
776  seginfo->action = GIN_SEGMENT_REPLACE;
777  }
778  else
779  {
780  seginfo->seg = NULL;
781  seginfo->items = NULL;
782  seginfo->action = GIN_SEGMENT_DELETE;
783  }
784  seginfo->nitems = ncleaned;
785 
786  removedsomething = true;
787  }
788  }
789 
790  /*
791  * If we removed any items, reconstruct the page from the pieces.
792  *
793  * We don't try to re-encode the segments here, even though some of them
794  * might be really small now that we've removed some items from them. It
795  * seems like a waste of effort, as there isn't really any benefit from
796  * larger segments per se; larger segments only help to pack more items in
797  * the same space. We might as well delay doing that until the next
798  * insertion, which will need to re-encode at least part of the page
799  * anyway.
800  *
801  * Also note if the page was in uncompressed, pre-9.4 format before, it is
802  * now represented as one huge segment that contains all the items. It
803  * might make sense to split that, to speed up random access, but we don't
804  * bother. You'll have to REINDEX anyway if you want the full gain of the
805  * new tighter index format.
806  */
807  if (removedsomething)
808  {
809  bool modified;
810 
811  /*
812  * Make sure we have a palloc'd copy of all segments, after the first
813  * segment that is modified. (dataPlaceToPageLeafRecompress requires
814  * this).