PostgreSQL Source Code  git master
gininsert.c File Reference
#include "postgres.h"
#include "access/gin_private.h"
#include "access/ginxlog.h"
#include "access/tableam.h"
#include "access/xloginsert.h"
#include "catalog/index.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/indexfsm.h"
#include "storage/predicate.h"
#include "storage/smgr.h"
#include "utils/memutils.h"
#include "utils/rel.h"
Include dependency graph for gininsert.c:

Go to the source code of this file.

Data Structures

struct  GinBuildState
 

Functions

static IndexTuple addItemPointersToLeafTuple (GinState *ginstate, IndexTuple old, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats, Buffer buffer)
 
static IndexTuple buildFreshLeafTuple (GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats, Buffer buffer)
 
void ginEntryInsert (GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
 
static void ginHeapTupleBulkInsert (GinBuildState *buildstate, OffsetNumber attnum, Datum value, bool isNull, ItemPointer heapptr)
 
static void ginBuildCallback (Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
 
IndexBuildResultginbuild (Relation heap, Relation index, IndexInfo *indexInfo)
 
void ginbuildempty (Relation index)
 
static void ginHeapTupleInsert (GinState *ginstate, OffsetNumber attnum, Datum value, bool isNull, ItemPointer item)
 
bool gininsert (Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
 

Function Documentation

◆ addItemPointersToLeafTuple()

static IndexTuple addItemPointersToLeafTuple ( GinState ginstate,
IndexTuple  old,
ItemPointerData items,
uint32  nitem,
GinStatsData buildStats,
Buffer  buffer 
)
static

Definition at line 49 of file gininsert.c.

References Assert, attnum, createPostingTree(), ginCompressPostingList(), GinFormTuple(), ginInsertItemPointers(), GinIsPostingTree, GinMaxItemSize, ginMergeItemPointers(), ginReadTuple(), GinSetPostingTree, gintuple_get_attrnum(), gintuple_get_key(), GinState::index, sort-test::key, pfree(), and SizeOfGinPostingList.

Referenced by ginEntryInsert().

53 {
55  Datum key;
56  GinNullCategory category;
57  IndexTuple res;
58  ItemPointerData *newItems,
59  *oldItems;
60  int oldNPosting,
61  newNPosting;
62  GinPostingList *compressedList;
63 
64  Assert(!GinIsPostingTree(old));
65 
66  attnum = gintuple_get_attrnum(ginstate, old);
67  key = gintuple_get_key(ginstate, old, &category);
68 
69  /* merge the old and new posting lists */
70  oldItems = ginReadTuple(ginstate, attnum, old, &oldNPosting);
71 
72  newItems = ginMergeItemPointers(items, nitem,
73  oldItems, oldNPosting,
74  &newNPosting);
75 
76  /* Compress the posting list, and try to a build tuple with room for it */
77  res = NULL;
78  compressedList = ginCompressPostingList(newItems, newNPosting, GinMaxItemSize,
79  NULL);
80  pfree(newItems);
81  if (compressedList)
82  {
83  res = GinFormTuple(ginstate, attnum, key, category,
84  (char *) compressedList,
85  SizeOfGinPostingList(compressedList),
86  newNPosting,
87  false);
88  pfree(compressedList);
89  }
90  if (!res)
91  {
92  /* posting list would be too big, convert to posting tree */
93  BlockNumber postingRoot;
94 
95  /*
96  * Initialize posting tree with the old tuple's posting list. It's
97  * surely small enough to fit on one posting-tree page, and should
98  * already be in order with no duplicates.
99  */
100  postingRoot = createPostingTree(ginstate->index,
101  oldItems,
102  oldNPosting,
103  buildStats,
104  buffer);
105 
106  /* Now insert the TIDs-to-be-added into the posting tree */
107  ginInsertItemPointers(ginstate->index, postingRoot,
108  items, nitem,
109  buildStats);
110 
111  /* And build a new posting-tree-only result tuple */
112  res = GinFormTuple(ginstate, attnum, key, category, NULL, 0, 0, true);
113  GinSetPostingTree(res, postingRoot);
114  }
115  pfree(oldItems);
116 
117  return res;
118 }
GinPostingList * ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize, int *nwritten)
void ginInsertItemPointers(Relation index, BlockNumber rootBlkno, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gindatapage.c:1902
Relation index
Definition: gin_private.h:58
BlockNumber createPostingTree(Relation index, ItemPointerData *items, uint32 nitems, GinStatsData *buildStats, Buffer entrybuffer)
Definition: gindatapage.c:1769
uint32 BlockNumber
Definition: block.h:31
ItemPointer ginMergeItemPointers(ItemPointerData *a, uint32 na, ItemPointerData *b, uint32 nb, int *nmerged)
uint16 OffsetNumber
Definition: off.h:24
#define GinSetPostingTree(itup, blkno)
Definition: ginblock.h:233
void pfree(void *pointer)
Definition: mcxt.c:1056
signed char GinNullCategory
Definition: ginblock.h:207
OffsetNumber gintuple_get_attrnum(GinState *ginstate, IndexTuple tuple)
Definition: ginutil.c:224
Datum gintuple_get_key(GinState *ginstate, IndexTuple tuple, GinNullCategory *category)
Definition: ginutil.c:257
uintptr_t Datum
Definition: postgres.h:367
int16 attnum
Definition: pg_attribute.h:79
#define Assert(condition)
Definition: c.h:745
IndexTuple GinFormTuple(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, Pointer data, Size dataSize, int nipd, bool errorTooBig)
Definition: ginentrypage.c:45
ItemPointer ginReadTuple(GinState *ginstate, OffsetNumber attnum, IndexTuple itup, int *nitems)
Definition: ginentrypage.c:163
#define GinIsPostingTree(itup)
Definition: ginblock.h:232
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
#define GinMaxItemSize
Definition: ginblock.h:249

◆ buildFreshLeafTuple()

static IndexTuple buildFreshLeafTuple ( GinState ginstate,
OffsetNumber  attnum,
Datum  key,
GinNullCategory  category,
ItemPointerData items,
uint32  nitem,
GinStatsData buildStats,
Buffer  buffer 
)
static

Definition at line 129 of file gininsert.c.

References createPostingTree(), ginCompressPostingList(), GinFormTuple(), GinMaxItemSize, GinSetPostingTree, GinState::index, pfree(), and SizeOfGinPostingList.

Referenced by ginEntryInsert().

133 {
134  IndexTuple res = NULL;
135  GinPostingList *compressedList;
136 
137  /* try to build a posting list tuple with all the items */
138  compressedList = ginCompressPostingList(items, nitem, GinMaxItemSize, NULL);
139  if (compressedList)
140  {
141  res = GinFormTuple(ginstate, attnum, key, category,
142  (char *) compressedList,
143  SizeOfGinPostingList(compressedList),
144  nitem, false);
145  pfree(compressedList);
146  }
147  if (!res)
148  {
149  /* posting list would be too big, build posting tree */
150  BlockNumber postingRoot;
151 
152  /*
153  * Build posting-tree-only result tuple. We do this first so as to
154  * fail quickly if the key is too big.
155  */
156  res = GinFormTuple(ginstate, attnum, key, category, NULL, 0, 0, true);
157 
158  /*
159  * Initialize a new posting tree with the TIDs.
160  */
161  postingRoot = createPostingTree(ginstate->index, items, nitem,
162  buildStats, buffer);
163 
164  /* And save the root link in the result tuple */
165  GinSetPostingTree(res, postingRoot);
166  }
167 
168  return res;
169 }
GinPostingList * ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize, int *nwritten)
Relation index
Definition: gin_private.h:58
BlockNumber createPostingTree(Relation index, ItemPointerData *items, uint32 nitems, GinStatsData *buildStats, Buffer entrybuffer)
Definition: gindatapage.c:1769
uint32 BlockNumber
Definition: block.h:31
#define GinSetPostingTree(itup, blkno)
Definition: ginblock.h:233
void pfree(void *pointer)
Definition: mcxt.c:1056
int16 attnum
Definition: pg_attribute.h:79
IndexTuple GinFormTuple(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, Pointer data, Size dataSize, int nipd, bool errorTooBig)
Definition: ginentrypage.c:45
#define SizeOfGinPostingList(plist)
Definition: ginblock.h:343
#define GinMaxItemSize
Definition: ginblock.h:249

◆ ginbuild()

IndexBuildResult* ginbuild ( Relation  heap,
Relation  index,
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:211
#define AllocSetContextCreate
Definition: memutils.h:170
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
#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:3541
#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:490
unsigned int uint32
Definition: c.h:374
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:562
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:1066
void ginUpdateStats(Relation index, const GinStatsData *stats, bool is_build)
Definition: ginutil.c:657
void * palloc(Size size)
Definition: mcxt.c:949
MemoryContext tmpCtx
Definition: gininsert.c:35
#define elog(elevel,...)
Definition: elog.h:214
#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

◆ ginBuildCallback()

static void ginBuildCallback ( Relation  index,
ItemPointer  tid,
Datum values,
bool isnull,
bool  tupleIsAlive,
void *  state 
)
static

Definition at line 280 of file gininsert.c.

References GinBuildState::accum, BuildAccumulator::allocatedMemory, attnum, GinBuildState::buildStats, CHECK_FOR_INTERRUPTS, ginBeginBAScan(), ginEntryInsert(), ginGetBAEntry(), ginHeapTupleBulkInsert(), ginInitBA(), GinBuildState::ginstate, i, sort-test::key, sort-test::list, maintenance_work_mem, MemoryContextReset(), MemoryContextSwitchTo(), TupleDescData::natts, GinState::origTupdesc, and GinBuildState::tmpCtx.

Referenced by ginbuild().

282 {
283  GinBuildState *buildstate = (GinBuildState *) state;
284  MemoryContext oldCtx;
285  int i;
286 
287  oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
288 
289  for (i = 0; i < buildstate->ginstate.origTupdesc->natts; i++)
290  ginHeapTupleBulkInsert(buildstate, (OffsetNumber) (i + 1),
291  values[i], isnull[i], tid);
292 
293  /* If we've maxed out our available memory, dump everything to the index */
294  if (buildstate->accum.allocatedMemory >= (Size) maintenance_work_mem * 1024L)
295  {
297  Datum key;
298  GinNullCategory category;
299  uint32 nlist;
301 
302  ginBeginBAScan(&buildstate->accum);
303  while ((list = ginGetBAEntry(&buildstate->accum,
304  &attnum, &key, &category, &nlist)) != NULL)
305  {
306  /* there could be many entries, so be willing to abort here */
308  ginEntryInsert(&buildstate->ginstate, attnum, key, category,
309  list, nlist, &buildstate->buildStats);
310  }
311 
312  MemoryContextReset(buildstate->tmpCtx);
313  ginInitBA(&buildstate->accum);
314  }
315 
316  MemoryContextSwitchTo(oldCtx);
317 }
GinStatsData buildStats
Definition: gininsert.c:34
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
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
signed char GinNullCategory
Definition: ginblock.h:207
BuildAccumulator accum
Definition: gininsert.c:37
static void ginHeapTupleBulkInsert(GinBuildState *buildstate, OffsetNumber attnum, Datum value, bool isNull, ItemPointer heapptr)
Definition: gininsert.c:256
unsigned int uint32
Definition: c.h:374
uintptr_t Datum
Definition: postgres.h:367
int16 attnum
Definition: pg_attribute.h:79
int maintenance_work_mem
Definition: globals.c:123
Definition: regguts.h:298
size_t Size
Definition: c.h:473
ItemPointerData * ginGetBAEntry(BuildAccumulator *accum, OffsetNumber *attnum, Datum *key, GinNullCategory *category, uint32 *n)
Definition: ginbulk.c:268
void ginBeginBAScan(BuildAccumulator *accum)
Definition: ginbulk.c:257
static Datum values[MAXATTR]
Definition: bootstrap.c:167
MemoryContext tmpCtx
Definition: gininsert.c:35
int i
TupleDesc origTupdesc
Definition: gin_private.h:72
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
void ginInitBA(BuildAccumulator *accum)
Definition: ginbulk.c:109

◆ 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:1033
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1469
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:653
#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:3541
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
void GinInitBuffer(Buffer b, uint32 f)
Definition: ginutil.c:357
int Buffer
Definition: buf.h:23
#define GIN_LEAF
Definition: ginblock.h:42

◆ 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:1056
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:3757
void ginInsertValue(GinBtree btree, GinBtreeStack *stack, void *insertdata, GinStatsData *buildStats)
Definition: ginbtree.c:777
void CheckForSerializableConflictIn(Relation relation, ItemPointer tid, BlockNumber blkno)
Definition: predicate.c:4377
int16 attnum
Definition: pg_attribute.h:79
#define GinGetPostingTree(itup)
Definition: ginblock.h:234
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2668
#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

◆ ginHeapTupleBulkInsert()

static void ginHeapTupleBulkInsert ( GinBuildState buildstate,
OffsetNumber  attnum,
Datum  value,
bool  isNull,
ItemPointer  heapptr 
)
static

Definition at line 256 of file gininsert.c.

References GinBuildState::accum, GinBuildState::funcCtx, ginExtractEntries(), ginInsertBAEntries(), BuildAccumulator::ginstate, GinBuildState::indtuples, MemoryContextReset(), and MemoryContextSwitchTo().

Referenced by ginBuildCallback().

259 {
260  Datum *entries;
261  GinNullCategory *categories;
262  int32 nentries;
263  MemoryContext oldCtx;
264 
265  oldCtx = MemoryContextSwitchTo(buildstate->funcCtx);
266  entries = ginExtractEntries(buildstate->accum.ginstate, attnum,
267  value, isNull,
268  &nentries, &categories);
269  MemoryContextSwitchTo(oldCtx);
270 
271  ginInsertBAEntries(&buildstate->accum, heapptr, attnum,
272  entries, categories, nentries);
273 
274  buildstate->indtuples += nentries;
275 
276  MemoryContextReset(buildstate->funcCtx);
277 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
double indtuples
Definition: gininsert.c:33
signed int int32
Definition: c.h:362
signed char GinNullCategory
Definition: ginblock.h:207
BuildAccumulator accum
Definition: gininsert.c:37
GinState * ginstate
Definition: gin_private.h:430
uintptr_t Datum
Definition: postgres.h:367
void ginInsertBAEntries(BuildAccumulator *accum, ItemPointer heapptr, OffsetNumber attnum, Datum *entries, GinNullCategory *categories, int32 nentries)
Definition: ginbulk.c:210
int16 attnum
Definition: pg_attribute.h:79
static struct @143 value
MemoryContext funcCtx
Definition: gininsert.c:36
Datum * ginExtractEntries(GinState *ginstate, OffsetNumber attnum, Datum value, bool isNull, int32 *nentries, GinNullCategory **categories)
Definition: ginutil.c:490

◆ ginHeapTupleInsert()

static void ginHeapTupleInsert ( GinState ginstate,
OffsetNumber  attnum,
Datum  value,
bool  isNull,
ItemPointer  item 
)
static

Definition at line 470 of file gininsert.c.

References ginEntryInsert(), ginExtractEntries(), and i.

Referenced by gininsert().

473 {
474  Datum *entries;
475  GinNullCategory *categories;
476  int32 i,
477  nentries;
478 
479  entries = ginExtractEntries(ginstate, attnum, value, isNull,
480  &nentries, &categories);
481 
482  for (i = 0; i < nentries; i++)
483  ginEntryInsert(ginstate, attnum, entries[i], categories[i],
484  item, 1, NULL);
485 }
signed int int32
Definition: c.h:362
void ginEntryInsert(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gininsert.c:179
signed char GinNullCategory
Definition: ginblock.h:207
uintptr_t Datum
Definition: postgres.h:367
int16 attnum
Definition: pg_attribute.h:79
static struct @143 value
int i
Datum * ginExtractEntries(GinState *ginstate, OffsetNumber attnum, Datum value, bool isNull, int32 *nentries, GinNullCategory **categories)
Definition: ginutil.c:490

◆ gininsert()

bool gininsert ( Relation  index,
Datum values,
bool isnull,
ItemPointer  ht_ctid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
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:179
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#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:178
#define GinGetUseFastUpdate(relation)
Definition: gin_private.h:33
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * palloc(Size size)
Definition: mcxt.c:949
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