PostgreSQL Source Code  git master
gin_private.h File Reference
#include "access/amapi.h"
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
#include "storage/bufmgr.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, bool rootConflictCheck, 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, Buffer entrybuffer)
 
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 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 ginadjustmembers (Oid opfamilyoid, Oid opclassoid, List *operators, List *functions)
 
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, bool forceCleanup, IndexBulkDeleteResult *stats)
 
GinPostingListginCompressPostingList (const ItemPointer ipd, int nipd, 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)
 
int ginTraverseLock (Buffer buffer, bool searchMode)
 

Macro Definition Documentation

◆ GIN_DEFAULT_USE_FASTUPDATE

#define GIN_DEFAULT_USE_FASTUPDATE   true

Definition at line 32 of file gin_private.h.

◆ GIN_EXCLUSIVE

◆ GIN_SHARE

◆ GIN_UNLOCK

◆ GinGetPendingListCleanupSize

#define GinGetPendingListCleanupSize (   relation)
Value:
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
relation->rd_rel->relam == GIN_AM_OID), \
(relation)->rd_options && \
((GinOptions *) (relation)->rd_options)->pendingListCleanupSize != -1 ? \
((GinOptions *) (relation)->rd_options)->pendingListCleanupSize : \
#define AssertMacro(condition)
Definition: c.h:801
int gin_pending_list_limit
Definition: ginfast.c:39

Definition at line 38 of file gin_private.h.

Referenced by ginHeapTupleFastInsert().

◆ GinGetUseFastUpdate

#define GinGetUseFastUpdate (   relation)
Value:
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
relation->rd_rel->relam == GIN_AM_OID), \
(relation)->rd_options ? \
((GinOptions *) (relation)->rd_options)->useFastUpdate : GIN_DEFAULT_USE_FASTUPDATE)
#define AssertMacro(condition)
Definition: c.h:801
#define GIN_DEFAULT_USE_FASTUPDATE
Definition: gin_private.h:32

Definition at line 33 of file gin_private.h.

Referenced by gininsert().

Typedef Documentation

◆ GinBtree

typedef struct GinBtreeData* GinBtree

Definition at line 138 of file gin_private.h.

◆ GinBtreeData

typedef struct GinBtreeData GinBtreeData

◆ GinBtreeStack

typedef struct GinBtreeStack GinBtreeStack

◆ GinEntryAccumulator

◆ GinOptions

typedef struct GinOptions GinOptions

◆ GinScanEntry

typedef struct GinScanEntryData* GinScanEntry

Definition at line 263 of file gin_private.h.

◆ GinScanEntryData

◆ GinScanKey

typedef struct GinScanKeyData* GinScanKey

Definition at line 261 of file gin_private.h.

◆ GinScanKeyData

◆ GinScanOpaque

Definition at line 383 of file gin_private.h.

◆ GinScanOpaqueData

◆ GinState

typedef struct GinState GinState

◆ GinTupleCollector

◆ GinVacuumState

Definition at line 240 of file gin_private.h.

Enumeration Type Documentation

◆ GinPlaceToPageRC

Enumerator
GPTP_NO_WORK 
GPTP_INSERT 
GPTP_SPLIT 

Definition at line 141 of file gin_private.h.

Function Documentation

◆ createPostingTree()

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

Definition at line 1769 of file gindatapage.c.

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

Referenced by addItemPointersToLeafTuple(), and buildFreshLeafTuple().

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

◆ freeGinBtreeStack()

void freeGinBtreeStack ( GinBtreeStack stack)

Definition at line 194 of file ginbtree.c.

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

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

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

◆ ginadjustmembers()

void ginadjustmembers ( Oid  opfamilyoid,
Oid  opclassoid,
List operators,
List functions 
)

Definition at line 279 of file ginvalidate.c.

References ereport, errcode(), errmsg(), ERROR, GIN_COMPARE_PARTIAL_PROC, GIN_COMPARE_PROC, GIN_CONSISTENT_PROC, GIN_EXTRACTQUERY_PROC, GIN_EXTRACTVALUE_PROC, GIN_OPTIONS_PROC, GIN_TRICONSISTENT_PROC, lfirst, OpFamilyMember::number, OpFamilyMember::ref_is_family, OpFamilyMember::ref_is_hard, and OpFamilyMember::refobjid.

Referenced by ginhandler().

283 {
284  ListCell *lc;
285 
286  /*
287  * Operator members of a GIN opfamily should never have hard dependencies,
288  * since their connection to the opfamily depends only on what the support
289  * functions think, and that can be altered. For consistency, we make all
290  * soft dependencies point to the opfamily, though a soft dependency on
291  * the opclass would work as well in the CREATE OPERATOR CLASS case.
292  */
293  foreach(lc, operators)
294  {
295  OpFamilyMember *op = (OpFamilyMember *) lfirst(lc);
296 
297  op->ref_is_hard = false;
298  op->ref_is_family = true;
299  op->refobjid = opfamilyoid;
300  }
301 
302  /*
303  * Required support functions should have hard dependencies. Preferably
304  * those are just dependencies on the opclass, but if we're in ALTER
305  * OPERATOR FAMILY, we leave the dependency pointing at the whole
306  * opfamily. (Given that GIN opclasses generally don't share opfamilies,
307  * it seems unlikely to be worth working harder.)
308  */
309  foreach(lc, functions)
310  {
311  OpFamilyMember *op = (OpFamilyMember *) lfirst(lc);
312 
313  switch (op->number)
314  {
317  /* Required support function */
318  op->ref_is_hard = true;
319  break;
320  case GIN_COMPARE_PROC:
321  case GIN_CONSISTENT_PROC:
324  case GIN_OPTIONS_PROC:
325  /* Optional, so force it to be a soft family dependency */
326  op->ref_is_hard = false;
327  op->ref_is_family = true;
328  op->refobjid = opfamilyoid;
329  break;
330  default:
331  ereport(ERROR,
332  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
333  errmsg("support function number %d is invalid for access method %s",
334  op->number, "gin")));
335  break;
336  }
337  }
338 }
bool ref_is_hard
Definition: amapi.h:88
#define GIN_EXTRACTQUERY_PROC
Definition: gin.h:24
int number
Definition: amapi.h:84
int errcode(int sqlerrcode)
Definition: elog.c:691
#define GIN_TRICONSISTENT_PROC
Definition: gin.h:27
#define GIN_COMPARE_PARTIAL_PROC
Definition: gin.h:26
#define ERROR
Definition: elog.h:43
#define GIN_CONSISTENT_PROC
Definition: gin.h:25
#define GIN_OPTIONS_PROC
Definition: gin.h:28
#define ereport(elevel,...)
Definition: elog.h:155
#define GIN_EXTRACTVALUE_PROC
Definition: gin.h:23
#define lfirst(lc)
Definition: pg_list.h:169
bool ref_is_family
Definition: amapi.h:89
int errmsg(const char *fmt,...)
Definition: elog.c:902
Oid refobjid
Definition: amapi.h:90
#define GIN_COMPARE_PROC
Definition: gin.h:22

◆ ginBeginBAScan()

void ginBeginBAScan ( BuildAccumulator accum)

Definition at line 257 of file ginbulk.c.

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

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

258 {
259  rbt_begin_iterate(accum->tree, LeftRightWalk, &accum->tree_walk);
260 }
void rbt_begin_iterate(RBTree *rbt, RBTOrderControl ctrl, RBTreeIterator *iter)
Definition: rbtree.c:740
RBTreeIterator tree_walk
Definition: gin_private.h:435

◆ ginbeginscan()

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, 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 }
#define AllocSetContextCreate
Definition: memutils.h:170
Relation indexRelation
Definition: relscan.h:115
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
GinScanOpaqueData * GinScanOpaque
Definition: gin_private.h:383
MemoryContext keyCtx
Definition: gin_private.h:378
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:95
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
GinScanKey keys
Definition: gin_private.h:371
MemoryContext tempCtx
Definition: gin_private.h:368
#define Assert(condition)
Definition: c.h:800
void * palloc(Size size)
Definition: mcxt.c:950
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:81

◆ ginbuild()

IndexBuildResult* ginbuild ( Relation  heap,
Relation  index,
struct IndexInfo indexInfo 
)

Definition at line 320 of file gininsert.c.

References GinBuildState::accum, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, attnum, 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, GinBuildState::indtuples, initGinState(), sort-test::key, sort-test::list, log_newpage_range(), MAIN_FORKNUM, MarkBufferDirty(), MemoryContextDelete(), MemoryContextSwitchTo(), GinStatsData::nEntryPages, palloc(), RelationGetNumberOfBlocks, RelationGetRelationName, RelationNeedsWAL, START_CRIT_SECTION, table_index_build_scan(), GinBuildState::tmpCtx, and UnlockReleaseBuffer().

Referenced by ginhandler().

