PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
gistbuild.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * gistbuild.c
4  * build algorithm for GiST indexes implementation.
5  *
6  *
7  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  * src/backend/access/gist/gistbuild.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include <math.h>
18 
19 #include "access/genam.h"
20 #include "access/gist_private.h"
21 #include "access/xloginsert.h"
22 #include "catalog/index.h"
23 #include "miscadmin.h"
24 #include "optimizer/cost.h"
25 #include "storage/bufmgr.h"
26 #include "storage/smgr.h"
27 #include "utils/memutils.h"
28 #include "utils/rel.h"
29 
30 /* Step of index tuples for check whether to switch to buffering build mode */
31 #define BUFFERING_MODE_SWITCH_CHECK_STEP 256
32 
33 /*
34  * Number of tuples to process in the slow way before switching to buffering
35  * mode, when buffering is explicitly turned on. Also, the number of tuples
36  * to process between readjusting the buffer size parameter, while in
37  * buffering mode.
38  */
39 #define BUFFERING_MODE_TUPLE_SIZE_STATS_TARGET 4096
40 
41 typedef enum
42 {
43  GIST_BUFFERING_DISABLED, /* in regular build mode and aren't going to
44  * switch */
45  GIST_BUFFERING_AUTO, /* in regular build mode, but will switch to
46  * buffering build mode if the index grows too
47  * big */
48  GIST_BUFFERING_STATS, /* gathering statistics of index tuple size
49  * before switching to the buffering build
50  * mode */
51  GIST_BUFFERING_ACTIVE /* in buffering build mode */
53 
54 /* Working state for gistbuild and its callback */
55 typedef struct
56 {
59 
60  int64 indtuples; /* number of tuples indexed */
61  int64 indtuplesSize; /* total size of all indexed tuples */
62 
63  Size freespace; /* amount of free space to leave on pages */
64 
65  /*
66  * Extra data structures used during a buffering build. 'gfbb' contains
67  * information related to managing the build buffers. 'parentMap' is a
68  * lookup table of the parent of each internal page.
69  */
72 
75 
76 /* prototypes for private functions */
77 static void gistInitBuffering(GISTBuildState *buildstate);
78 static int calculatePagesPerBuffer(GISTBuildState *buildstate, int levelStep);
79 static void gistBuildCallback(Relation index,
80  HeapTuple htup,
81  Datum *values,
82  bool *isnull,
83  bool tupleIsAlive,
84  void *state);
85 static void gistBufferingBuildInsert(GISTBuildState *buildstate,
86  IndexTuple itup);
87 static bool gistProcessItup(GISTBuildState *buildstate, IndexTuple itup,
88  BlockNumber startblkno, int startlevel);
90  Buffer buffer, int level,
91  IndexTuple *itup, int ntup, OffsetNumber oldoffnum,
92  BlockNumber parentblk, OffsetNumber downlinkoffnum);
94  BlockNumber childblkno, int level,
95  BlockNumber *parentblk,
96  OffsetNumber *downlinkoffnum);
97 static void gistProcessEmptyingQueue(GISTBuildState *buildstate);
98 static void gistEmptyAllBuffers(GISTBuildState *buildstate);
99 static int gistGetMaxLevel(Relation index);
100 
101 static void gistInitParentMap(GISTBuildState *buildstate);
102 static void gistMemorizeParent(GISTBuildState *buildstate, BlockNumber child,
104 static void gistMemorizeAllDownlinks(GISTBuildState *buildstate, Buffer parent);
106 
107 /*
108  * Main entry point to GiST index build. Initially calls insert over and over,
109  * but switches to more efficient buffering build algorithm after a certain
110  * number of tuples (unless buffering mode is disabled).
111  */
114 {
115  IndexBuildResult *result;
116  double reltuples;
117  GISTBuildState buildstate;
118  Buffer buffer;
119  Page page;
121  int fillfactor;
122 
123  buildstate.indexrel = index;
124  if (index->rd_options)
125  {
126  /* Get buffering mode from the options string */
128  char *bufferingMode = (char *) options + options->bufferingModeOffset;
129 
130  if (strcmp(bufferingMode, "on") == 0)
131  buildstate.bufferingMode = GIST_BUFFERING_STATS;
132  else if (strcmp(bufferingMode, "off") == 0)
134  else
135  buildstate.bufferingMode = GIST_BUFFERING_AUTO;
136 
137  fillfactor = options->fillfactor;
138  }
139  else
140  {
141  /*
142  * By default, switch to buffering mode when the index grows too large
143  * to fit in cache.
144  */
145  buildstate.bufferingMode = GIST_BUFFERING_AUTO;
146  fillfactor = GIST_DEFAULT_FILLFACTOR;
147  }
148  /* Calculate target amount of free space to leave on pages */
149  buildstate.freespace = BLCKSZ * (100 - fillfactor) / 100;
150 
151  /*
152  * We expect to be called exactly once for any index relation. If that's
153  * not the case, big trouble's what we have.
154  */
155  if (RelationGetNumberOfBlocks(index) != 0)
156  elog(ERROR, "index \"%s\" already contains data",
157  RelationGetRelationName(index));
158 
159  /* no locking is needed */
160  buildstate.giststate = initGISTstate(index);
161 
162  /*
163  * Create a temporary memory context that is reset once for each tuple
164  * processed. (Note: we don't bother to make this a child of the
165  * giststate's scanCxt, so we have to delete it separately at the end.)
166  */
167  buildstate.giststate->tempCxt = createTempGistContext();
168 
169  /* initialize the root page */
170  buffer = gistNewBuffer(index);
172  page = BufferGetPage(buffer);
173 
175 
176  GISTInitBuffer(buffer, F_LEAF);
177 
178  MarkBufferDirty(buffer);
179 
180  if (RelationNeedsWAL(index))
181  {
182  XLogRecPtr recptr;
183 
184  XLogBeginInsert();
186 
187  recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_CREATE_INDEX);
188  PageSetLSN(page, recptr);
189  }
190  else
191  PageSetLSN(page, gistGetFakeLSN(heap));
192 
193  UnlockReleaseBuffer(buffer);
194 
196 
197  /* build the index */
198  buildstate.indtuples = 0;
199  buildstate.indtuplesSize = 0;
200 
201  /*
202  * Do the heap scan.
203  */
204  reltuples = IndexBuildHeapScan(heap, index, indexInfo, true,
205  gistBuildCallback, (void *) &buildstate);
206 
207  /*
208  * If buffering was used, flush out all the tuples that are still in the
209  * buffers.
210  */
211  if (buildstate.bufferingMode == GIST_BUFFERING_ACTIVE)
212  {
213  elog(DEBUG1, "all tuples processed, emptying buffers");
214  gistEmptyAllBuffers(&buildstate);
215  gistFreeBuildBuffers(buildstate.gfbb);
216  }
217 
218  /* okay, all heap tuples are indexed */
219  MemoryContextSwitchTo(oldcxt);
221 
222  freeGISTstate(buildstate.giststate);
223 
224  /*
225  * Return statistics
226  */
227  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
228 
229  result->heap_tuples = reltuples;
230  result->index_tuples = (double) buildstate.indtuples;
231 
232  return result;
233 }
234 
235 /*
236  * Validator for "buffering" reloption on GiST indexes. Allows "on", "off"
237  * and "auto" values.
238  */
239 void
241 {
242  if (value == NULL ||
243  (strcmp(value, "on") != 0 &&
244  strcmp(value, "off") != 0 &&
245  strcmp(value, "auto") != 0))
246  {
247  ereport(ERROR,
248  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
249  errmsg("invalid value for \"buffering\" option"),
250  errdetail("Valid values are \"on\", \"off\", and \"auto\".")));
251  }
252 }
253 
254 /*
255  * Attempt to switch to buffering mode.
256  *
257  * If there is not enough memory for buffering build, sets bufferingMode
258  * to GIST_BUFFERING_DISABLED, so that we don't bother to try the switch
259  * anymore. Otherwise initializes the build buffers, and sets bufferingMode to
260  * GIST_BUFFERING_ACTIVE.
261  */
262 static void
264 {
265  Relation index = buildstate->indexrel;
266  int pagesPerBuffer;
267  Size pageFreeSpace;
268  Size itupAvgSize,
269  itupMinSize;
270  double avgIndexTuplesPerPage,
271  maxIndexTuplesPerPage;
272  int i;
273  int levelStep;
274 
275  /* Calc space of index page which is available for index tuples */
276  pageFreeSpace = BLCKSZ - SizeOfPageHeaderData - sizeof(GISTPageOpaqueData)
277  - sizeof(ItemIdData)
278  - buildstate->freespace;
279 
280  /*
281  * Calculate average size of already inserted index tuples using gathered
282  * statistics.
283  */
284  itupAvgSize = (double) buildstate->indtuplesSize /
285  (double) buildstate->indtuples;
286 
287  /*
288  * Calculate minimal possible size of index tuple by index metadata.
289  * Minimal possible size of varlena is VARHDRSZ.
290  *
291  * XXX: that's not actually true, as a short varlen can be just 2 bytes.
292  * And we should take padding into account here.
293  */
294  itupMinSize = (Size) MAXALIGN(sizeof(IndexTupleData));
295  for (i = 0; i < index->rd_att->natts; i++)
296  {
297  if (index->rd_att->attrs[i]->attlen < 0)
298  itupMinSize += VARHDRSZ;
299  else
300  itupMinSize += index->rd_att->attrs[i]->attlen;
301  }
302 
303  /* Calculate average and maximal number of index tuples which fit to page */
304  avgIndexTuplesPerPage = pageFreeSpace / itupAvgSize;
305  maxIndexTuplesPerPage = pageFreeSpace / itupMinSize;
306 
307  /*
308  * We need to calculate two parameters for the buffering algorithm:
309  * levelStep and pagesPerBuffer.
310  *
311  * levelStep determines the size of subtree that we operate on, while
312  * emptying a buffer. A higher value is better, as you need fewer buffer
313  * emptying steps to build the index. However, if you set it too high, the
314  * subtree doesn't fit in cache anymore, and you quickly lose the benefit
315  * of the buffers.
316  *
317  * In Arge et al's paper, levelStep is chosen as logB(M/4B), where B is
318  * the number of tuples on page (ie. fanout), and M is the amount of
319  * internal memory available. Curiously, they doesn't explain *why* that
320  * setting is optimal. We calculate it by taking the highest levelStep so
321  * that a subtree still fits in cache. For a small B, our way of
322  * calculating levelStep is very close to Arge et al's formula. For a
323  * large B, our formula gives a value that is 2x higher.
324  *
325  * The average size (in pages) of a subtree of depth n can be calculated
326  * as a geometric series:
327  *
328  * B^0 + B^1 + B^2 + ... + B^n = (1 - B^(n + 1)) / (1 - B)
329  *
330  * where B is the average number of index tuples on page. The subtree is
331  * cached in the shared buffer cache and the OS cache, so we choose
332  * levelStep so that the subtree size is comfortably smaller than
333  * effective_cache_size, with a safety factor of 4.
334  *
335  * The estimate on the average number of index tuples on page is based on
336  * average tuple sizes observed before switching to buffered build, so the
337  * real subtree size can be somewhat larger. Also, it would selfish to
338  * gobble the whole cache for our index build. The safety factor of 4
339  * should account for those effects.
340  *
341  * The other limiting factor for setting levelStep is that while
342  * processing a subtree, we need to hold one page for each buffer at the
343  * next lower buffered level. The max. number of buffers needed for that
344  * is maxIndexTuplesPerPage^levelStep. This is very conservative, but
345  * hopefully maintenance_work_mem is set high enough that you're
346  * constrained by effective_cache_size rather than maintenance_work_mem.
347  *
348  * XXX: the buffer hash table consumes a fair amount of memory too per
349  * buffer, but that is not currently taken into account. That scales on
350  * the total number of buffers used, ie. the index size and on levelStep.
351  * Note that a higher levelStep *reduces* the amount of memory needed for
352  * the hash table.
353  */
354  levelStep = 1;
355  for (;;)
356  {
357  double subtreesize;
358  double maxlowestlevelpages;
359 
360  /* size of an average subtree at this levelStep (in pages). */
361  subtreesize =
362  (1 - pow(avgIndexTuplesPerPage, (double) (levelStep + 1))) /
363  (1 - avgIndexTuplesPerPage);
364 
365  /* max number of pages at the lowest level of a subtree */
366  maxlowestlevelpages = pow(maxIndexTuplesPerPage, (double) levelStep);
367 
368  /* subtree must fit in cache (with safety factor of 4) */
369  if (subtreesize > effective_cache_size / 4)
370  break;
371 
372  /* each node in the lowest level of a subtree has one page in memory */
373  if (maxlowestlevelpages > ((double) maintenance_work_mem * 1024) / BLCKSZ)
374  break;
375 
376  /* Good, we can handle this levelStep. See if we can go one higher. */
377  levelStep++;
378  }
379 
380  /*
381  * We just reached an unacceptable value of levelStep in previous loop.
382  * So, decrease levelStep to get last acceptable value.
383  */
384  levelStep--;
385 
386  /*
387  * If there's not enough cache or maintenance_work_mem, fall back to plain
388  * inserts.
389  */
390  if (levelStep <= 0)
391  {
392  elog(DEBUG1, "failed to switch to buffered GiST build");
394  return;
395  }
396 
397  /*
398  * The second parameter to set is pagesPerBuffer, which determines the
399  * size of each buffer. We adjust pagesPerBuffer also during the build,
400  * which is why this calculation is in a separate function.
401  */
402  pagesPerBuffer = calculatePagesPerBuffer(buildstate, levelStep);
403 
404  /* Initialize GISTBuildBuffers with these parameters */
405  buildstate->gfbb = gistInitBuildBuffers(pagesPerBuffer, levelStep,
406  gistGetMaxLevel(index));
407 
408  gistInitParentMap(buildstate);
409 
410  buildstate->bufferingMode = GIST_BUFFERING_ACTIVE;
411 
412  elog(DEBUG1, "switched to buffered GiST build; level step = %d, pagesPerBuffer = %d",
413  levelStep, pagesPerBuffer);
414 }
415 
416 /*
417  * Calculate pagesPerBuffer parameter for the buffering algorithm.
418  *
419  * Buffer size is chosen so that assuming that tuples are distributed
420  * randomly, emptying half a buffer fills on average one page in every buffer
421  * at the next lower level.
422  */
423 static int
424 calculatePagesPerBuffer(GISTBuildState *buildstate, int levelStep)
425 {
426  double pagesPerBuffer;
427  double avgIndexTuplesPerPage;
428  double itupAvgSize;
429  Size pageFreeSpace;
430 
431  /* Calc space of index page which is available for index tuples */
432  pageFreeSpace = BLCKSZ - SizeOfPageHeaderData - sizeof(GISTPageOpaqueData)
433  - sizeof(ItemIdData)
434  - buildstate->freespace;
435 
436  /*
437  * Calculate average size of already inserted index tuples using gathered
438  * statistics.
439  */
440  itupAvgSize = (double) buildstate->indtuplesSize /
441  (double) buildstate->indtuples;
442 
443  avgIndexTuplesPerPage = pageFreeSpace / itupAvgSize;
444 
445  /*
446  * Recalculate required size of buffers.
447  */
448  pagesPerBuffer = 2 * pow(avgIndexTuplesPerPage, levelStep);
449 
450  return (int) rint(pagesPerBuffer);
451 }
452 
453 /*
454  * Per-tuple callback from IndexBuildHeapScan.
455  */
456 static void
458  HeapTuple htup,
459  Datum *values,
460  bool *isnull,
461  bool tupleIsAlive,
462  void *state)
463 {
464  GISTBuildState *buildstate = (GISTBuildState *) state;
465  IndexTuple itup;
466  MemoryContext oldCtx;
467 
468  oldCtx = MemoryContextSwitchTo(buildstate->giststate->tempCxt);
469 
470  /* form an index tuple and point it at the heap tuple */
471  itup = gistFormTuple(buildstate->giststate, index, values, isnull, true);
472  itup->t_tid = htup->t_self;
473 
474  if (buildstate->bufferingMode == GIST_BUFFERING_ACTIVE)
475  {
476  /* We have buffers, so use them. */
477  gistBufferingBuildInsert(buildstate, itup);
478  }
479  else
480  {
481  /*
482  * There's no buffers (yet). Since we already have the index relation
483  * locked, we call gistdoinsert directly.
484  */
485  gistdoinsert(index, itup, buildstate->freespace,
486  buildstate->giststate);
487  }
488 
489  /* Update tuple count and total size. */
490  buildstate->indtuples += 1;
491  buildstate->indtuplesSize += IndexTupleSize(itup);
492 
493  MemoryContextSwitchTo(oldCtx);
494  MemoryContextReset(buildstate->giststate->tempCxt);
495 
496  if (buildstate->bufferingMode == GIST_BUFFERING_ACTIVE &&
498  {
499  /* Adjust the target buffer size now */
500  buildstate->gfbb->pagesPerBuffer =
501  calculatePagesPerBuffer(buildstate, buildstate->gfbb->levelStep);
502  }
503 
504  /*
505  * In 'auto' mode, check if the index has grown too large to fit in cache,
506  * and switch to buffering mode if it has.
507  *
508  * To avoid excessive calls to smgrnblocks(), only check this every
509  * BUFFERING_MODE_SWITCH_CHECK_STEP index tuples
510  */
511  if ((buildstate->bufferingMode == GIST_BUFFERING_AUTO &&
512  buildstate->indtuples % BUFFERING_MODE_SWITCH_CHECK_STEP == 0 &&
514  (buildstate->bufferingMode == GIST_BUFFERING_STATS &&
516  {
517  /*
518  * Index doesn't fit in effective cache anymore. Try to switch to
519  * buffering build mode.
520  */
521  gistInitBuffering(buildstate);
522  }
523 }
524 
525 /*
526  * Insert function for buffering index build.
527  */
528 static void
530 {
531  /* Insert the tuple to buffers. */
532  gistProcessItup(buildstate, itup, 0, buildstate->gfbb->rootlevel);
533 
534  /* If we filled up (half of a) buffer, process buffer emptying. */
535  gistProcessEmptyingQueue(buildstate);
536 }
537 
538 /*
539  * Process an index tuple. Runs the tuple down the tree until we reach a leaf
540  * page or node buffer, and inserts the tuple there. Returns true if we have
541  * to stop buffer emptying process (because one of child buffers can't take
542  * index tuples anymore).
543  */
544 static bool
546  BlockNumber startblkno, int startlevel)
547 {
548  GISTSTATE *giststate = buildstate->giststate;
549  GISTBuildBuffers *gfbb = buildstate->gfbb;
550  Relation indexrel = buildstate->indexrel;
551  BlockNumber childblkno;
552  Buffer buffer;
553  bool result = false;
555  int level;
556  OffsetNumber downlinkoffnum = InvalidOffsetNumber;
557  BlockNumber parentblkno = InvalidBlockNumber;
558 
560 
561  /*
562  * Loop until we reach a leaf page (level == 0) or a level with buffers
563  * (not including the level we start at, because we would otherwise make
564  * no progress).
565  */
566  blkno = startblkno;
567  level = startlevel;
568  for (;;)
569  {
570  ItemId iid;
571  IndexTuple idxtuple,
572  newtup;
573  Page page;
574  OffsetNumber childoffnum;
575 
576  /* Have we reached a level with buffers? */
577  if (LEVEL_HAS_BUFFERS(level, gfbb) && level != startlevel)
578  break;
579 
580  /* Have we reached a leaf page? */
581  if (level == 0)
582  break;
583 
584  /*
585  * Nope. Descend down to the next level then. Choose a child to
586  * descend down to.
587  */
588 
589  buffer = ReadBuffer(indexrel, blkno);
590  LockBuffer(buffer, GIST_EXCLUSIVE);
591 
592  page = (Page) BufferGetPage(buffer);
593  childoffnum = gistchoose(indexrel, page, itup, giststate);
594  iid = PageGetItemId(page, childoffnum);
595  idxtuple = (IndexTuple) PageGetItem(page, iid);
596  childblkno = ItemPointerGetBlockNumber(&(idxtuple->t_tid));
597 
598  if (level > 1)
599  gistMemorizeParent(buildstate, childblkno, blkno);
600 
601  /*
602  * Check that the key representing the target child node is consistent
603  * with the key we're inserting. Update it if it's not.
604  */
605  newtup = gistgetadjusted(indexrel, idxtuple, itup, giststate);
606  if (newtup)
607  {
608  blkno = gistbufferinginserttuples(buildstate,
609  buffer,
610  level,
611  &newtup,
612  1,
613  childoffnum,
616  /* gistbufferinginserttuples() released the buffer */
617  }
618  else
619  UnlockReleaseBuffer(buffer);
620 
621  /* Descend to the child */
622  parentblkno = blkno;
623  blkno = childblkno;
624  downlinkoffnum = childoffnum;
625  Assert(level > 0);
626  level--;
627  }
628 
629  if (LEVEL_HAS_BUFFERS(level, gfbb))
630  {
631  /*
632  * We've reached level with buffers. Place the index tuple to the
633  * buffer, and add the buffer to the emptying queue if it overflows.
634  */
635  GISTNodeBuffer *childNodeBuffer;
636 
637  /* Find the buffer or create a new one */
638  childNodeBuffer = gistGetNodeBuffer(gfbb, giststate, blkno, level);
639 
640  /* Add index tuple to it */
641  gistPushItupToNodeBuffer(gfbb, childNodeBuffer, itup);
642 
643  if (BUFFER_OVERFLOWED(childNodeBuffer, gfbb))
644  result = true;
645  }
646  else
647  {
648  /*
649  * We've reached a leaf page. Place the tuple here.
650  */
651  Assert(level == 0);
652  buffer = ReadBuffer(indexrel, blkno);
653  LockBuffer(buffer, GIST_EXCLUSIVE);
654  gistbufferinginserttuples(buildstate, buffer, level,
655  &itup, 1, InvalidOffsetNumber,
656  parentblkno, downlinkoffnum);
657  /* gistbufferinginserttuples() released the buffer */
658  }
659 
660  return result;
661 }
662 
663 /*
664  * Insert tuples to a given page.
665  *
666  * This is analogous with gistinserttuples() in the regular insertion code.
667  *
668  * Returns the block number of the page where the (first) new or updated tuple
669  * was inserted. Usually that's the original page, but might be a sibling page
670  * if the original page was split.
671  *
672  * Caller should hold a lock on 'buffer' on entry. This function will unlock
673  * and unpin it.
674  */
675 static BlockNumber
676 gistbufferinginserttuples(GISTBuildState *buildstate, Buffer buffer, int level,
677  IndexTuple *itup, int ntup, OffsetNumber oldoffnum,
678  BlockNumber parentblk, OffsetNumber downlinkoffnum)
679 {
680  GISTBuildBuffers *gfbb = buildstate->gfbb;
681  List *splitinfo;
682  bool is_split;
683  BlockNumber placed_to_blk = InvalidBlockNumber;
684 
685  is_split = gistplacetopage(buildstate->indexrel,
686  buildstate->freespace,
687  buildstate->giststate,
688  buffer,
689  itup, ntup, oldoffnum, &placed_to_blk,
691  &splitinfo,
692  false);
693 
694  /*
695  * If this is a root split, update the root path item kept in memory. This
696  * ensures that all path stacks are always complete, including all parent
697  * nodes up to the root. That simplifies the algorithm to re-find correct
698  * parent.
699  */
700  if (is_split && BufferGetBlockNumber(buffer) == GIST_ROOT_BLKNO)
701  {
702  Page page = BufferGetPage(buffer);
703  OffsetNumber off;
704  OffsetNumber maxoff;
705 
706  Assert(level == gfbb->rootlevel);
707  gfbb->rootlevel++;
708 
709  elog(DEBUG2, "splitting GiST root page, now %d levels deep", gfbb->rootlevel);
710 
711  /*
712  * All the downlinks on the old root page are now on one of the child
713  * pages. Visit all the new child pages to memorize the parents of the
714  * grandchildren.
715  */
716  if (gfbb->rootlevel > 1)
717  {
718  maxoff = PageGetMaxOffsetNumber(page);
719  for (off = FirstOffsetNumber; off <= maxoff; off++)
720  {
721  ItemId iid = PageGetItemId(page, off);
722  IndexTuple idxtuple = (IndexTuple) PageGetItem(page, iid);
723  BlockNumber childblkno = ItemPointerGetBlockNumber(&(idxtuple->t_tid));
724  Buffer childbuf = ReadBuffer(buildstate->indexrel, childblkno);
725 
726  LockBuffer(childbuf, GIST_SHARE);
727  gistMemorizeAllDownlinks(buildstate, childbuf);
728  UnlockReleaseBuffer(childbuf);
729 
730  /*
731  * Also remember that the parent of the new child page is the
732  * root block.
733  */
734  gistMemorizeParent(buildstate, childblkno, GIST_ROOT_BLKNO);
735  }
736  }
737  }
738 
739  if (splitinfo)
740  {
741  /*
742  * Insert the downlinks to the parent. This is analogous with
743  * gistfinishsplit() in the regular insertion code, but the locking is
744  * simpler, and we have to maintain the buffers on internal nodes and
745  * the parent map.
746  */
747  IndexTuple *downlinks;
748  int ndownlinks,
749  i;
750  Buffer parentBuffer;
751  ListCell *lc;
752 
753  /* Parent may have changed since we memorized this path. */
754  parentBuffer =
756  BufferGetBlockNumber(buffer),
757  level,
758  &parentblk,
759  &downlinkoffnum);
760 
761  /*
762  * If there's a buffer associated with this page, that needs to be
763  * split too. gistRelocateBuildBuffersOnSplit() will also adjust the
764  * downlinks in 'splitinfo', to make sure they're consistent not only
765  * with the tuples already on the pages, but also the tuples in the
766  * buffers that will eventually be inserted to them.
767  */
769  buildstate->giststate,
770  buildstate->indexrel,
771  level,
772  buffer, splitinfo);
773 
774  /* Create an array of all the downlink tuples */
775  ndownlinks = list_length(splitinfo);
776  downlinks = (IndexTuple *) palloc(sizeof(IndexTuple) * ndownlinks);
777  i = 0;
778  foreach(lc, splitinfo)
779  {
780  GISTPageSplitInfo *splitinfo = lfirst(lc);
781 
782  /*
783  * Remember the parent of each new child page in our parent map.
784  * This assumes that the downlinks fit on the parent page. If the
785  * parent page is split, too, when we recurse up to insert the
786  * downlinks, the recursive gistbufferinginserttuples() call will
787  * update the map again.
788  */
789  if (level > 0)
790  gistMemorizeParent(buildstate,
791  BufferGetBlockNumber(splitinfo->buf),
792  BufferGetBlockNumber(parentBuffer));
793 
794  /*
795  * Also update the parent map for all the downlinks that got moved
796  * to a different page. (actually this also loops through the
797  * downlinks that stayed on the original page, but it does no
798  * harm).
799  */
800  if (level > 1)
801  gistMemorizeAllDownlinks(buildstate, splitinfo->buf);
802 
803  /*
804  * Since there's no concurrent access, we can release the lower
805  * level buffers immediately. This includes the original page.
806  */
807  UnlockReleaseBuffer(splitinfo->buf);
808  downlinks[i++] = splitinfo->downlink;
809  }
810 
811  /* Insert them into parent. */
812  gistbufferinginserttuples(buildstate, parentBuffer, level + 1,
813  downlinks, ndownlinks, downlinkoffnum,
815 
816  list_free_deep(splitinfo); /* we don't need this anymore */
817  }
818  else
819  UnlockReleaseBuffer(buffer);
820 
821  return placed_to_blk;
822 }
823 
824 /*
825  * Find the downlink pointing to a child page.
826  *
827  * 'childblkno' indicates the child page to find the parent for. 'level' is
828  * the level of the child. On entry, *parentblkno and *downlinkoffnum can
829  * point to a location where the downlink used to be - we will check that
830  * location first, and save some cycles if it hasn't moved. The function
831  * returns a buffer containing the downlink, exclusively-locked, and
832  * *parentblkno and *downlinkoffnum are set to the real location of the
833  * downlink.
834  *
835  * If the child page is a leaf (level == 0), the caller must supply a correct
836  * parentblkno. Otherwise we use the parent map hash table to find the parent
837  * block.
838  *
839  * This function serves the same purpose as gistFindCorrectParent() during
840  * normal index inserts, but this is simpler because we don't need to deal
841  * with concurrent inserts.
842  */
843 static Buffer
845  BlockNumber childblkno, int level,
846  BlockNumber *parentblkno,
847  OffsetNumber *downlinkoffnum)
848 {
850  Buffer buffer;
851  Page page;
852  OffsetNumber maxoff;
853  OffsetNumber off;
854 
855  if (level > 0)
856  parent = gistGetParent(buildstate, childblkno);
857  else
858  {
859  /*
860  * For a leaf page, the caller must supply a correct parent block
861  * number.
862  */
863  if (*parentblkno == InvalidBlockNumber)
864  elog(ERROR, "no parent buffer provided of child %d", childblkno);
865  parent = *parentblkno;
866  }
867 
868  buffer = ReadBuffer(buildstate->indexrel, parent);
869  page = BufferGetPage(buffer);
870  LockBuffer(buffer, GIST_EXCLUSIVE);
871  gistcheckpage(buildstate->indexrel, buffer);
872  maxoff = PageGetMaxOffsetNumber(page);
873 
874  /* Check if it was not moved */
875  if (parent == *parentblkno && *parentblkno != InvalidBlockNumber &&
876  *downlinkoffnum != InvalidOffsetNumber && *downlinkoffnum <= maxoff)
877  {
878  ItemId iid = PageGetItemId(page, *downlinkoffnum);
879  IndexTuple idxtuple = (IndexTuple) PageGetItem(page, iid);
880 
881  if (ItemPointerGetBlockNumber(&(idxtuple->t_tid)) == childblkno)
882  {
883  /* Still there */
884  return buffer;
885  }
886  }
887 
888  /*
889  * Downlink was not at the offset where it used to be. Scan the page to
890  * find it. During normal gist insertions, it might've moved to another
891  * page, to the right, but during a buffering build, we keep track of the
892  * parent of each page in the lookup table so we should always know what
893  * page it's on.
894  */
895  for (off = FirstOffsetNumber; off <= maxoff; off = OffsetNumberNext(off))
896  {
897  ItemId iid = PageGetItemId(page, off);
898  IndexTuple idxtuple = (IndexTuple) PageGetItem(page, iid);
899 
900  if (ItemPointerGetBlockNumber(&(idxtuple->t_tid)) == childblkno)
901  {
902  /* yes!!, found it */
903  *downlinkoffnum = off;
904  return buffer;
905  }
906  }
907 
908  elog(ERROR, "failed to re-find parent for block %u", childblkno);
909  return InvalidBuffer; /* keep compiler quiet */
910 }
911 
912 /*
913  * Process buffers emptying stack. Emptying of one buffer can cause emptying
914  * of other buffers. This function iterates until this cascading emptying
915  * process finished, e.g. until buffers emptying stack is empty.
916  */
917 static void
919 {
920  GISTBuildBuffers *gfbb = buildstate->gfbb;
921 
922  /* Iterate while we have elements in buffers emptying stack. */
923  while (gfbb->bufferEmptyingQueue != NIL)
924  {
925  GISTNodeBuffer *emptyingNodeBuffer;
926 
927  /* Get node buffer from emptying stack. */
928  emptyingNodeBuffer = (GISTNodeBuffer *) linitial(gfbb->bufferEmptyingQueue);
930  emptyingNodeBuffer->queuedForEmptying = false;
931 
932  /*
933  * We are going to load last pages of buffers where emptying will be
934  * to. So let's unload any previously loaded buffers.
935  */
936  gistUnloadNodeBuffers(gfbb);
937 
938  /*
939  * Pop tuples from the buffer and run them down to the buffers at
940  * lower level, or leaf pages. We continue until one of the lower
941  * level buffers fills up, or this buffer runs empty.
942  *
943  * In Arge et al's paper, the buffer emptying is stopped after
944  * processing 1/2 node buffer worth of tuples, to avoid overfilling
945  * any of the lower level buffers. However, it's more efficient to
946  * keep going until one of the lower level buffers actually fills up,
947  * so that's what we do. This doesn't need to be exact, if a buffer
948  * overfills by a few tuples, there's no harm done.
949  */
950  while (true)
951  {
952  IndexTuple itup;
953 
954  /* Get next index tuple from the buffer */
955  if (!gistPopItupFromNodeBuffer(gfbb, emptyingNodeBuffer, &itup))
956  break;
957 
958  /*
959  * Run it down to the underlying node buffer or leaf page.
960  *
961  * Note: it's possible that the buffer we're emptying splits as a
962  * result of this call. If that happens, our emptyingNodeBuffer
963  * points to the left half of the split. After split, it's very
964  * likely that the new left buffer is no longer over the half-full
965  * threshold, but we might as well keep flushing tuples from it
966  * until we fill a lower-level buffer.
967  */
968  if (gistProcessItup(buildstate, itup, emptyingNodeBuffer->nodeBlocknum, emptyingNodeBuffer->level))
969  {
970  /*
971  * A lower level buffer filled up. Stop emptying this buffer,
972  * to avoid overflowing the lower level buffer.
973  */
974  break;
975  }
976 
977  /* Free all the memory allocated during index tuple processing */
978  MemoryContextReset(buildstate->giststate->tempCxt);
979  }
980  }
981 }
982 
983 /*
984  * Empty all node buffers, from top to bottom. This is done at the end of
985  * index build to flush all remaining tuples to the index.
986  *
987  * Note: This destroys the buffersOnLevels lists, so the buffers should not
988  * be inserted to after this call.
989  */
990 static void
992 {
993  GISTBuildBuffers *gfbb = buildstate->gfbb;
994  MemoryContext oldCtx;
995  int i;
996 
997  oldCtx = MemoryContextSwitchTo(buildstate->giststate->tempCxt);
998 
999  /*
1000  * Iterate through the levels from top to bottom.
1001  */
1002  for (i = gfbb->buffersOnLevelsLen - 1; i >= 0; i--)
1003  {
1004  /*
1005  * Empty all buffers on this level. Note that new buffers can pop up
1006  * in the list during the processing, as a result of page splits, so a
1007  * simple walk through the list won't work. We remove buffers from the
1008  * list when we see them empty; a buffer can't become non-empty once
1009  * it's been fully emptied.
1010  */
1011  while (gfbb->buffersOnLevels[i] != NIL)
1012  {
1013  GISTNodeBuffer *nodeBuffer;
1014 
1015  nodeBuffer = (GISTNodeBuffer *) linitial(gfbb->buffersOnLevels[i]);
1016 
1017  if (nodeBuffer->blocksCount != 0)
1018  {
1019  /*
1020  * Add this buffer to the emptying queue, and proceed to empty
1021  * the queue.
1022  */
1023  if (!nodeBuffer->queuedForEmptying)
1024  {
1026  nodeBuffer->queuedForEmptying = true;
1027  gfbb->bufferEmptyingQueue =
1028  lcons(nodeBuffer, gfbb->bufferEmptyingQueue);
1029  MemoryContextSwitchTo(buildstate->giststate->tempCxt);
1030  }
1031  gistProcessEmptyingQueue(buildstate);
1032  }
1033  else
1034  gfbb->buffersOnLevels[i] =
1036  }
1037  elog(DEBUG2, "emptied all buffers at level %d", i);
1038  }
1039  MemoryContextSwitchTo(oldCtx);
1040 }
1041 
1042 /*
1043  * Get the depth of the GiST index.
1044  */
1045 static int
1047 {
1048  int maxLevel;
1050 
1051  /*
1052  * Traverse down the tree, starting from the root, until we hit the leaf
1053  * level.
1054  */
1055  maxLevel = 0;
1056  blkno = GIST_ROOT_BLKNO;
1057  while (true)
1058  {
1059  Buffer buffer;
1060  Page page;
1061  IndexTuple itup;
1062 
1063  buffer = ReadBuffer(index, blkno);
1064 
1065  /*
1066  * There's no concurrent access during index build, so locking is just
1067  * pro forma.
1068  */
1069  LockBuffer(buffer, GIST_SHARE);
1070  page = (Page) BufferGetPage(buffer);
1071 
1072  if (GistPageIsLeaf(page))
1073  {
1074  /* We hit the bottom, so we're done. */
1075  UnlockReleaseBuffer(buffer);
1076  break;
1077  }
1078 
1079  /*
1080  * Pick the first downlink on the page, and follow it. It doesn't
1081  * matter which downlink we choose, the tree has the same depth
1082  * everywhere, so we just pick the first one.
1083  */
1084  itup = (IndexTuple) PageGetItem(page,
1086  blkno = ItemPointerGetBlockNumber(&(itup->t_tid));
1087  UnlockReleaseBuffer(buffer);
1088 
1089  /*
1090  * We're going down on the tree. It means that there is yet one more
1091  * level in the tree.
1092  */
1093  maxLevel++;
1094  }
1095  return maxLevel;
1096 }
1097 
1098 
1099 /*
1100  * Routines for managing the parent map.
1101  *
1102  * Whenever a page is split, we need to insert the downlinks into the parent.
1103  * We need to somehow find the parent page to do that. In normal insertions,
1104  * we keep a stack of nodes visited when we descend the tree. However, in
1105  * buffering build, we can start descending the tree from any internal node,
1106  * when we empty a buffer by cascading tuples to its children. So we don't
1107  * have a full stack up to the root available at that time.
1108  *
1109  * So instead, we maintain a hash table to track the parent of every internal
1110  * page. We don't need to track the parents of leaf nodes, however. Whenever
1111  * we insert to a leaf, we've just descended down from its parent, so we know
1112  * its immediate parent already. This helps a lot to limit the memory used
1113  * by this hash table.
1114  *
1115  * Whenever an internal node is split, the parent map needs to be updated.
1116  * the parent of the new child page needs to be recorded, and also the
1117  * entries for all page whose downlinks are moved to a new page at the split
1118  * needs to be updated.
1119  *
1120  * We also update the parent map whenever we descend the tree. That might seem
1121  * unnecessary, because we maintain the map whenever a downlink is moved or
1122  * created, but it is needed because we switch to buffering mode after
1123  * creating a tree with regular index inserts. Any pages created before
1124  * switching to buffering mode will not be present in the parent map initially,
1125  * but will be added there the first time we visit them.
1126  */
1127 
1128 typedef struct
1129 {
1130  BlockNumber childblkno; /* hash key */
1132 } ParentMapEntry;
1133 
1134 static void
1136 {
1137  HASHCTL hashCtl;
1138 
1139  hashCtl.keysize = sizeof(BlockNumber);
1140  hashCtl.entrysize = sizeof(ParentMapEntry);
1141  hashCtl.hcxt = CurrentMemoryContext;
1142  buildstate->parentMap = hash_create("gistbuild parent map",
1143  1024,
1144  &hashCtl,
1146 }
1147 
1148 static void
1150 {
1151  ParentMapEntry *entry;
1152  bool found;
1153 
1154  entry = (ParentMapEntry *) hash_search(buildstate->parentMap,
1155  (const void *) &child,
1156  HASH_ENTER,
1157  &found);
1158  entry->parentblkno = parent;
1159 }
1160 
1161 /*
1162  * Scan all downlinks on a page, and memorize their parent.
1163  */
1164 static void
1166 {
1167  OffsetNumber maxoff;
1168  OffsetNumber off;
1169  BlockNumber parentblkno = BufferGetBlockNumber(parentbuf);
1170  Page page = BufferGetPage(parentbuf);
1171 
1172  Assert(!GistPageIsLeaf(page));
1173 
1174  maxoff = PageGetMaxOffsetNumber(page);
1175  for (off = FirstOffsetNumber; off <= maxoff; off++)
1176  {
1177  ItemId iid = PageGetItemId(page, off);
1178  IndexTuple idxtuple = (IndexTuple) PageGetItem(page, iid);
1179  BlockNumber childblkno = ItemPointerGetBlockNumber(&(idxtuple->t_tid));
1180 
1181  gistMemorizeParent(buildstate, childblkno, parentblkno);
1182  }
1183 }
1184 
1185 static BlockNumber
1187 {
1188  ParentMapEntry *entry;
1189  bool found;
1190 
1191  /* Find node buffer in hash table */
1192  entry = (ParentMapEntry *) hash_search(buildstate->parentMap,
1193  (const void *) &child,
1194  HASH_FIND,
1195  &found);
1196  if (!found)
1197  elog(ERROR, "could not find parent of block %d in lookup table", child);
1198 
1199  return entry->parentblkno;
1200 }
void gistRelocateBuildBuffersOnSplit(GISTBuildBuffers *gfbb, GISTSTATE *giststate, Relation r, int level, Buffer buffer, List *splitinfo)
#define NIL
Definition: pg_list.h:69
static struct @76 value
#define XLOG_GIST_CREATE_INDEX
Definition: gist_private.h:191
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:203
#define DEBUG1
Definition: elog.h:25
static bool gistProcessItup(GISTBuildState *buildstate, IndexTuple itup, BlockNumber startblkno, int startlevel)
Definition: gistbuild.c:545
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
void gistValidateBufferingOption(char *value)
Definition: gistbuild.c:240
int bufferingModeOffset
Definition: gist_private.h:426
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1445
void gistPushItupToNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer, IndexTuple itup)
MemoryContext createTempGistContext(void)
Definition: gist.c:103
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
#define VARHDRSZ
Definition: c.h:440
struct SMgrRelationData * rd_smgr
Definition: rel.h:57
MemoryContext context
Definition: gist_private.h:377
ItemPointerData t_tid
Definition: itup.h:37
BlockNumber parentblkno
Definition: gistbuild.c:1131
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
List ** buffersOnLevels
Definition: gist_private.h:404
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static void gistProcessEmptyingQueue(GISTBuildState *buildstate)
Definition: gistbuild.c:918
int effective_cache_size
Definition: costsize.c:112
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
BlockNumber nodeBlocknum
Definition: gist_private.h:336
#define InvalidBuffer
Definition: buf.h:25
Size entrysize
Definition: hsearch.h:73
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
int errcode(int sqlerrcode)
Definition: elog.c:575
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:138
uint32 BlockNumber
Definition: block.h:31
BlockNumber childblkno
Definition: gistbuild.c:1130
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:887
void gistdoinsert(Relation r, IndexTuple itup, Size freespace, GISTSTATE *giststate)
Definition: gist.c:576
#define SizeOfPageHeaderData
Definition: bufpage.h:213
GistBufferingMode bufferingMode
Definition: gistbuild.c:73
static Buffer gistBufferingFindCorrectParent(GISTBuildState *buildstate, BlockNumber childblkno, int level, BlockNumber *parentblk, OffsetNumber *downlinkoffnum)
Definition: gistbuild.c:844
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
IndexTuple gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *giststate)
Definition: gistutil.c:310
void list_free_deep(List *list)
Definition: list.c:1147
int natts
Definition: tupdesc.h:73
XLogRecPtr gistGetFakeLSN(Relation rel)
Definition: gistutil.c:845
static int gistGetMaxLevel(Relation index)
Definition: gistbuild.c:1046
#define BUFFERING_MODE_SWITCH_CHECK_STEP
Definition: gistbuild.c:31
#define LEVEL_HAS_BUFFERS(nlevel, gfbb)
Definition: gist_private.h:355
uint16 OffsetNumber
Definition: off.h:24
Definition: type.h:90
int64 indtuplesSize
Definition: gistbuild.c:61
IndexBuildResult * gistbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: gistbuild.c:113
static BlockNumber gistbufferinginserttuples(GISTBuildState *buildstate, Buffer buffer, int level, IndexTuple *itup, int ntup, OffsetNumber oldoffnum, BlockNumber parentblk, OffsetNumber downlinkoffnum)
Definition: gistbuild.c:676
Definition: dynahash.c:193
HTAB * parentMap
Definition: gistbuild.c:71
#define linitial(l)
Definition: pg_list.h:110
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
#define ERROR
Definition: elog.h:43
int fillfactor
Definition: pgbench.c:111
List * bufferEmptyingQueue
Definition: gist_private.h:393
ItemPointerData t_self
Definition: htup.h:65
int64 indtuples
Definition: gistbuild.c:60
MemoryContext tempCxt
Definition: gist_private.h:80
#define DEBUG2
Definition: elog.h:24
BlockNumber blkno
Definition: ginvacuum.c:290
IndexTuple downlink
Definition: gist_private.h:447
bool gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate, Buffer buffer, IndexTuple *itup, int ntup, OffsetNumber oldoffnum, BlockNumber *newblkno, Buffer leftchildbuf, List **splitinfo, bool markfollowright)
Definition: gist.c:205
GISTBuildBuffers * gistInitBuildBuffers(int pagesPerBuffer, int levelStep, int maxLevel)
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
int errdetail(const char *fmt,...)
Definition: elog.c:873
GISTNodeBuffer * gistGetNodeBuffer(GISTBuildBuffers *gfbb, GISTSTATE *giststate, BlockNumber nodeBlocknum, int level)
Size freespace
Definition: gistbuild.c:63
#define RelationGetRelationName(relation)
Definition: rel.h:391
struct GISTPageOpaqueData GISTPageOpaqueData
GistBufferingMode
Definition: gistbuild.c:41
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
static int calculatePagesPerBuffer(GISTBuildState *buildstate, int levelStep)
Definition: gistbuild.c:424
#define GIST_DEFAULT_FILLFACTOR
Definition: gist_private.h:492
double rint(double x)
Definition: rint.c:22
#define BufferGetPage(buffer)
Definition: bufmgr.h:172
void freeGISTstate(GISTSTATE *giststate)
Definition: gist.c:1487
static BlockNumber gistGetParent(GISTBuildState *buildstate, BlockNumber child)
Definition: gistbuild.c:1186
#define ereport(elevel, rest)
Definition: elog.h:122
OffsetNumber gistchoose(Relation r, Page p, IndexTuple it, GISTSTATE *giststate)
Definition: gistutil.c:368
#define GistPageIsLeaf(page)
Definition: gist.h:132
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:408
#define HASH_BLOBS
Definition: hsearch.h:88
bool queuedForEmptying
Definition: gist_private.h:343
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
uintptr_t Datum
Definition: postgres.h:374
static void gistBufferingBuildInsert(GISTBuildState *buildstate, IndexTuple itup)
Definition: gistbuild.c:529
GISTSTATE * giststate
Definition: gistbuild.c:58
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
Size keysize
Definition: hsearch.h:72
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
TupleDesc rd_att
Definition: rel.h:84
void gistUnloadNodeBuffers(GISTBuildBuffers *gfbb)
GISTSTATE * initGISTstate(Relation index)
Definition: gist.c:1398
struct DataPageDeleteStack * child
Definition: ginvacuum.c:287
#define InvalidOffsetNumber
Definition: off.h:26
int maintenance_work_mem
Definition: globals.c:113
static void gistEmptyAllBuffers(GISTBuildState *buildstate)
Definition: gistbuild.c:991
List * lcons(void *datum, List *list)
Definition: list.c:259
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:672
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:667
#define lfirst(lc)
Definition: pg_list.h:106
Definition: regguts.h:313
struct DataPageDeleteStack * parent
Definition: ginvacuum.c:288
Relation indexrel
Definition: gistbuild.c:57
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:719
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
static void gistMemorizeParent(GISTBuildState *buildstate, BlockNumber child, BlockNumber parent)
Definition: gistbuild.c:1149
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
size_t Size
Definition: c.h:352
static void gistMemorizeAllDownlinks(GISTBuildState *buildstate, Buffer parent)
Definition: gistbuild.c:1165
#define InvalidBlockNumber
Definition: block.h:33
static int list_length(const List *l)
Definition: pg_list.h:89
#define MAXALIGN(LEN)
Definition: c.h:580
#define GIST_SHARE
Definition: gist_private.h:44
#define RelationNeedsWAL(relation)
Definition: rel.h:460
static Datum values[MAXATTR]
Definition: bootstrap.c:160
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2588
void gistFreeBuildBuffers(GISTBuildBuffers *gfbb)
double IndexBuildHeapScan(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool allow_sync, IndexBuildCallback callback, void *callback_state)
Definition: index.c:2154
void * palloc(Size size)
Definition: mcxt.c:894
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void gistBuildCallback(Relation index, HeapTuple htup, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
Definition: gistbuild.c:457
#define F_LEAF
Definition: gist.h:42
GISTBuildBuffers * gfbb
Definition: gistbuild.c:70
static void gistInitBuffering(GISTBuildState *buildstate)
Definition: gistbuild.c:263
int i
#define GIST_ROOT_BLKNO
Definition: gist_private.h:298
void GISTInitBuffer(Buffer b, uint32 f)
Definition: gistutil.c:697
#define BUFFERING_MODE_TUPLE_SIZE_STATS_TARGET
Definition: gistbuild.c:39
IndexTuple gistFormTuple(GISTSTATE *giststate, Relation r, Datum attdata[], bool isnull[], bool isleaf)
Definition: gistutil.c:563
#define BUFFER_OVERFLOWED(nodeBuffer, gfbb)
Definition: gist_private.h:368
static void gistInitParentMap(GISTBuildState *buildstate)
Definition: gistbuild.c:1135
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:218
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:70
bool gistPopItupFromNodeBuffer(GISTBuildBuffers *gfbb, GISTNodeBuffer *nodeBuffer, IndexTuple *itup)
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define GIST_EXCLUSIVE
Definition: gist_private.h:45
#define PageSetLSN(page, lsn)
Definition: bufpage.h:365
Definition: pg_list.h:45
int Buffer
Definition: buf.h:23
Buffer gistNewBuffer(Relation r)
Definition: gistutil.c:758
bytea * rd_options
Definition: rel.h:112
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
Pointer Page
Definition: bufpage.h:74
#define IndexTupleSize(itup)
Definition: itup.h:70
double index_tuples
Definition: genam.h:30
List * list_delete_first(List *list)
Definition: list.c:666
double heap_tuples
Definition: genam.h:29