PostgreSQL Source Code  git master
gininsert.c File Reference
#include "postgres.h"
#include "access/gin_private.h"
#include "access/ginxlog.h"
#include "access/xloginsert.h"
#include "access/tableam.h"
#include "catalog/index.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/smgr.h"
#include "storage/indexfsm.h"
#include "storage/predicate.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, HeapTuple htup, 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 50 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().

54 {
56  Datum key;
57  GinNullCategory category;
58  IndexTuple res;
59  ItemPointerData *newItems,
60  *oldItems;
61  int oldNPosting,
62  newNPosting;
63  GinPostingList *compressedList;
64 
65  Assert(!GinIsPostingTree(old));
66 
67  attnum = gintuple_get_attrnum(ginstate, old);
68  key = gintuple_get_key(ginstate, old, &category);
69 
70  /* merge the old and new posting lists */
71  oldItems = ginReadTuple(ginstate, attnum, old, &oldNPosting);
72 
73  newItems = ginMergeItemPointers(items, nitem,
74  oldItems, oldNPosting,
75  &newNPosting);
76 
77  /* Compress the posting list, and try to a build tuple with room for it */
78  res = NULL;
79  compressedList = ginCompressPostingList(newItems, newNPosting, GinMaxItemSize,
80  NULL);
81  pfree(newItems);
82  if (compressedList)
83  {
84  res = GinFormTuple(ginstate, attnum, key, category,
85  (char *) compressedList,
86  SizeOfGinPostingList(compressedList),
87  newNPosting,
88  false);
89  pfree(compressedList);
90  }
91  if (!res)
92  {
93  /* posting list would be too big, convert to posting tree */
94  BlockNumber postingRoot;
95 
96  /*
97  * Initialize posting tree with the old tuple's posting list. It's
98  * surely small enough to fit on one posting-tree page, and should
99  * already be in order with no duplicates.
100  */
101  postingRoot = createPostingTree(ginstate->index,
102  oldItems,
103  oldNPosting,
104  buildStats,
105  buffer);
106 
107  /* Now insert the TIDs-to-be-added into the posting tree */
108  ginInsertItemPointers(ginstate->index, postingRoot,
109  items, nitem,
110  buildStats);
111 
112  /* And build a new posting-tree-only result tuple */
113  res = GinFormTuple(ginstate, attnum, key, category, NULL, 0, 0, true);
114  GinSetPostingTree(res, postingRoot);
115  }
116  pfree(oldItems);
117 
118  return res;
119 }
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:1899
Relation index
Definition: gin_private.h:53
BlockNumber createPostingTree(Relation index, ItemPointerData *items, uint32 nitems, GinStatsData *buildStats, Buffer entrybuffer)
Definition: gindatapage.c:1766
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:218
Datum gintuple_get_key(GinState *ginstate, IndexTuple tuple, GinNullCategory *category)
Definition: ginutil.c:251
uintptr_t Datum
Definition: postgres.h:367
int16 attnum
Definition: pg_attribute.h:79
#define Assert(condition)
Definition: c.h:732
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 130 of file gininsert.c.

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

Referenced by ginEntryInsert().

134 {
135  IndexTuple res = NULL;
136  GinPostingList *compressedList;
137 
138  /* try to build a posting list tuple with all the items */
139  compressedList = ginCompressPostingList(items, nitem, GinMaxItemSize, NULL);
140  if (compressedList)
141  {
142  res = GinFormTuple(ginstate, attnum, key, category,
143  (char *) compressedList,
144  SizeOfGinPostingList(compressedList),
145  nitem, false);
146  pfree(compressedList);
147  }
148  if (!res)
149  {
150  /* posting list would be too big, build posting tree */
151  BlockNumber postingRoot;
152 
153  /*
154  * Build posting-tree-only result tuple. We do this first so as to
155  * fail quickly if the key is too big.
156  */
157  res = GinFormTuple(ginstate, attnum, key, category, NULL, 0, 0, true);
158 
159  /*
160  * Initialize a new posting tree with the TIDs.
161  */
162  postingRoot = createPostingTree(ginstate->index, items, nitem,
163  buildStats, buffer);
164 
165  /* And save the root link in the result tuple */
166  GinSetPostingTree(res, postingRoot);
167  }
168 
169  return res;
170 }
GinPostingList * ginCompressPostingList(const ItemPointer ipd, int nipd, int maxsize, int *nwritten)
Relation index
Definition: gin_private.h:53
BlockNumber createPostingTree(Relation index, ItemPointerData *items, uint32 nitems, GinStatsData *buildStats, Buffer entrybuffer)
Definition: gindatapage.c:1766
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 317 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, reltuples, START_CRIT_SECTION, table_index_build_scan(), GinBuildState::tmpCtx, and UnlockReleaseBuffer().