321 {
322  IndexBuildResult *result;
323  double reltuples;
324  GinBuildState buildstate;
325  Buffer RootBuffer,
326  MetaBuffer;
328  Datum key;
329  GinNullCategory category;
330  uint32 nlist;
331  MemoryContext oldCtx;
333 
334  if (RelationGetNumberOfBlocks(index) != 0)
335  elog(ERROR, "index \"%s\" already contains data",
336  RelationGetRelationName(index));
337 
338  initGinState(&buildstate.ginstate, index);
339  buildstate.indtuples = 0;
340  memset(&buildstate.buildStats, 0, sizeof(GinStatsData));
341 
342  /* initialize the meta page */
343  MetaBuffer = GinNewBuffer(index);
344 
345  /* initialize the root page */
346  RootBuffer = GinNewBuffer(index);
347 
349  GinInitMetabuffer(MetaBuffer);
350  MarkBufferDirty(MetaBuffer);
351  GinInitBuffer(RootBuffer, GIN_LEAF);
352  MarkBufferDirty(RootBuffer);
353 
354 
355  UnlockReleaseBuffer(MetaBuffer);
356  UnlockReleaseBuffer(RootBuffer);
358 
359  /* count the root as first entry page */
360  buildstate.buildStats.nEntryPages++;
361 
362  /*
363  * create a temporary memory context that is used to hold data not yet
364  * dumped out to the index
365  */
367  "Gin build temporary context",
369 
370  /*
371  * create a temporary memory context that is used for calling
372  * ginExtractEntries(), and can be reset after each tuple
373  */
375  "Gin build temporary context for user-defined function",
377 
378  buildstate.accum.ginstate = &buildstate.ginstate;
379  ginInitBA(&buildstate.accum);
380 
381  /*
382  * Do the heap scan. We disallow sync scan here because dataPlaceToPage
383  * prefers to receive tuples in TID order.
384  */
385  reltuples = table_index_build_scan(heap, index, indexInfo, false, true,
386  ginBuildCallback, (void *) &buildstate,
387  NULL);
388 
389  /* dump remaining entries to the index */
390  oldCtx = MemoryContextSwitchTo(buildstate.tmpCtx);
391  ginBeginBAScan(&buildstate.accum);
392  while ((list = ginGetBAEntry(&buildstate.accum,
393  &attnum, &key, &category, &nlist)) != NULL)
394  {
395  /* there could be many entries, so be willing to abort here */
397  ginEntryInsert(&buildstate.ginstate, attnum, key, category,
398  list, nlist, &buildstate.buildStats);
399  }
400  MemoryContextSwitchTo(oldCtx);
401 
402  MemoryContextDelete(buildstate.funcCtx);
403  MemoryContextDelete(buildstate.tmpCtx);
404 
405  /*
406  * Update metapage stats
407  */
409  ginUpdateStats(index, &buildstate.buildStats, true);
410 
411  /*
412  * We didn't write WAL records as we built the index, so if WAL-logging is
413  * required, write all pages to the WAL now.
414  */
415  if (RelationNeedsWAL(index))
416  {
418  0, RelationGetNumberOfBlocks(index),
419  true);
420  }
421 
422  /*
423  * Return statistics
424  */
425  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
426 
427  result->heap_tuples = reltuples;
428  result->index_tuples = buildstate.indtuples;
429 
430  return result;
431 }
BlockNumber nEntryPages
Definition: gin.h:46
Buffer GinNewBuffer(Relation index)
Definition: ginutil.c:298
void GinInitMetabuffer(Buffer b)
Definition: ginutil.c:363
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
#define AllocSetContextCreate
Definition: memutils.h:170
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1471
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
GinStatsData buildStats
Definition: gininsert.c:34
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
static void ginBuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
Definition: gininsert.c:280
double indtuples
Definition: gininsert.c:33
uint16 OffsetNumber
Definition: off.h:24
GinState ginstate
Definition: gininsert.c:32
void ginEntryInsert(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gininsert.c:179
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3536
#define ERROR
Definition: elog.h:43
signed char GinNullCategory
Definition: ginblock.h:207
static double table_index_build_scan(Relation table_rel, Relation index_rel, struct IndexInfo *index_info, bool allow_sync, bool progress, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition: tableam.h:1552
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
BuildAccumulator accum
Definition: gininsert.c:37
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:95
#define RelationGetRelationName(relation)
Definition: rel.h:491
unsigned int uint32
Definition: c.h:429
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
GinState * ginstate
Definition: gin_private.h:430
uintptr_t Datum
Definition: postgres.h:367
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
int16 attnum
Definition: pg_attribute.h:79
void GinInitBuffer(Buffer b, uint32 f)
Definition: ginutil.c:357
BlockNumber nTotalPages
Definition: gin.h:45
ItemPointerData * ginGetBAEntry(BuildAccumulator *accum, OffsetNumber *attnum, Datum *key, GinNullCategory *category, uint32 *n)
Definition: ginbulk.c:268
#define RelationNeedsWAL(relation)
Definition: rel.h:563
void ginBeginBAScan(BuildAccumulator *accum)
Definition: ginbulk.c:257
void log_newpage_range(Relation rel, ForkNumber forkNum, BlockNumber startblk, BlockNumber endblk, bool page_std)
Definition: xloginsert.c:1123
void ginUpdateStats(Relation index, const GinStatsData *stats, bool is_build)
Definition: ginutil.c:657
void * palloc(Size size)
Definition: mcxt.c:950
MemoryContext tmpCtx
Definition: gininsert.c:35
#define elog(elevel,...)
Definition: elog.h:228
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
MemoryContext funcCtx
Definition: gininsert.c:36
int Buffer
Definition: buf.h:23
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:42

◆ ginbuildempty()

void ginbuildempty ( Relation  index)

Definition at line 437 of file gininsert.c.

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

Referenced by ginhandler().

438 {
439  Buffer RootBuffer,
440  MetaBuffer;
441 
442  /* An empty GIN index has two pages. */
443  MetaBuffer =
445  LockBuffer(MetaBuffer, BUFFER_LOCK_EXCLUSIVE);
446  RootBuffer =
448  LockBuffer(RootBuffer, BUFFER_LOCK_EXCLUSIVE);
449 
450  /* Initialize and xlog metabuffer and root buffer. */
452  GinInitMetabuffer(MetaBuffer);
453  MarkBufferDirty(MetaBuffer);
454  log_newpage_buffer(MetaBuffer, true);
455  GinInitBuffer(RootBuffer, GIN_LEAF);
456  MarkBufferDirty(RootBuffer);
457  log_newpage_buffer(RootBuffer, false);
459 
460  /* Unlock and release the buffers. */
461  UnlockReleaseBuffer(MetaBuffer);
462  UnlockReleaseBuffer(RootBuffer);
463 }
void GinInitMetabuffer(Buffer b)
Definition: ginutil.c:363
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1090
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1471
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:654
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
#define P_NEW
Definition: bufmgr.h:91
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3536
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3752
void GinInitBuffer(Buffer b, uint32 f)
Definition: ginutil.c:357
int Buffer
Definition: buf.h:23
#define GIN_LEAF
Definition: ginblock.h:42

◆ ginbulkdelete()

IndexBulkDeleteResult* ginbulkdelete ( IndexVacuumInfo info,
IndexBulkDeleteResult stats,
IndexBulkDeleteCallback  callback,
void *  callback_state 
)

Definition at line 564 of file ginvacuum.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, DataPageDeleteStack::blkno, BufferGetPage, GinVacuumState::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(), 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().

566 {
567  Relation index = info->index;
568  BlockNumber blkno = GIN_ROOT_BLKNO;
569  GinVacuumState gvs;
570  Buffer buffer;
571  BlockNumber rootOfPostingTree[BLCKSZ / (sizeof(IndexTupleData) + sizeof(ItemId))];
572  uint32 nRoot;
573 
575  "Gin vacuum temporary context",
577  gvs.index = index;
578  gvs.callback = callback;
579  gvs.callback_state = callback_state;
580  gvs.strategy = info->strategy;
581  initGinState(&gvs.ginstate, index);
582 
583  /* first time through? */
584  if (stats == NULL)
585  {
586  /* Yes, so initialize stats to zeroes */
588 
589  /*
590  * and cleanup any pending inserts
591  */
593  false, true, stats);
594  }
595 
596  /* we'll re-count the tuples each time */
597  stats->num_index_tuples = 0;
598  gvs.result = stats;
599 
600  buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno,
601  RBM_NORMAL, info->strategy);
602 
603  /* find leaf page */
604  for (;;)
605  {
606  Page page = BufferGetPage(buffer);
607  IndexTuple itup;
608 
609  LockBuffer(buffer, GIN_SHARE);
610 
611  Assert(!GinPageIsData(page));
612 
613  if (GinPageIsLeaf(page))
614  {
615  LockBuffer(buffer, GIN_UNLOCK);
616  LockBuffer(buffer, GIN_EXCLUSIVE);
617 
618  if (blkno == GIN_ROOT_BLKNO && !GinPageIsLeaf(page))
619  {
620  LockBuffer(buffer, GIN_UNLOCK);
621  continue; /* check it one more */
622  }
623  break;
624  }
625 
627 
628  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, FirstOffsetNumber));
629  blkno = GinGetDownlink(itup);
630  Assert(blkno != InvalidBlockNumber);
631 
632  UnlockReleaseBuffer(buffer);
633  buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno,
634  RBM_NORMAL, info->strategy);
635  }
636 
637  /* right now we found leftmost page in entry's BTree */
638 
639  for (;;)
640  {
641  Page page = BufferGetPage(buffer);
642  Page resPage;
643  uint32 i;
644 
645  Assert(!GinPageIsData(page));
646 
647  resPage = ginVacuumEntryPage(&gvs, buffer, rootOfPostingTree, &nRoot);
648 
649  blkno = GinPageGetOpaque(page)->rightlink;
650 
651  if (resPage)
652  {
654  PageRestoreTempPage(resPage, page);
655  MarkBufferDirty(buffer);
656  xlogVacuumPage(gvs.index, buffer);
657  UnlockReleaseBuffer(buffer);
659  }
660  else
661  {
662  UnlockReleaseBuffer(buffer);
663  }
664 
666 
667  for (i = 0; i < nRoot; i++)
668  {
669  ginVacuumPostingTree(&gvs, rootOfPostingTree[i]);
671  }
672 
673  if (blkno == InvalidBlockNumber) /* rightmost page */
674  break;
675 
676  buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno,
677  RBM_NORMAL, info->strategy);
678  LockBuffer(buffer, GIN_EXCLUSIVE);
679  }
680 
682 
683  return gvs.result;
684 }
#define GIN_UNLOCK
Definition: gin_private.h:48
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
#define AllocSetContextCreate
Definition: memutils.h:170
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:411
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1471
Relation index
Definition: ginvacuum.c:30
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:654
BufferAccessStrategy strategy
Definition: ginvacuum.c:35
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
BufferAccessStrategy strategy
Definition: genam.h:52
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
Relation index
Definition: genam.h:46
uint32 BlockNumber
Definition: block.h:31
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
IndexBulkDeleteResult * result
Definition: ginvacuum.c:31
Definition: type.h:89
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3536
static void xlogVacuumPage(Relation index, Buffer buffer)
Definition: ginvacuum.c:90
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
static Page ginVacuumEntryPage(GinVacuumState *gvs, Buffer buffer, BlockNumber *roots, uint32 *nroot)
Definition: ginvacuum.c:455
IndexBulkDeleteCallback callback
Definition: ginvacuum.c:32
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:95
unsigned int uint32
Definition: c.h:429
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define GinPageIsLeaf(page)
Definition: ginblock.h:113
void * callback_state
Definition: ginvacuum.c:33
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3325
#define GIN_SHARE
Definition: gin_private.h:49
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
void * palloc0(Size size)
Definition: mcxt.c:981
struct IndexTupleData IndexTupleData
#define GIN_EXCLUSIVE
Definition: gin_private.h:50
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3752
#define GinGetDownlink(itup)
Definition: ginblock.h:258
void ginInsertCleanup(GinState *ginstate, bool full_clean, bool fill_fsm, bool forceCleanup, IndexBulkDeleteResult *stats)
Definition: ginfast.c:774
#define GinPageIsData(page)
Definition: ginblock.h:116
#define Assert(condition)
Definition: c.h:800
#define InvalidBlockNumber
Definition: block.h:33
int i
static void ginVacuumPostingTree(GinVacuumState *gvs, BlockNumber rootBlkno)
Definition: ginvacuum.c:408
void vacuum_delay_point(void)
Definition: vacuum.c:2031
MemoryContext tmpCxt
Definition: ginvacuum.c:36
double num_index_tuples
Definition: genam.h:77
int Buffer
Definition: buf.h:23
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
#define GIN_ROOT_BLKNO
Definition: ginblock.h:53
Pointer Page
Definition: bufpage.h:78
GinState ginstate
Definition: ginvacuum.c:34

◆ ginCompareAttEntries()

int ginCompareAttEntries ( GinState ginstate,
OffsetNumber  attnuma,
Datum  a,
GinNullCategory  categorya,
OffsetNumber  attnumb,
Datum  b,
GinNullCategory  categoryb 
)

Definition at line 417 of file ginutil.c.

References ginCompareEntries().

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

420 {
421  /* attribute number is the first sort key */
422  if (attnuma != attnumb)
423  return (attnuma < attnumb) ? -1 : 1;
424 
425  return ginCompareEntries(ginstate, attnuma, a, categorya, b, categoryb);
426 }
int ginCompareEntries(GinState *ginstate, OffsetNumber attnum, Datum a, GinNullCategory categorya, Datum b, GinNullCategory categoryb)
Definition: ginutil.c:395

◆ ginCompareEntries()

int ginCompareEntries ( GinState ginstate,
OffsetNumber  attnum,
Datum  a,
GinNullCategory  categorya,
Datum  b,
GinNullCategory  categoryb 
)

Definition at line 395 of file ginutil.c.

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

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

398 {
399  /* if not of same null category, sort by that first */
400  if (categorya != categoryb)
401  return (categorya < categoryb) ? -1 : 1;
402 
403  /* all null items in same category are equal */
404  if (categorya != GIN_CAT_NORM_KEY)
405  return 0;
406 
407  /* both not null, so safe to call the compareFn */
408  return DatumGetInt32(FunctionCall2Coll(&ginstate->compareFn[attnum - 1],
409  ginstate->supportCollation[attnum - 1],
410  a, b));
411 }
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gin_private.h:87
#define DatumGetInt32(X)
Definition: postgres.h:472
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1152
#define GIN_CAT_NORM_KEY
Definition: ginblock.h:209
int16 attnum
Definition: pg_attribute.h:79
FmgrInfo compareFn[INDEX_MAX_KEYS]
Definition: gin_private.h:78

◆ ginCompareItemPointers()

static int ginCompareItemPointers ( ItemPointer  a,
ItemPointer  b 
)
inlinestatic

Definition at line 484 of file gin_private.h.

References GinItemPointerGetBlockNumber, GinItemPointerGetOffsetNumber, and ginTraverseLock().

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

485 {
486  uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
487  uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
488 
489  if (ia == ib)
490  return 0;
491  else if (ia > ib)
492  return 1;
493  else
494  return -1;
495 }
#define GinItemPointerGetOffsetNumber(pointer)
Definition: ginblock.h:147
#define GinItemPointerGetBlockNumber(pointer)
Definition: ginblock.h:144

◆ ginCompressPostingList()

GinPostingList* ginCompressPostingList ( const ItemPointer  ipd,
int  nipd,
int  maxsize,
int *  nwritten 
)

Definition at line 197 of file ginpostinglist.c.

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

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

199 {
200  uint64 prev;
201  int totalpacked = 0;
202  int maxbytes;
203  GinPostingList *result;
204  unsigned char *ptr;
205  unsigned char *endptr;
206 
207  maxsize = SHORTALIGN_DOWN(maxsize);
208 
209  result = palloc(maxsize);
210 
211  maxbytes = maxsize - offsetof(GinPostingList, bytes);
212  Assert(maxbytes > 0);
213 
214  /* Store the first special item */
215  result->first = ipd[0];
216 
217  prev = itemptr_to_uint64(&result->first);
218 
219  ptr = result->bytes;
220  endptr = result->bytes + maxbytes;
221  for (totalpacked = 1; totalpacked < nipd; totalpacked++)
222  {
223  uint64 val = itemptr_to_uint64(&ipd[totalpacked]);
224  uint64 delta = val - prev;
225 
226  Assert(val > prev);
227 
228  if (endptr - ptr >= MaxBytesPerInteger)
229  encode_varbyte(delta, &ptr);
230  else
231  {
232  /*
233  * There are less than 7 bytes left. Have to check if the next
234  * item fits in that space before writing it out.
235  */
236  unsigned char buf[MaxBytesPerInteger];
237  unsigned char *p = buf;
238 
239  encode_varbyte(delta, &p);
240  if (p - buf > (endptr - ptr))
241  break; /* output is full */
242 
243  memcpy(ptr, buf, p - buf);
244  ptr += (p - buf);
245  }
246  prev = val;
247  }
248  result->nbytes = ptr - result->bytes;
249 
250  /*
251  * If we wrote an odd number of bytes, zero out the padding byte at the
252  * end.
253  */
254  if (result->nbytes != SHORTALIGN(result->nbytes))
255  result->bytes[result->nbytes] = 0;
256 
257  if (nwritten)
258  *nwritten = totalpacked;
259 
260  Assert(SizeOfGinPostingList(result) <= maxsize);
261 
262  /*
263  * Check that the encoded segment decodes back to the original items.
264  */
265 #if defined (CHECK_ENCODING_ROUNDTRIP)
266  {
267  int ndecoded;
268  ItemPointer tmp = ginPostingListDecode(result, &ndecoded);
269 
270  Assert(ndecoded == totalpacked);
271  Assert(memcmp(tmp, ipd, ndecoded * sizeof(ItemPointerData)) == 0);
272  pfree(tmp);
273  }
274 #endif
275 
276  return result;
277 }
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded)
def bytes(source, encoding='ascii', errors='strict')
#define SHORTALIGN_DOWN(LEN)
Definition: c.h:761
ItemPointerData first
Definition: ginblock.h:338
void pfree(void *pointer)
Definition: mcxt.c:1057
unsigned char bytes[FLEXIBLE_ARRAY_MEMBER]
Definition: ginblock.h:340
#define MaxBytesPerInteger
static char * buf
Definition: pg_test_fsync.c:68
uint16 nbytes
Definition: ginblock.h:339
#define Assert(condition)
Definition: c.h:800
static uint64 itemptr_to_uint64(const ItemPointer iptr)
static void encode_varbyte(uint64 val, unsigned char **ptr)
void * palloc(Size size)
Definition: mcxt.c:950
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
#define SHORTALIGN(LEN)
Definition: c.h:749
long val
Definition: informix.c:664
#define offsetof(type, field)
Definition: c.h:723

◆ ginDataFillRoot()

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

Definition at line 1343 of file gindatapage.c.

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

Referenced by ginPrepareDataScan().

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

◆ GinDataLeafPageGetItems()

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

Definition at line 135 of file gindatapage.c.

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

Referenced by entryLoadMoreItems(), and startScanEntry().

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

◆ GinDataLeafPageGetItemsToTbm()

int GinDataLeafPageGetItemsToTbm ( Page  page,
TIDBitmap tbm 
)

Definition at line 182 of file gindatapage.c.

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

Referenced by scanPostingTree().

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

◆ GinDataPageAddPostingItem()

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

Definition at line 380 of file gindatapage.c.

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

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

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

◆ ginendscan()

void ginendscan ( IndexScanDesc  scan)

Definition at line 458 of file ginscan.c.

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

Referenced by ginhandler().

459 {
460  GinScanOpaque so = (GinScanOpaque) scan->opaque;
461 
462  ginFreeScanKeys(so);
463 
466 
467  pfree(so);
468 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
void pfree(void *pointer)
Definition: mcxt.c:1057
GinScanOpaqueData * GinScanOpaque
Definition: gin_private.h:383
MemoryContext keyCtx
Definition: gin_private.h:378
MemoryContext tempCtx
Definition: gin_private.h:368
void ginFreeScanKeys(GinScanOpaque so)
Definition: ginscan.c:233

◆ ginEntryFillRoot()

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:416
void pfree(void *pointer)
Definition: mcxt.c:1057
#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(elevel,...)
Definition: elog.h:228
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ ginEntryInsert()

void ginEntryInsert ( GinState ginstate,
OffsetNumber  attnum,
Datum  key,
GinNullCategory  category,
ItemPointerData items,
uint32  nitem,
GinStatsData buildStats 
)

Definition at line 179 of file gininsert.c.

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

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

183 {
184  GinBtreeData btree;
185  GinBtreeEntryInsertData insertdata;
186  GinBtreeStack *stack;
187  IndexTuple itup;
188  Page page;
189 
190  insertdata.isDelete = false;
191 
192  ginPrepareEntryScan(&btree, attnum, key, category, ginstate);
193  btree.isBuild = (buildStats != NULL);
194 
195  stack = ginFindLeafPage(&btree, false, false, NULL);
196  page = BufferGetPage(stack->buffer);
197 
198  if (btree.findItem(&btree, stack))
199  {
200  /* found pre-existing entry */
201  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, stack->off));
202 
203  if (GinIsPostingTree(itup))
204  {
205  /* add entries to existing posting tree */
206  BlockNumber rootPostingTree = GinGetPostingTree(itup);
207 
208  /* release all stack */
209  LockBuffer(stack->buffer, GIN_UNLOCK);
210  freeGinBtreeStack(stack);
211 
212  /* insert into posting tree */
213  ginInsertItemPointers(ginstate->index, rootPostingTree,
214  items, nitem,
215  buildStats);
216  return;
217  }
218 
219  CheckForSerializableConflictIn(ginstate->index, NULL,
220  BufferGetBlockNumber(stack->buffer));
221  /* modify an existing leaf entry */
222  itup = addItemPointersToLeafTuple(ginstate, itup,
223  items, nitem, buildStats, stack->buffer);
224 
225  insertdata.isDelete = true;
226  }
227  else
228  {
229  CheckForSerializableConflictIn(ginstate->index, NULL,
230  BufferGetBlockNumber(stack->buffer));
231  /* no match, so construct a new leaf entry */
232  itup = buildFreshLeafTuple(ginstate, attnum, key, category,
233  items, nitem, buildStats, stack->buffer);
234 
235  /*
236  * nEntries counts leaf tuples, so increment it only when we make a
237  * new one.
238  */
239  if (buildStats)
240  buildStats->nEntries++;
241  }
242 
243  /* Insert the new or modified leaf tuple */
244  insertdata.entry = itup;
245  ginInsertValue(&btree, stack, &insertdata, buildStats);
246  pfree(itup);
247 }
#define GIN_UNLOCK
Definition: gin_private.h:48
void ginInsertItemPointers(Relation index, BlockNumber rootBlkno, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gindatapage.c:1902
Relation index
Definition: gin_private.h:58
OffsetNumber off
Definition: gin_private.h:131
GinBtreeStack * ginFindLeafPage(GinBtree btree, bool searchMode, bool rootConflictCheck, Snapshot snapshot)
Definition: ginbtree.c:80
int64 nEntries
Definition: gin.h:48
uint32 BlockNumber
Definition: block.h:31
static IndexTuple addItemPointersToLeafTuple(GinState *ginstate, IndexTuple old, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats, Buffer buffer)
Definition: gininsert.c:49
bool(* findItem)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:154
void pfree(void *pointer)
Definition: mcxt.c:1057
IndexTupleData * IndexTuple
Definition: itup.h:53
static IndexTuple buildFreshLeafTuple(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats, Buffer buffer)
Definition: gininsert.c:129
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void ginPrepareEntryScan(GinBtree btree, OffsetNumber attnum, Datum key, GinNullCategory category, GinState *ginstate)
Definition: ginentrypage.c:745
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3752
void ginInsertValue(GinBtree btree, GinBtreeStack *stack, void *insertdata, GinStatsData *buildStats)
Definition: ginbtree.c:776
void CheckForSerializableConflictIn(Relation relation, ItemPointer tid, BlockNumber blkno)
Definition: predicate.c:4375
int16 attnum
Definition: pg_attribute.h:79
#define GinGetPostingTree(itup)
Definition: ginblock.h:234
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2663
#define GinIsPostingTree(itup)
Definition: ginblock.h:232
void freeGinBtreeStack(GinBtreeStack *stack)
Definition: ginbtree.c:194
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78

◆ ginExtractEntries()

Datum* ginExtractEntries ( GinState ginstate,
OffsetNumber  attnum,
Datum  value,
bool  isNull,
int32 nentries,
GinNullCategory **  categories 
)

Definition at line 490 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_NORM_KEY, GIN_CAT_NULL_ITEM, GIN_CAT_NULL_KEY, cmpEntriesArg::haveDups, i, keyEntryData::isnull, palloc(), palloc0(), pfree(), PointerGetDatum, qsort_arg(), and GinState::supportCollation.

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

493 {
494  Datum *entries;
495  bool *nullFlags;
496  int32 i;
497 
498  /*
499  * We don't call the extractValueFn on a null item. Instead generate a
500  * placeholder.
501  */
502  if (isNull)
503  {
504  *nentries = 1;
505  entries = (Datum *) palloc(sizeof(Datum));
506  entries[0] = (Datum) 0;
507  *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
508  (*categories)[0] = GIN_CAT_NULL_ITEM;
509  return entries;
510  }
511 
512  /* OK, call the opclass's extractValueFn */
513  nullFlags = NULL; /* in case extractValue doesn't set it */
514  entries = (Datum *)
516  ginstate->supportCollation[attnum - 1],
517  value,
518  PointerGetDatum(nentries),
519  PointerGetDatum(&nullFlags)));
520 
521  /*
522  * Generate a placeholder if the item contained no keys.
523  */
524  if (entries == NULL || *nentries <= 0)
525  {
526  *nentries = 1;
527  entries = (Datum *) palloc(sizeof(Datum));
528  entries[0] = (Datum) 0;
529  *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
530  (*categories)[0] = GIN_CAT_EMPTY_ITEM;
531  return entries;
532  }
533 
534  /*
535  * If the extractValueFn didn't create a nullFlags array, create one,
536  * assuming that everything's non-null.
537  */
538  if (nullFlags == NULL)
539  nullFlags = (bool *) palloc0(*nentries * sizeof(bool));
540 
541  /*
542  * If there's more than one key, sort and unique-ify.
543  *
544  * XXX Using qsort here is notationally painful, and the overhead is
545  * pretty bad too. For small numbers of keys it'd likely be better to use
546  * a simple insertion sort.
547  */
548  if (*nentries > 1)
549  {
550  keyEntryData *keydata;
552 
553  keydata = (keyEntryData *) palloc(*nentries * sizeof(keyEntryData));
554  for (i = 0; i < *nentries; i++)
555  {
556  keydata[i].datum = entries[i];
557  keydata[i].isnull = nullFlags[i];
558  }
559 
560  arg.cmpDatumFunc = &ginstate->compareFn[attnum - 1];
561  arg.collation = ginstate->supportCollation[attnum - 1];
562  arg.haveDups = false;
563  qsort_arg(keydata, *nentries, sizeof(keyEntryData),
564  cmpEntries, (void *) &arg);
565 
566  if (arg.haveDups)
567  {
568  /* there are duplicates, must get rid of 'em */
569  int32 j;
570 
571  entries[0] = keydata[0].datum;
572  nullFlags[0] = keydata[0].isnull;
573  j = 1;
574  for (i = 1; i < *nentries; i++)
575  {
576  if (cmpEntries(&keydata[i - 1], &keydata[i], &arg) != 0)
577  {
578  entries[j] = keydata[i].datum;
579  nullFlags[j] = keydata[i].isnull;
580  j++;
581  }
582  }
583  *nentries = j;
584  }
585  else
586  {
587  /* easy, no duplicates */
588  for (i = 0; i < *nentries; i++)
589  {
590  entries[i] = keydata[i].datum;
591  nullFlags[i] = keydata[i].isnull;
592  }
593  }
594 
595  pfree(keydata);
596  }
597 
598  /*
599  * Create GinNullCategory representation from nullFlags.
600  */
601  *categories = (GinNullCategory *) palloc0(*nentries * sizeof(GinNullCategory));
602  for (i = 0; i < *nentries; i++)
603  (*categories)[i] = (nullFlags[i] ? GIN_CAT_NULL_KEY : GIN_CAT_NORM_KEY);
604 
605  return entries;
606 }
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gin_private.h:87
FmgrInfo * cmpDatumFunc
Definition: ginutil.c:444
bool isnull
Definition: ginutil.c:439
#define PointerGetDatum(X)
Definition: postgres.h:556
FmgrInfo extractValueFn[INDEX_MAX_KEYS]
Definition: gin_private.h:79
Datum datum
Definition: ginutil.c:438
signed int int32
Definition: c.h:417
void pfree(void *pointer)
Definition: mcxt.c:1057
signed char GinNullCategory
Definition: ginblock.h:207
bool haveDups
Definition: ginutil.c:446
#define GIN_CAT_NORM_KEY
Definition: ginblock.h:209
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
Definition: qsort_arg.c:113
#define GIN_CAT_NULL_KEY
Definition: ginblock.h:210
void * palloc0(Size size)
Definition: mcxt.c:981
uintptr_t Datum
Definition: postgres.h:367
#define GIN_CAT_NULL_ITEM
Definition: ginblock.h:212
int16 attnum
Definition: pg_attribute.h:79
static struct @143 value
Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3)
Definition: fmgr.c:1174
Oid collation
Definition: ginutil.c:445
#define DatumGetPointer(X)
Definition: postgres.h:549
void * palloc(Size size)
Definition: mcxt.c:950
int i
void * arg
#define GIN_CAT_EMPTY_ITEM
Definition: ginblock.h:211
static int cmpEntries(const void *a, const void *b, void *arg)
Definition: ginutil.c:450
FmgrInfo compareFn[INDEX_MAX_KEYS]
Definition: gin_private.h:78

◆ ginFindLeafPage()

GinBtreeStack* ginFindLeafPage ( GinBtree  btree,
bool  searchMode,
bool  rootConflictCheck,
Snapshot  snapshot 
)

Definition at line 80 of file ginbtree.c.

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

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

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

◆ GinFormTuple()

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, sort-test::key, Max, MAXALIGN, 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:230
Relation index
Definition: gin_private.h:58
#define INDEX_SIZE_MASK
Definition: itup.h:65
int errcode(int sqlerrcode)
Definition: elog.c:691
#define IndexTupleHasNulls(itup)
Definition: itup.h:72
#define GinGetPosting(itup)
Definition: ginblock.h:239
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:47
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
signed char GinNullCategory
Definition: ginblock.h:207
#define GinSetPostingOffset(itup, n)
Definition: ginblock.h:238
#define GIN_CAT_NORM_KEY
Definition: ginblock.h:209
#define RelationGetRelationName(relation)
Definition: rel.h:491
unsigned int uint32
Definition: c.h:429
uintptr_t Datum
Definition: postgres.h:367
TupleDesc tupdesc[INDEX_MAX_KEYS]
Definition: gin_private.h:73
int16 attnum
Definition: pg_attribute.h:79
#define ereport(elevel,...)
Definition: elog.h:155
#define Max(x, y)
Definition: c.h:976
#define Assert(condition)
Definition: c.h:800
#define GinSetNullCategory(itup, ginstate, c)
Definition: ginblock.h:223
size_t Size
Definition: c.h:528
#define MAXALIGN(LEN)
Definition: c.h:753
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define GinMaxItemSize
Definition: ginblock.h:249
unsigned short t_info
Definition: itup.h:49
#define SHORTALIGN(LEN)
Definition: c.h:749
bool oneCol
Definition: gin_private.h:59
#define GinCategoryOffset(itup, ginstate)
Definition: ginblock.h:218
#define UInt16GetDatum(X)
Definition: postgres.h:465
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ ginFreeScanKeys()

void ginFreeScanKeys ( GinScanOpaque  so)

Definition at line 233 of file ginscan.c.

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

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

234 {
235  uint32 i;
236 
237  if (so->keys == NULL)
238  return;
239 
240  for (i = 0; i < so->totalentries; i++)
241  {
242  GinScanEntry entry = so->entries[i];
243 
244  if (entry->buffer != InvalidBuffer)
245  ReleaseBuffer(entry->buffer);
246  if (entry->list)
247  pfree(entry->list);
248  if (entry->matchIterator)
250  if (entry->matchBitmap)
251  tbm_free(entry->matchBitmap);
252  }
253 
255 
256  so->keys = NULL;
257  so->nkeys = 0;
258  so->entries = NULL;
259  so->totalentries = 0;
260 }
void tbm_end_iterate(TBMIterator *iterator)
Definition: tidbitmap.c:1145
TBMIterator * matchIterator
Definition: gin_private.h:352
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3513
ItemPointerData * list
Definition: gin_private.h:356
void pfree(void *pointer)
Definition: mcxt.c:1057
void tbm_free(TIDBitmap *tbm)
Definition: tidbitmap.c:321
MemoryContext keyCtx
Definition: gin_private.h:378
unsigned int uint32
Definition: c.h:429
TIDBitmap * matchBitmap
Definition: gin_private.h:351
GinScanKey keys
Definition: gin_private.h:371
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
GinScanEntry * entries
Definition: gin_private.h:374
int i

◆ ginGetBAEntry()

ItemPointerData* ginGetBAEntry ( BuildAccumulator accum,
OffsetNumber attnum,
Datum key,
GinNullCategory category,
uint32 n 
)

Definition at line 268 of file ginbulk.c.

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

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