Referenced by ginhandler().

318 {
319  IndexBuildResult *result;
320  double reltuples;
321  GinBuildState buildstate;
322  Buffer RootBuffer,
323  MetaBuffer;
325  Datum key;
326  GinNullCategory category;
327  uint32 nlist;
328  MemoryContext oldCtx;
330 
331  if (RelationGetNumberOfBlocks(index) != 0)
332  elog(ERROR, "index \"%s\" already contains data",
333  RelationGetRelationName(index));
334 
335  initGinState(&buildstate.ginstate, index);
336  buildstate.indtuples = 0;
337  memset(&buildstate.buildStats, 0, sizeof(GinStatsData));
338 
339  /* initialize the meta page */
340  MetaBuffer = GinNewBuffer(index);
341 
342  /* initialize the root page */
343  RootBuffer = GinNewBuffer(index);
344 
346  GinInitMetabuffer(MetaBuffer);
347  MarkBufferDirty(MetaBuffer);
348  GinInitBuffer(RootBuffer, GIN_LEAF);
349  MarkBufferDirty(RootBuffer);
350 
351 
352  UnlockReleaseBuffer(MetaBuffer);
353  UnlockReleaseBuffer(RootBuffer);
355 
356  /* count the root as first entry page */
357  buildstate.buildStats.nEntryPages++;
358 
359  /*
360  * create a temporary memory context that is used to hold data not yet
361  * dumped out to the index
362  */
364  "Gin build temporary context",
366 
367  /*
368  * create a temporary memory context that is used for calling
369  * ginExtractEntries(), and can be reset after each tuple
370  */
372  "Gin build temporary context for user-defined function",
374 
375  buildstate.accum.ginstate = &buildstate.ginstate;
376  ginInitBA(&buildstate.accum);
377 
378  /*
379  * Do the heap scan. We disallow sync scan here because dataPlaceToPage
380  * prefers to receive tuples in TID order.
381  */
382  reltuples = table_index_build_scan(heap, index, indexInfo, false, true,
383  ginBuildCallback, (void *) &buildstate,
384  NULL);
385 
386  /* dump remaining entries to the index */
387  oldCtx = MemoryContextSwitchTo(buildstate.tmpCtx);
388  ginBeginBAScan(&buildstate.accum);
389  while ((list = ginGetBAEntry(&buildstate.accum,
390  &attnum, &key, &category, &nlist)) != NULL)
391  {
392  /* there could be many entries, so be willing to abort here */
394  ginEntryInsert(&buildstate.ginstate, attnum, key, category,
395  list, nlist, &buildstate.buildStats);
396  }
397  MemoryContextSwitchTo(oldCtx);
398 
399  MemoryContextDelete(buildstate.funcCtx);
400  MemoryContextDelete(buildstate.tmpCtx);
401 
402  /*
403  * Update metapage stats
404  */
406  ginUpdateStats(index, &buildstate.buildStats, true);
407 
408  /*
409  * We didn't write WAL records as we built the index, so if WAL-logging is
410  * required, write all pages to the WAL now.
411  */
412  if (RelationNeedsWAL(index))
413  {
415  0, RelationGetNumberOfBlocks(index),
416  true);
417  }
418 
419  /*
420  * Return statistics
421  */
422  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
423 
424  result->heap_tuples = reltuples;
425  result->index_tuples = buildstate.indtuples;
426 
427  return result;
428 }
BlockNumber nEntryPages
Definition: gin.h:45
Buffer GinNewBuffer(Relation index)
Definition: ginutil.c:292
void GinInitMetabuffer(Buffer b)
Definition: ginutil.c:357
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1458
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
GinStatsData buildStats
Definition: gininsert.c:35
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
double indtuples
Definition: gininsert.c:34
static void ginBuildCallback(Relation index, HeapTuple htup, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
Definition: gininsert.c:276
uint16 OffsetNumber
Definition: off.h:24
GinState ginstate
Definition: gininsert.c:33
void ginEntryInsert(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gininsert.c:180
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
#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:1499
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
BuildAccumulator accum
Definition: gininsert.c:38
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:89
#define RelationGetRelationName(relation)
Definition: rel.h:453
unsigned int uint32
Definition: c.h:358
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
GinState * ginstate
Definition: gin_private.h:407
uintptr_t Datum
Definition: postgres.h:367
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:198
int16 attnum
Definition: pg_attribute.h:79
void GinInitBuffer(Buffer b, uint32 f)
Definition: ginutil.c:351
BlockNumber nTotalPages
Definition: gin.h:44
ItemPointerData * ginGetBAEntry(BuildAccumulator *accum, OffsetNumber *attnum, Datum *key, GinNullCategory *category, uint32 *n)
Definition: ginbulk.c:268
#define RelationNeedsWAL(relation)
Definition: rel.h:521
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:1042
void ginUpdateStats(Relation index, const GinStatsData *stats, bool is_build)
Definition: ginutil.c:665
void * palloc(Size size)
Definition: mcxt.c:949
MemoryContext tmpCtx
Definition: gininsert.c:36
#define elog(elevel,...)
Definition: elog.h:226
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
MemoryContext funcCtx
Definition: gininsert.c:37
int Buffer
Definition: buf.h:23
float4 reltuples
Definition: pg_class.h:63
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:41

◆ ginBuildCallback()

static void ginBuildCallback ( Relation  index,
HeapTuple  htup,
Datum values,
bool isnull,
bool  tupleIsAlive,
void *  state 
)
static

Definition at line 276 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, HeapTupleData::t_self, and GinBuildState::tmpCtx.

Referenced by ginbuild().

278 {
279  GinBuildState *buildstate = (GinBuildState *) state;
280  MemoryContext oldCtx;
281  int i;
282 
283  oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
284 
285  for (i = 0; i < buildstate->ginstate.origTupdesc->natts; i++)
286  ginHeapTupleBulkInsert(buildstate, (OffsetNumber) (i + 1),
287  values[i], isnull[i],
288  &htup->t_self);
289 
290  /* If we've maxed out our available memory, dump everything to the index */
291  if (buildstate->accum.allocatedMemory >= (Size) maintenance_work_mem * 1024L)
292  {
294  Datum key;
295  GinNullCategory category;
296  uint32 nlist;
298 
299  ginBeginBAScan(&buildstate->accum);
300  while ((list = ginGetBAEntry(&buildstate->accum,
301  &attnum, &key, &category, &nlist)) != NULL)
302  {
303  /* there could be many entries, so be willing to abort here */
305  ginEntryInsert(&buildstate->ginstate, attnum, key, category,
306  list, nlist, &buildstate->buildStats);
307  }
308 
309  MemoryContextReset(buildstate->tmpCtx);
310  ginInitBA(&buildstate->accum);
311  }
312 
313  MemoryContextSwitchTo(oldCtx);
314 }
GinStatsData buildStats
Definition: gininsert.c:35
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:33
void ginEntryInsert(GinState *ginstate, OffsetNumber attnum, Datum key, GinNullCategory category, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gininsert.c:180
signed char GinNullCategory
Definition: ginblock.h:207
ItemPointerData t_self
Definition: htup.h:65
BuildAccumulator accum
Definition: gininsert.c:38
static void ginHeapTupleBulkInsert(GinBuildState *buildstate, OffsetNumber attnum, Datum value, bool isNull, ItemPointer heapptr)
Definition: gininsert.c:252
unsigned int uint32
Definition: c.h:358
uintptr_t Datum
Definition: postgres.h:367
int16 attnum
Definition: pg_attribute.h:79
int maintenance_work_mem
Definition: globals.c:122
Definition: regguts.h:298
size_t Size
Definition: c.h:466
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:36
int i
TupleDesc origTupdesc
Definition: gin_private.h:67
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
void ginInitBA(BuildAccumulator *accum)
Definition: ginbulk.c:109

◆ ginbuildempty()

void ginbuildempty ( Relation  index)

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

435 {
436  Buffer RootBuffer,
437  MetaBuffer;
438 
439  /* An empty GIN index has two pages. */
440  MetaBuffer =
442  LockBuffer(MetaBuffer, BUFFER_LOCK_EXCLUSIVE);
443  RootBuffer =
445  LockBuffer(RootBuffer, BUFFER_LOCK_EXCLUSIVE);
446 
447  /* Initialize and xlog metabuffer and root buffer. */
449  GinInitMetabuffer(MetaBuffer);
450  MarkBufferDirty(MetaBuffer);
451  log_newpage_buffer(MetaBuffer, true);
452  GinInitBuffer(RootBuffer, GIN_LEAF);
453  MarkBufferDirty(RootBuffer);
454  log_newpage_buffer(RootBuffer, false);
456 
457  /* Unlock and release the buffers. */
458  UnlockReleaseBuffer(MetaBuffer);
459  UnlockReleaseBuffer(RootBuffer);
460 }
void GinInitMetabuffer(Buffer b)
Definition: ginutil.c:357
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1009
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1458
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
#define P_NEW
Definition: bufmgr.h:81
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:88
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
void GinInitBuffer(Buffer b, uint32 f)
Definition: ginutil.c:351
int Buffer
Definition: buf.h:23
#define GIN_LEAF
Definition: ginblock.h:41

◆ ginEntryInsert()

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

Definition at line 180 of file gininsert.c.

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

184 {
185  GinBtreeData btree;
186  GinBtreeEntryInsertData insertdata;
187  GinBtreeStack *stack;
188  IndexTuple itup;
189  Page page;
190 
191  insertdata.isDelete = false;
192 
193  /* During index build, count the to-be-inserted entry */
194  if (buildStats)
195  buildStats->nEntries++;
196 
197  ginPrepareEntryScan(&btree, attnum, key, category, ginstate);
198  btree.isBuild = (buildStats != NULL);
199 
200  stack = ginFindLeafPage(&btree, false, false, NULL);
201  page = BufferGetPage(stack->buffer);
202 
203  if (btree.findItem(&btree, stack))
204  {
205  /* found pre-existing entry */
206  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, stack->off));
207 
208  if (GinIsPostingTree(itup))
209  {
210  /* add entries to existing posting tree */
211  BlockNumber rootPostingTree = GinGetPostingTree(itup);
212 
213  /* release all stack */
214  LockBuffer(stack->buffer, GIN_UNLOCK);
215  freeGinBtreeStack(stack);
216 
217  /* insert into posting tree */
218  ginInsertItemPointers(ginstate->index, rootPostingTree,
219  items, nitem,
220  buildStats);
221  return;
222  }
223 
224  CheckForSerializableConflictIn(ginstate->index, NULL, stack->buffer);
225  /* modify an existing leaf entry */
226  itup = addItemPointersToLeafTuple(ginstate, itup,
227  items, nitem, buildStats, stack->buffer);
228 
229  insertdata.isDelete = true;
230  }
231  else
232  {
233  CheckForSerializableConflictIn(ginstate->index, NULL, stack->buffer);
234  /* no match, so construct a new leaf entry */
235  itup = buildFreshLeafTuple(ginstate, attnum, key, category,
236  items, nitem, buildStats, stack->buffer);
237  }
238 
239  /* Insert the new or modified leaf tuple */
240  insertdata.entry = itup;
241  ginInsertValue(&btree, stack, &insertdata, buildStats);
242  pfree(itup);
243 }
#define GIN_UNLOCK
Definition: gin_private.h:43
void ginInsertItemPointers(Relation index, BlockNumber rootBlkno, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats)
Definition: gindatapage.c:1899
Relation index
Definition: gin_private.h:53
OffsetNumber off
Definition: gin_private.h:126
GinBtreeStack * ginFindLeafPage(GinBtree btree, bool searchMode, bool rootConflictCheck, Snapshot snapshot)
Definition: ginbtree.c:80
int64 nEntries
Definition: gin.h:47
uint32 BlockNumber
Definition: block.h:31
void CheckForSerializableConflictIn(Relation relation, HeapTuple tuple, Buffer buffer)
Definition: predicate.c:4442
static IndexTuple addItemPointersToLeafTuple(GinState *ginstate, IndexTuple old, ItemPointerData *items, uint32 nitem, GinStatsData *buildStats, Buffer buffer)
Definition: gininsert.c:50
bool(* findItem)(GinBtree, GinBtreeStack *)
Definition: gin_private.h:149
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:130
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
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:3602
void ginInsertValue(GinBtree btree, GinBtreeStack *stack, void *insertdata, GinStatsData *buildStats)
Definition: ginbtree.c:784
int16 attnum
Definition: pg_attribute.h:79
#define GinGetPostingTree(itup)
Definition: ginblock.h:234
#define GinIsPostingTree(itup)
Definition: ginblock.h:232
void freeGinBtreeStack(GinBtreeStack *stack)
Definition: ginbtree.c:201
#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 252 of file gininsert.c.

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

Referenced by ginBuildCallback().

255 {
256  Datum *entries;
257  GinNullCategory *categories;
258  int32 nentries;
259  MemoryContext oldCtx;
260 
261  oldCtx = MemoryContextSwitchTo(buildstate->funcCtx);
262  entries = ginExtractEntries(buildstate->accum.ginstate, attnum,
263  value, isNull,
264  &nentries, &categories);
265  MemoryContextSwitchTo(oldCtx);
266 
267  ginInsertBAEntries(&buildstate->accum, heapptr, attnum,
268  entries, categories, nentries);
269 
270  buildstate->indtuples += nentries;
271 
272  MemoryContextReset(buildstate->funcCtx);
273 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static struct @145 value
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
double indtuples
Definition: gininsert.c:34
signed int int32
Definition: c.h:346
signed char GinNullCategory
Definition: ginblock.h:207
BuildAccumulator accum
Definition: gininsert.c:38
GinState * ginstate
Definition: gin_private.h:407
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
MemoryContext funcCtx
Definition: gininsert.c:37
Datum * ginExtractEntries(GinState *ginstate, OffsetNumber attnum, Datum value, bool isNull, int32 *nentries, GinNullCategory **categories)
Definition: ginutil.c:484

◆ ginHeapTupleInsert()

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

Definition at line 467 of file gininsert.c.

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

Referenced by gininsert().

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

◆ gininsert()

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

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

489 {
490  GinState *ginstate = (GinState *) indexInfo->ii_AmCache;
491  MemoryContext oldCtx;
492  MemoryContext insertCtx;
493  int i;
494 
495  /* Initialize GinState cache if first call in this statement */
496  if (ginstate == NULL)
497  {
498  oldCtx = MemoryContextSwitchTo(indexInfo->ii_Context);
499  ginstate = (GinState *) palloc(sizeof(GinState));
500  initGinState(ginstate, index);
501  indexInfo->ii_AmCache = (void *) ginstate;
502  MemoryContextSwitchTo(oldCtx);
503  }
504 
506  "Gin insert temporary context",
508 
509  oldCtx = MemoryContextSwitchTo(insertCtx);
510 
511  if (GinGetUseFastUpdate(index))
512  {
513  GinTupleCollector collector;
514 
515  memset(&collector, 0, sizeof(GinTupleCollector));
516 
517  for (i = 0; i < ginstate->origTupdesc->natts; i++)
518  ginHeapTupleFastCollect(ginstate, &collector,
519  (OffsetNumber) (i + 1),
520  values[i], isnull[i],
521  ht_ctid);
522 
523  ginHeapTupleFastInsert(ginstate, &collector);
524  }
525  else
526  {
527  for (i = 0; i < ginstate->origTupdesc->natts; i++)
528  ginHeapTupleInsert(ginstate, (OffsetNumber) (i + 1),
529  values[i], isnull[i],
530  ht_ctid);
531  }
532 
533  MemoryContextSwitchTo(oldCtx);
534  MemoryContextDelete(insertCtx);
535 
536  return false;
537 }
MemoryContext ii_Context
Definition: execnodes.h:177
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
void ginHeapTupleFastInsert(GinState *ginstate, GinTupleCollector *collector)
Definition: ginfast.c:218
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:467
uint16 OffsetNumber
Definition: off.h:24
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:89
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void * ii_AmCache
Definition: execnodes.h:176
#define GinGetUseFastUpdate(relation)
Definition: gin_private.h:32
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:473
TupleDesc origTupdesc
Definition: gin_private.h:67