271 {
272  GinEntryAccumulator *entry;
274 
275  entry = (GinEntryAccumulator *) rbt_iterate(&accum->tree_walk);
276 
277  if (entry == NULL)
278  return NULL; /* no more entries */
279 
280  *attnum = entry->attnum;
281  *key = entry->key;
282  *category = entry->category;
283  list = entry->list;
284  *n = entry->count;
285 
286  Assert(list != NULL && entry->count > 0);
287 
288  if (entry->shouldSort && entry->count > 1)
289  qsort(list, entry->count, sizeof(ItemPointerData),
291 
292  return list;
293 }
RBTNode * rbt_iterate(RBTreeIterator *iter)
Definition: rbtree.c:764
OffsetNumber attnum
Definition: gin_private.h:421
GinNullCategory category
Definition: gin_private.h:420
int16 attnum
Definition: pg_attribute.h:79
#define Assert(condition)
Definition: c.h:800
static int qsortCompareItemPointers(const void *a, const void *b)
Definition: ginbulk.c:246
RBTreeIterator tree_walk
Definition: gin_private.h:435
#define qsort(a, b, c, d)
Definition: port.h:497
ItemPointerData * list
Definition: gin_private.h:423

◆ gingetbitmap()

int64 gingetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

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

1918 {
1919  GinScanOpaque so = (GinScanOpaque) scan->opaque;
1920  int64 ntids;
1921  ItemPointerData iptr;
1922  bool recheck;
1923 
1924  /*
1925  * Set up the scan keys, and check for unsatisfiable query.
1926  */
1927  ginFreeScanKeys(so); /* there should be no keys yet, but just to be
1928  * sure */
1929  ginNewScanKey(scan);
1930 
1931  if (GinIsVoidRes(scan))
1932  return 0;
1933 
1934  ntids = 0;
1935 
1936  /*
1937  * First, scan the pending list and collect any matching entries into the
1938  * bitmap. After we scan a pending item, some other backend could post it
1939  * into the main index, and so we might visit it a second time during the
1940  * main scan. This is okay because we'll just re-set the same bit in the
1941  * bitmap. (The possibility of duplicate visits is a major reason why GIN
1942  * can't support the amgettuple API, however.) Note that it would not do
1943  * to scan the main index before the pending list, since concurrent
1944  * cleanup could then make us miss entries entirely.
1945  */
1946  scanPendingInsert(scan, tbm, &ntids);
1947 
1948  /*
1949  * Now scan the main index.
1950  */
1951  startScan(scan);
1952 
1953  ItemPointerSetMin(&iptr);
1954 
1955  for (;;)
1956  {
1958 
1959  if (!scanGetItem(scan, iptr, &iptr, &recheck))
1960  break;
1961 
1962  if (ItemPointerIsLossyPage(&iptr))
1964  else
1965  tbm_add_tuples(tbm, &iptr, 1, recheck);
1966  ntids++;
1967  }
1968 
1969  return ntids;
1970 }
static void scanPendingInsert(IndexScanDesc scan, TIDBitmap *tbm, int64 *ntids)
Definition: ginget.c:1822
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:376
#define ItemPointerIsLossyPage(p)
Definition: ginblock.h:176
static void startScan(IndexScanDesc scan)
Definition: ginget.c:600
GinScanOpaqueData * GinScanOpaque
Definition: gin_private.h:383
#define ItemPointerSetMin(p)
Definition: ginblock.h:167
static bool scanGetItem(IndexScanDesc scan, ItemPointerData advancePast, ItemPointerData *item, bool *recheck)
Definition: ginget.c:1284
void ginNewScanKey(IndexScanDesc scan)
Definition: ginscan.c:263
#define GinIsVoidRes(s)
Definition: ginget.c:1914
void tbm_add_page(TIDBitmap *tbm, BlockNumber pageno)
Definition: tidbitmap.c:442
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
void ginFreeScanKeys(GinScanOpaque so)
Definition: ginscan.c:233

◆ ginHeapTupleFastCollect()

void ginHeapTupleFastCollect ( GinState ginstate,
GinTupleCollector collector,
OffsetNumber  attnum,
Datum  value,
bool  isNull,
ItemPointer  ht_ctid 
)

Definition at line 474 of file ginfast.c.

References KeyArray::categories, elog, ERROR, ginExtractEntries(), GinFormTuple(), i, IndexTupleSize, GinTupleCollector::lentuples, Max, MaxAllocSize, GinTupleCollector::ntuples, palloc(), pg_nextpower2_32(), repalloc(), GinTupleCollector::sumsize, IndexTupleData::t_tid, and GinTupleCollector::tuples.

Referenced by gininsert().

478 {
479  Datum *entries;
480  GinNullCategory *categories;
481  int32 i,
482  nentries;
483 
484  /*
485  * Extract the key values that need to be inserted in the index
486  */
487  entries = ginExtractEntries(ginstate, attnum, value, isNull,
488  &nentries, &categories);
489 
490  /*
491  * Protect against integer overflow in allocation calculations
492  */
493  if (nentries < 0 ||
494  collector->ntuples + nentries > MaxAllocSize / sizeof(IndexTuple))
495  elog(ERROR, "too many entries for GIN index");
496 
497  /*
498  * Allocate/reallocate memory for storing collected tuples
499  */
500  if (collector->tuples == NULL)
501  {
502  /*
503  * Determine the number of elements to allocate in the tuples array
504  * initially. Make it a power of 2 to avoid wasting memory when
505  * resizing (since palloc likes powers of 2).
506  */
507  collector->lentuples = pg_nextpower2_32(Max(16, nentries));
508  collector->tuples = (IndexTuple *) palloc(sizeof(IndexTuple) * collector->lentuples);
509  }
510  else if (collector->lentuples < collector->ntuples + nentries)
511  {
512  /*
513  * Advance lentuples to the next suitable power of 2. This won't
514  * overflow, though we could get to a value that exceeds
515  * MaxAllocSize/sizeof(IndexTuple), causing an error in repalloc.
516  */
517  collector->lentuples = pg_nextpower2_32(collector->ntuples + nentries);
518  collector->tuples = (IndexTuple *) repalloc(collector->tuples,
519  sizeof(IndexTuple) * collector->lentuples);
520  }
521 
522  /*
523  * Build an index tuple for each key value, and add to array. In pending
524  * tuples we just stick the heap TID into t_tid.
525  */
526  for (i = 0; i < nentries; i++)
527  {
528  IndexTuple itup;
529 
530  itup = GinFormTuple(ginstate, attnum, entries[i], categories[i],
531  NULL, 0, 0, true);
532  itup->t_tid = *ht_ctid;
533  collector->tuples[collector->ntuples++] = itup;
534  collector->sumsize += IndexTupleSize(itup);
535  }
536 }
ItemPointerData t_tid
Definition: itup.h:37
signed int int32
Definition: c.h:417
#define ERROR
Definition: elog.h:43
signed char GinNullCategory
Definition: ginblock.h:207
static uint32 pg_nextpower2_32(uint32 num)
Definition: pg_bitutils.h:146
#define MaxAllocSize
Definition: memutils.h:40
IndexTuple * tuples
Definition: gin_private.h:452
uintptr_t Datum
Definition: postgres.h:367
int16 attnum
Definition: pg_attribute.h:79
static struct @143 value
#define Max(x, y)
Definition: c.h:976
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:1070
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:228
int i
Datum * ginExtractEntries(GinState *ginstate, OffsetNumber attnum, Datum value, bool isNull, int32 *nentries, GinNullCategory **categories)
Definition: ginutil.c:490
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ ginHeapTupleFastInsert()

void ginHeapTupleFastInsert ( GinState ginstate,
GinTupleCollector collector 
)

Definition at line 219 of file ginfast.c.

References Assert, BufferGetPage, CheckForSerializableConflictIn(), 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, 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().

220 {
221  Relation index = ginstate->index;
222  Buffer metabuffer;
223  Page metapage;
224  GinMetaPageData *metadata = NULL;
225  Buffer buffer = InvalidBuffer;
226  Page page = NULL;
227  ginxlogUpdateMeta data;
228  bool separateList = false;
229  bool needCleanup = false;
230  int cleanupSize;
231  bool needWal;
232 
233  if (collector->ntuples == 0)
234  return;
235 
236  needWal = RelationNeedsWAL(index);
237 
238  data.node = index->rd_node;
239  data.ntuples = 0;
241 
242  metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
243  metapage = BufferGetPage(metabuffer);
244 
245  /*
246  * An insertion to the pending list could logically belong anywhere in the
247  * tree, so it conflicts with all serializable scans. All scans acquire a
248  * predicate lock on the metabuffer to represent that.
249  */
251 
252  if (collector->sumsize + collector->ntuples * sizeof(ItemIdData) > GinListPageSize)
253  {
254  /*
255  * Total size is greater than one page => make sublist
256  */
257  separateList = true;
258  }
259  else
260  {
261  LockBuffer(metabuffer, GIN_EXCLUSIVE);
262  metadata = GinPageGetMeta(metapage);
263 
264  if (metadata->head == InvalidBlockNumber ||
265  collector->sumsize + collector->ntuples * sizeof(ItemIdData) > metadata->tailFreeSize)
266  {
267  /*
268  * Pending list is empty or total size is greater than freespace
269  * on tail page => make sublist
270  *
271  * We unlock metabuffer to keep high concurrency
272  */
273  separateList = true;
274  LockBuffer(metabuffer, GIN_UNLOCK);
275  }
276  }
277 
278  if (separateList)
279  {
280  /*
281  * We should make sublist separately and append it to the tail
282  */
283  GinMetaPageData sublist;
284 
285  memset(&sublist, 0, sizeof(GinMetaPageData));
286  makeSublist(index, collector->tuples, collector->ntuples, &sublist);
287 
288  if (needWal)
289  XLogBeginInsert();
290 
291  /*
292  * metapage was unlocked, see above
293  */
294  LockBuffer(metabuffer, GIN_EXCLUSIVE);
295  metadata = GinPageGetMeta(metapage);
296 
297  if (metadata->head == InvalidBlockNumber)
298  {
299  /*
300  * Main list is empty, so just insert sublist as main list
301  */
303 
304  metadata->head = sublist.head;
305  metadata->tail = sublist.tail;
306  metadata->tailFreeSize = sublist.tailFreeSize;
307 
308  metadata->nPendingPages = sublist.nPendingPages;
309  metadata->nPendingHeapTuples = sublist.nPendingHeapTuples;
310  }
311  else
312  {
313  /*
314  * Merge lists
315  */
316  data.prevTail = metadata->tail;
317  data.newRightlink = sublist.head;
318 
319  buffer = ReadBuffer(index, metadata->tail);
320  LockBuffer(buffer, GIN_EXCLUSIVE);
321  page = BufferGetPage(buffer);
322 
323  Assert(GinPageGetOpaque(page)->rightlink == InvalidBlockNumber);
324 
326 
327  GinPageGetOpaque(page)->rightlink = sublist.head;
328 
329  MarkBufferDirty(buffer);
330 
331  metadata->tail = sublist.tail;
332  metadata->tailFreeSize = sublist.tailFreeSize;
333 
334  metadata->nPendingPages += sublist.nPendingPages;
335  metadata->nPendingHeapTuples += sublist.nPendingHeapTuples;
336 
337  if (needWal)
339  }
340  }
341  else
342  {
343  /*
344  * Insert into tail page. Metapage is already locked
345  */
346  OffsetNumber l,
347  off;
348  int i,
349  tupsize;
350  char *ptr;
351  char *collectordata;
352 
353  buffer = ReadBuffer(index, metadata->tail);
354  LockBuffer(buffer, GIN_EXCLUSIVE);
355  page = BufferGetPage(buffer);
356 
357  off = (PageIsEmpty(page)) ? FirstOffsetNumber :
359 
360  collectordata = ptr = (char *) palloc(collector->sumsize);
361 
362  data.ntuples = collector->ntuples;
363 
364  if (needWal)
365  XLogBeginInsert();
366 
368 
369  /*
370  * Increase counter of heap tuples
371  */
372  Assert(GinPageGetOpaque(page)->maxoff <= metadata->nPendingHeapTuples);
373  GinPageGetOpaque(page)->maxoff++;
374  metadata->nPendingHeapTuples++;
375 
376  for (i = 0; i < collector->ntuples; i++)
377  {
378  tupsize = IndexTupleSize(collector->tuples[i]);
379  l = PageAddItem(page, (Item) collector->tuples[i], tupsize, off, false, false);
380 
381  if (l == InvalidOffsetNumber)
382  elog(ERROR, "failed to add item to index page in \"%s\"",
383  RelationGetRelationName(index));
384 
385  memcpy(ptr, collector->tuples[i], tupsize);
386  ptr += tupsize;
387 
388  off++;
389  }
390 
391  Assert((ptr - collectordata) <= collector->sumsize);
392  if (needWal)
393  {
395  XLogRegisterBufData(1, collectordata, collector->sumsize);
396  }
397 
398  metadata->tailFreeSize = PageGetExactFreeSpace(page);
399 
400  MarkBufferDirty(buffer);
401  }
402 
403  /*
404  * Set pd_lower just past the end of the metadata. This is essential,
405  * because without doing so, metadata will be lost if xlog.c compresses
406  * the page. (We must do this here because pre-v11 versions of PG did not
407  * set the metapage's pd_lower correctly, so a pg_upgraded index might
408  * contain the wrong value.)
409  */
410  ((PageHeader) metapage)->pd_lower =
411  ((char *) metadata + sizeof(GinMetaPageData)) - (char *) metapage;
412 
413  /*
414  * Write metabuffer, make xlog entry
415  */
416  MarkBufferDirty(metabuffer);
417 
418  if (needWal)
419  {
420  XLogRecPtr recptr;
421 
422  memcpy(&data.metadata, metadata, sizeof(GinMetaPageData));
423 
425  XLogRegisterData((char *) &data, sizeof(ginxlogUpdateMeta));
426 
427  recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_UPDATE_META_PAGE);
428  PageSetLSN(metapage, recptr);
429 
430  if (buffer != InvalidBuffer)
431  {
432  PageSetLSN(page, recptr);
433  }
434  }
435 
436  if (buffer != InvalidBuffer)
437  UnlockReleaseBuffer(buffer);
438 
439  /*
440  * Force pending list cleanup when it becomes too long. And,
441  * ginInsertCleanup could take significant amount of time, so we prefer to
442  * call it when it can do all the work in a single collection cycle. In
443  * non-vacuum mode, it shouldn't require maintenance_work_mem, so fire it
444  * while pending list is still small enough to fit into
445  * gin_pending_list_limit.
446  *
447  * ginInsertCleanup() should not be called inside our CRIT_SECTION.
448  */
449  cleanupSize = GinGetPendingListCleanupSize(index);
450  if (metadata->nPendingPages * GIN_PAGE_FREESIZE > cleanupSize * 1024L)
451  needCleanup = true;
452 
453  UnlockReleaseBuffer(metabuffer);
454 
456 
457  /*
458  * Since it could contend with concurrent cleanup process we cleanup
459  * pending list not forcibly.
460  */
461  if (needCleanup)
462  ginInsertCleanup(ginstate, false, true, false, NULL);
463 }
BlockNumber prevTail
Definition: ginxlog.h:172
#define GIN_UNLOCK
Definition: gin_private.h:48
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:368
#define PageIsEmpty(page)
Definition: bufpage.h:222
RelFileNode node
Definition: ginxlog.h:170
Relation index
Definition: gin_private.h:58
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1471
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:220
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
#define GinListPageSize
Definition: ginblock.h:328
Pointer Item
Definition: item.h:17
#define InvalidBuffer
Definition: buf.h:25
#define REGBUF_WILL_INIT
Definition: xloginsert.h:33
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
#define GIN_METAPAGE_BLKNO
Definition: ginblock.h:52
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
static void makeSublist(Relation index, IndexTuple *tuples, int32 ntuples, GinMetaPageData *res)
Definition: ginfast.c:145
uint16 OffsetNumber
Definition: off.h:24
Definition: type.h:89
int64 nPendingHeapTuples
Definition: ginblock.h:75
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3536
#define ERROR
Definition: elog.h:43
#define XLOG_GIN_UPDATE_META_PAGE
Definition: ginxlog.h:162
BlockNumber head
Definition: ginblock.h:62
BlockNumber tail
Definition: ginblock.h:63
#define FirstOffsetNumber
Definition: off.h:27
#define REGBUF_STANDARD
Definition: xloginsert.h:35
#define GinGetPendingListCleanupSize(relation)
Definition: gin_private.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:491
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
BlockNumber newRightlink
Definition: ginxlog.h:173
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:330
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:422
IndexTuple * tuples
Definition: gin_private.h:452
#define GIN_EXCLUSIVE
Definition: gin_private.h:50
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3752
void CheckForSerializableConflictIn(Relation relation, ItemPointer tid, BlockNumber blkno)
Definition: predicate.c:4375
#define InvalidOffsetNumber
Definition: off.h:26
void ginInsertCleanup(GinState *ginstate, bool full_clean, bool fill_fsm, bool forceCleanup, IndexBulkDeleteResult *stats)
Definition: ginfast.c:774
GinMetaPageData metadata
Definition: ginxlog.h:171
RelFileNode rd_node
Definition: rel.h:55
PageHeaderData * PageHeader
Definition: bufpage.h:166
uint32 tailFreeSize
Definition: ginblock.h:68
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:800
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:607
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define InvalidBlockNumber
Definition: block.h:33
#define RelationNeedsWAL(relation)
Definition: rel.h:563
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:841
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:228
int i
#define GIN_PAGE_FREESIZE
Definition: ginfast.c:41
#define GinPageGetMeta(p)
Definition: ginblock.h:105
void XLogBeginInsert(void)
Definition: xloginsert.c:123
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78
#define IndexTupleSize(itup)
Definition: itup.h:71
BlockNumber nPendingPages
Definition: ginblock.h:74

◆ ginInitBA()

void ginInitBA ( BuildAccumulator accum)

Definition at line 109 of file ginbulk.c.

References BuildAccumulator::allocatedMemory, cmpEntryAccumulator(), BuildAccumulator::eas_used, BuildAccumulator::entryallocator, ginAllocEntryAccumulator(), ginCombineData(), rbt_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 = rbt_create(sizeof(GinEntryAccumulator),
119  NULL, /* no freefunc needed */
120  (void *) accum);
121 }
GinEntryAccumulator * entryallocator
Definition: gin_private.h:432
RBTree * rbt_create(Size node_size, rbt_comparator comparator, rbt_combiner combiner, rbt_allocfunc allocfunc, rbt_freefunc freefunc, void *arg)
Definition: rbtree.c:102
static void ginCombineData(RBTNode *existing, const RBTNode *newdata, void *arg)
Definition: ginbulk.c:30
static RBTNode * ginAllocEntryAccumulator(void *arg)
Definition: ginbulk.c:85
static int cmpEntryAccumulator(const RBTNode *a, const RBTNode *b, void *arg)
Definition: ginbulk.c:72

◆ GinInitBuffer()

void GinInitBuffer ( Buffer  b,
uint32  f 
)

Definition at line 357 of file ginutil.c.

References BufferGetPage, BufferGetPageSize, and GinInitPage().

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

358 {
360 }
void GinInitPage(Page page, uint32 f, Size pageSize)
Definition: ginutil.c:344
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:156

◆ ginInitConsistentFunction()

void ginInitConsistentFunction ( GinState ginstate,
GinScanKey  key 
)

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

224 {
226  {
229  }
230  else
231  {
232  key->consistentFmgrInfo = &ginstate->consistentFn[key->attnum - 1];
233  key->triConsistentFmgrInfo = &ginstate->triConsistentFn[key->attnum - 1];
234  key->collation = ginstate->supportCollation[key->attnum - 1];
235 
236  if (OidIsValid(ginstate->consistentFn[key->attnum - 1].fn_oid))
238  else
240 
241  if (OidIsValid(ginstate->triConsistentFn[key->attnum - 1].fn_oid))
243  else
245  }
246 }
FmgrInfo * triConsistentFmgrInfo
Definition: gin_private.h:293
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gin_private.h:87
static GinTernaryValue directTriConsistentFn(GinScanKey key)
Definition: ginlogic.c:95
static GinTernaryValue shimTriConsistentFn(GinScanKey key)
Definition: ginlogic.c:152
#define GIN_SEARCH_MODE_EVERYTHING
Definition: gin.h:37
FmgrInfo consistentFn[INDEX_MAX_KEYS]
Definition: gin_private.h:81
FmgrInfo * consistentFmgrInfo
Definition: gin_private.h:292
#define OidIsValid(objectId)
Definition: c.h:706
OffsetNumber attnum
Definition: gin_private.h:304
static GinTernaryValue trueTriConsistentFn(GinScanKey key)
Definition: ginlogic.c:62
bool(* boolConsistentFn)(GinScanKey key)
Definition: gin_private.h:290
Oid fn_oid
Definition: fmgr.h:59
GinTernaryValue(* triConsistentFn)(GinScanKey key)
Definition: gin_private.h:291
static bool directBoolConsistentFn(GinScanKey key)
Definition: ginlogic.c:71
static bool trueConsistentFn(GinScanKey key)
Definition: ginlogic.c:56
static bool shimBoolConsistentFn(GinScanKey key)
Definition: ginlogic.c:114
FmgrInfo triConsistentFn[INDEX_MAX_KEYS]
Definition: gin_private.h:82

◆ GinInitMetabuffer()

void GinInitMetabuffer ( Buffer  b)

Definition at line 363 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(), ginRedoDeleteListPages(), and ginRedoUpdateMetapage().

364 {
365  GinMetaPageData *metadata;
366  Page page = BufferGetPage(b);
367 
369 
370  metadata = GinPageGetMeta(page);
371 
372  metadata->head = metadata->tail = InvalidBlockNumber;
373  metadata->tailFreeSize = 0;
374  metadata->nPendingPages = 0;
375  metadata->nPendingHeapTuples = 0;
376  metadata->nTotalPages = 0;
377  metadata->nEntryPages = 0;
378  metadata->nDataPages = 0;
379  metadata->nEntries = 0;
380  metadata->ginVersion = GIN_CURRENT_VERSION;
381 
382  /*
383  * Set pd_lower just past the end of the metadata. This is essential,
384  * because without doing so, metadata will be lost if xlog.c compresses
385  * the page.
386  */
387  ((PageHeader) page)->pd_lower =
388  ((char *) metadata + sizeof(GinMetaPageData)) - (char *) page;
389 }
BlockNumber nEntryPages
Definition: ginblock.h:81
void GinInitPage(Page page, uint32 f, Size pageSize)
Definition: ginutil.c:344
BlockNumber nTotalPages
Definition: ginblock.h:80
int64 nEntries
Definition: ginblock.h:83
int64 nPendingHeapTuples
Definition: ginblock.h:75
#define GIN_META
Definition: ginblock.h:44
BlockNumber head
Definition: ginblock.h:62
BlockNumber tail
Definition: ginblock.h:63
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:156
PageHeaderData * PageHeader
Definition: bufpage.h:166
uint32 tailFreeSize
Definition: ginblock.h:68
#define GIN_CURRENT_VERSION
Definition: ginblock.h:103
#define InvalidBlockNumber
Definition: block.h:33
int32 ginVersion
Definition: ginblock.h:100
BlockNumber nDataPages
Definition: ginblock.h:82
#define GinPageGetMeta(p)
Definition: ginblock.h:105
Pointer Page
Definition: bufpage.h:78
BlockNumber nPendingPages
Definition: ginblock.h:74

◆ GinInitPage()

void GinInitPage ( Page  page,
uint32  f,
Size  pageSize 
)

Definition at line 344 of file ginutil.c.

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

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

345 {
346  GinPageOpaque opaque;
347 
348  PageInit(page, pageSize, sizeof(GinPageOpaqueData));
349 
350  opaque = GinPageGetOpaque(page);
351  memset(opaque, 0, sizeof(GinPageOpaqueData));
352  opaque->flags = f;
353  opaque->rightlink = InvalidBlockNumber;
354 }
BlockNumber rightlink
Definition: ginblock.h:32
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
#define InvalidBlockNumber
Definition: block.h:33
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42

◆ gininsert()

bool gininsert ( Relation  index,
Datum values,
bool isnull,
ItemPointer  ht_ctid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
struct IndexInfo indexInfo 
)

Definition at line 488 of file gininsert.c.

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

Referenced by ginhandler().

492 {
493  GinState *ginstate = (GinState *) indexInfo->ii_AmCache;
494  MemoryContext oldCtx;
495  MemoryContext insertCtx;
496  int i;
497 
498  /* Initialize GinState cache if first call in this statement */
499  if (ginstate == NULL)
500  {
501  oldCtx = MemoryContextSwitchTo(indexInfo->ii_Context);
502  ginstate = (GinState *) palloc(sizeof(GinState));
503  initGinState(ginstate, index);
504  indexInfo->ii_AmCache = (void *) ginstate;
505  MemoryContextSwitchTo(oldCtx);
506  }
507 
509  "Gin insert temporary context",
511 
512  oldCtx = MemoryContextSwitchTo(insertCtx);
513 
514  if (GinGetUseFastUpdate(index))
515  {
516  GinTupleCollector collector;
517 
518  memset(&collector, 0, sizeof(GinTupleCollector));
519 
520  for (i = 0; i < ginstate->origTupdesc->natts; i++)
521  ginHeapTupleFastCollect(ginstate, &collector,
522  (OffsetNumber) (i + 1),
523  values[i], isnull[i],
524  ht_ctid);
525 
526  ginHeapTupleFastInsert(ginstate, &collector);
527  }
528  else
529  {
530  for (i = 0; i < ginstate->origTupdesc->natts; i++)
531  ginHeapTupleInsert(ginstate, (OffsetNumber) (i + 1),
532  values[i], isnull[i],
533  ht_ctid);
534  }
535 
536  MemoryContextSwitchTo(oldCtx);
537  MemoryContextDelete(insertCtx);
538 
539  return false;
540 }
MemoryContext ii_Context
Definition: execnodes.h:178
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
#define AllocSetContextCreate
Definition: memutils.h:170
void ginHeapTupleFastInsert(GinState *ginstate, GinTupleCollector *collector)
Definition: ginfast.c:219
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:470
uint16 OffsetNumber
Definition: off.h:24
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:95
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void * ii_AmCache
Definition: execnodes.h:177
#define GinGetUseFastUpdate(relation)
Definition: gin_private.h:33
static Datum values[MAXATTR]
Definition: bootstrap.c:165
void * palloc(Size size)
Definition: mcxt.c:950
int i
void ginHeapTupleFastCollect(GinState *ginstate, GinTupleCollector *collector, OffsetNumber attnum, Datum value, bool isNull, ItemPointer ht_ctid)
Definition: ginfast.c:474
TupleDesc origTupdesc
Definition: gin_private.h:72

◆ ginInsertBAEntries()

void ginInsertBAEntries ( BuildAccumulator accum,
ItemPointer  heapptr,
OffsetNumber  attnum,
Datum entries,
GinNullCategory categories,
int32  nentries 
)

Definition at line 210 of file ginbulk.c.

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

Referenced by ginHeapTupleBulkInsert(), and processPendingPage().

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

◆ ginInsertCleanup()

void ginInsertCleanup ( GinState ginstate,
bool  full_clean,
bool  fill_fsm,
bool  forceCleanup,
IndexBulkDeleteResult stats 
)

Definition at line 774 of file ginfast.c.

References BuildAccumulator::allocatedMemory, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, attnum, 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::key, sort-test::list, LockBuffer(), LockPage(), maintenance_work_mem, KeyArray::maxvalues, MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), 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().

777 {
778  Relation index = ginstate->index;
779  Buffer metabuffer,
780  buffer;
781  Page metapage,
782  page;
783  GinMetaPageData *metadata;
785  oldCtx;
786  BuildAccumulator accum;
787  KeyArray datums;
788  BlockNumber blkno,
789  blknoFinish;
790  bool cleanupFinish = false;
791  bool fsm_vac = false;
792  Size workMemory;
793 
794  /*
795  * We would like to prevent concurrent cleanup process. For that we will
796  * lock metapage in exclusive mode using LockPage() call. Nobody other
797  * will use that lock for metapage, so we keep possibility of concurrent
798  * insertion into pending list
799  */
800 
801  if (forceCleanup)
802  {
803  /*
804  * We are called from [auto]vacuum/analyze or gin_clean_pending_list()
805  * and we would like to wait concurrent cleanup to finish.
806  */
808  workMemory =
811  }
812  else
813  {
814  /*
815  * We are called from regular insert and if we see concurrent cleanup
816  * just exit in hope that concurrent process will clean up pending
817  * list.
818  */
820  return;
821  workMemory = work_mem;
822  }
823 
824  metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
825  LockBuffer(metabuffer, GIN_SHARE);
826  metapage = BufferGetPage(metabuffer);
827  metadata = GinPageGetMeta(metapage);
828 
829  if (metadata->head == InvalidBlockNumber)
830  {
831  /* Nothing to do */
832  UnlockReleaseBuffer(metabuffer);
834  return;
835  }
836 
837  /*
838  * Remember a tail page to prevent infinite cleanup if other backends add
839  * new tuples faster than we can cleanup.
840  */
841  blknoFinish = metadata->tail;
842 
843  /*
844  * Read and lock head of pending list
845  */
846  blkno = metadata->head;
847  buffer = ReadBuffer(index, blkno);
848  LockBuffer(buffer, GIN_SHARE);
849  page = BufferGetPage(buffer);
850 
851  LockBuffer(metabuffer, GIN_UNLOCK);
852 
853  /*
854  * Initialize. All temporary space will be in opCtx
855  */
857  "GIN insert cleanup temporary context",
859 
860  oldCtx = MemoryContextSwitchTo(opCtx);
861 
862  initKeyArray(&datums, 128);
863  ginInitBA(&accum);
864  accum.ginstate = ginstate;
865 
866  /*
867  * At the top of this loop, we have pin and lock on the current page of
868  * the pending list. However, we'll release that before exiting the loop.
869  * Note we also have pin but not lock on the metapage.
870  */
871  for (;;)
872  {
873  Assert(!GinPageIsDeleted(page));
874 
875  /*
876  * Are we walk through the page which as we remember was a tail when
877  * we start our cleanup? But if caller asks us to clean up whole
878  * pending list then ignore old tail, we will work until list becomes
879  * empty.
880  */
881  if (blkno == blknoFinish && full_clean == false)
882  cleanupFinish = true;
883 
884  /*
885  * read page's datums into accum
886  */
887  processPendingPage(&accum, &datums, page, FirstOffsetNumber);
888 
890 
891  /*
892  * Is it time to flush memory to disk? Flush if we are at the end of
893  * the pending list, or if we have a full row and memory is getting
894  * full.
895  */
896  if (GinPageGetOpaque(page)->rightlink == InvalidBlockNumber ||
897  (GinPageHasFullRow(page) &&
898  (accum.allocatedMemory >= workMemory * 1024L)))
899  {
901  uint32 nlist;
902  Datum key;
903  GinNullCategory category;
904  OffsetNumber maxoff,
905  attnum;
906 
907  /*
908  * Unlock current page to increase performance. Changes of page
909  * will be checked later by comparing maxoff after completion of
910  * memory flush.
911  */
912  maxoff = PageGetMaxOffsetNumber(page);
913  LockBuffer(buffer, GIN_UNLOCK);
914 
915  /*
916  * Moving collected data into regular structure can take
917  * significant amount of time - so, run it without locking pending
918  * list.
919  */
920  ginBeginBAScan(&accum);
921  while ((list = ginGetBAEntry(&accum,
922  &attnum, &key, &category, &nlist)) != NULL)
923  {
924  ginEntryInsert(ginstate, attnum, key, category,
925  list, nlist, NULL);
927  }
928 
929  /*
930  * Lock the whole list to remove pages
931  */
932  LockBuffer(metabuffer, GIN_EXCLUSIVE);
933  LockBuffer(buffer, GIN_SHARE);
934 
935  Assert(!GinPageIsDeleted(page));
936 
937  /*
938  * While we left the page unlocked, more stuff might have gotten
939  * added to it. If so, process those entries immediately. There
940  * shouldn't be very many, so we don't worry about the fact that
941  * we're doing this with exclusive lock. Insertion algorithm
942  * guarantees that inserted row(s) will not continue on next page.
943  * NOTE: intentionally no vacuum_delay_point in this loop.
944  */
945  if (PageGetMaxOffsetNumber(page) != maxoff)
946  {
947  ginInitBA(&accum);
948  processPendingPage(&accum, &datums, page, maxoff + 1);
949 
950  ginBeginBAScan(&accum);
951  while ((list = ginGetBAEntry(&accum,
952  &attnum, &key, &category, &nlist)) != NULL)
953  ginEntryInsert(ginstate, attnum, key, category,
954  list, nlist, NULL);
955  }
956 
957  /*
958  * Remember next page - it will become the new list head
959  */
960  blkno = GinPageGetOpaque(page)->rightlink;
961  UnlockReleaseBuffer(buffer); /* shiftList will do exclusive
962  * locking */
963 
964  /*
965  * remove read pages from pending list, at this point all content
966  * of read pages is in regular structure
967  */
968  shiftList(index, metabuffer, blkno, fill_fsm, stats);
969 
970  /* At this point, some pending pages have been freed up */
971  fsm_vac = true;
972 
973  Assert(blkno == metadata->head);
974  LockBuffer(metabuffer, GIN_UNLOCK);
975 
976  /*
977  * if we removed the whole pending list or we cleanup tail (which
978  * we remembered on start our cleanup process) then just exit
979  */
980  if (blkno == InvalidBlockNumber || cleanupFinish)
981  break;
982 
983  /*
984  * release memory used so far and reinit state
985  */
986  MemoryContextReset(opCtx);
987  initKeyArray(&datums, datums.maxvalues);
988  ginInitBA(&accum);
989  }
990  else
991  {
992  blkno = GinPageGetOpaque(page)->rightlink;
993  UnlockReleaseBuffer(buffer);
994  }
995 
996  /*
997  * Read next page in pending list
998  */
1000  buffer = ReadBuffer(index, blkno);
1001  LockBuffer(buffer, GIN_SHARE);
1002  page = BufferGetPage(buffer);
1003  }
1004 
1006  ReleaseBuffer(metabuffer);
1007 
1008  /*
1009  * As pending list pages can have a high churn rate, it is desirable to
1010  * recycle them immediately to the FreeSpaceMap when ordinary backends
1011  * clean the list.
1012  */
1013  if (fsm_vac && fill_fsm)
1014  IndexFreeSpaceMapVacuum(index);
1015 
1016  /* Clean up temporary space */
1017  MemoryContextSwitchTo(oldCtx);
1018  MemoryContextDelete(opCtx);
1019 }
#define GinPageHasFullRow(page)
Definition: ginblock.h:120
int autovacuum_work_mem
Definition: autovacuum.c:116
#define GIN_UNLOCK
Definition: gin_private.h:48
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
#define AllocSetContextCreate
Definition: memutils.h:170
Relation index
Definition: gin_private.h:58
#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:137
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3513
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
#define GIN_METAPAGE_BLKNO
Definition: ginblock.h:52
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
uint16 OffsetNumber
Definition: off.h:24
static void initKeyArray(KeyArray *keys, int32 maxvalues)
Definition: ginfast.c:666
Definition: type.h:89
void ginEntryInsert(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gininsert.c:179
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3536
signed char GinNullCategory
Definition: ginblock.h:207
BlockNumber head
Definition: ginblock.h:62
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void LockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
Definition: lmgr.c:485
BlockNumber tail
Definition: ginblock.h:63
#define FirstOffsetNumber
Definition: off.h:27
unsigned int uint32
Definition: c.h:429
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
GinState * ginstate
Definition: gin_private.h:430
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3325
#define GIN_SHARE
Definition: gin_private.h:49
int32 maxvalues
Definition: ginfast.c:49
static void processPendingPage(BuildAccumulator *accum, KeyArray *ka, Page page, OffsetNumber startoff)
Definition: ginfast.c:703
#define GinPageIsDeleted(page)
Definition: ginblock.h:125
uintptr_t Datum
Definition: postgres.h:367
#define GIN_EXCLUSIVE
Definition: gin_private.h:50
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3752
int work_mem
Definition: globals.c:121
int16 attnum
Definition: pg_attribute.h:79
int maintenance_work_mem
Definition: globals.c:123
#define Assert(condition)
Definition: c.h:800
static void shiftList(Relation index, Buffer metabuffer, BlockNumber newHead, bool fill_fsm, IndexBulkDeleteResult *stats)
Definition: ginfast.c:545
void IndexFreeSpaceMapVacuum(Relation rel)
Definition: indexfsm.c:71
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:607
size_t Size
Definition: c.h:528
#define InvalidBlockNumber
Definition: block.h:33
ItemPointerData * ginGetBAEntry(BuildAccumulator *accum, OffsetNumber *attnum, Datum *key, GinNullCategory *category, uint32 *n)
Definition: ginbulk.c:268
void UnlockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
Definition: lmgr.c:520
bool ConditionalLockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
Definition: lmgr.c:504
void ginBeginBAScan(BuildAccumulator *accum)
Definition: ginbulk.c:257
#define GinPageGetMeta(p)
Definition: ginblock.h:105
void vacuum_delay_point(void)
Definition: vacuum.c:2031
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78
void ginInitBA(BuildAccumulator *accum)
Definition: ginbulk.c:109

◆ ginInsertItemPointers()

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

Definition at line 1902 of file gindatapage.c.

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

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

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

◆ ginInsertValue()

void ginInsertValue ( GinBtree  btree,
GinBtreeStack stack,
void *  insertdata,
GinStatsData buildStats 
)

Definition at line 776 of file ginbtree.c.

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

Referenced by ginEntryInsert(), and ginInsertItemPointers().

778 {
779  bool done;
780 
781  /* If the leaf page was incompletely split, finish the split first */
783  ginFinishSplit(btree, stack, false, buildStats);
784 
785  done = ginPlaceToPage(btree, stack,
786  insertdata, InvalidBlockNumber,
787  InvalidBuffer, buildStats);
788  if (done)
789  {
790  LockBuffer(stack->buffer, GIN_UNLOCK);
791  freeGinBtreeStack(stack);
792  }
793  else
794  ginFinishSplit(btree, stack, true, buildStats);
795 }
#define GIN_UNLOCK
Definition: gin_private.h:48
#define GinPageIsIncompleteSplit(page)
Definition: ginblock.h:128
static void ginFinishSplit(GinBtree btree, GinBtreeStack *stack, bool freestack, GinStatsData *buildStats)
Definition: ginbtree.c:667
#define InvalidBuffer
Definition: buf.h:25
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3752
#define InvalidBlockNumber
Definition: block.h:33
void freeGinBtreeStack(GinBtreeStack *stack)
Definition: ginbtree.c:194
static bool ginPlaceToPage(GinBtree btree, GinBtreeStack *stack, void *insertdata, BlockNumber updateblkno, Buffer childbuf, GinStatsData *buildStats)
Definition: ginbtree.c:329

◆ ginMergeItemPointers()

ItemPointer ginMergeItemPointers ( ItemPointerData a,
uint32  na,
ItemPointerData b,
uint32  nb,
int *  nmerged 
)

Definition at line 378 of file ginpostinglist.c.

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

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

381 {
382  ItemPointerData *dst;
383 
384  dst = (ItemPointer) palloc((na + nb) * sizeof(ItemPointerData));
385 
386  /*
387  * If the argument arrays don't overlap, we can just append them to each
388  * other.
389  */
390  if (na == 0 || nb == 0 || ginCompareItemPointers(&a[na - 1], &b[0]) < 0)
391  {
392  memcpy(dst, a, na * sizeof(ItemPointerData));
393  memcpy(&dst[na], b, nb * sizeof(ItemPointerData));
394  *nmerged = na + nb;
395  }
396  else if (ginCompareItemPointers(&b[nb - 1], &a[0]) < 0)
397  {
398  memcpy(dst, b, nb * sizeof(ItemPointerData));
399  memcpy(&dst[nb], a, na * sizeof(ItemPointerData));
400  *nmerged = na + nb;
401  }
402  else
403  {
404  ItemPointerData *dptr = dst;
405  ItemPointerData *aptr = a;
406  ItemPointerData *bptr = b;
407 
408  while (aptr - a < na && bptr - b < nb)
409  {
410  int cmp = ginCompareItemPointers(aptr, bptr);
411 
412  if (cmp > 0)
413  *dptr++ = *bptr++;
414  else if (cmp == 0)
415  {
416  /* only keep one copy of the identical items */
417  *dptr++ = *bptr++;
418  aptr++;
419  }
420  else
421  *dptr++ = *aptr++;
422  }
423 
424  while (aptr - a < na)
425  *dptr++ = *aptr++;
426 
427  while (bptr - b < nb)
428  *dptr++ = *bptr++;
429 
430  *nmerged = dptr - dst;
431  }
432 
433  return dst;
434 }
ItemPointerData * ItemPointer
Definition: itemptr.h:49
struct ItemPointerData ItemPointerData
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:484
void * palloc(Size size)
Definition: mcxt.c:950
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ GinNewBuffer()

Buffer GinNewBuffer ( Relation  index)

Definition at line 298 of file ginutil.c.

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

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

299 {
300  Buffer buffer;
301  bool needLock;
302 
303  /* First, try to get a page from FSM */
304  for (;;)
305  {
306  BlockNumber blkno = GetFreeIndexPage(index);
307 
308  if (blkno == InvalidBlockNumber)
309  break;
310 
311  buffer = ReadBuffer(index, blkno);
312 
313  /*
314  * We have to guard against the possibility that someone else already
315  * recycled this page; the buffer may be locked if so.
316  */
317  if (ConditionalLockBuffer(buffer))
318  {
319  if (GinPageIsRecyclable(BufferGetPage(buffer)))
320  return buffer; /* OK to use */
321 
322  LockBuffer(buffer, GIN_UNLOCK);
323  }
324 
325  /* Can't use it, so release buffer and try again */
326  ReleaseBuffer(buffer);
327  }
328 
329  /* Must extend the file */
330  needLock = !RELATION_IS_LOCAL(index);
331  if (needLock)
333 
334  buffer = ReadBuffer(index, P_NEW);
335  LockBuffer(buffer, GIN_EXCLUSIVE);
336 
337  if (needLock)
339 
340  return buffer;
341 }
#define GIN_UNLOCK
Definition: gin_private.h:48
#define ExclusiveLock
Definition: lockdefs.h:44
#define RELATION_IS_LOCAL(relation)
Definition: rel.h:584
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3513
#define P_NEW
Definition: bufmgr.h:91
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:3778
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:402
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:452
#define GIN_EXCLUSIVE
Definition: gin_private.h:50
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3752
BlockNumber GetFreeIndexPage(Relation rel)
Definition: indexfsm.c:38
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:607
#define InvalidBlockNumber
Definition: block.h:33
bool GinPageIsRecyclable(Page page)
Definition: ginvacuum.c:801
int Buffer
Definition: buf.h:23

◆ ginNewScanKey()

void ginNewScanKey ( IndexScanDesc  scan)

Definition at line 263 of file ginscan.c.

References i, INDEX_MAX_KEYS, IndexScanDescData::keyData, and IndexScanDescData::opaque.

Referenced by gingetbitmap().

264 {
265  ScanKey scankey = scan->keyData;
266  GinScanOpaque so = (GinScanOpaque) scan->opaque;
267  int i;
268  bool hasNullQuery = false;
269  bool attrHasNormalScan[INDEX_MAX_KEYS] = {false};
270  MemoryContext oldCtx;
271 
272  /*
273  * Allocate all the scan key information in the key context. (If
274  * extractQuery leaks anything there, it won't be reset until the end of
275  * scan or rescan, but that's OK.)
276  */
277  oldCtx = MemoryContextSwitchTo(so->keyCtx);
278 
279  /* if no scan keys provided, allocate extra EVERYTHING GinScanKey */
280  so->keys = (GinScanKey)
281  palloc(Max(scan->numberOfKeys, 1) * sizeof(GinScanKeyData));
282  so->nkeys = 0;
283 
284  /* initialize expansible array of GinScanEntry pointers */
285  so->totalentries = 0;
286  so->allocentries = 32;
287  so->entries = (GinScanEntry *)
288  palloc(so->allocentries * sizeof(GinScanEntry));
289 
290  so->isVoidRes = false;
291 
292  for (i = 0; i < scan->numberOfKeys; i++)
293  {
294  ScanKey skey = &scankey[i];
295  Datum *queryValues;
296  int32 nQueryValues = 0;
297  bool *partial_matches = NULL;
298  Pointer *extra_data = NULL;
299  bool *nullFlags = NULL;
300  GinNullCategory *categories;
301  int32 searchMode = GIN_SEARCH_MODE_DEFAULT;
302 
303  /*
304  * We assume that GIN-indexable operators are strict, so a null query
305  * argument means an unsatisfiable query.
306  */
307  if (skey->sk_flags & SK_ISNULL)
308  {
309  so->isVoidRes = true;
310  break;
311  }
312 
313  /* OK to call the extractQueryFn */
314  queryValues = (Datum *)
315  DatumGetPointer(FunctionCall7Coll(&so->ginstate.extractQueryFn[skey->sk_attno - 1],
316  so->ginstate.supportCollation[skey->sk_attno - 1],
317  skey->sk_argument,
318  PointerGetDatum(&nQueryValues),
320  PointerGetDatum(&partial_matches),
321  PointerGetDatum(&extra_data),
322  PointerGetDatum(&nullFlags),
323  PointerGetDatum(&searchMode)));
324 
325  /*
326  * If bogus searchMode is returned, treat as GIN_SEARCH_MODE_ALL; note
327  * in particular we don't allow extractQueryFn to select
328  * GIN_SEARCH_MODE_EVERYTHING.
329  */
330  if (searchMode < GIN_SEARCH_MODE_DEFAULT ||
331  searchMode > GIN_SEARCH_MODE_ALL)
332  searchMode = GIN_SEARCH_MODE_ALL;
333 
334  /* Non-default modes require the index to have placeholders */
335  if (searchMode != GIN_SEARCH_MODE_DEFAULT)
336  hasNullQuery = true;
337 
338  /*
339  * In default mode, no keys means an unsatisfiable query.
340  */
341  if (queryValues == NULL || nQueryValues <= 0)
342  {
343  if (searchMode == GIN_SEARCH_MODE_DEFAULT)
344  {
345  so->isVoidRes = true;
346  break;
347  }
348  nQueryValues = 0; /* ensure sane value */
349  }
350 
351  /*
352  * Create GinNullCategory representation. If the extractQueryFn
353  * didn't create a nullFlags array, we assume everything is non-null.
354  * While at it, detect whether any null keys are present.
355  */
356  categories = (GinNullCategory *) palloc0(nQueryValues * sizeof(GinNullCategory));
357  if (nullFlags)
358  {
359  int32 j;
360 
361  for (j = 0; j < nQueryValues; j++)
362  {
363  if (nullFlags[j])
364  {
365  categories[j] = GIN_CAT_NULL_KEY;
366  hasNullQuery = true;
367  }
368  }
369  }
370 
371  ginFillScanKey(so, skey->sk_attno,
372  skey->sk_strategy, searchMode,
373  skey->sk_argument, nQueryValues,
374  queryValues, categories,
375  partial_matches, extra_data);
376 
377  /* Remember if we had any non-excludeOnly keys */
378  if (searchMode != GIN_SEARCH_MODE_ALL)
379  attrHasNormalScan[skey->sk_attno - 1] = true;
380  }
381 
382  /*
383  * Processing GIN_SEARCH_MODE_ALL scan keys requires us to make a second
384  * pass over the scan keys. Above we marked each such scan key as
385  * excludeOnly. If the involved column has any normal (not excludeOnly)
386  * scan key as well, then we can leave it like that. Otherwise, one
387  * excludeOnly scan key must receive a GIN_CAT_EMPTY_QUERY hidden entry
388  * and be set to normal (excludeOnly = false).
389  */
390  for (i = 0; i < so->nkeys; i++)
391  {
392  GinScanKey key = &so->keys[i];
393 
394  if (key->searchMode != GIN_SEARCH_MODE_ALL)
395  continue;
396 
397  if (!attrHasNormalScan[key->attnum - 1])
398  {
399  key->excludeOnly = false;
401  attrHasNormalScan[key->attnum - 1] = true;
402  }
403  }
404 
405  /*
406  * If there are no regular scan keys, generate an EVERYTHING scankey to
407  * drive a full-index scan.
408  */
409  if (so->nkeys == 0 && !so->isVoidRes)
410  {
411  hasNullQuery = true;
414  (Datum) 0, 0,
415  NULL, NULL, NULL, NULL);
416  }
417 
418  /*
419  * If the index is version 0, it may be missing null and placeholder
420  * entries, which would render searches for nulls and full-index scans
421  * unreliable. Throw an error if so.
422  */
423  if (hasNullQuery && !so->isVoidRes)
424  {
425  GinStatsData ginStats;
426 
427  ginGetStats(scan->indexRelation, &ginStats);
428  if (ginStats.ginVersion < 1)
429  ereport(ERROR,
430  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
431  errmsg("old GIN indexes do not support whole-index scans nor searches for nulls"),
432  errhint("To fix this, do REINDEX INDEX \"%s\".",
434  }
435 
436  MemoryContextSwitchTo(oldCtx);
437 
439 }
#define InvalidStrategy
Definition: stratnum.h:24
#define GIN_CAT_EMPTY_QUERY
Definition: ginblock.h:213
int errhint(const char *fmt,...)
Definition: elog.c:1149
static void ginScanKeyAddHiddenEntry(GinScanOpaque so, GinScanKey key, GinNullCategory queryCategory)
Definition: ginscan.c:137
#define PointerGetDatum(X)
Definition: postgres.h:556
#define GIN_SEARCH_MODE_EVERYTHING
Definition: gin.h:37
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:691
signed int int32
Definition: c.h:417
Relation indexRelation
Definition: relscan.h:115
#define GIN_SEARCH_MODE_ALL
Definition: gin.h:36
char * Pointer
Definition: c.h:406
#define ERROR
Definition: elog.h:43
signed char GinNullCategory
Definition: ginblock.h:207
Datum FunctionCall7Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7)
Definition: fmgr.c:1287
struct GinScanKeyData * GinScanKey
Definition: gin_private.h:261
StrategyNumber sk_strategy
Definition: skey.h:68
GinScanOpaqueData * GinScanOpaque
Definition: gin_private.h:383
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:153
#define FirstOffsetNumber
Definition: off.h:27
#define RelationGetRelationName(relation)
Definition: rel.h:491
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:1503
OffsetNumber attnum
Definition: gin_private.h:304
#define SK_ISNULL
Definition: skey.h:115
#define GIN_CAT_NULL_KEY
Definition: ginblock.h:210
void * palloc0(Size size)
Definition: mcxt.c:981
struct GinScanKeyData GinScanKeyData
uintptr_t Datum
Definition: postgres.h:367
void ginGetStats(Relation index, GinStatsData *stats)
Definition: ginutil.c:630
#define GIN_SEARCH_MODE_DEFAULT
Definition: gin.h:34
#define ereport(elevel,...)
Definition: elog.h:155
#define Max(x, y)
Definition: c.h:976
int sk_flags
Definition: skey.h:66
#define INDEX_MAX_KEYS
int32 ginVersion
Definition: gin.h:49
#define DatumGetPointer(X)
Definition: postgres.h:549
struct ScanKeyData * keyData
Definition: relscan.h:119
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:902
int i
Datum sk_argument
Definition: skey.h:72
#define UInt16GetDatum(X)
Definition: postgres.h:465
AttrNumber sk_attno
Definition: skey.h:67

◆ ginoptions()

bytea* ginoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 609 of file ginutil.c.

References build_reloptions(), lengthof, offsetof, RELOPT_KIND_GIN, RELOPT_TYPE_BOOL, and RELOPT_TYPE_INT.

Referenced by ginhandler().

610 {
611  static const relopt_parse_elt tab[] = {
612  {"fastupdate", RELOPT_TYPE_BOOL, offsetof(GinOptions, useFastUpdate)},
613  {"gin_pending_list_limit", RELOPT_TYPE_INT, offsetof(GinOptions,
614  pendingListCleanupSize)}
615  };
616 
617  return (bytea *) build_reloptions(reloptions, validate,
619  sizeof(GinOptions),
620  tab, lengthof(tab));
621 }
#define lengthof(array)
Definition: c.h:730
void * build_reloptions(Datum reloptions, bool validate, relopt_kind kind, Size relopt_struct_size, const relopt_parse_elt *relopt_elems, int num_relopt_elems)
Definition: reloptions.c:1887
Definition: c.h:617
#define offsetof(type, field)
Definition: c.h:723

◆ GinPageDeletePostingItem()

void GinPageDeletePostingItem ( Page  page,
OffsetNumber  offset 
)

Definition at line 417 of file gindatapage.c.

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

Referenced by ginDeletePage(), and ginRedoDeletePage().

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

◆ ginPostingListDecode()

ItemPointer ginPostingListDecode ( GinPostingList ptr,
int *  ndecoded 
)

Definition at line 284 of file ginpostinglist.c.

References ginPostingListDecodeAllSegments(), and SizeOfGinPostingList.

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

285 {
286  return ginPostingListDecodeAllSegments(plist,
287  SizeOfGinPostingList(plist),
288  ndecoded);
289 }
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
ItemPointer ginPostingListDecodeAllSegments(GinPostingList *segment, int len, int *ndecoded_out)

◆ ginPostingListDecodeAllSegments()

ItemPointer ginPostingListDecodeAllSegments ( GinPostingList ptr,
int  len,
int *  ndecoded 
)

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

298 {
299  ItemPointer result;
300  int nallocated;
301  uint64 val;
302  char *endseg = ((char *) segment) + len;
303  int ndecoded;
304  unsigned char *ptr;
305  unsigned char *endptr;
306 
307  /*
308  * Guess an initial size of the array.
309  */
310  nallocated = segment->nbytes * 2 + 1;
311  result = palloc(nallocated * sizeof(ItemPointerData));
312 
313  ndecoded = 0;
314  while ((char *) segment < endseg)
315  {
316  /* enlarge output array if needed */
317  if (ndecoded >= nallocated)
318  {
319  nallocated *= 2;
320  result = repalloc(result, nallocated * sizeof(ItemPointerData));
321  }
322 
323  /* copy the first item */
325  Assert(ndecoded == 0 || ginCompareItemPointers(&segment->first, &result[ndecoded - 1]) > 0);
326  result[ndecoded] = segment->first;
327  ndecoded++;
328 
329  val = itemptr_to_uint64(&segment->first);
330  ptr = segment->bytes;
331  endptr = segment->bytes + segment->nbytes;
332  while (ptr < endptr)
333  {
334  /* enlarge output array if needed */
335  if (ndecoded >= nallocated)
336  {
337  nallocated *= 2;
338  result = repalloc(result, nallocated * sizeof(ItemPointerData));
339  }
340 
341  val += decode_varbyte(&ptr);
342 
343  uint64_to_itemptr(val, &result[ndecoded]);
344  ndecoded++;
345  }
346  segment = GinNextPostingListSegment(segment);
347  }
348 
349  if (ndecoded_out)
350  *ndecoded_out = ndecoded;
351  return result;
352 }
static uint64 decode_varbyte(unsigned char **ptr)
#define GinNextPostingListSegment(cur)
Definition: ginblock.h:344
static void uint64_to_itemptr(uint64 val, ItemPointer iptr)
uint16 nbytes
Definition: ginblock.h:339
#define Assert(condition)
Definition: c.h:800
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
static uint64 itemptr_to_uint64(const ItemPointer iptr)
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
static int ginCompareItemPointers(ItemPointer a, ItemPointer b)
Definition: gin_private.h:484
void * palloc(Size size)
Definition: mcxt.c:950
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:39
long val
Definition: informix.c:664

◆ ginPostingListDecodeAllSegmentsToTbm()

int ginPostingListDecodeAllSegmentsToTbm ( GinPostingList ptr,
int  totalsize,
TIDBitmap tbm 
)

Definition at line 358 of file ginpostinglist.c.

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

Referenced by GinDataLeafPageGetItemsToTbm().

360 {
361  int ndecoded;
362  ItemPointer items;
363 
364  items = ginPostingListDecodeAllSegments(ptr, len, &ndecoded);
365  tbm_add_tuples(tbm, items, ndecoded, false);
366  pfree(items);
367 
368  return ndecoded;
369 }
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:376
void pfree(void *pointer)
Definition: mcxt.c:1057
ItemPointer ginPostingListDecodeAllSegments(GinPostingList *segment, int len, int *ndecoded_out)

◆ ginPrepareEntryScan()

void ginPrepareEntryScan ( GinBtree  btree,
OffsetNumber  attnum,
Datum  key,
GinNullCategory  category,
GinState ginstate 
)

Definition at line 745 of file ginentrypage.c.

References attnum, GinBtreeData::beginPlaceToPage, GinBtreeData::entryAttnum, entryBeginPlaceToPage(), GinBtreeData::entryCategory, entryExecPlaceToPage(), entryFindChildPtr(), entryGetLeftMostPage(), entryIsMoveRight(), GinBtreeData::entryKey, entryLocateEntry(), entryLocateLeafEntry(), entryPrepareDownlink(), GinBtreeData::execPlaceToPage, 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, sort-test::key, 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:167
void(* fillRoot)(GinBtree, Page, BlockNumber, Page, BlockNumber, Page)
Definition: gin_private.h:161
Relation index
Definition: gin_private.h:58
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
bool(* isMoveRight)(GinBtree, Page)
Definition: gin_private.h:153
BlockNumber rootBlkno
Definition: gin_private.h:166
bool(* findItem)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:154
OffsetNumber entryAttnum
Definition: gin_private.h:172
GinPlaceToPageRC(* beginPlaceToPage)(GinBtree, Buffer, GinBtreeStack *, void *, BlockNumber, void **, Page *, Page *)
Definition: gin_private.h:158
void(* execPlaceToPage)(GinBtree, Buffer, GinBtreeStack *, void *, BlockNumber, void *)
Definition: gin_private.h:159
Datum entryKey
Definition: gin_private.h:173
BlockNumber(* findChildPage)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:151
void ginEntryFillRoot(GinBtree btree, Page root, BlockNumber lblkno, Page lpage, BlockNumber rblkno, Page rpage)
Definition: ginentrypage.c:721
GinNullCategory entryCategory
Definition: gin_private.h:174
BlockNumber(* getLeftMostChild)(GinBtree, Page)
Definition: gin_private.h:152
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:165
static void * entryPrepareDownlink(GinBtree btree, Buffer lbuf)
Definition: ginentrypage.c:700
int16 attnum
Definition: pg_attribute.h:79
void *(* prepareDownlink)(GinBtree, Buffer)
Definition: gin_private.h:160
OffsetNumber(* findChildPtr)(GinBtree, Page, BlockNumber, OffsetNumber)
Definition: gin_private.h:157
#define GIN_ROOT_BLKNO
Definition: ginblock.h:53

◆ ginReadTuple()

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:229
#define GinGetPosting(itup)
Definition: ginblock.h:239
#define GinItupIsCompressed(itup)
Definition: ginblock.h:240
ItemPointerData * ItemPointer
Definition: itemptr.h:49
char * Pointer
Definition: c.h:406
#define ERROR
Definition: elog.h:43
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:228

◆ ginrescan()

void ginrescan ( IndexScanDesc  scan,
ScanKey  key,
int  nscankeys,
ScanKey  orderbys,
int  norderbys 
)

Definition at line 442 of file ginscan.c.

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

Referenced by ginhandler().

444 {
445  GinScanOpaque so = (GinScanOpaque) scan->opaque;
446 
447  ginFreeScanKeys(so);
448 
449  if (scankey && scan->numberOfKeys > 0)
450  {
451  memmove(scan->keyData, scankey,
452  scan->numberOfKeys * sizeof(ScanKeyData));
453  }
454 }
GinScanOpaqueData * GinScanOpaque
Definition: gin_private.h:383
struct ScanKeyData * keyData
Definition: relscan.h:119
void ginFreeScanKeys(GinScanOpaque so)
Definition: ginscan.c:233

◆ ginScanBeginPostingTree()

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

Definition at line 1930 of file gindatapage.c.

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

Referenced by scanPostingTree(), and startScanEntry().

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

◆ ginStepRight()

Buffer ginStepRight ( Buffer  buffer,
Relation  index,
int  lockmode 
)

Definition at line 173 of file ginbtree.c.

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

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

174 {
175  Buffer nextbuffer;
176  Page page = BufferGetPage(buffer);
177  bool isLeaf = GinPageIsLeaf(page);
178  bool isData = GinPageIsData(page);
179  BlockNumber blkno = GinPageGetOpaque(page)->rightlink;
180 
181  nextbuffer = ReadBuffer(index, blkno);
182  LockBuffer(nextbuffer, lockmode);
183  UnlockReleaseBuffer(buffer);
184 
185  /* Sanity check that the page we stepped to is of similar kind. */
186  page = BufferGetPage(nextbuffer);
187  if (isLeaf != GinPageIsLeaf(page) || isData != GinPageIsData(page))
188  elog(ERROR, "right sibling of GIN page is of different type");
189 
190  return nextbuffer;
191 }
uint32 BlockNumber
Definition: block.h:31
#define GinPageGetOpaque(page)
Definition: ginblock.h:111
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3536
#define ERROR
Definition: elog.h:43
#define GinPageIsLeaf(page)
Definition: ginblock.h:113
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3752
#define GinPageIsData(page)
Definition: ginblock.h:116
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:607
#define elog(elevel,...)
Definition: elog.h:228
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78

◆ ginTraverseLock()

int ginTraverseLock ( Buffer  buffer,
bool  searchMode 
)

Definition at line 36 of file ginbtree.c.

References BufferGetPage, GIN_EXCLUSIVE, GIN_SHARE, GIN_UNLOCK, GinPageIsLeaf, and LockBuffer().

Referenced by ginCompareItemPointers(), and ginFindLeafPage().

37 {
38  Page page;
39  int access = GIN_SHARE;
40 
41  LockBuffer(buffer, GIN_SHARE);
42  page = BufferGetPage(buffer);
43  if (GinPageIsLeaf(page))
44  {
45  if (searchMode == false)
46  {
47  /* we should relock our page */
48  LockBuffer(buffer, GIN_UNLOCK);
49  LockBuffer(buffer, GIN_EXCLUSIVE);
50 
51  /* But root can become non-leaf during relock */
52  if (!GinPageIsLeaf(page))
53  {
54  /* restore old lock type (very rare) */
55  LockBuffer(buffer, GIN_UNLOCK);
56  LockBuffer(buffer, GIN_SHARE);
57  }
58  else
59  access = GIN_EXCLUSIVE;
60  }
61  }
62 
63  return access;
64 }
#define GIN_UNLOCK
Definition: gin_private.h:48
#define GinPageIsLeaf(page)
Definition: ginblock.h:113
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define GIN_SHARE
Definition: gin_private.h:49
#define GIN_EXCLUSIVE
Definition: gin_private.h:50
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3752
Pointer Page
Definition: bufpage.h:78

◆ gintuple_get_attrnum()

OffsetNumber gintuple_get_attrnum ( GinState ginstate,
IndexTuple  tuple 
)

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

225 {
226  OffsetNumber colN;
227 
228  if (ginstate->oneCol)
229  {
230  /* column number is not stored explicitly */
231  colN = FirstOffsetNumber;
232  }
233  else
234  {
235  Datum res;
236  bool isnull;
237 
238  /*
239  * First attribute is always int16, so we can safely use any tuple
240  * descriptor to obtain first attribute of tuple
241  */
242  res = index_getattr(tuple, FirstOffsetNumber, ginstate->tupdesc[0],
243  &isnull);
244  Assert(!isnull);
245 
246  colN = DatumGetUInt16(res);
247  Assert(colN >= FirstOffsetNumber && colN <= ginstate->origTupdesc->natts);
248  }
249 
250  return colN;
251 }
#define DatumGetUInt16(X)
Definition: postgres.h:458
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
uintptr_t Datum
Definition: postgres.h:367
TupleDesc tupdesc[INDEX_MAX_KEYS]
Definition: gin_private.h:73
#define Assert(condition)
Definition: c.h:800
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
bool oneCol
Definition: gin_private.h:59

◆ gintuple_get_key()

Datum gintuple_get_key ( GinState ginstate,
IndexTuple  tuple,
GinNullCategory category 
)

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

259 {
260  Datum res;
261  bool isnull;
262 
263  if (ginstate->oneCol)
264  {
265  /*
266  * Single column index doesn't store attribute numbers in tuples
267  */
268  res = index_getattr(tuple, FirstOffsetNumber, ginstate->origTupdesc,
269  &isnull);
270  }
271  else
272  {
273  /*
274  * Since the datum type depends on which index column it's from, we
275  * must be careful to use the right tuple descriptor here.
276  */
277  OffsetNumber colN = gintuple_get_attrnum(ginstate, tuple);
278 
280  ginstate->tupdesc[colN - 1],
281  &isnull);
282  }
283 
284  if (isnull)
285  *category = GinGetNullCategory(tuple, ginstate);
286  else
287  *category = GIN_CAT_NORM_KEY;
288 
289  return res;
290 }
#define GinGetNullCategory(itup, ginstate)
Definition: ginblock.h:221
uint16 OffsetNumber
Definition: off.h:24
OffsetNumber gintuple_get_attrnum(GinState *ginstate, IndexTuple tuple)
Definition: ginutil.c:224
#define GIN_CAT_NORM_KEY
Definition: ginblock.h:209
#define FirstOffsetNumber
Definition: off.h:27
uintptr_t Datum
Definition: postgres.h:367
TupleDesc tupdesc[INDEX_MAX_KEYS]
Definition: gin_private.h:73
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
TupleDesc origTupdesc
Definition: gin_private.h:72
bool oneCol
Definition: gin_private.h:59

◆ ginvacuumcleanup()

IndexBulkDeleteResult* ginvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)

Definition at line 687 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, GinPageIsLeaf, GinPageIsList, GinPageIsRecyclable(), GinVacuumState::ginstate, ginUpdateStats(), IndexVacuumInfo::index, IndexFreeSpaceMapVacuum(), initGinState(), IsAutoVacuumWorkerProcess(), LockBuffer(), LockRelationForExtension(), MAIN_FORKNUM, Max, GinStatsData::nDataPages, GinStatsData::nEntries, GinStatsData::nEntryPages, GinStatsData::nTotalPages, IndexVacuumInfo::num_heap_tuples, IndexBulkDeleteResult::num_index_tuples, IndexBulkDeleteResult::num_pages, PageGetMaxOffsetNumber, IndexBulkDeleteResult::pages_free, palloc0(), RBM_NORMAL, ReadBufferExtended(), RecordFreeIndexPage(), RELATION_IS_LOCAL, RelationGetNumberOfBlocks, IndexVacuumInfo::strategy, UnlockRelationForExtension(), UnlockReleaseBuffer(), and vacuum_delay_point().

Referenced by ginhandler().

688 {
689  Relation index = info->index;
690  bool needLock;
691  BlockNumber npages,
692  blkno;
693  BlockNumber totFreePages;
694  GinState ginstate;
695  GinStatsData idxStat;
696 
697  /*
698  * In an autovacuum analyze, we want to clean up pending insertions.
699  * Otherwise, an ANALYZE-only call is a no-op.
700  */
701  if (info->analyze_only)
702  {
704  {
705  initGinState(&ginstate, index);
706  ginInsertCleanup(&ginstate, false, true, true, stats);
707  }
708  return stats;
709  }
710 
711  /*
712  * Set up all-zero stats and cleanup pending inserts if ginbulkdelete
713  * wasn't called
714  */
715  if (stats == NULL)
716  {
718  initGinState(&ginstate, index);
720  false, true, stats);
721  }
722 
723  memset(&idxStat, 0, sizeof(idxStat));
724 
725  /*
726  * XXX we always report the heap tuple count as the number of index
727  * entries. This is bogus if the index is partial, but it's real hard to
728  * tell how many distinct heap entries are referenced by a GIN index.
729  */
730  stats->num_index_tuples = Max(info->num_heap_tuples, 0);
731  stats->estimated_count = info->estimated_count;
732 
733  /*
734  * Need lock unless it's local to this backend.
735  */
736  needLock = !RELATION_IS_LOCAL(index);
737 
738  if (needLock)
740  npages = RelationGetNumberOfBlocks(index);
741  if (needLock)
743 
744  totFreePages = 0;
745 
746  for (blkno = GIN_ROOT_BLKNO; blkno < npages; blkno++)
747  {
748  Buffer buffer;
749  Page page;
750 
752 
753  buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno,
754  RBM_NORMAL, info->strategy);
755  LockBuffer(buffer, GIN_SHARE);
756  page = (Page) BufferGetPage(buffer);
757 
758  if (GinPageIsRecyclable(page))
759  {
760  Assert(blkno != GIN_ROOT_BLKNO);
761  RecordFreeIndexPage(index, blkno);
762  totFreePages++;
763  }
764  else if (GinPageIsData(page))
765  {
766  idxStat.nDataPages++;
767  }
768  else if (!GinPageIsList(page))
769  {
770  idxStat.nEntryPages++;
771 
772  if (GinPageIsLeaf(page))
773  idxStat.nEntries += PageGetMaxOffsetNumber(page);
774  }
775 
776  UnlockReleaseBuffer(buffer);
777  }
778 
779  /* Update the metapage with accurate page and entry counts */
780  idxStat.nTotalPages = npages;
781  ginUpdateStats(info->index, &idxStat, false);
782 
783  /* Finally, vacuum the FSM */
785 
786  stats->pages_free = totFreePages;
787 
788  if (needLock)
790  stats->num_pages = RelationGetNumberOfBlocks(index);
791  if (needLock)
793 
794  return stats;
795 }
BlockNumber nEntryPages
Definition: gin.h:46
#define ExclusiveLock
Definition: lockdefs.h:44
#define RELATION_IS_LOCAL(relation)
Definition: rel.h:584
bool analyze_only
Definition: genam.h:47
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:654
BufferAccessStrategy strategy
Definition: genam.h:52
Relation index
Definition: genam.h:46
int64 nEntries
Definition: gin.h:48
uint32 BlockNumber
Definition: block.h:31
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
Definition: type.h:89
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3536
BlockNumber num_pages
Definition: genam.h:74
BlockNumber pages_free
Definition: genam.h:80
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:95
#define GinPageIsLeaf(page)
Definition: ginblock.h:113
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3325
#define GIN_SHARE
Definition: gin_private.h:49
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:402
void * palloc0(Size size)
Definition: mcxt.c:981
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:452
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3752
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
BlockNumber nDataPages
Definition: gin.h:47
void ginInsertCleanup(GinState *ginstate, bool full_clean, bool fill_fsm, bool forceCleanup, IndexBulkDeleteResult *stats)
Definition: ginfast.c:774
#define GinPageIsData(page)
Definition: ginblock.h:116
double num_heap_tuples
Definition: genam.h:51
#define Max(x, y)
Definition: c.h:976
#define Assert(condition)
Definition: c.h:800
void IndexFreeSpaceMapVacuum(Relation rel)
Definition: indexfsm.c:71
#define GinPageIsList(page)
Definition: ginblock.h:118
BlockNumber nTotalPages
Definition: gin.h:45
bool GinPageIsRecyclable(Page page)
Definition: ginvacuum.c:801
void ginUpdateStats(Relation index, const GinStatsData *stats, bool is_build)
Definition: ginutil.c:657
void vacuum_delay_point(void)
Definition: vacuum.c:2031
void RecordFreeIndexPage(Relation rel, BlockNumber freeBlock)
Definition: indexfsm.c:52
double num_index_tuples
Definition: genam.h:77
int Buffer
Definition: buf.h:23
bool estimated_count
Definition: genam.h:76
#define GIN_ROOT_BLKNO
Definition: ginblock.h:53
Pointer Page
Definition: bufpage.h:78
bool estimated_count
Definition: genam.h:49

◆ ginVacuumItemPointers()

ItemPointer ginVacuumItemPointers ( GinVacuumState gvs,
ItemPointerData items,
int  nitem,
int *  nremaining 
)

Definition at line 48 of file ginvacuum.c.

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

Referenced by ginVacuumEntryPage(), and ginVacuumPostingTreeLeaf().

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

◆ ginVacuumPostingTreeLeaf()

void ginVacuumPostingTreeLeaf ( Relation  rel,